Bug Summary

File:llvm/include/llvm/ADT/APInt.h
Warning:line 1000, column 15
Assigned value is garbage or undefined

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name DAGCombiner.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/build-llvm/lib/CodeGen/SelectionDAG -resource-dir /usr/lib/llvm-13/lib/clang/13.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/build-llvm/lib/CodeGen/SelectionDAG -I /build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG -I /build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/build-llvm/include -I /build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-13/lib/clang/13.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/build-llvm/lib/CodeGen/SelectionDAG -fdebug-prefix-map=/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c=. -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2021-07-26-235520-9401-1 -x c++ /build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

1//===- DAGCombiner.cpp - Implement a DAG node combiner --------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
10// both before and after the DAG is legalized.
11//
12// This pass is not a substitute for the LLVM IR instcombine pass. This pass is
13// primarily intended to handle simplification opportunities that are implicit
14// in the LLVM IR and exposed by the various codegen lowering phases.
15//
16//===----------------------------------------------------------------------===//
17
18#include "llvm/ADT/APFloat.h"
19#include "llvm/ADT/APInt.h"
20#include "llvm/ADT/ArrayRef.h"
21#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/IntervalMap.h"
23#include "llvm/ADT/None.h"
24#include "llvm/ADT/Optional.h"
25#include "llvm/ADT/STLExtras.h"
26#include "llvm/ADT/SetVector.h"
27#include "llvm/ADT/SmallBitVector.h"
28#include "llvm/ADT/SmallPtrSet.h"
29#include "llvm/ADT/SmallSet.h"
30#include "llvm/ADT/SmallVector.h"
31#include "llvm/ADT/Statistic.h"
32#include "llvm/Analysis/AliasAnalysis.h"
33#include "llvm/Analysis/MemoryLocation.h"
34#include "llvm/Analysis/TargetLibraryInfo.h"
35#include "llvm/Analysis/VectorUtils.h"
36#include "llvm/CodeGen/DAGCombine.h"
37#include "llvm/CodeGen/ISDOpcodes.h"
38#include "llvm/CodeGen/MachineFrameInfo.h"
39#include "llvm/CodeGen/MachineFunction.h"
40#include "llvm/CodeGen/MachineMemOperand.h"
41#include "llvm/CodeGen/RuntimeLibcalls.h"
42#include "llvm/CodeGen/SelectionDAG.h"
43#include "llvm/CodeGen/SelectionDAGAddressAnalysis.h"
44#include "llvm/CodeGen/SelectionDAGNodes.h"
45#include "llvm/CodeGen/SelectionDAGTargetInfo.h"
46#include "llvm/CodeGen/TargetLowering.h"
47#include "llvm/CodeGen/TargetRegisterInfo.h"
48#include "llvm/CodeGen/TargetSubtargetInfo.h"
49#include "llvm/CodeGen/ValueTypes.h"
50#include "llvm/IR/Attributes.h"
51#include "llvm/IR/Constant.h"
52#include "llvm/IR/DataLayout.h"
53#include "llvm/IR/DerivedTypes.h"
54#include "llvm/IR/Function.h"
55#include "llvm/IR/LLVMContext.h"
56#include "llvm/IR/Metadata.h"
57#include "llvm/Support/Casting.h"
58#include "llvm/Support/CodeGen.h"
59#include "llvm/Support/CommandLine.h"
60#include "llvm/Support/Compiler.h"
61#include "llvm/Support/Debug.h"
62#include "llvm/Support/ErrorHandling.h"
63#include "llvm/Support/KnownBits.h"
64#include "llvm/Support/MachineValueType.h"
65#include "llvm/Support/MathExtras.h"
66#include "llvm/Support/raw_ostream.h"
67#include "llvm/Target/TargetMachine.h"
68#include "llvm/Target/TargetOptions.h"
69#include <algorithm>
70#include <cassert>
71#include <cstdint>
72#include <functional>
73#include <iterator>
74#include <string>
75#include <tuple>
76#include <utility>
77
78using namespace llvm;
79
80#define DEBUG_TYPE"dagcombine" "dagcombine"
81
82STATISTIC(NodesCombined , "Number of dag nodes combined")static llvm::Statistic NodesCombined = {"dagcombine", "NodesCombined"
, "Number of dag nodes combined"}
;
83STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created")static llvm::Statistic PreIndexedNodes = {"dagcombine", "PreIndexedNodes"
, "Number of pre-indexed nodes created"}
;
84STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created")static llvm::Statistic PostIndexedNodes = {"dagcombine", "PostIndexedNodes"
, "Number of post-indexed nodes created"}
;
85STATISTIC(OpsNarrowed , "Number of load/op/store narrowed")static llvm::Statistic OpsNarrowed = {"dagcombine", "OpsNarrowed"
, "Number of load/op/store narrowed"}
;
86STATISTIC(LdStFP2Int , "Number of fp load/store pairs transformed to int")static llvm::Statistic LdStFP2Int = {"dagcombine", "LdStFP2Int"
, "Number of fp load/store pairs transformed to int"}
;
87STATISTIC(SlicedLoads, "Number of load sliced")static llvm::Statistic SlicedLoads = {"dagcombine", "SlicedLoads"
, "Number of load sliced"}
;
88STATISTIC(NumFPLogicOpsConv, "Number of logic ops converted to fp ops")static llvm::Statistic NumFPLogicOpsConv = {"dagcombine", "NumFPLogicOpsConv"
, "Number of logic ops converted to fp ops"}
;
89
90static cl::opt<bool>
91CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
92 cl::desc("Enable DAG combiner's use of IR alias analysis"));
93
94static cl::opt<bool>
95UseTBAA("combiner-use-tbaa", cl::Hidden, cl::init(true),
96 cl::desc("Enable DAG combiner's use of TBAA"));
97
98#ifndef NDEBUG
99static cl::opt<std::string>
100CombinerAAOnlyFunc("combiner-aa-only-func", cl::Hidden,
101 cl::desc("Only use DAG-combiner alias analysis in this"
102 " function"));
103#endif
104
105/// Hidden option to stress test load slicing, i.e., when this option
106/// is enabled, load slicing bypasses most of its profitability guards.
107static cl::opt<bool>
108StressLoadSlicing("combiner-stress-load-slicing", cl::Hidden,
109 cl::desc("Bypass the profitability model of load slicing"),
110 cl::init(false));
111
112static cl::opt<bool>
113 MaySplitLoadIndex("combiner-split-load-index", cl::Hidden, cl::init(true),
114 cl::desc("DAG combiner may split indexing from loads"));
115
116static cl::opt<bool>
117 EnableStoreMerging("combiner-store-merging", cl::Hidden, cl::init(true),
118 cl::desc("DAG combiner enable merging multiple stores "
119 "into a wider store"));
120
121static cl::opt<unsigned> TokenFactorInlineLimit(
122 "combiner-tokenfactor-inline-limit", cl::Hidden, cl::init(2048),
123 cl::desc("Limit the number of operands to inline for Token Factors"));
124
125static cl::opt<unsigned> StoreMergeDependenceLimit(
126 "combiner-store-merge-dependence-limit", cl::Hidden, cl::init(10),
127 cl::desc("Limit the number of times for the same StoreNode and RootNode "
128 "to bail out in store merging dependence check"));
129
130static cl::opt<bool> EnableReduceLoadOpStoreWidth(
131 "combiner-reduce-load-op-store-width", cl::Hidden, cl::init(true),
132 cl::desc("DAG cominber enable reducing the width of load/op/store "
133 "sequence"));
134
135static cl::opt<bool> EnableShrinkLoadReplaceStoreWithStore(
136 "combiner-shrink-load-replace-store-with-store", cl::Hidden, cl::init(true),
137 cl::desc("DAG cominber enable load/<replace bytes>/store with "
138 "a narrower store"));
139
140namespace {
141
142 class DAGCombiner {
143 SelectionDAG &DAG;
144 const TargetLowering &TLI;
145 const SelectionDAGTargetInfo *STI;
146 CombineLevel Level;
147 CodeGenOpt::Level OptLevel;
148 bool LegalDAG = false;
149 bool LegalOperations = false;
150 bool LegalTypes = false;
151 bool ForCodeSize;
152 bool DisableGenericCombines;
153
154 /// Worklist of all of the nodes that need to be simplified.
155 ///
156 /// This must behave as a stack -- new nodes to process are pushed onto the
157 /// back and when processing we pop off of the back.
158 ///
159 /// The worklist will not contain duplicates but may contain null entries
160 /// due to nodes being deleted from the underlying DAG.
161 SmallVector<SDNode *, 64> Worklist;
162
163 /// Mapping from an SDNode to its position on the worklist.
164 ///
165 /// This is used to find and remove nodes from the worklist (by nulling
166 /// them) when they are deleted from the underlying DAG. It relies on
167 /// stable indices of nodes within the worklist.
168 DenseMap<SDNode *, unsigned> WorklistMap;
169 /// This records all nodes attempted to add to the worklist since we
170 /// considered a new worklist entry. As we keep do not add duplicate nodes
171 /// in the worklist, this is different from the tail of the worklist.
172 SmallSetVector<SDNode *, 32> PruningList;
173
174 /// Set of nodes which have been combined (at least once).
175 ///
176 /// This is used to allow us to reliably add any operands of a DAG node
177 /// which have not yet been combined to the worklist.
178 SmallPtrSet<SDNode *, 32> CombinedNodes;
179
180 /// Map from candidate StoreNode to the pair of RootNode and count.
181 /// The count is used to track how many times we have seen the StoreNode
182 /// with the same RootNode bail out in dependence check. If we have seen
183 /// the bail out for the same pair many times over a limit, we won't
184 /// consider the StoreNode with the same RootNode as store merging
185 /// candidate again.
186 DenseMap<SDNode *, std::pair<SDNode *, unsigned>> StoreRootCountMap;
187
188 // AA - Used for DAG load/store alias analysis.
189 AliasAnalysis *AA;
190
191 /// When an instruction is simplified, add all users of the instruction to
192 /// the work lists because they might get more simplified now.
193 void AddUsersToWorklist(SDNode *N) {
194 for (SDNode *Node : N->uses())
195 AddToWorklist(Node);
196 }
197
198 /// Convenient shorthand to add a node and all of its user to the worklist.
199 void AddToWorklistWithUsers(SDNode *N) {
200 AddUsersToWorklist(N);
201 AddToWorklist(N);
202 }
203
204 // Prune potentially dangling nodes. This is called after
205 // any visit to a node, but should also be called during a visit after any
206 // failed combine which may have created a DAG node.
207 void clearAddedDanglingWorklistEntries() {
208 // Check any nodes added to the worklist to see if they are prunable.
209 while (!PruningList.empty()) {
210 auto *N = PruningList.pop_back_val();
211 if (N->use_empty())
212 recursivelyDeleteUnusedNodes(N);
213 }
214 }
215
216 SDNode *getNextWorklistEntry() {
217 // Before we do any work, remove nodes that are not in use.
218 clearAddedDanglingWorklistEntries();
219 SDNode *N = nullptr;
220 // The Worklist holds the SDNodes in order, but it may contain null
221 // entries.
222 while (!N && !Worklist.empty()) {
223 N = Worklist.pop_back_val();
224 }
225
226 if (N) {
227 bool GoodWorklistEntry = WorklistMap.erase(N);
228 (void)GoodWorklistEntry;
229 assert(GoodWorklistEntry &&(static_cast <bool> (GoodWorklistEntry && "Found a worklist entry without a corresponding map entry!"
) ? void (0) : __assert_fail ("GoodWorklistEntry && \"Found a worklist entry without a corresponding map entry!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 230, __extension__ __PRETTY_FUNCTION__))
230 "Found a worklist entry without a corresponding map entry!")(static_cast <bool> (GoodWorklistEntry && "Found a worklist entry without a corresponding map entry!"
) ? void (0) : __assert_fail ("GoodWorklistEntry && \"Found a worklist entry without a corresponding map entry!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 230, __extension__ __PRETTY_FUNCTION__))
;
231 }
232 return N;
233 }
234
235 /// Call the node-specific routine that folds each particular type of node.
236 SDValue visit(SDNode *N);
237
238 public:
239 DAGCombiner(SelectionDAG &D, AliasAnalysis *AA, CodeGenOpt::Level OL)
240 : DAG(D), TLI(D.getTargetLoweringInfo()),
241 STI(D.getSubtarget().getSelectionDAGInfo()),
242 Level(BeforeLegalizeTypes), OptLevel(OL), AA(AA) {
243 ForCodeSize = DAG.shouldOptForSize();
244 DisableGenericCombines = STI && STI->disableGenericCombines(OptLevel);
245
246 MaximumLegalStoreInBits = 0;
247 // We use the minimum store size here, since that's all we can guarantee
248 // for the scalable vector types.
249 for (MVT VT : MVT::all_valuetypes())
250 if (EVT(VT).isSimple() && VT != MVT::Other &&
251 TLI.isTypeLegal(EVT(VT)) &&
252 VT.getSizeInBits().getKnownMinSize() >= MaximumLegalStoreInBits)
253 MaximumLegalStoreInBits = VT.getSizeInBits().getKnownMinSize();
254 }
255
256 void ConsiderForPruning(SDNode *N) {
257 // Mark this for potential pruning.
258 PruningList.insert(N);
259 }
260
261 /// Add to the worklist making sure its instance is at the back (next to be
262 /// processed.)
263 void AddToWorklist(SDNode *N) {
264 assert(N->getOpcode() != ISD::DELETED_NODE &&(static_cast <bool> (N->getOpcode() != ISD::DELETED_NODE
&& "Deleted Node added to Worklist") ? void (0) : __assert_fail
("N->getOpcode() != ISD::DELETED_NODE && \"Deleted Node added to Worklist\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 265, __extension__ __PRETTY_FUNCTION__))
265 "Deleted Node added to Worklist")(static_cast <bool> (N->getOpcode() != ISD::DELETED_NODE
&& "Deleted Node added to Worklist") ? void (0) : __assert_fail
("N->getOpcode() != ISD::DELETED_NODE && \"Deleted Node added to Worklist\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 265, __extension__ __PRETTY_FUNCTION__))
;
266
267 // Skip handle nodes as they can't usefully be combined and confuse the
268 // zero-use deletion strategy.
269 if (N->getOpcode() == ISD::HANDLENODE)
270 return;
271
272 ConsiderForPruning(N);
273
274 if (WorklistMap.insert(std::make_pair(N, Worklist.size())).second)
275 Worklist.push_back(N);
276 }
277
278 /// Remove all instances of N from the worklist.
279 void removeFromWorklist(SDNode *N) {
280 CombinedNodes.erase(N);
281 PruningList.remove(N);
282 StoreRootCountMap.erase(N);
283
284 auto It = WorklistMap.find(N);
285 if (It == WorklistMap.end())
286 return; // Not in the worklist.
287
288 // Null out the entry rather than erasing it to avoid a linear operation.
289 Worklist[It->second] = nullptr;
290 WorklistMap.erase(It);
291 }
292
293 void deleteAndRecombine(SDNode *N);
294 bool recursivelyDeleteUnusedNodes(SDNode *N);
295
296 /// Replaces all uses of the results of one DAG node with new values.
297 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
298 bool AddTo = true);
299
300 /// Replaces all uses of the results of one DAG node with new values.
301 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
302 return CombineTo(N, &Res, 1, AddTo);
303 }
304
305 /// Replaces all uses of the results of one DAG node with new values.
306 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
307 bool AddTo = true) {
308 SDValue To[] = { Res0, Res1 };
309 return CombineTo(N, To, 2, AddTo);
310 }
311
312 void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO);
313
314 private:
315 unsigned MaximumLegalStoreInBits;
316
317 /// Check the specified integer node value to see if it can be simplified or
318 /// if things it uses can be simplified by bit propagation.
319 /// If so, return true.
320 bool SimplifyDemandedBits(SDValue Op) {
321 unsigned BitWidth = Op.getScalarValueSizeInBits();
322 APInt DemandedBits = APInt::getAllOnesValue(BitWidth);
323 return SimplifyDemandedBits(Op, DemandedBits);
324 }
325
326 bool SimplifyDemandedBits(SDValue Op, const APInt &DemandedBits) {
327 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
328 KnownBits Known;
329 if (!TLI.SimplifyDemandedBits(Op, DemandedBits, Known, TLO, 0, false))
330 return false;
331
332 // Revisit the node.
333 AddToWorklist(Op.getNode());
334
335 CommitTargetLoweringOpt(TLO);
336 return true;
337 }
338
339 /// Check the specified vector node value to see if it can be simplified or
340 /// if things it uses can be simplified as it only uses some of the
341 /// elements. If so, return true.
342 bool SimplifyDemandedVectorElts(SDValue Op) {
343 // TODO: For now just pretend it cannot be simplified.
344 if (Op.getValueType().isScalableVector())
345 return false;
346
347 unsigned NumElts = Op.getValueType().getVectorNumElements();
348 APInt DemandedElts = APInt::getAllOnesValue(NumElts);
349 return SimplifyDemandedVectorElts(Op, DemandedElts);
350 }
351
352 bool SimplifyDemandedBits(SDValue Op, const APInt &DemandedBits,
353 const APInt &DemandedElts,
354 bool AssumeSingleUse = false);
355 bool SimplifyDemandedVectorElts(SDValue Op, const APInt &DemandedElts,
356 bool AssumeSingleUse = false);
357
358 bool CombineToPreIndexedLoadStore(SDNode *N);
359 bool CombineToPostIndexedLoadStore(SDNode *N);
360 SDValue SplitIndexingFromLoad(LoadSDNode *LD);
361 bool SliceUpLoad(SDNode *N);
362
363 // Scalars have size 0 to distinguish from singleton vectors.
364 SDValue ForwardStoreValueToDirectLoad(LoadSDNode *LD);
365 bool getTruncatedStoreValue(StoreSDNode *ST, SDValue &Val);
366 bool extendLoadedValueToExtension(LoadSDNode *LD, SDValue &Val);
367
368 /// Replace an ISD::EXTRACT_VECTOR_ELT of a load with a narrowed
369 /// load.
370 ///
371 /// \param EVE ISD::EXTRACT_VECTOR_ELT to be replaced.
372 /// \param InVecVT type of the input vector to EVE with bitcasts resolved.
373 /// \param EltNo index of the vector element to load.
374 /// \param OriginalLoad load that EVE came from to be replaced.
375 /// \returns EVE on success SDValue() on failure.
376 SDValue scalarizeExtractedVectorLoad(SDNode *EVE, EVT InVecVT,
377 SDValue EltNo,
378 LoadSDNode *OriginalLoad);
379 void ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad);
380 SDValue PromoteOperand(SDValue Op, EVT PVT, bool &Replace);
381 SDValue SExtPromoteOperand(SDValue Op, EVT PVT);
382 SDValue ZExtPromoteOperand(SDValue Op, EVT PVT);
383 SDValue PromoteIntBinOp(SDValue Op);
384 SDValue PromoteIntShiftOp(SDValue Op);
385 SDValue PromoteExtend(SDValue Op);
386 bool PromoteLoad(SDValue Op);
387
388 /// Call the node-specific routine that knows how to fold each
389 /// particular type of node. If that doesn't do anything, try the
390 /// target-specific DAG combines.
391 SDValue combine(SDNode *N);
392
393 // Visitation implementation - Implement dag node combining for different
394 // node types. The semantics are as follows:
395 // Return Value:
396 // SDValue.getNode() == 0 - No change was made
397 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
398 // otherwise - N should be replaced by the returned Operand.
399 //
400 SDValue visitTokenFactor(SDNode *N);
401 SDValue visitMERGE_VALUES(SDNode *N);
402 SDValue visitADD(SDNode *N);
403 SDValue visitADDLike(SDNode *N);
404 SDValue visitADDLikeCommutative(SDValue N0, SDValue N1, SDNode *LocReference);
405 SDValue visitSUB(SDNode *N);
406 SDValue visitADDSAT(SDNode *N);
407 SDValue visitSUBSAT(SDNode *N);
408 SDValue visitADDC(SDNode *N);
409 SDValue visitADDO(SDNode *N);
410 SDValue visitUADDOLike(SDValue N0, SDValue N1, SDNode *N);
411 SDValue visitSUBC(SDNode *N);
412 SDValue visitSUBO(SDNode *N);
413 SDValue visitADDE(SDNode *N);
414 SDValue visitADDCARRY(SDNode *N);
415 SDValue visitSADDO_CARRY(SDNode *N);
416 SDValue visitADDCARRYLike(SDValue N0, SDValue N1, SDValue CarryIn, SDNode *N);
417 SDValue visitSUBE(SDNode *N);
418 SDValue visitSUBCARRY(SDNode *N);
419 SDValue visitSSUBO_CARRY(SDNode *N);
420 SDValue visitMUL(SDNode *N);
421 SDValue visitMULFIX(SDNode *N);
422 SDValue useDivRem(SDNode *N);
423 SDValue visitSDIV(SDNode *N);
424 SDValue visitSDIVLike(SDValue N0, SDValue N1, SDNode *N);
425 SDValue visitUDIV(SDNode *N);
426 SDValue visitUDIVLike(SDValue N0, SDValue N1, SDNode *N);
427 SDValue visitREM(SDNode *N);
428 SDValue visitMULHU(SDNode *N);
429 SDValue visitMULHS(SDNode *N);
430 SDValue visitSMUL_LOHI(SDNode *N);
431 SDValue visitUMUL_LOHI(SDNode *N);
432 SDValue visitMULO(SDNode *N);
433 SDValue visitIMINMAX(SDNode *N);
434 SDValue visitAND(SDNode *N);
435 SDValue visitANDLike(SDValue N0, SDValue N1, SDNode *N);
436 SDValue visitOR(SDNode *N);
437 SDValue visitORLike(SDValue N0, SDValue N1, SDNode *N);
438 SDValue visitXOR(SDNode *N);
439 SDValue SimplifyVBinOp(SDNode *N);
440 SDValue visitSHL(SDNode *N);
441 SDValue visitSRA(SDNode *N);
442 SDValue visitSRL(SDNode *N);
443 SDValue visitFunnelShift(SDNode *N);
444 SDValue visitRotate(SDNode *N);
445 SDValue visitABS(SDNode *N);
446 SDValue visitBSWAP(SDNode *N);
447 SDValue visitBITREVERSE(SDNode *N);
448 SDValue visitCTLZ(SDNode *N);
449 SDValue visitCTLZ_ZERO_UNDEF(SDNode *N);
450 SDValue visitCTTZ(SDNode *N);
451 SDValue visitCTTZ_ZERO_UNDEF(SDNode *N);
452 SDValue visitCTPOP(SDNode *N);
453 SDValue visitSELECT(SDNode *N);
454 SDValue visitVSELECT(SDNode *N);
455 SDValue visitSELECT_CC(SDNode *N);
456 SDValue visitSETCC(SDNode *N);
457 SDValue visitSETCCCARRY(SDNode *N);
458 SDValue visitSIGN_EXTEND(SDNode *N);
459 SDValue visitZERO_EXTEND(SDNode *N);
460 SDValue visitANY_EXTEND(SDNode *N);
461 SDValue visitAssertExt(SDNode *N);
462 SDValue visitAssertAlign(SDNode *N);
463 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
464 SDValue visitEXTEND_VECTOR_INREG(SDNode *N);
465 SDValue visitTRUNCATE(SDNode *N);
466 SDValue visitBITCAST(SDNode *N);
467 SDValue visitFREEZE(SDNode *N);
468 SDValue visitBUILD_PAIR(SDNode *N);
469 SDValue visitFADD(SDNode *N);
470 SDValue visitSTRICT_FADD(SDNode *N);
471 SDValue visitFSUB(SDNode *N);
472 SDValue visitFMUL(SDNode *N);
473 SDValue visitFMA(SDNode *N);
474 SDValue visitFDIV(SDNode *N);
475 SDValue visitFREM(SDNode *N);
476 SDValue visitFSQRT(SDNode *N);
477 SDValue visitFCOPYSIGN(SDNode *N);
478 SDValue visitFPOW(SDNode *N);
479 SDValue visitSINT_TO_FP(SDNode *N);
480 SDValue visitUINT_TO_FP(SDNode *N);
481 SDValue visitFP_TO_SINT(SDNode *N);
482 SDValue visitFP_TO_UINT(SDNode *N);
483 SDValue visitFP_ROUND(SDNode *N);
484 SDValue visitFP_EXTEND(SDNode *N);
485 SDValue visitFNEG(SDNode *N);
486 SDValue visitFABS(SDNode *N);
487 SDValue visitFCEIL(SDNode *N);
488 SDValue visitFTRUNC(SDNode *N);
489 SDValue visitFFLOOR(SDNode *N);
490 SDValue visitFMINNUM(SDNode *N);
491 SDValue visitFMAXNUM(SDNode *N);
492 SDValue visitFMINIMUM(SDNode *N);
493 SDValue visitFMAXIMUM(SDNode *N);
494 SDValue visitBRCOND(SDNode *N);
495 SDValue visitBR_CC(SDNode *N);
496 SDValue visitLOAD(SDNode *N);
497
498 SDValue replaceStoreChain(StoreSDNode *ST, SDValue BetterChain);
499 SDValue replaceStoreOfFPConstant(StoreSDNode *ST);
500
501 SDValue visitSTORE(SDNode *N);
502 SDValue visitLIFETIME_END(SDNode *N);
503 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
504 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
505 SDValue visitBUILD_VECTOR(SDNode *N);
506 SDValue visitCONCAT_VECTORS(SDNode *N);
507 SDValue visitEXTRACT_SUBVECTOR(SDNode *N);
508 SDValue visitVECTOR_SHUFFLE(SDNode *N);
509 SDValue visitSCALAR_TO_VECTOR(SDNode *N);
510 SDValue visitINSERT_SUBVECTOR(SDNode *N);
511 SDValue visitMLOAD(SDNode *N);
512 SDValue visitMSTORE(SDNode *N);
513 SDValue visitMGATHER(SDNode *N);
514 SDValue visitMSCATTER(SDNode *N);
515 SDValue visitFP_TO_FP16(SDNode *N);
516 SDValue visitFP16_TO_FP(SDNode *N);
517 SDValue visitVECREDUCE(SDNode *N);
518
519 SDValue visitFADDForFMACombine(SDNode *N);
520 SDValue visitFSUBForFMACombine(SDNode *N);
521 SDValue visitFMULForFMADistributiveCombine(SDNode *N);
522
523 SDValue XformToShuffleWithZero(SDNode *N);
524 bool reassociationCanBreakAddressingModePattern(unsigned Opc,
525 const SDLoc &DL, SDValue N0,
526 SDValue N1);
527 SDValue reassociateOpsCommutative(unsigned Opc, const SDLoc &DL, SDValue N0,
528 SDValue N1);
529 SDValue reassociateOps(unsigned Opc, const SDLoc &DL, SDValue N0,
530 SDValue N1, SDNodeFlags Flags);
531
532 SDValue visitShiftByConstant(SDNode *N);
533
534 SDValue foldSelectOfConstants(SDNode *N);
535 SDValue foldVSelectOfConstants(SDNode *N);
536 SDValue foldBinOpIntoSelect(SDNode *BO);
537 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
538 SDValue hoistLogicOpWithSameOpcodeHands(SDNode *N);
539 SDValue SimplifySelect(const SDLoc &DL, SDValue N0, SDValue N1, SDValue N2);
540 SDValue SimplifySelectCC(const SDLoc &DL, SDValue N0, SDValue N1,
541 SDValue N2, SDValue N3, ISD::CondCode CC,
542 bool NotExtCompare = false);
543 SDValue convertSelectOfFPConstantsToLoadOffset(
544 const SDLoc &DL, SDValue N0, SDValue N1, SDValue N2, SDValue N3,
545 ISD::CondCode CC);
546 SDValue foldSignChangeInBitcast(SDNode *N);
547 SDValue foldSelectCCToShiftAnd(const SDLoc &DL, SDValue N0, SDValue N1,
548 SDValue N2, SDValue N3, ISD::CondCode CC);
549 SDValue foldSelectOfBinops(SDNode *N);
550 SDValue foldSextSetcc(SDNode *N);
551 SDValue foldLogicOfSetCCs(bool IsAnd, SDValue N0, SDValue N1,
552 const SDLoc &DL);
553 SDValue foldSubToUSubSat(EVT DstVT, SDNode *N);
554 SDValue unfoldMaskedMerge(SDNode *N);
555 SDValue unfoldExtremeBitClearingToShifts(SDNode *N);
556 SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
557 const SDLoc &DL, bool foldBooleans);
558 SDValue rebuildSetCC(SDValue N);
559
560 bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
561 SDValue &CC, bool MatchStrict = false) const;
562 bool isOneUseSetCC(SDValue N) const;
563
564 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
565 unsigned HiOp);
566 SDValue CombineConsecutiveLoads(SDNode *N, EVT VT);
567 SDValue CombineExtLoad(SDNode *N);
568 SDValue CombineZExtLogicopShiftLoad(SDNode *N);
569 SDValue combineRepeatedFPDivisors(SDNode *N);
570 SDValue combineInsertEltToShuffle(SDNode *N, unsigned InsIndex);
571 SDValue ConstantFoldBITCASTofBUILD_VECTOR(SDNode *, EVT);
572 SDValue BuildSDIV(SDNode *N);
573 SDValue BuildSDIVPow2(SDNode *N);
574 SDValue BuildUDIV(SDNode *N);
575 SDValue BuildLogBase2(SDValue V, const SDLoc &DL);
576 SDValue BuildDivEstimate(SDValue N, SDValue Op, SDNodeFlags Flags);
577 SDValue buildRsqrtEstimate(SDValue Op, SDNodeFlags Flags);
578 SDValue buildSqrtEstimate(SDValue Op, SDNodeFlags Flags);
579 SDValue buildSqrtEstimateImpl(SDValue Op, SDNodeFlags Flags, bool Recip);
580 SDValue buildSqrtNROneConst(SDValue Arg, SDValue Est, unsigned Iterations,
581 SDNodeFlags Flags, bool Reciprocal);
582 SDValue buildSqrtNRTwoConst(SDValue Arg, SDValue Est, unsigned Iterations,
583 SDNodeFlags Flags, bool Reciprocal);
584 SDValue MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
585 bool DemandHighBits = true);
586 SDValue MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1);
587 SDValue MatchRotatePosNeg(SDValue Shifted, SDValue Pos, SDValue Neg,
588 SDValue InnerPos, SDValue InnerNeg,
589 unsigned PosOpcode, unsigned NegOpcode,
590 const SDLoc &DL);
591 SDValue MatchFunnelPosNeg(SDValue N0, SDValue N1, SDValue Pos, SDValue Neg,
592 SDValue InnerPos, SDValue InnerNeg,
593 unsigned PosOpcode, unsigned NegOpcode,
594 const SDLoc &DL);
595 SDValue MatchRotate(SDValue LHS, SDValue RHS, const SDLoc &DL);
596 SDValue MatchLoadCombine(SDNode *N);
597 SDValue mergeTruncStores(StoreSDNode *N);
598 SDValue ReduceLoadWidth(SDNode *N);
599 SDValue ReduceLoadOpStoreWidth(SDNode *N);
600 SDValue splitMergedValStore(StoreSDNode *ST);
601 SDValue TransformFPLoadStorePair(SDNode *N);
602 SDValue convertBuildVecZextToZext(SDNode *N);
603 SDValue reduceBuildVecExtToExtBuildVec(SDNode *N);
604 SDValue reduceBuildVecTruncToBitCast(SDNode *N);
605 SDValue reduceBuildVecToShuffle(SDNode *N);
606 SDValue createBuildVecShuffle(const SDLoc &DL, SDNode *N,
607 ArrayRef<int> VectorMask, SDValue VecIn1,
608 SDValue VecIn2, unsigned LeftIdx,
609 bool DidSplitVec);
610 SDValue matchVSelectOpSizesWithSetCC(SDNode *Cast);
611
612 /// Walk up chain skipping non-aliasing memory nodes,
613 /// looking for aliasing nodes and adding them to the Aliases vector.
614 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
615 SmallVectorImpl<SDValue> &Aliases);
616
617 /// Return true if there is any possibility that the two addresses overlap.
618 bool isAlias(SDNode *Op0, SDNode *Op1) const;
619
620 /// Walk up chain skipping non-aliasing memory nodes, looking for a better
621 /// chain (aliasing node.)
622 SDValue FindBetterChain(SDNode *N, SDValue Chain);
623
624 /// Try to replace a store and any possibly adjacent stores on
625 /// consecutive chains with better chains. Return true only if St is
626 /// replaced.
627 ///
628 /// Notice that other chains may still be replaced even if the function
629 /// returns false.
630 bool findBetterNeighborChains(StoreSDNode *St);
631
632 // Helper for findBetterNeighborChains. Walk up store chain add additional
633 // chained stores that do not overlap and can be parallelized.
634 bool parallelizeChainedStores(StoreSDNode *St);
635
636 /// Holds a pointer to an LSBaseSDNode as well as information on where it
637 /// is located in a sequence of memory operations connected by a chain.
638 struct MemOpLink {
639 // Ptr to the mem node.
640 LSBaseSDNode *MemNode;
641
642 // Offset from the base ptr.
643 int64_t OffsetFromBase;
644
645 MemOpLink(LSBaseSDNode *N, int64_t Offset)
646 : MemNode(N), OffsetFromBase(Offset) {}
647 };
648
649 // Classify the origin of a stored value.
650 enum class StoreSource { Unknown, Constant, Extract, Load };
651 StoreSource getStoreSource(SDValue StoreVal) {
652 switch (StoreVal.getOpcode()) {
653 case ISD::Constant:
654 case ISD::ConstantFP:
655 return StoreSource::Constant;
656 case ISD::EXTRACT_VECTOR_ELT:
657 case ISD::EXTRACT_SUBVECTOR:
658 return StoreSource::Extract;
659 case ISD::LOAD:
660 return StoreSource::Load;
661 default:
662 return StoreSource::Unknown;
663 }
664 }
665
666 /// This is a helper function for visitMUL to check the profitability
667 /// of folding (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2).
668 /// MulNode is the original multiply, AddNode is (add x, c1),
669 /// and ConstNode is c2.
670 bool isMulAddWithConstProfitable(SDNode *MulNode,
671 SDValue &AddNode,
672 SDValue &ConstNode);
673
674 /// This is a helper function for visitAND and visitZERO_EXTEND. Returns
675 /// true if the (and (load x) c) pattern matches an extload. ExtVT returns
676 /// the type of the loaded value to be extended.
677 bool isAndLoadExtLoad(ConstantSDNode *AndC, LoadSDNode *LoadN,
678 EVT LoadResultTy, EVT &ExtVT);
679
680 /// Helper function to calculate whether the given Load/Store can have its
681 /// width reduced to ExtVT.
682 bool isLegalNarrowLdSt(LSBaseSDNode *LDSTN, ISD::LoadExtType ExtType,
683 EVT &MemVT, unsigned ShAmt = 0);
684
685 /// Used by BackwardsPropagateMask to find suitable loads.
686 bool SearchForAndLoads(SDNode *N, SmallVectorImpl<LoadSDNode*> &Loads,
687 SmallPtrSetImpl<SDNode*> &NodesWithConsts,
688 ConstantSDNode *Mask, SDNode *&NodeToMask);
689 /// Attempt to propagate a given AND node back to load leaves so that they
690 /// can be combined into narrow loads.
691 bool BackwardsPropagateMask(SDNode *N);
692
693 /// Helper function for mergeConsecutiveStores which merges the component
694 /// store chains.
695 SDValue getMergeStoreChains(SmallVectorImpl<MemOpLink> &StoreNodes,
696 unsigned NumStores);
697
698 /// This is a helper function for mergeConsecutiveStores. When the source
699 /// elements of the consecutive stores are all constants or all extracted
700 /// vector elements, try to merge them into one larger store introducing
701 /// bitcasts if necessary. \return True if a merged store was created.
702 bool mergeStoresOfConstantsOrVecElts(SmallVectorImpl<MemOpLink> &StoreNodes,
703 EVT MemVT, unsigned NumStores,
704 bool IsConstantSrc, bool UseVector,
705 bool UseTrunc);
706
707 /// This is a helper function for mergeConsecutiveStores. Stores that
708 /// potentially may be merged with St are placed in StoreNodes. RootNode is
709 /// a chain predecessor to all store candidates.
710 void getStoreMergeCandidates(StoreSDNode *St,
711 SmallVectorImpl<MemOpLink> &StoreNodes,
712 SDNode *&Root);
713
714 /// Helper function for mergeConsecutiveStores. Checks if candidate stores
715 /// have indirect dependency through their operands. RootNode is the
716 /// predecessor to all stores calculated by getStoreMergeCandidates and is
717 /// used to prune the dependency check. \return True if safe to merge.
718 bool checkMergeStoreCandidatesForDependencies(
719 SmallVectorImpl<MemOpLink> &StoreNodes, unsigned NumStores,
720 SDNode *RootNode);
721
722 /// This is a helper function for mergeConsecutiveStores. Given a list of
723 /// store candidates, find the first N that are consecutive in memory.
724 /// Returns 0 if there are not at least 2 consecutive stores to try merging.
725 unsigned getConsecutiveStores(SmallVectorImpl<MemOpLink> &StoreNodes,
726 int64_t ElementSizeBytes) const;
727
728 /// This is a helper function for mergeConsecutiveStores. It is used for
729 /// store chains that are composed entirely of constant values.
730 bool tryStoreMergeOfConstants(SmallVectorImpl<MemOpLink> &StoreNodes,
731 unsigned NumConsecutiveStores,
732 EVT MemVT, SDNode *Root, bool AllowVectors);
733
734 /// This is a helper function for mergeConsecutiveStores. It is used for
735 /// store chains that are composed entirely of extracted vector elements.
736 /// When extracting multiple vector elements, try to store them in one
737 /// vector store rather than a sequence of scalar stores.
738 bool tryStoreMergeOfExtracts(SmallVectorImpl<MemOpLink> &StoreNodes,
739 unsigned NumConsecutiveStores, EVT MemVT,
740 SDNode *Root);
741
742 /// This is a helper function for mergeConsecutiveStores. It is used for
743 /// store chains that are composed entirely of loaded values.
744 bool tryStoreMergeOfLoads(SmallVectorImpl<MemOpLink> &StoreNodes,
745 unsigned NumConsecutiveStores, EVT MemVT,
746 SDNode *Root, bool AllowVectors,
747 bool IsNonTemporalStore, bool IsNonTemporalLoad);
748
749 /// Merge consecutive store operations into a wide store.
750 /// This optimization uses wide integers or vectors when possible.
751 /// \return true if stores were merged.
752 bool mergeConsecutiveStores(StoreSDNode *St);
753
754 /// Try to transform a truncation where C is a constant:
755 /// (trunc (and X, C)) -> (and (trunc X), (trunc C))
756 ///
757 /// \p N needs to be a truncation and its first operand an AND. Other
758 /// requirements are checked by the function (e.g. that trunc is
759 /// single-use) and if missed an empty SDValue is returned.
760 SDValue distributeTruncateThroughAnd(SDNode *N);
761
762 /// Helper function to determine whether the target supports operation
763 /// given by \p Opcode for type \p VT, that is, whether the operation
764 /// is legal or custom before legalizing operations, and whether is
765 /// legal (but not custom) after legalization.
766 bool hasOperation(unsigned Opcode, EVT VT) {
767 return TLI.isOperationLegalOrCustom(Opcode, VT, LegalOperations);
768 }
769
770 public:
771 /// Runs the dag combiner on all nodes in the work list
772 void Run(CombineLevel AtLevel);
773
774 SelectionDAG &getDAG() const { return DAG; }
775
776 /// Returns a type large enough to hold any valid shift amount - before type
777 /// legalization these can be huge.
778 EVT getShiftAmountTy(EVT LHSTy) {
779 assert(LHSTy.isInteger() && "Shift amount is not an integer type!")(static_cast <bool> (LHSTy.isInteger() && "Shift amount is not an integer type!"
) ? void (0) : __assert_fail ("LHSTy.isInteger() && \"Shift amount is not an integer type!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 779, __extension__ __PRETTY_FUNCTION__))
;
780 return TLI.getShiftAmountTy(LHSTy, DAG.getDataLayout(), LegalTypes);
781 }
782
783 /// This method returns true if we are running before type legalization or
784 /// if the specified VT is legal.
785 bool isTypeLegal(const EVT &VT) {
786 if (!LegalTypes) return true;
787 return TLI.isTypeLegal(VT);
788 }
789
790 /// Convenience wrapper around TargetLowering::getSetCCResultType
791 EVT getSetCCResultType(EVT VT) const {
792 return TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
793 }
794
795 void ExtendSetCCUses(const SmallVectorImpl<SDNode *> &SetCCs,
796 SDValue OrigLoad, SDValue ExtLoad,
797 ISD::NodeType ExtType);
798 };
799
800/// This class is a DAGUpdateListener that removes any deleted
801/// nodes from the worklist.
802class WorklistRemover : public SelectionDAG::DAGUpdateListener {
803 DAGCombiner &DC;
804
805public:
806 explicit WorklistRemover(DAGCombiner &dc)
807 : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {}
808
809 void NodeDeleted(SDNode *N, SDNode *E) override {
810 DC.removeFromWorklist(N);
811 }
812};
813
814class WorklistInserter : public SelectionDAG::DAGUpdateListener {
815 DAGCombiner &DC;
816
817public:
818 explicit WorklistInserter(DAGCombiner &dc)
819 : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {}
820
821 // FIXME: Ideally we could add N to the worklist, but this causes exponential
822 // compile time costs in large DAGs, e.g. Halide.
823 void NodeInserted(SDNode *N) override { DC.ConsiderForPruning(N); }
824};
825
826} // end anonymous namespace
827
828//===----------------------------------------------------------------------===//
829// TargetLowering::DAGCombinerInfo implementation
830//===----------------------------------------------------------------------===//
831
832void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
833 ((DAGCombiner*)DC)->AddToWorklist(N);
834}
835
836SDValue TargetLowering::DAGCombinerInfo::
837CombineTo(SDNode *N, ArrayRef<SDValue> To, bool AddTo) {
838 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo);
839}
840
841SDValue TargetLowering::DAGCombinerInfo::
842CombineTo(SDNode *N, SDValue Res, bool AddTo) {
843 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo);
844}
845
846SDValue TargetLowering::DAGCombinerInfo::
847CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) {
848 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo);
849}
850
851bool TargetLowering::DAGCombinerInfo::
852recursivelyDeleteUnusedNodes(SDNode *N) {
853 return ((DAGCombiner*)DC)->recursivelyDeleteUnusedNodes(N);
854}
855
856void TargetLowering::DAGCombinerInfo::
857CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
858 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
859}
860
861//===----------------------------------------------------------------------===//
862// Helper Functions
863//===----------------------------------------------------------------------===//
864
865void DAGCombiner::deleteAndRecombine(SDNode *N) {
866 removeFromWorklist(N);
867
868 // If the operands of this node are only used by the node, they will now be
869 // dead. Make sure to re-visit them and recursively delete dead nodes.
870 for (const SDValue &Op : N->ops())
871 // For an operand generating multiple values, one of the values may
872 // become dead allowing further simplification (e.g. split index
873 // arithmetic from an indexed load).
874 if (Op->hasOneUse() || Op->getNumValues() > 1)
875 AddToWorklist(Op.getNode());
876
877 DAG.DeleteNode(N);
878}
879
880// APInts must be the same size for most operations, this helper
881// function zero extends the shorter of the pair so that they match.
882// We provide an Offset so that we can create bitwidths that won't overflow.
883static void zeroExtendToMatch(APInt &LHS, APInt &RHS, unsigned Offset = 0) {
884 unsigned Bits = Offset + std::max(LHS.getBitWidth(), RHS.getBitWidth());
885 LHS = LHS.zextOrSelf(Bits);
886 RHS = RHS.zextOrSelf(Bits);
887}
888
889// Return true if this node is a setcc, or is a select_cc
890// that selects between the target values used for true and false, making it
891// equivalent to a setcc. Also, set the incoming LHS, RHS, and CC references to
892// the appropriate nodes based on the type of node we are checking. This
893// simplifies life a bit for the callers.
894bool DAGCombiner::isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
895 SDValue &CC, bool MatchStrict) const {
896 if (N.getOpcode() == ISD::SETCC) {
897 LHS = N.getOperand(0);
898 RHS = N.getOperand(1);
899 CC = N.getOperand(2);
900 return true;
901 }
902
903 if (MatchStrict &&
904 (N.getOpcode() == ISD::STRICT_FSETCC ||
905 N.getOpcode() == ISD::STRICT_FSETCCS)) {
906 LHS = N.getOperand(1);
907 RHS = N.getOperand(2);
908 CC = N.getOperand(3);
909 return true;
910 }
911
912 if (N.getOpcode() != ISD::SELECT_CC ||
913 !TLI.isConstTrueVal(N.getOperand(2).getNode()) ||
914 !TLI.isConstFalseVal(N.getOperand(3).getNode()))
915 return false;
916
917 if (TLI.getBooleanContents(N.getValueType()) ==
918 TargetLowering::UndefinedBooleanContent)
919 return false;
920
921 LHS = N.getOperand(0);
922 RHS = N.getOperand(1);
923 CC = N.getOperand(4);
924 return true;
925}
926
927/// Return true if this is a SetCC-equivalent operation with only one use.
928/// If this is true, it allows the users to invert the operation for free when
929/// it is profitable to do so.
930bool DAGCombiner::isOneUseSetCC(SDValue N) const {
931 SDValue N0, N1, N2;
932 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
933 return true;
934 return false;
935}
936
937static bool isConstantSplatVectorMaskForType(SDNode *N, EVT ScalarTy) {
938 if (!ScalarTy.isSimple())
939 return false;
940
941 uint64_t MaskForTy = 0ULL;
942 switch (ScalarTy.getSimpleVT().SimpleTy) {
943 case MVT::i8:
944 MaskForTy = 0xFFULL;
945 break;
946 case MVT::i16:
947 MaskForTy = 0xFFFFULL;
948 break;
949 case MVT::i32:
950 MaskForTy = 0xFFFFFFFFULL;
951 break;
952 default:
953 return false;
954 break;
955 }
956
957 APInt Val;
958 if (ISD::isConstantSplatVector(N, Val))
959 return Val.getLimitedValue() == MaskForTy;
960
961 return false;
962}
963
964// Determines if it is a constant integer or a splat/build vector of constant
965// integers (and undefs).
966// Do not permit build vector implicit truncation.
967static bool isConstantOrConstantVector(SDValue N, bool NoOpaques = false) {
968 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N))
969 return !(Const->isOpaque() && NoOpaques);
970 if (N.getOpcode() != ISD::BUILD_VECTOR && N.getOpcode() != ISD::SPLAT_VECTOR)
971 return false;
972 unsigned BitWidth = N.getScalarValueSizeInBits();
973 for (const SDValue &Op : N->op_values()) {
974 if (Op.isUndef())
975 continue;
976 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(Op);
977 if (!Const || Const->getAPIntValue().getBitWidth() != BitWidth ||
978 (Const->isOpaque() && NoOpaques))
979 return false;
980 }
981 return true;
982}
983
984// Determines if a BUILD_VECTOR is composed of all-constants possibly mixed with
985// undef's.
986static bool isAnyConstantBuildVector(SDValue V, bool NoOpaques = false) {
987 if (V.getOpcode() != ISD::BUILD_VECTOR)
988 return false;
989 return isConstantOrConstantVector(V, NoOpaques) ||
990 ISD::isBuildVectorOfConstantFPSDNodes(V.getNode());
991}
992
993// Determine if this an indexed load with an opaque target constant index.
994static bool canSplitIdx(LoadSDNode *LD) {
995 return MaySplitLoadIndex &&
996 (LD->getOperand(2).getOpcode() != ISD::TargetConstant ||
997 !cast<ConstantSDNode>(LD->getOperand(2))->isOpaque());
998}
999
1000bool DAGCombiner::reassociationCanBreakAddressingModePattern(unsigned Opc,
1001 const SDLoc &DL,
1002 SDValue N0,
1003 SDValue N1) {
1004 // Currently this only tries to ensure we don't undo the GEP splits done by
1005 // CodeGenPrepare when shouldConsiderGEPOffsetSplit is true. To ensure this,
1006 // we check if the following transformation would be problematic:
1007 // (load/store (add, (add, x, offset1), offset2)) ->
1008 // (load/store (add, x, offset1+offset2)).
1009
1010 if (Opc != ISD::ADD || N0.getOpcode() != ISD::ADD)
1011 return false;
1012
1013 if (N0.hasOneUse())
1014 return false;
1015
1016 auto *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1017 auto *C2 = dyn_cast<ConstantSDNode>(N1);
1018 if (!C1 || !C2)
1019 return false;
1020
1021 const APInt &C1APIntVal = C1->getAPIntValue();
1022 const APInt &C2APIntVal = C2->getAPIntValue();
1023 if (C1APIntVal.getBitWidth() > 64 || C2APIntVal.getBitWidth() > 64)
1024 return false;
1025
1026 const APInt CombinedValueIntVal = C1APIntVal + C2APIntVal;
1027 if (CombinedValueIntVal.getBitWidth() > 64)
1028 return false;
1029 const int64_t CombinedValue = CombinedValueIntVal.getSExtValue();
1030
1031 for (SDNode *Node : N0->uses()) {
1032 auto LoadStore = dyn_cast<MemSDNode>(Node);
1033 if (LoadStore) {
1034 // Is x[offset2] already not a legal addressing mode? If so then
1035 // reassociating the constants breaks nothing (we test offset2 because
1036 // that's the one we hope to fold into the load or store).
1037 TargetLoweringBase::AddrMode AM;
1038 AM.HasBaseReg = true;
1039 AM.BaseOffs = C2APIntVal.getSExtValue();
1040 EVT VT = LoadStore->getMemoryVT();
1041 unsigned AS = LoadStore->getAddressSpace();
1042 Type *AccessTy = VT.getTypeForEVT(*DAG.getContext());
1043 if (!TLI.isLegalAddressingMode(DAG.getDataLayout(), AM, AccessTy, AS))
1044 continue;
1045
1046 // Would x[offset1+offset2] still be a legal addressing mode?
1047 AM.BaseOffs = CombinedValue;
1048 if (!TLI.isLegalAddressingMode(DAG.getDataLayout(), AM, AccessTy, AS))
1049 return true;
1050 }
1051 }
1052
1053 return false;
1054}
1055
1056// Helper for DAGCombiner::reassociateOps. Try to reassociate an expression
1057// such as (Opc N0, N1), if \p N0 is the same kind of operation as \p Opc.
1058SDValue DAGCombiner::reassociateOpsCommutative(unsigned Opc, const SDLoc &DL,
1059 SDValue N0, SDValue N1) {
1060 EVT VT = N0.getValueType();
1061
1062 if (N0.getOpcode() != Opc)
1063 return SDValue();
1064
1065 if (DAG.isConstantIntBuildVectorOrConstantInt(N0.getOperand(1))) {
1066 if (DAG.isConstantIntBuildVectorOrConstantInt(N1)) {
1067 // Reassociate: (op (op x, c1), c2) -> (op x, (op c1, c2))
1068 if (SDValue OpNode =
1069 DAG.FoldConstantArithmetic(Opc, DL, VT, {N0.getOperand(1), N1}))
1070 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
1071 return SDValue();
1072 }
1073 if (N0.hasOneUse()) {
1074 // Reassociate: (op (op x, c1), y) -> (op (op x, y), c1)
1075 // iff (op x, c1) has one use
1076 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT, N0.getOperand(0), N1);
1077 if (!OpNode.getNode())
1078 return SDValue();
1079 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
1080 }
1081 }
1082 return SDValue();
1083}
1084
1085// Try to reassociate commutative binops.
1086SDValue DAGCombiner::reassociateOps(unsigned Opc, const SDLoc &DL, SDValue N0,
1087 SDValue N1, SDNodeFlags Flags) {
1088 assert(TLI.isCommutativeBinOp(Opc) && "Operation not commutative.")(static_cast <bool> (TLI.isCommutativeBinOp(Opc) &&
"Operation not commutative.") ? void (0) : __assert_fail ("TLI.isCommutativeBinOp(Opc) && \"Operation not commutative.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 1088, __extension__ __PRETTY_FUNCTION__))
;
1089
1090 // Floating-point reassociation is not allowed without loose FP math.
1091 if (N0.getValueType().isFloatingPoint() ||
1092 N1.getValueType().isFloatingPoint())
1093 if (!Flags.hasAllowReassociation() || !Flags.hasNoSignedZeros())
1094 return SDValue();
1095
1096 if (SDValue Combined = reassociateOpsCommutative(Opc, DL, N0, N1))
1097 return Combined;
1098 if (SDValue Combined = reassociateOpsCommutative(Opc, DL, N1, N0))
1099 return Combined;
1100 return SDValue();
1101}
1102
1103SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
1104 bool AddTo) {
1105 assert(N->getNumValues() == NumTo && "Broken CombineTo call!")(static_cast <bool> (N->getNumValues() == NumTo &&
"Broken CombineTo call!") ? void (0) : __assert_fail ("N->getNumValues() == NumTo && \"Broken CombineTo call!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 1105, __extension__ __PRETTY_FUNCTION__))
;
1106 ++NodesCombined;
1107 LLVM_DEBUG(dbgs() << "\nReplacing.1 "; N->dump(&DAG); dbgs() << "\nWith: ";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nReplacing.1 "; N->dump
(&DAG); dbgs() << "\nWith: "; To[0].getNode()->dump
(&DAG); dbgs() << " and " << NumTo - 1 <<
" other values\n"; } } while (false)
1108 To[0].getNode()->dump(&DAG);do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nReplacing.1 "; N->dump
(&DAG); dbgs() << "\nWith: "; To[0].getNode()->dump
(&DAG); dbgs() << " and " << NumTo - 1 <<
" other values\n"; } } while (false)
1109 dbgs() << " and " << NumTo - 1 << " other values\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nReplacing.1 "; N->dump
(&DAG); dbgs() << "\nWith: "; To[0].getNode()->dump
(&DAG); dbgs() << " and " << NumTo - 1 <<
" other values\n"; } } while (false)
;
1110 for (unsigned i = 0, e = NumTo; i != e; ++i)
1111 assert((!To[i].getNode() ||(static_cast <bool> ((!To[i].getNode() || N->getValueType
(i) == To[i].getValueType()) && "Cannot combine value to value of different type!"
) ? void (0) : __assert_fail ("(!To[i].getNode() || N->getValueType(i) == To[i].getValueType()) && \"Cannot combine value to value of different type!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 1113, __extension__ __PRETTY_FUNCTION__))
1112 N->getValueType(i) == To[i].getValueType()) &&(static_cast <bool> ((!To[i].getNode() || N->getValueType
(i) == To[i].getValueType()) && "Cannot combine value to value of different type!"
) ? void (0) : __assert_fail ("(!To[i].getNode() || N->getValueType(i) == To[i].getValueType()) && \"Cannot combine value to value of different type!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 1113, __extension__ __PRETTY_FUNCTION__))
1113 "Cannot combine value to value of different type!")(static_cast <bool> ((!To[i].getNode() || N->getValueType
(i) == To[i].getValueType()) && "Cannot combine value to value of different type!"
) ? void (0) : __assert_fail ("(!To[i].getNode() || N->getValueType(i) == To[i].getValueType()) && \"Cannot combine value to value of different type!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 1113, __extension__ __PRETTY_FUNCTION__))
;
1114
1115 WorklistRemover DeadNodes(*this);
1116 DAG.ReplaceAllUsesWith(N, To);
1117 if (AddTo) {
1118 // Push the new nodes and any users onto the worklist
1119 for (unsigned i = 0, e = NumTo; i != e; ++i) {
1120 if (To[i].getNode()) {
1121 AddToWorklist(To[i].getNode());
1122 AddUsersToWorklist(To[i].getNode());
1123 }
1124 }
1125 }
1126
1127 // Finally, if the node is now dead, remove it from the graph. The node
1128 // may not be dead if the replacement process recursively simplified to
1129 // something else needing this node.
1130 if (N->use_empty())
1131 deleteAndRecombine(N);
1132 return SDValue(N, 0);
1133}
1134
1135void DAGCombiner::
1136CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
1137 // Replace the old value with the new one.
1138 ++NodesCombined;
1139 LLVM_DEBUG(dbgs() << "\nReplacing.2 "; TLO.Old.getNode()->dump(&DAG);do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nReplacing.2 "; TLO.Old.getNode
()->dump(&DAG); dbgs() << "\nWith: "; TLO.New.getNode
()->dump(&DAG); dbgs() << '\n'; } } while (false
)
1140 dbgs() << "\nWith: "; TLO.New.getNode()->dump(&DAG);do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nReplacing.2 "; TLO.Old.getNode
()->dump(&DAG); dbgs() << "\nWith: "; TLO.New.getNode
()->dump(&DAG); dbgs() << '\n'; } } while (false
)
1141 dbgs() << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nReplacing.2 "; TLO.Old.getNode
()->dump(&DAG); dbgs() << "\nWith: "; TLO.New.getNode
()->dump(&DAG); dbgs() << '\n'; } } while (false
)
;
1142
1143 // Replace all uses. If any nodes become isomorphic to other nodes and
1144 // are deleted, make sure to remove them from our worklist.
1145 WorklistRemover DeadNodes(*this);
1146 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New);
1147
1148 // Push the new node and any (possibly new) users onto the worklist.
1149 AddToWorklistWithUsers(TLO.New.getNode());
1150
1151 // Finally, if the node is now dead, remove it from the graph. The node
1152 // may not be dead if the replacement process recursively simplified to
1153 // something else needing this node.
1154 if (TLO.Old.getNode()->use_empty())
1155 deleteAndRecombine(TLO.Old.getNode());
1156}
1157
1158/// Check the specified integer node value to see if it can be simplified or if
1159/// things it uses can be simplified by bit propagation. If so, return true.
1160bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &DemandedBits,
1161 const APInt &DemandedElts,
1162 bool AssumeSingleUse) {
1163 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
1164 KnownBits Known;
1165 if (!TLI.SimplifyDemandedBits(Op, DemandedBits, DemandedElts, Known, TLO, 0,
1166 AssumeSingleUse))
1167 return false;
1168
1169 // Revisit the node.
1170 AddToWorklist(Op.getNode());
1171
1172 CommitTargetLoweringOpt(TLO);
1173 return true;
1174}
1175
1176/// Check the specified vector node value to see if it can be simplified or
1177/// if things it uses can be simplified as it only uses some of the elements.
1178/// If so, return true.
1179bool DAGCombiner::SimplifyDemandedVectorElts(SDValue Op,
1180 const APInt &DemandedElts,
1181 bool AssumeSingleUse) {
1182 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
1183 APInt KnownUndef, KnownZero;
1184 if (!TLI.SimplifyDemandedVectorElts(Op, DemandedElts, KnownUndef, KnownZero,
1185 TLO, 0, AssumeSingleUse))
1186 return false;
1187
1188 // Revisit the node.
1189 AddToWorklist(Op.getNode());
1190
1191 CommitTargetLoweringOpt(TLO);
1192 return true;
1193}
1194
1195void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) {
1196 SDLoc DL(Load);
1197 EVT VT = Load->getValueType(0);
1198 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, VT, SDValue(ExtLoad, 0));
1199
1200 LLVM_DEBUG(dbgs() << "\nReplacing.9 "; Load->dump(&DAG); dbgs() << "\nWith: ";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nReplacing.9 "; Load->
dump(&DAG); dbgs() << "\nWith: "; Trunc.getNode()->
dump(&DAG); dbgs() << '\n'; } } while (false)
1201 Trunc.getNode()->dump(&DAG); dbgs() << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nReplacing.9 "; Load->
dump(&DAG); dbgs() << "\nWith: "; Trunc.getNode()->
dump(&DAG); dbgs() << '\n'; } } while (false)
;
1202 WorklistRemover DeadNodes(*this);
1203 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc);
1204 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1));
1205 deleteAndRecombine(Load);
1206 AddToWorklist(Trunc.getNode());
1207}
1208
1209SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) {
1210 Replace = false;
1211 SDLoc DL(Op);
1212 if (ISD::isUNINDEXEDLoad(Op.getNode())) {
1213 LoadSDNode *LD = cast<LoadSDNode>(Op);
1214 EVT MemVT = LD->getMemoryVT();
1215 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD) ? ISD::EXTLOAD
1216 : LD->getExtensionType();
1217 Replace = true;
1218 return DAG.getExtLoad(ExtType, DL, PVT,
1219 LD->getChain(), LD->getBasePtr(),
1220 MemVT, LD->getMemOperand());
1221 }
1222
1223 unsigned Opc = Op.getOpcode();
1224 switch (Opc) {
1225 default: break;
1226 case ISD::AssertSext:
1227 if (SDValue Op0 = SExtPromoteOperand(Op.getOperand(0), PVT))
1228 return DAG.getNode(ISD::AssertSext, DL, PVT, Op0, Op.getOperand(1));
1229 break;
1230 case ISD::AssertZext:
1231 if (SDValue Op0 = ZExtPromoteOperand(Op.getOperand(0), PVT))
1232 return DAG.getNode(ISD::AssertZext, DL, PVT, Op0, Op.getOperand(1));
1233 break;
1234 case ISD::Constant: {
1235 unsigned ExtOpc =
1236 Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
1237 return DAG.getNode(ExtOpc, DL, PVT, Op);
1238 }
1239 }
1240
1241 if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT))
1242 return SDValue();
1243 return DAG.getNode(ISD::ANY_EXTEND, DL, PVT, Op);
1244}
1245
1246SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) {
1247 if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT))
1248 return SDValue();
1249 EVT OldVT = Op.getValueType();
1250 SDLoc DL(Op);
1251 bool Replace = false;
1252 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
1253 if (!NewOp.getNode())
1254 return SDValue();
1255 AddToWorklist(NewOp.getNode());
1256
1257 if (Replace)
1258 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
1259 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, NewOp.getValueType(), NewOp,
1260 DAG.getValueType(OldVT));
1261}
1262
1263SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) {
1264 EVT OldVT = Op.getValueType();
1265 SDLoc DL(Op);
1266 bool Replace = false;
1267 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
1268 if (!NewOp.getNode())
1269 return SDValue();
1270 AddToWorklist(NewOp.getNode());
1271
1272 if (Replace)
1273 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
1274 return DAG.getZeroExtendInReg(NewOp, DL, OldVT);
1275}
1276
1277/// Promote the specified integer binary operation if the target indicates it is
1278/// beneficial. e.g. On x86, it's usually better to promote i16 operations to
1279/// i32 since i16 instructions are longer.
1280SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) {
1281 if (!LegalOperations)
1282 return SDValue();
1283
1284 EVT VT = Op.getValueType();
1285 if (VT.isVector() || !VT.isInteger())
1286 return SDValue();
1287
1288 // If operation type is 'undesirable', e.g. i16 on x86, consider
1289 // promoting it.
1290 unsigned Opc = Op.getOpcode();
1291 if (TLI.isTypeDesirableForOp(Opc, VT))
1292 return SDValue();
1293
1294 EVT PVT = VT;
1295 // Consult target whether it is a good idea to promote this operation and
1296 // what's the right type to promote it to.
1297 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
1298 assert(PVT != VT && "Don't know what type to promote to!")(static_cast <bool> (PVT != VT && "Don't know what type to promote to!"
) ? void (0) : __assert_fail ("PVT != VT && \"Don't know what type to promote to!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 1298, __extension__ __PRETTY_FUNCTION__))
;
1299
1300 LLVM_DEBUG(dbgs() << "\nPromoting "; Op.getNode()->dump(&DAG))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nPromoting "; Op.getNode(
)->dump(&DAG); } } while (false)
;
1301
1302 bool Replace0 = false;
1303 SDValue N0 = Op.getOperand(0);
1304 SDValue NN0 = PromoteOperand(N0, PVT, Replace0);
1305
1306 bool Replace1 = false;
1307 SDValue N1 = Op.getOperand(1);
1308 SDValue NN1 = PromoteOperand(N1, PVT, Replace1);
1309 SDLoc DL(Op);
1310
1311 SDValue RV =
1312 DAG.getNode(ISD::TRUNCATE, DL, VT, DAG.getNode(Opc, DL, PVT, NN0, NN1));
1313
1314 // We are always replacing N0/N1's use in N and only need additional
1315 // replacements if there are additional uses.
1316 // Note: We are checking uses of the *nodes* (SDNode) rather than values
1317 // (SDValue) here because the node may reference multiple values
1318 // (for example, the chain value of a load node).
1319 Replace0 &= !N0->hasOneUse();
1320 Replace1 &= (N0 != N1) && !N1->hasOneUse();
1321
1322 // Combine Op here so it is preserved past replacements.
1323 CombineTo(Op.getNode(), RV);
1324
1325 // If operands have a use ordering, make sure we deal with
1326 // predecessor first.
1327 if (Replace0 && Replace1 && N0.getNode()->isPredecessorOf(N1.getNode())) {
1328 std::swap(N0, N1);
1329 std::swap(NN0, NN1);
1330 }
1331
1332 if (Replace0) {
1333 AddToWorklist(NN0.getNode());
1334 ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode());
1335 }
1336 if (Replace1) {
1337 AddToWorklist(NN1.getNode());
1338 ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode());
1339 }
1340 return Op;
1341 }
1342 return SDValue();
1343}
1344
1345/// Promote the specified integer shift operation if the target indicates it is
1346/// beneficial. e.g. On x86, it's usually better to promote i16 operations to
1347/// i32 since i16 instructions are longer.
1348SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) {
1349 if (!LegalOperations)
1350 return SDValue();
1351
1352 EVT VT = Op.getValueType();
1353 if (VT.isVector() || !VT.isInteger())
1354 return SDValue();
1355
1356 // If operation type is 'undesirable', e.g. i16 on x86, consider
1357 // promoting it.
1358 unsigned Opc = Op.getOpcode();
1359 if (TLI.isTypeDesirableForOp(Opc, VT))
1360 return SDValue();
1361
1362 EVT PVT = VT;
1363 // Consult target whether it is a good idea to promote this operation and
1364 // what's the right type to promote it to.
1365 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
1366 assert(PVT != VT && "Don't know what type to promote to!")(static_cast <bool> (PVT != VT && "Don't know what type to promote to!"
) ? void (0) : __assert_fail ("PVT != VT && \"Don't know what type to promote to!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 1366, __extension__ __PRETTY_FUNCTION__))
;
1367
1368 LLVM_DEBUG(dbgs() << "\nPromoting "; Op.getNode()->dump(&DAG))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nPromoting "; Op.getNode(
)->dump(&DAG); } } while (false)
;
1369
1370 bool Replace = false;
1371 SDValue N0 = Op.getOperand(0);
1372 SDValue N1 = Op.getOperand(1);
1373 if (Opc == ISD::SRA)
1374 N0 = SExtPromoteOperand(N0, PVT);
1375 else if (Opc == ISD::SRL)
1376 N0 = ZExtPromoteOperand(N0, PVT);
1377 else
1378 N0 = PromoteOperand(N0, PVT, Replace);
1379
1380 if (!N0.getNode())
1381 return SDValue();
1382
1383 SDLoc DL(Op);
1384 SDValue RV =
1385 DAG.getNode(ISD::TRUNCATE, DL, VT, DAG.getNode(Opc, DL, PVT, N0, N1));
1386
1387 if (Replace)
1388 ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode());
1389
1390 // Deal with Op being deleted.
1391 if (Op && Op.getOpcode() != ISD::DELETED_NODE)
1392 return RV;
1393 }
1394 return SDValue();
1395}
1396
1397SDValue DAGCombiner::PromoteExtend(SDValue Op) {
1398 if (!LegalOperations)
1399 return SDValue();
1400
1401 EVT VT = Op.getValueType();
1402 if (VT.isVector() || !VT.isInteger())
1403 return SDValue();
1404
1405 // If operation type is 'undesirable', e.g. i16 on x86, consider
1406 // promoting it.
1407 unsigned Opc = Op.getOpcode();
1408 if (TLI.isTypeDesirableForOp(Opc, VT))
1409 return SDValue();
1410
1411 EVT PVT = VT;
1412 // Consult target whether it is a good idea to promote this operation and
1413 // what's the right type to promote it to.
1414 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
1415 assert(PVT != VT && "Don't know what type to promote to!")(static_cast <bool> (PVT != VT && "Don't know what type to promote to!"
) ? void (0) : __assert_fail ("PVT != VT && \"Don't know what type to promote to!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 1415, __extension__ __PRETTY_FUNCTION__))
;
1416 // fold (aext (aext x)) -> (aext x)
1417 // fold (aext (zext x)) -> (zext x)
1418 // fold (aext (sext x)) -> (sext x)
1419 LLVM_DEBUG(dbgs() << "\nPromoting "; Op.getNode()->dump(&DAG))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nPromoting "; Op.getNode(
)->dump(&DAG); } } while (false)
;
1420 return DAG.getNode(Op.getOpcode(), SDLoc(Op), VT, Op.getOperand(0));
1421 }
1422 return SDValue();
1423}
1424
1425bool DAGCombiner::PromoteLoad(SDValue Op) {
1426 if (!LegalOperations)
1427 return false;
1428
1429 if (!ISD::isUNINDEXEDLoad(Op.getNode()))
1430 return false;
1431
1432 EVT VT = Op.getValueType();
1433 if (VT.isVector() || !VT.isInteger())
1434 return false;
1435
1436 // If operation type is 'undesirable', e.g. i16 on x86, consider
1437 // promoting it.
1438 unsigned Opc = Op.getOpcode();
1439 if (TLI.isTypeDesirableForOp(Opc, VT))
1440 return false;
1441
1442 EVT PVT = VT;
1443 // Consult target whether it is a good idea to promote this operation and
1444 // what's the right type to promote it to.
1445 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
1446 assert(PVT != VT && "Don't know what type to promote to!")(static_cast <bool> (PVT != VT && "Don't know what type to promote to!"
) ? void (0) : __assert_fail ("PVT != VT && \"Don't know what type to promote to!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 1446, __extension__ __PRETTY_FUNCTION__))
;
1447
1448 SDLoc DL(Op);
1449 SDNode *N = Op.getNode();
1450 LoadSDNode *LD = cast<LoadSDNode>(N);
1451 EVT MemVT = LD->getMemoryVT();
1452 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD) ? ISD::EXTLOAD
1453 : LD->getExtensionType();
1454 SDValue NewLD = DAG.getExtLoad(ExtType, DL, PVT,
1455 LD->getChain(), LD->getBasePtr(),
1456 MemVT, LD->getMemOperand());
1457 SDValue Result = DAG.getNode(ISD::TRUNCATE, DL, VT, NewLD);
1458
1459 LLVM_DEBUG(dbgs() << "\nPromoting "; N->dump(&DAG); dbgs() << "\nTo: ";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nPromoting "; N->dump(
&DAG); dbgs() << "\nTo: "; Result.getNode()->dump
(&DAG); dbgs() << '\n'; } } while (false)
1460 Result.getNode()->dump(&DAG); dbgs() << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nPromoting "; N->dump(
&DAG); dbgs() << "\nTo: "; Result.getNode()->dump
(&DAG); dbgs() << '\n'; } } while (false)
;
1461 WorklistRemover DeadNodes(*this);
1462 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
1463 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1));
1464 deleteAndRecombine(N);
1465 AddToWorklist(Result.getNode());
1466 return true;
1467 }
1468 return false;
1469}
1470
1471/// Recursively delete a node which has no uses and any operands for
1472/// which it is the only use.
1473///
1474/// Note that this both deletes the nodes and removes them from the worklist.
1475/// It also adds any nodes who have had a user deleted to the worklist as they
1476/// may now have only one use and subject to other combines.
1477bool DAGCombiner::recursivelyDeleteUnusedNodes(SDNode *N) {
1478 if (!N->use_empty())
1479 return false;
1480
1481 SmallSetVector<SDNode *, 16> Nodes;
1482 Nodes.insert(N);
1483 do {
1484 N = Nodes.pop_back_val();
1485 if (!N)
1486 continue;
1487
1488 if (N->use_empty()) {
1489 for (const SDValue &ChildN : N->op_values())
1490 Nodes.insert(ChildN.getNode());
1491
1492 removeFromWorklist(N);
1493 DAG.DeleteNode(N);
1494 } else {
1495 AddToWorklist(N);
1496 }
1497 } while (!Nodes.empty());
1498 return true;
1499}
1500
1501//===----------------------------------------------------------------------===//
1502// Main DAG Combiner implementation
1503//===----------------------------------------------------------------------===//
1504
1505void DAGCombiner::Run(CombineLevel AtLevel) {
1506 // set the instance variables, so that the various visit routines may use it.
1507 Level = AtLevel;
1508 LegalDAG = Level >= AfterLegalizeDAG;
1509 LegalOperations = Level >= AfterLegalizeVectorOps;
1510 LegalTypes = Level >= AfterLegalizeTypes;
1511
1512 WorklistInserter AddNodes(*this);
1513
1514 // Add all the dag nodes to the worklist.
1515 for (SDNode &Node : DAG.allnodes())
1516 AddToWorklist(&Node);
1517
1518 // Create a dummy node (which is not added to allnodes), that adds a reference
1519 // to the root node, preventing it from being deleted, and tracking any
1520 // changes of the root.
1521 HandleSDNode Dummy(DAG.getRoot());
1522
1523 // While we have a valid worklist entry node, try to combine it.
1524 while (SDNode *N = getNextWorklistEntry()) {
1525 // If N has no uses, it is dead. Make sure to revisit all N's operands once
1526 // N is deleted from the DAG, since they too may now be dead or may have a
1527 // reduced number of uses, allowing other xforms.
1528 if (recursivelyDeleteUnusedNodes(N))
1529 continue;
1530
1531 WorklistRemover DeadNodes(*this);
1532
1533 // If this combine is running after legalizing the DAG, re-legalize any
1534 // nodes pulled off the worklist.
1535 if (LegalDAG) {
1536 SmallSetVector<SDNode *, 16> UpdatedNodes;
1537 bool NIsValid = DAG.LegalizeOp(N, UpdatedNodes);
1538
1539 for (SDNode *LN : UpdatedNodes)
1540 AddToWorklistWithUsers(LN);
1541
1542 if (!NIsValid)
1543 continue;
1544 }
1545
1546 LLVM_DEBUG(dbgs() << "\nCombining: "; N->dump(&DAG))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nCombining: "; N->dump
(&DAG); } } while (false)
;
1547
1548 // Add any operands of the new node which have not yet been combined to the
1549 // worklist as well. Because the worklist uniques things already, this
1550 // won't repeatedly process the same operand.
1551 CombinedNodes.insert(N);
1552 for (const SDValue &ChildN : N->op_values())
1553 if (!CombinedNodes.count(ChildN.getNode()))
1554 AddToWorklist(ChildN.getNode());
1555
1556 SDValue RV = combine(N);
1557
1558 if (!RV.getNode())
1559 continue;
1560
1561 ++NodesCombined;
1562
1563 // If we get back the same node we passed in, rather than a new node or
1564 // zero, we know that the node must have defined multiple values and
1565 // CombineTo was used. Since CombineTo takes care of the worklist
1566 // mechanics for us, we have no work to do in this case.
1567 if (RV.getNode() == N)
1568 continue;
1569
1570 assert(N->getOpcode() != ISD::DELETED_NODE &&(static_cast <bool> (N->getOpcode() != ISD::DELETED_NODE
&& RV.getOpcode() != ISD::DELETED_NODE && "Node was deleted but visit returned new node!"
) ? void (0) : __assert_fail ("N->getOpcode() != ISD::DELETED_NODE && RV.getOpcode() != ISD::DELETED_NODE && \"Node was deleted but visit returned new node!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 1572, __extension__ __PRETTY_FUNCTION__))
1571 RV.getOpcode() != ISD::DELETED_NODE &&(static_cast <bool> (N->getOpcode() != ISD::DELETED_NODE
&& RV.getOpcode() != ISD::DELETED_NODE && "Node was deleted but visit returned new node!"
) ? void (0) : __assert_fail ("N->getOpcode() != ISD::DELETED_NODE && RV.getOpcode() != ISD::DELETED_NODE && \"Node was deleted but visit returned new node!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 1572, __extension__ __PRETTY_FUNCTION__))
1572 "Node was deleted but visit returned new node!")(static_cast <bool> (N->getOpcode() != ISD::DELETED_NODE
&& RV.getOpcode() != ISD::DELETED_NODE && "Node was deleted but visit returned new node!"
) ? void (0) : __assert_fail ("N->getOpcode() != ISD::DELETED_NODE && RV.getOpcode() != ISD::DELETED_NODE && \"Node was deleted but visit returned new node!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 1572, __extension__ __PRETTY_FUNCTION__))
;
1573
1574 LLVM_DEBUG(dbgs() << " ... into: "; RV.getNode()->dump(&DAG))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << " ... into: "; RV.getNode()
->dump(&DAG); } } while (false)
;
1575
1576 if (N->getNumValues() == RV.getNode()->getNumValues())
1577 DAG.ReplaceAllUsesWith(N, RV.getNode());
1578 else {
1579 assert(N->getValueType(0) == RV.getValueType() &&(static_cast <bool> (N->getValueType(0) == RV.getValueType
() && N->getNumValues() == 1 && "Type mismatch"
) ? void (0) : __assert_fail ("N->getValueType(0) == RV.getValueType() && N->getNumValues() == 1 && \"Type mismatch\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 1580, __extension__ __PRETTY_FUNCTION__))
1580 N->getNumValues() == 1 && "Type mismatch")(static_cast <bool> (N->getValueType(0) == RV.getValueType
() && N->getNumValues() == 1 && "Type mismatch"
) ? void (0) : __assert_fail ("N->getValueType(0) == RV.getValueType() && N->getNumValues() == 1 && \"Type mismatch\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 1580, __extension__ __PRETTY_FUNCTION__))
;
1581 DAG.ReplaceAllUsesWith(N, &RV);
1582 }
1583
1584 // Push the new node and any users onto the worklist. Omit this if the
1585 // new node is the EntryToken (e.g. if a store managed to get optimized
1586 // out), because re-visiting the EntryToken and its users will not uncover
1587 // any additional opportunities, but there may be a large number of such
1588 // users, potentially causing compile time explosion.
1589 if (RV.getOpcode() != ISD::EntryToken) {
1590 AddToWorklist(RV.getNode());
1591 AddUsersToWorklist(RV.getNode());
1592 }
1593
1594 // Finally, if the node is now dead, remove it from the graph. The node
1595 // may not be dead if the replacement process recursively simplified to
1596 // something else needing this node. This will also take care of adding any
1597 // operands which have lost a user to the worklist.
1598 recursivelyDeleteUnusedNodes(N);
1599 }
1600
1601 // If the root changed (e.g. it was a dead load, update the root).
1602 DAG.setRoot(Dummy.getValue());
1603 DAG.RemoveDeadNodes();
1604}
1605
1606SDValue DAGCombiner::visit(SDNode *N) {
1607 switch (N->getOpcode()) {
1608 default: break;
1609 case ISD::TokenFactor: return visitTokenFactor(N);
1610 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
1611 case ISD::ADD: return visitADD(N);
1612 case ISD::SUB: return visitSUB(N);
1613 case ISD::SADDSAT:
1614 case ISD::UADDSAT: return visitADDSAT(N);
1615 case ISD::SSUBSAT:
1616 case ISD::USUBSAT: return visitSUBSAT(N);
1617 case ISD::ADDC: return visitADDC(N);
1618 case ISD::SADDO:
1619 case ISD::UADDO: return visitADDO(N);
1620 case ISD::SUBC: return visitSUBC(N);
1621 case ISD::SSUBO:
1622 case ISD::USUBO: return visitSUBO(N);
1623 case ISD::ADDE: return visitADDE(N);
1624 case ISD::ADDCARRY: return visitADDCARRY(N);
1625 case ISD::SADDO_CARRY: return visitSADDO_CARRY(N);
1626 case ISD::SUBE: return visitSUBE(N);
1627 case ISD::SUBCARRY: return visitSUBCARRY(N);
1628 case ISD::SSUBO_CARRY: return visitSSUBO_CARRY(N);
1629 case ISD::SMULFIX:
1630 case ISD::SMULFIXSAT:
1631 case ISD::UMULFIX:
1632 case ISD::UMULFIXSAT: return visitMULFIX(N);
1633 case ISD::MUL: return visitMUL(N);
1634 case ISD::SDIV: return visitSDIV(N);
1635 case ISD::UDIV: return visitUDIV(N);
1636 case ISD::SREM:
1637 case ISD::UREM: return visitREM(N);
1638 case ISD::MULHU: return visitMULHU(N);
1639 case ISD::MULHS: return visitMULHS(N);
1640 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
1641 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
1642 case ISD::SMULO:
1643 case ISD::UMULO: return visitMULO(N);
1644 case ISD::SMIN:
1645 case ISD::SMAX:
1646 case ISD::UMIN:
1647 case ISD::UMAX: return visitIMINMAX(N);
1648 case ISD::AND: return visitAND(N);
1649 case ISD::OR: return visitOR(N);
1650 case ISD::XOR: return visitXOR(N);
1651 case ISD::SHL: return visitSHL(N);
1652 case ISD::SRA: return visitSRA(N);
1653 case ISD::SRL: return visitSRL(N);
1654 case ISD::ROTR:
1655 case ISD::ROTL: return visitRotate(N);
1656 case ISD::FSHL:
1657 case ISD::FSHR: return visitFunnelShift(N);
1658 case ISD::ABS: return visitABS(N);
1659 case ISD::BSWAP: return visitBSWAP(N);
1660 case ISD::BITREVERSE: return visitBITREVERSE(N);
1661 case ISD::CTLZ: return visitCTLZ(N);
1662 case ISD::CTLZ_ZERO_UNDEF: return visitCTLZ_ZERO_UNDEF(N);
1663 case ISD::CTTZ: return visitCTTZ(N);
1664 case ISD::CTTZ_ZERO_UNDEF: return visitCTTZ_ZERO_UNDEF(N);
1665 case ISD::CTPOP: return visitCTPOP(N);
1666 case ISD::SELECT: return visitSELECT(N);
1667 case ISD::VSELECT: return visitVSELECT(N);
1668 case ISD::SELECT_CC: return visitSELECT_CC(N);
1669 case ISD::SETCC: return visitSETCC(N);
1670 case ISD::SETCCCARRY: return visitSETCCCARRY(N);
1671 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
1672 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
1673 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
1674 case ISD::AssertSext:
1675 case ISD::AssertZext: return visitAssertExt(N);
1676 case ISD::AssertAlign: return visitAssertAlign(N);
1677 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
1678 case ISD::SIGN_EXTEND_VECTOR_INREG:
1679 case ISD::ZERO_EXTEND_VECTOR_INREG: return visitEXTEND_VECTOR_INREG(N);
1680 case ISD::TRUNCATE: return visitTRUNCATE(N);
1681 case ISD::BITCAST: return visitBITCAST(N);
1682 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
1683 case ISD::FADD: return visitFADD(N);
1684 case ISD::STRICT_FADD: return visitSTRICT_FADD(N);
1685 case ISD::FSUB: return visitFSUB(N);
1686 case ISD::FMUL: return visitFMUL(N);
1687 case ISD::FMA: return visitFMA(N);
1688 case ISD::FDIV: return visitFDIV(N);
1689 case ISD::FREM: return visitFREM(N);
1690 case ISD::FSQRT: return visitFSQRT(N);
1691 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
1692 case ISD::FPOW: return visitFPOW(N);
1693 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
1694 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
1695 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
1696 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
1697 case ISD::FP_ROUND: return visitFP_ROUND(N);
1698 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
1699 case ISD::FNEG: return visitFNEG(N);
1700 case ISD::FABS: return visitFABS(N);
1701 case ISD::FFLOOR: return visitFFLOOR(N);
1702 case ISD::FMINNUM: return visitFMINNUM(N);
1703 case ISD::FMAXNUM: return visitFMAXNUM(N);
1704 case ISD::FMINIMUM: return visitFMINIMUM(N);
1705 case ISD::FMAXIMUM: return visitFMAXIMUM(N);
1706 case ISD::FCEIL: return visitFCEIL(N);
1707 case ISD::FTRUNC: return visitFTRUNC(N);
1708 case ISD::BRCOND: return visitBRCOND(N);
1709 case ISD::BR_CC: return visitBR_CC(N);
1710 case ISD::LOAD: return visitLOAD(N);
1711 case ISD::STORE: return visitSTORE(N);
1712 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
1713 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
1714 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
1715 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
1716 case ISD::EXTRACT_SUBVECTOR: return visitEXTRACT_SUBVECTOR(N);
1717 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
1718 case ISD::SCALAR_TO_VECTOR: return visitSCALAR_TO_VECTOR(N);
1719 case ISD::INSERT_SUBVECTOR: return visitINSERT_SUBVECTOR(N);
1720 case ISD::MGATHER: return visitMGATHER(N);
1721 case ISD::MLOAD: return visitMLOAD(N);
1722 case ISD::MSCATTER: return visitMSCATTER(N);
1723 case ISD::MSTORE: return visitMSTORE(N);
1724 case ISD::LIFETIME_END: return visitLIFETIME_END(N);
1725 case ISD::FP_TO_FP16: return visitFP_TO_FP16(N);
1726 case ISD::FP16_TO_FP: return visitFP16_TO_FP(N);
1727 case ISD::FREEZE: return visitFREEZE(N);
1728 case ISD::VECREDUCE_FADD:
1729 case ISD::VECREDUCE_FMUL:
1730 case ISD::VECREDUCE_ADD:
1731 case ISD::VECREDUCE_MUL:
1732 case ISD::VECREDUCE_AND:
1733 case ISD::VECREDUCE_OR:
1734 case ISD::VECREDUCE_XOR:
1735 case ISD::VECREDUCE_SMAX:
1736 case ISD::VECREDUCE_SMIN:
1737 case ISD::VECREDUCE_UMAX:
1738 case ISD::VECREDUCE_UMIN:
1739 case ISD::VECREDUCE_FMAX:
1740 case ISD::VECREDUCE_FMIN: return visitVECREDUCE(N);
1741 }
1742 return SDValue();
1743}
1744
1745SDValue DAGCombiner::combine(SDNode *N) {
1746 SDValue RV;
1747 if (!DisableGenericCombines)
1748 RV = visit(N);
1749
1750 // If nothing happened, try a target-specific DAG combine.
1751 if (!RV.getNode()) {
1752 assert(N->getOpcode() != ISD::DELETED_NODE &&(static_cast <bool> (N->getOpcode() != ISD::DELETED_NODE
&& "Node was deleted but visit returned NULL!") ? void
(0) : __assert_fail ("N->getOpcode() != ISD::DELETED_NODE && \"Node was deleted but visit returned NULL!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 1753, __extension__ __PRETTY_FUNCTION__))
1753 "Node was deleted but visit returned NULL!")(static_cast <bool> (N->getOpcode() != ISD::DELETED_NODE
&& "Node was deleted but visit returned NULL!") ? void
(0) : __assert_fail ("N->getOpcode() != ISD::DELETED_NODE && \"Node was deleted but visit returned NULL!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 1753, __extension__ __PRETTY_FUNCTION__))
;
1754
1755 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
1756 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
1757
1758 // Expose the DAG combiner to the target combiner impls.
1759 TargetLowering::DAGCombinerInfo
1760 DagCombineInfo(DAG, Level, false, this);
1761
1762 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
1763 }
1764 }
1765
1766 // If nothing happened still, try promoting the operation.
1767 if (!RV.getNode()) {
1768 switch (N->getOpcode()) {
1769 default: break;
1770 case ISD::ADD:
1771 case ISD::SUB:
1772 case ISD::MUL:
1773 case ISD::AND:
1774 case ISD::OR:
1775 case ISD::XOR:
1776 RV = PromoteIntBinOp(SDValue(N, 0));
1777 break;
1778 case ISD::SHL:
1779 case ISD::SRA:
1780 case ISD::SRL:
1781 RV = PromoteIntShiftOp(SDValue(N, 0));
1782 break;
1783 case ISD::SIGN_EXTEND:
1784 case ISD::ZERO_EXTEND:
1785 case ISD::ANY_EXTEND:
1786 RV = PromoteExtend(SDValue(N, 0));
1787 break;
1788 case ISD::LOAD:
1789 if (PromoteLoad(SDValue(N, 0)))
1790 RV = SDValue(N, 0);
1791 break;
1792 }
1793 }
1794
1795 // If N is a commutative binary node, try to eliminate it if the commuted
1796 // version is already present in the DAG.
1797 if (!RV.getNode() && TLI.isCommutativeBinOp(N->getOpcode()) &&
1798 N->getNumValues() == 1) {
1799 SDValue N0 = N->getOperand(0);
1800 SDValue N1 = N->getOperand(1);
1801
1802 // Constant operands are canonicalized to RHS.
1803 if (N0 != N1 && (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1))) {
1804 SDValue Ops[] = {N1, N0};
1805 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(), Ops,
1806 N->getFlags());
1807 if (CSENode)
1808 return SDValue(CSENode, 0);
1809 }
1810 }
1811
1812 return RV;
1813}
1814
1815/// Given a node, return its input chain if it has one, otherwise return a null
1816/// sd operand.
1817static SDValue getInputChainForNode(SDNode *N) {
1818 if (unsigned NumOps = N->getNumOperands()) {
1819 if (N->getOperand(0).getValueType() == MVT::Other)
1820 return N->getOperand(0);
1821 if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
1822 return N->getOperand(NumOps-1);
1823 for (unsigned i = 1; i < NumOps-1; ++i)
1824 if (N->getOperand(i).getValueType() == MVT::Other)
1825 return N->getOperand(i);
1826 }
1827 return SDValue();
1828}
1829
1830SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
1831 // If N has two operands, where one has an input chain equal to the other,
1832 // the 'other' chain is redundant.
1833 if (N->getNumOperands() == 2) {
1834 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
1835 return N->getOperand(0);
1836 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
1837 return N->getOperand(1);
1838 }
1839
1840 // Don't simplify token factors if optnone.
1841 if (OptLevel == CodeGenOpt::None)
1842 return SDValue();
1843
1844 // Don't simplify the token factor if the node itself has too many operands.
1845 if (N->getNumOperands() > TokenFactorInlineLimit)
1846 return SDValue();
1847
1848 // If the sole user is a token factor, we should make sure we have a
1849 // chance to merge them together. This prevents TF chains from inhibiting
1850 // optimizations.
1851 if (N->hasOneUse() && N->use_begin()->getOpcode() == ISD::TokenFactor)
1852 AddToWorklist(*(N->use_begin()));
1853
1854 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
1855 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
1856 SmallPtrSet<SDNode*, 16> SeenOps;
1857 bool Changed = false; // If we should replace this token factor.
1858
1859 // Start out with this token factor.
1860 TFs.push_back(N);
1861
1862 // Iterate through token factors. The TFs grows when new token factors are
1863 // encountered.
1864 for (unsigned i = 0; i < TFs.size(); ++i) {
1865 // Limit number of nodes to inline, to avoid quadratic compile times.
1866 // We have to add the outstanding Token Factors to Ops, otherwise we might
1867 // drop Ops from the resulting Token Factors.
1868 if (Ops.size() > TokenFactorInlineLimit) {
1869 for (unsigned j = i; j < TFs.size(); j++)
1870 Ops.emplace_back(TFs[j], 0);
1871 // Drop unprocessed Token Factors from TFs, so we do not add them to the
1872 // combiner worklist later.
1873 TFs.resize(i);
1874 break;
1875 }
1876
1877 SDNode *TF = TFs[i];
1878 // Check each of the operands.
1879 for (const SDValue &Op : TF->op_values()) {
1880 switch (Op.getOpcode()) {
1881 case ISD::EntryToken:
1882 // Entry tokens don't need to be added to the list. They are
1883 // redundant.
1884 Changed = true;
1885 break;
1886
1887 case ISD::TokenFactor:
1888 if (Op.hasOneUse() && !is_contained(TFs, Op.getNode())) {
1889 // Queue up for processing.
1890 TFs.push_back(Op.getNode());
1891 Changed = true;
1892 break;
1893 }
1894 LLVM_FALLTHROUGH[[gnu::fallthrough]];
1895
1896 default:
1897 // Only add if it isn't already in the list.
1898 if (SeenOps.insert(Op.getNode()).second)
1899 Ops.push_back(Op);
1900 else
1901 Changed = true;
1902 break;
1903 }
1904 }
1905 }
1906
1907 // Re-visit inlined Token Factors, to clean them up in case they have been
1908 // removed. Skip the first Token Factor, as this is the current node.
1909 for (unsigned i = 1, e = TFs.size(); i < e; i++)
1910 AddToWorklist(TFs[i]);
1911
1912 // Remove Nodes that are chained to another node in the list. Do so
1913 // by walking up chains breath-first stopping when we've seen
1914 // another operand. In general we must climb to the EntryNode, but we can exit
1915 // early if we find all remaining work is associated with just one operand as
1916 // no further pruning is possible.
1917
1918 // List of nodes to search through and original Ops from which they originate.
1919 SmallVector<std::pair<SDNode *, unsigned>, 8> Worklist;
1920 SmallVector<unsigned, 8> OpWorkCount; // Count of work for each Op.
1921 SmallPtrSet<SDNode *, 16> SeenChains;
1922 bool DidPruneOps = false;
1923
1924 unsigned NumLeftToConsider = 0;
1925 for (const SDValue &Op : Ops) {
1926 Worklist.push_back(std::make_pair(Op.getNode(), NumLeftToConsider++));
1927 OpWorkCount.push_back(1);
1928 }
1929
1930 auto AddToWorklist = [&](unsigned CurIdx, SDNode *Op, unsigned OpNumber) {
1931 // If this is an Op, we can remove the op from the list. Remark any
1932 // search associated with it as from the current OpNumber.
1933 if (SeenOps.contains(Op)) {
1934 Changed = true;
1935 DidPruneOps = true;
1936 unsigned OrigOpNumber = 0;
1937 while (OrigOpNumber < Ops.size() && Ops[OrigOpNumber].getNode() != Op)
1938 OrigOpNumber++;
1939 assert((OrigOpNumber != Ops.size()) &&(static_cast <bool> ((OrigOpNumber != Ops.size()) &&
"expected to find TokenFactor Operand") ? void (0) : __assert_fail
("(OrigOpNumber != Ops.size()) && \"expected to find TokenFactor Operand\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 1940, __extension__ __PRETTY_FUNCTION__))
1940 "expected to find TokenFactor Operand")(static_cast <bool> ((OrigOpNumber != Ops.size()) &&
"expected to find TokenFactor Operand") ? void (0) : __assert_fail
("(OrigOpNumber != Ops.size()) && \"expected to find TokenFactor Operand\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 1940, __extension__ __PRETTY_FUNCTION__))
;
1941 // Re-mark worklist from OrigOpNumber to OpNumber
1942 for (unsigned i = CurIdx + 1; i < Worklist.size(); ++i) {
1943 if (Worklist[i].second == OrigOpNumber) {
1944 Worklist[i].second = OpNumber;
1945 }
1946 }
1947 OpWorkCount[OpNumber] += OpWorkCount[OrigOpNumber];
1948 OpWorkCount[OrigOpNumber] = 0;
1949 NumLeftToConsider--;
1950 }
1951 // Add if it's a new chain
1952 if (SeenChains.insert(Op).second) {
1953 OpWorkCount[OpNumber]++;
1954 Worklist.push_back(std::make_pair(Op, OpNumber));
1955 }
1956 };
1957
1958 for (unsigned i = 0; i < Worklist.size() && i < 1024; ++i) {
1959 // We need at least be consider at least 2 Ops to prune.
1960 if (NumLeftToConsider <= 1)
1961 break;
1962 auto CurNode = Worklist[i].first;
1963 auto CurOpNumber = Worklist[i].second;
1964 assert((OpWorkCount[CurOpNumber] > 0) &&(static_cast <bool> ((OpWorkCount[CurOpNumber] > 0) &&
"Node should not appear in worklist") ? void (0) : __assert_fail
("(OpWorkCount[CurOpNumber] > 0) && \"Node should not appear in worklist\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 1965, __extension__ __PRETTY_FUNCTION__))
1965 "Node should not appear in worklist")(static_cast <bool> ((OpWorkCount[CurOpNumber] > 0) &&
"Node should not appear in worklist") ? void (0) : __assert_fail
("(OpWorkCount[CurOpNumber] > 0) && \"Node should not appear in worklist\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 1965, __extension__ __PRETTY_FUNCTION__))
;
1966 switch (CurNode->getOpcode()) {
1967 case ISD::EntryToken:
1968 // Hitting EntryToken is the only way for the search to terminate without
1969 // hitting
1970 // another operand's search. Prevent us from marking this operand
1971 // considered.
1972 NumLeftToConsider++;
1973 break;
1974 case ISD::TokenFactor:
1975 for (const SDValue &Op : CurNode->op_values())
1976 AddToWorklist(i, Op.getNode(), CurOpNumber);
1977 break;
1978 case ISD::LIFETIME_START:
1979 case ISD::LIFETIME_END:
1980 case ISD::CopyFromReg:
1981 case ISD::CopyToReg:
1982 AddToWorklist(i, CurNode->getOperand(0).getNode(), CurOpNumber);
1983 break;
1984 default:
1985 if (auto *MemNode = dyn_cast<MemSDNode>(CurNode))
1986 AddToWorklist(i, MemNode->getChain().getNode(), CurOpNumber);
1987 break;
1988 }
1989 OpWorkCount[CurOpNumber]--;
1990 if (OpWorkCount[CurOpNumber] == 0)
1991 NumLeftToConsider--;
1992 }
1993
1994 // If we've changed things around then replace token factor.
1995 if (Changed) {
1996 SDValue Result;
1997 if (Ops.empty()) {
1998 // The entry token is the only possible outcome.
1999 Result = DAG.getEntryNode();
2000 } else {
2001 if (DidPruneOps) {
2002 SmallVector<SDValue, 8> PrunedOps;
2003 //
2004 for (const SDValue &Op : Ops) {
2005 if (SeenChains.count(Op.getNode()) == 0)
2006 PrunedOps.push_back(Op);
2007 }
2008 Result = DAG.getTokenFactor(SDLoc(N), PrunedOps);
2009 } else {
2010 Result = DAG.getTokenFactor(SDLoc(N), Ops);
2011 }
2012 }
2013 return Result;
2014 }
2015 return SDValue();
2016}
2017
2018/// MERGE_VALUES can always be eliminated.
2019SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
2020 WorklistRemover DeadNodes(*this);
2021 // Replacing results may cause a different MERGE_VALUES to suddenly
2022 // be CSE'd with N, and carry its uses with it. Iterate until no
2023 // uses remain, to ensure that the node can be safely deleted.
2024 // First add the users of this node to the work list so that they
2025 // can be tried again once they have new operands.
2026 AddUsersToWorklist(N);
2027 do {
2028 // Do as a single replacement to avoid rewalking use lists.
2029 SmallVector<SDValue, 8> Ops;
2030 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2031 Ops.push_back(N->getOperand(i));
2032 DAG.ReplaceAllUsesWith(N, Ops.data());
2033 } while (!N->use_empty());
2034 deleteAndRecombine(N);
2035 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2036}
2037
2038/// If \p N is a ConstantSDNode with isOpaque() == false return it casted to a
2039/// ConstantSDNode pointer else nullptr.
2040static ConstantSDNode *getAsNonOpaqueConstant(SDValue N) {
2041 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N);
2042 return Const != nullptr && !Const->isOpaque() ? Const : nullptr;
2043}
2044
2045/// Return true if 'Use' is a load or a store that uses N as its base pointer
2046/// and that N may be folded in the load / store addressing mode.
2047static bool canFoldInAddressingMode(SDNode *N, SDNode *Use, SelectionDAG &DAG,
2048 const TargetLowering &TLI) {
2049 EVT VT;
2050 unsigned AS;
2051
2052 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Use)) {
2053 if (LD->isIndexed() || LD->getBasePtr().getNode() != N)
2054 return false;
2055 VT = LD->getMemoryVT();
2056 AS = LD->getAddressSpace();
2057 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(Use)) {
2058 if (ST->isIndexed() || ST->getBasePtr().getNode() != N)
2059 return false;
2060 VT = ST->getMemoryVT();
2061 AS = ST->getAddressSpace();
2062 } else if (MaskedLoadSDNode *LD = dyn_cast<MaskedLoadSDNode>(Use)) {
2063 if (LD->isIndexed() || LD->getBasePtr().getNode() != N)
2064 return false;
2065 VT = LD->getMemoryVT();
2066 AS = LD->getAddressSpace();
2067 } else if (MaskedStoreSDNode *ST = dyn_cast<MaskedStoreSDNode>(Use)) {
2068 if (ST->isIndexed() || ST->getBasePtr().getNode() != N)
2069 return false;
2070 VT = ST->getMemoryVT();
2071 AS = ST->getAddressSpace();
2072 } else
2073 return false;
2074
2075 TargetLowering::AddrMode AM;
2076 if (N->getOpcode() == ISD::ADD) {
2077 AM.HasBaseReg = true;
2078 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
2079 if (Offset)
2080 // [reg +/- imm]
2081 AM.BaseOffs = Offset->getSExtValue();
2082 else
2083 // [reg +/- reg]
2084 AM.Scale = 1;
2085 } else if (N->getOpcode() == ISD::SUB) {
2086 AM.HasBaseReg = true;
2087 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
2088 if (Offset)
2089 // [reg +/- imm]
2090 AM.BaseOffs = -Offset->getSExtValue();
2091 else
2092 // [reg +/- reg]
2093 AM.Scale = 1;
2094 } else
2095 return false;
2096
2097 return TLI.isLegalAddressingMode(DAG.getDataLayout(), AM,
2098 VT.getTypeForEVT(*DAG.getContext()), AS);
2099}
2100
2101SDValue DAGCombiner::foldBinOpIntoSelect(SDNode *BO) {
2102 assert(TLI.isBinOp(BO->getOpcode()) && BO->getNumValues() == 1 &&(static_cast <bool> (TLI.isBinOp(BO->getOpcode()) &&
BO->getNumValues() == 1 && "Unexpected binary operator"
) ? void (0) : __assert_fail ("TLI.isBinOp(BO->getOpcode()) && BO->getNumValues() == 1 && \"Unexpected binary operator\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 2103, __extension__ __PRETTY_FUNCTION__))
2103 "Unexpected binary operator")(static_cast <bool> (TLI.isBinOp(BO->getOpcode()) &&
BO->getNumValues() == 1 && "Unexpected binary operator"
) ? void (0) : __assert_fail ("TLI.isBinOp(BO->getOpcode()) && BO->getNumValues() == 1 && \"Unexpected binary operator\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 2103, __extension__ __PRETTY_FUNCTION__))
;
2104
2105 // Don't do this unless the old select is going away. We want to eliminate the
2106 // binary operator, not replace a binop with a select.
2107 // TODO: Handle ISD::SELECT_CC.
2108 unsigned SelOpNo = 0;
2109 SDValue Sel = BO->getOperand(0);
2110 if (Sel.getOpcode() != ISD::SELECT || !Sel.hasOneUse()) {
2111 SelOpNo = 1;
2112 Sel = BO->getOperand(1);
2113 }
2114
2115 if (Sel.getOpcode() != ISD::SELECT || !Sel.hasOneUse())
2116 return SDValue();
2117
2118 SDValue CT = Sel.getOperand(1);
2119 if (!isConstantOrConstantVector(CT, true) &&
2120 !DAG.isConstantFPBuildVectorOrConstantFP(CT))
2121 return SDValue();
2122
2123 SDValue CF = Sel.getOperand(2);
2124 if (!isConstantOrConstantVector(CF, true) &&
2125 !DAG.isConstantFPBuildVectorOrConstantFP(CF))
2126 return SDValue();
2127
2128 // Bail out if any constants are opaque because we can't constant fold those.
2129 // The exception is "and" and "or" with either 0 or -1 in which case we can
2130 // propagate non constant operands into select. I.e.:
2131 // and (select Cond, 0, -1), X --> select Cond, 0, X
2132 // or X, (select Cond, -1, 0) --> select Cond, -1, X
2133 auto BinOpcode = BO->getOpcode();
2134 bool CanFoldNonConst =
2135 (BinOpcode == ISD::AND || BinOpcode == ISD::OR) &&
2136 (isNullOrNullSplat(CT) || isAllOnesOrAllOnesSplat(CT)) &&
2137 (isNullOrNullSplat(CF) || isAllOnesOrAllOnesSplat(CF));
2138
2139 SDValue CBO = BO->getOperand(SelOpNo ^ 1);
2140 if (!CanFoldNonConst &&
2141 !isConstantOrConstantVector(CBO, true) &&
2142 !DAG.isConstantFPBuildVectorOrConstantFP(CBO))
2143 return SDValue();
2144
2145 EVT VT = BO->getValueType(0);
2146
2147 // We have a select-of-constants followed by a binary operator with a
2148 // constant. Eliminate the binop by pulling the constant math into the select.
2149 // Example: add (select Cond, CT, CF), CBO --> select Cond, CT + CBO, CF + CBO
2150 SDLoc DL(Sel);
2151 SDValue NewCT = SelOpNo ? DAG.getNode(BinOpcode, DL, VT, CBO, CT)
2152 : DAG.getNode(BinOpcode, DL, VT, CT, CBO);
2153 if (!CanFoldNonConst && !NewCT.isUndef() &&
2154 !isConstantOrConstantVector(NewCT, true) &&
2155 !DAG.isConstantFPBuildVectorOrConstantFP(NewCT))
2156 return SDValue();
2157
2158 SDValue NewCF = SelOpNo ? DAG.getNode(BinOpcode, DL, VT, CBO, CF)
2159 : DAG.getNode(BinOpcode, DL, VT, CF, CBO);
2160 if (!CanFoldNonConst && !NewCF.isUndef() &&
2161 !isConstantOrConstantVector(NewCF, true) &&
2162 !DAG.isConstantFPBuildVectorOrConstantFP(NewCF))
2163 return SDValue();
2164
2165 SDValue SelectOp = DAG.getSelect(DL, VT, Sel.getOperand(0), NewCT, NewCF);
2166 SelectOp->setFlags(BO->getFlags());
2167 return SelectOp;
2168}
2169
2170static SDValue foldAddSubBoolOfMaskedVal(SDNode *N, SelectionDAG &DAG) {
2171 assert((N->getOpcode() == ISD::ADD || N->getOpcode() == ISD::SUB) &&(static_cast <bool> ((N->getOpcode() == ISD::ADD || N
->getOpcode() == ISD::SUB) && "Expecting add or sub"
) ? void (0) : __assert_fail ("(N->getOpcode() == ISD::ADD || N->getOpcode() == ISD::SUB) && \"Expecting add or sub\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 2172, __extension__ __PRETTY_FUNCTION__))
2172 "Expecting add or sub")(static_cast <bool> ((N->getOpcode() == ISD::ADD || N
->getOpcode() == ISD::SUB) && "Expecting add or sub"
) ? void (0) : __assert_fail ("(N->getOpcode() == ISD::ADD || N->getOpcode() == ISD::SUB) && \"Expecting add or sub\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 2172, __extension__ __PRETTY_FUNCTION__))
;
2173
2174 // Match a constant operand and a zext operand for the math instruction:
2175 // add Z, C
2176 // sub C, Z
2177 bool IsAdd = N->getOpcode() == ISD::ADD;
2178 SDValue C = IsAdd ? N->getOperand(1) : N->getOperand(0);
2179 SDValue Z = IsAdd ? N->getOperand(0) : N->getOperand(1);
2180 auto *CN = dyn_cast<ConstantSDNode>(C);
2181 if (!CN || Z.getOpcode() != ISD::ZERO_EXTEND)
2182 return SDValue();
2183
2184 // Match the zext operand as a setcc of a boolean.
2185 if (Z.getOperand(0).getOpcode() != ISD::SETCC ||
2186 Z.getOperand(0).getValueType() != MVT::i1)
2187 return SDValue();
2188
2189 // Match the compare as: setcc (X & 1), 0, eq.
2190 SDValue SetCC = Z.getOperand(0);
2191 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
2192 if (CC != ISD::SETEQ || !isNullConstant(SetCC.getOperand(1)) ||
2193 SetCC.getOperand(0).getOpcode() != ISD::AND ||
2194 !isOneConstant(SetCC.getOperand(0).getOperand(1)))
2195 return SDValue();
2196
2197 // We are adding/subtracting a constant and an inverted low bit. Turn that
2198 // into a subtract/add of the low bit with incremented/decremented constant:
2199 // add (zext i1 (seteq (X & 1), 0)), C --> sub C+1, (zext (X & 1))
2200 // sub C, (zext i1 (seteq (X & 1), 0)) --> add C-1, (zext (X & 1))
2201 EVT VT = C.getValueType();
2202 SDLoc DL(N);
2203 SDValue LowBit = DAG.getZExtOrTrunc(SetCC.getOperand(0), DL, VT);
2204 SDValue C1 = IsAdd ? DAG.getConstant(CN->getAPIntValue() + 1, DL, VT) :
2205 DAG.getConstant(CN->getAPIntValue() - 1, DL, VT);
2206 return DAG.getNode(IsAdd ? ISD::SUB : ISD::ADD, DL, VT, C1, LowBit);
2207}
2208
2209/// Try to fold a 'not' shifted sign-bit with add/sub with constant operand into
2210/// a shift and add with a different constant.
2211static SDValue foldAddSubOfSignBit(SDNode *N, SelectionDAG &DAG) {
2212 assert((N->getOpcode() == ISD::ADD || N->getOpcode() == ISD::SUB) &&(static_cast <bool> ((N->getOpcode() == ISD::ADD || N
->getOpcode() == ISD::SUB) && "Expecting add or sub"
) ? void (0) : __assert_fail ("(N->getOpcode() == ISD::ADD || N->getOpcode() == ISD::SUB) && \"Expecting add or sub\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 2213, __extension__ __PRETTY_FUNCTION__))
2213 "Expecting add or sub")(static_cast <bool> ((N->getOpcode() == ISD::ADD || N
->getOpcode() == ISD::SUB) && "Expecting add or sub"
) ? void (0) : __assert_fail ("(N->getOpcode() == ISD::ADD || N->getOpcode() == ISD::SUB) && \"Expecting add or sub\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 2213, __extension__ __PRETTY_FUNCTION__))
;
2214
2215 // We need a constant operand for the add/sub, and the other operand is a
2216 // logical shift right: add (srl), C or sub C, (srl).
2217 bool IsAdd = N->getOpcode() == ISD::ADD;
2218 SDValue ConstantOp = IsAdd ? N->getOperand(1) : N->getOperand(0);
2219 SDValue ShiftOp = IsAdd ? N->getOperand(0) : N->getOperand(1);
2220 if (!DAG.isConstantIntBuildVectorOrConstantInt(ConstantOp) ||
2221 ShiftOp.getOpcode() != ISD::SRL)
2222 return SDValue();
2223
2224 // The shift must be of a 'not' value.
2225 SDValue Not = ShiftOp.getOperand(0);
2226 if (!Not.hasOneUse() || !isBitwiseNot(Not))
2227 return SDValue();
2228
2229 // The shift must be moving the sign bit to the least-significant-bit.
2230 EVT VT = ShiftOp.getValueType();
2231 SDValue ShAmt = ShiftOp.getOperand(1);
2232 ConstantSDNode *ShAmtC = isConstOrConstSplat(ShAmt);
2233 if (!ShAmtC || ShAmtC->getAPIntValue() != (VT.getScalarSizeInBits() - 1))
2234 return SDValue();
2235
2236 // Eliminate the 'not' by adjusting the shift and add/sub constant:
2237 // add (srl (not X), 31), C --> add (sra X, 31), (C + 1)
2238 // sub C, (srl (not X), 31) --> add (srl X, 31), (C - 1)
2239 SDLoc DL(N);
2240 auto ShOpcode = IsAdd ? ISD::SRA : ISD::SRL;
2241 SDValue NewShift = DAG.getNode(ShOpcode, DL, VT, Not.getOperand(0), ShAmt);
2242 if (SDValue NewC =
2243 DAG.FoldConstantArithmetic(IsAdd ? ISD::ADD : ISD::SUB, DL, VT,
2244 {ConstantOp, DAG.getConstant(1, DL, VT)}))
2245 return DAG.getNode(ISD::ADD, DL, VT, NewShift, NewC);
2246 return SDValue();
2247}
2248
2249/// Try to fold a node that behaves like an ADD (note that N isn't necessarily
2250/// an ISD::ADD here, it could for example be an ISD::OR if we know that there
2251/// are no common bits set in the operands).
2252SDValue DAGCombiner::visitADDLike(SDNode *N) {
2253 SDValue N0 = N->getOperand(0);
2254 SDValue N1 = N->getOperand(1);
2255 EVT VT = N0.getValueType();
2256 SDLoc DL(N);
2257
2258 // fold vector ops
2259 if (VT.isVector()) {
2260 if (SDValue FoldedVOp = SimplifyVBinOp(N))
2261 return FoldedVOp;
2262
2263 // fold (add x, 0) -> x, vector edition
2264 if (ISD::isConstantSplatVectorAllZeros(N1.getNode()))
2265 return N0;
2266 if (ISD::isConstantSplatVectorAllZeros(N0.getNode()))
2267 return N1;
2268 }
2269
2270 // fold (add x, undef) -> undef
2271 if (N0.isUndef())
2272 return N0;
2273
2274 if (N1.isUndef())
2275 return N1;
2276
2277 if (DAG.isConstantIntBuildVectorOrConstantInt(N0)) {
2278 // canonicalize constant to RHS
2279 if (!DAG.isConstantIntBuildVectorOrConstantInt(N1))
2280 return DAG.getNode(ISD::ADD, DL, VT, N1, N0);
2281 // fold (add c1, c2) -> c1+c2
2282 return DAG.FoldConstantArithmetic(ISD::ADD, DL, VT, {N0, N1});
2283 }
2284
2285 // fold (add x, 0) -> x
2286 if (isNullConstant(N1))
2287 return N0;
2288
2289 if (isConstantOrConstantVector(N1, /* NoOpaque */ true)) {
2290 // fold ((A-c1)+c2) -> (A+(c2-c1))
2291 if (N0.getOpcode() == ISD::SUB &&
2292 isConstantOrConstantVector(N0.getOperand(1), /* NoOpaque */ true)) {
2293 SDValue Sub =
2294 DAG.FoldConstantArithmetic(ISD::SUB, DL, VT, {N1, N0.getOperand(1)});
2295 assert(Sub && "Constant folding failed")(static_cast <bool> (Sub && "Constant folding failed"
) ? void (0) : __assert_fail ("Sub && \"Constant folding failed\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 2295, __extension__ __PRETTY_FUNCTION__))
;
2296 return DAG.getNode(ISD::ADD, DL, VT, N0.getOperand(0), Sub);
2297 }
2298
2299 // fold ((c1-A)+c2) -> (c1+c2)-A
2300 if (N0.getOpcode() == ISD::SUB &&
2301 isConstantOrConstantVector(N0.getOperand(0), /* NoOpaque */ true)) {
2302 SDValue Add =
2303 DAG.FoldConstantArithmetic(ISD::ADD, DL, VT, {N1, N0.getOperand(0)});
2304 assert(Add && "Constant folding failed")(static_cast <bool> (Add && "Constant folding failed"
) ? void (0) : __assert_fail ("Add && \"Constant folding failed\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 2304, __extension__ __PRETTY_FUNCTION__))
;
2305 return DAG.getNode(ISD::SUB, DL, VT, Add, N0.getOperand(1));
2306 }
2307
2308 // add (sext i1 X), 1 -> zext (not i1 X)
2309 // We don't transform this pattern:
2310 // add (zext i1 X), -1 -> sext (not i1 X)
2311 // because most (?) targets generate better code for the zext form.
2312 if (N0.getOpcode() == ISD::SIGN_EXTEND && N0.hasOneUse() &&
2313 isOneOrOneSplat(N1)) {
2314 SDValue X = N0.getOperand(0);
2315 if ((!LegalOperations ||
2316 (TLI.isOperationLegal(ISD::XOR, X.getValueType()) &&
2317 TLI.isOperationLegal(ISD::ZERO_EXTEND, VT))) &&
2318 X.getScalarValueSizeInBits() == 1) {
2319 SDValue Not = DAG.getNOT(DL, X, X.getValueType());
2320 return DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Not);
2321 }
2322 }
2323
2324 // Fold (add (or x, c0), c1) -> (add x, (c0 + c1)) if (or x, c0) is
2325 // equivalent to (add x, c0).
2326 if (N0.getOpcode() == ISD::OR &&
2327 isConstantOrConstantVector(N0.getOperand(1), /* NoOpaque */ true) &&
2328 DAG.haveNoCommonBitsSet(N0.getOperand(0), N0.getOperand(1))) {
2329 if (SDValue Add0 = DAG.FoldConstantArithmetic(ISD::ADD, DL, VT,
2330 {N1, N0.getOperand(1)}))
2331 return DAG.getNode(ISD::ADD, DL, VT, N0.getOperand(0), Add0);
2332 }
2333 }
2334
2335 if (SDValue NewSel = foldBinOpIntoSelect(N))
2336 return NewSel;
2337
2338 // reassociate add
2339 if (!reassociationCanBreakAddressingModePattern(ISD::ADD, DL, N0, N1)) {
2340 if (SDValue RADD = reassociateOps(ISD::ADD, DL, N0, N1, N->getFlags()))
2341 return RADD;
2342
2343 // Reassociate (add (or x, c), y) -> (add add(x, y), c)) if (or x, c) is
2344 // equivalent to (add x, c).
2345 auto ReassociateAddOr = [&](SDValue N0, SDValue N1) {
2346 if (N0.getOpcode() == ISD::OR && N0.hasOneUse() &&
2347 isConstantOrConstantVector(N0.getOperand(1), /* NoOpaque */ true) &&
2348 DAG.haveNoCommonBitsSet(N0.getOperand(0), N0.getOperand(1))) {
2349 return DAG.getNode(ISD::ADD, DL, VT,
2350 DAG.getNode(ISD::ADD, DL, VT, N1, N0.getOperand(0)),
2351 N0.getOperand(1));
2352 }
2353 return SDValue();
2354 };
2355 if (SDValue Add = ReassociateAddOr(N0, N1))
2356 return Add;
2357 if (SDValue Add = ReassociateAddOr(N1, N0))
2358 return Add;
2359 }
2360 // fold ((0-A) + B) -> B-A
2361 if (N0.getOpcode() == ISD::SUB && isNullOrNullSplat(N0.getOperand(0)))
2362 return DAG.getNode(ISD::SUB, DL, VT, N1, N0.getOperand(1));
2363
2364 // fold (A + (0-B)) -> A-B
2365 if (N1.getOpcode() == ISD::SUB && isNullOrNullSplat(N1.getOperand(0)))
2366 return DAG.getNode(ISD::SUB, DL, VT, N0, N1.getOperand(1));
2367
2368 // fold (A+(B-A)) -> B
2369 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
2370 return N1.getOperand(0);
2371
2372 // fold ((B-A)+A) -> B
2373 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
2374 return N0.getOperand(0);
2375
2376 // fold ((A-B)+(C-A)) -> (C-B)
2377 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB &&
2378 N0.getOperand(0) == N1.getOperand(1))
2379 return DAG.getNode(ISD::SUB, DL, VT, N1.getOperand(0),
2380 N0.getOperand(1));
2381
2382 // fold ((A-B)+(B-C)) -> (A-C)
2383 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB &&
2384 N0.getOperand(1) == N1.getOperand(0))
2385 return DAG.getNode(ISD::SUB, DL, VT, N0.getOperand(0),
2386 N1.getOperand(1));
2387
2388 // fold (A+(B-(A+C))) to (B-C)
2389 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
2390 N0 == N1.getOperand(1).getOperand(0))
2391 return DAG.getNode(ISD::SUB, DL, VT, N1.getOperand(0),
2392 N1.getOperand(1).getOperand(1));
2393
2394 // fold (A+(B-(C+A))) to (B-C)
2395 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
2396 N0 == N1.getOperand(1).getOperand(1))
2397 return DAG.getNode(ISD::SUB, DL, VT, N1.getOperand(0),
2398 N1.getOperand(1).getOperand(0));
2399
2400 // fold (A+((B-A)+or-C)) to (B+or-C)
2401 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
2402 N1.getOperand(0).getOpcode() == ISD::SUB &&
2403 N0 == N1.getOperand(0).getOperand(1))
2404 return DAG.getNode(N1.getOpcode(), DL, VT, N1.getOperand(0).getOperand(0),
2405 N1.getOperand(1));
2406
2407 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
2408 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
2409 SDValue N00 = N0.getOperand(0);
2410 SDValue N01 = N0.getOperand(1);
2411 SDValue N10 = N1.getOperand(0);
2412 SDValue N11 = N1.getOperand(1);
2413
2414 if (isConstantOrConstantVector(N00) || isConstantOrConstantVector(N10))
2415 return DAG.getNode(ISD::SUB, DL, VT,
2416 DAG.getNode(ISD::ADD, SDLoc(N0), VT, N00, N10),
2417 DAG.getNode(ISD::ADD, SDLoc(N1), VT, N01, N11));
2418 }
2419
2420 // fold (add (umax X, C), -C) --> (usubsat X, C)
2421 if (N0.getOpcode() == ISD::UMAX && hasOperation(ISD::USUBSAT, VT)) {
2422 auto MatchUSUBSAT = [](ConstantSDNode *Max, ConstantSDNode *Op) {
2423 return (!Max && !Op) ||
2424 (Max && Op && Max->getAPIntValue() == (-Op->getAPIntValue()));
2425 };
2426 if (ISD::matchBinaryPredicate(N0.getOperand(1), N1, MatchUSUBSAT,
2427 /*AllowUndefs*/ true))
2428 return DAG.getNode(ISD::USUBSAT, DL, VT, N0.getOperand(0),
2429 N0.getOperand(1));
2430 }
2431
2432 if (SimplifyDemandedBits(SDValue(N, 0)))
2433 return SDValue(N, 0);
2434
2435 if (isOneOrOneSplat(N1)) {
2436 // fold (add (xor a, -1), 1) -> (sub 0, a)
2437 if (isBitwiseNot(N0))
2438 return DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT),
2439 N0.getOperand(0));
2440
2441 // fold (add (add (xor a, -1), b), 1) -> (sub b, a)
2442 if (N0.getOpcode() == ISD::ADD ||
2443 N0.getOpcode() == ISD::UADDO ||
2444 N0.getOpcode() == ISD::SADDO) {
2445 SDValue A, Xor;
2446
2447 if (isBitwiseNot(N0.getOperand(0))) {
2448 A = N0.getOperand(1);
2449 Xor = N0.getOperand(0);
2450 } else if (isBitwiseNot(N0.getOperand(1))) {
2451 A = N0.getOperand(0);
2452 Xor = N0.getOperand(1);
2453 }
2454
2455 if (Xor)
2456 return DAG.getNode(ISD::SUB, DL, VT, A, Xor.getOperand(0));
2457 }
2458
2459 // Look for:
2460 // add (add x, y), 1
2461 // And if the target does not like this form then turn into:
2462 // sub y, (xor x, -1)
2463 if (!TLI.preferIncOfAddToSubOfNot(VT) && N0.hasOneUse() &&
2464 N0.getOpcode() == ISD::ADD) {
2465 SDValue Not = DAG.getNode(ISD::XOR, DL, VT, N0.getOperand(0),
2466 DAG.getAllOnesConstant(DL, VT));
2467 return DAG.getNode(ISD::SUB, DL, VT, N0.getOperand(1), Not);
2468 }
2469 }
2470
2471 // (x - y) + -1 -> add (xor y, -1), x
2472 if (N0.hasOneUse() && N0.getOpcode() == ISD::SUB &&
2473 isAllOnesOrAllOnesSplat(N1)) {
2474 SDValue Xor = DAG.getNode(ISD::XOR, DL, VT, N0.getOperand(1), N1);
2475 return DAG.getNode(ISD::ADD, DL, VT, Xor, N0.getOperand(0));
2476 }
2477
2478 if (SDValue Combined = visitADDLikeCommutative(N0, N1, N))
2479 return Combined;
2480
2481 if (SDValue Combined = visitADDLikeCommutative(N1, N0, N))
2482 return Combined;
2483
2484 return SDValue();
2485}
2486
2487SDValue DAGCombiner::visitADD(SDNode *N) {
2488 SDValue N0 = N->getOperand(0);
2489 SDValue N1 = N->getOperand(1);
2490 EVT VT = N0.getValueType();
2491 SDLoc DL(N);
2492
2493 if (SDValue Combined = visitADDLike(N))
2494 return Combined;
2495
2496 if (SDValue V = foldAddSubBoolOfMaskedVal(N, DAG))
2497 return V;
2498
2499 if (SDValue V = foldAddSubOfSignBit(N, DAG))
2500 return V;
2501
2502 // fold (a+b) -> (a|b) iff a and b share no bits.
2503 if ((!LegalOperations || TLI.isOperationLegal(ISD::OR, VT)) &&
2504 DAG.haveNoCommonBitsSet(N0, N1))
2505 return DAG.getNode(ISD::OR, DL, VT, N0, N1);
2506
2507 // Fold (add (vscale * C0), (vscale * C1)) to (vscale * (C0 + C1)).
2508 if (N0.getOpcode() == ISD::VSCALE && N1.getOpcode() == ISD::VSCALE) {
2509 const APInt &C0 = N0->getConstantOperandAPInt(0);
2510 const APInt &C1 = N1->getConstantOperandAPInt(0);
2511 return DAG.getVScale(DL, VT, C0 + C1);
2512 }
2513
2514 // fold a+vscale(c1)+vscale(c2) -> a+vscale(c1+c2)
2515 if ((N0.getOpcode() == ISD::ADD) &&
2516 (N0.getOperand(1).getOpcode() == ISD::VSCALE) &&
2517 (N1.getOpcode() == ISD::VSCALE)) {
2518 const APInt &VS0 = N0.getOperand(1)->getConstantOperandAPInt(0);
2519 const APInt &VS1 = N1->getConstantOperandAPInt(0);
2520 SDValue VS = DAG.getVScale(DL, VT, VS0 + VS1);
2521 return DAG.getNode(ISD::ADD, DL, VT, N0.getOperand(0), VS);
2522 }
2523
2524 // Fold (add step_vector(c1), step_vector(c2) to step_vector(c1+c2))
2525 if (N0.getOpcode() == ISD::STEP_VECTOR &&
2526 N1.getOpcode() == ISD::STEP_VECTOR) {
2527 const APInt &C0 = N0->getConstantOperandAPInt(0);
2528 const APInt &C1 = N1->getConstantOperandAPInt(0);
2529 APInt NewStep = C0 + C1;
2530 return DAG.getStepVector(DL, VT, NewStep);
2531 }
2532
2533 // Fold a + step_vector(c1) + step_vector(c2) to a + step_vector(c1+c2)
2534 if ((N0.getOpcode() == ISD::ADD) &&
2535 (N0.getOperand(1).getOpcode() == ISD::STEP_VECTOR) &&
2536 (N1.getOpcode() == ISD::STEP_VECTOR)) {
2537 const APInt &SV0 = N0.getOperand(1)->getConstantOperandAPInt(0);
2538 const APInt &SV1 = N1->getConstantOperandAPInt(0);
2539 APInt NewStep = SV0 + SV1;
2540 SDValue SV = DAG.getStepVector(DL, VT, NewStep);
2541 return DAG.getNode(ISD::ADD, DL, VT, N0.getOperand(0), SV);
2542 }
2543
2544 return SDValue();
2545}
2546
2547SDValue DAGCombiner::visitADDSAT(SDNode *N) {
2548 unsigned Opcode = N->getOpcode();
2549 SDValue N0 = N->getOperand(0);
2550 SDValue N1 = N->getOperand(1);
2551 EVT VT = N0.getValueType();
2552 SDLoc DL(N);
2553
2554 // fold vector ops
2555 if (VT.isVector()) {
2556 // TODO SimplifyVBinOp
2557
2558 // fold (add_sat x, 0) -> x, vector edition
2559 if (ISD::isConstantSplatVectorAllZeros(N1.getNode()))
2560 return N0;
2561 if (ISD::isConstantSplatVectorAllZeros(N0.getNode()))
2562 return N1;
2563 }
2564
2565 // fold (add_sat x, undef) -> -1
2566 if (N0.isUndef() || N1.isUndef())
2567 return DAG.getAllOnesConstant(DL, VT);
2568
2569 if (DAG.isConstantIntBuildVectorOrConstantInt(N0)) {
2570 // canonicalize constant to RHS
2571 if (!DAG.isConstantIntBuildVectorOrConstantInt(N1))
2572 return DAG.getNode(Opcode, DL, VT, N1, N0);
2573 // fold (add_sat c1, c2) -> c3
2574 return DAG.FoldConstantArithmetic(Opcode, DL, VT, {N0, N1});
2575 }
2576
2577 // fold (add_sat x, 0) -> x
2578 if (isNullConstant(N1))
2579 return N0;
2580
2581 // If it cannot overflow, transform into an add.
2582 if (Opcode == ISD::UADDSAT)
2583 if (DAG.computeOverflowKind(N0, N1) == SelectionDAG::OFK_Never)
2584 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
2585
2586 return SDValue();
2587}
2588
2589static SDValue getAsCarry(const TargetLowering &TLI, SDValue V) {
2590 bool Masked = false;
2591
2592 // First, peel away TRUNCATE/ZERO_EXTEND/AND nodes due to legalization.
2593 while (true) {
2594 if (V.getOpcode() == ISD::TRUNCATE || V.getOpcode() == ISD::ZERO_EXTEND) {
2595 V = V.getOperand(0);
2596 continue;
2597 }
2598
2599 if (V.getOpcode() == ISD::AND && isOneConstant(V.getOperand(1))) {
2600 Masked = true;
2601 V = V.getOperand(0);
2602 continue;
2603 }
2604
2605 break;
2606 }
2607
2608 // If this is not a carry, return.
2609 if (V.getResNo() != 1)
2610 return SDValue();
2611
2612 if (V.getOpcode() != ISD::ADDCARRY && V.getOpcode() != ISD::SUBCARRY &&
2613 V.getOpcode() != ISD::UADDO && V.getOpcode() != ISD::USUBO)
2614 return SDValue();
2615
2616 EVT VT = V.getNode()->getValueType(0);
2617 if (!TLI.isOperationLegalOrCustom(V.getOpcode(), VT))
2618 return SDValue();
2619
2620 // If the result is masked, then no matter what kind of bool it is we can
2621 // return. If it isn't, then we need to make sure the bool type is either 0 or
2622 // 1 and not other values.
2623 if (Masked ||
2624 TLI.getBooleanContents(V.getValueType()) ==
2625 TargetLoweringBase::ZeroOrOneBooleanContent)
2626 return V;
2627
2628 return SDValue();
2629}
2630
2631/// Given the operands of an add/sub operation, see if the 2nd operand is a
2632/// masked 0/1 whose source operand is actually known to be 0/-1. If so, invert
2633/// the opcode and bypass the mask operation.
2634static SDValue foldAddSubMasked1(bool IsAdd, SDValue N0, SDValue N1,
2635 SelectionDAG &DAG, const SDLoc &DL) {
2636 if (N1.getOpcode() != ISD::AND || !isOneOrOneSplat(N1->getOperand(1)))
2637 return SDValue();
2638
2639 EVT VT = N0.getValueType();
2640 if (DAG.ComputeNumSignBits(N1.getOperand(0)) != VT.getScalarSizeInBits())
2641 return SDValue();
2642
2643 // add N0, (and (AssertSext X, i1), 1) --> sub N0, X
2644 // sub N0, (and (AssertSext X, i1), 1) --> add N0, X
2645 return DAG.getNode(IsAdd ? ISD::SUB : ISD::ADD, DL, VT, N0, N1.getOperand(0));
2646}
2647
2648/// Helper for doing combines based on N0 and N1 being added to each other.
2649SDValue DAGCombiner::visitADDLikeCommutative(SDValue N0, SDValue N1,
2650 SDNode *LocReference) {
2651 EVT VT = N0.getValueType();
2652 SDLoc DL(LocReference);
2653
2654 // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n))
2655 if (N1.getOpcode() == ISD::SHL && N1.getOperand(0).getOpcode() == ISD::SUB &&
2656 isNullOrNullSplat(N1.getOperand(0).getOperand(0)))
2657 return DAG.getNode(ISD::SUB, DL, VT, N0,
2658 DAG.getNode(ISD::SHL, DL, VT,
2659 N1.getOperand(0).getOperand(1),
2660 N1.getOperand(1)));
2661
2662 if (SDValue V = foldAddSubMasked1(true, N0, N1, DAG, DL))
2663 return V;
2664
2665 // Look for:
2666 // add (add x, 1), y
2667 // And if the target does not like this form then turn into:
2668 // sub y, (xor x, -1)
2669 if (!TLI.preferIncOfAddToSubOfNot(VT) && N0.hasOneUse() &&
2670 N0.getOpcode() == ISD::ADD && isOneOrOneSplat(N0.getOperand(1))) {
2671 SDValue Not = DAG.getNode(ISD::XOR, DL, VT, N0.getOperand(0),
2672 DAG.getAllOnesConstant(DL, VT));
2673 return DAG.getNode(ISD::SUB, DL, VT, N1, Not);
2674 }
2675
2676 // Hoist one-use subtraction by non-opaque constant:
2677 // (x - C) + y -> (x + y) - C
2678 // This is necessary because SUB(X,C) -> ADD(X,-C) doesn't work for vectors.
2679 if (N0.hasOneUse() && N0.getOpcode() == ISD::SUB &&
2680 isConstantOrConstantVector(N0.getOperand(1), /*NoOpaques=*/true)) {
2681 SDValue Add = DAG.getNode(ISD::ADD, DL, VT, N0.getOperand(0), N1);
2682 return DAG.getNode(ISD::SUB, DL, VT, Add, N0.getOperand(1));
2683 }
2684 // Hoist one-use subtraction from non-opaque constant:
2685 // (C - x) + y -> (y - x) + C
2686 if (N0.hasOneUse() && N0.getOpcode() == ISD::SUB &&
2687 isConstantOrConstantVector(N0.getOperand(0), /*NoOpaques=*/true)) {
2688 SDValue Sub = DAG.getNode(ISD::SUB, DL, VT, N1, N0.getOperand(1));
2689 return DAG.getNode(ISD::ADD, DL, VT, Sub, N0.getOperand(0));
2690 }
2691
2692 // If the target's bool is represented as 0/1, prefer to make this 'sub 0/1'
2693 // rather than 'add 0/-1' (the zext should get folded).
2694 // add (sext i1 Y), X --> sub X, (zext i1 Y)
2695 if (N0.getOpcode() == ISD::SIGN_EXTEND &&
2696 N0.getOperand(0).getScalarValueSizeInBits() == 1 &&
2697 TLI.getBooleanContents(VT) == TargetLowering::ZeroOrOneBooleanContent) {
2698 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0));
2699 return DAG.getNode(ISD::SUB, DL, VT, N1, ZExt);
2700 }
2701
2702 // add X, (sextinreg Y i1) -> sub X, (and Y 1)
2703 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
2704 VTSDNode *TN = cast<VTSDNode>(N1.getOperand(1));
2705 if (TN->getVT() == MVT::i1) {
2706 SDValue ZExt = DAG.getNode(ISD::AND, DL, VT, N1.getOperand(0),
2707 DAG.getConstant(1, DL, VT));
2708 return DAG.getNode(ISD::SUB, DL, VT, N0, ZExt);
2709 }
2710 }
2711
2712 // (add X, (addcarry Y, 0, Carry)) -> (addcarry X, Y, Carry)
2713 if (N1.getOpcode() == ISD::ADDCARRY && isNullConstant(N1.getOperand(1)) &&
2714 N1.getResNo() == 0)
2715 return DAG.getNode(ISD::ADDCARRY, DL, N1->getVTList(),
2716 N0, N1.getOperand(0), N1.getOperand(2));
2717
2718 // (add X, Carry) -> (addcarry X, 0, Carry)
2719 if (TLI.isOperationLegalOrCustom(ISD::ADDCARRY, VT))
2720 if (SDValue Carry = getAsCarry(TLI, N1))
2721 return DAG.getNode(ISD::ADDCARRY, DL,
2722 DAG.getVTList(VT, Carry.getValueType()), N0,
2723 DAG.getConstant(0, DL, VT), Carry);
2724
2725 return SDValue();
2726}
2727
2728SDValue DAGCombiner::visitADDC(SDNode *N) {
2729 SDValue N0 = N->getOperand(0);
2730 SDValue N1 = N->getOperand(1);
2731 EVT VT = N0.getValueType();
2732 SDLoc DL(N);
2733
2734 // If the flag result is dead, turn this into an ADD.
2735 if (!N->hasAnyUseOfValue(1))
2736 return CombineTo(N, DAG.getNode(ISD::ADD, DL, VT, N0, N1),
2737 DAG.getNode(ISD::CARRY_FALSE, DL, MVT::Glue));
2738
2739 // canonicalize constant to RHS.
2740 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2741 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2742 if (N0C && !N1C)
2743 return DAG.getNode(ISD::ADDC, DL, N->getVTList(), N1, N0);
2744
2745 // fold (addc x, 0) -> x + no carry out
2746 if (isNullConstant(N1))
2747 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
2748 DL, MVT::Glue));
2749
2750 // If it cannot overflow, transform into an add.
2751 if (DAG.computeOverflowKind(N0, N1) == SelectionDAG::OFK_Never)
2752 return CombineTo(N, DAG.getNode(ISD::ADD, DL, VT, N0, N1),
2753 DAG.getNode(ISD::CARRY_FALSE, DL, MVT::Glue));
2754
2755 return SDValue();
2756}
2757
2758/**
2759 * Flips a boolean if it is cheaper to compute. If the Force parameters is set,
2760 * then the flip also occurs if computing the inverse is the same cost.
2761 * This function returns an empty SDValue in case it cannot flip the boolean
2762 * without increasing the cost of the computation. If you want to flip a boolean
2763 * no matter what, use DAG.getLogicalNOT.
2764 */
2765static SDValue extractBooleanFlip(SDValue V, SelectionDAG &DAG,
2766 const TargetLowering &TLI,
2767 bool Force) {
2768 if (Force && isa<ConstantSDNode>(V))
2769 return DAG.getLogicalNOT(SDLoc(V), V, V.getValueType());
2770
2771 if (V.getOpcode() != ISD::XOR)
2772 return SDValue();
2773
2774 ConstantSDNode *Const = isConstOrConstSplat(V.getOperand(1), false);
2775 if (!Const)
2776 return SDValue();
2777
2778 EVT VT = V.getValueType();
2779
2780 bool IsFlip = false;
2781 switch(TLI.getBooleanContents(VT)) {
2782 case TargetLowering::ZeroOrOneBooleanContent:
2783 IsFlip = Const->isOne();
2784 break;
2785 case TargetLowering::ZeroOrNegativeOneBooleanContent:
2786 IsFlip = Const->isAllOnesValue();
2787 break;
2788 case TargetLowering::UndefinedBooleanContent:
2789 IsFlip = (Const->getAPIntValue() & 0x01) == 1;
2790 break;
2791 }
2792
2793 if (IsFlip)
2794 return V.getOperand(0);
2795 if (Force)
2796 return DAG.getLogicalNOT(SDLoc(V), V, V.getValueType());
2797 return SDValue();
2798}
2799
2800SDValue DAGCombiner::visitADDO(SDNode *N) {
2801 SDValue N0 = N->getOperand(0);
2802 SDValue N1 = N->getOperand(1);
2803 EVT VT = N0.getValueType();
2804 bool IsSigned = (ISD::SADDO == N->getOpcode());
2805
2806 EVT CarryVT = N->getValueType(1);
2807 SDLoc DL(N);
2808
2809 // If the flag result is dead, turn this into an ADD.
2810 if (!N->hasAnyUseOfValue(1))
2811 return CombineTo(N, DAG.getNode(ISD::ADD, DL, VT, N0, N1),
2812 DAG.getUNDEF(CarryVT));
2813
2814 // canonicalize constant to RHS.
2815 if (DAG.isConstantIntBuildVectorOrConstantInt(N0) &&
2816 !DAG.isConstantIntBuildVectorOrConstantInt(N1))
2817 return DAG.getNode(N->getOpcode(), DL, N->getVTList(), N1, N0);
2818
2819 // fold (addo x, 0) -> x + no carry out
2820 if (isNullOrNullSplat(N1))
2821 return CombineTo(N, N0, DAG.getConstant(0, DL, CarryVT));
2822
2823 if (!IsSigned) {
2824 // If it cannot overflow, transform into an add.
2825 if (DAG.computeOverflowKind(N0, N1) == SelectionDAG::OFK_Never)
2826 return CombineTo(N, DAG.getNode(ISD::ADD, DL, VT, N0, N1),
2827 DAG.getConstant(0, DL, CarryVT));
2828
2829 // fold (uaddo (xor a, -1), 1) -> (usub 0, a) and flip carry.
2830 if (isBitwiseNot(N0) && isOneOrOneSplat(N1)) {
2831 SDValue Sub = DAG.getNode(ISD::USUBO, DL, N->getVTList(),
2832 DAG.getConstant(0, DL, VT), N0.getOperand(0));
2833 return CombineTo(
2834 N, Sub, DAG.getLogicalNOT(DL, Sub.getValue(1), Sub->getValueType(1)));
2835 }
2836
2837 if (SDValue Combined = visitUADDOLike(N0, N1, N))
2838 return Combined;
2839
2840 if (SDValue Combined = visitUADDOLike(N1, N0, N))
2841 return Combined;
2842 }
2843
2844 return SDValue();
2845}
2846
2847SDValue DAGCombiner::visitUADDOLike(SDValue N0, SDValue N1, SDNode *N) {
2848 EVT VT = N0.getValueType();
2849 if (VT.isVector())
2850 return SDValue();
2851
2852 // (uaddo X, (addcarry Y, 0, Carry)) -> (addcarry X, Y, Carry)
2853 // If Y + 1 cannot overflow.
2854 if (N1.getOpcode() == ISD::ADDCARRY && isNullConstant(N1.getOperand(1))) {
2855 SDValue Y = N1.getOperand(0);
2856 SDValue One = DAG.getConstant(1, SDLoc(N), Y.getValueType());
2857 if (DAG.computeOverflowKind(Y, One) == SelectionDAG::OFK_Never)
2858 return DAG.getNode(ISD::ADDCARRY, SDLoc(N), N->getVTList(), N0, Y,
2859 N1.getOperand(2));
2860 }
2861
2862 // (uaddo X, Carry) -> (addcarry X, 0, Carry)
2863 if (TLI.isOperationLegalOrCustom(ISD::ADDCARRY, VT))
2864 if (SDValue Carry = getAsCarry(TLI, N1))
2865 return DAG.getNode(ISD::ADDCARRY, SDLoc(N), N->getVTList(), N0,
2866 DAG.getConstant(0, SDLoc(N), VT), Carry);
2867
2868 return SDValue();
2869}
2870
2871SDValue DAGCombiner::visitADDE(SDNode *N) {
2872 SDValue N0 = N->getOperand(0);
2873 SDValue N1 = N->getOperand(1);
2874 SDValue CarryIn = N->getOperand(2);
2875
2876 // canonicalize constant to RHS
2877 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2878 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2879 if (N0C && !N1C)
2880 return DAG.getNode(ISD::ADDE, SDLoc(N), N->getVTList(),
2881 N1, N0, CarryIn);
2882
2883 // fold (adde x, y, false) -> (addc x, y)
2884 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
2885 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N0, N1);
2886
2887 return SDValue();
2888}
2889
2890SDValue DAGCombiner::visitADDCARRY(SDNode *N) {
2891 SDValue N0 = N->getOperand(0);
2892 SDValue N1 = N->getOperand(1);
2893 SDValue CarryIn = N->getOperand(2);
2894 SDLoc DL(N);
2895
2896 // canonicalize constant to RHS
2897 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2898 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2899 if (N0C && !N1C)
2900 return DAG.getNode(ISD::ADDCARRY, DL, N->getVTList(), N1, N0, CarryIn);
2901
2902 // fold (addcarry x, y, false) -> (uaddo x, y)
2903 if (isNullConstant(CarryIn)) {
2904 if (!LegalOperations ||
2905 TLI.isOperationLegalOrCustom(ISD::UADDO, N->getValueType(0)))
2906 return DAG.getNode(ISD::UADDO, DL, N->getVTList(), N0, N1);
2907 }
2908
2909 // fold (addcarry 0, 0, X) -> (and (ext/trunc X), 1) and no carry.
2910 if (isNullConstant(N0) && isNullConstant(N1)) {
2911 EVT VT = N0.getValueType();
2912 EVT CarryVT = CarryIn.getValueType();
2913 SDValue CarryExt = DAG.getBoolExtOrTrunc(CarryIn, DL, VT, CarryVT);
2914 AddToWorklist(CarryExt.getNode());
2915 return CombineTo(N, DAG.getNode(ISD::AND, DL, VT, CarryExt,
2916 DAG.getConstant(1, DL, VT)),
2917 DAG.getConstant(0, DL, CarryVT));
2918 }
2919
2920 if (SDValue Combined = visitADDCARRYLike(N0, N1, CarryIn, N))
2921 return Combined;
2922
2923 if (SDValue Combined = visitADDCARRYLike(N1, N0, CarryIn, N))
2924 return Combined;
2925
2926 return SDValue();
2927}
2928
2929SDValue DAGCombiner::visitSADDO_CARRY(SDNode *N) {
2930 SDValue N0 = N->getOperand(0);
2931 SDValue N1 = N->getOperand(1);
2932 SDValue CarryIn = N->getOperand(2);
2933 SDLoc DL(N);
2934
2935 // canonicalize constant to RHS
2936 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2937 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2938 if (N0C && !N1C)
2939 return DAG.getNode(ISD::SADDO_CARRY, DL, N->getVTList(), N1, N0, CarryIn);
2940
2941 // fold (saddo_carry x, y, false) -> (saddo x, y)
2942 if (isNullConstant(CarryIn)) {
2943 if (!LegalOperations ||
2944 TLI.isOperationLegalOrCustom(ISD::SADDO, N->getValueType(0)))
2945 return DAG.getNode(ISD::SADDO, DL, N->getVTList(), N0, N1);
2946 }
2947
2948 return SDValue();
2949}
2950
2951/**
2952 * If we are facing some sort of diamond carry propapagtion pattern try to
2953 * break it up to generate something like:
2954 * (addcarry X, 0, (addcarry A, B, Z):Carry)
2955 *
2956 * The end result is usually an increase in operation required, but because the
2957 * carry is now linearized, other tranforms can kick in and optimize the DAG.
2958 *
2959 * Patterns typically look something like
2960 * (uaddo A, B)
2961 * / \
2962 * Carry Sum
2963 * | \
2964 * | (addcarry *, 0, Z)
2965 * | /
2966 * \ Carry
2967 * | /
2968 * (addcarry X, *, *)
2969 *
2970 * But numerous variation exist. Our goal is to identify A, B, X and Z and
2971 * produce a combine with a single path for carry propagation.
2972 */
2973static SDValue combineADDCARRYDiamond(DAGCombiner &Combiner, SelectionDAG &DAG,
2974 SDValue X, SDValue Carry0, SDValue Carry1,
2975 SDNode *N) {
2976 if (Carry1.getResNo() != 1 || Carry0.getResNo() != 1)
2977 return SDValue();
2978 if (Carry1.getOpcode() != ISD::UADDO)
2979 return SDValue();
2980
2981 SDValue Z;
2982
2983 /**
2984 * First look for a suitable Z. It will present itself in the form of
2985 * (addcarry Y, 0, Z) or its equivalent (uaddo Y, 1) for Z=true
2986 */
2987 if (Carry0.getOpcode() == ISD::ADDCARRY &&
2988 isNullConstant(Carry0.getOperand(1))) {
2989 Z = Carry0.getOperand(2);
2990 } else if (Carry0.getOpcode() == ISD::UADDO &&
2991 isOneConstant(Carry0.getOperand(1))) {
2992 EVT VT = Combiner.getSetCCResultType(Carry0.getValueType());
2993 Z = DAG.getConstant(1, SDLoc(Carry0.getOperand(1)), VT);
2994 } else {
2995 // We couldn't find a suitable Z.
2996 return SDValue();
2997 }
2998
2999
3000 auto cancelDiamond = [&](SDValue A,SDValue B) {
3001 SDLoc DL(N);
3002 SDValue NewY = DAG.getNode(ISD::ADDCARRY, DL, Carry0->getVTList(), A, B, Z);
3003 Combiner.AddToWorklist(NewY.getNode());
3004 return DAG.getNode(ISD::ADDCARRY, DL, N->getVTList(), X,
3005 DAG.getConstant(0, DL, X.getValueType()),
3006 NewY.getValue(1));
3007 };
3008
3009 /**
3010 * (uaddo A, B)
3011 * |
3012 * Sum
3013 * |
3014 * (addcarry *, 0, Z)
3015 */
3016 if (Carry0.getOperand(0) == Carry1.getValue(0)) {
3017 return cancelDiamond(Carry1.getOperand(0), Carry1.getOperand(1));
3018 }
3019
3020 /**
3021 * (addcarry A, 0, Z)
3022 * |
3023 * Sum
3024 * |
3025 * (uaddo *, B)
3026 */
3027 if (Carry1.getOperand(0) == Carry0.getValue(0)) {
3028 return cancelDiamond(Carry0.getOperand(0), Carry1.getOperand(1));
3029 }
3030
3031 if (Carry1.getOperand(1) == Carry0.getValue(0)) {
3032 return cancelDiamond(Carry1.getOperand(0), Carry0.getOperand(0));
3033 }
3034
3035 return SDValue();
3036}
3037
3038// If we are facing some sort of diamond carry/borrow in/out pattern try to
3039// match patterns like:
3040//
3041// (uaddo A, B) CarryIn
3042// | \ |
3043// | \ |
3044// PartialSum PartialCarryOutX /
3045// | | /
3046// | ____|____________/
3047// | / |
3048// (uaddo *, *) \________
3049// | \ \
3050// | \ |
3051// | PartialCarryOutY |
3052// | \ |
3053// | \ /
3054// AddCarrySum | ______/
3055// | /
3056// CarryOut = (or *, *)
3057//
3058// And generate ADDCARRY (or SUBCARRY) with two result values:
3059//
3060// {AddCarrySum, CarryOut} = (addcarry A, B, CarryIn)
3061//
3062// Our goal is to identify A, B, and CarryIn and produce ADDCARRY/SUBCARRY with
3063// a single path for carry/borrow out propagation:
3064static SDValue combineCarryDiamond(DAGCombiner &Combiner, SelectionDAG &DAG,
3065 const TargetLowering &TLI, SDValue Carry0,
3066 SDValue Carry1, SDNode *N) {
3067 if (Carry0.getResNo() != 1 || Carry1.getResNo() != 1)
3068 return SDValue();
3069 unsigned Opcode = Carry0.getOpcode();
3070 if (Opcode != Carry1.getOpcode())
3071 return SDValue();
3072 if (Opcode != ISD::UADDO && Opcode != ISD::USUBO)
3073 return SDValue();
3074
3075 // Canonicalize the add/sub of A and B as Carry0 and the add/sub of the
3076 // carry/borrow in as Carry1. (The top and middle uaddo nodes respectively in
3077 // the above ASCII art.)
3078 if (Carry1.getOperand(0) != Carry0.getValue(0) &&
3079 Carry1.getOperand(1) != Carry0.getValue(0))
3080 std::swap(Carry0, Carry1);
3081 if (Carry1.getOperand(0) != Carry0.getValue(0) &&
3082 Carry1.getOperand(1) != Carry0.getValue(0))
3083 return SDValue();
3084
3085 // The carry in value must be on the righthand side for subtraction.
3086 unsigned CarryInOperandNum =
3087 Carry1.getOperand(0) == Carry0.getValue(0) ? 1 : 0;
3088 if (Opcode == ISD::USUBO && CarryInOperandNum != 1)
3089 return SDValue();
3090 SDValue CarryIn = Carry1.getOperand(CarryInOperandNum);
3091
3092 unsigned NewOp = Opcode == ISD::UADDO ? ISD::ADDCARRY : ISD::SUBCARRY;
3093 if (!TLI.isOperationLegalOrCustom(NewOp, Carry0.getValue(0).getValueType()))
3094 return SDValue();
3095
3096 // Verify that the carry/borrow in is plausibly a carry/borrow bit.
3097 // TODO: make getAsCarry() aware of how partial carries are merged.
3098 if (CarryIn.getOpcode() != ISD::ZERO_EXTEND)
3099 return SDValue();
3100 CarryIn = CarryIn.getOperand(0);
3101 if (CarryIn.getValueType() != MVT::i1)
3102 return SDValue();
3103
3104 SDLoc DL(N);
3105 SDValue Merged =
3106 DAG.getNode(NewOp, DL, Carry1->getVTList(), Carry0.getOperand(0),
3107 Carry0.getOperand(1), CarryIn);
3108
3109 // Please note that because we have proven that the result of the UADDO/USUBO
3110 // of A and B feeds into the UADDO/USUBO that does the carry/borrow in, we can
3111 // therefore prove that if the first UADDO/USUBO overflows, the second
3112 // UADDO/USUBO cannot. For example consider 8-bit numbers where 0xFF is the
3113 // maximum value.
3114 //
3115 // 0xFF + 0xFF == 0xFE with carry but 0xFE + 1 does not carry
3116 // 0x00 - 0xFF == 1 with a carry/borrow but 1 - 1 == 0 (no carry/borrow)
3117 //
3118 // This is important because it means that OR and XOR can be used to merge
3119 // carry flags; and that AND can return a constant zero.
3120 //
3121 // TODO: match other operations that can merge flags (ADD, etc)
3122 DAG.ReplaceAllUsesOfValueWith(Carry1.getValue(0), Merged.getValue(0));
3123 if (N->getOpcode() == ISD::AND)
3124 return DAG.getConstant(0, DL, MVT::i1);
3125 return Merged.getValue(1);
3126}
3127
3128SDValue DAGCombiner::visitADDCARRYLike(SDValue N0, SDValue N1, SDValue CarryIn,
3129 SDNode *N) {
3130 // fold (addcarry (xor a, -1), b, c) -> (subcarry b, a, !c) and flip carry.
3131 if (isBitwiseNot(N0))
3132 if (SDValue NotC = extractBooleanFlip(CarryIn, DAG, TLI, true)) {
3133 SDLoc DL(N);
3134 SDValue Sub = DAG.getNode(ISD::SUBCARRY, DL, N->getVTList(), N1,
3135 N0.getOperand(0), NotC);
3136 return CombineTo(
3137 N, Sub, DAG.getLogicalNOT(DL, Sub.getValue(1), Sub->getValueType(1)));
3138 }
3139
3140 // Iff the flag result is dead:
3141 // (addcarry (add|uaddo X, Y), 0, Carry) -> (addcarry X, Y, Carry)
3142 // Don't do this if the Carry comes from the uaddo. It won't remove the uaddo
3143 // or the dependency between the instructions.
3144 if ((N0.getOpcode() == ISD::ADD ||
3145 (N0.getOpcode() == ISD::UADDO && N0.getResNo() == 0 &&
3146 N0.getValue(1) != CarryIn)) &&
3147 isNullConstant(N1) && !N->hasAnyUseOfValue(1))
3148 return DAG.getNode(ISD::ADDCARRY, SDLoc(N), N->getVTList(),
3149 N0.getOperand(0), N0.getOperand(1), CarryIn);
3150
3151 /**
3152 * When one of the addcarry argument is itself a carry, we may be facing
3153 * a diamond carry propagation. In which case we try to transform the DAG
3154 * to ensure linear carry propagation if that is possible.
3155 */
3156 if (auto Y = getAsCarry(TLI, N1)) {
3157 // Because both are carries, Y and Z can be swapped.
3158 if (auto R = combineADDCARRYDiamond(*this, DAG, N0, Y, CarryIn, N))
3159 return R;
3160 if (auto R = combineADDCARRYDiamond(*this, DAG, N0, CarryIn, Y, N))
3161 return R;
3162 }
3163
3164 return SDValue();
3165}
3166
3167// Attempt to create a USUBSAT(LHS, RHS) node with DstVT, performing a
3168// clamp/truncation if necessary.
3169static SDValue getTruncatedUSUBSAT(EVT DstVT, EVT SrcVT, SDValue LHS,
3170 SDValue RHS, SelectionDAG &DAG,
3171 const SDLoc &DL) {
3172 assert(DstVT.getScalarSizeInBits() <= SrcVT.getScalarSizeInBits() &&(static_cast <bool> (DstVT.getScalarSizeInBits() <= SrcVT
.getScalarSizeInBits() && "Illegal truncation") ? void
(0) : __assert_fail ("DstVT.getScalarSizeInBits() <= SrcVT.getScalarSizeInBits() && \"Illegal truncation\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 3173, __extension__ __PRETTY_FUNCTION__))
3173 "Illegal truncation")(static_cast <bool> (DstVT.getScalarSizeInBits() <= SrcVT
.getScalarSizeInBits() && "Illegal truncation") ? void
(0) : __assert_fail ("DstVT.getScalarSizeInBits() <= SrcVT.getScalarSizeInBits() && \"Illegal truncation\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 3173, __extension__ __PRETTY_FUNCTION__))
;
3174
3175 if (DstVT == SrcVT)
3176 return DAG.getNode(ISD::USUBSAT, DL, DstVT, LHS, RHS);
3177
3178 // If the LHS is zero-extended then we can perform the USUBSAT as DstVT by
3179 // clamping RHS.
3180 APInt UpperBits = APInt::getBitsSetFrom(SrcVT.getScalarSizeInBits(),
3181 DstVT.getScalarSizeInBits());
3182 if (!DAG.MaskedValueIsZero(LHS, UpperBits))
3183 return SDValue();
3184
3185 SDValue SatLimit =
3186 DAG.getConstant(APInt::getLowBitsSet(SrcVT.getScalarSizeInBits(),
3187 DstVT.getScalarSizeInBits()),
3188 DL, SrcVT);
3189 RHS = DAG.getNode(ISD::UMIN, DL, SrcVT, RHS, SatLimit);
3190 RHS = DAG.getNode(ISD::TRUNCATE, DL, DstVT, RHS);
3191 LHS = DAG.getNode(ISD::TRUNCATE, DL, DstVT, LHS);
3192 return DAG.getNode(ISD::USUBSAT, DL, DstVT, LHS, RHS);
3193}
3194
3195// Try to find umax(a,b) - b or a - umin(a,b) patterns that may be converted to
3196// usubsat(a,b), optionally as a truncated type.
3197SDValue DAGCombiner::foldSubToUSubSat(EVT DstVT, SDNode *N) {
3198 if (N->getOpcode() != ISD::SUB ||
3199 !(!LegalOperations || hasOperation(ISD::USUBSAT, DstVT)))
3200 return SDValue();
3201
3202 EVT SubVT = N->getValueType(0);
3203 SDValue Op0 = N->getOperand(0);
3204 SDValue Op1 = N->getOperand(1);
3205
3206 // Try to find umax(a,b) - b or a - umin(a,b) patterns
3207 // they may be converted to usubsat(a,b).
3208 if (Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) {
3209 SDValue MaxLHS = Op0.getOperand(0);
3210 SDValue MaxRHS = Op0.getOperand(1);
3211 if (MaxLHS == Op1)
3212 return getTruncatedUSUBSAT(DstVT, SubVT, MaxRHS, Op1, DAG, SDLoc(N));
3213 if (MaxRHS == Op1)
3214 return getTruncatedUSUBSAT(DstVT, SubVT, MaxLHS, Op1, DAG, SDLoc(N));
3215 }
3216
3217 if (Op1.getOpcode() == ISD::UMIN && Op1.hasOneUse()) {
3218 SDValue MinLHS = Op1.getOperand(0);
3219 SDValue MinRHS = Op1.getOperand(1);
3220 if (MinLHS == Op0)
3221 return getTruncatedUSUBSAT(DstVT, SubVT, Op0, MinRHS, DAG, SDLoc(N));
3222 if (MinRHS == Op0)
3223 return getTruncatedUSUBSAT(DstVT, SubVT, Op0, MinLHS, DAG, SDLoc(N));
3224 }
3225
3226 // sub(a,trunc(umin(zext(a),b))) -> usubsat(a,trunc(umin(b,SatLimit)))
3227 if (Op1.getOpcode() == ISD::TRUNCATE &&
3228 Op1.getOperand(0).getOpcode() == ISD::UMIN &&
3229 Op1.getOperand(0).hasOneUse()) {
3230 SDValue MinLHS = Op1.getOperand(0).getOperand(0);
3231 SDValue MinRHS = Op1.getOperand(0).getOperand(1);
3232 if (MinLHS.getOpcode() == ISD::ZERO_EXTEND && MinLHS.getOperand(0) == Op0)
3233 return getTruncatedUSUBSAT(DstVT, MinLHS.getValueType(), MinLHS, MinRHS,
3234 DAG, SDLoc(N));
3235 if (MinRHS.getOpcode() == ISD::ZERO_EXTEND && MinRHS.getOperand(0) == Op0)
3236 return getTruncatedUSUBSAT(DstVT, MinLHS.getValueType(), MinRHS, MinLHS,
3237 DAG, SDLoc(N));
3238 }
3239
3240 return SDValue();
3241}
3242
3243// Since it may not be valid to emit a fold to zero for vector initializers
3244// check if we can before folding.
3245static SDValue tryFoldToZero(const SDLoc &DL, const TargetLowering &TLI, EVT VT,
3246 SelectionDAG &DAG, bool LegalOperations) {
3247 if (!VT.isVector())
3248 return DAG.getConstant(0, DL, VT);
3249 if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT))
3250 return DAG.getConstant(0, DL, VT);
3251 return SDValue();
3252}
3253
3254SDValue DAGCombiner::visitSUB(SDNode *N) {
3255 SDValue N0 = N->getOperand(0);
3256 SDValue N1 = N->getOperand(1);
3257 EVT VT = N0.getValueType();
3258 SDLoc DL(N);
3259
3260 // fold vector ops
3261 if (VT.isVector()) {
3262 if (SDValue FoldedVOp = SimplifyVBinOp(N))
3263 return FoldedVOp;
3264
3265 // fold (sub x, 0) -> x, vector edition
3266 if (ISD::isConstantSplatVectorAllZeros(N1.getNode()))
3267 return N0;
3268 }
3269
3270 // fold (sub x, x) -> 0
3271 // FIXME: Refactor this and xor and other similar operations together.
3272 if (N0 == N1)
3273 return tryFoldToZero(DL, TLI, VT, DAG, LegalOperations);
3274
3275 // fold (sub c1, c2) -> c3
3276 if (SDValue C = DAG.FoldConstantArithmetic(ISD::SUB, DL, VT, {N0, N1}))
3277 return C;
3278
3279 if (SDValue NewSel = foldBinOpIntoSelect(N))
3280 return NewSel;
3281
3282 ConstantSDNode *N1C = getAsNonOpaqueConstant(N1);
3283
3284 // fold (sub x, c) -> (add x, -c)
3285 if (N1C) {
3286 return DAG.getNode(ISD::ADD, DL, VT, N0,
3287 DAG.getConstant(-N1C->getAPIntValue(), DL, VT));
3288 }
3289
3290 if (isNullOrNullSplat(N0)) {
3291 unsigned BitWidth = VT.getScalarSizeInBits();
3292 // Right-shifting everything out but the sign bit followed by negation is
3293 // the same as flipping arithmetic/logical shift type without the negation:
3294 // -(X >>u 31) -> (X >>s 31)
3295 // -(X >>s 31) -> (X >>u 31)
3296 if (N1->getOpcode() == ISD::SRA || N1->getOpcode() == ISD::SRL) {
3297 ConstantSDNode *ShiftAmt = isConstOrConstSplat(N1.getOperand(1));
3298 if (ShiftAmt && ShiftAmt->getAPIntValue() == (BitWidth - 1)) {
3299 auto NewSh = N1->getOpcode() == ISD::SRA ? ISD::SRL : ISD::SRA;
3300 if (!LegalOperations || TLI.isOperationLegal(NewSh, VT))
3301 return DAG.getNode(NewSh, DL, VT, N1.getOperand(0), N1.getOperand(1));
3302 }
3303 }
3304
3305 // 0 - X --> 0 if the sub is NUW.
3306 if (N->getFlags().hasNoUnsignedWrap())
3307 return N0;
3308
3309 if (DAG.MaskedValueIsZero(N1, ~APInt::getSignMask(BitWidth))) {
3310 // N1 is either 0 or the minimum signed value. If the sub is NSW, then
3311 // N1 must be 0 because negating the minimum signed value is undefined.
3312 if (N->getFlags().hasNoSignedWrap())
3313 return N0;
3314
3315 // 0 - X --> X if X is 0 or the minimum signed value.
3316 return N1;
3317 }
3318
3319 // Convert 0 - abs(x).
3320 SDValue Result;
3321 if (N1->getOpcode() == ISD::ABS &&
3322 !TLI.isOperationLegalOrCustom(ISD::ABS, VT) &&
3323 TLI.expandABS(N1.getNode(), Result, DAG, true))
3324 return Result;
3325
3326 // Fold neg(splat(neg(x)) -> splat(x)
3327 if (VT.isVector()) {
3328 SDValue N1S = DAG.getSplatValue(N1, true);
3329 if (N1S && N1S.getOpcode() == ISD::SUB &&
3330 isNullConstant(N1S.getOperand(0))) {
3331 if (VT.isScalableVector())
3332 return DAG.getSplatVector(VT, DL, N1S.getOperand(1));
3333 return DAG.getSplatBuildVector(VT, DL, N1S.getOperand(1));
3334 }
3335 }
3336 }
3337
3338 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1)
3339 if (isAllOnesOrAllOnesSplat(N0))
3340 return DAG.getNode(ISD::XOR, DL, VT, N1, N0);
3341
3342 // fold (A - (0-B)) -> A+B
3343 if (N1.getOpcode() == ISD::SUB && isNullOrNullSplat(N1.getOperand(0)))
3344 return DAG.getNode(ISD::ADD, DL, VT, N0, N1.getOperand(1));
3345
3346 // fold A-(A-B) -> B
3347 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0))
3348 return N1.getOperand(1);
3349
3350 // fold (A+B)-A -> B
3351 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
3352 return N0.getOperand(1);
3353
3354 // fold (A+B)-B -> A
3355 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
3356 return N0.getOperand(0);
3357
3358 // fold (A+C1)-C2 -> A+(C1-C2)
3359 if (N0.getOpcode() == ISD::ADD &&
3360 isConstantOrConstantVector(N1, /* NoOpaques */ true) &&
3361 isConstantOrConstantVector(N0.getOperand(1), /* NoOpaques */ true)) {
3362 SDValue NewC =
3363 DAG.FoldConstantArithmetic(ISD::SUB, DL, VT, {N0.getOperand(1), N1});
3364 assert(NewC && "Constant folding failed")(static_cast <bool> (NewC && "Constant folding failed"
) ? void (0) : __assert_fail ("NewC && \"Constant folding failed\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 3364, __extension__ __PRETTY_FUNCTION__))
;
3365 return DAG.getNode(ISD::ADD, DL, VT, N0.getOperand(0), NewC);
3366 }
3367
3368 // fold C2-(A+C1) -> (C2-C1)-A
3369 if (N1.getOpcode() == ISD::ADD) {
3370 SDValue N11 = N1.getOperand(1);
3371 if (isConstantOrConstantVector(N0, /* NoOpaques */ true) &&
3372 isConstantOrConstantVector(N11, /* NoOpaques */ true)) {
3373 SDValue NewC = DAG.FoldConstantArithmetic(ISD::SUB, DL, VT, {N0, N11});
3374 assert(NewC && "Constant folding failed")(static_cast <bool> (NewC && "Constant folding failed"
) ? void (0) : __assert_fail ("NewC && \"Constant folding failed\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 3374, __extension__ __PRETTY_FUNCTION__))
;
3375 return DAG.getNode(ISD::SUB, DL, VT, NewC, N1.getOperand(0));
3376 }
3377 }
3378
3379 // fold (A-C1)-C2 -> A-(C1+C2)
3380 if (N0.getOpcode() == ISD::SUB &&
3381 isConstantOrConstantVector(N1, /* NoOpaques */ true) &&
3382 isConstantOrConstantVector(N0.getOperand(1), /* NoOpaques */ true)) {
3383 SDValue NewC =
3384 DAG.FoldConstantArithmetic(ISD::ADD, DL, VT, {N0.getOperand(1), N1});
3385 assert(NewC && "Constant folding failed")(static_cast <bool> (NewC && "Constant folding failed"
) ? void (0) : __assert_fail ("NewC && \"Constant folding failed\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 3385, __extension__ __PRETTY_FUNCTION__))
;
3386 return DAG.getNode(ISD::SUB, DL, VT, N0.getOperand(0), NewC);
3387 }
3388
3389 // fold (c1-A)-c2 -> (c1-c2)-A
3390 if (N0.getOpcode() == ISD::SUB &&
3391 isConstantOrConstantVector(N1, /* NoOpaques */ true) &&
3392 isConstantOrConstantVector(N0.getOperand(0), /* NoOpaques */ true)) {
3393 SDValue NewC =
3394 DAG.FoldConstantArithmetic(ISD::SUB, DL, VT, {N0.getOperand(0), N1});
3395 assert(NewC && "Constant folding failed")(static_cast <bool> (NewC && "Constant folding failed"
) ? void (0) : __assert_fail ("NewC && \"Constant folding failed\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 3395, __extension__ __PRETTY_FUNCTION__))
;
3396 return DAG.getNode(ISD::SUB, DL, VT, NewC, N0.getOperand(1));
3397 }
3398
3399 // fold ((A+(B+or-C))-B) -> A+or-C
3400 if (N0.getOpcode() == ISD::ADD &&
3401 (N0.getOperand(1).getOpcode() == ISD::SUB ||
3402 N0.getOperand(1).getOpcode() == ISD::ADD) &&
3403 N0.getOperand(1).getOperand(0) == N1)
3404 return DAG.getNode(N0.getOperand(1).getOpcode(), DL, VT, N0.getOperand(0),
3405 N0.getOperand(1).getOperand(1));
3406
3407 // fold ((A+(C+B))-B) -> A+C
3408 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1).getOpcode() == ISD::ADD &&
3409 N0.getOperand(1).getOperand(1) == N1)
3410 return DAG.getNode(ISD::ADD, DL, VT, N0.getOperand(0),
3411 N0.getOperand(1).getOperand(0));
3412
3413 // fold ((A-(B-C))-C) -> A-B
3414 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1).getOpcode() == ISD::SUB &&
3415 N0.getOperand(1).getOperand(1) == N1)
3416 return DAG.getNode(ISD::SUB, DL, VT, N0.getOperand(0),
3417 N0.getOperand(1).getOperand(0));
3418
3419 // fold (A-(B-C)) -> A+(C-B)
3420 if (N1.getOpcode() == ISD::SUB && N1.hasOneUse())
3421 return DAG.getNode(ISD::ADD, DL, VT, N0,
3422 DAG.getNode(ISD::SUB, DL, VT, N1.getOperand(1),
3423 N1.getOperand(0)));
3424
3425 // A - (A & B) -> A & (~B)
3426 if (N1.getOpcode() == ISD::AND) {
3427 SDValue A = N1.getOperand(0);
3428 SDValue B = N1.getOperand(1);
3429 if (A != N0)
3430 std::swap(A, B);
3431 if (A == N0 &&
3432 (N1.hasOneUse() || isConstantOrConstantVector(B, /*NoOpaques=*/true))) {
3433 SDValue InvB =
3434 DAG.getNode(ISD::XOR, DL, VT, B, DAG.getAllOnesConstant(DL, VT));
3435 return DAG.getNode(ISD::AND, DL, VT, A, InvB);
3436 }
3437 }
3438
3439 // fold (X - (-Y * Z)) -> (X + (Y * Z))
3440 if (N1.getOpcode() == ISD::MUL && N1.hasOneUse()) {
3441 if (N1.getOperand(0).getOpcode() == ISD::SUB &&
3442 isNullOrNullSplat(N1.getOperand(0).getOperand(0))) {
3443 SDValue Mul = DAG.getNode(ISD::MUL, DL, VT,
3444 N1.getOperand(0).getOperand(1),
3445 N1.getOperand(1));
3446 return DAG.getNode(ISD::ADD, DL, VT, N0, Mul);
3447 }
3448 if (N1.getOperand(1).getOpcode() == ISD::SUB &&
3449 isNullOrNullSplat(N1.getOperand(1).getOperand(0))) {
3450 SDValue Mul = DAG.getNode(ISD::MUL, DL, VT,
3451 N1.getOperand(0),
3452 N1.getOperand(1).getOperand(1));
3453 return DAG.getNode(ISD::ADD, DL, VT, N0, Mul);
3454 }
3455 }
3456
3457 // If either operand of a sub is undef, the result is undef
3458 if (N0.isUndef())
3459 return N0;
3460 if (N1.isUndef())
3461 return N1;
3462
3463 if (SDValue V = foldAddSubBoolOfMaskedVal(N, DAG))
3464 return V;
3465
3466 if (SDValue V = foldAddSubOfSignBit(N, DAG))
3467 return V;
3468
3469 if (SDValue V = foldAddSubMasked1(false, N0, N1, DAG, SDLoc(N)))
3470 return V;
3471
3472 if (SDValue V = foldSubToUSubSat(VT, N))
3473 return V;
3474
3475 // (x - y) - 1 -> add (xor y, -1), x
3476 if (N0.hasOneUse() && N0.getOpcode() == ISD::SUB && isOneOrOneSplat(N1)) {
3477 SDValue Xor = DAG.getNode(ISD::XOR, DL, VT, N0.getOperand(1),
3478 DAG.getAllOnesConstant(DL, VT));
3479 return DAG.getNode(ISD::ADD, DL, VT, Xor, N0.getOperand(0));
3480 }
3481
3482 // Look for:
3483 // sub y, (xor x, -1)
3484 // And if the target does not like this form then turn into:
3485 // add (add x, y), 1
3486 if (TLI.preferIncOfAddToSubOfNot(VT) && N1.hasOneUse() && isBitwiseNot(N1)) {
3487 SDValue Add = DAG.getNode(ISD::ADD, DL, VT, N0, N1.getOperand(0));
3488 return DAG.getNode(ISD::ADD, DL, VT, Add, DAG.getConstant(1, DL, VT));
3489 }
3490
3491 // Hoist one-use addition by non-opaque constant:
3492 // (x + C) - y -> (x - y) + C
3493 if (N0.hasOneUse() && N0.getOpcode() == ISD::ADD &&
3494 isConstantOrConstantVector(N0.getOperand(1), /*NoOpaques=*/true)) {
3495 SDValue Sub = DAG.getNode(ISD::SUB, DL, VT, N0.getOperand(0), N1);
3496 return DAG.getNode(ISD::ADD, DL, VT, Sub, N0.getOperand(1));
3497 }
3498 // y - (x + C) -> (y - x) - C
3499 if (N1.hasOneUse() && N1.getOpcode() == ISD::ADD &&
3500 isConstantOrConstantVector(N1.getOperand(1), /*NoOpaques=*/true)) {
3501 SDValue Sub = DAG.getNode(ISD::SUB, DL, VT, N0, N1.getOperand(0));
3502 return DAG.getNode(ISD::SUB, DL, VT, Sub, N1.getOperand(1));
3503 }
3504 // (x - C) - y -> (x - y) - C
3505 // This is necessary because SUB(X,C) -> ADD(X,-C) doesn't work for vectors.
3506 if (N0.hasOneUse() && N0.getOpcode() == ISD::SUB &&
3507 isConstantOrConstantVector(N0.getOperand(1), /*NoOpaques=*/true)) {
3508 SDValue Sub = DAG.getNode(ISD::SUB, DL, VT, N0.getOperand(0), N1);
3509 return DAG.getNode(ISD::SUB, DL, VT, Sub, N0.getOperand(1));
3510 }
3511 // (C - x) - y -> C - (x + y)
3512 if (N0.hasOneUse() && N0.getOpcode() == ISD::SUB &&
3513 isConstantOrConstantVector(N0.getOperand(0), /*NoOpaques=*/true)) {
3514 SDValue Add = DAG.getNode(ISD::ADD, DL, VT, N0.getOperand(1), N1);
3515 return DAG.getNode(ISD::SUB, DL, VT, N0.getOperand(0), Add);
3516 }
3517
3518 // If the target's bool is represented as 0/-1, prefer to make this 'add 0/-1'
3519 // rather than 'sub 0/1' (the sext should get folded).
3520 // sub X, (zext i1 Y) --> add X, (sext i1 Y)
3521 if (N1.getOpcode() == ISD::ZERO_EXTEND &&
3522 N1.getOperand(0).getScalarValueSizeInBits() == 1 &&
3523 TLI.getBooleanContents(VT) ==
3524 TargetLowering::ZeroOrNegativeOneBooleanContent) {
3525 SDValue SExt = DAG.getNode(ISD::SIGN_EXTEND, DL, VT, N1.getOperand(0));
3526 return DAG.getNode(ISD::ADD, DL, VT, N0, SExt);
3527 }
3528
3529 // fold Y = sra (X, size(X)-1); sub (xor (X, Y), Y) -> (abs X)
3530 if (TLI.isOperationLegalOrCustom(ISD::ABS, VT)) {
3531 if (N0.getOpcode() == ISD::XOR && N1.getOpcode() == ISD::SRA) {
3532 SDValue X0 = N0.getOperand(0), X1 = N0.getOperand(1);
3533 SDValue S0 = N1.getOperand(0);
3534 if ((X0 == S0 && X1 == N1) || (X0 == N1 && X1 == S0))
3535 if (ConstantSDNode *C = isConstOrConstSplat(N1.getOperand(1)))
3536 if (C->getAPIntValue() == (VT.getScalarSizeInBits() - 1))
3537 return DAG.getNode(ISD::ABS, SDLoc(N), VT, S0);
3538 }
3539 }
3540
3541 // If the relocation model supports it, consider symbol offsets.
3542 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
3543 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
3544 // fold (sub Sym, c) -> Sym-c
3545 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
3546 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT,
3547 GA->getOffset() -
3548 (uint64_t)N1C->getSExtValue());
3549 // fold (sub Sym+c1, Sym+c2) -> c1-c2
3550 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
3551 if (GA->getGlobal() == GB->getGlobal())
3552 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
3553 DL, VT);
3554 }
3555
3556 // sub X, (sextinreg Y i1) -> add X, (and Y 1)
3557 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
3558 VTSDNode *TN = cast<VTSDNode>(N1.getOperand(1));
3559 if (TN->getVT() == MVT::i1) {
3560 SDValue ZExt = DAG.getNode(ISD::AND, DL, VT, N1.getOperand(0),
3561 DAG.getConstant(1, DL, VT));
3562 return DAG.getNode(ISD::ADD, DL, VT, N0, ZExt);
3563 }
3564 }
3565
3566 // canonicalize (sub X, (vscale * C)) to (add X, (vscale * -C))
3567 if (N1.getOpcode() == ISD::VSCALE) {
3568 const APInt &IntVal = N1.getConstantOperandAPInt(0);
3569 return DAG.getNode(ISD::ADD, DL, VT, N0, DAG.getVScale(DL, VT, -IntVal));
3570 }
3571
3572 // canonicalize (sub X, step_vector(C)) to (add X, step_vector(-C))
3573 if (N1.getOpcode() == ISD::STEP_VECTOR && N1.hasOneUse()) {
3574 APInt NewStep = -N1.getConstantOperandAPInt(0);
3575 return DAG.getNode(ISD::ADD, DL, VT, N0,
3576 DAG.getStepVector(DL, VT, NewStep));
3577 }
3578
3579 // Prefer an add for more folding potential and possibly better codegen:
3580 // sub N0, (lshr N10, width-1) --> add N0, (ashr N10, width-1)
3581 if (!LegalOperations && N1.getOpcode() == ISD::SRL && N1.hasOneUse()) {
3582 SDValue ShAmt = N1.getOperand(1);
3583 ConstantSDNode *ShAmtC = isConstOrConstSplat(ShAmt);
3584 if (ShAmtC &&
3585 ShAmtC->getAPIntValue() == (N1.getScalarValueSizeInBits() - 1)) {
3586 SDValue SRA = DAG.getNode(ISD::SRA, DL, VT, N1.getOperand(0), ShAmt);
3587 return DAG.getNode(ISD::ADD, DL, VT, N0, SRA);
3588 }
3589 }
3590
3591 if (TLI.isOperationLegalOrCustom(ISD::ADDCARRY, VT)) {
3592 // (sub Carry, X) -> (addcarry (sub 0, X), 0, Carry)
3593 if (SDValue Carry = getAsCarry(TLI, N0)) {
3594 SDValue X = N1;
3595 SDValue Zero = DAG.getConstant(0, DL, VT);
3596 SDValue NegX = DAG.getNode(ISD::SUB, DL, VT, Zero, X);
3597 return DAG.getNode(ISD::ADDCARRY, DL,
3598 DAG.getVTList(VT, Carry.getValueType()), NegX, Zero,
3599 Carry);
3600 }
3601 }
3602
3603 return SDValue();
3604}
3605
3606SDValue DAGCombiner::visitSUBSAT(SDNode *N) {
3607 SDValue N0 = N->getOperand(0);
3608 SDValue N1 = N->getOperand(1);
3609 EVT VT = N0.getValueType();
3610 SDLoc DL(N);
3611
3612 // fold vector ops
3613 if (VT.isVector()) {
3614 // TODO SimplifyVBinOp
3615
3616 // fold (sub_sat x, 0) -> x, vector edition
3617 if (ISD::isConstantSplatVectorAllZeros(N1.getNode()))
3618 return N0;
3619 }
3620
3621 // fold (sub_sat x, undef) -> 0
3622 if (N0.isUndef() || N1.isUndef())
3623 return DAG.getConstant(0, DL, VT);
3624
3625 // fold (sub_sat x, x) -> 0
3626 if (N0 == N1)
3627 return DAG.getConstant(0, DL, VT);
3628
3629 // fold (sub_sat c1, c2) -> c3
3630 if (SDValue C = DAG.FoldConstantArithmetic(N->getOpcode(), DL, VT, {N0, N1}))
3631 return C;
3632
3633 // fold (sub_sat x, 0) -> x
3634 if (isNullConstant(N1))
3635 return N0;
3636
3637 return SDValue();
3638}
3639
3640SDValue DAGCombiner::visitSUBC(SDNode *N) {
3641 SDValue N0 = N->getOperand(0);
3642 SDValue N1 = N->getOperand(1);
3643 EVT VT = N0.getValueType();
3644 SDLoc DL(N);
3645
3646 // If the flag result is dead, turn this into an SUB.
3647 if (!N->hasAnyUseOfValue(1))
3648 return CombineTo(N, DAG.getNode(ISD::SUB, DL, VT, N0, N1),
3649 DAG.getNode(ISD::CARRY_FALSE, DL, MVT::Glue));
3650
3651 // fold (subc x, x) -> 0 + no borrow
3652 if (N0 == N1)
3653 return CombineTo(N, DAG.getConstant(0, DL, VT),
3654 DAG.getNode(ISD::CARRY_FALSE, DL, MVT::Glue));
3655
3656 // fold (subc x, 0) -> x + no borrow
3657 if (isNullConstant(N1))
3658 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, DL, MVT::Glue));
3659
3660 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) + no borrow
3661 if (isAllOnesConstant(N0))
3662 return CombineTo(N, DAG.getNode(ISD::XOR, DL, VT, N1, N0),
3663 DAG.getNode(ISD::CARRY_FALSE, DL, MVT::Glue));
3664
3665 return SDValue();
3666}
3667
3668SDValue DAGCombiner::visitSUBO(SDNode *N) {
3669 SDValue N0 = N->getOperand(0);
3670 SDValue N1 = N->getOperand(1);
3671 EVT VT = N0.getValueType();
3672 bool IsSigned = (ISD::SSUBO == N->getOpcode());
3673
3674 EVT CarryVT = N->getValueType(1);
3675 SDLoc DL(N);
3676
3677 // If the flag result is dead, turn this into an SUB.
3678 if (!N->hasAnyUseOfValue(1))
3679 return CombineTo(N, DAG.getNode(ISD::SUB, DL, VT, N0, N1),
3680 DAG.getUNDEF(CarryVT));
3681
3682 // fold (subo x, x) -> 0 + no borrow
3683 if (N0 == N1)
3684 return CombineTo(N, DAG.getConstant(0, DL, VT),
3685 DAG.getConstant(0, DL, CarryVT));
3686
3687 ConstantSDNode *N1C = getAsNonOpaqueConstant(N1);
3688
3689 // fold (subox, c) -> (addo x, -c)
3690 if (IsSigned && N1C && !N1C->getAPIntValue().isMinSignedValue()) {
3691 return DAG.getNode(ISD::SADDO, DL, N->getVTList(), N0,
3692 DAG.getConstant(-N1C->getAPIntValue(), DL, VT));
3693 }
3694
3695 // fold (subo x, 0) -> x + no borrow
3696 if (isNullOrNullSplat(N1))
3697 return CombineTo(N, N0, DAG.getConstant(0, DL, CarryVT));
3698
3699 // Canonicalize (usubo -1, x) -> ~x, i.e. (xor x, -1) + no borrow
3700 if (!IsSigned && isAllOnesOrAllOnesSplat(N0))
3701 return CombineTo(N, DAG.getNode(ISD::XOR, DL, VT, N1, N0),
3702 DAG.getConstant(0, DL, CarryVT));
3703
3704 return SDValue();
3705}
3706
3707SDValue DAGCombiner::visitSUBE(SDNode *N) {
3708 SDValue N0 = N->getOperand(0);
3709 SDValue N1 = N->getOperand(1);
3710 SDValue CarryIn = N->getOperand(2);
3711
3712 // fold (sube x, y, false) -> (subc x, y)
3713 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
3714 return DAG.getNode(ISD::SUBC, SDLoc(N), N->getVTList(), N0, N1);
3715
3716 return SDValue();
3717}
3718
3719SDValue DAGCombiner::visitSUBCARRY(SDNode *N) {
3720 SDValue N0 = N->getOperand(0);
3721 SDValue N1 = N->getOperand(1);
3722 SDValue CarryIn = N->getOperand(2);
3723
3724 // fold (subcarry x, y, false) -> (usubo x, y)
3725 if (isNullConstant(CarryIn)) {
3726 if (!LegalOperations ||
3727 TLI.isOperationLegalOrCustom(ISD::USUBO, N->getValueType(0)))
3728 return DAG.getNode(ISD::USUBO, SDLoc(N), N->getVTList(), N0, N1);
3729 }
3730
3731 return SDValue();
3732}
3733
3734SDValue DAGCombiner::visitSSUBO_CARRY(SDNode *N) {
3735 SDValue N0 = N->getOperand(0);
3736 SDValue N1 = N->getOperand(1);
3737 SDValue CarryIn = N->getOperand(2);
3738
3739 // fold (ssubo_carry x, y, false) -> (ssubo x, y)
3740 if (isNullConstant(CarryIn)) {
3741 if (!LegalOperations ||
3742 TLI.isOperationLegalOrCustom(ISD::SSUBO, N->getValueType(0)))
3743 return DAG.getNode(ISD::SSUBO, SDLoc(N), N->getVTList(), N0, N1);
3744 }
3745
3746 return SDValue();
3747}
3748
3749// Notice that "mulfix" can be any of SMULFIX, SMULFIXSAT, UMULFIX and
3750// UMULFIXSAT here.
3751SDValue DAGCombiner::visitMULFIX(SDNode *N) {
3752 SDValue N0 = N->getOperand(0);
3753 SDValue N1 = N->getOperand(1);
3754 SDValue Scale = N->getOperand(2);
3755 EVT VT = N0.getValueType();
3756
3757 // fold (mulfix x, undef, scale) -> 0
3758 if (N0.isUndef() || N1.isUndef())
3759 return DAG.getConstant(0, SDLoc(N), VT);
3760
3761 // Canonicalize constant to RHS (vector doesn't have to splat)
3762 if (DAG.isConstantIntBuildVectorOrConstantInt(N0) &&
3763 !DAG.isConstantIntBuildVectorOrConstantInt(N1))
3764 return DAG.getNode(N->getOpcode(), SDLoc(N), VT, N1, N0, Scale);
3765
3766 // fold (mulfix x, 0, scale) -> 0
3767 if (isNullConstant(N1))
3768 return DAG.getConstant(0, SDLoc(N), VT);
3769
3770 return SDValue();
3771}
3772
3773SDValue DAGCombiner::visitMUL(SDNode *N) {
3774 SDValue N0 = N->getOperand(0);
3775 SDValue N1 = N->getOperand(1);
3776 EVT VT = N0.getValueType();
3777
3778 // fold (mul x, undef) -> 0
3779 if (N0.isUndef() || N1.isUndef())
3780 return DAG.getConstant(0, SDLoc(N), VT);
3781
3782 bool N1IsConst = false;
3783 bool N1IsOpaqueConst = false;
3784 APInt ConstValue1;
3785
3786 // fold vector ops
3787 if (VT.isVector()) {
3788 if (SDValue FoldedVOp = SimplifyVBinOp(N))
3789 return FoldedVOp;
3790
3791 N1IsConst = ISD::isConstantSplatVector(N1.getNode(), ConstValue1);
3792 assert((!N1IsConst ||(static_cast <bool> ((!N1IsConst || ConstValue1.getBitWidth
() == VT.getScalarSizeInBits()) && "Splat APInt should be element width"
) ? void (0) : __assert_fail ("(!N1IsConst || ConstValue1.getBitWidth() == VT.getScalarSizeInBits()) && \"Splat APInt should be element width\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 3794, __extension__ __PRETTY_FUNCTION__))
3793 ConstValue1.getBitWidth() == VT.getScalarSizeInBits()) &&(static_cast <bool> ((!N1IsConst || ConstValue1.getBitWidth
() == VT.getScalarSizeInBits()) && "Splat APInt should be element width"
) ? void (0) : __assert_fail ("(!N1IsConst || ConstValue1.getBitWidth() == VT.getScalarSizeInBits()) && \"Splat APInt should be element width\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 3794, __extension__ __PRETTY_FUNCTION__))
3794 "Splat APInt should be element width")(static_cast <bool> ((!N1IsConst || ConstValue1.getBitWidth
() == VT.getScalarSizeInBits()) && "Splat APInt should be element width"
) ? void (0) : __assert_fail ("(!N1IsConst || ConstValue1.getBitWidth() == VT.getScalarSizeInBits()) && \"Splat APInt should be element width\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 3794, __extension__ __PRETTY_FUNCTION__))
;
3795 } else {
3796 N1IsConst = isa<ConstantSDNode>(N1);
3797 if (N1IsConst) {
3798 ConstValue1 = cast<ConstantSDNode>(N1)->getAPIntValue();
3799 N1IsOpaqueConst = cast<ConstantSDNode>(N1)->isOpaque();
3800 }
3801 }
3802
3803 // fold (mul c1, c2) -> c1*c2
3804 if (SDValue C = DAG.FoldConstantArithmetic(ISD::MUL, SDLoc(N), VT, {N0, N1}))
3805 return C;
3806
3807 // canonicalize constant to RHS (vector doesn't have to splat)
3808 if (DAG.isConstantIntBuildVectorOrConstantInt(N0) &&
3809 !DAG.isConstantIntBuildVectorOrConstantInt(N1))
3810 return DAG.getNode(ISD::MUL, SDLoc(N), VT, N1, N0);
3811
3812 // fold (mul x, 0) -> 0
3813 if (N1IsConst && ConstValue1.isNullValue())
3814 return N1;
3815
3816 // fold (mul x, 1) -> x
3817 if (N1IsConst && ConstValue1.isOneValue())
3818 return N0;
3819
3820 if (SDValue NewSel = foldBinOpIntoSelect(N))
3821 return NewSel;
3822
3823 // fold (mul x, -1) -> 0-x
3824 if (N1IsConst && ConstValue1.isAllOnesValue()) {
3825 SDLoc DL(N);
3826 return DAG.getNode(ISD::SUB, DL, VT,
3827 DAG.getConstant(0, DL, VT), N0);
3828 }
3829
3830 // fold (mul x, (1 << c)) -> x << c
3831 if (isConstantOrConstantVector(N1, /*NoOpaques*/ true) &&
3832 DAG.isKnownToBeAPowerOfTwo(N1) &&
3833 (!VT.isVector() || Level <= AfterLegalizeVectorOps)) {
3834 SDLoc DL(N);
3835 SDValue LogBase2 = BuildLogBase2(N1, DL);
3836 EVT ShiftVT = getShiftAmountTy(N0.getValueType());
3837 SDValue Trunc = DAG.getZExtOrTrunc(LogBase2, DL, ShiftVT);
3838 return DAG.getNode(ISD::SHL, DL, VT, N0, Trunc);
3839 }
3840
3841 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
3842 if (N1IsConst && !N1IsOpaqueConst && (-ConstValue1).isPowerOf2()) {
3843 unsigned Log2Val = (-ConstValue1).logBase2();
3844 SDLoc DL(N);
3845 // FIXME: If the input is something that is easily negated (e.g. a
3846 // single-use add), we should put the negate there.
3847 return DAG.getNode(ISD::SUB, DL, VT,
3848 DAG.getConstant(0, DL, VT),
3849 DAG.getNode(ISD::SHL, DL, VT, N0,
3850 DAG.getConstant(Log2Val, DL,
3851 getShiftAmountTy(N0.getValueType()))));
3852 }
3853
3854 // Try to transform:
3855 // (1) multiply-by-(power-of-2 +/- 1) into shift and add/sub.
3856 // mul x, (2^N + 1) --> add (shl x, N), x
3857 // mul x, (2^N - 1) --> sub (shl x, N), x
3858 // Examples: x * 33 --> (x << 5) + x
3859 // x * 15 --> (x << 4) - x
3860 // x * -33 --> -((x << 5) + x)
3861 // x * -15 --> -((x << 4) - x) ; this reduces --> x - (x << 4)
3862 // (2) multiply-by-(power-of-2 +/- power-of-2) into shifts and add/sub.
3863 // mul x, (2^N + 2^M) --> (add (shl x, N), (shl x, M))
3864 // mul x, (2^N - 2^M) --> (sub (shl x, N), (shl x, M))
3865 // Examples: x * 0x8800 --> (x << 15) + (x << 11)
3866 // x * 0xf800 --> (x << 16) - (x << 11)
3867 // x * -0x8800 --> -((x << 15) + (x << 11))
3868 // x * -0xf800 --> -((x << 16) - (x << 11)) ; (x << 11) - (x << 16)
3869 if (N1IsConst && TLI.decomposeMulByConstant(*DAG.getContext(), VT, N1)) {
3870 // TODO: We could handle more general decomposition of any constant by
3871 // having the target set a limit on number of ops and making a
3872 // callback to determine that sequence (similar to sqrt expansion).
3873 unsigned MathOp = ISD::DELETED_NODE;
3874 APInt MulC = ConstValue1.abs();
3875 // The constant `2` should be treated as (2^0 + 1).
3876 unsigned TZeros = MulC == 2 ? 0 : MulC.countTrailingZeros();
3877 MulC.lshrInPlace(TZeros);
3878 if ((MulC - 1).isPowerOf2())
3879 MathOp = ISD::ADD;
3880 else if ((MulC + 1).isPowerOf2())
3881 MathOp = ISD::SUB;
3882
3883 if (MathOp != ISD::DELETED_NODE) {
3884 unsigned ShAmt =
3885 MathOp == ISD::ADD ? (MulC - 1).logBase2() : (MulC + 1).logBase2();
3886 ShAmt += TZeros;
3887 assert(ShAmt < VT.getScalarSizeInBits() &&(static_cast <bool> (ShAmt < VT.getScalarSizeInBits(
) && "multiply-by-constant generated out of bounds shift"
) ? void (0) : __assert_fail ("ShAmt < VT.getScalarSizeInBits() && \"multiply-by-constant generated out of bounds shift\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 3888, __extension__ __PRETTY_FUNCTION__))
3888 "multiply-by-constant generated out of bounds shift")(static_cast <bool> (ShAmt < VT.getScalarSizeInBits(
) && "multiply-by-constant generated out of bounds shift"
) ? void (0) : __assert_fail ("ShAmt < VT.getScalarSizeInBits() && \"multiply-by-constant generated out of bounds shift\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 3888, __extension__ __PRETTY_FUNCTION__))
;
3889 SDLoc DL(N);
3890 SDValue Shl =
3891 DAG.getNode(ISD::SHL, DL, VT, N0, DAG.getConstant(ShAmt, DL, VT));
3892 SDValue R =
3893 TZeros ? DAG.getNode(MathOp, DL, VT, Shl,
3894 DAG.getNode(ISD::SHL, DL, VT, N0,
3895 DAG.getConstant(TZeros, DL, VT)))
3896 : DAG.getNode(MathOp, DL, VT, Shl, N0);
3897 if (ConstValue1.isNegative())
3898 R = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), R);
3899 return R;
3900 }
3901 }
3902
3903 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
3904 if (N0.getOpcode() == ISD::SHL &&
3905 isConstantOrConstantVector(N1, /* NoOpaques */ true) &&
3906 isConstantOrConstantVector(N0.getOperand(1), /* NoOpaques */ true)) {
3907 SDValue C3 = DAG.getNode(ISD::SHL, SDLoc(N), VT, N1, N0.getOperand(1));
3908 if (isConstantOrConstantVector(C3))
3909 return DAG.getNode(ISD::MUL, SDLoc(N), VT, N0.getOperand(0), C3);
3910 }
3911
3912 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
3913 // use.
3914 {
3915 SDValue Sh(nullptr, 0), Y(nullptr, 0);
3916
3917 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
3918 if (N0.getOpcode() == ISD::SHL &&
3919 isConstantOrConstantVector(N0.getOperand(1)) &&
3920 N0.getNode()->hasOneUse()) {
3921 Sh = N0; Y = N1;
3922 } else if (N1.getOpcode() == ISD::SHL &&
3923 isConstantOrConstantVector(N1.getOperand(1)) &&
3924 N1.getNode()->hasOneUse()) {
3925 Sh = N1; Y = N0;
3926 }
3927
3928 if (Sh.getNode()) {
3929 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT, Sh.getOperand(0), Y);
3930 return DAG.getNode(ISD::SHL, SDLoc(N), VT, Mul, Sh.getOperand(1));
3931 }
3932 }
3933
3934 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
3935 if (DAG.isConstantIntBuildVectorOrConstantInt(N1) &&
3936 N0.getOpcode() == ISD::ADD &&
3937 DAG.isConstantIntBuildVectorOrConstantInt(N0.getOperand(1)) &&
3938 isMulAddWithConstProfitable(N, N0, N1))
3939 return DAG.getNode(ISD::ADD, SDLoc(N), VT,
3940 DAG.getNode(ISD::MUL, SDLoc(N0), VT,
3941 N0.getOperand(0), N1),
3942 DAG.getNode(ISD::MUL, SDLoc(N1), VT,
3943 N0.getOperand(1), N1));
3944
3945 // Fold (mul (vscale * C0), C1) to (vscale * (C0 * C1)).
3946 if (N0.getOpcode() == ISD::VSCALE)
3947 if (ConstantSDNode *NC1 = isConstOrConstSplat(N1)) {
3948 const APInt &C0 = N0.getConstantOperandAPInt(0);
3949 const APInt &C1 = NC1->getAPIntValue();
3950 return DAG.getVScale(SDLoc(N), VT, C0 * C1);
3951 }
3952
3953 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
3954 APInt MulVal;
3955 if (N0.getOpcode() == ISD::STEP_VECTOR)
3956 if (ISD::isConstantSplatVector(N1.getNode(), MulVal)) {
3957 const APInt &C0 = N0.getConstantOperandAPInt(0);
3958 APInt NewStep = C0 * MulVal;
3959 return DAG.getStepVector(SDLoc(N), VT, NewStep);
3960 }
3961
3962 // Fold ((mul x, 0/undef) -> 0,
3963 // (mul x, 1) -> x) -> x)
3964 // -> and(x, mask)
3965 // We can replace vectors with '0' and '1' factors with a clearing mask.
3966 if (VT.isFixedLengthVector()) {
3967 unsigned NumElts = VT.getVectorNumElements();
3968 SmallBitVector ClearMask;
3969 ClearMask.reserve(NumElts);
3970 auto IsClearMask = [&ClearMask](ConstantSDNode *V) {
3971 if (!V || V->isNullValue()) {
3972 ClearMask.push_back(true);
3973 return true;
3974 }
3975 ClearMask.push_back(false);
3976 return V->isOne();
3977 };
3978 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::AND, VT)) &&
3979 ISD::matchUnaryPredicate(N1, IsClearMask, /*AllowUndefs*/ true)) {
3980 assert(N1.getOpcode() == ISD::BUILD_VECTOR && "Unknown constant vector")(static_cast <bool> (N1.getOpcode() == ISD::BUILD_VECTOR
&& "Unknown constant vector") ? void (0) : __assert_fail
("N1.getOpcode() == ISD::BUILD_VECTOR && \"Unknown constant vector\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 3980, __extension__ __PRETTY_FUNCTION__))
;
3981 SDLoc DL(N);
3982 EVT LegalSVT = N1.getOperand(0).getValueType();
3983 SDValue Zero = DAG.getConstant(0, DL, LegalSVT);
3984 SDValue AllOnes = DAG.getAllOnesConstant(DL, LegalSVT);
3985 SmallVector<SDValue, 16> Mask(NumElts, AllOnes);
3986 for (unsigned I = 0; I != NumElts; ++I)
3987 if (ClearMask[I])
3988 Mask[I] = Zero;
3989 return DAG.getNode(ISD::AND, DL, VT, N0, DAG.getBuildVector(VT, DL, Mask));
3990 }
3991 }
3992
3993 // reassociate mul
3994 if (SDValue RMUL = reassociateOps(ISD::MUL, SDLoc(N), N0, N1, N->getFlags()))
3995 return RMUL;
3996
3997 return SDValue();
3998}
3999
4000/// Return true if divmod libcall is available.
4001static bool isDivRemLibcallAvailable(SDNode *Node, bool isSigned,
4002 const TargetLowering &TLI) {
4003 RTLIB::Libcall LC;
4004 EVT NodeType = Node->getValueType(0);
4005 if (!NodeType.isSimple())
4006 return false;
4007 switch (NodeType.getSimpleVT().SimpleTy) {
4008 default: return false; // No libcall for vector types.
4009 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
4010 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
4011 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
4012 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
4013 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
4014 }
4015
4016 return TLI.getLibcallName(LC) != nullptr;
4017}
4018
4019/// Issue divrem if both quotient and remainder are needed.
4020SDValue DAGCombiner::useDivRem(SDNode *Node) {
4021 if (Node->use_empty())
4022 return SDValue(); // This is a dead node, leave it alone.
4023
4024 unsigned Opcode = Node->getOpcode();
4025 bool isSigned = (Opcode == ISD::SDIV) || (Opcode == ISD::SREM);
4026 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
4027
4028 // DivMod lib calls can still work on non-legal types if using lib-calls.
4029 EVT VT = Node->getValueType(0);
4030 if (VT.isVector() || !VT.isInteger())
4031 return SDValue();
4032
4033 if (!TLI.isTypeLegal(VT) && !TLI.isOperationCustom(DivRemOpc, VT))
4034 return SDValue();
4035
4036 // If DIVREM is going to get expanded into a libcall,
4037 // but there is no libcall available, then don't combine.
4038 if (!TLI.isOperationLegalOrCustom(DivRemOpc, VT) &&
4039 !isDivRemLibcallAvailable(Node, isSigned, TLI))
4040 return SDValue();
4041
4042 // If div is legal, it's better to do the normal expansion
4043 unsigned OtherOpcode = 0;
4044 if ((Opcode == ISD::SDIV) || (Opcode == ISD::UDIV)) {
4045 OtherOpcode = isSigned ? ISD::SREM : ISD::UREM;
4046 if (TLI.isOperationLegalOrCustom(Opcode, VT))
4047 return SDValue();
4048 } else {
4049 OtherOpcode = isSigned ? ISD::SDIV : ISD::UDIV;
4050 if (TLI.isOperationLegalOrCustom(OtherOpcode, VT))
4051 return SDValue();
4052 }
4053
4054 SDValue Op0 = Node->getOperand(0);
4055 SDValue Op1 = Node->getOperand(1);
4056 SDValue combined;
4057 for (SDNode::use_iterator UI = Op0.getNode()->use_begin(),
4058 UE = Op0.getNode()->use_end(); UI != UE; ++UI) {
4059 SDNode *User = *UI;
4060 if (User == Node || User->getOpcode() == ISD::DELETED_NODE ||
4061 User->use_empty())
4062 continue;
4063 // Convert the other matching node(s), too;
4064 // otherwise, the DIVREM may get target-legalized into something
4065 // target-specific that we won't be able to recognize.
4066 unsigned UserOpc = User->getOpcode();
4067 if ((UserOpc == Opcode || UserOpc == OtherOpcode || UserOpc == DivRemOpc) &&
4068 User->getOperand(0) == Op0 &&
4069 User->getOperand(1) == Op1) {
4070 if (!combined) {
4071 if (UserOpc == OtherOpcode) {
4072 SDVTList VTs = DAG.getVTList(VT, VT);
4073 combined = DAG.getNode(DivRemOpc, SDLoc(Node), VTs, Op0, Op1);
4074 } else if (UserOpc == DivRemOpc) {
4075 combined = SDValue(User, 0);
4076 } else {
4077 assert(UserOpc == Opcode)(static_cast <bool> (UserOpc == Opcode) ? void (0) : __assert_fail
("UserOpc == Opcode", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 4077, __extension__ __PRETTY_FUNCTION__))
;
4078 continue;
4079 }
4080 }
4081 if (UserOpc == ISD::SDIV || UserOpc == ISD::UDIV)
4082 CombineTo(User, combined);
4083 else if (UserOpc == ISD::SREM || UserOpc == ISD::UREM)
4084 CombineTo(User, combined.getValue(1));
4085 }
4086 }
4087 return combined;
4088}
4089
4090static SDValue simplifyDivRem(SDNode *N, SelectionDAG &DAG) {
4091 SDValue N0 = N->getOperand(0);
4092 SDValue N1 = N->getOperand(1);
4093 EVT VT = N->getValueType(0);
4094 SDLoc DL(N);
4095
4096 unsigned Opc = N->getOpcode();
4097 bool IsDiv = (ISD::SDIV == Opc) || (ISD::UDIV == Opc);
4098 ConstantSDNode *N1C = isConstOrConstSplat(N1);
4099
4100 // X / undef -> undef
4101 // X % undef -> undef
4102 // X / 0 -> undef
4103 // X % 0 -> undef
4104 // NOTE: This includes vectors where any divisor element is zero/undef.
4105 if (DAG.isUndef(Opc, {N0, N1}))
4106 return DAG.getUNDEF(VT);
4107
4108 // undef / X -> 0
4109 // undef % X -> 0
4110 if (N0.isUndef())
4111 return DAG.getConstant(0, DL, VT);
4112
4113 // 0 / X -> 0
4114 // 0 % X -> 0
4115 ConstantSDNode *N0C = isConstOrConstSplat(N0);
4116 if (N0C && N0C->isNullValue())
4117 return N0;
4118
4119 // X / X -> 1
4120 // X % X -> 0
4121 if (N0 == N1)
4122 return DAG.getConstant(IsDiv ? 1 : 0, DL, VT);
4123
4124 // X / 1 -> X
4125 // X % 1 -> 0
4126 // If this is a boolean op (single-bit element type), we can't have
4127 // division-by-zero or remainder-by-zero, so assume the divisor is 1.
4128 // TODO: Similarly, if we're zero-extending a boolean divisor, then assume
4129 // it's a 1.
4130 if ((N1C && N1C->isOne()) || (VT.getScalarType() == MVT::i1))
4131 return IsDiv ? N0 : DAG.getConstant(0, DL, VT);
4132
4133 return SDValue();
4134}
4135
4136SDValue DAGCombiner::visitSDIV(SDNode *N) {
4137 SDValue N0 = N->getOperand(0);
4138 SDValue N1 = N->getOperand(1);
4139 EVT VT = N->getValueType(0);
4140 EVT CCVT = getSetCCResultType(VT);
4141
4142 // fold vector ops
4143 if (VT.isVector())
4144 if (SDValue FoldedVOp = SimplifyVBinOp(N))
4145 return FoldedVOp;
4146
4147 SDLoc DL(N);
4148
4149 // fold (sdiv c1, c2) -> c1/c2
4150 ConstantSDNode *N1C = isConstOrConstSplat(N1);
4151 if (SDValue C = DAG.FoldConstantArithmetic(ISD::SDIV, DL, VT, {N0, N1}))
4152 return C;
4153
4154 // fold (sdiv X, -1) -> 0-X
4155 if (N1C && N1C->isAllOnesValue())
4156 return DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), N0);
4157
4158 // fold (sdiv X, MIN_SIGNED) -> select(X == MIN_SIGNED, 1, 0)
4159 if (N1C && N1C->getAPIntValue().isMinSignedValue())
4160 return DAG.getSelect(DL, VT, DAG.getSetCC(DL, CCVT, N0, N1, ISD::SETEQ),
4161 DAG.getConstant(1, DL, VT),
4162 DAG.getConstant(0, DL, VT));
4163
4164 if (SDValue V = simplifyDivRem(N, DAG))
4165 return V;
4166
4167 if (SDValue NewSel = foldBinOpIntoSelect(N))
4168 return NewSel;
4169
4170 // If we know the sign bits of both operands are zero, strength reduce to a
4171 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
4172 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
4173 return DAG.getNode(ISD::UDIV, DL, N1.getValueType(), N0, N1);
4174
4175 if (SDValue V = visitSDIVLike(N0, N1, N)) {
4176 // If the corresponding remainder node exists, update its users with
4177 // (Dividend - (Quotient * Divisor).
4178 if (SDNode *RemNode = DAG.getNodeIfExists(ISD::SREM, N->getVTList(),
4179 { N0, N1 })) {
4180 SDValue Mul = DAG.getNode(ISD::MUL, DL, VT, V, N1);
4181 SDValue Sub = DAG.getNode(ISD::SUB, DL, VT, N0, Mul);
4182 AddToWorklist(Mul.getNode());
4183 AddToWorklist(Sub.getNode());
4184 CombineTo(RemNode, Sub);
4185 }
4186 return V;
4187 }
4188
4189 // sdiv, srem -> sdivrem
4190 // If the divisor is constant, then return DIVREM only if isIntDivCheap() is
4191 // true. Otherwise, we break the simplification logic in visitREM().
4192 AttributeList Attr = DAG.getMachineFunction().getFunction().getAttributes();
4193 if (!N1C || TLI.isIntDivCheap(N->getValueType(0), Attr))
4194 if (SDValue DivRem = useDivRem(N))
4195 return DivRem;
4196
4197 return SDValue();
4198}
4199
4200SDValue DAGCombiner::visitSDIVLike(SDValue N0, SDValue N1, SDNode *N) {
4201 SDLoc DL(N);
4202 EVT VT = N->getValueType(0);
4203 EVT CCVT = getSetCCResultType(VT);
4204 unsigned BitWidth = VT.getScalarSizeInBits();
4205
4206 // Helper for determining whether a value is a power-2 constant scalar or a
4207 // vector of such elements.
4208 auto IsPowerOfTwo = [](ConstantSDNode *C) {
4209 if (C->isNullValue() || C->isOpaque())
4210 return false;
4211 if (C->getAPIntValue().isPowerOf2())
4212 return true;
4213 if ((-C->getAPIntValue()).isPowerOf2())
4214 return true;
4215 return false;
4216 };
4217
4218 // fold (sdiv X, pow2) -> simple ops after legalize
4219 // FIXME: We check for the exact bit here because the generic lowering gives
4220 // better results in that case. The target-specific lowering should learn how
4221 // to handle exact sdivs efficiently.
4222 if (!N->getFlags().hasExact() && ISD::matchUnaryPredicate(N1, IsPowerOfTwo)) {
4223 // Target-specific implementation of sdiv x, pow2.
4224 if (SDValue Res = BuildSDIVPow2(N))
4225 return Res;
4226
4227 // Create constants that are functions of the shift amount value.
4228 EVT ShiftAmtTy = getShiftAmountTy(N0.getValueType());
4229 SDValue Bits = DAG.getConstant(BitWidth, DL, ShiftAmtTy);
4230 SDValue C1 = DAG.getNode(ISD::CTTZ, DL, VT, N1);
4231 C1 = DAG.getZExtOrTrunc(C1, DL, ShiftAmtTy);
4232 SDValue Inexact = DAG.getNode(ISD::SUB, DL, ShiftAmtTy, Bits, C1);
4233 if (!isConstantOrConstantVector(Inexact))
4234 return SDValue();
4235
4236 // Splat the sign bit into the register
4237 SDValue Sign = DAG.getNode(ISD::SRA, DL, VT, N0,
4238 DAG.getConstant(BitWidth - 1, DL, ShiftAmtTy));
4239 AddToWorklist(Sign.getNode());
4240
4241 // Add (N0 < 0) ? abs2 - 1 : 0;
4242 SDValue Srl = DAG.getNode(ISD::SRL, DL, VT, Sign, Inexact);
4243 AddToWorklist(Srl.getNode());
4244 SDValue Add = DAG.getNode(ISD::ADD, DL, VT, N0, Srl);
4245 AddToWorklist(Add.getNode());
4246 SDValue Sra = DAG.getNode(ISD::SRA, DL, VT, Add, C1);
4247 AddToWorklist(Sra.getNode());
4248
4249 // Special case: (sdiv X, 1) -> X
4250 // Special Case: (sdiv X, -1) -> 0-X
4251 SDValue One = DAG.getConstant(1, DL, VT);
4252 SDValue AllOnes = DAG.getAllOnesConstant(DL, VT);
4253 SDValue IsOne = DAG.getSetCC(DL, CCVT, N1, One, ISD::SETEQ);
4254 SDValue IsAllOnes = DAG.getSetCC(DL, CCVT, N1, AllOnes, ISD::SETEQ);
4255 SDValue IsOneOrAllOnes = DAG.getNode(ISD::OR, DL, CCVT, IsOne, IsAllOnes);
4256 Sra = DAG.getSelect(DL, VT, IsOneOrAllOnes, N0, Sra);
4257
4258 // If dividing by a positive value, we're done. Otherwise, the result must
4259 // be negated.
4260 SDValue Zero = DAG.getConstant(0, DL, VT);
4261 SDValue Sub = DAG.getNode(ISD::SUB, DL, VT, Zero, Sra);
4262
4263 // FIXME: Use SELECT_CC once we improve SELECT_CC constant-folding.
4264 SDValue IsNeg = DAG.getSetCC(DL, CCVT, N1, Zero, ISD::SETLT);
4265 SDValue Res = DAG.getSelect(DL, VT, IsNeg, Sub, Sra);
4266 return Res;
4267 }
4268
4269 // If integer divide is expensive and we satisfy the requirements, emit an
4270 // alternate sequence. Targets may check function attributes for size/speed
4271 // trade-offs.
4272 AttributeList Attr = DAG.getMachineFunction().getFunction().getAttributes();
4273 if (isConstantOrConstantVector(N1) &&
4274 !TLI.isIntDivCheap(N->getValueType(0), Attr))
4275 if (SDValue Op = BuildSDIV(N))
4276 return Op;
4277
4278 return SDValue();
4279}
4280
4281SDValue DAGCombiner::visitUDIV(SDNode *N) {
4282 SDValue N0 = N->getOperand(0);
4283 SDValue N1 = N->getOperand(1);
4284 EVT VT = N->getValueType(0);
4285 EVT CCVT = getSetCCResultType(VT);
4286
4287 // fold vector ops
4288 if (VT.isVector())
4289 if (SDValue FoldedVOp = SimplifyVBinOp(N))
4290 return FoldedVOp;
4291
4292 SDLoc DL(N);
4293
4294 // fold (udiv c1, c2) -> c1/c2
4295 ConstantSDNode *N1C = isConstOrConstSplat(N1);
4296 if (SDValue C = DAG.FoldConstantArithmetic(ISD::UDIV, DL, VT, {N0, N1}))
4297 return C;
4298
4299 // fold (udiv X, -1) -> select(X == -1, 1, 0)
4300 if (N1C && N1C->getAPIntValue().isAllOnesValue())
4301 return DAG.getSelect(DL, VT, DAG.getSetCC(DL, CCVT, N0, N1, ISD::SETEQ),
4302 DAG.getConstant(1, DL, VT),
4303 DAG.getConstant(0, DL, VT));
4304
4305 if (SDValue V = simplifyDivRem(N, DAG))
4306 return V;
4307
4308 if (SDValue NewSel = foldBinOpIntoSelect(N))
4309 return NewSel;
4310
4311 if (SDValue V = visitUDIVLike(N0, N1, N)) {
4312 // If the corresponding remainder node exists, update its users with
4313 // (Dividend - (Quotient * Divisor).
4314 if (SDNode *RemNode = DAG.getNodeIfExists(ISD::UREM, N->getVTList(),
4315 { N0, N1 })) {
4316 SDValue Mul = DAG.getNode(ISD::MUL, DL, VT, V, N1);
4317 SDValue Sub = DAG.getNode(ISD::SUB, DL, VT, N0, Mul);
4318 AddToWorklist(Mul.getNode());
4319 AddToWorklist(Sub.getNode());
4320 CombineTo(RemNode, Sub);
4321 }
4322 return V;
4323 }
4324
4325 // sdiv, srem -> sdivrem
4326 // If the divisor is constant, then return DIVREM only if isIntDivCheap() is
4327 // true. Otherwise, we break the simplification logic in visitREM().
4328 AttributeList Attr = DAG.getMachineFunction().getFunction().getAttributes();
4329 if (!N1C || TLI.isIntDivCheap(N->getValueType(0), Attr))
4330 if (SDValue DivRem = useDivRem(N))
4331 return DivRem;
4332
4333 return SDValue();
4334}
4335
4336SDValue DAGCombiner::visitUDIVLike(SDValue N0, SDValue N1, SDNode *N) {
4337 SDLoc DL(N);
4338 EVT VT = N->getValueType(0);
4339
4340 // fold (udiv x, (1 << c)) -> x >>u c
4341 if (isConstantOrConstantVector(N1, /*NoOpaques*/ true) &&
4342 DAG.isKnownToBeAPowerOfTwo(N1)) {
4343 SDValue LogBase2 = BuildLogBase2(N1, DL);
4344 AddToWorklist(LogBase2.getNode());
4345
4346 EVT ShiftVT = getShiftAmountTy(N0.getValueType());
4347 SDValue Trunc = DAG.getZExtOrTrunc(LogBase2, DL, ShiftVT);
4348 AddToWorklist(Trunc.getNode());
4349 return DAG.getNode(ISD::SRL, DL, VT, N0, Trunc);
4350 }
4351
4352 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
4353 if (N1.getOpcode() == ISD::SHL) {
4354 SDValue N10 = N1.getOperand(0);
4355 if (isConstantOrConstantVector(N10, /*NoOpaques*/ true) &&
4356 DAG.isKnownToBeAPowerOfTwo(N10)) {
4357 SDValue LogBase2 = BuildLogBase2(N10, DL);
4358 AddToWorklist(LogBase2.getNode());
4359
4360 EVT ADDVT = N1.getOperand(1).getValueType();
4361 SDValue Trunc = DAG.getZExtOrTrunc(LogBase2, DL, ADDVT);
4362 AddToWorklist(Trunc.getNode());
4363 SDValue Add = DAG.getNode(ISD::ADD, DL, ADDVT, N1.getOperand(1), Trunc);
4364 AddToWorklist(Add.getNode());
4365 return DAG.getNode(ISD::SRL, DL, VT, N0, Add);
4366 }
4367 }
4368
4369 // fold (udiv x, c) -> alternate
4370 AttributeList Attr = DAG.getMachineFunction().getFunction().getAttributes();
4371 if (isConstantOrConstantVector(N1) &&
4372 !TLI.isIntDivCheap(N->getValueType(0), Attr))
4373 if (SDValue Op = BuildUDIV(N))
4374 return Op;
4375
4376 return SDValue();
4377}
4378
4379// handles ISD::SREM and ISD::UREM
4380SDValue DAGCombiner::visitREM(SDNode *N) {
4381 unsigned Opcode = N->getOpcode();
4382 SDValue N0 = N->getOperand(0);
4383 SDValue N1 = N->getOperand(1);
4384 EVT VT = N->getValueType(0);
4385 EVT CCVT = getSetCCResultType(VT);
4386
4387 bool isSigned = (Opcode == ISD::SREM);
4388 SDLoc DL(N);
4389
4390 // fold (rem c1, c2) -> c1%c2
4391 ConstantSDNode *N1C = isConstOrConstSplat(N1);
4392 if (SDValue C = DAG.FoldConstantArithmetic(Opcode, DL, VT, {N0, N1}))
4393 return C;
4394
4395 // fold (urem X, -1) -> select(X == -1, 0, x)
4396 if (!isSigned && N1C && N1C->getAPIntValue().isAllOnesValue())
4397 return DAG.getSelect(DL, VT, DAG.getSetCC(DL, CCVT, N0, N1, ISD::SETEQ),
4398 DAG.getConstant(0, DL, VT), N0);
4399
4400 if (SDValue V = simplifyDivRem(N, DAG))
4401 return V;
4402
4403 if (SDValue NewSel = foldBinOpIntoSelect(N))
4404 return NewSel;
4405
4406 if (isSigned) {
4407 // If we know the sign bits of both operands are zero, strength reduce to a
4408 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
4409 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
4410 return DAG.getNode(ISD::UREM, DL, VT, N0, N1);
4411 } else {
4412 if (DAG.isKnownToBeAPowerOfTwo(N1)) {
4413 // fold (urem x, pow2) -> (and x, pow2-1)
4414 SDValue NegOne = DAG.getAllOnesConstant(DL, VT);
4415 SDValue Add = DAG.getNode(ISD::ADD, DL, VT, N1, NegOne);
4416 AddToWorklist(Add.getNode());
4417 return DAG.getNode(ISD::AND, DL, VT, N0, Add);
4418 }
4419 if (N1.getOpcode() == ISD::SHL &&
4420 DAG.isKnownToBeAPowerOfTwo(N1.getOperand(0))) {
4421 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
4422 SDValue NegOne = DAG.getAllOnesConstant(DL, VT);
4423 SDValue Add = DAG.getNode(ISD::ADD, DL, VT, N1, NegOne);
4424 AddToWorklist(Add.getNode());
4425 return DAG.getNode(ISD::AND, DL, VT, N0, Add);
4426 }
4427 }
4428
4429 AttributeList Attr = DAG.getMachineFunction().getFunction().getAttributes();
4430
4431 // If X/C can be simplified by the division-by-constant logic, lower
4432 // X%C to the equivalent of X-X/C*C.
4433 // Reuse the SDIVLike/UDIVLike combines - to avoid mangling nodes, the
4434 // speculative DIV must not cause a DIVREM conversion. We guard against this
4435 // by skipping the simplification if isIntDivCheap(). When div is not cheap,
4436 // combine will not return a DIVREM. Regardless, checking cheapness here
4437 // makes sense since the simplification results in fatter code.
4438 if (DAG.isKnownNeverZero(N1) && !TLI.isIntDivCheap(VT, Attr)) {
4439 SDValue OptimizedDiv =
4440 isSigned ? visitSDIVLike(N0, N1, N) : visitUDIVLike(N0, N1, N);
4441 if (OptimizedDiv.getNode()) {
4442 // If the equivalent Div node also exists, update its users.
4443 unsigned DivOpcode = isSigned ? ISD::SDIV : ISD::UDIV;
4444 if (SDNode *DivNode = DAG.getNodeIfExists(DivOpcode, N->getVTList(),
4445 { N0, N1 }))
4446 CombineTo(DivNode, OptimizedDiv);
4447 SDValue Mul = DAG.getNode(ISD::MUL, DL, VT, OptimizedDiv, N1);
4448 SDValue Sub = DAG.getNode(ISD::SUB, DL, VT, N0, Mul);
4449 AddToWorklist(OptimizedDiv.getNode());
4450 AddToWorklist(Mul.getNode());
4451 return Sub;
4452 }
4453 }
4454
4455 // sdiv, srem -> sdivrem
4456 if (SDValue DivRem = useDivRem(N))
4457 return DivRem.getValue(1);
4458
4459 return SDValue();
4460}
4461
4462SDValue DAGCombiner::visitMULHS(SDNode *N) {
4463 SDValue N0 = N->getOperand(0);
4464 SDValue N1 = N->getOperand(1);
4465 EVT VT = N->getValueType(0);
4466 SDLoc DL(N);
4467
4468 if (VT.isVector()) {
4469 // fold (mulhs x, 0) -> 0
4470 // do not return N0/N1, because undef node may exist.
4471 if (ISD::isConstantSplatVectorAllZeros(N0.getNode()) ||
4472 ISD::isConstantSplatVectorAllZeros(N1.getNode()))
4473 return DAG.getConstant(0, DL, VT);
4474 }
4475
4476 // fold (mulhs c1, c2)
4477 if (SDValue C = DAG.FoldConstantArithmetic(ISD::MULHS, DL, VT, {N0, N1}))
4478 return C;
4479
4480 // fold (mulhs x, 0) -> 0
4481 if (isNullConstant(N1))
4482 return N1;
4483 // fold (mulhs x, 1) -> (sra x, size(x)-1)
4484 if (isOneConstant(N1))
4485 return DAG.getNode(ISD::SRA, DL, N0.getValueType(), N0,
4486 DAG.getConstant(N0.getScalarValueSizeInBits() - 1, DL,
4487 getShiftAmountTy(N0.getValueType())));
4488
4489 // fold (mulhs x, undef) -> 0
4490 if (N0.isUndef() || N1.isUndef())
4491 return DAG.getConstant(0, DL, VT);
4492
4493 // If the type twice as wide is legal, transform the mulhs to a wider multiply
4494 // plus a shift.
4495 if (!TLI.isOperationLegalOrCustom(ISD::MULHS, VT) && VT.isSimple() &&
4496 !VT.isVector()) {
4497 MVT Simple = VT.getSimpleVT();
4498 unsigned SimpleSize = Simple.getSizeInBits();
4499 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
4500 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
4501 N0 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N0);
4502 N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1);
4503 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
4504 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
4505 DAG.getConstant(SimpleSize, DL,
4506 getShiftAmountTy(N1.getValueType())));
4507 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
4508 }
4509 }
4510
4511 return SDValue();
4512}
4513
4514SDValue DAGCombiner::visitMULHU(SDNode *N) {
4515 SDValue N0 = N->getOperand(0);
4516 SDValue N1 = N->getOperand(1);
4517 EVT VT = N->getValueType(0);
4518 SDLoc DL(N);
4519
4520 if (VT.isVector()) {
4521 // fold (mulhu x, 0) -> 0
4522 // do not return N0/N1, because undef node may exist.
4523 if (ISD::isConstantSplatVectorAllZeros(N0.getNode()) ||
4524 ISD::isConstantSplatVectorAllZeros(N1.getNode()))
4525 return DAG.getConstant(0, DL, VT);
4526 }
4527
4528 // fold (mulhu c1, c2)
4529 if (SDValue C = DAG.FoldConstantArithmetic(ISD::MULHU, DL, VT, {N0, N1}))
4530 return C;
4531
4532 // fold (mulhu x, 0) -> 0
4533 if (isNullConstant(N1))
4534 return N1;
4535 // fold (mulhu x, 1) -> 0
4536 if (isOneConstant(N1))
4537 return DAG.getConstant(0, DL, N0.getValueType());
4538 // fold (mulhu x, undef) -> 0
4539 if (N0.isUndef() || N1.isUndef())
4540 return DAG.getConstant(0, DL, VT);
4541
4542 // fold (mulhu x, (1 << c)) -> x >> (bitwidth - c)
4543 if (isConstantOrConstantVector(N1, /*NoOpaques*/ true) &&
4544 DAG.isKnownToBeAPowerOfTwo(N1) && hasOperation(ISD::SRL, VT)) {
4545 unsigned NumEltBits = VT.getScalarSizeInBits();
4546 SDValue LogBase2 = BuildLogBase2(N1, DL);
4547 SDValue SRLAmt = DAG.getNode(
4548 ISD::SUB, DL, VT, DAG.getConstant(NumEltBits, DL, VT), LogBase2);
4549 EVT ShiftVT = getShiftAmountTy(N0.getValueType());
4550 SDValue Trunc = DAG.getZExtOrTrunc(SRLAmt, DL, ShiftVT);
4551 return DAG.getNode(ISD::SRL, DL, VT, N0, Trunc);
4552 }
4553
4554 // If the type twice as wide is legal, transform the mulhu to a wider multiply
4555 // plus a shift.
4556 if (!TLI.isOperationLegalOrCustom(ISD::MULHU, VT) && VT.isSimple() &&
4557 !VT.isVector()) {
4558 MVT Simple = VT.getSimpleVT();
4559 unsigned SimpleSize = Simple.getSizeInBits();
4560 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
4561 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
4562 N0 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N0);
4563 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1);
4564 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
4565 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
4566 DAG.getConstant(SimpleSize, DL,
4567 getShiftAmountTy(N1.getValueType())));
4568 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
4569 }
4570 }
4571
4572 return SDValue();
4573}
4574
4575/// Perform optimizations common to nodes that compute two values. LoOp and HiOp
4576/// give the opcodes for the two computations that are being performed. Return
4577/// true if a simplification was made.
4578SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
4579 unsigned HiOp) {
4580 // If the high half is not needed, just compute the low half.
4581 bool HiExists = N->hasAnyUseOfValue(1);
4582 if (!HiExists && (!LegalOperations ||
4583 TLI.isOperationLegalOrCustom(LoOp, N->getValueType(0)))) {
4584 SDValue Res = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0), N->ops());
4585 return CombineTo(N, Res, Res);
4586 }
4587
4588 // If the low half is not needed, just compute the high half.
4589 bool LoExists = N->hasAnyUseOfValue(0);
4590 if (!LoExists && (!LegalOperations ||
4591 TLI.isOperationLegalOrCustom(HiOp, N->getValueType(1)))) {
4592 SDValue Res = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1), N->ops());
4593 return CombineTo(N, Res, Res);
4594 }
4595
4596 // If both halves are used, return as it is.
4597 if (LoExists && HiExists)
4598 return SDValue();
4599
4600 // If the two computed results can be simplified separately, separate them.
4601 if (LoExists) {
4602 SDValue Lo = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0), N->ops());
4603 AddToWorklist(Lo.getNode());
4604 SDValue LoOpt = combine(Lo.getNode());
4605 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
4606 (!LegalOperations ||
4607 TLI.isOperationLegalOrCustom(LoOpt.getOpcode(), LoOpt.getValueType())))
4608 return CombineTo(N, LoOpt, LoOpt);
4609 }
4610
4611 if (HiExists) {
4612 SDValue Hi = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1), N->ops());
4613 AddToWorklist(Hi.getNode());
4614 SDValue HiOpt = combine(Hi.getNode());
4615 if (HiOpt.getNode() && HiOpt != Hi &&
4616 (!LegalOperations ||
4617 TLI.isOperationLegalOrCustom(HiOpt.getOpcode(), HiOpt.getValueType())))
4618 return CombineTo(N, HiOpt, HiOpt);
4619 }
4620
4621 return SDValue();
4622}
4623
4624SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
4625 if (SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS))
4626 return Res;
4627
4628 EVT VT = N->getValueType(0);
4629 SDLoc DL(N);
4630
4631 // If the type is twice as wide is legal, transform the mulhu to a wider
4632 // multiply plus a shift.
4633 if (VT.isSimple() && !VT.isVector()) {
4634 MVT Simple = VT.getSimpleVT();
4635 unsigned SimpleSize = Simple.getSizeInBits();
4636 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
4637 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
4638 SDValue Lo = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(0));
4639 SDValue Hi = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(1));
4640 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
4641 // Compute the high part as N1.
4642 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
4643 DAG.getConstant(SimpleSize, DL,
4644 getShiftAmountTy(Lo.getValueType())));
4645 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
4646 // Compute the low part as N0.
4647 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
4648 return CombineTo(N, Lo, Hi);
4649 }
4650 }
4651
4652 return SDValue();
4653}
4654
4655SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
4656 if (SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU))
4657 return Res;
4658
4659 EVT VT = N->getValueType(0);
4660 SDLoc DL(N);
4661
4662 // (umul_lohi N0, 0) -> (0, 0)
4663 if (isNullConstant(N->getOperand(1))) {
4664 SDValue Zero = DAG.getConstant(0, DL, VT);
4665 return CombineTo(N, Zero, Zero);
4666 }
4667
4668 // (umul_lohi N0, 1) -> (N0, 0)
4669 if (isOneConstant(N->getOperand(1))) {
4670 SDValue Zero = DAG.getConstant(0, DL, VT);
4671 return CombineTo(N, N->getOperand(0), Zero);
4672 }
4673
4674 // If the type is twice as wide is legal, transform the mulhu to a wider
4675 // multiply plus a shift.
4676 if (VT.isSimple() && !VT.isVector()) {
4677 MVT Simple = VT.getSimpleVT();
4678 unsigned SimpleSize = Simple.getSizeInBits();
4679 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
4680 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
4681 SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(0));
4682 SDValue Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(1));
4683 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
4684 // Compute the high part as N1.
4685 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
4686 DAG.getConstant(SimpleSize, DL,
4687 getShiftAmountTy(Lo.getValueType())));
4688 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
4689 // Compute the low part as N0.
4690 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
4691 return CombineTo(N, Lo, Hi);
4692 }
4693 }
4694
4695 return SDValue();
4696}
4697
4698SDValue DAGCombiner::visitMULO(SDNode *N) {
4699 SDValue N0 = N->getOperand(0);
4700 SDValue N1 = N->getOperand(1);
4701 EVT VT = N0.getValueType();
4702 bool IsSigned = (ISD::SMULO == N->getOpcode());
4703
4704 EVT CarryVT = N->getValueType(1);
4705 SDLoc DL(N);
4706
4707 ConstantSDNode *N0C = isConstOrConstSplat(N0);
4708 ConstantSDNode *N1C = isConstOrConstSplat(N1);
4709
4710 // fold operation with constant operands.
4711 // TODO: Move this to FoldConstantArithmetic when it supports nodes with
4712 // multiple results.
4713 if (N0C && N1C) {
4714 bool Overflow;
4715 APInt Result =
4716 IsSigned ? N0C->getAPIntValue().smul_ov(N1C->getAPIntValue(), Overflow)
4717 : N0C->getAPIntValue().umul_ov(N1C->getAPIntValue(), Overflow);
4718 return CombineTo(N, DAG.getConstant(Result, DL, VT),
4719 DAG.getBoolConstant(Overflow, DL, CarryVT, CarryVT));
4720 }
4721
4722 // canonicalize constant to RHS.
4723 if (DAG.isConstantIntBuildVectorOrConstantInt(N0) &&
4724 !DAG.isConstantIntBuildVectorOrConstantInt(N1))
4725 return DAG.getNode(N->getOpcode(), DL, N->getVTList(), N1, N0);
4726
4727 // fold (mulo x, 0) -> 0 + no carry out
4728 if (isNullOrNullSplat(N1))
4729 return CombineTo(N, DAG.getConstant(0, DL, VT),
4730 DAG.getConstant(0, DL, CarryVT));
4731
4732 // (mulo x, 2) -> (addo x, x)
4733 if (N1C && N1C->getAPIntValue() == 2)
4734 return DAG.getNode(IsSigned ? ISD::SADDO : ISD::UADDO, DL,
4735 N->getVTList(), N0, N0);
4736
4737 if (IsSigned) {
4738 // A 1 bit SMULO overflows if both inputs are 1.
4739 if (VT.getScalarSizeInBits() == 1) {
4740 SDValue And = DAG.getNode(ISD::AND, DL, VT, N0, N1);
4741 return CombineTo(N, And,
4742 DAG.getSetCC(DL, CarryVT, And,
4743 DAG.getConstant(0, DL, VT), ISD::SETNE));
4744 }
4745
4746 // Multiplying n * m significant bits yields a result of n + m significant
4747 // bits. If the total number of significant bits does not exceed the
4748 // result bit width (minus 1), there is no overflow.
4749 unsigned SignBits = DAG.ComputeNumSignBits(N0);
4750 if (SignBits > 1)
4751 SignBits += DAG.ComputeNumSignBits(N1);
4752 if (SignBits > VT.getScalarSizeInBits() + 1)
4753 return CombineTo(N, DAG.getNode(ISD::MUL, DL, VT, N0, N1),
4754 DAG.getConstant(0, DL, CarryVT));
4755 } else {
4756 KnownBits N1Known = DAG.computeKnownBits(N1);
4757 KnownBits N0Known = DAG.computeKnownBits(N0);
4758 bool Overflow;
4759 (void)N0Known.getMaxValue().umul_ov(N1Known.getMaxValue(), Overflow);
4760 if (!Overflow)
4761 return CombineTo(N, DAG.getNode(ISD::MUL, DL, VT, N0, N1),
4762 DAG.getConstant(0, DL, CarryVT));
4763 }
4764
4765 return SDValue();
4766}
4767
4768SDValue DAGCombiner::visitIMINMAX(SDNode *N) {
4769 SDValue N0 = N->getOperand(0);
4770 SDValue N1 = N->getOperand(1);
4771 EVT VT = N0.getValueType();
4772 unsigned Opcode = N->getOpcode();
4773
4774 // fold vector ops
4775 if (VT.isVector())
4776 if (SDValue FoldedVOp = SimplifyVBinOp(N))
4777 return FoldedVOp;
4778
4779 // fold operation with constant operands.
4780 if (SDValue C = DAG.FoldConstantArithmetic(Opcode, SDLoc(N), VT, {N0, N1}))
4781 return C;
4782
4783 // canonicalize constant to RHS
4784 if (DAG.isConstantIntBuildVectorOrConstantInt(N0) &&
4785 !DAG.isConstantIntBuildVectorOrConstantInt(N1))
4786 return DAG.getNode(N->getOpcode(), SDLoc(N), VT, N1, N0);
4787
4788 // Is sign bits are zero, flip between UMIN/UMAX and SMIN/SMAX.
4789 // Only do this if the current op isn't legal and the flipped is.
4790 if (!TLI.isOperationLegal(Opcode, VT) &&
4791 (N0.isUndef() || DAG.SignBitIsZero(N0)) &&
4792 (N1.isUndef() || DAG.SignBitIsZero(N1))) {
4793 unsigned AltOpcode;
4794 switch (Opcode) {
4795 case ISD::SMIN: AltOpcode = ISD::UMIN; break;
4796 case ISD::SMAX: AltOpcode = ISD::UMAX; break;
4797 case ISD::UMIN: AltOpcode = ISD::SMIN; break;
4798 case ISD::UMAX: AltOpcode = ISD::SMAX; break;
4799 default: llvm_unreachable("Unknown MINMAX opcode")::llvm::llvm_unreachable_internal("Unknown MINMAX opcode", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 4799)
;
4800 }
4801 if (TLI.isOperationLegal(AltOpcode, VT))
4802 return DAG.getNode(AltOpcode, SDLoc(N), VT, N0, N1);
4803 }
4804
4805 // Simplify the operands using demanded-bits information.
4806 if (SimplifyDemandedBits(SDValue(N, 0)))
4807 return SDValue(N, 0);
4808
4809 return SDValue();
4810}
4811
4812/// If this is a bitwise logic instruction and both operands have the same
4813/// opcode, try to sink the other opcode after the logic instruction.
4814SDValue DAGCombiner::hoistLogicOpWithSameOpcodeHands(SDNode *N) {
4815 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
4816 EVT VT = N0.getValueType();
4817 unsigned LogicOpcode = N->getOpcode();
4818 unsigned HandOpcode = N0.getOpcode();
4819 assert((LogicOpcode == ISD::AND || LogicOpcode == ISD::OR ||(static_cast <bool> ((LogicOpcode == ISD::AND || LogicOpcode
== ISD::OR || LogicOpcode == ISD::XOR) && "Expected logic opcode"
) ? void (0) : __assert_fail ("(LogicOpcode == ISD::AND || LogicOpcode == ISD::OR || LogicOpcode == ISD::XOR) && \"Expected logic opcode\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 4820, __extension__ __PRETTY_FUNCTION__))
4820 LogicOpcode == ISD::XOR) && "Expected logic opcode")(static_cast <bool> ((LogicOpcode == ISD::AND || LogicOpcode
== ISD::OR || LogicOpcode == ISD::XOR) && "Expected logic opcode"
) ? void (0) : __assert_fail ("(LogicOpcode == ISD::AND || LogicOpcode == ISD::OR || LogicOpcode == ISD::XOR) && \"Expected logic opcode\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 4820, __extension__ __PRETTY_FUNCTION__))
;
4821 assert(HandOpcode == N1.getOpcode() && "Bad input!")(static_cast <bool> (HandOpcode == N1.getOpcode() &&
"Bad input!") ? void (0) : __assert_fail ("HandOpcode == N1.getOpcode() && \"Bad input!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 4821, __extension__ __PRETTY_FUNCTION__))
;
4822
4823 // Bail early if none of these transforms apply.
4824 if (N0.getNumOperands() == 0)
4825 return SDValue();
4826
4827 // FIXME: We should check number of uses of the operands to not increase
4828 // the instruction count for all transforms.
4829
4830 // Handle size-changing casts.
4831 SDValue X = N0.getOperand(0);
4832 SDValue Y = N1.getOperand(0);
4833 EVT XVT = X.getValueType();
4834 SDLoc DL(N);
4835 if (HandOpcode == ISD::ANY_EXTEND || HandOpcode == ISD::ZERO_EXTEND ||
4836 HandOpcode == ISD::SIGN_EXTEND) {
4837 // If both operands have other uses, this transform would create extra
4838 // instructions without eliminating anything.
4839 if (!N0.hasOneUse() && !N1.hasOneUse())
4840 return SDValue();
4841 // We need matching integer source types.
4842 if (XVT != Y.getValueType())
4843 return SDValue();
4844 // Don't create an illegal op during or after legalization. Don't ever
4845 // create an unsupported vector op.
4846 if ((VT.isVector() || LegalOperations) &&
4847 !TLI.isOperationLegalOrCustom(LogicOpcode, XVT))
4848 return SDValue();
4849 // Avoid infinite looping with PromoteIntBinOp.
4850 // TODO: Should we apply desirable/legal constraints to all opcodes?
4851 if (HandOpcode == ISD::ANY_EXTEND && LegalTypes &&
4852 !TLI.isTypeDesirableForOp(LogicOpcode, XVT))
4853 return SDValue();
4854 // logic_op (hand_op X), (hand_op Y) --> hand_op (logic_op X, Y)
4855 SDValue Logic = DAG.getNode(LogicOpcode, DL, XVT, X, Y);
4856 return DAG.getNode(HandOpcode, DL, VT, Logic);
4857 }
4858
4859 // logic_op (truncate x), (truncate y) --> truncate (logic_op x, y)
4860 if (HandOpcode == ISD::TRUNCATE) {
4861 // If both operands have other uses, this transform would create extra
4862 // instructions without eliminating anything.
4863 if (!N0.hasOneUse() && !N1.hasOneUse())
4864 return SDValue();
4865 // We need matching source types.
4866 if (XVT != Y.getValueType())
4867 return SDValue();
4868 // Don't create an illegal op during or after legalization.
4869 if (LegalOperations && !TLI.isOperationLegal(LogicOpcode, XVT))
4870 return SDValue();
4871 // Be extra careful sinking truncate. If it's free, there's no benefit in
4872 // widening a binop. Also, don't create a logic op on an illegal type.
4873 if (TLI.isZExtFree(VT, XVT) && TLI.isTruncateFree(XVT, VT))
4874 return SDValue();
4875 if (!TLI.isTypeLegal(XVT))
4876 return SDValue();
4877 SDValue Logic = DAG.getNode(LogicOpcode, DL, XVT, X, Y);
4878 return DAG.getNode(HandOpcode, DL, VT, Logic);
4879 }
4880
4881 // For binops SHL/SRL/SRA/AND:
4882 // logic_op (OP x, z), (OP y, z) --> OP (logic_op x, y), z
4883 if ((HandOpcode == ISD::SHL || HandOpcode == ISD::SRL ||
4884 HandOpcode == ISD::SRA || HandOpcode == ISD::AND) &&
4885 N0.getOperand(1) == N1.getOperand(1)) {
4886 // If either operand has other uses, this transform is not an improvement.
4887 if (!N0.hasOneUse() || !N1.hasOneUse())
4888 return SDValue();
4889 SDValue Logic = DAG.getNode(LogicOpcode, DL, XVT, X, Y);
4890 return DAG.getNode(HandOpcode, DL, VT, Logic, N0.getOperand(1));
4891 }
4892
4893 // Unary ops: logic_op (bswap x), (bswap y) --> bswap (logic_op x, y)
4894 if (HandOpcode == ISD::BSWAP) {
4895 // If either operand has other uses, this transform is not an improvement.
4896 if (!N0.hasOneUse() || !N1.hasOneUse())
4897 return SDValue();
4898 SDValue Logic = DAG.getNode(LogicOpcode, DL, XVT, X, Y);
4899 return DAG.getNode(HandOpcode, DL, VT, Logic);
4900 }
4901
4902 // Simplify xor/and/or (bitcast(A), bitcast(B)) -> bitcast(op (A,B))
4903 // Only perform this optimization up until type legalization, before
4904 // LegalizeVectorOprs. LegalizeVectorOprs promotes vector operations by
4905 // adding bitcasts. For example (xor v4i32) is promoted to (v2i64), and
4906 // we don't want to undo this promotion.
4907 // We also handle SCALAR_TO_VECTOR because xor/or/and operations are cheaper
4908 // on scalars.
4909 if ((HandOpcode == ISD::BITCAST || HandOpcode == ISD::SCALAR_TO_VECTOR) &&
4910 Level <= AfterLegalizeTypes) {
4911 // Input types must be integer and the same.
4912 if (XVT.isInteger() && XVT == Y.getValueType() &&
4913 !(VT.isVector() && TLI.isTypeLegal(VT) &&
4914 !XVT.isVector() && !TLI.isTypeLegal(XVT))) {
4915 SDValue Logic = DAG.getNode(LogicOpcode, DL, XVT, X, Y);
4916 return DAG.getNode(HandOpcode, DL, VT, Logic);
4917 }
4918 }
4919
4920 // Xor/and/or are indifferent to the swizzle operation (shuffle of one value).
4921 // Simplify xor/and/or (shuff(A), shuff(B)) -> shuff(op (A,B))
4922 // If both shuffles use the same mask, and both shuffle within a single
4923 // vector, then it is worthwhile to move the swizzle after the operation.
4924 // The type-legalizer generates this pattern when loading illegal
4925 // vector types from memory. In many cases this allows additional shuffle
4926 // optimizations.
4927 // There are other cases where moving the shuffle after the xor/and/or
4928 // is profitable even if shuffles don't perform a swizzle.
4929 // If both shuffles use the same mask, and both shuffles have the same first
4930 // or second operand, then it might still be profitable to move the shuffle
4931 // after the xor/and/or operation.
4932 if (HandOpcode == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG) {
4933 auto *SVN0 = cast<ShuffleVectorSDNode>(N0);
4934 auto *SVN1 = cast<ShuffleVectorSDNode>(N1);
4935 assert(X.getValueType() == Y.getValueType() &&(static_cast <bool> (X.getValueType() == Y.getValueType
() && "Inputs to shuffles are not the same type") ? void
(0) : __assert_fail ("X.getValueType() == Y.getValueType() && \"Inputs to shuffles are not the same type\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 4936, __extension__ __PRETTY_FUNCTION__))
4936 "Inputs to shuffles are not the same type")(static_cast <bool> (X.getValueType() == Y.getValueType
() && "Inputs to shuffles are not the same type") ? void
(0) : __assert_fail ("X.getValueType() == Y.getValueType() && \"Inputs to shuffles are not the same type\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 4936, __extension__ __PRETTY_FUNCTION__))
;
4937
4938 // Check that both shuffles use the same mask. The masks are known to be of
4939 // the same length because the result vector type is the same.
4940 // Check also that shuffles have only one use to avoid introducing extra
4941 // instructions.
4942 if (!SVN0->hasOneUse() || !SVN1->hasOneUse() ||
4943 !SVN0->getMask().equals(SVN1->getMask()))
4944 return SDValue();
4945
4946 // Don't try to fold this node if it requires introducing a
4947 // build vector of all zeros that might be illegal at this stage.
4948 SDValue ShOp = N0.getOperand(1);
4949 if (LogicOpcode == ISD::XOR && !ShOp.isUndef())
4950 ShOp = tryFoldToZero(DL, TLI, VT, DAG, LegalOperations);
4951
4952 // (logic_op (shuf (A, C), shuf (B, C))) --> shuf (logic_op (A, B), C)
4953 if (N0.getOperand(1) == N1.getOperand(1) && ShOp.getNode()) {
4954 SDValue Logic = DAG.getNode(LogicOpcode, DL, VT,
4955 N0.getOperand(0), N1.getOperand(0));
4956 return DAG.getVectorShuffle(VT, DL, Logic, ShOp, SVN0->getMask());
4957 }
4958
4959 // Don't try to fold this node if it requires introducing a
4960 // build vector of all zeros that might be illegal at this stage.
4961 ShOp = N0.getOperand(0);
4962 if (LogicOpcode == ISD::XOR && !ShOp.isUndef())
4963 ShOp = tryFoldToZero(DL, TLI, VT, DAG, LegalOperations);
4964
4965 // (logic_op (shuf (C, A), shuf (C, B))) --> shuf (C, logic_op (A, B))
4966 if (N0.getOperand(0) == N1.getOperand(0) && ShOp.getNode()) {
4967 SDValue Logic = DAG.getNode(LogicOpcode, DL, VT, N0.getOperand(1),
4968 N1.getOperand(1));
4969 return DAG.getVectorShuffle(VT, DL, ShOp, Logic, SVN0->getMask());
4970 }
4971 }
4972
4973 return SDValue();
4974}
4975
4976/// Try to make (and/or setcc (LL, LR), setcc (RL, RR)) more efficient.
4977SDValue DAGCombiner::foldLogicOfSetCCs(bool IsAnd, SDValue N0, SDValue N1,
4978 const SDLoc &DL) {
4979 SDValue LL, LR, RL, RR, N0CC, N1CC;
4980 if (!isSetCCEquivalent(N0, LL, LR, N0CC) ||
4981 !isSetCCEquivalent(N1, RL, RR, N1CC))
4982 return SDValue();
4983
4984 assert(N0.getValueType() == N1.getValueType() &&(static_cast <bool> (N0.getValueType() == N1.getValueType
() && "Unexpected operand types for bitwise logic op"
) ? void (0) : __assert_fail ("N0.getValueType() == N1.getValueType() && \"Unexpected operand types for bitwise logic op\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 4985, __extension__ __PRETTY_FUNCTION__))
4985 "Unexpected operand types for bitwise logic op")(static_cast <bool> (N0.getValueType() == N1.getValueType
() && "Unexpected operand types for bitwise logic op"
) ? void (0) : __assert_fail ("N0.getValueType() == N1.getValueType() && \"Unexpected operand types for bitwise logic op\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 4985, __extension__ __PRETTY_FUNCTION__))
;
4986 assert(LL.getValueType() == LR.getValueType() &&(static_cast <bool> (LL.getValueType() == LR.getValueType
() && RL.getValueType() == RR.getValueType() &&
"Unexpected operand types for setcc") ? void (0) : __assert_fail
("LL.getValueType() == LR.getValueType() && RL.getValueType() == RR.getValueType() && \"Unexpected operand types for setcc\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 4988, __extension__ __PRETTY_FUNCTION__))
4987 RL.getValueType() == RR.getValueType() &&(static_cast <bool> (LL.getValueType() == LR.getValueType
() && RL.getValueType() == RR.getValueType() &&
"Unexpected operand types for setcc") ? void (0) : __assert_fail
("LL.getValueType() == LR.getValueType() && RL.getValueType() == RR.getValueType() && \"Unexpected operand types for setcc\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 4988, __extension__ __PRETTY_FUNCTION__))
4988 "Unexpected operand types for setcc")(static_cast <bool> (LL.getValueType() == LR.getValueType
() && RL.getValueType() == RR.getValueType() &&
"Unexpected operand types for setcc") ? void (0) : __assert_fail
("LL.getValueType() == LR.getValueType() && RL.getValueType() == RR.getValueType() && \"Unexpected operand types for setcc\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 4988, __extension__ __PRETTY_FUNCTION__))
;
4989
4990 // If we're here post-legalization or the logic op type is not i1, the logic
4991 // op type must match a setcc result type. Also, all folds require new
4992 // operations on the left and right operands, so those types must match.
4993 EVT VT = N0.getValueType();
4994 EVT OpVT = LL.getValueType();
4995 if (LegalOperations || VT.getScalarType() != MVT::i1)
4996 if (VT != getSetCCResultType(OpVT))
4997 return SDValue();
4998 if (OpVT != RL.getValueType())
4999 return SDValue();
5000
5001 ISD::CondCode CC0 = cast<CondCodeSDNode>(N0CC)->get();
5002 ISD::CondCode CC1 = cast<CondCodeSDNode>(N1CC)->get();
5003 bool IsInteger = OpVT.isInteger();
5004 if (LR == RR && CC0 == CC1 && IsInteger) {
5005 bool IsZero = isNullOrNullSplat(LR);
5006 bool IsNeg1 = isAllOnesOrAllOnesSplat(LR);
5007
5008 // All bits clear?
5009 bool AndEqZero = IsAnd && CC1 == ISD::SETEQ && IsZero;
5010 // All sign bits clear?
5011 bool AndGtNeg1 = IsAnd && CC1 == ISD::SETGT && IsNeg1;
5012 // Any bits set?
5013 bool OrNeZero = !IsAnd && CC1 == ISD::SETNE && IsZero;
5014 // Any sign bits set?
5015 bool OrLtZero = !IsAnd && CC1 == ISD::SETLT && IsZero;
5016
5017 // (and (seteq X, 0), (seteq Y, 0)) --> (seteq (or X, Y), 0)
5018 // (and (setgt X, -1), (setgt Y, -1)) --> (setgt (or X, Y), -1)
5019 // (or (setne X, 0), (setne Y, 0)) --> (setne (or X, Y), 0)
5020 // (or (setlt X, 0), (setlt Y, 0)) --> (setlt (or X, Y), 0)
5021 if (AndEqZero || AndGtNeg1 || OrNeZero || OrLtZero) {
5022 SDValue Or = DAG.getNode(ISD::OR, SDLoc(N0), OpVT, LL, RL);
5023 AddToWorklist(Or.getNode());
5024 return DAG.getSetCC(DL, VT, Or, LR, CC1);
5025 }
5026
5027 // All bits set?
5028 bool AndEqNeg1 = IsAnd && CC1 == ISD::SETEQ && IsNeg1;
5029 // All sign bits set?
5030 bool AndLtZero = IsAnd && CC1 == ISD::SETLT && IsZero;
5031 // Any bits clear?
5032 bool OrNeNeg1 = !IsAnd && CC1 == ISD::SETNE && IsNeg1;
5033 // Any sign bits clear?
5034 bool OrGtNeg1 = !IsAnd && CC1 == ISD::SETGT && IsNeg1;
5035
5036 // (and (seteq X, -1), (seteq Y, -1)) --> (seteq (and X, Y), -1)
5037 // (and (setlt X, 0), (setlt Y, 0)) --> (setlt (and X, Y), 0)
5038 // (or (setne X, -1), (setne Y, -1)) --> (setne (and X, Y), -1)
5039 // (or (setgt X, -1), (setgt Y -1)) --> (setgt (and X, Y), -1)
5040 if (AndEqNeg1 || AndLtZero || OrNeNeg1 || OrGtNeg1) {
5041 SDValue And = DAG.getNode(ISD::AND, SDLoc(N0), OpVT, LL, RL);
5042 AddToWorklist(And.getNode());
5043 return DAG.getSetCC(DL, VT, And, LR, CC1);
5044 }
5045 }
5046
5047 // TODO: What is the 'or' equivalent of this fold?
5048 // (and (setne X, 0), (setne X, -1)) --> (setuge (add X, 1), 2)
5049 if (IsAnd && LL == RL && CC0 == CC1 && OpVT.getScalarSizeInBits() > 1 &&
5050 IsInteger && CC0 == ISD::SETNE &&
5051 ((isNullConstant(LR) && isAllOnesConstant(RR)) ||
5052 (isAllOnesConstant(LR) && isNullConstant(RR)))) {
5053 SDValue One = DAG.getConstant(1, DL, OpVT);
5054 SDValue Two = DAG.getConstant(2, DL, OpVT);
5055 SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N0), OpVT, LL, One);
5056 AddToWorklist(Add.getNode());
5057 return DAG.getSetCC(DL, VT, Add, Two, ISD::SETUGE);
5058 }
5059
5060 // Try more general transforms if the predicates match and the only user of
5061 // the compares is the 'and' or 'or'.
5062 if (IsInteger && TLI.convertSetCCLogicToBitwiseLogic(OpVT) && CC0 == CC1 &&
5063 N0.hasOneUse() && N1.hasOneUse()) {
5064 // and (seteq A, B), (seteq C, D) --> seteq (or (xor A, B), (xor C, D)), 0
5065 // or (setne A, B), (setne C, D) --> setne (or (xor A, B), (xor C, D)), 0
5066 if ((IsAnd && CC1 == ISD::SETEQ) || (!IsAnd && CC1 == ISD::SETNE)) {
5067 SDValue XorL = DAG.getNode(ISD::XOR, SDLoc(N0), OpVT, LL, LR);
5068 SDValue XorR = DAG.getNode(ISD::XOR, SDLoc(N1), OpVT, RL, RR);
5069 SDValue Or = DAG.getNode(ISD::OR, DL, OpVT, XorL, XorR);
5070 SDValue Zero = DAG.getConstant(0, DL, OpVT);
5071 return DAG.getSetCC(DL, VT, Or, Zero, CC1);
5072 }
5073
5074 // Turn compare of constants whose difference is 1 bit into add+and+setcc.
5075 // TODO - support non-uniform vector amounts.
5076 if ((IsAnd && CC1 == ISD::SETNE) || (!IsAnd && CC1 == ISD::SETEQ)) {
5077 // Match a shared variable operand and 2 non-opaque constant operands.
5078 ConstantSDNode *C0 = isConstOrConstSplat(LR);
5079 ConstantSDNode *C1 = isConstOrConstSplat(RR);
5080 if (LL == RL && C0 && C1 && !C0->isOpaque() && !C1->isOpaque()) {
5081 const APInt &CMax =
5082 APIntOps::umax(C0->getAPIntValue(), C1->getAPIntValue());
5083 const APInt &CMin =
5084 APIntOps::umin(C0->getAPIntValue(), C1->getAPIntValue());
5085 // The difference of the constants must be a single bit.
5086 if ((CMax - CMin).isPowerOf2()) {
5087 // and/or (setcc X, CMax, ne), (setcc X, CMin, ne/eq) -->
5088 // setcc ((sub X, CMin), ~(CMax - CMin)), 0, ne/eq
5089 SDValue Max = DAG.getNode(ISD::UMAX, DL, OpVT, LR, RR);
5090 SDValue Min = DAG.getNode(ISD::UMIN, DL, OpVT, LR, RR);
5091 SDValue Offset = DAG.getNode(ISD::SUB, DL, OpVT, LL, Min);
5092 SDValue Diff = DAG.getNode(ISD::SUB, DL, OpVT, Max, Min);
5093 SDValue Mask = DAG.getNOT(DL, Diff, OpVT);
5094 SDValue And = DAG.getNode(ISD::AND, DL, OpVT, Offset, Mask);
5095 SDValue Zero = DAG.getConstant(0, DL, OpVT);
5096 return DAG.getSetCC(DL, VT, And, Zero, CC0);
5097 }
5098 }
5099 }
5100 }
5101
5102 // Canonicalize equivalent operands to LL == RL.
5103 if (LL == RR && LR == RL) {
5104 CC1 = ISD::getSetCCSwappedOperands(CC1);
5105 std::swap(RL, RR);
5106 }
5107
5108 // (and (setcc X, Y, CC0), (setcc X, Y, CC1)) --> (setcc X, Y, NewCC)
5109 // (or (setcc X, Y, CC0), (setcc X, Y, CC1)) --> (setcc X, Y, NewCC)
5110 if (LL == RL && LR == RR) {
5111 ISD::CondCode NewCC = IsAnd ? ISD::getSetCCAndOperation(CC0, CC1, OpVT)
5112 : ISD::getSetCCOrOperation(CC0, CC1, OpVT);
5113 if (NewCC != ISD::SETCC_INVALID &&
5114 (!LegalOperations ||
5115 (TLI.isCondCodeLegal(NewCC, LL.getSimpleValueType()) &&
5116 TLI.isOperationLegal(ISD::SETCC, OpVT))))
5117 return DAG.getSetCC(DL, VT, LL, LR, NewCC);
5118 }
5119
5120 return SDValue();
5121}
5122
5123/// This contains all DAGCombine rules which reduce two values combined by
5124/// an And operation to a single value. This makes them reusable in the context
5125/// of visitSELECT(). Rules involving constants are not included as
5126/// visitSELECT() already handles those cases.
5127SDValue DAGCombiner::visitANDLike(SDValue N0, SDValue N1, SDNode *N) {
5128 EVT VT = N1.getValueType();
5129 SDLoc DL(N);
5130
5131 // fold (and x, undef) -> 0
5132 if (N0.isUndef() || N1.isUndef())
5133 return DAG.getConstant(0, DL, VT);
5134
5135 if (SDValue V = foldLogicOfSetCCs(true, N0, N1, DL))
5136 return V;
5137
5138 if (N0.getOpcode() == ISD::ADD && N1.getOpcode() == ISD::SRL &&
5139 VT.getSizeInBits() <= 64) {
5140 if (ConstantSDNode *ADDI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
5141 if (ConstantSDNode *SRLI = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
5142 // Look for (and (add x, c1), (lshr y, c2)). If C1 wasn't a legal
5143 // immediate for an add, but it is legal if its top c2 bits are set,
5144 // transform the ADD so the immediate doesn't need to be materialized
5145 // in a register.
5146 APInt ADDC = ADDI->getAPIntValue();
5147 APInt SRLC = SRLI->getAPIntValue();
5148 if (ADDC.getMinSignedBits() <= 64 &&
5149 SRLC.ult(VT.getSizeInBits()) &&
5150 !TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
5151 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
5152 SRLC.getZExtValue());
5153 if (DAG.MaskedValueIsZero(N0.getOperand(1), Mask)) {
5154 ADDC |= Mask;
5155 if (TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
5156 SDLoc DL0(N0);
5157 SDValue NewAdd =
5158 DAG.getNode(ISD::ADD, DL0, VT,
5159 N0.getOperand(0), DAG.getConstant(ADDC, DL, VT));
5160 CombineTo(N0.getNode(), NewAdd);
5161 // Return N so it doesn't get rechecked!
5162 return SDValue(N, 0);
5163 }
5164 }
5165 }
5166 }
5167 }
5168 }
5169
5170 // Reduce bit extract of low half of an integer to the narrower type.
5171 // (and (srl i64:x, K), KMask) ->
5172 // (i64 zero_extend (and (srl (i32 (trunc i64:x)), K)), KMask)
5173 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
5174 if (ConstantSDNode *CAnd = dyn_cast<ConstantSDNode>(N1)) {
5175 if (ConstantSDNode *CShift = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
5176 unsigned Size = VT.getSizeInBits();
5177 const APInt &AndMask = CAnd->getAPIntValue();
5178 unsigned ShiftBits = CShift->getZExtValue();
5179
5180 // Bail out, this node will probably disappear anyway.
5181 if (ShiftBits == 0)
5182 return SDValue();
5183
5184 unsigned MaskBits = AndMask.countTrailingOnes();
5185 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), Size / 2);
5186
5187 if (AndMask.isMask() &&
5188 // Required bits must not span the two halves of the integer and
5189 // must fit in the half size type.
5190 (ShiftBits + MaskBits <= Size / 2) &&
5191 TLI.isNarrowingProfitable(VT, HalfVT) &&
5192 TLI.isTypeDesirableForOp(ISD::AND, HalfVT) &&
5193 TLI.isTypeDesirableForOp(ISD::SRL, HalfVT) &&
5194 TLI.isTruncateFree(VT, HalfVT) &&
5195 TLI.isZExtFree(HalfVT, VT)) {
5196 // The isNarrowingProfitable is to avoid regressions on PPC and
5197 // AArch64 which match a few 64-bit bit insert / bit extract patterns
5198 // on downstream users of this. Those patterns could probably be
5199 // extended to handle extensions mixed in.
5200
5201 SDValue SL(N0);
5202 assert(MaskBits <= Size)(static_cast <bool> (MaskBits <= Size) ? void (0) : __assert_fail
("MaskBits <= Size", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 5202, __extension__ __PRETTY_FUNCTION__))
;
5203
5204 // Extracting the highest bit of the low half.
5205 EVT ShiftVT = TLI.getShiftAmountTy(HalfVT, DAG.getDataLayout());
5206 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SL, HalfVT,
5207 N0.getOperand(0));
5208
5209 SDValue NewMask = DAG.getConstant(AndMask.trunc(Size / 2), SL, HalfVT);
5210 SDValue ShiftK = DAG.getConstant(ShiftBits, SL, ShiftVT);
5211 SDValue Shift = DAG.getNode(ISD::SRL, SL, HalfVT, Trunc, ShiftK);
5212 SDValue And = DAG.getNode(ISD::AND, SL, HalfVT, Shift, NewMask);
5213 return DAG.getNode(ISD::ZERO_EXTEND, SL, VT, And);
5214 }
5215 }
5216 }
5217 }
5218
5219 return SDValue();
5220}
5221
5222bool DAGCombiner::isAndLoadExtLoad(ConstantSDNode *AndC, LoadSDNode *LoadN,
5223 EVT LoadResultTy, EVT &ExtVT) {
5224 if (!AndC->getAPIntValue().isMask())
5225 return false;
5226
5227 unsigned ActiveBits = AndC->getAPIntValue().countTrailingOnes();
5228
5229 ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
5230 EVT LoadedVT = LoadN->getMemoryVT();
5231
5232 if (ExtVT == LoadedVT &&
5233 (!LegalOperations ||
5234 TLI.isLoadExtLegal(ISD::ZEXTLOAD, LoadResultTy, ExtVT))) {
5235 // ZEXTLOAD will match without needing to change the size of the value being
5236 // loaded.
5237 return true;
5238 }
5239
5240 // Do not change the width of a volatile or atomic loads.
5241 if (!LoadN->isSimple())
5242 return false;
5243
5244 // Do not generate loads of non-round integer types since these can
5245 // be expensive (and would be wrong if the type is not byte sized).
5246 if (!LoadedVT.bitsGT(ExtVT) || !ExtVT.isRound())
5247 return false;
5248
5249 if (LegalOperations &&
5250 !TLI.isLoadExtLegal(ISD::ZEXTLOAD, LoadResultTy, ExtVT))
5251 return false;
5252
5253 if (!TLI.shouldReduceLoadWidth(LoadN, ISD::ZEXTLOAD, ExtVT))
5254 return false;
5255
5256 return true;
5257}
5258
5259bool DAGCombiner::isLegalNarrowLdSt(LSBaseSDNode *LDST,
5260 ISD::LoadExtType ExtType, EVT &MemVT,
5261 unsigned ShAmt) {
5262 if (!LDST)
5263 return false;
5264 // Only allow byte offsets.
5265 if (ShAmt % 8)
5266 return false;
5267
5268 // Do not generate loads of non-round integer types since these can
5269 // be expensive (and would be wrong if the type is not byte sized).
5270 if (!MemVT.isRound())
5271 return false;
5272
5273 // Don't change the width of a volatile or atomic loads.
5274 if (!LDST->isSimple())
5275 return false;
5276
5277 EVT LdStMemVT = LDST->getMemoryVT();
5278
5279 // Bail out when changing the scalable property, since we can't be sure that
5280 // we're actually narrowing here.
5281 if (LdStMemVT.isScalableVector() != MemVT.isScalableVector())
5282 return false;
5283
5284 // Verify that we are actually reducing a load width here.
5285 if (LdStMemVT.bitsLT(MemVT))
5286 return false;
5287
5288 // Ensure that this isn't going to produce an unsupported memory access.
5289 if (ShAmt) {
5290 assert(ShAmt % 8 == 0 && "ShAmt is byte offset")(static_cast <bool> (ShAmt % 8 == 0 && "ShAmt is byte offset"
) ? void (0) : __assert_fail ("ShAmt % 8 == 0 && \"ShAmt is byte offset\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 5290, __extension__ __PRETTY_FUNCTION__))
;
5291 const unsigned ByteShAmt = ShAmt / 8;
5292 const Align LDSTAlign = LDST->getAlign();
5293 const Align NarrowAlign = commonAlignment(LDSTAlign, ByteShAmt);
5294 if (!TLI.allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), MemVT,
5295 LDST->getAddressSpace(), NarrowAlign,
5296 LDST->getMemOperand()->getFlags()))
5297 return false;
5298 }
5299
5300 // It's not possible to generate a constant of extended or untyped type.
5301 EVT PtrType = LDST->getBasePtr().getValueType();
5302 if (PtrType == MVT::Untyped || PtrType.isExtended())
5303 return false;
5304
5305 if (isa<LoadSDNode>(LDST)) {
5306 LoadSDNode *Load = cast<LoadSDNode>(LDST);
5307 // Don't transform one with multiple uses, this would require adding a new
5308 // load.
5309 if (!SDValue(Load, 0).hasOneUse())
5310 return false;
5311
5312 if (LegalOperations &&
5313 !TLI.isLoadExtLegal(ExtType, Load->getValueType(0), MemVT))
5314 return false;
5315
5316 // For the transform to be legal, the load must produce only two values
5317 // (the value loaded and the chain). Don't transform a pre-increment
5318 // load, for example, which produces an extra value. Otherwise the
5319 // transformation is not equivalent, and the downstream logic to replace
5320 // uses gets things wrong.
5321 if (Load->getNumValues() > 2)
5322 return false;
5323
5324 // If the load that we're shrinking is an extload and we're not just
5325 // discarding the extension we can't simply shrink the load. Bail.
5326 // TODO: It would be possible to merge the extensions in some cases.
5327 if (Load->getExtensionType() != ISD::NON_EXTLOAD &&
5328 Load->getMemoryVT().getSizeInBits() < MemVT.getSizeInBits() + ShAmt)
5329 return false;
5330
5331 if (!TLI.shouldReduceLoadWidth(Load, ExtType, MemVT))
5332 return false;
5333 } else {
5334 assert(isa<StoreSDNode>(LDST) && "It is not a Load nor a Store SDNode")(static_cast <bool> (isa<StoreSDNode>(LDST) &&
"It is not a Load nor a Store SDNode") ? void (0) : __assert_fail
("isa<StoreSDNode>(LDST) && \"It is not a Load nor a Store SDNode\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 5334, __extension__ __PRETTY_FUNCTION__))
;
5335 StoreSDNode *Store = cast<StoreSDNode>(LDST);
5336 // Can't write outside the original store
5337 if (Store->getMemoryVT().getSizeInBits() < MemVT.getSizeInBits() + ShAmt)
5338 return false;
5339
5340 if (LegalOperations &&
5341 !TLI.isTruncStoreLegal(Store->getValue().getValueType(), MemVT))
5342 return false;
5343 }
5344 return true;
5345}
5346
5347bool DAGCombiner::SearchForAndLoads(SDNode *N,
5348 SmallVectorImpl<LoadSDNode*> &Loads,
5349 SmallPtrSetImpl<SDNode*> &NodesWithConsts,
5350 ConstantSDNode *Mask,
5351 SDNode *&NodeToMask) {
5352 // Recursively search for the operands, looking for loads which can be
5353 // narrowed.
5354 for (SDValue Op : N->op_values()) {
5355 if (Op.getValueType().isVector())
5356 return false;
5357
5358 // Some constants may need fixing up later if they are too large.
5359 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
5360 if ((N->getOpcode() == ISD::OR || N->getOpcode() == ISD::XOR) &&
5361 (Mask->getAPIntValue() & C->getAPIntValue()) != C->getAPIntValue())
5362 NodesWithConsts.insert(N);
5363 continue;
5364 }
5365
5366 if (!Op.hasOneUse())
5367 return false;
5368
5369 switch(Op.getOpcode()) {
5370 case ISD::LOAD: {
5371 auto *Load = cast<LoadSDNode>(Op);
5372 EVT ExtVT;
5373 if (isAndLoadExtLoad(Mask, Load, Load->getValueType(0), ExtVT) &&
5374 isLegalNarrowLdSt(Load, ISD::ZEXTLOAD, ExtVT)) {
5375
5376 // ZEXTLOAD is already small enough.
5377 if (Load->getExtensionType() == ISD::ZEXTLOAD &&
5378 ExtVT.bitsGE(Load->getMemoryVT()))
5379 continue;
5380
5381 // Use LE to convert equal sized loads to zext.
5382 if (ExtVT.bitsLE(Load->getMemoryVT()))
5383 Loads.push_back(Load);
5384
5385 continue;
5386 }
5387 return false;
5388 }
5389 case ISD::ZERO_EXTEND:
5390 case ISD::AssertZext: {
5391 unsigned ActiveBits = Mask->getAPIntValue().countTrailingOnes();
5392 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
5393 EVT VT = Op.getOpcode() == ISD::AssertZext ?
5394 cast<VTSDNode>(Op.getOperand(1))->getVT() :
5395 Op.getOperand(0).getValueType();
5396
5397 // We can accept extending nodes if the mask is wider or an equal
5398 // width to the original type.
5399 if (ExtVT.bitsGE(VT))
5400 continue;
5401 break;
5402 }
5403 case ISD::OR:
5404 case ISD::XOR:
5405 case ISD::AND:
5406 if (!SearchForAndLoads(Op.getNode(), Loads, NodesWithConsts, Mask,
5407 NodeToMask))
5408 return false;
5409 continue;
5410 }
5411
5412 // Allow one node which will masked along with any loads found.
5413 if (NodeToMask)
5414 return false;
5415
5416 // Also ensure that the node to be masked only produces one data result.
5417 NodeToMask = Op.getNode();
5418 if (NodeToMask->getNumValues() > 1) {
5419 bool HasValue = false;
5420 for (unsigned i = 0, e = NodeToMask->getNumValues(); i < e; ++i) {
5421 MVT VT = SDValue(NodeToMask, i).getSimpleValueType();
5422 if (VT != MVT::Glue && VT != MVT::Other) {
5423 if (HasValue) {
5424 NodeToMask = nullptr;
5425 return false;
5426 }
5427 HasValue = true;
5428 }
5429 }
5430 assert(HasValue && "Node to be masked has no data result?")(static_cast <bool> (HasValue && "Node to be masked has no data result?"
) ? void (0) : __assert_fail ("HasValue && \"Node to be masked has no data result?\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 5430, __extension__ __PRETTY_FUNCTION__))
;
5431 }
5432 }
5433 return true;
5434}
5435
5436bool DAGCombiner::BackwardsPropagateMask(SDNode *N) {
5437 auto *Mask = dyn_cast<ConstantSDNode>(N->getOperand(1));
5438 if (!Mask)
5439 return false;
5440
5441 if (!Mask->getAPIntValue().isMask())
5442 return false;
5443
5444 // No need to do anything if the and directly uses a load.
5445 if (isa<LoadSDNode>(N->getOperand(0)))
5446 return false;
5447
5448 SmallVector<LoadSDNode*, 8> Loads;
5449 SmallPtrSet<SDNode*, 2> NodesWithConsts;
5450 SDNode *FixupNode = nullptr;
5451 if (SearchForAndLoads(N, Loads, NodesWithConsts, Mask, FixupNode)) {
5452 if (Loads.size() == 0)
5453 return false;
5454
5455 LLVM_DEBUG(dbgs() << "Backwards propagate AND: "; N->dump())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "Backwards propagate AND: "
; N->dump(); } } while (false)
;
5456 SDValue MaskOp = N->getOperand(1);
5457
5458 // If it exists, fixup the single node we allow in the tree that needs
5459 // masking.
5460 if (FixupNode) {
5461 LLVM_DEBUG(dbgs() << "First, need to fix up: "; FixupNode->dump())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "First, need to fix up: "; FixupNode
->dump(); } } while (false)
;
5462 SDValue And = DAG.getNode(ISD::AND, SDLoc(FixupNode),
5463 FixupNode->getValueType(0),
5464 SDValue(FixupNode, 0), MaskOp);
5465 DAG.ReplaceAllUsesOfValueWith(SDValue(FixupNode, 0), And);
5466 if (And.getOpcode() == ISD ::AND)
5467 DAG.UpdateNodeOperands(And.getNode(), SDValue(FixupNode, 0), MaskOp);
5468 }
5469
5470 // Narrow any constants that need it.
5471 for (auto *LogicN : NodesWithConsts) {
5472 SDValue Op0 = LogicN->getOperand(0);
5473 SDValue Op1 = LogicN->getOperand(1);
5474
5475 if (isa<ConstantSDNode>(Op0))
5476 std::swap(Op0, Op1);
5477
5478 SDValue And = DAG.getNode(ISD::AND, SDLoc(Op1), Op1.getValueType(),
5479 Op1, MaskOp);
5480
5481 DAG.UpdateNodeOperands(LogicN, Op0, And);
5482 }
5483
5484 // Create narrow loads.
5485 for (auto *Load : Loads) {
5486 LLVM_DEBUG(dbgs() << "Propagate AND back to: "; Load->dump())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "Propagate AND back to: "; Load
->dump(); } } while (false)
;
5487 SDValue And = DAG.getNode(ISD::AND, SDLoc(Load), Load->getValueType(0),
5488 SDValue(Load, 0), MaskOp);
5489 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), And);
5490 if (And.getOpcode() == ISD ::AND)
5491 And = SDValue(
5492 DAG.UpdateNodeOperands(And.getNode(), SDValue(Load, 0), MaskOp), 0);
5493 SDValue NewLoad = ReduceLoadWidth(And.getNode());
5494 assert(NewLoad &&(static_cast <bool> (NewLoad && "Shouldn't be masking the load if it can't be narrowed"
) ? void (0) : __assert_fail ("NewLoad && \"Shouldn't be masking the load if it can't be narrowed\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 5495, __extension__ __PRETTY_FUNCTION__))
5495 "Shouldn't be masking the load if it can't be narrowed")(static_cast <bool> (NewLoad && "Shouldn't be masking the load if it can't be narrowed"
) ? void (0) : __assert_fail ("NewLoad && \"Shouldn't be masking the load if it can't be narrowed\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 5495, __extension__ __PRETTY_FUNCTION__))
;
5496 CombineTo(Load, NewLoad, NewLoad.getValue(1));
5497 }
5498 DAG.ReplaceAllUsesWith(N, N->getOperand(0).getNode());
5499 return true;
5500 }
5501 return false;
5502}
5503
5504// Unfold
5505// x & (-1 'logical shift' y)
5506// To
5507// (x 'opposite logical shift' y) 'logical shift' y
5508// if it is better for performance.
5509SDValue DAGCombiner::unfoldExtremeBitClearingToShifts(SDNode *N) {
5510 assert(N->getOpcode() == ISD::AND)(static_cast <bool> (N->getOpcode() == ISD::AND) ? void
(0) : __assert_fail ("N->getOpcode() == ISD::AND", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 5510, __extension__ __PRETTY_FUNCTION__))
;
5511
5512 SDValue N0 = N->getOperand(0);
5513 SDValue N1 = N->getOperand(1);
5514
5515 // Do we actually prefer shifts over mask?
5516 if (!TLI.shouldFoldMaskToVariableShiftPair(N0))
5517 return SDValue();
5518
5519 // Try to match (-1 '[outer] logical shift' y)
5520 unsigned OuterShift;
5521 unsigned InnerShift; // The opposite direction to the OuterShift.
5522 SDValue Y; // Shift amount.
5523 auto matchMask = [&OuterShift, &InnerShift, &Y](SDValue M) -> bool {
5524 if (!M.hasOneUse())
5525 return false;
5526 OuterShift = M->getOpcode();
5527 if (OuterShift == ISD::SHL)
5528 InnerShift = ISD::SRL;
5529 else if (OuterShift == ISD::SRL)
5530 InnerShift = ISD::SHL;
5531 else
5532 return false;
5533 if (!isAllOnesConstant(M->getOperand(0)))
5534 return false;
5535 Y = M->getOperand(1);
5536 return true;
5537 };
5538
5539 SDValue X;
5540 if (matchMask(N1))
5541 X = N0;
5542 else if (matchMask(N0))
5543 X = N1;
5544 else
5545 return SDValue();
5546
5547 SDLoc DL(N);
5548 EVT VT = N->getValueType(0);
5549
5550 // tmp = x 'opposite logical shift' y
5551 SDValue T0 = DAG.getNode(InnerShift, DL, VT, X, Y);
5552 // ret = tmp 'logical shift' y
5553 SDValue T1 = DAG.getNode(OuterShift, DL, VT, T0, Y);
5554
5555 return T1;
5556}
5557
5558/// Try to replace shift/logic that tests if a bit is clear with mask + setcc.
5559/// For a target with a bit test, this is expected to become test + set and save
5560/// at least 1 instruction.
5561static SDValue combineShiftAnd1ToBitTest(SDNode *And, SelectionDAG &DAG) {
5562 assert(And->getOpcode() == ISD::AND && "Expected an 'and' op")(static_cast <bool> (And->getOpcode() == ISD::AND &&
"Expected an 'and' op") ? void (0) : __assert_fail ("And->getOpcode() == ISD::AND && \"Expected an 'and' op\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 5562, __extension__ __PRETTY_FUNCTION__))
;
5563
5564 // This is probably not worthwhile without a supported type.
5565 EVT VT = And->getValueType(0);
5566 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5567 if (!TLI.isTypeLegal(VT))
5568 return SDValue();
5569
5570 // Look through an optional extension and find a 'not'.
5571 // TODO: Should we favor test+set even without the 'not' op?
5572 SDValue Not = And->getOperand(0), And1 = And->getOperand(1);
5573 if (Not.getOpcode() == ISD::ANY_EXTEND)
5574 Not = Not.getOperand(0);
5575 if (!isBitwiseNot(Not) || !Not.hasOneUse() || !isOneConstant(And1))
5576 return SDValue();
5577
5578 // Look though an optional truncation. The source operand may not be the same
5579 // type as the original 'and', but that is ok because we are masking off
5580 // everything but the low bit.
5581 SDValue Srl = Not.getOperand(0);
5582 if (Srl.getOpcode() == ISD::TRUNCATE)
5583 Srl = Srl.getOperand(0);
5584
5585 // Match a shift-right by constant.
5586 if (Srl.getOpcode() != ISD::SRL || !Srl.hasOneUse() ||
5587 !isa<ConstantSDNode>(Srl.getOperand(1)))
5588 return SDValue();
5589
5590 // We might have looked through casts that make this transform invalid.
5591 // TODO: If the source type is wider than the result type, do the mask and
5592 // compare in the source type.
5593 const APInt &ShiftAmt = Srl.getConstantOperandAPInt(1);
5594 unsigned VTBitWidth = VT.getSizeInBits();
5595 if (ShiftAmt.uge(VTBitWidth))
5596 return SDValue();
5597
5598 // Turn this into a bit-test pattern using mask op + setcc:
5599 // and (not (srl X, C)), 1 --> (and X, 1<<C) == 0
5600 SDLoc DL(And);
5601 SDValue X = DAG.getZExtOrTrunc(Srl.getOperand(0), DL, VT);
5602 EVT CCVT = TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
5603 SDValue Mask = DAG.getConstant(
5604 APInt::getOneBitSet(VTBitWidth, ShiftAmt.getZExtValue()), DL, VT);
5605 SDValue NewAnd = DAG.getNode(ISD::AND, DL, VT, X, Mask);
5606 SDValue Zero = DAG.getConstant(0, DL, VT);
5607 SDValue Setcc = DAG.getSetCC(DL, CCVT, NewAnd, Zero, ISD::SETEQ);
5608 return DAG.getZExtOrTrunc(Setcc, DL, VT);
5609}
5610
5611SDValue DAGCombiner::visitAND(SDNode *N) {
5612 SDValue N0 = N->getOperand(0);
5613 SDValue N1 = N->getOperand(1);
5614 EVT VT = N1.getValueType();
5615
5616 // x & x --> x
5617 if (N0 == N1)
5618 return N0;
5619
5620 // fold vector ops
5621 if (VT.isVector()) {
5622 if (SDValue FoldedVOp = SimplifyVBinOp(N))
5623 return FoldedVOp;
5624
5625 // fold (and x, 0) -> 0, vector edition
5626 if (ISD::isConstantSplatVectorAllZeros(N0.getNode()))
5627 // do not return N0, because undef node may exist in N0
5628 return DAG.getConstant(APInt::getNullValue(N0.getScalarValueSizeInBits()),
5629 SDLoc(N), N0.getValueType());
5630 if (ISD::isConstantSplatVectorAllZeros(N1.getNode()))
5631 // do not return N1, because undef node may exist in N1
5632 return DAG.getConstant(APInt::getNullValue(N1.getScalarValueSizeInBits()),
5633 SDLoc(N), N1.getValueType());
5634
5635 // fold (and x, -1) -> x, vector edition
5636 if (ISD::isConstantSplatVectorAllOnes(N0.getNode()))
5637 return N1;
5638 if (ISD::isConstantSplatVectorAllOnes(N1.getNode()))
5639 return N0;
5640
5641 // fold (and (masked_load) (build_vec (x, ...))) to zext_masked_load
5642 auto *MLoad = dyn_cast<MaskedLoadSDNode>(N0);
5643 auto *BVec = dyn_cast<BuildVectorSDNode>(N1);
5644 if (MLoad && BVec && MLoad->getExtensionType() == ISD::EXTLOAD &&
5645 N0.hasOneUse() && N1.hasOneUse()) {
5646 EVT LoadVT = MLoad->getMemoryVT();
5647 EVT ExtVT = VT;
5648 if (TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT, LoadVT)) {
5649 // For this AND to be a zero extension of the masked load the elements
5650 // of the BuildVec must mask the bottom bits of the extended element
5651 // type
5652 if (ConstantSDNode *Splat = BVec->getConstantSplatNode()) {
5653 uint64_t ElementSize =
5654 LoadVT.getVectorElementType().getScalarSizeInBits();
5655 if (Splat->getAPIntValue().isMask(ElementSize)) {
5656 return DAG.getMaskedLoad(
5657 ExtVT, SDLoc(N), MLoad->getChain(), MLoad->getBasePtr(),
5658 MLoad->getOffset(), MLoad->getMask(), MLoad->getPassThru(),
5659 LoadVT, MLoad->getMemOperand(), MLoad->getAddressingMode(),
5660 ISD::ZEXTLOAD, MLoad->isExpandingLoad());
5661 }
5662 }
5663 }
5664 }
5665 }
5666
5667 // fold (and c1, c2) -> c1&c2
5668 ConstantSDNode *N1C = isConstOrConstSplat(N1);
5669 if (SDValue C = DAG.FoldConstantArithmetic(ISD::AND, SDLoc(N), VT, {N0, N1}))
5670 return C;
5671
5672 // canonicalize constant to RHS
5673 if (DAG.isConstantIntBuildVectorOrConstantInt(N0) &&
5674 !DAG.isConstantIntBuildVectorOrConstantInt(N1))
5675 return DAG.getNode(ISD::AND, SDLoc(N), VT, N1, N0);
5676
5677 // fold (and x, -1) -> x
5678 if (isAllOnesConstant(N1))
5679 return N0;
5680
5681 // if (and x, c) is known to be zero, return 0
5682 unsigned BitWidth = VT.getScalarSizeInBits();
5683 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
5684 APInt::getAllOnesValue(BitWidth)))
5685 return DAG.getConstant(0, SDLoc(N), VT);
5686
5687 if (SDValue NewSel = foldBinOpIntoSelect(N))
5688 return NewSel;
5689
5690 // reassociate and
5691 if (SDValue RAND = reassociateOps(ISD::AND, SDLoc(N), N0, N1, N->getFlags()))
5692 return RAND;
5693
5694 // Try to convert a constant mask AND into a shuffle clear mask.
5695 if (VT.isVector())
5696 if (SDValue Shuffle = XformToShuffleWithZero(N))
5697 return Shuffle;
5698
5699 if (SDValue Combined = combineCarryDiamond(*this, DAG, TLI, N0, N1, N))
5700 return Combined;
5701
5702 // fold (and (or x, C), D) -> D if (C & D) == D
5703 auto MatchSubset = [](ConstantSDNode *LHS, ConstantSDNode *RHS) {
5704 return RHS->getAPIntValue().isSubsetOf(LHS->getAPIntValue());
5705 };
5706 if (N0.getOpcode() == ISD::OR &&
5707 ISD::matchBinaryPredicate(N0.getOperand(1), N1, MatchSubset))
5708 return N1;
5709 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
5710 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
5711 SDValue N0Op0 = N0.getOperand(0);
5712 APInt Mask = ~N1C->getAPIntValue();
5713 Mask = Mask.trunc(N0Op0.getScalarValueSizeInBits());
5714 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
5715 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N),
5716 N0.getValueType(), N0Op0);
5717
5718 // Replace uses of the AND with uses of the Zero extend node.
5719 CombineTo(N, Zext);
5720
5721 // We actually want to replace all uses of the any_extend with the
5722 // zero_extend, to avoid duplicating things. This will later cause this
5723 // AND to be folded.
5724 CombineTo(N0.getNode(), Zext);
5725 return SDValue(N, 0); // Return N so it doesn't get rechecked!
5726 }
5727 }
5728
5729 // similarly fold (and (X (load ([non_ext|any_ext|zero_ext] V))), c) ->
5730 // (X (load ([non_ext|zero_ext] V))) if 'and' only clears top bits which must
5731 // already be zero by virtue of the width of the base type of the load.
5732 //
5733 // the 'X' node here can either be nothing or an extract_vector_elt to catch
5734 // more cases.
5735 if ((N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5736 N0.getValueSizeInBits() == N0.getOperand(0).getScalarValueSizeInBits() &&
5737 N0.getOperand(0).getOpcode() == ISD::LOAD &&
5738 N0.getOperand(0).getResNo() == 0) ||
5739 (N0.getOpcode() == ISD::LOAD && N0.getResNo() == 0)) {
5740 LoadSDNode *Load = cast<LoadSDNode>( (N0.getOpcode() == ISD::LOAD) ?
5741 N0 : N0.getOperand(0) );
5742
5743 // Get the constant (if applicable) the zero'th operand is being ANDed with.
5744 // This can be a pure constant or a vector splat, in which case we treat the
5745 // vector as a scalar and use the splat value.
5746 APInt Constant = APInt::getNullValue(1);
5747 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
5748 Constant = C->getAPIntValue();
5749 } else if (BuildVectorSDNode *Vector = dyn_cast<BuildVectorSDNode>(N1)) {
5750 APInt SplatValue, SplatUndef;
5751 unsigned SplatBitSize;
5752 bool HasAnyUndefs;
5753 bool IsSplat = Vector->isConstantSplat(SplatValue, SplatUndef,
5754 SplatBitSize, HasAnyUndefs);
5755 if (IsSplat) {
5756 // Undef bits can contribute to a possible optimisation if set, so
5757 // set them.
5758 SplatValue |= SplatUndef;
5759
5760 // The splat value may be something like "0x00FFFFFF", which means 0 for
5761 // the first vector value and FF for the rest, repeating. We need a mask
5762 // that will apply equally to all members of the vector, so AND all the
5763 // lanes of the constant together.
5764 unsigned EltBitWidth = Vector->getValueType(0).getScalarSizeInBits();
5765
5766 // If the splat value has been compressed to a bitlength lower
5767 // than the size of the vector lane, we need to re-expand it to
5768 // the lane size.
5769 if (EltBitWidth > SplatBitSize)
5770 for (SplatValue = SplatValue.zextOrTrunc(EltBitWidth);
5771 SplatBitSize < EltBitWidth; SplatBitSize = SplatBitSize * 2)
5772 SplatValue |= SplatValue.shl(SplatBitSize);
5773
5774 // Make sure that variable 'Constant' is only set if 'SplatBitSize' is a
5775 // multiple of 'BitWidth'. Otherwise, we could propagate a wrong value.
5776 if ((SplatBitSize % EltBitWidth) == 0) {
5777 Constant = APInt::getAllOnesValue(EltBitWidth);
5778 for (unsigned i = 0, n = (SplatBitSize / EltBitWidth); i < n; ++i)
5779 Constant &= SplatValue.extractBits(EltBitWidth, i * EltBitWidth);
5780 }
5781 }
5782 }
5783
5784 // If we want to change an EXTLOAD to a ZEXTLOAD, ensure a ZEXTLOAD is
5785 // actually legal and isn't going to get expanded, else this is a false
5786 // optimisation.
5787 bool CanZextLoadProfitably = TLI.isLoadExtLegal(ISD::ZEXTLOAD,
5788 Load->getValueType(0),
5789 Load->getMemoryVT());
5790
5791 // Resize the constant to the same size as the original memory access before
5792 // extension. If it is still the AllOnesValue then this AND is completely
5793 // unneeded.
5794 Constant = Constant.zextOrTrunc(Load->getMemoryVT().getScalarSizeInBits());
5795
5796 bool B;
5797 switch (Load->getExtensionType()) {
5798 default: B = false; break;
5799 case ISD::EXTLOAD: B = CanZextLoadProfitably; break;
5800 case ISD::ZEXTLOAD:
5801 case ISD::NON_EXTLOAD: B = true; break;
5802 }
5803
5804 if (B && Constant.isAllOnesValue()) {
5805 // If the load type was an EXTLOAD, convert to ZEXTLOAD in order to
5806 // preserve semantics once we get rid of the AND.
5807 SDValue NewLoad(Load, 0);
5808
5809 // Fold the AND away. NewLoad may get replaced immediately.
5810 CombineTo(N, (N0.getNode() == Load) ? NewLoad : N0);
5811
5812 if (Load->getExtensionType() == ISD::EXTLOAD) {
5813 NewLoad = DAG.getLoad(Load->getAddressingMode(), ISD::ZEXTLOAD,
5814 Load->getValueType(0), SDLoc(Load),
5815 Load->getChain(), Load->getBasePtr(),
5816 Load->getOffset(), Load->getMemoryVT(),
5817 Load->getMemOperand());
5818 // Replace uses of the EXTLOAD with the new ZEXTLOAD.
5819 if (Load->getNumValues() == 3) {
5820 // PRE/POST_INC loads have 3 values.
5821 SDValue To[] = { NewLoad.getValue(0), NewLoad.getValue(1),
5822 NewLoad.getValue(2) };
5823 CombineTo(Load, To, 3, true);
5824 } else {
5825 CombineTo(Load, NewLoad.getValue(0), NewLoad.getValue(1));
5826 }
5827 }
5828
5829 return SDValue(N, 0); // Return N so it doesn't get rechecked!
5830 }
5831 }
5832
5833 // fold (and (masked_gather x)) -> (zext_masked_gather x)
5834 if (auto *GN0 = dyn_cast<MaskedGatherSDNode>(N0)) {
5835 EVT MemVT = GN0->getMemoryVT();
5836 EVT ScalarVT = MemVT.getScalarType();
5837
5838 if (SDValue(GN0, 0).hasOneUse() &&
5839 isConstantSplatVectorMaskForType(N1.getNode(), ScalarVT) &&
5840 TLI.isVectorLoadExtDesirable(SDValue(SDValue(GN0, 0)))) {
5841 SDValue Ops[] = {GN0->getChain(), GN0->getPassThru(), GN0->getMask(),
5842 GN0->getBasePtr(), GN0->getIndex(), GN0->getScale()};
5843
5844 SDValue ZExtLoad = DAG.getMaskedGather(
5845 DAG.getVTList(VT, MVT::Other), MemVT, SDLoc(N), Ops,
5846 GN0->getMemOperand(), GN0->getIndexType(), ISD::ZEXTLOAD);
5847
5848 CombineTo(N, ZExtLoad);
5849 AddToWorklist(ZExtLoad.getNode());
5850 // Avoid recheck of N.
5851 return SDValue(N, 0);
5852 }
5853 }
5854
5855 // fold (and (load x), 255) -> (zextload x, i8)
5856 // fold (and (extload x, i16), 255) -> (zextload x, i8)
5857 // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8)
5858 if (!VT.isVector() && N1C && (N0.getOpcode() == ISD::LOAD ||
5859 (N0.getOpcode() == ISD::ANY_EXTEND &&
5860 N0.getOperand(0).getOpcode() == ISD::LOAD))) {
5861 if (SDValue Res = ReduceLoadWidth(N)) {
5862 LoadSDNode *LN0 = N0->getOpcode() == ISD::ANY_EXTEND
5863 ? cast<LoadSDNode>(N0.getOperand(0)) : cast<LoadSDNode>(N0);
5864 AddToWorklist(N);
5865 DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 0), Res);
5866 return SDValue(N, 0);
5867 }
5868 }
5869
5870 if (LegalTypes) {
5871 // Attempt to propagate the AND back up to the leaves which, if they're
5872 // loads, can be combined to narrow loads and the AND node can be removed.
5873 // Perform after legalization so that extend nodes will already be
5874 // combined into the loads.
5875 if (BackwardsPropagateMask(N))
5876 return SDValue(N, 0);
5877 }
5878
5879 if (SDValue Combined = visitANDLike(N0, N1, N))
5880 return Combined;
5881
5882 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
5883 if (N0.getOpcode() == N1.getOpcode())
5884 if (SDValue V = hoistLogicOpWithSameOpcodeHands(N))
5885 return V;
5886
5887 // Masking the negated extension of a boolean is just the zero-extended
5888 // boolean:
5889 // and (sub 0, zext(bool X)), 1 --> zext(bool X)
5890 // and (sub 0, sext(bool X)), 1 --> zext(bool X)
5891 //
5892 // Note: the SimplifyDemandedBits fold below can make an information-losing
5893 // transform, and then we have no way to find this better fold.
5894 if (N1C && N1C->isOne() && N0.getOpcode() == ISD::SUB) {
5895 if (isNullOrNullSplat(N0.getOperand(0))) {
5896 SDValue SubRHS = N0.getOperand(1);
5897 if (SubRHS.getOpcode() == ISD::ZERO_EXTEND &&
5898 SubRHS.getOperand(0).getScalarValueSizeInBits() == 1)
5899 return SubRHS;
5900 if (SubRHS.getOpcode() == ISD::SIGN_EXTEND &&
5901 SubRHS.getOperand(0).getScalarValueSizeInBits() == 1)
5902 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, SubRHS.getOperand(0));
5903 }
5904 }
5905
5906 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
5907 // fold (and (sra)) -> (and (srl)) when possible.
5908 if (SimplifyDemandedBits(SDValue(N, 0)))
5909 return SDValue(N, 0);
5910
5911 // fold (zext_inreg (extload x)) -> (zextload x)
5912 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
5913 if (ISD::isUNINDEXEDLoad(N0.getNode()) &&
5914 (ISD::isEXTLoad(N0.getNode()) ||
5915 (ISD::isSEXTLoad(N0.getNode()) && N0.hasOneUse()))) {
5916 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
5917 EVT MemVT = LN0->getMemoryVT();
5918 // If we zero all the possible extended bits, then we can turn this into
5919 // a zextload if we are running before legalize or the operation is legal.
5920 unsigned ExtBitSize = N1.getScalarValueSizeInBits();
5921 unsigned MemBitSize = MemVT.getScalarSizeInBits();
5922 APInt ExtBits = APInt::getHighBitsSet(ExtBitSize, ExtBitSize - MemBitSize);
5923 if (DAG.MaskedValueIsZero(N1, ExtBits) &&
5924 ((!LegalOperations && LN0->isSimple()) ||
5925 TLI.isLoadExtLegal(ISD::ZEXTLOAD, VT, MemVT))) {
5926 SDValue ExtLoad =
5927 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT, LN0->getChain(),
5928 LN0->getBasePtr(), MemVT, LN0->getMemOperand());
5929 AddToWorklist(N);
5930 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
5931 return SDValue(N, 0); // Return N so it doesn't get rechecked!
5932 }
5933 }
5934
5935 // fold (and (or (srl N, 8), (shl N, 8)), 0xffff) -> (srl (bswap N), const)
5936 if (N1C && N1C->getAPIntValue() == 0xffff && N0.getOpcode() == ISD::OR) {
5937 if (SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
5938 N0.getOperand(1), false))
5939 return BSwap;
5940 }
5941
5942 if (SDValue Shifts = unfoldExtremeBitClearingToShifts(N))
5943 return Shifts;
5944
5945 if (TLI.hasBitTest(N0, N1))
5946 if (SDValue V = combineShiftAnd1ToBitTest(N, DAG))
5947 return V;
5948
5949 // Recognize the following pattern:
5950 //
5951 // AndVT = (and (sign_extend NarrowVT to AndVT) #bitmask)
5952 //
5953 // where bitmask is a mask that clears the upper bits of AndVT. The
5954 // number of bits in bitmask must be a power of two.
5955 auto IsAndZeroExtMask = [](SDValue LHS, SDValue RHS) {
5956 if (LHS->getOpcode() != ISD::SIGN_EXTEND)
5957 return false;
5958
5959 auto *C = dyn_cast<ConstantSDNode>(RHS);
5960 if (!C)
5961 return false;
5962
5963 if (!C->getAPIntValue().isMask(
5964 LHS.getOperand(0).getValueType().getFixedSizeInBits()))
5965 return false;
5966
5967 return true;
5968 };
5969
5970 // Replace (and (sign_extend ...) #bitmask) with (zero_extend ...).
5971 if (IsAndZeroExtMask(N0, N1))
5972 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0.getOperand(0));
5973
5974 return SDValue();
5975}
5976
5977/// Match (a >> 8) | (a << 8) as (bswap a) >> 16.
5978SDValue DAGCombiner::MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
5979 bool DemandHighBits) {
5980 if (!LegalOperations)
5981 return SDValue();
5982
5983 EVT VT = N->getValueType(0);
5984 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16)
5985 return SDValue();
5986 if (!TLI.isOperationLegalOrCustom(ISD::BSWAP, VT))
5987 return SDValue();
5988
5989 // Recognize (and (shl a, 8), 0xff00), (and (srl a, 8), 0xff)
5990 bool LookPassAnd0 = false;
5991 bool LookPassAnd1 = false;
5992 if (N0.getOpcode() == ISD::AND && N0.getOperand(0).getOpcode() == ISD::SRL)
5993 std::swap(N0, N1);
5994 if (N1.getOpcode() == ISD::AND && N1.getOperand(0).getOpcode() == ISD::SHL)
5995 std::swap(N0, N1);
5996 if (N0.getOpcode() == ISD::AND) {
5997 if (!N0.getNode()->hasOneUse())
5998 return SDValue();
5999 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
6000 // Also handle 0xffff since the LHS is guaranteed to have zeros there.
6001 // This is needed for X86.
6002 if (!N01C || (N01C->getZExtValue() != 0xFF00 &&
6003 N01C->getZExtValue() != 0xFFFF))
6004 return SDValue();
6005 N0 = N0.getOperand(0);
6006 LookPassAnd0 = true;
6007 }
6008
6009 if (N1.getOpcode() == ISD::AND) {
6010 if (!N1.getNode()->hasOneUse())
6011 return SDValue();
6012 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
6013 if (!N11C || N11C->getZExtValue() != 0xFF)
6014 return SDValue();
6015 N1 = N1.getOperand(0);
6016 LookPassAnd1 = true;
6017 }
6018
6019 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
6020 std::swap(N0, N1);
6021 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
6022 return SDValue();
6023 if (!N0.getNode()->hasOneUse() || !N1.getNode()->hasOneUse())
6024 return SDValue();
6025
6026 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
6027 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
6028 if (!N01C || !N11C)
6029 return SDValue();
6030 if (N01C->getZExtValue() != 8 || N11C->getZExtValue() != 8)
6031 return SDValue();
6032
6033 // Look for (shl (and a, 0xff), 8), (srl (and a, 0xff00), 8)
6034 SDValue N00 = N0->getOperand(0);
6035 if (!LookPassAnd0 && N00.getOpcode() == ISD::AND) {
6036 if (!N00.getNode()->hasOneUse())
6037 return SDValue();
6038 ConstantSDNode *N001C = dyn_cast<ConstantSDNode>(N00.getOperand(1));
6039 if (!N001C || N001C->getZExtValue() != 0xFF)
6040 return SDValue();
6041 N00 = N00.getOperand(0);
6042 LookPassAnd0 = true;
6043 }
6044
6045 SDValue N10 = N1->getOperand(0);
6046 if (!LookPassAnd1 && N10.getOpcode() == ISD::AND) {
6047 if (!N10.getNode()->hasOneUse())
6048 return SDValue();
6049 ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N10.getOperand(1));
6050 // Also allow 0xFFFF since the bits will be shifted out. This is needed
6051 // for X86.
6052 if (!N101C || (N101C->getZExtValue() != 0xFF00 &&
6053 N101C->getZExtValue() != 0xFFFF))
6054 return SDValue();
6055 N10 = N10.getOperand(0);
6056 LookPassAnd1 = true;
6057 }
6058
6059 if (N00 != N10)
6060 return SDValue();
6061
6062 // Make sure everything beyond the low halfword gets set to zero since the SRL
6063 // 16 will clear the top bits.
6064 unsigned OpSizeInBits = VT.getSizeInBits();
6065 if (DemandHighBits && OpSizeInBits > 16) {
6066 // If the left-shift isn't masked out then the only way this is a bswap is
6067 // if all bits beyond the low 8 are 0. In that case the entire pattern
6068 // reduces to a left shift anyway: leave it for other parts of the combiner.
6069 if (!LookPassAnd0)
6070 return SDValue();
6071
6072 // However, if the right shift isn't masked out then it might be because
6073 // it's not needed. See if we can spot that too.
6074 if (!LookPassAnd1 &&
6075 !DAG.MaskedValueIsZero(
6076 N10, APInt::getHighBitsSet(OpSizeInBits, OpSizeInBits - 16)))
6077 return SDValue();
6078 }
6079
6080 SDValue Res = DAG.getNode(ISD::BSWAP, SDLoc(N), VT, N00);
6081 if (OpSizeInBits > 16) {
6082 SDLoc DL(N);
6083 Res = DAG.getNode(ISD::SRL, DL, VT, Res,
6084 DAG.getConstant(OpSizeInBits - 16, DL,
6085 getShiftAmountTy(VT)));
6086 }
6087 return Res;
6088}
6089
6090/// Return true if the specified node is an element that makes up a 32-bit
6091/// packed halfword byteswap.
6092/// ((x & 0x000000ff) << 8) |
6093/// ((x & 0x0000ff00) >> 8) |
6094/// ((x & 0x00ff0000) << 8) |
6095/// ((x & 0xff000000) >> 8)
6096static bool isBSwapHWordElement(SDValue N, MutableArrayRef<SDNode *> Parts) {
6097 if (!N.getNode()->hasOneUse())
6098 return false;
6099
6100 unsigned Opc = N.getOpcode();
6101 if (Opc != ISD::AND && Opc != ISD::SHL && Opc != ISD::SRL)
6102 return false;
6103
6104 SDValue N0 = N.getOperand(0);
6105 unsigned Opc0 = N0.getOpcode();
6106 if (Opc0 != ISD::AND && Opc0 != ISD::SHL && Opc0 != ISD::SRL)
6107 return false;
6108
6109 ConstantSDNode *N1C = nullptr;
6110 // SHL or SRL: look upstream for AND mask operand
6111 if (Opc == ISD::AND)
6112 N1C = dyn_cast<ConstantSDNode>(N.getOperand(1));
6113 else if (Opc0 == ISD::AND)
6114 N1C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
6115 if (!N1C)
6116 return false;
6117
6118 unsigned MaskByteOffset;
6119 switch (N1C->getZExtValue()) {
6120 default:
6121 return false;
6122 case 0xFF: MaskByteOffset = 0; break;
6123 case 0xFF00: MaskByteOffset = 1; break;
6124 case 0xFFFF:
6125 // In case demanded bits didn't clear the bits that will be shifted out.
6126 // This is needed for X86.
6127 if (Opc == ISD::SRL || (Opc == ISD::AND && Opc0 == ISD::SHL)) {
6128 MaskByteOffset = 1;
6129 break;
6130 }
6131 return false;
6132 case 0xFF0000: MaskByteOffset = 2; break;
6133 case 0xFF000000: MaskByteOffset = 3; break;
6134 }
6135
6136 // Look for (x & 0xff) << 8 as well as ((x << 8) & 0xff00).
6137 if (Opc == ISD::AND) {
6138 if (MaskByteOffset == 0 || MaskByteOffset == 2) {
6139 // (x >> 8) & 0xff
6140 // (x >> 8) & 0xff0000
6141 if (Opc0 != ISD::SRL)
6142 return false;
6143 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
6144 if (!C || C->getZExtValue() != 8)
6145 return false;
6146 } else {
6147 // (x << 8) & 0xff00
6148 // (x << 8) & 0xff000000
6149 if (Opc0 != ISD::SHL)
6150 return false;
6151 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
6152 if (!C || C->getZExtValue() != 8)
6153 return false;
6154 }
6155 } else if (Opc == ISD::SHL) {
6156 // (x & 0xff) << 8
6157 // (x & 0xff0000) << 8
6158 if (MaskByteOffset != 0 && MaskByteOffset != 2)
6159 return false;
6160 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
6161 if (!C || C->getZExtValue() != 8)
6162 return false;
6163 } else { // Opc == ISD::SRL
6164 // (x & 0xff00) >> 8
6165 // (x & 0xff000000) >> 8
6166 if (MaskByteOffset != 1 && MaskByteOffset != 3)
6167 return false;
6168 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
6169 if (!C || C->getZExtValue() != 8)
6170 return false;
6171 }
6172
6173 if (Parts[MaskByteOffset])
6174 return false;
6175
6176 Parts[MaskByteOffset] = N0.getOperand(0).getNode();
6177 return true;
6178}
6179
6180// Match 2 elements of a packed halfword bswap.
6181static bool isBSwapHWordPair(SDValue N, MutableArrayRef<SDNode *> Parts) {
6182 if (N.getOpcode() == ISD::OR)
6183 return isBSwapHWordElement(N.getOperand(0), Parts) &&
6184 isBSwapHWordElement(N.getOperand(1), Parts);
6185
6186 if (N.getOpcode() == ISD::SRL && N.getOperand(0).getOpcode() == ISD::BSWAP) {
6187 ConstantSDNode *C = isConstOrConstSplat(N.getOperand(1));
6188 if (!C || C->getAPIntValue() != 16)
6189 return false;
6190 Parts[0] = Parts[1] = N.getOperand(0).getOperand(0).getNode();
6191 return true;
6192 }
6193
6194 return false;
6195}
6196
6197// Match this pattern:
6198// (or (and (shl (A, 8)), 0xff00ff00), (and (srl (A, 8)), 0x00ff00ff))
6199// And rewrite this to:
6200// (rotr (bswap A), 16)
6201static SDValue matchBSwapHWordOrAndAnd(const TargetLowering &TLI,
6202 SelectionDAG &DAG, SDNode *N, SDValue N0,
6203 SDValue N1, EVT VT, EVT ShiftAmountTy) {
6204 assert(N->getOpcode() == ISD::OR && VT == MVT::i32 &&(static_cast <bool> (N->getOpcode() == ISD::OR &&
VT == MVT::i32 && "MatchBSwapHWordOrAndAnd: expecting i32"
) ? void (0) : __assert_fail ("N->getOpcode() == ISD::OR && VT == MVT::i32 && \"MatchBSwapHWordOrAndAnd: expecting i32\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 6205, __extension__ __PRETTY_FUNCTION__))
6205 "MatchBSwapHWordOrAndAnd: expecting i32")(static_cast <bool> (N->getOpcode() == ISD::OR &&
VT == MVT::i32 && "MatchBSwapHWordOrAndAnd: expecting i32"
) ? void (0) : __assert_fail ("N->getOpcode() == ISD::OR && VT == MVT::i32 && \"MatchBSwapHWordOrAndAnd: expecting i32\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 6205, __extension__ __PRETTY_FUNCTION__))
;
6206 if (!TLI.isOperationLegalOrCustom(ISD::ROTR, VT))
6207 return SDValue();
6208 if (N0.getOpcode() != ISD::AND || N1.getOpcode() != ISD::AND)
6209 return SDValue();
6210 // TODO: this is too restrictive; lifting this restriction requires more tests
6211 if (!N0->hasOneUse() || !N1->hasOneUse())
6212 return SDValue();
6213 ConstantSDNode *Mask0 = isConstOrConstSplat(N0.getOperand(1));
6214 ConstantSDNode *Mask1 = isConstOrConstSplat(N1.getOperand(1));
6215 if (!Mask0 || !Mask1)
6216 return SDValue();
6217 if (Mask0->getAPIntValue() != 0xff00ff00 ||
6218 Mask1->getAPIntValue() != 0x00ff00ff)
6219 return SDValue();
6220 SDValue Shift0 = N0.getOperand(0);
6221 SDValue Shift1 = N1.getOperand(0);
6222 if (Shift0.getOpcode() != ISD::SHL || Shift1.getOpcode() != ISD::SRL)
6223 return SDValue();
6224 ConstantSDNode *ShiftAmt0 = isConstOrConstSplat(Shift0.getOperand(1));
6225 ConstantSDNode *ShiftAmt1 = isConstOrConstSplat(Shift1.getOperand(1));
6226 if (!ShiftAmt0 || !ShiftAmt1)
6227 return SDValue();
6228 if (ShiftAmt0->getAPIntValue() != 8 || ShiftAmt1->getAPIntValue() != 8)
6229 return SDValue();
6230 if (Shift0.getOperand(0) != Shift1.getOperand(0))
6231 return SDValue();
6232
6233 SDLoc DL(N);
6234 SDValue BSwap = DAG.getNode(ISD::BSWAP, DL, VT, Shift0.getOperand(0));
6235 SDValue ShAmt = DAG.getConstant(16, DL, ShiftAmountTy);
6236 return DAG.getNode(ISD::ROTR, DL, VT, BSwap, ShAmt);
6237}
6238
6239/// Match a 32-bit packed halfword bswap. That is
6240/// ((x & 0x000000ff) << 8) |
6241/// ((x & 0x0000ff00) >> 8) |
6242/// ((x & 0x00ff0000) << 8) |
6243/// ((x & 0xff000000) >> 8)
6244/// => (rotl (bswap x), 16)
6245SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) {
6246 if (!LegalOperations)
6247 return SDValue();
6248
6249 EVT VT = N->getValueType(0);
6250 if (VT != MVT::i32)
6251 return SDValue();
6252 if (!TLI.isOperationLegalOrCustom(ISD::BSWAP, VT))
6253 return SDValue();
6254
6255 if (SDValue BSwap = matchBSwapHWordOrAndAnd(TLI, DAG, N, N0, N1, VT,
6256 getShiftAmountTy(VT)))
6257 return BSwap;
6258
6259 // Try again with commuted operands.
6260 if (SDValue BSwap = matchBSwapHWordOrAndAnd(TLI, DAG, N, N1, N0, VT,
6261 getShiftAmountTy(VT)))
6262 return BSwap;
6263
6264
6265 // Look for either
6266 // (or (bswaphpair), (bswaphpair))
6267 // (or (or (bswaphpair), (and)), (and))
6268 // (or (or (and), (bswaphpair)), (and))
6269 SDNode *Parts[4] = {};
6270
6271 if (isBSwapHWordPair(N0, Parts)) {
6272 // (or (or (and), (and)), (or (and), (and)))
6273 if (!isBSwapHWordPair(N1, Parts))
6274 return SDValue();
6275 } else if (N0.getOpcode() == ISD::OR) {
6276 // (or (or (or (and), (and)), (and)), (and))
6277 if (!isBSwapHWordElement(N1, Parts))
6278 return SDValue();
6279 SDValue N00 = N0.getOperand(0);
6280 SDValue N01 = N0.getOperand(1);
6281 if (!(isBSwapHWordElement(N01, Parts) && isBSwapHWordPair(N00, Parts)) &&
6282 !(isBSwapHWordElement(N00, Parts) && isBSwapHWordPair(N01, Parts)))
6283 return SDValue();
6284 } else
6285 return SDValue();
6286
6287 // Make sure the parts are all coming from the same node.
6288 if (Parts[0] != Parts[1] || Parts[0] != Parts[2] || Parts[0] != Parts[3])
6289 return SDValue();
6290
6291 SDLoc DL(N);
6292 SDValue BSwap = DAG.getNode(ISD::BSWAP, DL, VT,
6293 SDValue(Parts[0], 0));
6294
6295 // Result of the bswap should be rotated by 16. If it's not legal, then
6296 // do (x << 16) | (x >> 16).
6297 SDValue ShAmt = DAG.getConstant(16, DL, getShiftAmountTy(VT));
6298 if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT))
6299 return DAG.getNode(ISD::ROTL, DL, VT, BSwap, ShAmt);
6300 if (TLI.isOperationLegalOrCustom(ISD::ROTR, VT))
6301 return DAG.getNode(ISD::ROTR, DL, VT, BSwap, ShAmt);
6302 return DAG.getNode(ISD::OR, DL, VT,
6303 DAG.getNode(ISD::SHL, DL, VT, BSwap, ShAmt),
6304 DAG.getNode(ISD::SRL, DL, VT, BSwap, ShAmt));
6305}
6306
6307/// This contains all DAGCombine rules which reduce two values combined by
6308/// an Or operation to a single value \see visitANDLike().
6309SDValue DAGCombiner::visitORLike(SDValue N0, SDValue N1, SDNode *N) {
6310 EVT VT = N1.getValueType();
6311 SDLoc DL(N);
6312
6313 // fold (or x, undef) -> -1
6314 if (!LegalOperations && (N0.isUndef() || N1.isUndef()))
6315 return DAG.getAllOnesConstant(DL, VT);
6316
6317 if (SDValue V = foldLogicOfSetCCs(false, N0, N1, DL))
6318 return V;
6319
6320 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
6321 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == ISD::AND &&
6322 // Don't increase # computations.
6323 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
6324 // We can only do this xform if we know that bits from X that are set in C2
6325 // but not in C1 are already zero. Likewise for Y.
6326 if (const ConstantSDNode *N0O1C =
6327 getAsNonOpaqueConstant(N0.getOperand(1))) {
6328 if (const ConstantSDNode *N1O1C =
6329 getAsNonOpaqueConstant(N1.getOperand(1))) {
6330 // We can only do this xform if we know that bits from X that are set in
6331 // C2 but not in C1 are already zero. Likewise for Y.
6332 const APInt &LHSMask = N0O1C->getAPIntValue();
6333 const APInt &RHSMask = N1O1C->getAPIntValue();
6334
6335 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
6336 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
6337 SDValue X = DAG.getNode(ISD::OR, SDLoc(N0), VT,
6338 N0.getOperand(0), N1.getOperand(0));
6339 return DAG.getNode(ISD::AND, DL, VT, X,
6340 DAG.getConstant(LHSMask | RHSMask, DL, VT));
6341 }
6342 }
6343 }
6344 }
6345
6346 // (or (and X, M), (and X, N)) -> (and X, (or M, N))
6347 if (N0.getOpcode() == ISD::AND &&
6348 N1.getOpcode() == ISD::AND &&
6349 N0.getOperand(0) == N1.getOperand(0) &&
6350 // Don't increase # computations.
6351 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
6352 SDValue X = DAG.getNode(ISD::OR, SDLoc(N0), VT,
6353 N0.getOperand(1), N1.getOperand(1));
6354 return DAG.getNode(ISD::AND, DL, VT, N0.getOperand(0), X);
6355 }
6356
6357 return SDValue();
6358}
6359
6360/// OR combines for which the commuted variant will be tried as well.
6361static SDValue visitORCommutative(
6362 SelectionDAG &DAG, SDValue N0, SDValue N1, SDNode *N) {
6363 EVT VT = N0.getValueType();
6364 if (N0.getOpcode() == ISD::AND) {
6365 // fold (or (and X, (xor Y, -1)), Y) -> (or X, Y)
6366 if (isBitwiseNot(N0.getOperand(1)) && N0.getOperand(1).getOperand(0) == N1)
6367 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0.getOperand(0), N1);
6368
6369 // fold (or (and (xor Y, -1), X), Y) -> (or X, Y)
6370 if (isBitwiseNot(N0.getOperand(0)) && N0.getOperand(0).getOperand(0) == N1)
6371 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0.getOperand(1), N1);
6372 }
6373
6374 return SDValue();
6375}
6376
6377SDValue DAGCombiner::visitOR(SDNode *N) {
6378 SDValue N0 = N->getOperand(0);
6379 SDValue N1 = N->getOperand(1);
6380 EVT VT = N1.getValueType();
6381
6382 // x | x --> x
6383 if (N0 == N1)
6384 return N0;
6385
6386 // fold vector ops
6387 if (VT.isVector()) {
6388 if (SDValue FoldedVOp = SimplifyVBinOp(N))
6389 return FoldedVOp;
6390
6391 // fold (or x, 0) -> x, vector edition
6392 if (ISD::isConstantSplatVectorAllZeros(N0.getNode()))
6393 return N1;
6394 if (ISD::isConstantSplatVectorAllZeros(N1.getNode()))
6395 return N0;
6396
6397 // fold (or x, -1) -> -1, vector edition
6398 if (ISD::isConstantSplatVectorAllOnes(N0.getNode()))
6399 // do not return N0, because undef node may exist in N0
6400 return DAG.getAllOnesConstant(SDLoc(N), N0.getValueType());
6401 if (ISD::isConstantSplatVectorAllOnes(N1.getNode()))
6402 // do not return N1, because undef node may exist in N1
6403 return DAG.getAllOnesConstant(SDLoc(N), N1.getValueType());
6404
6405 // fold (or (shuf A, V_0, MA), (shuf B, V_0, MB)) -> (shuf A, B, Mask)
6406 // Do this only if the resulting shuffle is legal.
6407 if (isa<ShuffleVectorSDNode>(N0) &&
6408 isa<ShuffleVectorSDNode>(N1) &&
6409 // Avoid folding a node with illegal type.
6410 TLI.isTypeLegal(VT)) {
6411 bool ZeroN00 = ISD::isBuildVectorAllZeros(N0.getOperand(0).getNode());
6412 bool ZeroN01 = ISD::isBuildVectorAllZeros(N0.getOperand(1).getNode());
6413 bool ZeroN10 = ISD::isBuildVectorAllZeros(N1.getOperand(0).getNode());
6414 bool ZeroN11 = ISD::isBuildVectorAllZeros(N1.getOperand(1).getNode());
6415 // Ensure both shuffles have a zero input.
6416 if ((ZeroN00 != ZeroN01) && (ZeroN10 != ZeroN11)) {
6417 assert((!ZeroN00 || !ZeroN01) && "Both inputs zero!")(static_cast <bool> ((!ZeroN00 || !ZeroN01) && "Both inputs zero!"
) ? void (0) : __assert_fail ("(!ZeroN00 || !ZeroN01) && \"Both inputs zero!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 6417, __extension__ __PRETTY_FUNCTION__))
;
6418 assert((!ZeroN10 || !ZeroN11) && "Both inputs zero!")(static_cast <bool> ((!ZeroN10 || !ZeroN11) && "Both inputs zero!"
) ? void (0) : __assert_fail ("(!ZeroN10 || !ZeroN11) && \"Both inputs zero!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 6418, __extension__ __PRETTY_FUNCTION__))
;
6419 const ShuffleVectorSDNode *SV0 = cast<ShuffleVectorSDNode>(N0);
6420 const ShuffleVectorSDNode *SV1 = cast<ShuffleVectorSDNode>(N1);
6421 bool CanFold = true;
6422 int NumElts = VT.getVectorNumElements();
6423 SmallVector<int, 4> Mask(NumElts);
6424
6425 for (int i = 0; i != NumElts; ++i) {
6426 int M0 = SV0->getMaskElt(i);
6427 int M1 = SV1->getMaskElt(i);
6428
6429 // Determine if either index is pointing to a zero vector.
6430 bool M0Zero = M0 < 0 || (ZeroN00 == (M0 < NumElts));
6431 bool M1Zero = M1 < 0 || (ZeroN10 == (M1 < NumElts));
6432
6433 // If one element is zero and the otherside is undef, keep undef.
6434 // This also handles the case that both are undef.
6435 if ((M0Zero && M1 < 0) || (M1Zero && M0 < 0)) {
6436 Mask[i] = -1;
6437 continue;
6438 }
6439
6440 // Make sure only one of the elements is zero.
6441 if (M0Zero == M1Zero) {
6442 CanFold = false;
6443 break;
6444 }
6445
6446 assert((M0 >= 0 || M1 >= 0) && "Undef index!")(static_cast <bool> ((M0 >= 0 || M1 >= 0) &&
"Undef index!") ? void (0) : __assert_fail ("(M0 >= 0 || M1 >= 0) && \"Undef index!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 6446, __extension__ __PRETTY_FUNCTION__))
;
6447
6448 // We have a zero and non-zero element. If the non-zero came from
6449 // SV0 make the index a LHS index. If it came from SV1, make it
6450 // a RHS index. We need to mod by NumElts because we don't care
6451 // which operand it came from in the original shuffles.
6452 Mask[i] = M1Zero ? M0 % NumElts : (M1 % NumElts) + NumElts;
6453 }
6454
6455 if (CanFold) {
6456 SDValue NewLHS = ZeroN00 ? N0.getOperand(1) : N0.getOperand(0);
6457 SDValue NewRHS = ZeroN10 ? N1.getOperand(1) : N1.getOperand(0);
6458
6459 SDValue LegalShuffle =
6460 TLI.buildLegalVectorShuffle(VT, SDLoc(N), NewLHS, NewRHS,
6461 Mask, DAG);
6462 if (LegalShuffle)
6463 return LegalShuffle;
6464 }
6465 }
6466 }
6467 }
6468
6469 // fold (or c1, c2) -> c1|c2
6470 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
6471 if (SDValue C = DAG.FoldConstantArithmetic(ISD::OR, SDLoc(N), VT, {N0, N1}))
6472 return C;
6473
6474 // canonicalize constant to RHS
6475 if (DAG.isConstantIntBuildVectorOrConstantInt(N0) &&
6476 !DAG.isConstantIntBuildVectorOrConstantInt(N1))
6477 return DAG.getNode(ISD::OR, SDLoc(N), VT, N1, N0);
6478
6479 // fold (or x, 0) -> x
6480 if (isNullConstant(N1))
6481 return N0;
6482
6483 // fold (or x, -1) -> -1
6484 if (isAllOnesConstant(N1))
6485 return N1;
6486
6487 if (SDValue NewSel = foldBinOpIntoSelect(N))
6488 return NewSel;
6489
6490 // fold (or x, c) -> c iff (x & ~c) == 0
6491 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
6492 return N1;
6493
6494 if (SDValue Combined = visitORLike(N0, N1, N))
6495 return Combined;
6496
6497 if (SDValue Combined = combineCarryDiamond(*this, DAG, TLI, N0, N1, N))
6498 return Combined;
6499
6500 // Recognize halfword bswaps as (bswap + rotl 16) or (bswap + shl 16)
6501 if (SDValue BSwap = MatchBSwapHWord(N, N0, N1))
6502 return BSwap;
6503 if (SDValue BSwap = MatchBSwapHWordLow(N, N0, N1))
6504 return BSwap;
6505
6506 // reassociate or
6507 if (SDValue ROR = reassociateOps(ISD::OR, SDLoc(N), N0, N1, N->getFlags()))
6508 return ROR;
6509
6510 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
6511 // iff (c1 & c2) != 0 or c1/c2 are undef.
6512 auto MatchIntersect = [](ConstantSDNode *C1, ConstantSDNode *C2) {
6513 return !C1 || !C2 || C1->getAPIntValue().intersects(C2->getAPIntValue());
6514 };
6515 if (N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
6516 ISD::matchBinaryPredicate(N0.getOperand(1), N1, MatchIntersect, true)) {
6517 if (SDValue COR = DAG.FoldConstantArithmetic(ISD::OR, SDLoc(N1), VT,
6518 {N1, N0.getOperand(1)})) {
6519 SDValue IOR = DAG.getNode(ISD::OR, SDLoc(N0), VT, N0.getOperand(0), N1);
6520 AddToWorklist(IOR.getNode());
6521 return DAG.getNode(ISD::AND, SDLoc(N), VT, COR, IOR);
6522 }
6523 }
6524
6525 if (SDValue Combined = visitORCommutative(DAG, N0, N1, N))
6526 return Combined;
6527 if (SDValue Combined = visitORCommutative(DAG, N1, N0, N))
6528 return Combined;
6529
6530 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
6531 if (N0.getOpcode() == N1.getOpcode())
6532 if (SDValue V = hoistLogicOpWithSameOpcodeHands(N))
6533 return V;
6534
6535 // See if this is some rotate idiom.
6536 if (SDValue Rot = MatchRotate(N0, N1, SDLoc(N)))
6537 return Rot;
6538
6539 if (SDValue Load = MatchLoadCombine(N))
6540 return Load;
6541
6542 // Simplify the operands using demanded-bits information.
6543 if (SimplifyDemandedBits(SDValue(N, 0)))
6544 return SDValue(N, 0);
6545
6546 // If OR can be rewritten into ADD, try combines based on ADD.
6547 if ((!LegalOperations || TLI.isOperationLegal(ISD::ADD, VT)) &&
6548 DAG.haveNoCommonBitsSet(N0, N1))
6549 if (SDValue Combined = visitADDLike(N))
6550 return Combined;
6551
6552 return SDValue();
6553}
6554
6555static SDValue stripConstantMask(SelectionDAG &DAG, SDValue Op, SDValue &Mask) {
6556 if (Op.getOpcode() == ISD::AND &&
6557 DAG.isConstantIntBuildVectorOrConstantInt(Op.getOperand(1))) {
6558 Mask = Op.getOperand(1);
6559 return Op.getOperand(0);
6560 }
6561 return Op;
6562}
6563
6564/// Match "(X shl/srl V1) & V2" where V2 may not be present.
6565static bool matchRotateHalf(SelectionDAG &DAG, SDValue Op, SDValue &Shift,
6566 SDValue &Mask) {
6567 Op = stripConstantMask(DAG, Op, Mask);
6568 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
6569 Shift = Op;
6570 return true;
6571 }
6572 return false;
6573}
6574
6575/// Helper function for visitOR to extract the needed side of a rotate idiom
6576/// from a shl/srl/mul/udiv. This is meant to handle cases where
6577/// InstCombine merged some outside op with one of the shifts from
6578/// the rotate pattern.
6579/// \returns An empty \c SDValue if the needed shift couldn't be extracted.
6580/// Otherwise, returns an expansion of \p ExtractFrom based on the following
6581/// patterns:
6582///
6583/// (or (add v v) (shrl v bitwidth-1)):
6584/// expands (add v v) -> (shl v 1)
6585///
6586/// (or (mul v c0) (shrl (mul v c1) c2)):
6587/// expands (mul v c0) -> (shl (mul v c1) c3)
6588///
6589/// (or (udiv v c0) (shl (udiv v c1) c2)):
6590/// expands (udiv v c0) -> (shrl (udiv v c1) c3)
6591///
6592/// (or (shl v c0) (shrl (shl v c1) c2)):
6593/// expands (shl v c0) -> (shl (shl v c1) c3)
6594///
6595/// (or (shrl v c0) (shl (shrl v c1) c2)):
6596/// expands (shrl v c0) -> (shrl (shrl v c1) c3)
6597///
6598/// Such that in all cases, c3+c2==bitwidth(op v c1).
6599static SDValue extractShiftForRotate(SelectionDAG &DAG, SDValue OppShift,
6600 SDValue ExtractFrom, SDValue &Mask,
6601 const SDLoc &DL) {
6602 assert(OppShift && ExtractFrom && "Empty SDValue")(static_cast <bool> (OppShift && ExtractFrom &&
"Empty SDValue") ? void (0) : __assert_fail ("OppShift && ExtractFrom && \"Empty SDValue\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 6602, __extension__ __PRETTY_FUNCTION__))
;
6603 assert((static_cast <bool> ((OppShift.getOpcode() == ISD::SHL ||
OppShift.getOpcode() == ISD::SRL) && "Existing shift must be valid as a rotate half"
) ? void (0) : __assert_fail ("(OppShift.getOpcode() == ISD::SHL || OppShift.getOpcode() == ISD::SRL) && \"Existing shift must be valid as a rotate half\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 6605, __extension__ __PRETTY_FUNCTION__))
6604 (OppShift.getOpcode() == ISD::SHL || OppShift.getOpcode() == ISD::SRL) &&(static_cast <bool> ((OppShift.getOpcode() == ISD::SHL ||
OppShift.getOpcode() == ISD::SRL) && "Existing shift must be valid as a rotate half"
) ? void (0) : __assert_fail ("(OppShift.getOpcode() == ISD::SHL || OppShift.getOpcode() == ISD::SRL) && \"Existing shift must be valid as a rotate half\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 6605, __extension__ __PRETTY_FUNCTION__))
6605 "Existing shift must be valid as a rotate half")(static_cast <bool> ((OppShift.getOpcode() == ISD::SHL ||
OppShift.getOpcode() == ISD::SRL) && "Existing shift must be valid as a rotate half"
) ? void (0) : __assert_fail ("(OppShift.getOpcode() == ISD::SHL || OppShift.getOpcode() == ISD::SRL) && \"Existing shift must be valid as a rotate half\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 6605, __extension__ __PRETTY_FUNCTION__))
;
6606
6607 ExtractFrom = stripConstantMask(DAG, ExtractFrom, Mask);
6608
6609 // Value and Type of the shift.
6610 SDValue OppShiftLHS = OppShift.getOperand(0);
6611 EVT ShiftedVT = OppShiftLHS.getValueType();
6612
6613 // Amount of the existing shift.
6614 ConstantSDNode *OppShiftCst = isConstOrConstSplat(OppShift.getOperand(1));
6615
6616 // (add v v) -> (shl v 1)
6617 // TODO: Should this be a general DAG canonicalization?
6618 if (OppShift.getOpcode() == ISD::SRL && OppShiftCst &&
6619 ExtractFrom.getOpcode() == ISD::ADD &&
6620 ExtractFrom.getOperand(0) == ExtractFrom.getOperand(1) &&
6621 ExtractFrom.getOperand(0) == OppShiftLHS &&
6622 OppShiftCst->getAPIntValue() == ShiftedVT.getScalarSizeInBits() - 1)
6623 return DAG.getNode(ISD::SHL, DL, ShiftedVT, OppShiftLHS,
6624 DAG.getShiftAmountConstant(1, ShiftedVT, DL));
6625
6626 // Preconditions:
6627 // (or (op0 v c0) (shiftl/r (op0 v c1) c2))
6628 //
6629 // Find opcode of the needed shift to be extracted from (op0 v c0).
6630 unsigned Opcode = ISD::DELETED_NODE;
6631 bool IsMulOrDiv = false;
6632 // Set Opcode and IsMulOrDiv if the extract opcode matches the needed shift
6633 // opcode or its arithmetic (mul or udiv) variant.
6634 auto SelectOpcode = [&](unsigned NeededShift, unsigned MulOrDivVariant) {
6635 IsMulOrDiv = ExtractFrom.getOpcode() == MulOrDivVariant;
6636 if (!IsMulOrDiv && ExtractFrom.getOpcode() != NeededShift)
6637 return false;
6638 Opcode = NeededShift;
6639 return true;
6640 };
6641 // op0 must be either the needed shift opcode or the mul/udiv equivalent
6642 // that the needed shift can be extracted from.
6643 if ((OppShift.getOpcode() != ISD::SRL || !SelectOpcode(ISD::SHL, ISD::MUL)) &&
6644 (OppShift.getOpcode() != ISD::SHL || !SelectOpcode(ISD::SRL, ISD::UDIV)))
6645 return SDValue();
6646
6647 // op0 must be the same opcode on both sides, have the same LHS argument,
6648 // and produce the same value type.
6649 if (OppShiftLHS.getOpcode() != ExtractFrom.getOpcode() ||
6650 OppShiftLHS.getOperand(0) != ExtractFrom.getOperand(0) ||
6651 ShiftedVT != ExtractFrom.getValueType())
6652 return SDValue();
6653
6654 // Constant mul/udiv/shift amount from the RHS of the shift's LHS op.
6655 ConstantSDNode *OppLHSCst = isConstOrConstSplat(OppShiftLHS.getOperand(1));
6656 // Constant mul/udiv/shift amount from the RHS of the ExtractFrom op.
6657 ConstantSDNode *ExtractFromCst =
6658 isConstOrConstSplat(ExtractFrom.getOperand(1));
6659 // TODO: We should be able to handle non-uniform constant vectors for these values
6660 // Check that we have constant values.
6661 if (!OppShiftCst || !OppShiftCst->getAPIntValue() ||
6662 !OppLHSCst || !OppLHSCst->getAPIntValue() ||
6663 !ExtractFromCst || !ExtractFromCst->getAPIntValue())
6664 return SDValue();
6665
6666 // Compute the shift amount we need to extract to complete the rotate.
6667 const unsigned VTWidth = ShiftedVT.getScalarSizeInBits();
6668 if (OppShiftCst->getAPIntValue().ugt(VTWidth))
6669 return SDValue();
6670 APInt NeededShiftAmt = VTWidth - OppShiftCst->getAPIntValue();
6671 // Normalize the bitwidth of the two mul/udiv/shift constant operands.
6672 APInt ExtractFromAmt = ExtractFromCst->getAPIntValue();
6673 APInt OppLHSAmt = OppLHSCst->getAPIntValue();
6674 zeroExtendToMatch(ExtractFromAmt, OppLHSAmt);
6675
6676 // Now try extract the needed shift from the ExtractFrom op and see if the
6677 // result matches up with the existing shift's LHS op.
6678 if (IsMulOrDiv) {
6679 // Op to extract from is a mul or udiv by a constant.
6680 // Check:
6681 // c2 / (1 << (bitwidth(op0 v c0) - c1)) == c0
6682 // c2 % (1 << (bitwidth(op0 v c0) - c1)) == 0
6683 const APInt ExtractDiv = APInt::getOneBitSet(ExtractFromAmt.getBitWidth(),
6684 NeededShiftAmt.getZExtValue());
6685 APInt ResultAmt;
6686 APInt Rem;
6687 APInt::udivrem(ExtractFromAmt, ExtractDiv, ResultAmt, Rem);
6688 if (Rem != 0 || ResultAmt != OppLHSAmt)
6689 return SDValue();
6690 } else {
6691 // Op to extract from is a shift by a constant.
6692 // Check:
6693 // c2 - (bitwidth(op0 v c0) - c1) == c0
6694 if (OppLHSAmt != ExtractFromAmt - NeededShiftAmt.zextOrTrunc(
6695 ExtractFromAmt.getBitWidth()))
6696 return SDValue();
6697 }
6698
6699 // Return the expanded shift op that should allow a rotate to be formed.
6700 EVT ShiftVT = OppShift.getOperand(1).getValueType();
6701 EVT ResVT = ExtractFrom.getValueType();
6702 SDValue NewShiftNode = DAG.getConstant(NeededShiftAmt, DL, ShiftVT);
6703 return DAG.getNode(Opcode, DL, ResVT, OppShiftLHS, NewShiftNode);
6704}
6705
6706// Return true if we can prove that, whenever Neg and Pos are both in the
6707// range [0, EltSize), Neg == (Pos == 0 ? 0 : EltSize - Pos). This means that
6708// for two opposing shifts shift1 and shift2 and a value X with OpBits bits:
6709//
6710// (or (shift1 X, Neg), (shift2 X, Pos))
6711//
6712// reduces to a rotate in direction shift2 by Pos or (equivalently) a rotate
6713// in direction shift1 by Neg. The range [0, EltSize) means that we only need
6714// to consider shift amounts with defined behavior.
6715//
6716// The IsRotate flag should be set when the LHS of both shifts is the same.
6717// Otherwise if matching a general funnel shift, it should be clear.
6718static bool matchRotateSub(SDValue Pos, SDValue Neg, unsigned EltSize,
6719 SelectionDAG &DAG, bool IsRotate) {
6720 // If EltSize is a power of 2 then:
6721 //
6722 // (a) (Pos == 0 ? 0 : EltSize - Pos) == (EltSize - Pos) & (EltSize - 1)
6723 // (b) Neg == Neg & (EltSize - 1) whenever Neg is in [0, EltSize).
6724 //
6725 // So if EltSize is a power of 2 and Neg is (and Neg', EltSize-1), we check
6726 // for the stronger condition:
6727 //
6728 // Neg & (EltSize - 1) == (EltSize - Pos) & (EltSize - 1) [A]
6729 //
6730 // for all Neg and Pos. Since Neg & (EltSize - 1) == Neg' & (EltSize - 1)
6731 // we can just replace Neg with Neg' for the rest of the function.
6732 //
6733 // In other cases we check for the even stronger condition:
6734 //
6735 // Neg == EltSize - Pos [B]
6736 //
6737 // for all Neg and Pos. Note that the (or ...) then invokes undefined
6738 // behavior if Pos == 0 (and consequently Neg == EltSize).
6739 //
6740 // We could actually use [A] whenever EltSize is a power of 2, but the
6741 // only extra cases that it would match are those uninteresting ones
6742 // where Neg and Pos are never in range at the same time. E.g. for
6743 // EltSize == 32, using [A] would allow a Neg of the form (sub 64, Pos)
6744 // as well as (sub 32, Pos), but:
6745 //
6746 // (or (shift1 X, (sub 64, Pos)), (shift2 X, Pos))
6747 //
6748 // always invokes undefined behavior for 32-bit X.
6749 //
6750 // Below, Mask == EltSize - 1 when using [A] and is all-ones otherwise.
6751 //
6752 // NOTE: We can only do this when matching an AND and not a general
6753 // funnel shift.
6754 unsigned MaskLoBits = 0;
6755 if (IsRotate && Neg.getOpcode() == ISD::AND && isPowerOf2_64(EltSize)) {
6756 if (ConstantSDNode *NegC = isConstOrConstSplat(Neg.getOperand(1))) {
6757 KnownBits Known = DAG.computeKnownBits(Neg.getOperand(0));
6758 unsigned Bits = Log2_64(EltSize);
6759 if (NegC->getAPIntValue().getActiveBits() <= Bits &&
6760 ((NegC->getAPIntValue() | Known.Zero).countTrailingOnes() >= Bits)) {
6761 Neg = Neg.getOperand(0);
6762 MaskLoBits = Bits;
6763 }
6764 }
6765 }
6766
6767 // Check whether Neg has the form (sub NegC, NegOp1) for some NegC and NegOp1.
6768 if (Neg.getOpcode() != ISD::SUB)
6769 return false;
6770 ConstantSDNode *NegC = isConstOrConstSplat(Neg.getOperand(0));
6771 if (!NegC)
6772 return false;
6773 SDValue NegOp1 = Neg.getOperand(1);
6774
6775 // On the RHS of [A], if Pos is Pos' & (EltSize - 1), just replace Pos with
6776 // Pos'. The truncation is redundant for the purpose of the equality.
6777 if (MaskLoBits && Pos.getOpcode() == ISD::AND) {
6778 if (ConstantSDNode *PosC = isConstOrConstSplat(Pos.getOperand(1))) {
6779 KnownBits Known = DAG.computeKnownBits(Pos.getOperand(0));
6780 if (PosC->getAPIntValue().getActiveBits() <= MaskLoBits &&
6781 ((PosC->getAPIntValue() | Known.Zero).countTrailingOnes() >=
6782 MaskLoBits))
6783 Pos = Pos.getOperand(0);
6784 }
6785 }
6786
6787 // The condition we need is now:
6788 //
6789 // (NegC - NegOp1) & Mask == (EltSize - Pos) & Mask
6790 //
6791 // If NegOp1 == Pos then we need:
6792 //
6793 // EltSize & Mask == NegC & Mask
6794 //
6795 // (because "x & Mask" is a truncation and distributes through subtraction).
6796 //
6797 // We also need to account for a potential truncation of NegOp1 if the amount
6798 // has already been legalized to a shift amount type.
6799 APInt Width;
6800 if ((Pos == NegOp1) ||
6801 (NegOp1.getOpcode() == ISD::TRUNCATE && Pos == NegOp1.getOperand(0)))
6802 Width = NegC->getAPIntValue();
6803
6804 // Check for cases where Pos has the form (add NegOp1, PosC) for some PosC.
6805 // Then the condition we want to prove becomes:
6806 //
6807 // (NegC - NegOp1) & Mask == (EltSize - (NegOp1 + PosC)) & Mask
6808 //
6809 // which, again because "x & Mask" is a truncation, becomes:
6810 //
6811 // NegC & Mask == (EltSize - PosC) & Mask
6812 // EltSize & Mask == (NegC + PosC) & Mask
6813 else if (Pos.getOpcode() == ISD::ADD && Pos.getOperand(0) == NegOp1) {
6814 if (ConstantSDNode *PosC = isConstOrConstSplat(Pos.getOperand(1)))
6815 Width = PosC->getAPIntValue() + NegC->getAPIntValue();
6816 else
6817 return false;
6818 } else
6819 return false;
6820
6821 // Now we just need to check that EltSize & Mask == Width & Mask.
6822 if (MaskLoBits)
6823 // EltSize & Mask is 0 since Mask is EltSize - 1.
6824 return Width.getLoBits(MaskLoBits) == 0;
6825 return Width == EltSize;
6826}
6827
6828// A subroutine of MatchRotate used once we have found an OR of two opposite
6829// shifts of Shifted. If Neg == <operand size> - Pos then the OR reduces
6830// to both (PosOpcode Shifted, Pos) and (NegOpcode Shifted, Neg), with the
6831// former being preferred if supported. InnerPos and InnerNeg are Pos and
6832// Neg with outer conversions stripped away.
6833SDValue DAGCombiner::MatchRotatePosNeg(SDValue Shifted, SDValue Pos,
6834 SDValue Neg, SDValue InnerPos,
6835 SDValue InnerNeg, unsigned PosOpcode,
6836 unsigned NegOpcode, const SDLoc &DL) {
6837 // fold (or (shl x, (*ext y)),
6838 // (srl x, (*ext (sub 32, y)))) ->
6839 // (rotl x, y) or (rotr x, (sub 32, y))
6840 //
6841 // fold (or (shl x, (*ext (sub 32, y))),
6842 // (srl x, (*ext y))) ->
6843 // (rotr x, y) or (rotl x, (sub 32, y))
6844 EVT VT = Shifted.getValueType();
6845 if (matchRotateSub(InnerPos, InnerNeg, VT.getScalarSizeInBits(), DAG,
6846 /*IsRotate*/ true)) {
6847 bool HasPos = TLI.isOperationLegalOrCustom(PosOpcode, VT);
6848 return DAG.getNode(HasPos ? PosOpcode : NegOpcode, DL, VT, Shifted,
6849 HasPos ? Pos : Neg);
6850 }
6851
6852 return SDValue();
6853}
6854
6855// A subroutine of MatchRotate used once we have found an OR of two opposite
6856// shifts of N0 + N1. If Neg == <operand size> - Pos then the OR reduces
6857// to both (PosOpcode N0, N1, Pos) and (NegOpcode N0, N1, Neg), with the
6858// former being preferred if supported. InnerPos and InnerNeg are Pos and
6859// Neg with outer conversions stripped away.
6860// TODO: Merge with MatchRotatePosNeg.
6861SDValue DAGCombiner::MatchFunnelPosNeg(SDValue N0, SDValue N1, SDValue Pos,
6862 SDValue Neg, SDValue InnerPos,
6863 SDValue InnerNeg, unsigned PosOpcode,
6864 unsigned NegOpcode, const SDLoc &DL) {
6865 EVT VT = N0.getValueType();
6866 unsigned EltBits = VT.getScalarSizeInBits();
6867
6868 // fold (or (shl x0, (*ext y)),
6869 // (srl x1, (*ext (sub 32, y)))) ->
6870 // (fshl x0, x1, y) or (fshr x0, x1, (sub 32, y))
6871 //
6872 // fold (or (shl x0, (*ext (sub 32, y))),
6873 // (srl x1, (*ext y))) ->
6874 // (fshr x0, x1, y) or (fshl x0, x1, (sub 32, y))
6875 if (matchRotateSub(InnerPos, InnerNeg, EltBits, DAG, /*IsRotate*/ N0 == N1)) {
6876 bool HasPos = TLI.isOperationLegalOrCustom(PosOpcode, VT);
6877 return DAG.getNode(HasPos ? PosOpcode : NegOpcode, DL, VT, N0, N1,
6878 HasPos ? Pos : Neg);
6879 }
6880
6881 // Matching the shift+xor cases, we can't easily use the xor'd shift amount
6882 // so for now just use the PosOpcode case if its legal.
6883 // TODO: When can we use the NegOpcode case?
6884 if (PosOpcode == ISD::FSHL && isPowerOf2_32(EltBits)) {
6885 auto IsBinOpImm = [](SDValue Op, unsigned BinOpc, unsigned Imm) {
6886 if (Op.getOpcode() != BinOpc)
6887 return false;
6888 ConstantSDNode *Cst = isConstOrConstSplat(Op.getOperand(1));
6889 return Cst && (Cst->getAPIntValue() == Imm);
6890 };
6891
6892 // fold (or (shl x0, y), (srl (srl x1, 1), (xor y, 31)))
6893 // -> (fshl x0, x1, y)
6894 if (IsBinOpImm(N1, ISD::SRL, 1) &&
6895 IsBinOpImm(InnerNeg, ISD::XOR, EltBits - 1) &&
6896 InnerPos == InnerNeg.getOperand(0) &&
6897 TLI.isOperationLegalOrCustom(ISD::FSHL, VT)) {
6898 return DAG.getNode(ISD::FSHL, DL, VT, N0, N1.getOperand(0), Pos);
6899 }
6900
6901 // fold (or (shl (shl x0, 1), (xor y, 31)), (srl x1, y))
6902 // -> (fshr x0, x1, y)
6903 if (IsBinOpImm(N0, ISD::SHL, 1) &&
6904 IsBinOpImm(InnerPos, ISD::XOR, EltBits - 1) &&
6905 InnerNeg == InnerPos.getOperand(0) &&
6906 TLI.isOperationLegalOrCustom(ISD::FSHR, VT)) {
6907 return DAG.getNode(ISD::FSHR, DL, VT, N0.getOperand(0), N1, Neg);
6908 }
6909
6910 // fold (or (shl (add x0, x0), (xor y, 31)), (srl x1, y))
6911 // -> (fshr x0, x1, y)
6912 // TODO: Should add(x,x) -> shl(x,1) be a general DAG canonicalization?
6913 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N0.getOperand(1) &&
6914 IsBinOpImm(InnerPos, ISD::XOR, EltBits - 1) &&
6915 InnerNeg == InnerPos.getOperand(0) &&
6916 TLI.isOperationLegalOrCustom(ISD::FSHR, VT)) {
6917 return DAG.getNode(ISD::FSHR, DL, VT, N0.getOperand(0), N1, Neg);
6918 }
6919 }
6920
6921 return SDValue();
6922}
6923
6924// MatchRotate - Handle an 'or' of two operands. If this is one of the many
6925// idioms for rotate, and if the target supports rotation instructions, generate
6926// a rot[lr]. This also matches funnel shift patterns, similar to rotation but
6927// with different shifted sources.
6928SDValue DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, const SDLoc &DL) {
6929 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
6930 EVT VT = LHS.getValueType();
6931 if (!TLI.isTypeLegal(VT))
6932 return SDValue();
6933
6934 // The target must have at least one rotate/funnel flavor.
6935 bool HasROTL = hasOperation(ISD::ROTL, VT);
6936 bool HasROTR = hasOperation(ISD::ROTR, VT);
6937 bool HasFSHL = hasOperation(ISD::FSHL, VT);
6938 bool HasFSHR = hasOperation(ISD::FSHR, VT);
6939 if (!HasROTL && !HasROTR && !HasFSHL && !HasFSHR)
6940 return SDValue();
6941
6942 // Check for truncated rotate.
6943 if (LHS.getOpcode() == ISD::TRUNCATE && RHS.getOpcode() == ISD::TRUNCATE &&
6944 LHS.getOperand(0).getValueType() == RHS.getOperand(0).getValueType()) {
6945 assert(LHS.getValueType() == RHS.getValueType())(static_cast <bool> (LHS.getValueType() == RHS.getValueType
()) ? void (0) : __assert_fail ("LHS.getValueType() == RHS.getValueType()"
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 6945, __extension__ __PRETTY_FUNCTION__))
;
6946 if (SDValue Rot = MatchRotate(LHS.getOperand(0), RHS.getOperand(0), DL)) {
6947 return DAG.getNode(ISD::TRUNCATE, SDLoc(LHS), LHS.getValueType(), Rot);
6948 }
6949 }
6950
6951 // Match "(X shl/srl V1) & V2" where V2 may not be present.
6952 SDValue LHSShift; // The shift.
6953 SDValue LHSMask; // AND value if any.
6954 matchRotateHalf(DAG, LHS, LHSShift, LHSMask);
6955
6956 SDValue RHSShift; // The shift.
6957 SDValue RHSMask; // AND value if any.
6958 matchRotateHalf(DAG, RHS, RHSShift, RHSMask);
6959
6960 // If neither side matched a rotate half, bail
6961 if (!LHSShift && !RHSShift)
6962 return SDValue();
6963
6964 // InstCombine may have combined a constant shl, srl, mul, or udiv with one
6965 // side of the rotate, so try to handle that here. In all cases we need to
6966 // pass the matched shift from the opposite side to compute the opcode and
6967 // needed shift amount to extract. We still want to do this if both sides
6968 // matched a rotate half because one half may be a potential overshift that
6969 // can be broken down (ie if InstCombine merged two shl or srl ops into a
6970 // single one).
6971
6972 // Have LHS side of the rotate, try to extract the needed shift from the RHS.
6973 if (LHSShift)
6974 if (SDValue NewRHSShift =
6975 extractShiftForRotate(DAG, LHSShift, RHS, RHSMask, DL))
6976 RHSShift = NewRHSShift;
6977 // Have RHS side of the rotate, try to extract the needed shift from the LHS.
6978 if (RHSShift)
6979 if (SDValue NewLHSShift =
6980 extractShiftForRotate(DAG, RHSShift, LHS, LHSMask, DL))
6981 LHSShift = NewLHSShift;
6982
6983 // If a side is still missing, nothing else we can do.
6984 if (!RHSShift || !LHSShift)
6985 return SDValue();
6986
6987 // At this point we've matched or extracted a shift op on each side.
6988
6989 if (LHSShift.getOpcode() == RHSShift.getOpcode())
6990 return SDValue(); // Shifts must disagree.
6991
6992 bool IsRotate = LHSShift.getOperand(0) == RHSShift.getOperand(0);
6993 if (!IsRotate && !(HasFSHL || HasFSHR))
6994 return SDValue(); // Requires funnel shift support.
6995
6996 // Canonicalize shl to left side in a shl/srl pair.
6997 if (RHSShift.getOpcode() == ISD::SHL) {
6998 std::swap(LHS, RHS);
6999 std::swap(LHSShift, RHSShift);
7000 std::swap(LHSMask, RHSMask);
7001 }
7002
7003 unsigned EltSizeInBits = VT.getScalarSizeInBits();
7004 SDValue LHSShiftArg = LHSShift.getOperand(0);
7005 SDValue LHSShiftAmt = LHSShift.getOperand(1);
7006 SDValue RHSShiftArg = RHSShift.getOperand(0);
7007 SDValue RHSShiftAmt = RHSShift.getOperand(1);
7008
7009 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
7010 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
7011 // fold (or (shl x, C1), (srl y, C2)) -> (fshl x, y, C1)
7012 // fold (or (shl x, C1), (srl y, C2)) -> (fshr x, y, C2)
7013 // iff C1+C2 == EltSizeInBits
7014 auto MatchRotateSum = [EltSizeInBits](ConstantSDNode *LHS,
7015 ConstantSDNode *RHS) {
7016 return (LHS->getAPIntValue() + RHS->getAPIntValue()) == EltSizeInBits;
7017 };
7018 if (ISD::matchBinaryPredicate(LHSShiftAmt, RHSShiftAmt, MatchRotateSum)) {
7019 SDValue Res;
7020 if (IsRotate && (HasROTL || HasROTR))
7021 Res = DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT, LHSShiftArg,
7022 HasROTL ? LHSShiftAmt : RHSShiftAmt);
7023 else
7024 Res = DAG.getNode(HasFSHL ? ISD::FSHL : ISD::FSHR, DL, VT, LHSShiftArg,
7025 RHSShiftArg, HasFSHL ? LHSShiftAmt : RHSShiftAmt);
7026
7027 // If there is an AND of either shifted operand, apply it to the result.
7028 if (LHSMask.getNode() || RHSMask.getNode()) {
7029 SDValue AllOnes = DAG.getAllOnesConstant(DL, VT);
7030 SDValue Mask = AllOnes;
7031
7032 if (LHSMask.getNode()) {
7033 SDValue RHSBits = DAG.getNode(ISD::SRL, DL, VT, AllOnes, RHSShiftAmt);
7034 Mask = DAG.getNode(ISD::AND, DL, VT, Mask,
7035 DAG.getNode(ISD::OR, DL, VT, LHSMask, RHSBits));
7036 }
7037 if (RHSMask.getNode()) {
7038 SDValue LHSBits = DAG.getNode(ISD::SHL, DL, VT, AllOnes, LHSShiftAmt);
7039 Mask = DAG.getNode(ISD::AND, DL, VT, Mask,
7040 DAG.getNode(ISD::OR, DL, VT, RHSMask, LHSBits));
7041 }
7042
7043 Res = DAG.getNode(ISD::AND, DL, VT, Res, Mask);
7044 }
7045
7046 return Res;
7047 }
7048
7049 // If there is a mask here, and we have a variable shift, we can't be sure
7050 // that we're masking out the right stuff.
7051 if (LHSMask.getNode() || RHSMask.getNode())
7052 return SDValue();
7053
7054 // If the shift amount is sign/zext/any-extended just peel it off.
7055 SDValue LExtOp0 = LHSShiftAmt;
7056 SDValue RExtOp0 = RHSShiftAmt;
7057 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
7058 LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
7059 LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
7060 LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
7061 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
7062 RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
7063 RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
7064 RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
7065 LExtOp0 = LHSShiftAmt.getOperand(0);
7066 RExtOp0 = RHSShiftAmt.getOperand(0);
7067 }
7068
7069 if (IsRotate && (HasROTL || HasROTR)) {
7070 SDValue TryL =
7071 MatchRotatePosNeg(LHSShiftArg, LHSShiftAmt, RHSShiftAmt, LExtOp0,
7072 RExtOp0, ISD::ROTL, ISD::ROTR, DL);
7073 if (TryL)
7074 return TryL;
7075
7076 SDValue TryR =
7077 MatchRotatePosNeg(RHSShiftArg, RHSShiftAmt, LHSShiftAmt, RExtOp0,
7078 LExtOp0, ISD::ROTR, ISD::ROTL, DL);
7079 if (TryR)
7080 return TryR;
7081 }
7082
7083 SDValue TryL =
7084 MatchFunnelPosNeg(LHSShiftArg, RHSShiftArg, LHSShiftAmt, RHSShiftAmt,
7085 LExtOp0, RExtOp0, ISD::FSHL, ISD::FSHR, DL);
7086 if (TryL)
7087 return TryL;
7088
7089 SDValue TryR =
7090 MatchFunnelPosNeg(LHSShiftArg, RHSShiftArg, RHSShiftAmt, LHSShiftAmt,
7091 RExtOp0, LExtOp0, ISD::FSHR, ISD::FSHL, DL);
7092 if (TryR)
7093 return TryR;
7094
7095 return SDValue();
7096}
7097
7098namespace {
7099
7100/// Represents known origin of an individual byte in load combine pattern. The
7101/// value of the byte is either constant zero or comes from memory.
7102struct ByteProvider {
7103 // For constant zero providers Load is set to nullptr. For memory providers
7104 // Load represents the node which loads the byte from memory.
7105 // ByteOffset is the offset of the byte in the value produced by the load.
7106 LoadSDNode *Load = nullptr;
7107 unsigned ByteOffset = 0;
7108
7109 ByteProvider() = default;
7110
7111 static ByteProvider getMemory(LoadSDNode *Load, unsigned ByteOffset) {
7112 return ByteProvider(Load, ByteOffset);
7113 }
7114
7115 static ByteProvider getConstantZero() { return ByteProvider(nullptr, 0); }
7116
7117 bool isConstantZero() const { return !Load; }
7118 bool isMemory() const { return Load; }
7119
7120 bool operator==(const ByteProvider &Other) const {
7121 return Other.Load == Load && Other.ByteOffset == ByteOffset;
7122 }
7123
7124private:
7125 ByteProvider(LoadSDNode *Load, unsigned ByteOffset)
7126 : Load(Load), ByteOffset(ByteOffset) {}
7127};
7128
7129} // end anonymous namespace
7130
7131/// Recursively traverses the expression calculating the origin of the requested
7132/// byte of the given value. Returns None if the provider can't be calculated.
7133///
7134/// For all the values except the root of the expression verifies that the value
7135/// has exactly one use and if it's not true return None. This way if the origin
7136/// of the byte is returned it's guaranteed that the values which contribute to
7137/// the byte are not used outside of this expression.
7138///
7139/// Because the parts of the expression are not allowed to have more than one
7140/// use this function iterates over trees, not DAGs. So it never visits the same
7141/// node more than once.
7142static const Optional<ByteProvider>
7143calculateByteProvider(SDValue Op, unsigned Index, unsigned Depth,
7144 bool Root = false) {
7145 // Typical i64 by i8 pattern requires recursion up to 8 calls depth
7146 if (Depth == 10)
7147 return None;
7148
7149 if (!Root && !Op.hasOneUse())
7150 return None;
7151
7152 assert(Op.getValueType().isScalarInteger() && "can't handle other types")(static_cast <bool> (Op.getValueType().isScalarInteger(
) && "can't handle other types") ? void (0) : __assert_fail
("Op.getValueType().isScalarInteger() && \"can't handle other types\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7152, __extension__ __PRETTY_FUNCTION__))
;
7153 unsigned BitWidth = Op.getValueSizeInBits();
7154 if (BitWidth % 8 != 0)
7155 return None;
7156 unsigned ByteWidth = BitWidth / 8;
7157 assert(Index < ByteWidth && "invalid index requested")(static_cast <bool> (Index < ByteWidth && "invalid index requested"
) ? void (0) : __assert_fail ("Index < ByteWidth && \"invalid index requested\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7157, __extension__ __PRETTY_FUNCTION__))
;
7158 (void) ByteWidth;
7159
7160 switch (Op.getOpcode()) {
7161 case ISD::OR: {
7162 auto LHS = calculateByteProvider(Op->getOperand(0), Index, Depth + 1);
7163 if (!LHS)
7164 return None;
7165 auto RHS = calculateByteProvider(Op->getOperand(1), Index, Depth + 1);
7166 if (!RHS)
7167 return None;
7168
7169 if (LHS->isConstantZero())
7170 return RHS;
7171 if (RHS->isConstantZero())
7172 return LHS;
7173 return None;
7174 }
7175 case ISD::SHL: {
7176 auto ShiftOp = dyn_cast<ConstantSDNode>(Op->getOperand(1));
7177 if (!ShiftOp)
7178 return None;
7179
7180 uint64_t BitShift = ShiftOp->getZExtValue();
7181 if (BitShift % 8 != 0)
7182 return None;
7183 uint64_t ByteShift = BitShift / 8;
7184
7185 return Index < ByteShift
7186 ? ByteProvider::getConstantZero()
7187 : calculateByteProvider(Op->getOperand(0), Index - ByteShift,
7188 Depth + 1);
7189 }
7190 case ISD::ANY_EXTEND:
7191 case ISD::SIGN_EXTEND:
7192 case ISD::ZERO_EXTEND: {
7193 SDValue NarrowOp = Op->getOperand(0);
7194 unsigned NarrowBitWidth = NarrowOp.getScalarValueSizeInBits();
7195 if (NarrowBitWidth % 8 != 0)
7196 return None;
7197 uint64_t NarrowByteWidth = NarrowBitWidth / 8;
7198
7199 if (Index >= NarrowByteWidth)
7200 return Op.getOpcode() == ISD::ZERO_EXTEND
7201 ? Optional<ByteProvider>(ByteProvider::getConstantZero())
7202 : None;
7203 return calculateByteProvider(NarrowOp, Index, Depth + 1);
7204 }
7205 case ISD::BSWAP:
7206 return calculateByteProvider(Op->getOperand(0), ByteWidth - Index - 1,
7207 Depth + 1);
7208 case ISD::LOAD: {
7209 auto L = cast<LoadSDNode>(Op.getNode());
7210 if (!L->isSimple() || L->isIndexed())
7211 return None;
7212
7213 unsigned NarrowBitWidth = L->getMemoryVT().getSizeInBits();
7214 if (NarrowBitWidth % 8 != 0)
7215 return None;
7216 uint64_t NarrowByteWidth = NarrowBitWidth / 8;
7217
7218 if (Index >= NarrowByteWidth)
7219 return L->getExtensionType() == ISD::ZEXTLOAD
7220 ? Optional<ByteProvider>(ByteProvider::getConstantZero())
7221 : None;
7222 return ByteProvider::getMemory(L, Index);
7223 }
7224 }
7225
7226 return None;
7227}
7228
7229static unsigned littleEndianByteAt(unsigned BW, unsigned i) {
7230 return i;
7231}
7232
7233static unsigned bigEndianByteAt(unsigned BW, unsigned i) {
7234 return BW - i - 1;
7235}
7236
7237// Check if the bytes offsets we are looking at match with either big or
7238// little endian value loaded. Return true for big endian, false for little
7239// endian, and None if match failed.
7240static Optional<bool> isBigEndian(const ArrayRef<int64_t> ByteOffsets,
7241 int64_t FirstOffset) {
7242 // The endian can be decided only when it is 2 bytes at least.
7243 unsigned Width = ByteOffsets.size();
7244 if (Width < 2)
7245 return None;
7246
7247 bool BigEndian = true, LittleEndian = true;
7248 for (unsigned i = 0; i < Width; i++) {
7249 int64_t CurrentByteOffset = ByteOffsets[i] - FirstOffset;
7250 LittleEndian &= CurrentByteOffset == littleEndianByteAt(Width, i);
7251 BigEndian &= CurrentByteOffset == bigEndianByteAt(Width, i);
7252 if (!BigEndian && !LittleEndian)
7253 return None;
7254 }
7255
7256 assert((BigEndian != LittleEndian) && "It should be either big endian or"(static_cast <bool> ((BigEndian != LittleEndian) &&
"It should be either big endian or" "little endian") ? void (
0) : __assert_fail ("(BigEndian != LittleEndian) && \"It should be either big endian or\" \"little endian\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7257, __extension__ __PRETTY_FUNCTION__))
7257 "little endian")(static_cast <bool> ((BigEndian != LittleEndian) &&
"It should be either big endian or" "little endian") ? void (
0) : __assert_fail ("(BigEndian != LittleEndian) && \"It should be either big endian or\" \"little endian\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7257, __extension__ __PRETTY_FUNCTION__))
;
7258 return BigEndian;
7259}
7260
7261static SDValue stripTruncAndExt(SDValue Value) {
7262 switch (Value.getOpcode()) {
7263 case ISD::TRUNCATE:
7264 case ISD::ZERO_EXTEND:
7265 case ISD::SIGN_EXTEND:
7266 case ISD::ANY_EXTEND:
7267 return stripTruncAndExt(Value.getOperand(0));
7268 }
7269 return Value;
7270}
7271
7272/// Match a pattern where a wide type scalar value is stored by several narrow
7273/// stores. Fold it into a single store or a BSWAP and a store if the targets
7274/// supports it.
7275///
7276/// Assuming little endian target:
7277/// i8 *p = ...
7278/// i32 val = ...
7279/// p[0] = (val >> 0) & 0xFF;
7280/// p[1] = (val >> 8) & 0xFF;
7281/// p[2] = (val >> 16) & 0xFF;
7282/// p[3] = (val >> 24) & 0xFF;
7283/// =>
7284/// *((i32)p) = val;
7285///
7286/// i8 *p = ...
7287/// i32 val = ...
7288/// p[0] = (val >> 24) & 0xFF;
7289/// p[1] = (val >> 16) & 0xFF;
7290/// p[2] = (val >> 8) & 0xFF;
7291/// p[3] = (val >> 0) & 0xFF;
7292/// =>
7293/// *((i32)p) = BSWAP(val);
7294SDValue DAGCombiner::mergeTruncStores(StoreSDNode *N) {
7295 // The matching looks for "store (trunc x)" patterns that appear early but are
7296 // likely to be replaced by truncating store nodes during combining.
7297 // TODO: If there is evidence that running this later would help, this
7298 // limitation could be removed. Legality checks may need to be added
7299 // for the created store and optional bswap/rotate.
7300 if (LegalOperations)
7301 return SDValue();
7302
7303 // We only handle merging simple stores of 1-4 bytes.
7304 // TODO: Allow unordered atomics when wider type is legal (see D66309)
7305 EVT MemVT = N->getMemoryVT();
7306 if (!(MemVT == MVT::i8 || MemVT == MVT::i16 || MemVT == MVT::i32) ||
7307 !N->isSimple() || N->isIndexed())
7308 return SDValue();
7309
7310 // Collect all of the stores in the chain.
7311 SDValue Chain = N->getChain();
7312 SmallVector<StoreSDNode *, 8> Stores = {N};
7313 while (auto *Store = dyn_cast<StoreSDNode>(Chain)) {
7314 // All stores must be the same size to ensure that we are writing all of the
7315 // bytes in the wide value.
7316 // TODO: We could allow multiple sizes by tracking each stored byte.
7317 if (Store->getMemoryVT() != MemVT || !Store->isSimple() ||
7318 Store->isIndexed())
7319 return SDValue();
7320 Stores.push_back(Store);
7321 Chain = Store->getChain();
7322 }
7323 // There is no reason to continue if we do not have at least a pair of stores.
7324 if (Stores.size() < 2)
7325 return SDValue();
7326
7327 // Handle simple types only.
7328 LLVMContext &Context = *DAG.getContext();
7329 unsigned NumStores = Stores.size();
7330 unsigned NarrowNumBits = N->getMemoryVT().getScalarSizeInBits();
7331 unsigned WideNumBits = NumStores * NarrowNumBits;
7332 EVT WideVT = EVT::getIntegerVT(Context, WideNumBits);
7333 if (WideVT != MVT::i16 && WideVT != MVT::i32 && WideVT != MVT::i64)
7334 return SDValue();
7335
7336 // Check if all bytes of the source value that we are looking at are stored
7337 // to the same base address. Collect offsets from Base address into OffsetMap.
7338 SDValue SourceValue;
7339 SmallVector<int64_t, 8> OffsetMap(NumStores, INT64_MAX(9223372036854775807L));
7340 int64_t FirstOffset = INT64_MAX(9223372036854775807L);
7341 StoreSDNode *FirstStore = nullptr;
7342 Optional<BaseIndexOffset> Base;
7343 for (auto Store : Stores) {
7344 // All the stores store different parts of the CombinedValue. A truncate is
7345 // required to get the partial value.
7346 SDValue Trunc = Store->getValue();
7347 if (Trunc.getOpcode() != ISD::TRUNCATE)
7348 return SDValue();
7349 // Other than the first/last part, a shift operation is required to get the
7350 // offset.
7351 int64_t Offset = 0;
7352 SDValue WideVal = Trunc.getOperand(0);
7353 if ((WideVal.getOpcode() == ISD::SRL || WideVal.getOpcode() == ISD::SRA) &&
7354 isa<ConstantSDNode>(WideVal.getOperand(1))) {
7355 // The shift amount must be a constant multiple of the narrow type.
7356 // It is translated to the offset address in the wide source value "y".
7357 //
7358 // x = srl y, ShiftAmtC
7359 // i8 z = trunc x
7360 // store z, ...
7361 uint64_t ShiftAmtC = WideVal.getConstantOperandVal(1);
7362 if (ShiftAmtC % NarrowNumBits != 0)
7363 return SDValue();
7364
7365 Offset = ShiftAmtC / NarrowNumBits;
7366 WideVal = WideVal.getOperand(0);
7367 }
7368
7369 // Stores must share the same source value with different offsets.
7370 // Truncate and extends should be stripped to get the single source value.
7371 if (!SourceValue)
7372 SourceValue = WideVal;
7373 else if (stripTruncAndExt(SourceValue) != stripTruncAndExt(WideVal))
7374 return SDValue();
7375 else if (SourceValue.getValueType() != WideVT) {
7376 if (WideVal.getValueType() == WideVT ||
7377 WideVal.getScalarValueSizeInBits() >
7378 SourceValue.getScalarValueSizeInBits())
7379 SourceValue = WideVal;
7380 // Give up if the source value type is smaller than the store size.
7381 if (SourceValue.getScalarValueSizeInBits() < WideVT.getScalarSizeInBits())
7382 return SDValue();
7383 }
7384
7385 // Stores must share the same base address.
7386 BaseIndexOffset Ptr = BaseIndexOffset::match(Store, DAG);
7387 int64_t ByteOffsetFromBase = 0;
7388 if (!Base)
7389 Base = Ptr;
7390 else if (!Base->equalBaseIndex(Ptr, DAG, ByteOffsetFromBase))
7391 return SDValue();
7392
7393 // Remember the first store.
7394 if (ByteOffsetFromBase < FirstOffset) {
7395 FirstStore = Store;
7396 FirstOffset = ByteOffsetFromBase;
7397 }
7398 // Map the offset in the store and the offset in the combined value, and
7399 // early return if it has been set before.
7400 if (Offset < 0 || Offset >= NumStores || OffsetMap[Offset] != INT64_MAX(9223372036854775807L))
7401 return SDValue();
7402 OffsetMap[Offset] = ByteOffsetFromBase;
7403 }
7404
7405 assert(FirstOffset != INT64_MAX && "First byte offset must be set")(static_cast <bool> (FirstOffset != (9223372036854775807L
) && "First byte offset must be set") ? void (0) : __assert_fail
("FirstOffset != INT64_MAX && \"First byte offset must be set\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7405, __extension__ __PRETTY_FUNCTION__))
;
7406 assert(FirstStore && "First store must be set")(static_cast <bool> (FirstStore && "First store must be set"
) ? void (0) : __assert_fail ("FirstStore && \"First store must be set\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7406, __extension__ __PRETTY_FUNCTION__))
;
7407
7408 // Check that a store of the wide type is both allowed and fast on the target
7409 const DataLayout &Layout = DAG.getDataLayout();
7410 bool Fast = false;
7411 bool Allowed = TLI.allowsMemoryAccess(Context, Layout, WideVT,
7412 *FirstStore->getMemOperand(), &Fast);
7413 if (!Allowed || !Fast)
7414 return SDValue();
7415
7416 // Check if the pieces of the value are going to the expected places in memory
7417 // to merge the stores.
7418 auto checkOffsets = [&](bool MatchLittleEndian) {
7419 if (MatchLittleEndian) {
7420 for (unsigned i = 0; i != NumStores; ++i)
7421 if (OffsetMap[i] != i * (NarrowNumBits / 8) + FirstOffset)
7422 return false;
7423 } else { // MatchBigEndian by reversing loop counter.
7424 for (unsigned i = 0, j = NumStores - 1; i != NumStores; ++i, --j)
7425 if (OffsetMap[j] != i * (NarrowNumBits / 8) + FirstOffset)
7426 return false;
7427 }
7428 return true;
7429 };
7430
7431 // Check if the offsets line up for the native data layout of this target.
7432 bool NeedBswap = false;
7433 bool NeedRotate = false;
7434 if (!checkOffsets(Layout.isLittleEndian())) {
7435 // Special-case: check if byte offsets line up for the opposite endian.
7436 if (NarrowNumBits == 8 && checkOffsets(Layout.isBigEndian()))
7437 NeedBswap = true;
7438 else if (NumStores == 2 && checkOffsets(Layout.isBigEndian()))
7439 NeedRotate = true;
7440 else
7441 return SDValue();
7442 }
7443
7444 SDLoc DL(N);
7445 if (WideVT != SourceValue.getValueType()) {
7446 assert(SourceValue.getValueType().getScalarSizeInBits() > WideNumBits &&(static_cast <bool> (SourceValue.getValueType().getScalarSizeInBits
() > WideNumBits && "Unexpected store value to merge"
) ? void (0) : __assert_fail ("SourceValue.getValueType().getScalarSizeInBits() > WideNumBits && \"Unexpected store value to merge\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7447, __extension__ __PRETTY_FUNCTION__))
7447 "Unexpected store value to merge")(static_cast <bool> (SourceValue.getValueType().getScalarSizeInBits
() > WideNumBits && "Unexpected store value to merge"
) ? void (0) : __assert_fail ("SourceValue.getValueType().getScalarSizeInBits() > WideNumBits && \"Unexpected store value to merge\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7447, __extension__ __PRETTY_FUNCTION__))
;
7448 SourceValue = DAG.getNode(ISD::TRUNCATE, DL, WideVT, SourceValue);
7449 }
7450
7451 // Before legalize we can introduce illegal bswaps/rotates which will be later
7452 // converted to an explicit bswap sequence. This way we end up with a single
7453 // store and byte shuffling instead of several stores and byte shuffling.
7454 if (NeedBswap) {
7455 SourceValue = DAG.getNode(ISD::BSWAP, DL, WideVT, SourceValue);
7456 } else if (NeedRotate) {
7457 assert(WideNumBits % 2 == 0 && "Unexpected type for rotate")(static_cast <bool> (WideNumBits % 2 == 0 && "Unexpected type for rotate"
) ? void (0) : __assert_fail ("WideNumBits % 2 == 0 && \"Unexpected type for rotate\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7457, __extension__ __PRETTY_FUNCTION__))
;
7458 SDValue RotAmt = DAG.getConstant(WideNumBits / 2, DL, WideVT);
7459 SourceValue = DAG.getNode(ISD::ROTR, DL, WideVT, SourceValue, RotAmt);
7460 }
7461
7462 SDValue NewStore =
7463 DAG.getStore(Chain, DL, SourceValue, FirstStore->getBasePtr(),
7464 FirstStore->getPointerInfo(), FirstStore->getAlign());
7465
7466 // Rely on other DAG combine rules to remove the other individual stores.
7467 DAG.ReplaceAllUsesWith(N, NewStore.getNode());
7468 return NewStore;
7469}
7470
7471/// Match a pattern where a wide type scalar value is loaded by several narrow
7472/// loads and combined by shifts and ors. Fold it into a single load or a load
7473/// and a BSWAP if the targets supports it.
7474///
7475/// Assuming little endian target:
7476/// i8 *a = ...
7477/// i32 val = a[0] | (a[1] << 8) | (a[2] << 16) | (a[3] << 24)
7478/// =>
7479/// i32 val = *((i32)a)
7480///
7481/// i8 *a = ...
7482/// i32 val = (a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3]
7483/// =>
7484/// i32 val = BSWAP(*((i32)a))
7485///
7486/// TODO: This rule matches complex patterns with OR node roots and doesn't
7487/// interact well with the worklist mechanism. When a part of the pattern is
7488/// updated (e.g. one of the loads) its direct users are put into the worklist,
7489/// but the root node of the pattern which triggers the load combine is not
7490/// necessarily a direct user of the changed node. For example, once the address
7491/// of t28 load is reassociated load combine won't be triggered:
7492/// t25: i32 = add t4, Constant:i32<2>
7493/// t26: i64 = sign_extend t25
7494/// t27: i64 = add t2, t26
7495/// t28: i8,ch = load<LD1[%tmp9]> t0, t27, undef:i64
7496/// t29: i32 = zero_extend t28
7497/// t32: i32 = shl t29, Constant:i8<8>
7498/// t33: i32 = or t23, t32
7499/// As a possible fix visitLoad can check if the load can be a part of a load
7500/// combine pattern and add corresponding OR roots to the worklist.
7501SDValue DAGCombiner::MatchLoadCombine(SDNode *N) {
7502 assert(N->getOpcode() == ISD::OR &&(static_cast <bool> (N->getOpcode() == ISD::OR &&
"Can only match load combining against OR nodes") ? void (0)
: __assert_fail ("N->getOpcode() == ISD::OR && \"Can only match load combining against OR nodes\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7503, __extension__ __PRETTY_FUNCTION__))
7503 "Can only match load combining against OR nodes")(static_cast <bool> (N->getOpcode() == ISD::OR &&
"Can only match load combining against OR nodes") ? void (0)
: __assert_fail ("N->getOpcode() == ISD::OR && \"Can only match load combining against OR nodes\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7503, __extension__ __PRETTY_FUNCTION__))
;
7504
7505 // Handles simple types only
7506 EVT VT = N->getValueType(0);
7507 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
7508 return SDValue();
7509 unsigned ByteWidth = VT.getSizeInBits() / 8;
7510
7511 bool IsBigEndianTarget = DAG.getDataLayout().isBigEndian();
7512 auto MemoryByteOffset = [&] (ByteProvider P) {
7513 assert(P.isMemory() && "Must be a memory byte provider")(static_cast <bool> (P.isMemory() && "Must be a memory byte provider"
) ? void (0) : __assert_fail ("P.isMemory() && \"Must be a memory byte provider\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7513, __extension__ __PRETTY_FUNCTION__))
;
7514 unsigned LoadBitWidth = P.Load->getMemoryVT().getSizeInBits();
7515 assert(LoadBitWidth % 8 == 0 &&(static_cast <bool> (LoadBitWidth % 8 == 0 && "can only analyze providers for individual bytes not bit"
) ? void (0) : __assert_fail ("LoadBitWidth % 8 == 0 && \"can only analyze providers for individual bytes not bit\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7516, __extension__ __PRETTY_FUNCTION__))
7516 "can only analyze providers for individual bytes not bit")(static_cast <bool> (LoadBitWidth % 8 == 0 && "can only analyze providers for individual bytes not bit"
) ? void (0) : __assert_fail ("LoadBitWidth % 8 == 0 && \"can only analyze providers for individual bytes not bit\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7516, __extension__ __PRETTY_FUNCTION__))
;
7517 unsigned LoadByteWidth = LoadBitWidth / 8;
7518 return IsBigEndianTarget
7519 ? bigEndianByteAt(LoadByteWidth, P.ByteOffset)
7520 : littleEndianByteAt(LoadByteWidth, P.ByteOffset);
7521 };
7522
7523 Optional<BaseIndexOffset> Base;
7524 SDValue Chain;
7525
7526 SmallPtrSet<LoadSDNode *, 8> Loads;
7527 Optional<ByteProvider> FirstByteProvider;
7528 int64_t FirstOffset = INT64_MAX(9223372036854775807L);
7529
7530 // Check if all the bytes of the OR we are looking at are loaded from the same
7531 // base address. Collect bytes offsets from Base address in ByteOffsets.
7532 SmallVector<int64_t, 8> ByteOffsets(ByteWidth);
7533 unsigned ZeroExtendedBytes = 0;
7534 for (int i = ByteWidth - 1; i >= 0; --i) {
7535 auto P = calculateByteProvider(SDValue(N, 0), i, 0, /*Root=*/true);
7536 if (!P)
7537 return SDValue();
7538
7539 if (P->isConstantZero()) {
7540 // It's OK for the N most significant bytes to be 0, we can just
7541 // zero-extend the load.
7542 if (++ZeroExtendedBytes != (ByteWidth - static_cast<unsigned>(i)))
7543 return SDValue();
7544 continue;
7545 }
7546 assert(P->isMemory() && "provenance should either be memory or zero")(static_cast <bool> (P->isMemory() && "provenance should either be memory or zero"
) ? void (0) : __assert_fail ("P->isMemory() && \"provenance should either be memory or zero\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7546, __extension__ __PRETTY_FUNCTION__))
;
7547
7548 LoadSDNode *L = P->Load;
7549 assert(L->hasNUsesOfValue(1, 0) && L->isSimple() &&(static_cast <bool> (L->hasNUsesOfValue(1, 0) &&
L->isSimple() && !L->isIndexed() && "Must be enforced by calculateByteProvider"
) ? void (0) : __assert_fail ("L->hasNUsesOfValue(1, 0) && L->isSimple() && !L->isIndexed() && \"Must be enforced by calculateByteProvider\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7551, __extension__ __PRETTY_FUNCTION__))
7550 !L->isIndexed() &&(static_cast <bool> (L->hasNUsesOfValue(1, 0) &&
L->isSimple() && !L->isIndexed() && "Must be enforced by calculateByteProvider"
) ? void (0) : __assert_fail ("L->hasNUsesOfValue(1, 0) && L->isSimple() && !L->isIndexed() && \"Must be enforced by calculateByteProvider\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7551, __extension__ __PRETTY_FUNCTION__))
7551 "Must be enforced by calculateByteProvider")(static_cast <bool> (L->hasNUsesOfValue(1, 0) &&
L->isSimple() && !L->isIndexed() && "Must be enforced by calculateByteProvider"
) ? void (0) : __assert_fail ("L->hasNUsesOfValue(1, 0) && L->isSimple() && !L->isIndexed() && \"Must be enforced by calculateByteProvider\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7551, __extension__ __PRETTY_FUNCTION__))
;
7552 assert(L->getOffset().isUndef() && "Unindexed load must have undef offset")(static_cast <bool> (L->getOffset().isUndef() &&
"Unindexed load must have undef offset") ? void (0) : __assert_fail
("L->getOffset().isUndef() && \"Unindexed load must have undef offset\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7552, __extension__ __PRETTY_FUNCTION__))
;
7553
7554 // All loads must share the same chain
7555 SDValue LChain = L->getChain();
7556 if (!Chain)
7557 Chain = LChain;
7558 else if (Chain != LChain)
7559 return SDValue();
7560
7561 // Loads must share the same base address
7562 BaseIndexOffset Ptr = BaseIndexOffset::match(L, DAG);
7563 int64_t ByteOffsetFromBase = 0;
7564 if (!Base)
7565 Base = Ptr;
7566 else if (!Base->equalBaseIndex(Ptr, DAG, ByteOffsetFromBase))
7567 return SDValue();
7568
7569 // Calculate the offset of the current byte from the base address
7570 ByteOffsetFromBase += MemoryByteOffset(*P);
7571 ByteOffsets[i] = ByteOffsetFromBase;
7572
7573 // Remember the first byte load
7574 if (ByteOffsetFromBase < FirstOffset) {
7575 FirstByteProvider = P;
7576 FirstOffset = ByteOffsetFromBase;
7577 }
7578
7579 Loads.insert(L);
7580 }
7581 assert(!Loads.empty() && "All the bytes of the value must be loaded from "(static_cast <bool> (!Loads.empty() && "All the bytes of the value must be loaded from "
"memory, so there must be at least one load which produces the value"
) ? void (0) : __assert_fail ("!Loads.empty() && \"All the bytes of the value must be loaded from \" \"memory, so there must be at least one load which produces the value\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7582, __extension__ __PRETTY_FUNCTION__))
7582 "memory, so there must be at least one load which produces the value")(static_cast <bool> (!Loads.empty() && "All the bytes of the value must be loaded from "
"memory, so there must be at least one load which produces the value"
) ? void (0) : __assert_fail ("!Loads.empty() && \"All the bytes of the value must be loaded from \" \"memory, so there must be at least one load which produces the value\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7582, __extension__ __PRETTY_FUNCTION__))
;
7583 assert(Base && "Base address of the accessed memory location must be set")(static_cast <bool> (Base && "Base address of the accessed memory location must be set"
) ? void (0) : __assert_fail ("Base && \"Base address of the accessed memory location must be set\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7583, __extension__ __PRETTY_FUNCTION__))
;
7584 assert(FirstOffset != INT64_MAX && "First byte offset must be set")(static_cast <bool> (FirstOffset != (9223372036854775807L
) && "First byte offset must be set") ? void (0) : __assert_fail
("FirstOffset != INT64_MAX && \"First byte offset must be set\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7584, __extension__ __PRETTY_FUNCTION__))
;
7585
7586 bool NeedsZext = ZeroExtendedBytes > 0;
7587
7588 EVT MemVT =
7589 EVT::getIntegerVT(*DAG.getContext(), (ByteWidth - ZeroExtendedBytes) * 8);
7590
7591 if (!MemVT.isSimple())
7592 return SDValue();
7593
7594 // Before legalize we can introduce too wide illegal loads which will be later
7595 // split into legal sized loads. This enables us to combine i64 load by i8
7596 // patterns to a couple of i32 loads on 32 bit targets.
7597 if (LegalOperations &&
7598 !TLI.isOperationLegal(NeedsZext ? ISD::ZEXTLOAD : ISD::NON_EXTLOAD,
7599 MemVT))
7600 return SDValue();
7601
7602 // Check if the bytes of the OR we are looking at match with either big or
7603 // little endian value load
7604 Optional<bool> IsBigEndian = isBigEndian(
7605 makeArrayRef(ByteOffsets).drop_back(ZeroExtendedBytes), FirstOffset);
7606 if (!IsBigEndian.hasValue())
7607 return SDValue();
7608
7609 assert(FirstByteProvider && "must be set")(static_cast <bool> (FirstByteProvider && "must be set"
) ? void (0) : __assert_fail ("FirstByteProvider && \"must be set\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7609, __extension__ __PRETTY_FUNCTION__))
;
7610
7611 // Ensure that the first byte is loaded from zero offset of the first load.
7612 // So the combined value can be loaded from the first load address.
7613 if (MemoryByteOffset(*FirstByteProvider) != 0)
7614 return SDValue();
7615 LoadSDNode *FirstLoad = FirstByteProvider->Load;
7616
7617 // The node we are looking at matches with the pattern, check if we can
7618 // replace it with a single (possibly zero-extended) load and bswap + shift if
7619 // needed.
7620
7621 // If the load needs byte swap check if the target supports it
7622 bool NeedsBswap = IsBigEndianTarget != *IsBigEndian;
7623
7624 // Before legalize we can introduce illegal bswaps which will be later
7625 // converted to an explicit bswap sequence. This way we end up with a single
7626 // load and byte shuffling instead of several loads and byte shuffling.
7627 // We do not introduce illegal bswaps when zero-extending as this tends to
7628 // introduce too many arithmetic instructions.
7629 if (NeedsBswap && (LegalOperations || NeedsZext) &&
7630 !TLI.isOperationLegal(ISD::BSWAP, VT))
7631 return SDValue();
7632
7633 // If we need to bswap and zero extend, we have to insert a shift. Check that
7634 // it is legal.
7635 if (NeedsBswap && NeedsZext && LegalOperations &&
7636 !TLI.isOperationLegal(ISD::SHL, VT))
7637 return SDValue();
7638
7639 // Check that a load of the wide type is both allowed and fast on the target
7640 bool Fast = false;
7641 bool Allowed =
7642 TLI.allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), MemVT,
7643 *FirstLoad->getMemOperand(), &Fast);
7644 if (!Allowed || !Fast)
7645 return SDValue();
7646
7647 SDValue NewLoad =
7648 DAG.getExtLoad(NeedsZext ? ISD::ZEXTLOAD : ISD::NON_EXTLOAD, SDLoc(N), VT,
7649 Chain, FirstLoad->getBasePtr(),
7650 FirstLoad->getPointerInfo(), MemVT, FirstLoad->getAlign());
7651
7652 // Transfer chain users from old loads to the new load.
7653 for (LoadSDNode *L : Loads)
7654 DAG.ReplaceAllUsesOfValueWith(SDValue(L, 1), SDValue(NewLoad.getNode(), 1));
7655
7656 if (!NeedsBswap)
7657 return NewLoad;
7658
7659 SDValue ShiftedLoad =
7660 NeedsZext
7661 ? DAG.getNode(ISD::SHL, SDLoc(N), VT, NewLoad,
7662 DAG.getShiftAmountConstant(ZeroExtendedBytes * 8, VT,
7663 SDLoc(N), LegalOperations))
7664 : NewLoad;
7665 return DAG.getNode(ISD::BSWAP, SDLoc(N), VT, ShiftedLoad);
7666}
7667
7668// If the target has andn, bsl, or a similar bit-select instruction,
7669// we want to unfold masked merge, with canonical pattern of:
7670// | A | |B|
7671// ((x ^ y) & m) ^ y
7672// | D |
7673// Into:
7674// (x & m) | (y & ~m)
7675// If y is a constant, and the 'andn' does not work with immediates,
7676// we unfold into a different pattern:
7677// ~(~x & m) & (m | y)
7678// NOTE: we don't unfold the pattern if 'xor' is actually a 'not', because at
7679// the very least that breaks andnpd / andnps patterns, and because those
7680// patterns are simplified in IR and shouldn't be created in the DAG
7681SDValue DAGCombiner::unfoldMaskedMerge(SDNode *N) {
7682 assert(N->getOpcode() == ISD::XOR)(static_cast <bool> (N->getOpcode() == ISD::XOR) ? void
(0) : __assert_fail ("N->getOpcode() == ISD::XOR", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7682, __extension__ __PRETTY_FUNCTION__))
;
7683
7684 // Don't touch 'not' (i.e. where y = -1).
7685 if (isAllOnesOrAllOnesSplat(N->getOperand(1)))
7686 return SDValue();
7687
7688 EVT VT = N->getValueType(0);
7689
7690 // There are 3 commutable operators in the pattern,
7691 // so we have to deal with 8 possible variants of the basic pattern.
7692 SDValue X, Y, M;
7693 auto matchAndXor = [&X, &Y, &M](SDValue And, unsigned XorIdx, SDValue Other) {
7694 if (And.getOpcode() != ISD::AND || !And.hasOneUse())
7695 return false;
7696 SDValue Xor = And.getOperand(XorIdx);
7697 if (Xor.getOpcode() != ISD::XOR || !Xor.hasOneUse())
7698 return false;
7699 SDValue Xor0 = Xor.getOperand(0);
7700 SDValue Xor1 = Xor.getOperand(1);
7701 // Don't touch 'not' (i.e. where y = -1).
7702 if (isAllOnesOrAllOnesSplat(Xor1))
7703 return false;
7704 if (Other == Xor0)
7705 std::swap(Xor0, Xor1);
7706 if (Other != Xor1)
7707 return false;
7708 X = Xor0;
7709 Y = Xor1;
7710 M = And.getOperand(XorIdx ? 0 : 1);
7711 return true;
7712 };
7713
7714 SDValue N0 = N->getOperand(0);
7715 SDValue N1 = N->getOperand(1);
7716 if (!matchAndXor(N0, 0, N1) && !matchAndXor(N0, 1, N1) &&
7717 !matchAndXor(N1, 0, N0) && !matchAndXor(N1, 1, N0))
7718 return SDValue();
7719
7720 // Don't do anything if the mask is constant. This should not be reachable.
7721 // InstCombine should have already unfolded this pattern, and DAGCombiner
7722 // probably shouldn't produce it, too.
7723 if (isa<ConstantSDNode>(M.getNode()))
7724 return SDValue();
7725
7726 // We can transform if the target has AndNot
7727 if (!TLI.hasAndNot(M))
7728 return SDValue();
7729
7730 SDLoc DL(N);
7731
7732 // If Y is a constant, check that 'andn' works with immediates.
7733 if (!TLI.hasAndNot(Y)) {
7734 assert(TLI.hasAndNot(X) && "Only mask is a variable? Unreachable.")(static_cast <bool> (TLI.hasAndNot(X) && "Only mask is a variable? Unreachable."
) ? void (0) : __assert_fail ("TLI.hasAndNot(X) && \"Only mask is a variable? Unreachable.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7734, __extension__ __PRETTY_FUNCTION__))
;
7735 // If not, we need to do a bit more work to make sure andn is still used.
7736 SDValue NotX = DAG.getNOT(DL, X, VT);
7737 SDValue LHS = DAG.getNode(ISD::AND, DL, VT, NotX, M);
7738 SDValue NotLHS = DAG.getNOT(DL, LHS, VT);
7739 SDValue RHS = DAG.getNode(ISD::OR, DL, VT, M, Y);
7740 return DAG.getNode(ISD::AND, DL, VT, NotLHS, RHS);
7741 }
7742
7743 SDValue LHS = DAG.getNode(ISD::AND, DL, VT, X, M);
7744 SDValue NotM = DAG.getNOT(DL, M, VT);
7745 SDValue RHS = DAG.getNode(ISD::AND, DL, VT, Y, NotM);
7746
7747 return DAG.getNode(ISD::OR, DL, VT, LHS, RHS);
7748}
7749
7750SDValue DAGCombiner::visitXOR(SDNode *N) {
7751 SDValue N0 = N->getOperand(0);
7752 SDValue N1 = N->getOperand(1);
7753 EVT VT = N0.getValueType();
7754
7755 // fold vector ops
7756 if (VT.isVector()) {
7757 if (SDValue FoldedVOp = SimplifyVBinOp(N))
7758 return FoldedVOp;
7759
7760 // fold (xor x, 0) -> x, vector edition
7761 if (ISD::isConstantSplatVectorAllZeros(N0.getNode()))
7762 return N1;
7763 if (ISD::isConstantSplatVectorAllZeros(N1.getNode()))
7764 return N0;
7765 }
7766
7767 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
7768 SDLoc DL(N);
7769 if (N0.isUndef() && N1.isUndef())
7770 return DAG.getConstant(0, DL, VT);
7771
7772 // fold (xor x, undef) -> undef
7773 if (N0.isUndef())
7774 return N0;
7775 if (N1.isUndef())
7776 return N1;
7777
7778 // fold (xor c1, c2) -> c1^c2
7779 if (SDValue C = DAG.FoldConstantArithmetic(ISD::XOR, DL, VT, {N0, N1}))
7780 return C;
7781
7782 // canonicalize constant to RHS
7783 if (DAG.isConstantIntBuildVectorOrConstantInt(N0) &&
7784 !DAG.isConstantIntBuildVectorOrConstantInt(N1))
7785 return DAG.getNode(ISD::XOR, DL, VT, N1, N0);
7786
7787 // fold (xor x, 0) -> x
7788 if (isNullConstant(N1))
7789 return N0;
7790
7791 if (SDValue NewSel = foldBinOpIntoSelect(N))
7792 return NewSel;
7793
7794 // reassociate xor
7795 if (SDValue RXOR = reassociateOps(ISD::XOR, DL, N0, N1, N->getFlags()))
7796 return RXOR;
7797
7798 // fold !(x cc y) -> (x !cc y)
7799 unsigned N0Opcode = N0.getOpcode();
7800 SDValue LHS, RHS, CC;
7801 if (TLI.isConstTrueVal(N1.getNode()) &&
7802 isSetCCEquivalent(N0, LHS, RHS, CC, /*MatchStrict*/true)) {
7803 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
7804 LHS.getValueType());
7805 if (!LegalOperations ||
7806 TLI.isCondCodeLegal(NotCC, LHS.getSimpleValueType())) {
7807 switch (N0Opcode) {
7808 default:
7809 llvm_unreachable("Unhandled SetCC Equivalent!")::llvm::llvm_unreachable_internal("Unhandled SetCC Equivalent!"
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7809)
;
7810 case ISD::SETCC:
7811 return DAG.getSetCC(SDLoc(N0), VT, LHS, RHS, NotCC);
7812 case ISD::SELECT_CC:
7813 return DAG.getSelectCC(SDLoc(N0), LHS, RHS, N0.getOperand(2),
7814 N0.getOperand(3), NotCC);
7815 case ISD::STRICT_FSETCC:
7816 case ISD::STRICT_FSETCCS: {
7817 if (N0.hasOneUse()) {
7818 // FIXME Can we handle multiple uses? Could we token factor the chain
7819 // results from the new/old setcc?
7820 SDValue SetCC =
7821 DAG.getSetCC(SDLoc(N0), VT, LHS, RHS, NotCC,
7822 N0.getOperand(0), N0Opcode == ISD::STRICT_FSETCCS);
7823 CombineTo(N, SetCC);
7824 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), SetCC.getValue(1));
7825 recursivelyDeleteUnusedNodes(N0.getNode());
7826 return SDValue(N, 0); // Return N so it doesn't get rechecked!
7827 }
7828 break;
7829 }
7830 }
7831 }
7832 }
7833
7834 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
7835 if (isOneConstant(N1) && N0Opcode == ISD::ZERO_EXTEND && N0.hasOneUse() &&
7836 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
7837 SDValue V = N0.getOperand(0);
7838 SDLoc DL0(N0);
7839 V = DAG.getNode(ISD::XOR, DL0, V.getValueType(), V,
7840 DAG.getConstant(1, DL0, V.getValueType()));
7841 AddToWorklist(V.getNode());
7842 return DAG.getNode(ISD::ZERO_EXTEND, DL, VT, V);
7843 }
7844
7845 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
7846 if (isOneConstant(N1) && VT == MVT::i1 && N0.hasOneUse() &&
7847 (N0Opcode == ISD::OR || N0Opcode == ISD::AND)) {
7848 SDValue N00 = N0.getOperand(0), N01 = N0.getOperand(1);
7849 if (isOneUseSetCC(N01) || isOneUseSetCC(N00)) {
7850 unsigned NewOpcode = N0Opcode == ISD::AND ? ISD::OR : ISD::AND;
7851 N00 = DAG.getNode(ISD::XOR, SDLoc(N00), VT, N00, N1); // N00 = ~N00
7852 N01 = DAG.getNode(ISD::XOR, SDLoc(N01), VT, N01, N1); // N01 = ~N01
7853 AddToWorklist(N00.getNode()); AddToWorklist(N01.getNode());
7854 return DAG.getNode(NewOpcode, DL, VT, N00, N01);
7855 }
7856 }
7857 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
7858 if (isAllOnesConstant(N1) && N0.hasOneUse() &&
7859 (N0Opcode == ISD::OR || N0Opcode == ISD::AND)) {
7860 SDValue N00 = N0.getOperand(0), N01 = N0.getOperand(1);
7861 if (isa<ConstantSDNode>(N01) || isa<ConstantSDNode>(N00)) {
7862 unsigned NewOpcode = N0Opcode == ISD::AND ? ISD::OR : ISD::AND;
7863 N00 = DAG.getNode(ISD::XOR, SDLoc(N00), VT, N00, N1); // N00 = ~N00
7864 N01 = DAG.getNode(ISD::XOR, SDLoc(N01), VT, N01, N1); // N01 = ~N01
7865 AddToWorklist(N00.getNode()); AddToWorklist(N01.getNode());
7866 return DAG.getNode(NewOpcode, DL, VT, N00, N01);
7867 }
7868 }
7869
7870 // fold (not (neg x)) -> (add X, -1)
7871 // FIXME: This can be generalized to (not (sub Y, X)) -> (add X, ~Y) if
7872 // Y is a constant or the subtract has a single use.
7873 if (isAllOnesConstant(N1) && N0.getOpcode() == ISD::SUB &&
7874 isNullConstant(N0.getOperand(0))) {
7875 return DAG.getNode(ISD::ADD, DL, VT, N0.getOperand(1),
7876 DAG.getAllOnesConstant(DL, VT));
7877 }
7878
7879 // fold (not (add X, -1)) -> (neg X)
7880 if (isAllOnesConstant(N1) && N0.getOpcode() == ISD::ADD &&
7881 isAllOnesOrAllOnesSplat(N0.getOperand(1))) {
7882 return DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT),
7883 N0.getOperand(0));
7884 }
7885
7886 // fold (xor (and x, y), y) -> (and (not x), y)
7887 if (N0Opcode == ISD::AND && N0.hasOneUse() && N0->getOperand(1) == N1) {
7888 SDValue X = N0.getOperand(0);
7889 SDValue NotX = DAG.getNOT(SDLoc(X), X, VT);
7890 AddToWorklist(NotX.getNode());
7891 return DAG.getNode(ISD::AND, DL, VT, NotX, N1);
7892 }
7893
7894 if ((N0Opcode == ISD::SRL || N0Opcode == ISD::SHL) && N0.hasOneUse()) {
7895 ConstantSDNode *XorC = isConstOrConstSplat(N1);
7896 ConstantSDNode *ShiftC = isConstOrConstSplat(N0.getOperand(1));
7897 unsigned BitWidth = VT.getScalarSizeInBits();
7898 if (XorC && ShiftC) {
7899 // Don't crash on an oversized shift. We can not guarantee that a bogus
7900 // shift has been simplified to undef.
7901 uint64_t ShiftAmt = ShiftC->getLimitedValue();
7902 if (ShiftAmt < BitWidth) {
7903 APInt Ones = APInt::getAllOnesValue(BitWidth);
7904 Ones = N0Opcode == ISD::SHL ? Ones.shl(ShiftAmt) : Ones.lshr(ShiftAmt);
7905 if (XorC->getAPIntValue() == Ones) {
7906 // If the xor constant is a shifted -1, do a 'not' before the shift:
7907 // xor (X << ShiftC), XorC --> (not X) << ShiftC
7908 // xor (X >> ShiftC), XorC --> (not X) >> ShiftC
7909 SDValue Not = DAG.getNOT(DL, N0.getOperand(0), VT);
7910 return DAG.getNode(N0Opcode, DL, VT, Not, N0.getOperand(1));
7911 }
7912 }
7913 }
7914 }
7915
7916 // fold Y = sra (X, size(X)-1); xor (add (X, Y), Y) -> (abs X)
7917 if (TLI.isOperationLegalOrCustom(ISD::ABS, VT)) {
7918 SDValue A = N0Opcode == ISD::ADD ? N0 : N1;
7919 SDValue S = N0Opcode == ISD::SRA ? N0 : N1;
7920 if (A.getOpcode() == ISD::ADD && S.getOpcode() == ISD::SRA) {
7921 SDValue A0 = A.getOperand(0), A1 = A.getOperand(1);
7922 SDValue S0 = S.getOperand(0);
7923 if ((A0 == S && A1 == S0) || (A1 == S && A0 == S0))
7924 if (ConstantSDNode *C = isConstOrConstSplat(S.getOperand(1)))
7925 if (C->getAPIntValue() == (VT.getScalarSizeInBits() - 1))
7926 return DAG.getNode(ISD::ABS, DL, VT, S0);
7927 }
7928 }
7929
7930 // fold (xor x, x) -> 0
7931 if (N0 == N1)
7932 return tryFoldToZero(DL, TLI, VT, DAG, LegalOperations);
7933
7934 // fold (xor (shl 1, x), -1) -> (rotl ~1, x)
7935 // Here is a concrete example of this equivalence:
7936 // i16 x == 14
7937 // i16 shl == 1 << 14 == 16384 == 0b0100000000000000
7938 // i16 xor == ~(1 << 14) == 49151 == 0b1011111111111111
7939 //
7940 // =>
7941 //
7942 // i16 ~1 == 0b1111111111111110
7943 // i16 rol(~1, 14) == 0b1011111111111111
7944 //
7945 // Some additional tips to help conceptualize this transform:
7946 // - Try to see the operation as placing a single zero in a value of all ones.
7947 // - There exists no value for x which would allow the result to contain zero.
7948 // - Values of x larger than the bitwidth are undefined and do not require a
7949 // consistent result.
7950 // - Pushing the zero left requires shifting one bits in from the right.
7951 // A rotate left of ~1 is a nice way of achieving the desired result.
7952 if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT) && N0Opcode == ISD::SHL &&
7953 isAllOnesConstant(N1) && isOneConstant(N0.getOperand(0))) {
7954 return DAG.getNode(ISD::ROTL, DL, VT, DAG.getConstant(~1, DL, VT),
7955 N0.getOperand(1));
7956 }
7957
7958 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
7959 if (N0Opcode == N1.getOpcode())
7960 if (SDValue V = hoistLogicOpWithSameOpcodeHands(N))
7961 return V;
7962
7963 // Unfold ((x ^ y) & m) ^ y into (x & m) | (y & ~m) if profitable
7964 if (SDValue MM = unfoldMaskedMerge(N))
7965 return MM;
7966
7967 // Simplify the expression using non-local knowledge.
7968 if (SimplifyDemandedBits(SDValue(N, 0)))
7969 return SDValue(N, 0);
7970
7971 if (SDValue Combined = combineCarryDiamond(*this, DAG, TLI, N0, N1, N))
7972 return Combined;
7973
7974 return SDValue();
7975}
7976
7977/// If we have a shift-by-constant of a bitwise logic op that itself has a
7978/// shift-by-constant operand with identical opcode, we may be able to convert
7979/// that into 2 independent shifts followed by the logic op. This is a
7980/// throughput improvement.
7981static SDValue combineShiftOfShiftedLogic(SDNode *Shift, SelectionDAG &DAG) {
7982 // Match a one-use bitwise logic op.
7983 SDValue LogicOp = Shift->getOperand(0);
7984 if (!LogicOp.hasOneUse())
7985 return SDValue();
7986
7987 unsigned LogicOpcode = LogicOp.getOpcode();
7988 if (LogicOpcode != ISD::AND && LogicOpcode != ISD::OR &&
7989 LogicOpcode != ISD::XOR)
7990 return SDValue();
7991
7992 // Find a matching one-use shift by constant.
7993 unsigned ShiftOpcode = Shift->getOpcode();
7994 SDValue C1 = Shift->getOperand(1);
7995 ConstantSDNode *C1Node = isConstOrConstSplat(C1);
7996 assert(C1Node && "Expected a shift with constant operand")(static_cast <bool> (C1Node && "Expected a shift with constant operand"
) ? void (0) : __assert_fail ("C1Node && \"Expected a shift with constant operand\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 7996, __extension__ __PRETTY_FUNCTION__))
;
7997 const APInt &C1Val = C1Node->getAPIntValue();
7998 auto matchFirstShift = [&](SDValue V, SDValue &ShiftOp,
7999 const APInt *&ShiftAmtVal) {
8000 if (V.getOpcode() != ShiftOpcode || !V.hasOneUse())
8001 return false;
8002
8003 ConstantSDNode *ShiftCNode = isConstOrConstSplat(V.getOperand(1));
8004 if (!ShiftCNode)
8005 return false;
8006
8007 // Capture the shifted operand and shift amount value.
8008 ShiftOp = V.getOperand(0);
8009 ShiftAmtVal = &ShiftCNode->getAPIntValue();
8010
8011 // Shift amount types do not have to match their operand type, so check that
8012 // the constants are the same width.
8013 if (ShiftAmtVal->getBitWidth() != C1Val.getBitWidth())
8014 return false;
8015
8016 // The fold is not valid if the sum of the shift values exceeds bitwidth.
8017 if ((*ShiftAmtVal + C1Val).uge(V.getScalarValueSizeInBits()))
8018 return false;
8019
8020 return true;
8021 };
8022
8023 // Logic ops are commutative, so check each operand for a match.
8024 SDValue X, Y;
8025 const APInt *C0Val;
8026 if (matchFirstShift(LogicOp.getOperand(0), X, C0Val))
8027 Y = LogicOp.getOperand(1);
8028 else if (matchFirstShift(LogicOp.getOperand(1), X, C0Val))
8029 Y = LogicOp.getOperand(0);
8030 else
8031 return SDValue();
8032
8033 // shift (logic (shift X, C0), Y), C1 -> logic (shift X, C0+C1), (shift Y, C1)
8034 SDLoc DL(Shift);
8035 EVT VT = Shift->getValueType(0);
8036 EVT ShiftAmtVT = Shift->getOperand(1).getValueType();
8037 SDValue ShiftSumC = DAG.getConstant(*C0Val + C1Val, DL, ShiftAmtVT);
8038 SDValue NewShift1 = DAG.getNode(ShiftOpcode, DL, VT, X, ShiftSumC);
8039 SDValue NewShift2 = DAG.getNode(ShiftOpcode, DL, VT, Y, C1);
8040 return DAG.getNode(LogicOpcode, DL, VT, NewShift1, NewShift2);
8041}
8042
8043/// Handle transforms common to the three shifts, when the shift amount is a
8044/// constant.
8045/// We are looking for: (shift being one of shl/sra/srl)
8046/// shift (binop X, C0), C1
8047/// And want to transform into:
8048/// binop (shift X, C1), (shift C0, C1)
8049SDValue DAGCombiner::visitShiftByConstant(SDNode *N) {
8050 assert(isConstOrConstSplat(N->getOperand(1)) && "Expected constant operand")(static_cast <bool> (isConstOrConstSplat(N->getOperand
(1)) && "Expected constant operand") ? void (0) : __assert_fail
("isConstOrConstSplat(N->getOperand(1)) && \"Expected constant operand\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 8050, __extension__ __PRETTY_FUNCTION__))
;
8051
8052 // Do not turn a 'not' into a regular xor.
8053 if (isBitwiseNot(N->getOperand(0)))
8054 return SDValue();
8055
8056 // The inner binop must be one-use, since we want to replace it.
8057 SDValue LHS = N->getOperand(0);
8058 if (!LHS.hasOneUse() || !TLI.isDesirableToCommuteWithShift(N, Level))
8059 return SDValue();
8060
8061 // TODO: This is limited to early combining because it may reveal regressions
8062 // otherwise. But since we just checked a target hook to see if this is
8063 // desirable, that should have filtered out cases where this interferes
8064 // with some other pattern matching.
8065 if (!LegalTypes)
8066 if (SDValue R = combineShiftOfShiftedLogic(N, DAG))
8067 return R;
8068
8069 // We want to pull some binops through shifts, so that we have (and (shift))
8070 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
8071 // thing happens with address calculations, so it's important to canonicalize
8072 // it.
8073 switch (LHS.getOpcode()) {
8074 default:
8075 return SDValue();
8076 case ISD::OR:
8077 case ISD::XOR:
8078 case ISD::AND:
8079 break;
8080 case ISD::ADD:
8081 if (N->getOpcode() != ISD::SHL)
8082 return SDValue(); // only shl(add) not sr[al](add).
8083 break;
8084 }
8085
8086 // We require the RHS of the binop to be a constant and not opaque as well.
8087 ConstantSDNode *BinOpCst = getAsNonOpaqueConstant(LHS.getOperand(1));
8088 if (!BinOpCst)
8089 return SDValue();
8090
8091 // FIXME: disable this unless the input to the binop is a shift by a constant
8092 // or is copy/select. Enable this in other cases when figure out it's exactly
8093 // profitable.
8094 SDValue BinOpLHSVal = LHS.getOperand(0);
8095 bool IsShiftByConstant = (BinOpLHSVal.getOpcode() == ISD::SHL ||
8096 BinOpLHSVal.getOpcode() == ISD::SRA ||
8097 BinOpLHSVal.getOpcode() == ISD::SRL) &&
8098 isa<ConstantSDNode>(BinOpLHSVal.getOperand(1));
8099 bool IsCopyOrSelect = BinOpLHSVal.getOpcode() == ISD::CopyFromReg ||
8100 BinOpLHSVal.getOpcode() == ISD::SELECT;
8101
8102 if (!IsShiftByConstant && !IsCopyOrSelect)
8103 return SDValue();
8104
8105 if (IsCopyOrSelect && N->hasOneUse())
8106 return SDValue();
8107
8108 // Fold the constants, shifting the binop RHS by the shift amount.
8109 SDLoc DL(N);
8110 EVT VT = N->getValueType(0);
8111 SDValue NewRHS = DAG.getNode(N->getOpcode(), DL, VT, LHS.getOperand(1),
8112 N->getOperand(1));
8113 assert(isa<ConstantSDNode>(NewRHS) && "Folding was not successful!")(static_cast <bool> (isa<ConstantSDNode>(NewRHS) &&
"Folding was not successful!") ? void (0) : __assert_fail ("isa<ConstantSDNode>(NewRHS) && \"Folding was not successful!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 8113, __extension__ __PRETTY_FUNCTION__))
;
8114
8115 SDValue NewShift = DAG.getNode(N->getOpcode(), DL, VT, LHS.getOperand(0),
8116 N->getOperand(1));
8117 return DAG.getNode(LHS.getOpcode(), DL, VT, NewShift, NewRHS);
8118}
8119
8120SDValue DAGCombiner::distributeTruncateThroughAnd(SDNode *N) {
8121 assert(N->getOpcode() == ISD::TRUNCATE)(static_cast <bool> (N->getOpcode() == ISD::TRUNCATE
) ? void (0) : __assert_fail ("N->getOpcode() == ISD::TRUNCATE"
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 8121, __extension__ __PRETTY_FUNCTION__))
;
8122 assert(N->getOperand(0).getOpcode() == ISD::AND)(static_cast <bool> (N->getOperand(0).getOpcode() ==
ISD::AND) ? void (0) : __assert_fail ("N->getOperand(0).getOpcode() == ISD::AND"
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 8122, __extension__ __PRETTY_FUNCTION__))
;
8123
8124 // (truncate:TruncVT (and N00, N01C)) -> (and (truncate:TruncVT N00), TruncC)
8125 EVT TruncVT = N->getValueType(0);
8126 if (N->hasOneUse() && N->getOperand(0).hasOneUse() &&
8127 TLI.isTypeDesirableForOp(ISD::AND, TruncVT)) {
8128 SDValue N01 = N->getOperand(0).getOperand(1);
8129 if (isConstantOrConstantVector(N01, /* NoOpaques */ true)) {
8130 SDLoc DL(N);
8131 SDValue N00 = N->getOperand(0).getOperand(0);
8132 SDValue Trunc00 = DAG.getNode(ISD::TRUNCATE, DL, TruncVT, N00);
8133 SDValue Trunc01 = DAG.getNode(ISD::TRUNCATE, DL, TruncVT, N01);
8134 AddToWorklist(Trunc00.getNode());
8135 AddToWorklist(Trunc01.getNode());
8136 return DAG.getNode(ISD::AND, DL, TruncVT, Trunc00, Trunc01);
8137 }
8138 }
8139
8140 return SDValue();
8141}
8142
8143SDValue DAGCombiner::visitRotate(SDNode *N) {
8144 SDLoc dl(N);
8145 SDValue N0 = N->getOperand(0);
8146 SDValue N1 = N->getOperand(1);
8147 EVT VT = N->getValueType(0);
8148 unsigned Bitsize = VT.getScalarSizeInBits();
8149
8150 // fold (rot x, 0) -> x
8151 if (isNullOrNullSplat(N1))
8152 return N0;
8153
8154 // fold (rot x, c) -> x iff (c % BitSize) == 0
8155 if (isPowerOf2_32(Bitsize) && Bitsize > 1) {
8156 APInt ModuloMask(N1.getScalarValueSizeInBits(), Bitsize - 1);
8157 if (DAG.MaskedValueIsZero(N1, ModuloMask))
8158 return N0;
8159 }
8160
8161 // fold (rot x, c) -> (rot x, c % BitSize)
8162 bool OutOfRange = false;
8163 auto MatchOutOfRange = [Bitsize, &OutOfRange](ConstantSDNode *C) {
8164 OutOfRange |= C->getAPIntValue().uge(Bitsize);
8165 return true;
8166 };
8167 if (ISD::matchUnaryPredicate(N1, MatchOutOfRange) && OutOfRange) {
8168 EVT AmtVT = N1.getValueType();
8169 SDValue Bits = DAG.getConstant(Bitsize, dl, AmtVT);
8170 if (SDValue Amt =
8171 DAG.FoldConstantArithmetic(ISD::UREM, dl, AmtVT, {N1, Bits}))
8172 return DAG.getNode(N->getOpcode(), dl, VT, N0, Amt);
8173 }
8174
8175 // rot i16 X, 8 --> bswap X
8176 auto *RotAmtC = isConstOrConstSplat(N1);
8177 if (RotAmtC && RotAmtC->getAPIntValue() == 8 &&
8178 VT.getScalarSizeInBits() == 16 && hasOperation(ISD::BSWAP, VT))
8179 return DAG.getNode(ISD::BSWAP, dl, VT, N0);
8180
8181 // Simplify the operands using demanded-bits information.
8182 if (SimplifyDemandedBits(SDValue(N, 0)))
8183 return SDValue(N, 0);
8184
8185 // fold (rot* x, (trunc (and y, c))) -> (rot* x, (and (trunc y), (trunc c))).
8186 if (N1.getOpcode() == ISD::TRUNCATE &&
8187 N1.getOperand(0).getOpcode() == ISD::AND) {
8188 if (SDValue NewOp1 = distributeTruncateThroughAnd(N1.getNode()))
8189 return DAG.getNode(N->getOpcode(), dl, VT, N0, NewOp1);
8190 }
8191
8192 unsigned NextOp = N0.getOpcode();
8193 // fold (rot* (rot* x, c2), c1) -> (rot* x, c1 +- c2 % bitsize)
8194 if (NextOp == ISD::ROTL || NextOp == ISD::ROTR) {
8195 SDNode *C1 = DAG.isConstantIntBuildVectorOrConstantInt(N1);
8196 SDNode *C2 = DAG.isConstantIntBuildVectorOrConstantInt(N0.getOperand(1));
8197 if (C1 && C2 && C1->getValueType(0) == C2->getValueType(0)) {
8198 EVT ShiftVT = C1->getValueType(0);
8199 bool SameSide = (N->getOpcode() == NextOp);
8200 unsigned CombineOp = SameSide ? ISD::ADD : ISD::SUB;
8201 if (SDValue CombinedShift = DAG.FoldConstantArithmetic(
8202 CombineOp, dl, ShiftVT, {N1, N0.getOperand(1)})) {
8203 SDValue BitsizeC = DAG.getConstant(Bitsize, dl, ShiftVT);
8204 SDValue CombinedShiftNorm = DAG.FoldConstantArithmetic(
8205 ISD::SREM, dl, ShiftVT, {CombinedShift, BitsizeC});
8206 return DAG.getNode(N->getOpcode(), dl, VT, N0->getOperand(0),
8207 CombinedShiftNorm);
8208 }
8209 }
8210 }
8211 return SDValue();
8212}
8213
8214SDValue DAGCombiner::visitSHL(SDNode *N) {
8215 SDValue N0 = N->getOperand(0);
8216 SDValue N1 = N->getOperand(1);
8217 if (SDValue V = DAG.simplifyShift(N0, N1))
8218 return V;
8219
8220 EVT VT = N0.getValueType();
8221 EVT ShiftVT = N1.getValueType();
8222 unsigned OpSizeInBits = VT.getScalarSizeInBits();
8223
8224 // fold vector ops
8225 if (VT.isVector()) {
8226 if (SDValue FoldedVOp = SimplifyVBinOp(N))
8227 return FoldedVOp;
8228
8229 BuildVectorSDNode *N1CV = dyn_cast<BuildVectorSDNode>(N1);
8230 // If setcc produces all-one true value then:
8231 // (shl (and (setcc) N01CV) N1CV) -> (and (setcc) N01CV<<N1CV)
8232 if (N1CV && N1CV->isConstant()) {
8233 if (N0.getOpcode() == ISD::AND) {
8234 SDValue N00 = N0->getOperand(0);
8235 SDValue N01 = N0->getOperand(1);
8236 BuildVectorSDNode *N01CV = dyn_cast<BuildVectorSDNode>(N01);
8237
8238 if (N01CV && N01CV->isConstant() && N00.getOpcode() == ISD::SETCC &&
8239 TLI.getBooleanContents(N00.getOperand(0).getValueType()) ==
8240 TargetLowering::ZeroOrNegativeOneBooleanContent) {
8241 if (SDValue C =
8242 DAG.FoldConstantArithmetic(ISD::SHL, SDLoc(N), VT, {N01, N1}))
8243 return DAG.getNode(ISD::AND, SDLoc(N), VT, N00, C);
8244 }
8245 }
8246 }
8247 }
8248
8249 ConstantSDNode *N1C = isConstOrConstSplat(N1);
8250
8251 // fold (shl c1, c2) -> c1<<c2
8252 if (SDValue C = DAG.FoldConstantArithmetic(ISD::SHL, SDLoc(N), VT, {N0, N1}))
8253 return C;
8254
8255 if (SDValue NewSel = foldBinOpIntoSelect(N))
8256 return NewSel;
8257
8258 // if (shl x, c) is known to be zero, return 0
8259 if (DAG.MaskedValueIsZero(SDValue(N, 0),
8260 APInt::getAllOnesValue(OpSizeInBits)))
8261 return DAG.getConstant(0, SDLoc(N), VT);
8262
8263 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
8264 if (N1.getOpcode() == ISD::TRUNCATE &&
8265 N1.getOperand(0).getOpcode() == ISD::AND) {
8266 if (SDValue NewOp1 = distributeTruncateThroughAnd(N1.getNode()))
8267 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0, NewOp1);
8268 }
8269
8270 if (SimplifyDemandedBits(SDValue(N, 0)))
8271 return SDValue(N, 0);
8272
8273 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
8274 if (N0.getOpcode() == ISD::SHL) {
8275 auto MatchOutOfRange = [OpSizeInBits](ConstantSDNode *LHS,
8276 ConstantSDNode *RHS) {
8277 APInt c1 = LHS->getAPIntValue();
8278 APInt c2 = RHS->getAPIntValue();
8279 zeroExtendToMatch(c1, c2, 1 /* Overflow Bit */);
8280 return (c1 + c2).uge(OpSizeInBits);
8281 };
8282 if (ISD::matchBinaryPredicate(N1, N0.getOperand(1), MatchOutOfRange))
8283 return DAG.getConstant(0, SDLoc(N), VT);
8284
8285 auto MatchInRange = [OpSizeInBits](ConstantSDNode *LHS,
8286 ConstantSDNode *RHS) {
8287 APInt c1 = LHS->getAPIntValue();
8288 APInt c2 = RHS->getAPIntValue();
8289 zeroExtendToMatch(c1, c2, 1 /* Overflow Bit */);
8290 return (c1 + c2).ult(OpSizeInBits);
8291 };
8292 if (ISD::matchBinaryPredicate(N1, N0.getOperand(1), MatchInRange)) {
8293 SDLoc DL(N);
8294 SDValue Sum = DAG.getNode(ISD::ADD, DL, ShiftVT, N1, N0.getOperand(1));
8295 return DAG.getNode(ISD::SHL, DL, VT, N0.getOperand(0), Sum);
8296 }
8297 }
8298
8299 // fold (shl (ext (shl x, c1)), c2) -> (shl (ext x), (add c1, c2))
8300 // For this to be valid, the second form must not preserve any of the bits
8301 // that are shifted out by the inner shift in the first form. This means
8302 // the outer shift size must be >= the number of bits added by the ext.
8303 // As a corollary, we don't care what kind of ext it is.
8304 if ((N0.getOpcode() == ISD::ZERO_EXTEND ||
8305 N0.getOpcode() == ISD::ANY_EXTEND ||
8306 N0.getOpcode() == ISD::SIGN_EXTEND) &&
8307 N0.getOperand(0).getOpcode() == ISD::SHL) {
8308 SDValue N0Op0 = N0.getOperand(0);
8309 SDValue InnerShiftAmt = N0Op0.getOperand(1);
8310 EVT InnerVT = N0Op0.getValueType();
8311 uint64_t InnerBitwidth = InnerVT.getScalarSizeInBits();
8312
8313 auto MatchOutOfRange = [OpSizeInBits, InnerBitwidth](ConstantSDNode *LHS,
8314 ConstantSDNode *RHS) {
8315 APInt c1 = LHS->getAPIntValue();
8316 APInt c2 = RHS->getAPIntValue();
8317 zeroExtendToMatch(c1, c2, 1 /* Overflow Bit */);
8318 return c2.uge(OpSizeInBits - InnerBitwidth) &&
8319 (c1 + c2).uge(OpSizeInBits);
8320 };
8321 if (ISD::matchBinaryPredicate(InnerShiftAmt, N1, MatchOutOfRange,
8322 /*AllowUndefs*/ false,
8323 /*AllowTypeMismatch*/ true))
8324 return DAG.getConstant(0, SDLoc(N), VT);
8325
8326 auto MatchInRange = [OpSizeInBits, InnerBitwidth](ConstantSDNode *LHS,
8327 ConstantSDNode *RHS) {
8328 APInt c1 = LHS->getAPIntValue();
8329 APInt c2 = RHS->getAPIntValue();
8330 zeroExtendToMatch(c1, c2, 1 /* Overflow Bit */);
8331 return c2.uge(OpSizeInBits - InnerBitwidth) &&
8332 (c1 + c2).ult(OpSizeInBits);
8333 };
8334 if (ISD::matchBinaryPredicate(InnerShiftAmt, N1, MatchInRange,
8335 /*AllowUndefs*/ false,
8336 /*AllowTypeMismatch*/ true)) {
8337 SDLoc DL(N);
8338 SDValue Ext = DAG.getNode(N0.getOpcode(), DL, VT, N0Op0.getOperand(0));
8339 SDValue Sum = DAG.getZExtOrTrunc(InnerShiftAmt, DL, ShiftVT);
8340 Sum = DAG.getNode(ISD::ADD, DL, ShiftVT, Sum, N1);
8341 return DAG.getNode(ISD::SHL, DL, VT, Ext, Sum);
8342 }
8343 }
8344
8345 // fold (shl (zext (srl x, C)), C) -> (zext (shl (srl x, C), C))
8346 // Only fold this if the inner zext has no other uses to avoid increasing
8347 // the total number of instructions.
8348 if (N0.getOpcode() == ISD::ZERO_EXTEND && N0.hasOneUse() &&
8349 N0.getOperand(0).getOpcode() == ISD::SRL) {
8350 SDValue N0Op0 = N0.getOperand(0);
8351 SDValue InnerShiftAmt = N0Op0.getOperand(1);
8352
8353 auto MatchEqual = [VT](ConstantSDNode *LHS, ConstantSDNode *RHS) {
8354 APInt c1 = LHS->getAPIntValue();
8355 APInt c2 = RHS->getAPIntValue();
8356 zeroExtendToMatch(c1, c2);
8357 return c1.ult(VT.getScalarSizeInBits()) && (c1 == c2);
8358 };
8359 if (ISD::matchBinaryPredicate(InnerShiftAmt, N1, MatchEqual,
8360 /*AllowUndefs*/ false,
8361 /*AllowTypeMismatch*/ true)) {
8362 SDLoc DL(N);
8363 EVT InnerShiftAmtVT = N0Op0.getOperand(1).getValueType();
8364 SDValue NewSHL = DAG.getZExtOrTrunc(N1, DL, InnerShiftAmtVT);
8365 NewSHL = DAG.getNode(ISD::SHL, DL, N0Op0.getValueType(), N0Op0, NewSHL);
8366 AddToWorklist(NewSHL.getNode());
8367 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N0), VT, NewSHL);
8368 }
8369 }
8370
8371 // fold (shl (sr[la] exact X, C1), C2) -> (shl X, (C2-C1)) if C1 <= C2
8372 // fold (shl (sr[la] exact X, C1), C2) -> (sr[la] X, (C2-C1)) if C1 > C2
8373 // TODO - support non-uniform vector shift amounts.
8374 if (N1C && (N0.getOpcode() == ISD::SRL || N0.getOpcode() == ISD::SRA) &&
8375 N0->getFlags().hasExact()) {
8376 if (ConstantSDNode *N0C1 = isConstOrConstSplat(N0.getOperand(1))) {
8377 uint64_t C1 = N0C1->getZExtValue();
8378 uint64_t C2 = N1C->getZExtValue();
8379 SDLoc DL(N);
8380 if (C1 <= C2)
8381 return DAG.getNode(ISD::SHL, DL, VT, N0.getOperand(0),
8382 DAG.getConstant(C2 - C1, DL, ShiftVT));
8383 return DAG.getNode(N0.getOpcode(), DL, VT, N0.getOperand(0),
8384 DAG.getConstant(C1 - C2, DL, ShiftVT));
8385 }
8386 }
8387
8388 // fold (shl (srl x, c1), c2) -> (and (shl x, (sub c2, c1), MASK) or
8389 // (and (srl x, (sub c1, c2), MASK)
8390 // Only fold this if the inner shift has no other uses -- if it does, folding
8391 // this will increase the total number of instructions.
8392 // TODO - drop hasOneUse requirement if c1 == c2?
8393 // TODO - support non-uniform vector shift amounts.
8394 if (N1C && N0.getOpcode() == ISD::SRL && N0.hasOneUse() &&
8395 TLI.shouldFoldConstantShiftPairToMask(N, Level)) {
8396 if (ConstantSDNode *N0C1 = isConstOrConstSplat(N0.getOperand(1))) {
8397 if (N0C1->getAPIntValue().ult(OpSizeInBits)) {
8398 uint64_t c1 = N0C1->getZExtValue();
8399 uint64_t c2 = N1C->getZExtValue();
8400 APInt Mask = APInt::getHighBitsSet(OpSizeInBits, OpSizeInBits - c1);
8401 SDValue Shift;
8402 if (c2 > c1) {
8403 Mask <<= c2 - c1;
8404 SDLoc DL(N);
8405 Shift = DAG.getNode(ISD::SHL, DL, VT, N0.getOperand(0),
8406 DAG.getConstant(c2 - c1, DL, ShiftVT));
8407 } else {
8408 Mask.lshrInPlace(c1 - c2);
8409 SDLoc DL(N);
8410 Shift = DAG.getNode(ISD::SRL, DL, VT, N0.getOperand(0),
8411 DAG.getConstant(c1 - c2, DL, ShiftVT));
8412 }
8413 SDLoc DL(N0);
8414 return DAG.getNode(ISD::AND, DL, VT, Shift,
8415 DAG.getConstant(Mask, DL, VT));
8416 }
8417 }
8418 }
8419
8420 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
8421 if (N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1) &&
8422 isConstantOrConstantVector(N1, /* No Opaques */ true)) {
8423 SDLoc DL(N);
8424 SDValue AllBits = DAG.getAllOnesConstant(DL, VT);
8425 SDValue HiBitsMask = DAG.getNode(ISD::SHL, DL, VT, AllBits, N1);
8426 return DAG.getNode(ISD::AND, DL, VT, N0.getOperand(0), HiBitsMask);
8427 }
8428
8429 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1 << c2)
8430 // fold (shl (or x, c1), c2) -> (or (shl x, c2), c1 << c2)
8431 // Variant of version done on multiply, except mul by a power of 2 is turned
8432 // into a shift.
8433 if ((N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::OR) &&
8434 N0.getNode()->hasOneUse() &&
8435 isConstantOrConstantVector(N1, /* No Opaques */ true) &&
8436 isConstantOrConstantVector(N0.getOperand(1), /* No Opaques */ true) &&
8437 TLI.isDesirableToCommuteWithShift(N, Level)) {
8438 SDValue Shl0 = DAG.getNode(ISD::SHL, SDLoc(N0), VT, N0.getOperand(0), N1);
8439 SDValue Shl1 = DAG.getNode(ISD::SHL, SDLoc(N1), VT, N0.getOperand(1), N1);
8440 AddToWorklist(Shl0.getNode());
8441 AddToWorklist(Shl1.getNode());
8442 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, Shl0, Shl1);
8443 }
8444
8445 // fold (shl (mul x, c1), c2) -> (mul x, c1 << c2)
8446 if (N0.getOpcode() == ISD::MUL && N0.getNode()->hasOneUse() &&
8447 isConstantOrConstantVector(N1, /* No Opaques */ true) &&
8448 isConstantOrConstantVector(N0.getOperand(1), /* No Opaques */ true)) {
8449 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(N1), VT, N0.getOperand(1), N1);
8450 if (isConstantOrConstantVector(Shl))
8451 return DAG.getNode(ISD::MUL, SDLoc(N), VT, N0.getOperand(0), Shl);
8452 }
8453
8454 if (N1C && !N1C->isOpaque())
8455 if (SDValue NewSHL = visitShiftByConstant(N))
8456 return NewSHL;
8457
8458 // Fold (shl (vscale * C0), C1) to (vscale * (C0 << C1)).
8459 if (N0.getOpcode() == ISD::VSCALE)
8460 if (ConstantSDNode *NC1 = isConstOrConstSplat(N->getOperand(1))) {
8461 const APInt &C0 = N0.getConstantOperandAPInt(0);
8462 const APInt &C1 = NC1->getAPIntValue();
8463 return DAG.getVScale(SDLoc(N), VT, C0 << C1);
8464 }
8465
8466 // Fold (shl step_vector(C0), C1) to (step_vector(C0 << C1)).
8467 APInt ShlVal;
8468 if (N0.getOpcode() == ISD::STEP_VECTOR)
8469 if (ISD::isConstantSplatVector(N1.getNode(), ShlVal)) {
8470 const APInt &C0 = N0.getConstantOperandAPInt(0);
8471 if (ShlVal.ult(C0.getBitWidth())) {
8472 APInt NewStep = C0 << ShlVal;
8473 return DAG.getStepVector(SDLoc(N), VT, NewStep);
8474 }
8475 }
8476
8477 return SDValue();
8478}
8479
8480// Transform a right shift of a multiply into a multiply-high.
8481// Examples:
8482// (srl (mul (zext i32:$a to i64), (zext i32:$a to i64)), 32) -> (mulhu $a, $b)
8483// (sra (mul (sext i32:$a to i64), (sext i32:$a to i64)), 32) -> (mulhs $a, $b)
8484static SDValue combineShiftToMULH(SDNode *N, SelectionDAG &DAG,
8485 const TargetLowering &TLI) {
8486 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&(static_cast <bool> ((N->getOpcode() == ISD::SRL || N
->getOpcode() == ISD::SRA) && "SRL or SRA node is required here!"
) ? void (0) : __assert_fail ("(N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) && \"SRL or SRA node is required here!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 8487, __extension__ __PRETTY_FUNCTION__))
8487 "SRL or SRA node is required here!")(static_cast <bool> ((N->getOpcode() == ISD::SRL || N
->getOpcode() == ISD::SRA) && "SRL or SRA node is required here!"
) ? void (0) : __assert_fail ("(N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) && \"SRL or SRA node is required here!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 8487, __extension__ __PRETTY_FUNCTION__))
;
8488
8489 // Check the shift amount. Proceed with the transformation if the shift
8490 // amount is constant.
8491 ConstantSDNode *ShiftAmtSrc = isConstOrConstSplat(N->getOperand(1));
8492 if (!ShiftAmtSrc)
8493 return SDValue();
8494
8495 SDLoc DL(N);
8496
8497 // The operation feeding into the shift must be a multiply.
8498 SDValue ShiftOperand = N->getOperand(0);
8499 if (ShiftOperand.getOpcode() != ISD::MUL)
8500 return SDValue();
8501
8502 // Both operands must be equivalent extend nodes.
8503 SDValue LeftOp = ShiftOperand.getOperand(0);
8504 SDValue RightOp = ShiftOperand.getOperand(1);
8505 bool IsSignExt = LeftOp.getOpcode() == ISD::SIGN_EXTEND;
8506 bool IsZeroExt = LeftOp.getOpcode() == ISD::ZERO_EXTEND;
8507
8508 if ((!(IsSignExt || IsZeroExt)) || LeftOp.getOpcode() != RightOp.getOpcode())
8509 return SDValue();
8510
8511 EVT WideVT1 = LeftOp.getValueType();
8512 EVT WideVT2 = RightOp.getValueType();
8513 (void)WideVT2;
8514 // Proceed with the transformation if the wide types match.
8515 assert((WideVT1 == WideVT2) &&(static_cast <bool> ((WideVT1 == WideVT2) && "Cannot have a multiply node with two different operand types."
) ? void (0) : __assert_fail ("(WideVT1 == WideVT2) && \"Cannot have a multiply node with two different operand types.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 8516, __extension__ __PRETTY_FUNCTION__))
8516 "Cannot have a multiply node with two different operand types.")(static_cast <bool> ((WideVT1 == WideVT2) && "Cannot have a multiply node with two different operand types."
) ? void (0) : __assert_fail ("(WideVT1 == WideVT2) && \"Cannot have a multiply node with two different operand types.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 8516, __extension__ __PRETTY_FUNCTION__))
;
8517
8518 EVT NarrowVT = LeftOp.getOperand(0).getValueType();
8519 // Check that the two extend nodes are the same type.
8520 if (NarrowVT != RightOp.getOperand(0).getValueType())
8521 return SDValue();
8522
8523 // Proceed with the transformation if the wide type is twice as large
8524 // as the narrow type.
8525 unsigned NarrowVTSize = NarrowVT.getScalarSizeInBits();
8526 if (WideVT1.getScalarSizeInBits() != 2 * NarrowVTSize)
8527 return SDValue();
8528
8529 // Check the shift amount with the narrow type size.
8530 // Proceed with the transformation if the shift amount is the width
8531 // of the narrow type.
8532 unsigned ShiftAmt = ShiftAmtSrc->getZExtValue();
8533 if (ShiftAmt != NarrowVTSize)
8534 return SDValue();
8535
8536 // If the operation feeding into the MUL is a sign extend (sext),
8537 // we use mulhs. Othewise, zero extends (zext) use mulhu.
8538 unsigned MulhOpcode = IsSignExt ? ISD::MULHS : ISD::MULHU;
8539
8540 // Combine to mulh if mulh is legal/custom for the narrow type on the target.
8541 if (!TLI.isOperationLegalOrCustom(MulhOpcode, NarrowVT))
8542 return SDValue();
8543
8544 SDValue Result = DAG.getNode(MulhOpcode, DL, NarrowVT, LeftOp.getOperand(0),
8545 RightOp.getOperand(0));
8546 return (N->getOpcode() == ISD::SRA ? DAG.getSExtOrTrunc(Result, DL, WideVT1)
8547 : DAG.getZExtOrTrunc(Result, DL, WideVT1));
8548}
8549
8550SDValue DAGCombiner::visitSRA(SDNode *N) {
8551 SDValue N0 = N->getOperand(0);
8552 SDValue N1 = N->getOperand(1);
8553 if (SDValue V = DAG.simplifyShift(N0, N1))
8554 return V;
8555
8556 EVT VT = N0.getValueType();
8557 unsigned OpSizeInBits = VT.getScalarSizeInBits();
8558
8559 // Arithmetic shifting an all-sign-bit value is a no-op.
8560 // fold (sra 0, x) -> 0
8561 // fold (sra -1, x) -> -1
8562 if (DAG.ComputeNumSignBits(N0) == OpSizeInBits)
8563 return N0;
8564
8565 // fold vector ops
8566 if (VT.isVector())
8567 if (SDValue FoldedVOp = SimplifyVBinOp(N))
8568 return FoldedVOp;
8569
8570 ConstantSDNode *N1C = isConstOrConstSplat(N1);
8571
8572 // fold (sra c1, c2) -> (sra c1, c2)
8573 if (SDValue C = DAG.FoldConstantArithmetic(ISD::SRA, SDLoc(N), VT, {N0, N1}))
8574 return C;
8575
8576 if (SDValue NewSel = foldBinOpIntoSelect(N))
8577 return NewSel;
8578
8579 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
8580 // sext_inreg.
8581 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
8582 unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue();
8583 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits);
8584 if (VT.isVector())
8585 ExtVT = EVT::getVectorVT(*DAG.getContext(), ExtVT,
8586 VT.getVectorElementCount());
8587 if (!LegalOperations ||
8588 TLI.getOperationAction(ISD::SIGN_EXTEND_INREG, ExtVT) ==
8589 TargetLowering::Legal)
8590 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
8591 N0.getOperand(0), DAG.getValueType(ExtVT));
8592 // Even if we can't convert to sext_inreg, we might be able to remove
8593 // this shift pair if the input is already sign extended.
8594 if (DAG.ComputeNumSignBits(N0.getOperand(0)) > N1C->getZExtValue())
8595 return N0.getOperand(0);
8596 }
8597
8598 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
8599 // clamp (add c1, c2) to max shift.
8600 if (N0.getOpcode() == ISD::SRA) {
8601 SDLoc DL(N);
8602 EVT ShiftVT = N1.getValueType();
8603 EVT ShiftSVT = ShiftVT.getScalarType();
8604 SmallVector<SDValue, 16> ShiftValues;
8605
8606 auto SumOfShifts = [&](ConstantSDNode *LHS, ConstantSDNode *RHS) {
8607 APInt c1 = LHS->getAPIntValue();
8608 APInt c2 = RHS->getAPIntValue();
8609 zeroExtendToMatch(c1, c2, 1 /* Overflow Bit */);
8610 APInt Sum = c1 + c2;
8611 unsigned ShiftSum =
8612 Sum.uge(OpSizeInBits) ? (OpSizeInBits - 1) : Sum.getZExtValue();
8613 ShiftValues.push_back(DAG.getConstant(ShiftSum, DL, ShiftSVT));
8614 return true;
8615 };
8616 if (ISD::matchBinaryPredicate(N1, N0.getOperand(1), SumOfShifts)) {
8617 SDValue ShiftValue;
8618 if (N1.getOpcode() == ISD::BUILD_VECTOR)
8619 ShiftValue = DAG.getBuildVector(ShiftVT, DL, ShiftValues);
8620 else if (N1.getOpcode() == ISD::SPLAT_VECTOR) {
8621 assert(ShiftValues.size() == 1 &&(static_cast <bool> (ShiftValues.size() == 1 &&
"Expected matchBinaryPredicate to return one element for " "SPLAT_VECTORs"
) ? void (0) : __assert_fail ("ShiftValues.size() == 1 && \"Expected matchBinaryPredicate to return one element for \" \"SPLAT_VECTORs\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 8623, __extension__ __PRETTY_FUNCTION__))
8622 "Expected matchBinaryPredicate to return one element for "(static_cast <bool> (ShiftValues.size() == 1 &&
"Expected matchBinaryPredicate to return one element for " "SPLAT_VECTORs"
) ? void (0) : __assert_fail ("ShiftValues.size() == 1 && \"Expected matchBinaryPredicate to return one element for \" \"SPLAT_VECTORs\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 8623, __extension__ __PRETTY_FUNCTION__))
8623 "SPLAT_VECTORs")(static_cast <bool> (ShiftValues.size() == 1 &&
"Expected matchBinaryPredicate to return one element for " "SPLAT_VECTORs"
) ? void (0) : __assert_fail ("ShiftValues.size() == 1 && \"Expected matchBinaryPredicate to return one element for \" \"SPLAT_VECTORs\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 8623, __extension__ __PRETTY_FUNCTION__))
;
8624 ShiftValue = DAG.getSplatVector(ShiftVT, DL, ShiftValues[0]);
8625 } else
8626 ShiftValue = ShiftValues[0];
8627 return DAG.getNode(ISD::SRA, DL, VT, N0.getOperand(0), ShiftValue);
8628 }
8629 }
8630
8631 // fold (sra (shl X, m), (sub result_size, n))
8632 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
8633 // result_size - n != m.
8634 // If truncate is free for the target sext(shl) is likely to result in better
8635 // code.
8636 if (N0.getOpcode() == ISD::SHL && N1C) {
8637 // Get the two constanst of the shifts, CN0 = m, CN = n.
8638 const ConstantSDNode *N01C = isConstOrConstSplat(N0.getOperand(1));
8639 if (N01C) {
8640 LLVMContext &Ctx = *DAG.getContext();
8641 // Determine what the truncate's result bitsize and type would be.
8642 EVT TruncVT = EVT::getIntegerVT(Ctx, OpSizeInBits - N1C->getZExtValue());
8643
8644 if (VT.isVector())
8645 TruncVT = EVT::getVectorVT(Ctx, TruncVT, VT.getVectorElementCount());
8646
8647 // Determine the residual right-shift amount.
8648 int ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
8649
8650 // If the shift is not a no-op (in which case this should be just a sign
8651 // extend already), the truncated to type is legal, sign_extend is legal
8652 // on that type, and the truncate to that type is both legal and free,
8653 // perform the transform.
8654 if ((ShiftAmt > 0) &&
8655 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
8656 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
8657 TLI.isTruncateFree(VT, TruncVT)) {
8658 SDLoc DL(N);
8659 SDValue Amt = DAG.getConstant(ShiftAmt, DL,
8660 getShiftAmountTy(N0.getOperand(0).getValueType()));
8661 SDValue Shift = DAG.getNode(ISD::SRL, DL, VT,
8662 N0.getOperand(0), Amt);
8663 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, TruncVT,
8664 Shift);
8665 return DAG.getNode(ISD::SIGN_EXTEND, DL,
8666 N->getValueType(0), Trunc);
8667 }
8668 }
8669 }
8670
8671 // We convert trunc/ext to opposing shifts in IR, but casts may be cheaper.
8672 // sra (add (shl X, N1C), AddC), N1C -->
8673 // sext (add (trunc X to (width - N1C)), AddC')
8674 if (N0.getOpcode() == ISD::ADD && N0.hasOneUse() && N1C &&
8675 N0.getOperand(0).getOpcode() == ISD::SHL &&
8676 N0.getOperand(0).getOperand(1) == N1 && N0.getOperand(0).hasOneUse()) {
8677 if (ConstantSDNode *AddC = isConstOrConstSplat(N0.getOperand(1))) {
8678 SDValue Shl = N0.getOperand(0);
8679 // Determine what the truncate's type would be and ask the target if that
8680 // is a free operation.
8681 LLVMContext &Ctx = *DAG.getContext();
8682 unsigned ShiftAmt = N1C->getZExtValue();
8683 EVT TruncVT = EVT::getIntegerVT(Ctx, OpSizeInBits - ShiftAmt);
8684 if (VT.isVector())
8685 TruncVT = EVT::getVectorVT(Ctx, TruncVT, VT.getVectorElementCount());
8686
8687 // TODO: The simple type check probably belongs in the default hook
8688 // implementation and/or target-specific overrides (because
8689 // non-simple types likely require masking when legalized), but that
8690 // restriction may conflict with other transforms.
8691 if (TruncVT.isSimple() && isTypeLegal(TruncVT) &&
8692 TLI.isTruncateFree(VT, TruncVT)) {
8693 SDLoc DL(N);
8694 SDValue Trunc = DAG.getZExtOrTrunc(Shl.getOperand(0), DL, TruncVT);
8695 SDValue ShiftC = DAG.getConstant(AddC->getAPIntValue().lshr(ShiftAmt).
8696 trunc(TruncVT.getScalarSizeInBits()), DL, TruncVT);
8697 SDValue Add = DAG.getNode(ISD::ADD, DL, TruncVT, Trunc, ShiftC);
8698 return DAG.getSExtOrTrunc(Add, DL, VT);
8699 }
8700 }
8701 }
8702
8703 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
8704 if (N1.getOpcode() == ISD::TRUNCATE &&
8705 N1.getOperand(0).getOpcode() == ISD::AND) {
8706 if (SDValue NewOp1 = distributeTruncateThroughAnd(N1.getNode()))
8707 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0, NewOp1);
8708 }
8709
8710 // fold (sra (trunc (sra x, c1)), c2) -> (trunc (sra x, c1 + c2))
8711 // fold (sra (trunc (srl x, c1)), c2) -> (trunc (sra x, c1 + c2))
8712 // if c1 is equal to the number of bits the trunc removes
8713 // TODO - support non-uniform vector shift amounts.
8714 if (N0.getOpcode() == ISD::TRUNCATE &&
8715 (N0.getOperand(0).getOpcode() == ISD::SRL ||
8716 N0.getOperand(0).getOpcode() == ISD::SRA) &&
8717 N0.getOperand(0).hasOneUse() &&
8718 N0.getOperand(0).getOperand(1).hasOneUse() && N1C) {
8719 SDValue N0Op0 = N0.getOperand(0);
8720 if (ConstantSDNode *LargeShift = isConstOrConstSplat(N0Op0.getOperand(1))) {
8721 EVT LargeVT = N0Op0.getValueType();
8722 unsigned TruncBits = LargeVT.getScalarSizeInBits() - OpSizeInBits;
8723 if (LargeShift->getAPIntValue() == TruncBits) {
8724 SDLoc DL(N);
8725 SDValue Amt = DAG.getConstant(N1C->getZExtValue() + TruncBits, DL,
8726 getShiftAmountTy(LargeVT));
8727 SDValue SRA =
8728 DAG.getNode(ISD::SRA, DL, LargeVT, N0Op0.getOperand(0), Amt);
8729 return DAG.getNode(ISD::TRUNCATE, DL, VT, SRA);
8730 }
8731 }
8732 }
8733
8734 // Simplify, based on bits shifted out of the LHS.
8735 if (SimplifyDemandedBits(SDValue(N, 0)))
8736 return SDValue(N, 0);
8737
8738 // If the sign bit is known to be zero, switch this to a SRL.
8739 if (DAG.SignBitIsZero(N0))
8740 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, N1);
8741
8742 if (N1C && !N1C->isOpaque())
8743 if (SDValue NewSRA = visitShiftByConstant(N))
8744 return NewSRA;
8745
8746 // Try to transform this shift into a multiply-high if
8747 // it matches the appropriate pattern detected in combineShiftToMULH.
8748 if (SDValue MULH = combineShiftToMULH(N, DAG, TLI))
8749 return MULH;
8750
8751 return SDValue();
8752}
8753
8754SDValue DAGCombiner::visitSRL(SDNode *N) {
8755 SDValue N0 = N->getOperand(0);
8756 SDValue N1 = N->getOperand(1);
8757 if (SDValue V = DAG.simplifyShift(N0, N1))
8758 return V;
8759
8760 EVT VT = N0.getValueType();
8761 unsigned OpSizeInBits = VT.getScalarSizeInBits();
8762
8763 // fold vector ops
8764 if (VT.isVector())
8765 if (SDValue FoldedVOp = SimplifyVBinOp(N))
8766 return FoldedVOp;
8767
8768 ConstantSDNode *N1C = isConstOrConstSplat(N1);
8769
8770 // fold (srl c1, c2) -> c1 >>u c2
8771 if (SDValue C = DAG.FoldConstantArithmetic(ISD::SRL, SDLoc(N), VT, {N0, N1}))
8772 return C;
8773
8774 if (SDValue NewSel = foldBinOpIntoSelect(N))
8775 return NewSel;
8776
8777 // if (srl x, c) is known to be zero, return 0
8778 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
8779 APInt::getAllOnesValue(OpSizeInBits)))
8780 return DAG.getConstant(0, SDLoc(N), VT);
8781
8782 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
8783 if (N0.getOpcode() == ISD::SRL) {
8784 auto MatchOutOfRange = [OpSizeInBits](ConstantSDNode *LHS,
8785 ConstantSDNode *RHS) {
8786 APInt c1 = LHS->getAPIntValue();
8787 APInt c2 = RHS->getAPIntValue();
8788 zeroExtendToMatch(c1, c2, 1 /* Overflow Bit */);
8789 return (c1 + c2).uge(OpSizeInBits);
8790 };
8791 if (ISD::matchBinaryPredicate(N1, N0.getOperand(1), MatchOutOfRange))
8792 return DAG.getConstant(0, SDLoc(N), VT);
8793
8794 auto MatchInRange = [OpSizeInBits](ConstantSDNode *LHS,
8795 ConstantSDNode *RHS) {
8796 APInt c1 = LHS->getAPIntValue();
8797 APInt c2 = RHS->getAPIntValue();
8798 zeroExtendToMatch(c1, c2, 1 /* Overflow Bit */);
8799 return (c1 + c2).ult(OpSizeInBits);
8800 };
8801 if (ISD::matchBinaryPredicate(N1, N0.getOperand(1), MatchInRange)) {
8802 SDLoc DL(N);
8803 EVT ShiftVT = N1.getValueType();
8804 SDValue Sum = DAG.getNode(ISD::ADD, DL, ShiftVT, N1, N0.getOperand(1));
8805 return DAG.getNode(ISD::SRL, DL, VT, N0.getOperand(0), Sum);
8806 }
8807 }
8808
8809 if (N1C && N0.getOpcode() == ISD::TRUNCATE &&
8810 N0.getOperand(0).getOpcode() == ISD::SRL) {
8811 SDValue InnerShift = N0.getOperand(0);
8812 // TODO - support non-uniform vector shift amounts.
8813 if (auto *N001C = isConstOrConstSplat(InnerShift.getOperand(1))) {
8814 uint64_t c1 = N001C->getZExtValue();
8815 uint64_t c2 = N1C->getZExtValue();
8816 EVT InnerShiftVT = InnerShift.getValueType();
8817 EVT ShiftAmtVT = InnerShift.getOperand(1).getValueType();
8818 uint64_t InnerShiftSize = InnerShiftVT.getScalarSizeInBits();
8819 // srl (trunc (srl x, c1)), c2 --> 0 or (trunc (srl x, (add c1, c2)))
8820 // This is only valid if the OpSizeInBits + c1 = size of inner shift.
8821 if (c1 + OpSizeInBits == InnerShiftSize) {
8822 SDLoc DL(N);
8823 if (c1 + c2 >= InnerShiftSize)
8824 return DAG.getConstant(0, DL, VT);
8825 SDValue NewShiftAmt = DAG.getConstant(c1 + c2, DL, ShiftAmtVT);
8826 SDValue NewShift = DAG.getNode(ISD::SRL, DL, InnerShiftVT,
8827 InnerShift.getOperand(0), NewShiftAmt);
8828 return DAG.getNode(ISD::TRUNCATE, DL, VT, NewShift);
8829 }
8830 // In the more general case, we can clear the high bits after the shift:
8831 // srl (trunc (srl x, c1)), c2 --> trunc (and (srl x, (c1+c2)), Mask)
8832 if (N0.hasOneUse() && InnerShift.hasOneUse() &&
8833 c1 + c2 < InnerShiftSize) {
8834 SDLoc DL(N);
8835 SDValue NewShiftAmt = DAG.getConstant(c1 + c2, DL, ShiftAmtVT);
8836 SDValue NewShift = DAG.getNode(ISD::SRL, DL, InnerShiftVT,
8837 InnerShift.getOperand(0), NewShiftAmt);
8838 SDValue Mask = DAG.getConstant(APInt::getLowBitsSet(InnerShiftSize,
8839 OpSizeInBits - c2),
8840 DL, InnerShiftVT);
8841 SDValue And = DAG.getNode(ISD::AND, DL, InnerShiftVT, NewShift, Mask);
8842 return DAG.getNode(ISD::TRUNCATE, DL, VT, And);
8843 }
8844 }
8845 }
8846
8847 // fold (srl (shl x, c), c) -> (and x, cst2)
8848 // TODO - (srl (shl x, c1), c2).
8849 if (N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 &&
8850 isConstantOrConstantVector(N1, /* NoOpaques */ true)) {
8851 SDLoc DL(N);
8852 SDValue Mask =
8853 DAG.getNode(ISD::SRL, DL, VT, DAG.getAllOnesConstant(DL, VT), N1);
8854 AddToWorklist(Mask.getNode());
8855 return DAG.getNode(ISD::AND, DL, VT, N0.getOperand(0), Mask);
8856 }
8857
8858 // fold (srl (anyextend x), c) -> (and (anyextend (srl x, c)), mask)
8859 // TODO - support non-uniform vector shift amounts.
8860 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
8861 // Shifting in all undef bits?
8862 EVT SmallVT = N0.getOperand(0).getValueType();
8863 unsigned BitSize = SmallVT.getScalarSizeInBits();
8864 if (N1C->getAPIntValue().uge(BitSize))
8865 return DAG.getUNDEF(VT);
8866
8867 if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) {
8868 uint64_t ShiftAmt = N1C->getZExtValue();
8869 SDLoc DL0(N0);
8870 SDValue SmallShift = DAG.getNode(ISD::SRL, DL0, SmallVT,
8871 N0.getOperand(0),
8872 DAG.getConstant(ShiftAmt, DL0,
8873 getShiftAmountTy(SmallVT)));
8874 AddToWorklist(SmallShift.getNode());
8875 APInt Mask = APInt::getLowBitsSet(OpSizeInBits, OpSizeInBits - ShiftAmt);
8876 SDLoc DL(N);
8877 return DAG.getNode(ISD::AND, DL, VT,
8878 DAG.getNode(ISD::ANY_EXTEND, DL, VT, SmallShift),
8879 DAG.getConstant(Mask, DL, VT));
8880 }
8881 }
8882
8883 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
8884 // bit, which is unmodified by sra.
8885 if (N1C && N1C->getAPIntValue() == (OpSizeInBits - 1)) {
8886 if (N0.getOpcode() == ISD::SRA)
8887 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0), N1);
8888 }
8889
8890 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
8891 if (N1C && N0.getOpcode() == ISD::CTLZ &&
8892 N1C->getAPIntValue() == Log2_32(OpSizeInBits)) {
8893 KnownBits Known = DAG.computeKnownBits(N0.getOperand(0));
8894
8895 // If any of the input bits are KnownOne, then the input couldn't be all
8896 // zeros, thus the result of the srl will always be zero.
8897 if (Known.One.getBoolValue()) return DAG.getConstant(0, SDLoc(N0), VT);
8898
8899 // If all of the bits input the to ctlz node are known to be zero, then
8900 // the result of the ctlz is "32" and the result of the shift is one.
8901 APInt UnknownBits = ~Known.Zero;
8902 if (UnknownBits == 0) return DAG.getConstant(1, SDLoc(N0), VT);
8903
8904 // Otherwise, check to see if there is exactly one bit input to the ctlz.
8905 if (UnknownBits.isPowerOf2()) {
8906 // Okay, we know that only that the single bit specified by UnknownBits
8907 // could be set on input to the CTLZ node. If this bit is set, the SRL
8908 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
8909 // to an SRL/XOR pair, which is likely to simplify more.
8910 unsigned ShAmt = UnknownBits.countTrailingZeros();
8911 SDValue Op = N0.getOperand(0);
8912
8913 if (ShAmt) {
8914 SDLoc DL(N0);
8915 Op = DAG.getNode(ISD::SRL, DL, VT, Op,
8916 DAG.getConstant(ShAmt, DL,
8917 getShiftAmountTy(Op.getValueType())));
8918 AddToWorklist(Op.getNode());
8919 }
8920
8921 SDLoc DL(N);
8922 return DAG.getNode(ISD::XOR, DL, VT,
8923 Op, DAG.getConstant(1, DL, VT));
8924 }
8925 }
8926
8927 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
8928 if (N1.getOpcode() == ISD::TRUNCATE &&
8929 N1.getOperand(0).getOpcode() == ISD::AND) {
8930 if (SDValue NewOp1 = distributeTruncateThroughAnd(N1.getNode()))
8931 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, NewOp1);
8932 }
8933
8934 // fold operands of srl based on knowledge that the low bits are not
8935 // demanded.
8936 if (SimplifyDemandedBits(SDValue(N, 0)))
8937 return SDValue(N, 0);
8938
8939 if (N1C && !N1C->isOpaque())
8940 if (SDValue NewSRL = visitShiftByConstant(N))
8941 return NewSRL;
8942
8943 // Attempt to convert a srl of a load into a narrower zero-extending load.
8944 if (SDValue NarrowLoad = ReduceLoadWidth(N))
8945 return NarrowLoad;
8946
8947 // Here is a common situation. We want to optimize:
8948 //
8949 // %a = ...
8950 // %b = and i32 %a, 2
8951 // %c = srl i32 %b, 1
8952 // brcond i32 %c ...
8953 //
8954 // into
8955 //
8956 // %a = ...
8957 // %b = and %a, 2
8958 // %c = setcc eq %b, 0
8959 // brcond %c ...
8960 //
8961 // However when after the source operand of SRL is optimized into AND, the SRL
8962 // itself may not be optimized further. Look for it and add the BRCOND into
8963 // the worklist.
8964 if (N->hasOneUse()) {
8965 SDNode *Use = *N->use_begin();
8966 if (Use->getOpcode() == ISD::BRCOND)
8967 AddToWorklist(Use);
8968 else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) {
8969 // Also look pass the truncate.
8970 Use = *Use->use_begin();
8971 if (Use->getOpcode() == ISD::BRCOND)
8972 AddToWorklist(Use);
8973 }
8974 }
8975
8976 // Try to transform this shift into a multiply-high if
8977 // it matches the appropriate pattern detected in combineShiftToMULH.
8978 if (SDValue MULH = combineShiftToMULH(N, DAG, TLI))
8979 return MULH;
8980
8981 return SDValue();
8982}
8983
8984SDValue DAGCombiner::visitFunnelShift(SDNode *N) {
8985 EVT VT = N->getValueType(0);
8986 SDValue N0 = N->getOperand(0);
8987 SDValue N1 = N->getOperand(1);
8988 SDValue N2 = N->getOperand(2);
8989 bool IsFSHL = N->getOpcode() == ISD::FSHL;
8990 unsigned BitWidth = VT.getScalarSizeInBits();
8991
8992 // fold (fshl N0, N1, 0) -> N0
8993 // fold (fshr N0, N1, 0) -> N1
8994 if (isPowerOf2_32(BitWidth))
8995 if (DAG.MaskedValueIsZero(
8996 N2, APInt(N2.getScalarValueSizeInBits(), BitWidth - 1)))
8997 return IsFSHL ? N0 : N1;
8998
8999 auto IsUndefOrZero = [](SDValue V) {
9000 return V.isUndef() || isNullOrNullSplat(V, /*AllowUndefs*/ true);
9001 };
9002
9003 // TODO - support non-uniform vector shift amounts.
9004 if (ConstantSDNode *Cst = isConstOrConstSplat(N2)) {
9005 EVT ShAmtTy = N2.getValueType();
9006
9007 // fold (fsh* N0, N1, c) -> (fsh* N0, N1, c % BitWidth)
9008 if (Cst->getAPIntValue().uge(BitWidth)) {
9009 uint64_t RotAmt = Cst->getAPIntValue().urem(BitWidth);
9010 return DAG.getNode(N->getOpcode(), SDLoc(N), VT, N0, N1,
9011 DAG.getConstant(RotAmt, SDLoc(N), ShAmtTy));
9012 }
9013
9014 unsigned ShAmt = Cst->getZExtValue();
9015 if (ShAmt == 0)
9016 return IsFSHL ? N0 : N1;
9017
9018 // fold fshl(undef_or_zero, N1, C) -> lshr(N1, BW-C)
9019 // fold fshr(undef_or_zero, N1, C) -> lshr(N1, C)
9020 // fold fshl(N0, undef_or_zero, C) -> shl(N0, C)
9021 // fold fshr(N0, undef_or_zero, C) -> shl(N0, BW-C)
9022 if (IsUndefOrZero(N0))
9023 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N1,
9024 DAG.getConstant(IsFSHL ? BitWidth - ShAmt : ShAmt,
9025 SDLoc(N), ShAmtTy));
9026 if (IsUndefOrZero(N1))
9027 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
9028 DAG.getConstant(IsFSHL ? ShAmt : BitWidth - ShAmt,
9029 SDLoc(N), ShAmtTy));
9030
9031 // fold (fshl ld1, ld0, c) -> (ld0[ofs]) iff ld0 and ld1 are consecutive.
9032 // fold (fshr ld1, ld0, c) -> (ld0[ofs]) iff ld0 and ld1 are consecutive.
9033 // TODO - bigendian support once we have test coverage.
9034 // TODO - can we merge this with CombineConseutiveLoads/MatchLoadCombine?
9035 // TODO - permit LHS EXTLOAD if extensions are shifted out.
9036 if ((BitWidth % 8) == 0 && (ShAmt % 8) == 0 && !VT.isVector() &&
9037 !DAG.getDataLayout().isBigEndian()) {
9038 auto *LHS = dyn_cast<LoadSDNode>(N0);
9039 auto *RHS = dyn_cast<LoadSDNode>(N1);
9040 if (LHS && RHS && LHS->isSimple() && RHS->isSimple() &&
9041 LHS->getAddressSpace() == RHS->getAddressSpace() &&
9042 (LHS->hasOneUse() || RHS->hasOneUse()) && ISD::isNON_EXTLoad(RHS) &&
9043 ISD::isNON_EXTLoad(LHS)) {
9044 if (DAG.areNonVolatileConsecutiveLoads(LHS, RHS, BitWidth / 8, 1)) {
9045 SDLoc DL(RHS);
9046 uint64_t PtrOff =
9047 IsFSHL ? (((BitWidth - ShAmt) % BitWidth) / 8) : (ShAmt / 8);
9048 Align NewAlign = commonAlignment(RHS->getAlign(), PtrOff);
9049 bool Fast = false;
9050 if (TLI.allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), VT,
9051 RHS->getAddressSpace(), NewAlign,
9052 RHS->getMemOperand()->getFlags(), &Fast) &&
9053 Fast) {
9054 SDValue NewPtr = DAG.getMemBasePlusOffset(
9055 RHS->getBasePtr(), TypeSize::Fixed(PtrOff), DL);
9056 AddToWorklist(NewPtr.getNode());
9057 SDValue Load = DAG.getLoad(
9058 VT, DL, RHS->getChain(), NewPtr,
9059 RHS->getPointerInfo().getWithOffset(PtrOff), NewAlign,
9060 RHS->getMemOperand()->getFlags(), RHS->getAAInfo());
9061 // Replace the old load's chain with the new load's chain.
9062 WorklistRemover DeadNodes(*this);
9063 DAG.ReplaceAllUsesOfValueWith(N1.getValue(1), Load.getValue(1));
9064 return Load;
9065 }
9066 }
9067 }
9068 }
9069 }
9070
9071 // fold fshr(undef_or_zero, N1, N2) -> lshr(N1, N2)
9072 // fold fshl(N0, undef_or_zero, N2) -> shl(N0, N2)
9073 // iff We know the shift amount is in range.
9074 // TODO: when is it worth doing SUB(BW, N2) as well?
9075 if (isPowerOf2_32(BitWidth)) {
9076 APInt ModuloBits(N2.getScalarValueSizeInBits(), BitWidth - 1);
9077 if (IsUndefOrZero(N0) && !IsFSHL && DAG.MaskedValueIsZero(N2, ~ModuloBits))
9078 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N1, N2);
9079 if (IsUndefOrZero(N1) && IsFSHL && DAG.MaskedValueIsZero(N2, ~ModuloBits))
9080 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0, N2);
9081 }
9082
9083 // fold (fshl N0, N0, N2) -> (rotl N0, N2)
9084 // fold (fshr N0, N0, N2) -> (rotr N0, N2)
9085 // TODO: Investigate flipping this rotate if only one is legal, if funnel shift
9086 // is legal as well we might be better off avoiding non-constant (BW - N2).
9087 unsigned RotOpc = IsFSHL ? ISD::ROTL : ISD::ROTR;
9088 if (N0 == N1 && hasOperation(RotOpc, VT))
9089 return DAG.getNode(RotOpc, SDLoc(N), VT, N0, N2);
9090
9091 // Simplify, based on bits shifted out of N0/N1.
9092 if (SimplifyDemandedBits(SDValue(N, 0)))
9093 return SDValue(N, 0);
9094
9095 return SDValue();
9096}
9097
9098// Given a ABS node, detect the following pattern:
9099// (ABS (SUB (EXTEND a), (EXTEND b))).
9100// Generates UABD/SABD instruction.
9101static SDValue combineABSToABD(SDNode *N, SelectionDAG &DAG,
9102 const TargetLowering &TLI) {
9103 SDValue AbsOp1 = N->getOperand(0);
9104 SDValue Op0, Op1;
9105
9106 if (AbsOp1.getOpcode() != ISD::SUB)
9107 return SDValue();
9108
9109 Op0 = AbsOp1.getOperand(0);
9110 Op1 = AbsOp1.getOperand(1);
9111
9112 unsigned Opc0 = Op0.getOpcode();
9113 // Check if the operands of the sub are (zero|sign)-extended.
9114 if (Opc0 != Op1.getOpcode() ||
9115 (Opc0 != ISD::ZERO_EXTEND && Opc0 != ISD::SIGN_EXTEND))
9116 return SDValue();
9117
9118 EVT VT1 = Op0.getOperand(0).getValueType();
9119 EVT VT2 = Op1.getOperand(0).getValueType();
9120 // Check if the operands are of same type and valid size.
9121 unsigned ABDOpcode = (Opc0 == ISD::SIGN_EXTEND) ? ISD::ABDS : ISD::ABDU;
9122 if (VT1 != VT2 || !TLI.isOperationLegalOrCustom(ABDOpcode, VT1))
9123 return SDValue();
9124
9125 Op0 = Op0.getOperand(0);
9126 Op1 = Op1.getOperand(0);
9127 SDValue ABD =
9128 DAG.getNode(ABDOpcode, SDLoc(N), Op0->getValueType(0), Op0, Op1);
9129 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), N->getValueType(0), ABD);
9130}
9131
9132SDValue DAGCombiner::visitABS(SDNode *N) {
9133 SDValue N0 = N->getOperand(0);
9134 EVT VT = N->getValueType(0);
9135
9136 // fold (abs c1) -> c2
9137 if (DAG.isConstantIntBuildVectorOrConstantInt(N0))
9138 return DAG.getNode(ISD::ABS, SDLoc(N), VT, N0);
9139 // fold (abs (abs x)) -> (abs x)
9140 if (N0.getOpcode() == ISD::ABS)
9141 return N0;
9142 // fold (abs x) -> x iff not-negative
9143 if (DAG.SignBitIsZero(N0))
9144 return N0;
9145
9146 if (SDValue ABD = combineABSToABD(N, DAG, TLI))
9147 return ABD;
9148
9149 return SDValue();
9150}
9151
9152SDValue DAGCombiner::visitBSWAP(SDNode *N) {
9153 SDValue N0 = N->getOperand(0);
9154 EVT VT = N->getValueType(0);
9155
9156 // fold (bswap c1) -> c2
9157 if (DAG.isConstantIntBuildVectorOrConstantInt(N0))
9158 return DAG.getNode(ISD::BSWAP, SDLoc(N), VT, N0);
9159 // fold (bswap (bswap x)) -> x
9160 if (N0.getOpcode() == ISD::BSWAP)
9161 return N0->getOperand(0);
9162 return SDValue();
9163}
9164
9165SDValue DAGCombiner::visitBITREVERSE(SDNode *N) {
9166 SDValue N0 = N->getOperand(0);
9167 EVT VT = N->getValueType(0);
9168
9169 // fold (bitreverse c1) -> c2
9170 if (DAG.isConstantIntBuildVectorOrConstantInt(N0))
9171 return DAG.getNode(ISD::BITREVERSE, SDLoc(N), VT, N0);
9172 // fold (bitreverse (bitreverse x)) -> x
9173 if (N0.getOpcode() == ISD::BITREVERSE)
9174 return N0.getOperand(0);
9175 return SDValue();
9176}
9177
9178SDValue DAGCombiner::visitCTLZ(SDNode *N) {
9179 SDValue N0 = N->getOperand(0);
9180 EVT VT = N->getValueType(0);
9181
9182 // fold (ctlz c1) -> c2
9183 if (DAG.isConstantIntBuildVectorOrConstantInt(N0))
9184 return DAG.getNode(ISD::CTLZ, SDLoc(N), VT, N0);
9185
9186 // If the value is known never to be zero, switch to the undef version.
9187 if (!LegalOperations || TLI.isOperationLegal(ISD::CTLZ_ZERO_UNDEF, VT)) {
9188 if (DAG.isKnownNeverZero(N0))
9189 return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SDLoc(N), VT, N0);
9190 }
9191
9192 return SDValue();
9193}
9194
9195SDValue DAGCombiner::visitCTLZ_ZERO_UNDEF(SDNode *N) {
9196 SDValue N0 = N->getOperand(0);
9197 EVT VT = N->getValueType(0);
9198
9199 // fold (ctlz_zero_undef c1) -> c2
9200 if (DAG.isConstantIntBuildVectorOrConstantInt(N0))
9201 return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SDLoc(N), VT, N0);
9202 return SDValue();
9203}
9204
9205SDValue DAGCombiner::visitCTTZ(SDNode *N) {
9206 SDValue N0 = N->getOperand(0);
9207 EVT VT = N->getValueType(0);
9208
9209 // fold (cttz c1) -> c2
9210 if (DAG.isConstantIntBuildVectorOrConstantInt(N0))
9211 return DAG.getNode(ISD::CTTZ, SDLoc(N), VT, N0);
9212
9213 // If the value is known never to be zero, switch to the undef version.
9214 if (!LegalOperations || TLI.isOperationLegal(ISD::CTTZ_ZERO_UNDEF, VT)) {
9215 if (DAG.isKnownNeverZero(N0))
9216 return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, SDLoc(N), VT, N0);
9217 }
9218
9219 return SDValue();
9220}
9221
9222SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) {
9223 SDValue N0 = N->getOperand(0);
9224 EVT VT = N->getValueType(0);
9225
9226 // fold (cttz_zero_undef c1) -> c2
9227 if (DAG.isConstantIntBuildVectorOrConstantInt(N0))
9228 return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, SDLoc(N), VT, N0);
9229 return SDValue();
9230}
9231
9232SDValue DAGCombiner::visitCTPOP(SDNode *N) {
9233 SDValue N0 = N->getOperand(0);
9234 EVT VT = N->getValueType(0);
9235
9236 // fold (ctpop c1) -> c2
9237 if (DAG.isConstantIntBuildVectorOrConstantInt(N0))
9238 return DAG.getNode(ISD::CTPOP, SDLoc(N), VT, N0);
9239 return SDValue();
9240}
9241
9242// FIXME: This should be checking for no signed zeros on individual operands, as
9243// well as no nans.
9244static bool isLegalToCombineMinNumMaxNum(SelectionDAG &DAG, SDValue LHS,
9245 SDValue RHS,
9246 const TargetLowering &TLI) {
9247 const TargetOptions &Options = DAG.getTarget().Options;
9248 EVT VT = LHS.getValueType();
9249
9250 return Options.NoSignedZerosFPMath && VT.isFloatingPoint() &&
9251 TLI.isProfitableToCombineMinNumMaxNum(VT) &&
9252 DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS);
9253}
9254
9255/// Generate Min/Max node
9256static SDValue combineMinNumMaxNum(const SDLoc &DL, EVT VT, SDValue LHS,
9257 SDValue RHS, SDValue True, SDValue False,
9258 ISD::CondCode CC, const TargetLowering &TLI,
9259 SelectionDAG &DAG) {
9260 if (!(LHS == True && RHS == False) && !(LHS == False && RHS == True))
9261 return SDValue();
9262
9263 EVT TransformVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
9264 switch (CC) {
9265 case ISD::SETOLT:
9266 case ISD::SETOLE:
9267 case ISD::SETLT:
9268 case ISD::SETLE:
9269 case ISD::SETULT:
9270 case ISD::SETULE: {
9271 // Since it's known never nan to get here already, either fminnum or
9272 // fminnum_ieee are OK. Try the ieee version first, since it's fminnum is
9273 // expanded in terms of it.
9274 unsigned IEEEOpcode = (LHS == True) ? ISD::FMINNUM_IEEE : ISD::FMAXNUM_IEEE;
9275 if (TLI.isOperationLegalOrCustom(IEEEOpcode, VT))
9276 return DAG.getNode(IEEEOpcode, DL, VT, LHS, RHS);
9277
9278 unsigned Opcode = (LHS == True) ? ISD::FMINNUM : ISD::FMAXNUM;
9279 if (TLI.isOperationLegalOrCustom(Opcode, TransformVT))
9280 return DAG.getNode(Opcode, DL, VT, LHS, RHS);
9281 return SDValue();
9282 }
9283 case ISD::SETOGT:
9284 case ISD::SETOGE:
9285 case ISD::SETGT:
9286 case ISD::SETGE:
9287 case ISD::SETUGT:
9288 case ISD::SETUGE: {
9289 unsigned IEEEOpcode = (LHS == True) ? ISD::FMAXNUM_IEEE : ISD::FMINNUM_IEEE;
9290 if (TLI.isOperationLegalOrCustom(IEEEOpcode, VT))
9291 return DAG.getNode(IEEEOpcode, DL, VT, LHS, RHS);
9292
9293 unsigned Opcode = (LHS == True) ? ISD::FMAXNUM : ISD::FMINNUM;
9294 if (TLI.isOperationLegalOrCustom(Opcode, TransformVT))
9295 return DAG.getNode(Opcode, DL, VT, LHS, RHS);
9296 return SDValue();
9297 }
9298 default:
9299 return SDValue();
9300 }
9301}
9302
9303/// If a (v)select has a condition value that is a sign-bit test, try to smear
9304/// the condition operand sign-bit across the value width and use it as a mask.
9305static SDValue foldSelectOfConstantsUsingSra(SDNode *N, SelectionDAG &DAG) {
9306 SDValue Cond = N->getOperand(0);
9307 SDValue C1 = N->getOperand(1);
9308 SDValue C2 = N->getOperand(2);
9309 if (!isConstantOrConstantVector(C1) || !isConstantOrConstantVector(C2))
9310 return SDValue();
9311
9312 EVT VT = N->getValueType(0);
9313 if (Cond.getOpcode() != ISD::SETCC || !Cond.hasOneUse() ||
9314 VT != Cond.getOperand(0).getValueType())
9315 return SDValue();
9316
9317 // The inverted-condition + commuted-select variants of these patterns are
9318 // canonicalized to these forms in IR.
9319 SDValue X = Cond.getOperand(0);
9320 SDValue CondC = Cond.getOperand(1);
9321 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
9322 if (CC == ISD::SETGT && isAllOnesOrAllOnesSplat(CondC) &&
9323 isAllOnesOrAllOnesSplat(C2)) {
9324 // i32 X > -1 ? C1 : -1 --> (X >>s 31) | C1
9325 SDLoc DL(N);
9326 SDValue ShAmtC = DAG.getConstant(X.getScalarValueSizeInBits() - 1, DL, VT);
9327 SDValue Sra = DAG.getNode(ISD::SRA, DL, VT, X, ShAmtC);
9328 return DAG.getNode(ISD::OR, DL, VT, Sra, C1);
9329 }
9330 if (CC == ISD::SETLT && isNullOrNullSplat(CondC) && isNullOrNullSplat(C2)) {
9331 // i8 X < 0 ? C1 : 0 --> (X >>s 7) & C1
9332 SDLoc DL(N);
9333 SDValue ShAmtC = DAG.getConstant(X.getScalarValueSizeInBits() - 1, DL, VT);
9334 SDValue Sra = DAG.getNode(ISD::SRA, DL, VT, X, ShAmtC);
9335 return DAG.getNode(ISD::AND, DL, VT, Sra, C1);
9336 }
9337 return SDValue();
9338}
9339
9340SDValue DAGCombiner::foldSelectOfConstants(SDNode *N) {
9341 SDValue Cond = N->getOperand(0);
9342 SDValue N1 = N->getOperand(1);
9343 SDValue N2 = N->getOperand(2);
9344 EVT VT = N->getValueType(0);
9345 EVT CondVT = Cond.getValueType();
9346 SDLoc DL(N);
9347
9348 if (!VT.isInteger())
9349 return SDValue();
9350
9351 auto *C1 = dyn_cast<ConstantSDNode>(N1);
9352 auto *C2 = dyn_cast<ConstantSDNode>(N2);
9353 if (!C1 || !C2)
9354 return SDValue();
9355
9356 // Only do this before legalization to avoid conflicting with target-specific
9357 // transforms in the other direction (create a select from a zext/sext). There
9358 // is also a target-independent combine here in DAGCombiner in the other
9359 // direction for (select Cond, -1, 0) when the condition is not i1.
9360 if (CondVT == MVT::i1 && !LegalOperations) {
9361 if (C1->isNullValue() && C2->isOne()) {
9362 // select Cond, 0, 1 --> zext (!Cond)
9363 SDValue NotCond = DAG.getNOT(DL, Cond, MVT::i1);
9364 if (VT != MVT::i1)
9365 NotCond = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, NotCond);
9366 return NotCond;
9367 }
9368 if (C1->isNullValue() && C2->isAllOnesValue()) {
9369 // select Cond, 0, -1 --> sext (!Cond)
9370 SDValue NotCond = DAG.getNOT(DL, Cond, MVT::i1);
9371 if (VT != MVT::i1)
9372 NotCond = DAG.getNode(ISD::SIGN_EXTEND, DL, VT, NotCond);
9373 return NotCond;
9374 }
9375 if (C1->isOne() && C2->isNullValue()) {
9376 // select Cond, 1, 0 --> zext (Cond)
9377 if (VT != MVT::i1)
9378 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Cond);
9379 return Cond;
9380 }
9381 if (C1->isAllOnesValue() && C2->isNullValue()) {
9382 // select Cond, -1, 0 --> sext (Cond)
9383 if (VT != MVT::i1)
9384 Cond = DAG.getNode(ISD::SIGN_EXTEND, DL, VT, Cond);
9385 return Cond;
9386 }
9387
9388 // Use a target hook because some targets may prefer to transform in the
9389 // other direction.
9390 if (TLI.convertSelectOfConstantsToMath(VT)) {
9391 // For any constants that differ by 1, we can transform the select into an
9392 // extend and add.
9393 const APInt &C1Val = C1->getAPIntValue();
9394 const APInt &C2Val = C2->getAPIntValue();
9395 if (C1Val - 1 == C2Val) {
9396 // select Cond, C1, C1-1 --> add (zext Cond), C1-1
9397 if (VT != MVT::i1)
9398 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Cond);
9399 return DAG.getNode(ISD::ADD, DL, VT, Cond, N2);
9400 }
9401 if (C1Val + 1 == C2Val) {
9402 // select Cond, C1, C1+1 --> add (sext Cond), C1+1
9403 if (VT != MVT::i1)
9404 Cond = DAG.getNode(ISD::SIGN_EXTEND, DL, VT, Cond);
9405 return DAG.getNode(ISD::ADD, DL, VT, Cond, N2);
9406 }
9407
9408 // select Cond, Pow2, 0 --> (zext Cond) << log2(Pow2)
9409 if (C1Val.isPowerOf2() && C2Val.isNullValue()) {
9410 if (VT != MVT::i1)
9411 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Cond);
9412 SDValue ShAmtC = DAG.getConstant(C1Val.exactLogBase2(), DL, VT);
9413 return DAG.getNode(ISD::SHL, DL, VT, Cond, ShAmtC);
9414 }
9415
9416 if (SDValue V = foldSelectOfConstantsUsingSra(N, DAG))
9417 return V;
9418 }
9419
9420 return SDValue();
9421 }
9422
9423 // fold (select Cond, 0, 1) -> (xor Cond, 1)
9424 // We can't do this reliably if integer based booleans have different contents
9425 // to floating point based booleans. This is because we can't tell whether we
9426 // have an integer-based boolean or a floating-point-based boolean unless we
9427 // can find the SETCC that produced it and inspect its operands. This is
9428 // fairly easy if C is the SETCC node, but it can potentially be
9429 // undiscoverable (or not reasonably discoverable). For example, it could be
9430 // in another basic block or it could require searching a complicated
9431 // expression.
9432 if (CondVT.isInteger() &&
9433 TLI.getBooleanContents(/*isVec*/false, /*isFloat*/true) ==
9434 TargetLowering::ZeroOrOneBooleanContent &&
9435 TLI.getBooleanContents(/*isVec*/false, /*isFloat*/false) ==
9436 TargetLowering::ZeroOrOneBooleanContent &&
9437 C1->isNullValue() && C2->isOne()) {
9438 SDValue NotCond =
9439 DAG.getNode(ISD::XOR, DL, CondVT, Cond, DAG.getConstant(1, DL, CondVT));
9440 if (VT.bitsEq(CondVT))
9441 return NotCond;
9442 return DAG.getZExtOrTrunc(NotCond, DL, VT);
9443 }
9444
9445 return SDValue();
9446}
9447
9448static SDValue foldBoolSelectToLogic(SDNode *N, SelectionDAG &DAG) {
9449 assert((N->getOpcode() == ISD::SELECT || N->getOpcode() == ISD::VSELECT) &&(static_cast <bool> ((N->getOpcode() == ISD::SELECT ||
N->getOpcode() == ISD::VSELECT) && "Expected a (v)select"
) ? void (0) : __assert_fail ("(N->getOpcode() == ISD::SELECT || N->getOpcode() == ISD::VSELECT) && \"Expected a (v)select\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 9450, __extension__ __PRETTY_FUNCTION__))
9450 "Expected a (v)select")(static_cast <bool> ((N->getOpcode() == ISD::SELECT ||
N->getOpcode() == ISD::VSELECT) && "Expected a (v)select"
) ? void (0) : __assert_fail ("(N->getOpcode() == ISD::SELECT || N->getOpcode() == ISD::VSELECT) && \"Expected a (v)select\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 9450, __extension__ __PRETTY_FUNCTION__))
;
9451 SDValue Cond = N->getOperand(0);
9452 SDValue T = N->getOperand(1), F = N->getOperand(2);
9453 EVT VT = N->getValueType(0);
9454 if (VT != Cond.getValueType() || VT.getScalarSizeInBits() != 1)
9455 return SDValue();
9456
9457 // select Cond, Cond, F --> or Cond, F
9458 // select Cond, 1, F --> or Cond, F
9459 if (Cond == T || isOneOrOneSplat(T, /* AllowUndefs */ true))
9460 return DAG.getNode(ISD::OR, SDLoc(N), VT, Cond, F);
9461
9462 // select Cond, T, Cond --> and Cond, T
9463 // select Cond, T, 0 --> and Cond, T
9464 if (Cond == F || isNullOrNullSplat(F, /* AllowUndefs */ true))
9465 return DAG.getNode(ISD::AND, SDLoc(N), VT, Cond, T);
9466
9467 // select Cond, T, 1 --> or (not Cond), T
9468 if (isOneOrOneSplat(F, /* AllowUndefs */ true)) {
9469 SDValue NotCond = DAG.getNOT(SDLoc(N), Cond, VT);
9470 return DAG.getNode(ISD::OR, SDLoc(N), VT, NotCond, T);
9471 }
9472
9473 // select Cond, 0, F --> and (not Cond), F
9474 if (isNullOrNullSplat(T, /* AllowUndefs */ true)) {
9475 SDValue NotCond = DAG.getNOT(SDLoc(N), Cond, VT);
9476 return DAG.getNode(ISD::AND, SDLoc(N), VT, NotCond, F);
9477 }
9478
9479 return SDValue();
9480}
9481
9482SDValue DAGCombiner::visitSELECT(SDNode *N) {
9483 SDValue N0 = N->getOperand(0);
9484 SDValue N1 = N->getOperand(1);
9485 SDValue N2 = N->getOperand(2);
9486 EVT VT = N->getValueType(0);
9487 EVT VT0 = N0.getValueType();
9488 SDLoc DL(N);
9489 SDNodeFlags Flags = N->getFlags();
9490
9491 if (SDValue V = DAG.simplifySelect(N0, N1, N2))
9492 return V;
9493
9494 if (SDValue V = foldSelectOfConstants(N))
9495 return V;
9496
9497 if (SDValue V = foldBoolSelectToLogic(N, DAG))
9498 return V;
9499
9500 // If we can fold this based on the true/false value, do so.
9501 if (SimplifySelectOps(N, N1, N2))
9502 return SDValue(N, 0); // Don't revisit N.
9503
9504 if (VT0 == MVT::i1) {
9505 // The code in this block deals with the following 2 equivalences:
9506 // select(C0|C1, x, y) <=> select(C0, x, select(C1, x, y))
9507 // select(C0&C1, x, y) <=> select(C0, select(C1, x, y), y)
9508 // The target can specify its preferred form with the
9509 // shouldNormalizeToSelectSequence() callback. However we always transform
9510 // to the right anyway if we find the inner select exists in the DAG anyway
9511 // and we always transform to the left side if we know that we can further
9512 // optimize the combination of the conditions.
9513 bool normalizeToSequence =
9514 TLI.shouldNormalizeToSelectSequence(*DAG.getContext(), VT);
9515 // select (and Cond0, Cond1), X, Y
9516 // -> select Cond0, (select Cond1, X, Y), Y
9517 if (N0->getOpcode() == ISD::AND && N0->hasOneUse()) {
9518 SDValue Cond0 = N0->getOperand(0);
9519 SDValue Cond1 = N0->getOperand(1);
9520 SDValue InnerSelect =
9521 DAG.getNode(ISD::SELECT, DL, N1.getValueType(), Cond1, N1, N2, Flags);
9522 if (normalizeToSequence || !InnerSelect.use_empty())
9523 return DAG.getNode(ISD::SELECT, DL, N1.getValueType(), Cond0,
9524 InnerSelect, N2, Flags);
9525 // Cleanup on failure.
9526 if (InnerSelect.use_empty())
9527 recursivelyDeleteUnusedNodes(InnerSelect.getNode());
9528 }
9529 // select (or Cond0, Cond1), X, Y -> select Cond0, X, (select Cond1, X, Y)
9530 if (N0->getOpcode() == ISD::OR && N0->hasOneUse()) {
9531 SDValue Cond0 = N0->getOperand(0);
9532 SDValue Cond1 = N0->getOperand(1);
9533 SDValue InnerSelect = DAG.getNode(ISD::SELECT, DL, N1.getValueType(),
9534 Cond1, N1, N2, Flags);
9535 if (normalizeToSequence || !InnerSelect.use_empty())
9536 return DAG.getNode(ISD::SELECT, DL, N1.getValueType(), Cond0, N1,
9537 InnerSelect, Flags);
9538 // Cleanup on failure.
9539 if (InnerSelect.use_empty())
9540 recursivelyDeleteUnusedNodes(InnerSelect.getNode());
9541 }
9542
9543 // select Cond0, (select Cond1, X, Y), Y -> select (and Cond0, Cond1), X, Y
9544 if (N1->getOpcode() == ISD::SELECT && N1->hasOneUse()) {
9545 SDValue N1_0 = N1->getOperand(0);
9546 SDValue N1_1 = N1->getOperand(1);
9547 SDValue N1_2 = N1->getOperand(2);
9548 if (N1_2 == N2 && N0.getValueType() == N1_0.getValueType()) {
9549 // Create the actual and node if we can generate good code for it.
9550 if (!normalizeToSequence) {
9551 SDValue And = DAG.getNode(ISD::AND, DL, N0.getValueType(), N0, N1_0);
9552 return DAG.getNode(ISD::SELECT, DL, N1.getValueType(), And, N1_1,
9553 N2, Flags);
9554 }
9555 // Otherwise see if we can optimize the "and" to a better pattern.
9556 if (SDValue Combined = visitANDLike(N0, N1_0, N)) {
9557 return DAG.getNode(ISD::SELECT, DL, N1.getValueType(), Combined, N1_1,
9558 N2, Flags);
9559 }
9560 }
9561 }
9562 // select Cond0, X, (select Cond1, X, Y) -> select (or Cond0, Cond1), X, Y
9563 if (N2->getOpcode() == ISD::SELECT && N2->hasOneUse()) {
9564 SDValue N2_0 = N2->getOperand(0);
9565 SDValue N2_1 = N2->getOperand(1);
9566 SDValue N2_2 = N2->getOperand(2);
9567 if (N2_1 == N1 && N0.getValueType() == N2_0.getValueType()) {
9568 // Create the actual or node if we can generate good code for it.
9569 if (!normalizeToSequence) {
9570 SDValue Or = DAG.getNode(ISD::OR, DL, N0.getValueType(), N0, N2_0);
9571 return DAG.getNode(ISD::SELECT, DL, N1.getValueType(), Or, N1,
9572 N2_2, Flags);
9573 }
9574 // Otherwise see if we can optimize to a better pattern.
9575 if (SDValue Combined = visitORLike(N0, N2_0, N))
9576 return DAG.getNode(ISD::SELECT, DL, N1.getValueType(), Combined, N1,
9577 N2_2, Flags);
9578 }
9579 }
9580 }
9581
9582 // select (not Cond), N1, N2 -> select Cond, N2, N1
9583 if (SDValue F = extractBooleanFlip(N0, DAG, TLI, false)) {
9584 SDValue SelectOp = DAG.getSelect(DL, VT, F, N2, N1);
9585 SelectOp->setFlags(Flags);
9586 return SelectOp;
9587 }
9588
9589 // Fold selects based on a setcc into other things, such as min/max/abs.
9590 if (N0.getOpcode() == ISD::SETCC) {
9591 SDValue Cond0 = N0.getOperand(0), Cond1 = N0.getOperand(1);
9592 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
9593
9594 // select (fcmp lt x, y), x, y -> fminnum x, y
9595 // select (fcmp gt x, y), x, y -> fmaxnum x, y
9596 //
9597 // This is OK if we don't care what happens if either operand is a NaN.
9598 if (N0.hasOneUse() && isLegalToCombineMinNumMaxNum(DAG, N1, N2, TLI))
9599 if (SDValue FMinMax = combineMinNumMaxNum(DL, VT, Cond0, Cond1, N1, N2,
9600 CC, TLI, DAG))
9601 return FMinMax;
9602
9603 // Use 'unsigned add with overflow' to optimize an unsigned saturating add.
9604 // This is conservatively limited to pre-legal-operations to give targets
9605 // a chance to reverse the transform if they want to do that. Also, it is
9606 // unlikely that the pattern would be formed late, so it's probably not
9607 // worth going through the other checks.
9608 if (!LegalOperations && TLI.isOperationLegalOrCustom(ISD::UADDO, VT) &&
9609 CC == ISD::SETUGT && N0.hasOneUse() && isAllOnesConstant(N1) &&
9610 N2.getOpcode() == ISD::ADD && Cond0 == N2.getOperand(0)) {
9611 auto *C = dyn_cast<ConstantSDNode>(N2.getOperand(1));
9612 auto *NotC = dyn_cast<ConstantSDNode>(Cond1);
9613 if (C && NotC && C->getAPIntValue() == ~NotC->getAPIntValue()) {
9614 // select (setcc Cond0, ~C, ugt), -1, (add Cond0, C) -->
9615 // uaddo Cond0, C; select uaddo.1, -1, uaddo.0
9616 //
9617 // The IR equivalent of this transform would have this form:
9618 // %a = add %x, C
9619 // %c = icmp ugt %x, ~C
9620 // %r = select %c, -1, %a
9621 // =>
9622 // %u = call {iN,i1} llvm.uadd.with.overflow(%x, C)
9623 // %u0 = extractvalue %u, 0
9624 // %u1 = extractvalue %u, 1
9625 // %r = select %u1, -1, %u0
9626 SDVTList VTs = DAG.getVTList(VT, VT0);
9627 SDValue UAO = DAG.getNode(ISD::UADDO, DL, VTs, Cond0, N2.getOperand(1));
9628 return DAG.getSelect(DL, VT, UAO.getValue(1), N1, UAO.getValue(0));
9629 }
9630 }
9631
9632 if (TLI.isOperationLegal(ISD::SELECT_CC, VT) ||
9633 (!LegalOperations &&
9634 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))) {
9635 // Any flags available in a select/setcc fold will be on the setcc as they
9636 // migrated from fcmp
9637 Flags = N0.getNode()->getFlags();
9638 SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, VT, Cond0, Cond1, N1,
9639 N2, N0.getOperand(2));
9640 SelectNode->setFlags(Flags);
9641 return SelectNode;
9642 }
9643
9644 if (SDValue NewSel = SimplifySelect(DL, N0, N1, N2))
9645 return NewSel;
9646 }
9647
9648 if (!VT.isVector())
9649 if (SDValue BinOp = foldSelectOfBinops(N))
9650 return BinOp;
9651
9652 return SDValue();
9653}
9654
9655// This function assumes all the vselect's arguments are CONCAT_VECTOR
9656// nodes and that the condition is a BV of ConstantSDNodes (or undefs).
9657static SDValue ConvertSelectToConcatVector(SDNode *N, SelectionDAG &DAG) {
9658 SDLoc DL(N);
9659 SDValue Cond = N->getOperand(0);
9660 SDValue LHS = N->getOperand(1);
9661 SDValue RHS = N->getOperand(2);
9662 EVT VT = N->getValueType(0);
9663 int NumElems = VT.getVectorNumElements();
9664 assert(LHS.getOpcode() == ISD::CONCAT_VECTORS &&(static_cast <bool> (LHS.getOpcode() == ISD::CONCAT_VECTORS
&& RHS.getOpcode() == ISD::CONCAT_VECTORS &&
Cond.getOpcode() == ISD::BUILD_VECTOR) ? void (0) : __assert_fail
("LHS.getOpcode() == ISD::CONCAT_VECTORS && RHS.getOpcode() == ISD::CONCAT_VECTORS && Cond.getOpcode() == ISD::BUILD_VECTOR"
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 9666, __extension__ __PRETTY_FUNCTION__))
9665 RHS.getOpcode() == ISD::CONCAT_VECTORS &&(static_cast <bool> (LHS.getOpcode() == ISD::CONCAT_VECTORS
&& RHS.getOpcode() == ISD::CONCAT_VECTORS &&
Cond.getOpcode() == ISD::BUILD_VECTOR) ? void (0) : __assert_fail
("LHS.getOpcode() == ISD::CONCAT_VECTORS && RHS.getOpcode() == ISD::CONCAT_VECTORS && Cond.getOpcode() == ISD::BUILD_VECTOR"
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 9666, __extension__ __PRETTY_FUNCTION__))
9666 Cond.getOpcode() == ISD::BUILD_VECTOR)(static_cast <bool> (LHS.getOpcode() == ISD::CONCAT_VECTORS
&& RHS.getOpcode() == ISD::CONCAT_VECTORS &&
Cond.getOpcode() == ISD::BUILD_VECTOR) ? void (0) : __assert_fail
("LHS.getOpcode() == ISD::CONCAT_VECTORS && RHS.getOpcode() == ISD::CONCAT_VECTORS && Cond.getOpcode() == ISD::BUILD_VECTOR"
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 9666, __extension__ __PRETTY_FUNCTION__))
;
9667
9668 // CONCAT_VECTOR can take an arbitrary number of arguments. We only care about
9669 // binary ones here.
9670 if (LHS->getNumOperands() != 2 || RHS->getNumOperands() != 2)
9671 return SDValue();
9672
9673 // We're sure we have an even number of elements due to the
9674 // concat_vectors we have as arguments to vselect.
9675 // Skip BV elements until we find one that's not an UNDEF
9676 // After we find an UNDEF element, keep looping until we get to half the
9677 // length of the BV and see if all the non-undef nodes are the same.
9678 ConstantSDNode *BottomHalf = nullptr;
9679 for (int i = 0; i < NumElems / 2; ++i) {
9680 if (Cond->getOperand(i)->isUndef())
9681 continue;
9682
9683 if (BottomHalf == nullptr)
9684 BottomHalf = cast<ConstantSDNode>(Cond.getOperand(i));
9685 else if (Cond->getOperand(i).getNode() != BottomHalf)
9686 return SDValue();
9687 }
9688
9689 // Do the same for the second half of the BuildVector
9690 ConstantSDNode *TopHalf = nullptr;
9691 for (int i = NumElems / 2; i < NumElems; ++i) {
9692 if (Cond->getOperand(i)->isUndef())
9693 continue;
9694
9695 if (TopHalf == nullptr)
9696 TopHalf = cast<ConstantSDNode>(Cond.getOperand(i));
9697 else if (Cond->getOperand(i).getNode() != TopHalf)
9698 return SDValue();
9699 }
9700
9701 assert(TopHalf && BottomHalf &&(static_cast <bool> (TopHalf && BottomHalf &&
"One half of the selector was all UNDEFs and the other was all the "
"same value. This should have been addressed before this function."
) ? void (0) : __assert_fail ("TopHalf && BottomHalf && \"One half of the selector was all UNDEFs and the other was all the \" \"same value. This should have been addressed before this function.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 9703, __extension__ __PRETTY_FUNCTION__))
9702 "One half of the selector was all UNDEFs and the other was all the "(static_cast <bool> (TopHalf && BottomHalf &&
"One half of the selector was all UNDEFs and the other was all the "
"same value. This should have been addressed before this function."
) ? void (0) : __assert_fail ("TopHalf && BottomHalf && \"One half of the selector was all UNDEFs and the other was all the \" \"same value. This should have been addressed before this function.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 9703, __extension__ __PRETTY_FUNCTION__))
9703 "same value. This should have been addressed before this function.")(static_cast <bool> (TopHalf && BottomHalf &&
"One half of the selector was all UNDEFs and the other was all the "
"same value. This should have been addressed before this function."
) ? void (0) : __assert_fail ("TopHalf && BottomHalf && \"One half of the selector was all UNDEFs and the other was all the \" \"same value. This should have been addressed before this function.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 9703, __extension__ __PRETTY_FUNCTION__))
;
9704 return DAG.getNode(
9705 ISD::CONCAT_VECTORS, DL, VT,
9706 BottomHalf->isNullValue() ? RHS->getOperand(0) : LHS->getOperand(0),
9707 TopHalf->isNullValue() ? RHS->getOperand(1) : LHS->getOperand(1));
9708}
9709
9710bool refineUniformBase(SDValue &BasePtr, SDValue &Index, SelectionDAG &DAG) {
9711 if (!isNullConstant(BasePtr) || Index.getOpcode() != ISD::ADD)
9712 return false;
9713
9714 // For now we check only the LHS of the add.
9715 SDValue LHS = Index.getOperand(0);
9716 SDValue SplatVal = DAG.getSplatValue(LHS);
9717 if (!SplatVal)
9718 return false;
9719
9720 BasePtr = SplatVal;
9721 Index = Index.getOperand(1);
9722 return true;
9723}
9724
9725// Fold sext/zext of index into index type.
9726bool refineIndexType(MaskedGatherScatterSDNode *MGS, SDValue &Index,
9727 bool Scaled, SelectionDAG &DAG) {
9728 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9729
9730 if (Index.getOpcode() == ISD::ZERO_EXTEND) {
9731 SDValue Op = Index.getOperand(0);
9732 MGS->setIndexType(Scaled ? ISD::UNSIGNED_SCALED : ISD::UNSIGNED_UNSCALED);
9733 if (TLI.shouldRemoveExtendFromGSIndex(Op.getValueType())) {
9734 Index = Op;
9735 return true;
9736 }
9737 }
9738
9739 if (Index.getOpcode() == ISD::SIGN_EXTEND) {
9740 SDValue Op = Index.getOperand(0);
9741 MGS->setIndexType(Scaled ? ISD::SIGNED_SCALED : ISD::SIGNED_UNSCALED);
9742 if (TLI.shouldRemoveExtendFromGSIndex(Op.getValueType())) {
9743 Index = Op;
9744 return true;
9745 }
9746 }
9747
9748 return false;
9749}
9750
9751SDValue DAGCombiner::visitMSCATTER(SDNode *N) {
9752 MaskedScatterSDNode *MSC = cast<MaskedScatterSDNode>(N);
9753 SDValue Mask = MSC->getMask();
9754 SDValue Chain = MSC->getChain();
9755 SDValue Index = MSC->getIndex();
9756 SDValue Scale = MSC->getScale();
9757 SDValue StoreVal = MSC->getValue();
9758 SDValue BasePtr = MSC->getBasePtr();
9759 SDLoc DL(N);
9760
9761 // Zap scatters with a zero mask.
9762 if (ISD::isConstantSplatVectorAllZeros(Mask.getNode()))
9763 return Chain;
9764
9765 if (refineUniformBase(BasePtr, Index, DAG)) {
9766 SDValue Ops[] = {Chain, StoreVal, Mask, BasePtr, Index, Scale};
9767 return DAG.getMaskedScatter(
9768 DAG.getVTList(MVT::Other), MSC->getMemoryVT(), DL, Ops,
9769 MSC->getMemOperand(), MSC->getIndexType(), MSC->isTruncatingStore());
9770 }
9771
9772 if (refineIndexType(MSC, Index, MSC->isIndexScaled(), DAG)) {
9773 SDValue Ops[] = {Chain, StoreVal, Mask, BasePtr, Index, Scale};
9774 return DAG.getMaskedScatter(
9775 DAG.getVTList(MVT::Other), MSC->getMemoryVT(), DL, Ops,
9776 MSC->getMemOperand(), MSC->getIndexType(), MSC->isTruncatingStore());
9777 }
9778
9779 return SDValue();
9780}
9781
9782SDValue DAGCombiner::visitMSTORE(SDNode *N) {
9783 MaskedStoreSDNode *MST = cast<MaskedStoreSDNode>(N);
9784 SDValue Mask = MST->getMask();
9785 SDValue Chain = MST->getChain();
9786 SDLoc DL(N);
9787
9788 // Zap masked stores with a zero mask.
9789 if (ISD::isConstantSplatVectorAllZeros(Mask.getNode()))
9790 return Chain;
9791
9792 // If this is a masked load with an all ones mask, we can use a unmasked load.
9793 // FIXME: Can we do this for indexed, compressing, or truncating stores?
9794 if (ISD::isConstantSplatVectorAllOnes(Mask.getNode()) &&
9795 MST->isUnindexed() && !MST->isCompressingStore() &&
9796 !MST->isTruncatingStore())
9797 return DAG.getStore(MST->getChain(), SDLoc(N), MST->getValue(),
9798 MST->getBasePtr(), MST->getMemOperand());
9799
9800 // Try transforming N to an indexed store.
9801 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
9802 return SDValue(N, 0);
9803
9804 return SDValue();
9805}
9806
9807SDValue DAGCombiner::visitMGATHER(SDNode *N) {
9808 MaskedGatherSDNode *MGT = cast<MaskedGatherSDNode>(N);
9809 SDValue Mask = MGT->getMask();
9810 SDValue Chain = MGT->getChain();
9811 SDValue Index = MGT->getIndex();
9812 SDValue Scale = MGT->getScale();
9813 SDValue PassThru = MGT->getPassThru();
9814 SDValue BasePtr = MGT->getBasePtr();
9815 SDLoc DL(N);
9816
9817 // Zap gathers with a zero mask.
9818 if (ISD::isConstantSplatVectorAllZeros(Mask.getNode()))
9819 return CombineTo(N, PassThru, MGT->getChain());
9820
9821 if (refineUniformBase(BasePtr, Index, DAG)) {
9822 SDValue Ops[] = {Chain, PassThru, Mask, BasePtr, Index, Scale};
9823 return DAG.getMaskedGather(DAG.getVTList(N->getValueType(0), MVT::Other),
9824 MGT->getMemoryVT(), DL, Ops,
9825 MGT->getMemOperand(), MGT->getIndexType(),
9826 MGT->getExtensionType());
9827 }
9828
9829 if (refineIndexType(MGT, Index, MGT->isIndexScaled(), DAG)) {
9830 SDValue Ops[] = {Chain, PassThru, Mask, BasePtr, Index, Scale};
9831 return DAG.getMaskedGather(DAG.getVTList(N->getValueType(0), MVT::Other),
9832 MGT->getMemoryVT(), DL, Ops,
9833 MGT->getMemOperand(), MGT->getIndexType(),
9834 MGT->getExtensionType());
9835 }
9836
9837 return SDValue();
9838}
9839
9840SDValue DAGCombiner::visitMLOAD(SDNode *N) {
9841 MaskedLoadSDNode *MLD = cast<MaskedLoadSDNode>(N);
9842 SDValue Mask = MLD->getMask();
9843 SDLoc DL(N);
9844
9845 // Zap masked loads with a zero mask.
9846 if (ISD::isConstantSplatVectorAllZeros(Mask.getNode()))
9847 return CombineTo(N, MLD->getPassThru(), MLD->getChain());
9848
9849 // If this is a masked load with an all ones mask, we can use a unmasked load.
9850 // FIXME: Can we do this for indexed, expanding, or extending loads?
9851 if (ISD::isConstantSplatVectorAllOnes(Mask.getNode()) &&
9852 MLD->isUnindexed() && !MLD->isExpandingLoad() &&
9853 MLD->getExtensionType() == ISD::NON_EXTLOAD) {
9854 SDValue NewLd = DAG.getLoad(N->getValueType(0), SDLoc(N), MLD->getChain(),
9855 MLD->getBasePtr(), MLD->getMemOperand());
9856 return CombineTo(N, NewLd, NewLd.getValue(1));
9857 }
9858
9859 // Try transforming N to an indexed load.
9860 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
9861 return SDValue(N, 0);
9862
9863 return SDValue();
9864}
9865
9866/// A vector select of 2 constant vectors can be simplified to math/logic to
9867/// avoid a variable select instruction and possibly avoid constant loads.
9868SDValue DAGCombiner::foldVSelectOfConstants(SDNode *N) {
9869 SDValue Cond = N->getOperand(0);
9870 SDValue N1 = N->getOperand(1);
9871 SDValue N2 = N->getOperand(2);
9872 EVT VT = N->getValueType(0);
9873 if (!Cond.hasOneUse() || Cond.getScalarValueSizeInBits() != 1 ||
9874 !TLI.convertSelectOfConstantsToMath(VT) ||
9875 !ISD::isBuildVectorOfConstantSDNodes(N1.getNode()) ||
9876 !ISD::isBuildVectorOfConstantSDNodes(N2.getNode()))
9877 return SDValue();
9878
9879 // Check if we can use the condition value to increment/decrement a single
9880 // constant value. This simplifies a select to an add and removes a constant
9881 // load/materialization from the general case.
9882 bool AllAddOne = true;
9883 bool AllSubOne = true;
9884 unsigned Elts = VT.getVectorNumElements();
9885 for (unsigned i = 0; i != Elts; ++i) {
9886 SDValue N1Elt = N1.getOperand(i);
9887 SDValue N2Elt = N2.getOperand(i);
9888 if (N1Elt.isUndef() || N2Elt.isUndef())
9889 continue;
9890 if (N1Elt.getValueType() != N2Elt.getValueType())
9891 continue;
9892
9893 const APInt &C1 = cast<ConstantSDNode>(N1Elt)->getAPIntValue();
9894 const APInt &C2 = cast<ConstantSDNode>(N2Elt)->getAPIntValue();
9895 if (C1 != C2 + 1)
9896 AllAddOne = false;
9897 if (C1 != C2 - 1)
9898 AllSubOne = false;
9899 }
9900
9901 // Further simplifications for the extra-special cases where the constants are
9902 // all 0 or all -1 should be implemented as folds of these patterns.
9903 SDLoc DL(N);
9904 if (AllAddOne || AllSubOne) {
9905 // vselect <N x i1> Cond, C+1, C --> add (zext Cond), C
9906 // vselect <N x i1> Cond, C-1, C --> add (sext Cond), C
9907 auto ExtendOpcode = AllAddOne ? ISD::ZERO_EXTEND : ISD::SIGN_EXTEND;
9908 SDValue ExtendedCond = DAG.getNode(ExtendOpcode, DL, VT, Cond);
9909 return DAG.getNode(ISD::ADD, DL, VT, ExtendedCond, N2);
9910 }
9911
9912 // select Cond, Pow2C, 0 --> (zext Cond) << log2(Pow2C)
9913 APInt Pow2C;
9914 if (ISD::isConstantSplatVector(N1.getNode(), Pow2C) && Pow2C.isPowerOf2() &&
9915 isNullOrNullSplat(N2)) {
9916 SDValue ZextCond = DAG.getZExtOrTrunc(Cond, DL, VT);
9917 SDValue ShAmtC = DAG.getConstant(Pow2C.exactLogBase2(), DL, VT);
9918 return DAG.getNode(ISD::SHL, DL, VT, ZextCond, ShAmtC);
9919 }
9920
9921 if (SDValue V = foldSelectOfConstantsUsingSra(N, DAG))
9922 return V;
9923
9924 // The general case for select-of-constants:
9925 // vselect <N x i1> Cond, C1, C2 --> xor (and (sext Cond), (C1^C2)), C2
9926 // ...but that only makes sense if a vselect is slower than 2 logic ops, so
9927 // leave that to a machine-specific pass.
9928 return SDValue();
9929}
9930
9931SDValue DAGCombiner::visitVSELECT(SDNode *N) {
9932 SDValue N0 = N->getOperand(0);
9933 SDValue N1 = N->getOperand(1);
9934 SDValue N2 = N->getOperand(2);
9935 EVT VT = N->getValueType(0);
9936 SDLoc DL(N);
9937
9938 if (SDValue V = DAG.simplifySelect(N0, N1, N2))
9939 return V;
9940
9941 if (SDValue V = foldBoolSelectToLogic(N, DAG))
9942 return V;
9943
9944 // vselect (not Cond), N1, N2 -> vselect Cond, N2, N1
9945 if (SDValue F = extractBooleanFlip(N0, DAG, TLI, false))
9946 return DAG.getSelect(DL, VT, F, N2, N1);
9947
9948 // Canonicalize integer abs.
9949 // vselect (setg[te] X, 0), X, -X ->
9950 // vselect (setgt X, -1), X, -X ->
9951 // vselect (setl[te] X, 0), -X, X ->
9952 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
9953 if (N0.getOpcode() == ISD::SETCC) {
9954 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
9955 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
9956 bool isAbs = false;
9957 bool RHSIsAllZeros = ISD::isBuildVectorAllZeros(RHS.getNode());
9958
9959 if (((RHSIsAllZeros && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
9960 (ISD::isBuildVectorAllOnes(RHS.getNode()) && CC == ISD::SETGT)) &&
9961 N1 == LHS && N2.getOpcode() == ISD::SUB && N1 == N2.getOperand(1))
9962 isAbs = ISD::isBuildVectorAllZeros(N2.getOperand(0).getNode());
9963 else if ((RHSIsAllZeros && (CC == ISD::SETLT || CC == ISD::SETLE)) &&
9964 N2 == LHS && N1.getOpcode() == ISD::SUB && N2 == N1.getOperand(1))
9965 isAbs = ISD::isBuildVectorAllZeros(N1.getOperand(0).getNode());
9966
9967 if (isAbs) {
9968 if (TLI.isOperationLegalOrCustom(ISD::ABS, VT))
9969 return DAG.getNode(ISD::ABS, DL, VT, LHS);
9970
9971 SDValue Shift = DAG.getNode(ISD::SRA, DL, VT, LHS,
9972 DAG.getConstant(VT.getScalarSizeInBits() - 1,
9973 DL, getShiftAmountTy(VT)));
9974 SDValue Add = DAG.getNode(ISD::ADD, DL, VT, LHS, Shift);
9975 AddToWorklist(Shift.getNode());
9976 AddToWorklist(Add.getNode());
9977 return DAG.getNode(ISD::XOR, DL, VT, Add, Shift);
9978 }
9979
9980 // vselect x, y (fcmp lt x, y) -> fminnum x, y
9981 // vselect x, y (fcmp gt x, y) -> fmaxnum x, y
9982 //
9983 // This is OK if we don't care about what happens if either operand is a
9984 // NaN.
9985 //
9986 if (N0.hasOneUse() && isLegalToCombineMinNumMaxNum(DAG, LHS, RHS, TLI)) {
9987 if (SDValue FMinMax =
9988 combineMinNumMaxNum(DL, VT, LHS, RHS, N1, N2, CC, TLI, DAG))
9989 return FMinMax;
9990 }
9991
9992 // If this select has a condition (setcc) with narrower operands than the
9993 // select, try to widen the compare to match the select width.
9994 // TODO: This should be extended to handle any constant.
9995 // TODO: This could be extended to handle non-loading patterns, but that
9996 // requires thorough testing to avoid regressions.
9997 if (isNullOrNullSplat(RHS)) {
9998 EVT NarrowVT = LHS.getValueType();
9999 EVT WideVT = N1.getValueType().changeVectorElementTypeToInteger();
10000 EVT SetCCVT = getSetCCResultType(LHS.getValueType());
10001 unsigned SetCCWidth = SetCCVT.getScalarSizeInBits();
10002 unsigned WideWidth = WideVT.getScalarSizeInBits();
10003 bool IsSigned = isSignedIntSetCC(CC);
10004 auto LoadExtOpcode = IsSigned ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
10005 if (LHS.getOpcode() == ISD::LOAD && LHS.hasOneUse() &&
10006 SetCCWidth != 1 && SetCCWidth < WideWidth &&
10007 TLI.isLoadExtLegalOrCustom(LoadExtOpcode, WideVT, NarrowVT) &&
10008 TLI.isOperationLegalOrCustom(ISD::SETCC, WideVT)) {
10009 // Both compare operands can be widened for free. The LHS can use an
10010 // extended load, and the RHS is a constant:
10011 // vselect (ext (setcc load(X), C)), N1, N2 -->
10012 // vselect (setcc extload(X), C'), N1, N2
10013 auto ExtOpcode = IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
10014 SDValue WideLHS = DAG.getNode(ExtOpcode, DL, WideVT, LHS);
10015 SDValue WideRHS = DAG.getNode(ExtOpcode, DL, WideVT, RHS);
10016 EVT WideSetCCVT = getSetCCResultType(WideVT);
10017 SDValue WideSetCC = DAG.getSetCC(DL, WideSetCCVT, WideLHS, WideRHS, CC);
10018 return DAG.getSelect(DL, N1.getValueType(), WideSetCC, N1, N2);
10019 }
10020 }
10021
10022 // Match VSELECTs into add with unsigned saturation.
10023 if (hasOperation(ISD::UADDSAT, VT)) {
10024 // Check if one of the arms of the VSELECT is vector with all bits set.
10025 // If it's on the left side invert the predicate to simplify logic below.
10026 SDValue Other;
10027 ISD::CondCode SatCC = CC;
10028 if (ISD::isBuildVectorAllOnes(N1.getNode())) {
10029 Other = N2;
10030 SatCC = ISD::getSetCCInverse(SatCC, VT.getScalarType());
10031 } else if (ISD::isBuildVectorAllOnes(N2.getNode())) {
10032 Other = N1;
10033 }
10034
10035 if (Other && Other.getOpcode() == ISD::ADD) {
10036 SDValue CondLHS = LHS, CondRHS = RHS;
10037 SDValue OpLHS = Other.getOperand(0), OpRHS = Other.getOperand(1);
10038
10039 // Canonicalize condition operands.
10040 if (SatCC == ISD::SETUGE) {
10041 std::swap(CondLHS, CondRHS);
10042 SatCC = ISD::SETULE;
10043 }
10044
10045 // We can test against either of the addition operands.
10046 // x <= x+y ? x+y : ~0 --> uaddsat x, y
10047 // x+y >= x ? x+y : ~0 --> uaddsat x, y
10048 if (SatCC == ISD::SETULE && Other == CondRHS &&
10049 (OpLHS == CondLHS || OpRHS == CondLHS))
10050 return DAG.getNode(ISD::UADDSAT, DL, VT, OpLHS, OpRHS);
10051
10052 if (isa<BuildVectorSDNode>(OpRHS) && isa<BuildVectorSDNode>(CondRHS) &&
10053 CondLHS == OpLHS) {
10054 // If the RHS is a constant we have to reverse the const
10055 // canonicalization.
10056 // x >= ~C ? x+C : ~0 --> uaddsat x, C
10057 auto MatchUADDSAT = [](ConstantSDNode *Op, ConstantSDNode *Cond) {
10058 return Cond->getAPIntValue() == ~Op->getAPIntValue();
10059 };
10060 if (SatCC == ISD::SETULE &&
10061 ISD::matchBinaryPredicate(OpRHS, CondRHS, MatchUADDSAT))
10062 return DAG.getNode(ISD::UADDSAT, DL, VT, OpLHS, OpRHS);
10063 }
10064 }
10065 }
10066
10067 // Match VSELECTs into sub with unsigned saturation.
10068 if (hasOperation(ISD::USUBSAT, VT)) {
10069 // Check if one of the arms of the VSELECT is a zero vector. If it's on
10070 // the left side invert the predicate to simplify logic below.
10071 SDValue Other;
10072 ISD::CondCode SatCC = CC;
10073 if (ISD::isBuildVectorAllZeros(N1.getNode())) {
10074 Other = N2;
10075 SatCC = ISD::getSetCCInverse(SatCC, VT.getScalarType());
10076 } else if (ISD::isBuildVectorAllZeros(N2.getNode())) {
10077 Other = N1;
10078 }
10079
10080 if (Other && Other.getNumOperands() == 2) {
10081 SDValue CondRHS = RHS;
10082 SDValue OpLHS = Other.getOperand(0), OpRHS = Other.getOperand(1);
10083
10084 if (Other.getOpcode() == ISD::SUB &&
10085 LHS.getOpcode() == ISD::ZERO_EXTEND && LHS.getOperand(0) == OpLHS &&
10086 OpRHS.getOpcode() == ISD::TRUNCATE && OpRHS.getOperand(0) == RHS) {
10087 // Look for a general sub with unsigned saturation first.
10088 // zext(x) >= y ? x - trunc(y) : 0
10089 // --> usubsat(x,trunc(umin(y,SatLimit)))
10090 // zext(x) > y ? x - trunc(y) : 0
10091 // --> usubsat(x,trunc(umin(y,SatLimit)))
10092 if (SatCC == ISD::SETUGE || SatCC == ISD::SETUGT)
10093 return getTruncatedUSUBSAT(VT, LHS.getValueType(), LHS, RHS, DAG,
10094 DL);
10095 }
10096
10097 if (OpLHS == LHS) {
10098 // Look for a general sub with unsigned saturation first.
10099 // x >= y ? x-y : 0 --> usubsat x, y
10100 // x > y ? x-y : 0 --> usubsat x, y
10101 if ((SatCC == ISD::SETUGE || SatCC == ISD::SETUGT) &&
10102 Other.getOpcode() == ISD::SUB && OpRHS == CondRHS)
10103 return DAG.getNode(ISD::USUBSAT, DL, VT, OpLHS, OpRHS);
10104
10105 if (auto *OpRHSBV = dyn_cast<BuildVectorSDNode>(OpRHS)) {
10106 if (isa<BuildVectorSDNode>(CondRHS)) {
10107 // If the RHS is a constant we have to reverse the const
10108 // canonicalization.
10109 // x > C-1 ? x+-C : 0 --> usubsat x, C
10110 auto MatchUSUBSAT = [](ConstantSDNode *Op, ConstantSDNode *Cond) {
10111 return (!Op && !Cond) ||
10112 (Op && Cond &&
10113 Cond->getAPIntValue() == (-Op->getAPIntValue() - 1));
10114 };
10115 if (SatCC == ISD::SETUGT && Other.getOpcode() == ISD::ADD &&
10116 ISD::matchBinaryPredicate(OpRHS, CondRHS, MatchUSUBSAT,
10117 /*AllowUndefs*/ true)) {
10118 OpRHS = DAG.getNode(ISD::SUB, DL, VT,
10119 DAG.getConstant(0, DL, VT), OpRHS);
10120 return DAG.getNode(ISD::USUBSAT, DL, VT, OpLHS, OpRHS);
10121 }
10122
10123 // Another special case: If C was a sign bit, the sub has been
10124 // canonicalized into a xor.
10125 // FIXME: Would it be better to use computeKnownBits to determine
10126 // whether it's safe to decanonicalize the xor?
10127 // x s< 0 ? x^C : 0 --> usubsat x, C
10128 if (auto *OpRHSConst = OpRHSBV->getConstantSplatNode()) {
10129 if (SatCC == ISD::SETLT && Other.getOpcode() == ISD::XOR &&
10130 ISD::isBuildVectorAllZeros(CondRHS.getNode()) &&
10131 OpRHSConst->getAPIntValue().isSignMask()) {
10132 // Note that we have to rebuild the RHS constant here to
10133 // ensure we don't rely on particular values of undef lanes.
10134 OpRHS = DAG.getConstant(OpRHSConst->getAPIntValue(), DL, VT);
10135 return DAG.getNode(ISD::USUBSAT, DL, VT, OpLHS, OpRHS);
10136 }
10137 }
10138 }
10139 }
10140 }
10141 }
10142 }
10143 }
10144
10145 if (SimplifySelectOps(N, N1, N2))
10146 return SDValue(N, 0); // Don't revisit N.
10147
10148 // Fold (vselect all_ones, N1, N2) -> N1
10149 if (ISD::isConstantSplatVectorAllOnes(N0.getNode()))
10150 return N1;
10151 // Fold (vselect all_zeros, N1, N2) -> N2
10152 if (ISD::isConstantSplatVectorAllZeros(N0.getNode()))
10153 return N2;
10154
10155 // The ConvertSelectToConcatVector function is assuming both the above
10156 // checks for (vselect (build_vector all{ones,zeros) ...) have been made
10157 // and addressed.
10158 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
10159 N2.getOpcode() == ISD::CONCAT_VECTORS &&
10160 ISD::isBuildVectorOfConstantSDNodes(N0.getNode())) {
10161 if (SDValue CV = ConvertSelectToConcatVector(N, DAG))
10162 return CV;
10163 }
10164
10165 if (SDValue V = foldVSelectOfConstants(N))
10166 return V;
10167
10168 return SDValue();
10169}
10170
10171SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
10172 SDValue N0 = N->getOperand(0);
10173 SDValue N1 = N->getOperand(1);
10174 SDValue N2 = N->getOperand(2);
10175 SDValue N3 = N->getOperand(3);
10176 SDValue N4 = N->getOperand(4);
10177 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
10178
10179 // fold select_cc lhs, rhs, x, x, cc -> x
10180 if (N2 == N3)
10181 return N2;
10182
10183 // Determine if the condition we're dealing with is constant
10184 if (SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()), N0, N1,
10185 CC, SDLoc(N), false)) {
10186 AddToWorklist(SCC.getNode());
10187
10188 if (ConstantSDNode *SCCC = dyn_cast<ConstantSDNode>(SCC.getNode())) {
10189 if (!SCCC->isNullValue())
10190 return N2; // cond always true -> true val
10191 else
10192 return N3; // cond always false -> false val
10193 } else if (SCC->isUndef()) {
10194 // When the condition is UNDEF, just return the first operand. This is
10195 // coherent the DAG creation, no setcc node is created in this case
10196 return N2;
10197 } else if (SCC.getOpcode() == ISD::SETCC) {
10198 // Fold to a simpler select_cc
10199 SDValue SelectOp = DAG.getNode(
10200 ISD::SELECT_CC, SDLoc(N), N2.getValueType(), SCC.getOperand(0),
10201 SCC.getOperand(1), N2, N3, SCC.getOperand(2));
10202 SelectOp->setFlags(SCC->getFlags());
10203 return SelectOp;
10204 }
10205 }
10206
10207 // If we can fold this based on the true/false value, do so.
10208 if (SimplifySelectOps(N, N2, N3))
10209 return SDValue(N, 0); // Don't revisit N.
10210
10211 // fold select_cc into other things, such as min/max/abs
10212 return SimplifySelectCC(SDLoc(N), N0, N1, N2, N3, CC);
10213}
10214
10215SDValue DAGCombiner::visitSETCC(SDNode *N) {
10216 // setcc is very commonly used as an argument to brcond. This pattern
10217 // also lend itself to numerous combines and, as a result, it is desired
10218 // we keep the argument to a brcond as a setcc as much as possible.
10219 bool PreferSetCC =
10220 N->hasOneUse() && N->use_begin()->getOpcode() == ISD::BRCOND;
10221
10222 SDValue Combined = SimplifySetCC(
10223 N->getValueType(0), N->getOperand(0), N->getOperand(1),
10224 cast<CondCodeSDNode>(N->getOperand(2))->get(), SDLoc(N), !PreferSetCC);
10225
10226 if (!Combined)
10227 return SDValue();
10228
10229 // If we prefer to have a setcc, and we don't, we'll try our best to
10230 // recreate one using rebuildSetCC.
10231 if (PreferSetCC && Combined.getOpcode() != ISD::SETCC) {
10232 SDValue NewSetCC = rebuildSetCC(Combined);
10233
10234 // We don't have anything interesting to combine to.
10235 if (NewSetCC.getNode() == N)
10236 return SDValue();
10237
10238 if (NewSetCC)
10239 return NewSetCC;
10240 }
10241
10242 return Combined;
10243}
10244
10245SDValue DAGCombiner::visitSETCCCARRY(SDNode *N) {
10246 SDValue LHS = N->getOperand(0);
10247 SDValue RHS = N->getOperand(1);
10248 SDValue Carry = N->getOperand(2);
10249 SDValue Cond = N->getOperand(3);
10250
10251 // If Carry is false, fold to a regular SETCC.
10252 if (isNullConstant(Carry))
10253 return DAG.getNode(ISD::SETCC, SDLoc(N), N->getVTList(), LHS, RHS, Cond);
10254
10255 return SDValue();
10256}
10257
10258/// Check if N satisfies:
10259/// N is used once.
10260/// N is a Load.
10261/// The load is compatible with ExtOpcode. It means
10262/// If load has explicit zero/sign extension, ExpOpcode must have the same
10263/// extension.
10264/// Otherwise returns true.
10265static bool isCompatibleLoad(SDValue N, unsigned ExtOpcode) {
10266 if (!N.hasOneUse())
10267 return false;
10268
10269 if (!isa<LoadSDNode>(N))
10270 return false;
10271
10272 LoadSDNode *Load = cast<LoadSDNode>(N);
10273 ISD::LoadExtType LoadExt = Load->getExtensionType();
10274 if (LoadExt == ISD::NON_EXTLOAD || LoadExt == ISD::EXTLOAD)
10275 return true;
10276
10277 // Now LoadExt is either SEXTLOAD or ZEXTLOAD, ExtOpcode must have the same
10278 // extension.
10279 if ((LoadExt == ISD::SEXTLOAD && ExtOpcode != ISD::SIGN_EXTEND) ||
10280 (LoadExt == ISD::ZEXTLOAD && ExtOpcode != ISD::ZERO_EXTEND))
10281 return false;
10282
10283 return true;
10284}
10285
10286/// Fold
10287/// (sext (select c, load x, load y)) -> (select c, sextload x, sextload y)
10288/// (zext (select c, load x, load y)) -> (select c, zextload x, zextload y)
10289/// (aext (select c, load x, load y)) -> (select c, extload x, extload y)
10290/// This function is called by the DAGCombiner when visiting sext/zext/aext
10291/// dag nodes (see for example method DAGCombiner::visitSIGN_EXTEND).
10292static SDValue tryToFoldExtendSelectLoad(SDNode *N, const TargetLowering &TLI,
10293 SelectionDAG &DAG) {
10294 unsigned Opcode = N->getOpcode();
10295 SDValue N0 = N->getOperand(0);
10296 EVT VT = N->getValueType(0);
10297 SDLoc DL(N);
10298
10299 assert((Opcode == ISD::SIGN_EXTEND || Opcode == ISD::ZERO_EXTEND ||(static_cast <bool> ((Opcode == ISD::SIGN_EXTEND || Opcode
== ISD::ZERO_EXTEND || Opcode == ISD::ANY_EXTEND) &&
"Expected EXTEND dag node in input!") ? void (0) : __assert_fail
("(Opcode == ISD::SIGN_EXTEND || Opcode == ISD::ZERO_EXTEND || Opcode == ISD::ANY_EXTEND) && \"Expected EXTEND dag node in input!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 10301, __extension__ __PRETTY_FUNCTION__))
10300 Opcode == ISD::ANY_EXTEND) &&(static_cast <bool> ((Opcode == ISD::SIGN_EXTEND || Opcode
== ISD::ZERO_EXTEND || Opcode == ISD::ANY_EXTEND) &&
"Expected EXTEND dag node in input!") ? void (0) : __assert_fail
("(Opcode == ISD::SIGN_EXTEND || Opcode == ISD::ZERO_EXTEND || Opcode == ISD::ANY_EXTEND) && \"Expected EXTEND dag node in input!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 10301, __extension__ __PRETTY_FUNCTION__))
10301 "Expected EXTEND dag node in input!")(static_cast <bool> ((Opcode == ISD::SIGN_EXTEND || Opcode
== ISD::ZERO_EXTEND || Opcode == ISD::ANY_EXTEND) &&
"Expected EXTEND dag node in input!") ? void (0) : __assert_fail
("(Opcode == ISD::SIGN_EXTEND || Opcode == ISD::ZERO_EXTEND || Opcode == ISD::ANY_EXTEND) && \"Expected EXTEND dag node in input!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 10301, __extension__ __PRETTY_FUNCTION__))
;
10302
10303 if (!(N0->getOpcode() == ISD::SELECT || N0->getOpcode() == ISD::VSELECT) ||
10304 !N0.hasOneUse())
10305 return SDValue();
10306
10307 SDValue Op1 = N0->getOperand(1);
10308 SDValue Op2 = N0->getOperand(2);
10309 if (!isCompatibleLoad(Op1, Opcode) || !isCompatibleLoad(Op2, Opcode))
10310 return SDValue();
10311
10312 auto ExtLoadOpcode = ISD::EXTLOAD;
10313 if (Opcode == ISD::SIGN_EXTEND)
10314 ExtLoadOpcode = ISD::SEXTLOAD;
10315 else if (Opcode == ISD::ZERO_EXTEND)
10316 ExtLoadOpcode = ISD::ZEXTLOAD;
10317
10318 LoadSDNode *Load1 = cast<LoadSDNode>(Op1);
10319 LoadSDNode *Load2 = cast<LoadSDNode>(Op2);
10320 if (!TLI.isLoadExtLegal(ExtLoadOpcode, VT, Load1->getMemoryVT()) ||
10321 !TLI.isLoadExtLegal(ExtLoadOpcode, VT, Load2->getMemoryVT()))
10322 return SDValue();
10323
10324 SDValue Ext1 = DAG.getNode(Opcode, DL, VT, Op1);
10325 SDValue Ext2 = DAG.getNode(Opcode, DL, VT, Op2);
10326 return DAG.getSelect(DL, VT, N0->getOperand(0), Ext1, Ext2);
10327}
10328
10329/// Try to fold a sext/zext/aext dag node into a ConstantSDNode or
10330/// a build_vector of constants.
10331/// This function is called by the DAGCombiner when visiting sext/zext/aext
10332/// dag nodes (see for example method DAGCombiner::visitSIGN_EXTEND).
10333/// Vector extends are not folded if operations are legal; this is to
10334/// avoid introducing illegal build_vector dag nodes.
10335static SDValue tryToFoldExtendOfConstant(SDNode *N, const TargetLowering &TLI,
10336 SelectionDAG &DAG, bool LegalTypes) {
10337 unsigned Opcode = N->getOpcode();
10338 SDValue N0 = N->getOperand(0);
10339 EVT VT = N->getValueType(0);
10340 SDLoc DL(N);
10341
10342 assert((Opcode == ISD::SIGN_EXTEND || Opcode == ISD::ZERO_EXTEND ||(static_cast <bool> ((Opcode == ISD::SIGN_EXTEND || Opcode
== ISD::ZERO_EXTEND || Opcode == ISD::ANY_EXTEND || Opcode ==
ISD::SIGN_EXTEND_VECTOR_INREG || Opcode == ISD::ZERO_EXTEND_VECTOR_INREG
) && "Expected EXTEND dag node in input!") ? void (0)
: __assert_fail ("(Opcode == ISD::SIGN_EXTEND || Opcode == ISD::ZERO_EXTEND || Opcode == ISD::ANY_EXTEND || Opcode == ISD::SIGN_EXTEND_VECTOR_INREG || Opcode == ISD::ZERO_EXTEND_VECTOR_INREG) && \"Expected EXTEND dag node in input!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 10345, __extension__ __PRETTY_FUNCTION__))
10343 Opcode == ISD::ANY_EXTEND || Opcode == ISD::SIGN_EXTEND_VECTOR_INREG ||(static_cast <bool> ((Opcode == ISD::SIGN_EXTEND || Opcode
== ISD::ZERO_EXTEND || Opcode == ISD::ANY_EXTEND || Opcode ==
ISD::SIGN_EXTEND_VECTOR_INREG || Opcode == ISD::ZERO_EXTEND_VECTOR_INREG
) && "Expected EXTEND dag node in input!") ? void (0)
: __assert_fail ("(Opcode == ISD::SIGN_EXTEND || Opcode == ISD::ZERO_EXTEND || Opcode == ISD::ANY_EXTEND || Opcode == ISD::SIGN_EXTEND_VECTOR_INREG || Opcode == ISD::ZERO_EXTEND_VECTOR_INREG) && \"Expected EXTEND dag node in input!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 10345, __extension__ __PRETTY_FUNCTION__))
10344 Opcode == ISD::ZERO_EXTEND_VECTOR_INREG)(static_cast <bool> ((Opcode == ISD::SIGN_EXTEND || Opcode
== ISD::ZERO_EXTEND || Opcode == ISD::ANY_EXTEND || Opcode ==
ISD::SIGN_EXTEND_VECTOR_INREG || Opcode == ISD::ZERO_EXTEND_VECTOR_INREG
) && "Expected EXTEND dag node in input!") ? void (0)
: __assert_fail ("(Opcode == ISD::SIGN_EXTEND || Opcode == ISD::ZERO_EXTEND || Opcode == ISD::ANY_EXTEND || Opcode == ISD::SIGN_EXTEND_VECTOR_INREG || Opcode == ISD::ZERO_EXTEND_VECTOR_INREG) && \"Expected EXTEND dag node in input!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 10345, __extension__ __PRETTY_FUNCTION__))
10345 && "Expected EXTEND dag node in input!")(static_cast <bool> ((Opcode == ISD::SIGN_EXTEND || Opcode
== ISD::ZERO_EXTEND || Opcode == ISD::ANY_EXTEND || Opcode ==
ISD::SIGN_EXTEND_VECTOR_INREG || Opcode == ISD::ZERO_EXTEND_VECTOR_INREG
) && "Expected EXTEND dag node in input!") ? void (0)
: __assert_fail ("(Opcode == ISD::SIGN_EXTEND || Opcode == ISD::ZERO_EXTEND || Opcode == ISD::ANY_EXTEND || Opcode == ISD::SIGN_EXTEND_VECTOR_INREG || Opcode == ISD::ZERO_EXTEND_VECTOR_INREG) && \"Expected EXTEND dag node in input!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 10345, __extension__ __PRETTY_FUNCTION__))
;
10346
10347 // fold (sext c1) -> c1
10348 // fold (zext c1) -> c1
10349 // fold (aext c1) -> c1
10350 if (isa<ConstantSDNode>(N0))
10351 return DAG.getNode(Opcode, DL, VT, N0);
10352
10353 // fold (sext (select cond, c1, c2)) -> (select cond, sext c1, sext c2)
10354 // fold (zext (select cond, c1, c2)) -> (select cond, zext c1, zext c2)
10355 // fold (aext (select cond, c1, c2)) -> (select cond, sext c1, sext c2)
10356 if (N0->getOpcode() == ISD::SELECT) {
10357 SDValue Op1 = N0->getOperand(1);
10358 SDValue Op2 = N0->getOperand(2);
10359 if (isa<ConstantSDNode>(Op1) && isa<ConstantSDNode>(Op2) &&
10360 (Opcode != ISD::ZERO_EXTEND || !TLI.isZExtFree(N0.getValueType(), VT))) {
10361 // For any_extend, choose sign extension of the constants to allow a
10362 // possible further transform to sign_extend_inreg.i.e.
10363 //
10364 // t1: i8 = select t0, Constant:i8<-1>, Constant:i8<0>
10365 // t2: i64 = any_extend t1
10366 // -->
10367 // t3: i64 = select t0, Constant:i64<-1>, Constant:i64<0>
10368 // -->
10369 // t4: i64 = sign_extend_inreg t3
10370 unsigned FoldOpc = Opcode;
10371 if (FoldOpc == ISD::ANY_EXTEND)
10372 FoldOpc = ISD::SIGN_EXTEND;
10373 return DAG.getSelect(DL, VT, N0->getOperand(0),
10374 DAG.getNode(FoldOpc, DL, VT, Op1),
10375 DAG.getNode(FoldOpc, DL, VT, Op2));
10376 }
10377 }
10378
10379 // fold (sext (build_vector AllConstants) -> (build_vector AllConstants)
10380 // fold (zext (build_vector AllConstants) -> (build_vector AllConstants)
10381 // fold (aext (build_vector AllConstants) -> (build_vector AllConstants)
10382 EVT SVT = VT.getScalarType();
10383 if (!(VT.isVector() && (!LegalTypes || TLI.isTypeLegal(SVT)) &&
10384 ISD::isBuildVectorOfConstantSDNodes(N0.getNode())))
10385 return SDValue();
10386
10387 // We can fold this node into a build_vector.
10388 unsigned VTBits = SVT.getSizeInBits();
10389 unsigned EVTBits = N0->getValueType(0).getScalarSizeInBits();
10390 SmallVector<SDValue, 8> Elts;
10391 unsigned NumElts = VT.getVectorNumElements();
10392
10393 // For zero-extensions, UNDEF elements still guarantee to have the upper
10394 // bits set to zero.
10395 bool IsZext =
10396 Opcode == ISD::ZERO_EXTEND || Opcode == ISD::ZERO_EXTEND_VECTOR_INREG;
10397
10398 for (unsigned i = 0; i != NumElts; ++i) {
10399 SDValue Op = N0.getOperand(i);
10400 if (Op.isUndef()) {
10401 Elts.push_back(IsZext ? DAG.getConstant(0, DL, SVT) : DAG.getUNDEF(SVT));
10402 continue;
10403 }
10404
10405 SDLoc DL(Op);
10406 // Get the constant value and if needed trunc it to the size of the type.
10407 // Nodes like build_vector might have constants wider than the scalar type.
10408 APInt C = cast<ConstantSDNode>(Op)->getAPIntValue().zextOrTrunc(EVTBits);
10409 if (Opcode == ISD::SIGN_EXTEND || Opcode == ISD::SIGN_EXTEND_VECTOR_INREG)
10410 Elts.push_back(DAG.getConstant(C.sext(VTBits), DL, SVT));
10411 else
10412 Elts.push_back(DAG.getConstant(C.zext(VTBits), DL, SVT));
10413 }
10414
10415 return DAG.getBuildVector(VT, DL, Elts);
10416}
10417
10418// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
10419// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
10420// transformation. Returns true if extension are possible and the above
10421// mentioned transformation is profitable.
10422static bool ExtendUsesToFormExtLoad(EVT VT, SDNode *N, SDValue N0,
10423 unsigned ExtOpc,
10424 SmallVectorImpl<SDNode *> &ExtendNodes,
10425 const TargetLowering &TLI) {
10426 bool HasCopyToRegUses = false;
10427 bool isTruncFree = TLI.isTruncateFree(VT, N0.getValueType());
10428 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
10429 UE = N0.getNode()->use_end();
10430 UI != UE; ++UI) {
10431 SDNode *User = *UI;
10432 if (User == N)
10433 continue;
10434 if (UI.getUse().getResNo() != N0.getResNo())
10435 continue;
10436 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
10437 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
10438 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
10439 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
10440 // Sign bits will be lost after a zext.
10441 return false;
10442 bool Add = false;
10443 for (unsigned i = 0; i != 2; ++i) {
10444 SDValue UseOp = User->getOperand(i);
10445 if (UseOp == N0)
10446 continue;
10447 if (!isa<ConstantSDNode>(UseOp))
10448 return false;
10449 Add = true;
10450 }
10451 if (Add)
10452 ExtendNodes.push_back(User);
10453 continue;
10454 }
10455 // If truncates aren't free and there are users we can't
10456 // extend, it isn't worthwhile.
10457 if (!isTruncFree)
10458 return false;
10459 // Remember if this value is live-out.
10460 if (User->getOpcode() == ISD::CopyToReg)
10461 HasCopyToRegUses = true;
10462 }
10463
10464 if (HasCopyToRegUses) {
10465 bool BothLiveOut = false;
10466 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
10467 UI != UE; ++UI) {
10468 SDUse &Use = UI.getUse();
10469 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
10470 BothLiveOut = true;
10471 break;
10472 }
10473 }
10474 if (BothLiveOut)
10475 // Both unextended and extended values are live out. There had better be
10476 // a good reason for the transformation.
10477 return ExtendNodes.size();
10478 }
10479 return true;
10480}
10481
10482void DAGCombiner::ExtendSetCCUses(const SmallVectorImpl<SDNode *> &SetCCs,
10483 SDValue OrigLoad, SDValue ExtLoad,
10484 ISD::NodeType ExtType) {
10485 // Extend SetCC uses if necessary.
10486 SDLoc DL(ExtLoad);
10487 for (SDNode *SetCC : SetCCs) {
10488 SmallVector<SDValue, 4> Ops;
10489
10490 for (unsigned j = 0; j != 2; ++j) {
10491 SDValue SOp = SetCC->getOperand(j);
10492 if (SOp == OrigLoad)
10493 Ops.push_back(ExtLoad);
10494 else
10495 Ops.push_back(DAG.getNode(ExtType, DL, ExtLoad->getValueType(0), SOp));
10496 }
10497
10498 Ops.push_back(SetCC->getOperand(2));
10499 CombineTo(SetCC, DAG.getNode(ISD::SETCC, DL, SetCC->getValueType(0), Ops));
10500 }
10501}
10502
10503// FIXME: Bring more similar combines here, common to sext/zext (maybe aext?).
10504SDValue DAGCombiner::CombineExtLoad(SDNode *N) {
10505 SDValue N0 = N->getOperand(0);
10506 EVT DstVT = N->getValueType(0);
10507 EVT SrcVT = N0.getValueType();
10508
10509 assert((N->getOpcode() == ISD::SIGN_EXTEND ||(static_cast <bool> ((N->getOpcode() == ISD::SIGN_EXTEND
|| N->getOpcode() == ISD::ZERO_EXTEND) && "Unexpected node type (not an extend)!"
) ? void (0) : __assert_fail ("(N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND) && \"Unexpected node type (not an extend)!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 10511, __extension__ __PRETTY_FUNCTION__))
10510 N->getOpcode() == ISD::ZERO_EXTEND) &&(static_cast <bool> ((N->getOpcode() == ISD::SIGN_EXTEND
|| N->getOpcode() == ISD::ZERO_EXTEND) && "Unexpected node type (not an extend)!"
) ? void (0) : __assert_fail ("(N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND) && \"Unexpected node type (not an extend)!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 10511, __extension__ __PRETTY_FUNCTION__))
10511 "Unexpected node type (not an extend)!")(static_cast <bool> ((N->getOpcode() == ISD::SIGN_EXTEND
|| N->getOpcode() == ISD::ZERO_EXTEND) && "Unexpected node type (not an extend)!"
) ? void (0) : __assert_fail ("(N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND) && \"Unexpected node type (not an extend)!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 10511, __extension__ __PRETTY_FUNCTION__))
;
10512
10513 // fold (sext (load x)) to multiple smaller sextloads; same for zext.
10514 // For example, on a target with legal v4i32, but illegal v8i32, turn:
10515 // (v8i32 (sext (v8i16 (load x))))
10516 // into:
10517 // (v8i32 (concat_vectors (v4i32 (sextload x)),
10518 // (v4i32 (sextload (x + 16)))))
10519 // Where uses of the original load, i.e.:
10520 // (v8i16 (load x))
10521 // are replaced with:
10522 // (v8i16 (truncate
10523 // (v8i32 (concat_vectors (v4i32 (sextload x)),
10524 // (v4i32 (sextload (x + 16)))))))
10525 //
10526 // This combine is only applicable to illegal, but splittable, vectors.
10527 // All legal types, and illegal non-vector types, are handled elsewhere.
10528 // This combine is controlled by TargetLowering::isVectorLoadExtDesirable.
10529 //
10530 if (N0->getOpcode() != ISD::LOAD)
10531 return SDValue();
10532
10533 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
10534
10535 if (!ISD::isNON_EXTLoad(LN0) || !ISD::isUNINDEXEDLoad(LN0) ||
10536 !N0.hasOneUse() || !LN0->isSimple() ||
10537 !DstVT.isVector() || !DstVT.isPow2VectorType() ||
10538 !TLI.isVectorLoadExtDesirable(SDValue(N, 0)))
10539 return SDValue();
10540
10541 SmallVector<SDNode *, 4> SetCCs;
10542 if (!ExtendUsesToFormExtLoad(DstVT, N, N0, N->getOpcode(), SetCCs, TLI))
10543 return SDValue();
10544
10545 ISD::LoadExtType ExtType =
10546 N->getOpcode() == ISD::SIGN_EXTEND ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
10547
10548 // Try to split the vector types to get down to legal types.
10549 EVT SplitSrcVT = SrcVT;
10550 EVT SplitDstVT = DstVT;
10551 while (!TLI.isLoadExtLegalOrCustom(ExtType, SplitDstVT, SplitSrcVT) &&
10552 SplitSrcVT.getVectorNumElements() > 1) {
10553 SplitDstVT = DAG.GetSplitDestVTs(SplitDstVT).first;
10554 SplitSrcVT = DAG.GetSplitDestVTs(SplitSrcVT).first;
10555 }
10556
10557 if (!TLI.isLoadExtLegalOrCustom(ExtType, SplitDstVT, SplitSrcVT))
10558 return SDValue();
10559
10560 assert(!DstVT.isScalableVector() && "Unexpected scalable vector type")(static_cast <bool> (!DstVT.isScalableVector() &&
"Unexpected scalable vector type") ? void (0) : __assert_fail
("!DstVT.isScalableVector() && \"Unexpected scalable vector type\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 10560, __extension__ __PRETTY_FUNCTION__))
;
10561
10562 SDLoc DL(N);
10563 const unsigned NumSplits =
10564 DstVT.getVectorNumElements() / SplitDstVT.getVectorNumElements();
10565 const unsigned Stride = SplitSrcVT.getStoreSize();
10566 SmallVector<SDValue, 4> Loads;
10567 SmallVector<SDValue, 4> Chains;
10568
10569 SDValue BasePtr = LN0->getBasePtr();
10570 for (unsigned Idx = 0; Idx < NumSplits; Idx++) {
10571 const unsigned Offset = Idx * Stride;
10572 const Align Align = commonAlignment(LN0->getAlign(), Offset);
10573
10574 SDValue SplitLoad = DAG.getExtLoad(
10575 ExtType, SDLoc(LN0), SplitDstVT, LN0->getChain(), BasePtr,
10576 LN0->getPointerInfo().getWithOffset(Offset), SplitSrcVT, Align,
10577 LN0->getMemOperand()->getFlags(), LN0->getAAInfo());
10578
10579 BasePtr = DAG.getMemBasePlusOffset(BasePtr, TypeSize::Fixed(Stride), DL);
10580
10581 Loads.push_back(SplitLoad.getValue(0));
10582 Chains.push_back(SplitLoad.getValue(1));
10583 }
10584
10585 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
10586 SDValue NewValue = DAG.getNode(ISD::CONCAT_VECTORS, DL, DstVT, Loads);
10587
10588 // Simplify TF.
10589 AddToWorklist(NewChain.getNode());
10590
10591 CombineTo(N, NewValue);
10592
10593 // Replace uses of the original load (before extension)
10594 // with a truncate of the concatenated sextloaded vectors.
10595 SDValue Trunc =
10596 DAG.getNode(ISD::TRUNCATE, SDLoc(N0), N0.getValueType(), NewValue);
10597 ExtendSetCCUses(SetCCs, N0, NewValue, (ISD::NodeType)N->getOpcode());
10598 CombineTo(N0.getNode(), Trunc, NewChain);
10599 return SDValue(N, 0); // Return N so it doesn't get rechecked!
10600}
10601
10602// fold (zext (and/or/xor (shl/shr (load x), cst), cst)) ->
10603// (and/or/xor (shl/shr (zextload x), (zext cst)), (zext cst))
10604SDValue DAGCombiner::CombineZExtLogicopShiftLoad(SDNode *N) {
10605 assert(N->getOpcode() == ISD::ZERO_EXTEND)(static_cast <bool> (N->getOpcode() == ISD::ZERO_EXTEND
) ? void (0) : __assert_fail ("N->getOpcode() == ISD::ZERO_EXTEND"
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 10605, __extension__ __PRETTY_FUNCTION__))
;
10606 EVT VT = N->getValueType(0);
10607 EVT OrigVT = N->getOperand(0).getValueType();
10608 if (TLI.isZExtFree(OrigVT, VT))
10609 return SDValue();
10610
10611 // and/or/xor
10612 SDValue N0 = N->getOperand(0);
10613 if (!(N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
10614 N0.getOpcode() == ISD::XOR) ||
10615 N0.getOperand(1).getOpcode() != ISD::Constant ||
10616 (LegalOperations && !TLI.isOperationLegal(N0.getOpcode(), VT)))
10617 return SDValue();
10618
10619 // shl/shr
10620 SDValue N1 = N0->getOperand(0);
10621 if (!(N1.getOpcode() == ISD::SHL || N1.getOpcode() == ISD::SRL) ||
10622 N1.getOperand(1).getOpcode() != ISD::Constant ||
10623 (LegalOperations && !TLI.isOperationLegal(N1.getOpcode(), VT)))
10624 return SDValue();
10625
10626 // load
10627 if (!isa<LoadSDNode>(N1.getOperand(0)))
10628 return SDValue();
10629 LoadSDNode *Load = cast<LoadSDNode>(N1.getOperand(0));
10630 EVT MemVT = Load->getMemoryVT();
10631 if (!TLI.isLoadExtLegal(ISD::ZEXTLOAD, VT, MemVT) ||
10632 Load->getExtensionType() == ISD::SEXTLOAD || Load->isIndexed())
10633 return SDValue();
10634
10635
10636 // If the shift op is SHL, the logic op must be AND, otherwise the result
10637 // will be wrong.
10638 if (N1.getOpcode() == ISD::SHL && N0.getOpcode() != ISD::AND)
10639 return SDValue();
10640
10641 if (!N0.hasOneUse() || !N1.hasOneUse())
10642 return SDValue();
10643
10644 SmallVector<SDNode*, 4> SetCCs;
10645 if (!ExtendUsesToFormExtLoad(VT, N1.getNode(), N1.getOperand(0),
10646 ISD::ZERO_EXTEND, SetCCs, TLI))
10647 return SDValue();
10648
10649 // Actually do the transformation.
10650 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(Load), VT,
10651 Load->getChain(), Load->getBasePtr(),
10652 Load->getMemoryVT(), Load->getMemOperand());
10653
10654 SDLoc DL1(N1);
10655 SDValue Shift = DAG.getNode(N1.getOpcode(), DL1, VT, ExtLoad,
10656 N1.getOperand(1));
10657
10658 APInt Mask = N0.getConstantOperandAPInt(1).zext(VT.getSizeInBits());
10659 SDLoc DL0(N0);
10660 SDValue And = DAG.getNode(N0.getOpcode(), DL0, VT, Shift,
10661 DAG.getConstant(Mask, DL0, VT));
10662
10663 ExtendSetCCUses(SetCCs, N1.getOperand(0), ExtLoad, ISD::ZERO_EXTEND);
10664 CombineTo(N, And);
10665 if (SDValue(Load, 0).hasOneUse()) {
10666 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), ExtLoad.getValue(1));
10667 } else {
10668 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(Load),
10669 Load->getValueType(0), ExtLoad);
10670 CombineTo(Load, Trunc, ExtLoad.getValue(1));
10671 }
10672
10673 // N0 is dead at this point.
10674 recursivelyDeleteUnusedNodes(N0.getNode());
10675
10676 return SDValue(N,0); // Return N so it doesn't get rechecked!
10677}
10678
10679/// If we're narrowing or widening the result of a vector select and the final
10680/// size is the same size as a setcc (compare) feeding the select, then try to
10681/// apply the cast operation to the select's operands because matching vector
10682/// sizes for a select condition and other operands should be more efficient.
10683SDValue DAGCombiner::matchVSelectOpSizesWithSetCC(SDNode *Cast) {
10684 unsigned CastOpcode = Cast->getOpcode();
10685 assert((CastOpcode == ISD::SIGN_EXTEND || CastOpcode == ISD::ZERO_EXTEND ||(static_cast <bool> ((CastOpcode == ISD::SIGN_EXTEND ||
CastOpcode == ISD::ZERO_EXTEND || CastOpcode == ISD::TRUNCATE
|| CastOpcode == ISD::FP_EXTEND || CastOpcode == ISD::FP_ROUND
) && "Unexpected opcode for vector select narrowing/widening"
) ? void (0) : __assert_fail ("(CastOpcode == ISD::SIGN_EXTEND || CastOpcode == ISD::ZERO_EXTEND || CastOpcode == ISD::TRUNCATE || CastOpcode == ISD::FP_EXTEND || CastOpcode == ISD::FP_ROUND) && \"Unexpected opcode for vector select narrowing/widening\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 10688, __extension__ __PRETTY_FUNCTION__))
10686 CastOpcode == ISD::TRUNCATE || CastOpcode == ISD::FP_EXTEND ||(static_cast <bool> ((CastOpcode == ISD::SIGN_EXTEND ||
CastOpcode == ISD::ZERO_EXTEND || CastOpcode == ISD::TRUNCATE
|| CastOpcode == ISD::FP_EXTEND || CastOpcode == ISD::FP_ROUND
) && "Unexpected opcode for vector select narrowing/widening"
) ? void (0) : __assert_fail ("(CastOpcode == ISD::SIGN_EXTEND || CastOpcode == ISD::ZERO_EXTEND || CastOpcode == ISD::TRUNCATE || CastOpcode == ISD::FP_EXTEND || CastOpcode == ISD::FP_ROUND) && \"Unexpected opcode for vector select narrowing/widening\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 10688, __extension__ __PRETTY_FUNCTION__))
10687 CastOpcode == ISD::FP_ROUND) &&(static_cast <bool> ((CastOpcode == ISD::SIGN_EXTEND ||
CastOpcode == ISD::ZERO_EXTEND || CastOpcode == ISD::TRUNCATE
|| CastOpcode == ISD::FP_EXTEND || CastOpcode == ISD::FP_ROUND
) && "Unexpected opcode for vector select narrowing/widening"
) ? void (0) : __assert_fail ("(CastOpcode == ISD::SIGN_EXTEND || CastOpcode == ISD::ZERO_EXTEND || CastOpcode == ISD::TRUNCATE || CastOpcode == ISD::FP_EXTEND || CastOpcode == ISD::FP_ROUND) && \"Unexpected opcode for vector select narrowing/widening\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 10688, __extension__ __PRETTY_FUNCTION__))
10688 "Unexpected opcode for vector select narrowing/widening")(static_cast <bool> ((CastOpcode == ISD::SIGN_EXTEND ||
CastOpcode == ISD::ZERO_EXTEND || CastOpcode == ISD::TRUNCATE
|| CastOpcode == ISD::FP_EXTEND || CastOpcode == ISD::FP_ROUND
) && "Unexpected opcode for vector select narrowing/widening"
) ? void (0) : __assert_fail ("(CastOpcode == ISD::SIGN_EXTEND || CastOpcode == ISD::ZERO_EXTEND || CastOpcode == ISD::TRUNCATE || CastOpcode == ISD::FP_EXTEND || CastOpcode == ISD::FP_ROUND) && \"Unexpected opcode for vector select narrowing/widening\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 10688, __extension__ __PRETTY_FUNCTION__))
;
10689
10690 // We only do this transform before legal ops because the pattern may be
10691 // obfuscated by target-specific operations after legalization. Do not create
10692 // an illegal select op, however, because that may be difficult to lower.
10693 EVT VT = Cast->getValueType(0);
10694 if (LegalOperations || !TLI.isOperationLegalOrCustom(ISD::VSELECT, VT))
10695 return SDValue();
10696
10697 SDValue VSel = Cast->getOperand(0);
10698 if (VSel.getOpcode() != ISD::VSELECT || !VSel.hasOneUse() ||
10699 VSel.getOperand(0).getOpcode() != ISD::SETCC)
10700 return SDValue();
10701
10702 // Does the setcc have the same vector size as the casted select?
10703 SDValue SetCC = VSel.getOperand(0);
10704 EVT SetCCVT = getSetCCResultType(SetCC.getOperand(0).getValueType());
10705 if (SetCCVT.getSizeInBits() != VT.getSizeInBits())
10706 return SDValue();
10707
10708 // cast (vsel (setcc X), A, B) --> vsel (setcc X), (cast A), (cast B)
10709 SDValue A = VSel.getOperand(1);
10710 SDValue B = VSel.getOperand(2);
10711 SDValue CastA, CastB;
10712 SDLoc DL(Cast);
10713 if (CastOpcode == ISD::FP_ROUND) {
10714 // FP_ROUND (fptrunc) has an extra flag operand to pass along.
10715 CastA = DAG.getNode(CastOpcode, DL, VT, A, Cast->getOperand(1));
10716 CastB = DAG.getNode(CastOpcode, DL, VT, B, Cast->getOperand(1));
10717 } else {
10718 CastA = DAG.getNode(CastOpcode, DL, VT, A);
10719 CastB = DAG.getNode(CastOpcode, DL, VT, B);
10720 }
10721 return DAG.getNode(ISD::VSELECT, DL, VT, SetCC, CastA, CastB);
10722}
10723
10724// fold ([s|z]ext ([s|z]extload x)) -> ([s|z]ext (truncate ([s|z]extload x)))
10725// fold ([s|z]ext ( extload x)) -> ([s|z]ext (truncate ([s|z]extload x)))
10726static SDValue tryToFoldExtOfExtload(SelectionDAG &DAG, DAGCombiner &Combiner,
10727 const TargetLowering &TLI, EVT VT,
10728 bool LegalOperations, SDNode *N,
10729 SDValue N0, ISD::LoadExtType ExtLoadType) {
10730 SDNode *N0Node = N0.getNode();
10731 bool isAExtLoad = (ExtLoadType == ISD::SEXTLOAD) ? ISD::isSEXTLoad(N0Node)
10732 : ISD::isZEXTLoad(N0Node);
10733 if ((!isAExtLoad && !ISD::isEXTLoad(N0Node)) ||
10734 !ISD::isUNINDEXEDLoad(N0Node) || !N0.hasOneUse())
10735 return SDValue();
10736
10737 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
10738 EVT MemVT = LN0->getMemoryVT();
10739 if ((LegalOperations || !LN0->isSimple() ||
10740 VT.isVector()) &&
10741 !TLI.isLoadExtLegal(ExtLoadType, VT, MemVT))
10742 return SDValue();
10743
10744 SDValue ExtLoad =
10745 DAG.getExtLoad(ExtLoadType, SDLoc(LN0), VT, LN0->getChain(),
10746 LN0->getBasePtr(), MemVT, LN0->getMemOperand());
10747 Combiner.CombineTo(N, ExtLoad);
10748 DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1), ExtLoad.getValue(1));
10749 if (LN0->use_empty())
10750 Combiner.recursivelyDeleteUnusedNodes(LN0);
10751 return SDValue(N, 0); // Return N so it doesn't get rechecked!
10752}
10753
10754// fold ([s|z]ext (load x)) -> ([s|z]ext (truncate ([s|z]extload x)))
10755// Only generate vector extloads when 1) they're legal, and 2) they are
10756// deemed desirable by the target.
10757static SDValue tryToFoldExtOfLoad(SelectionDAG &DAG, DAGCombiner &Combiner,
10758 const TargetLowering &TLI, EVT VT,
10759 bool LegalOperations, SDNode *N, SDValue N0,
10760 ISD::LoadExtType ExtLoadType,
10761 ISD::NodeType ExtOpc) {
10762 if (!ISD::isNON_EXTLoad(N0.getNode()) ||
10763 !ISD::isUNINDEXEDLoad(N0.getNode()) ||
10764 ((LegalOperations || VT.isVector() ||
10765 !cast<LoadSDNode>(N0)->isSimple()) &&
10766 !TLI.isLoadExtLegal(ExtLoadType, VT, N0.getValueType())))
10767 return {};
10768
10769 bool DoXform = true;
10770 SmallVector<SDNode *, 4> SetCCs;
10771 if (!N0.hasOneUse())
10772 DoXform = ExtendUsesToFormExtLoad(VT, N, N0, ExtOpc, SetCCs, TLI);
10773 if (VT.isVector())
10774 DoXform &= TLI.isVectorLoadExtDesirable(SDValue(N, 0));
10775 if (!DoXform)
10776 return {};
10777
10778 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
10779 SDValue ExtLoad = DAG.getExtLoad(ExtLoadType, SDLoc(LN0), VT, LN0->getChain(),
10780 LN0->getBasePtr(), N0.getValueType(),
10781 LN0->getMemOperand());
10782 Combiner.ExtendSetCCUses(SetCCs, N0, ExtLoad, ExtOpc);
10783 // If the load value is used only by N, replace it via CombineTo N.
10784 bool NoReplaceTrunc = SDValue(LN0, 0).hasOneUse();
10785 Combiner.CombineTo(N, ExtLoad);
10786 if (NoReplaceTrunc) {
10787 DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1), ExtLoad.getValue(1));
10788 Combiner.recursivelyDeleteUnusedNodes(LN0);
10789 } else {
10790 SDValue Trunc =
10791 DAG.getNode(ISD::TRUNCATE, SDLoc(N0), N0.getValueType(), ExtLoad);
10792 Combiner.CombineTo(LN0, Trunc, ExtLoad.getValue(1));
10793 }
10794 return SDValue(N, 0); // Return N so it doesn't get rechecked!
10795}
10796
10797static SDValue tryToFoldExtOfMaskedLoad(SelectionDAG &DAG,
10798 const TargetLowering &TLI, EVT VT,
10799 SDNode *N, SDValue N0,
10800 ISD::LoadExtType ExtLoadType,
10801 ISD::NodeType ExtOpc) {
10802 if (!N0.hasOneUse())
10803 return SDValue();
10804
10805 MaskedLoadSDNode *Ld = dyn_cast<MaskedLoadSDNode>(N0);
10806 if (!Ld || Ld->getExtensionType() != ISD::NON_EXTLOAD)
10807 return SDValue();
10808
10809 if (!TLI.isLoadExtLegal(ExtLoadType, VT, Ld->getValueType(0)))
10810 return SDValue();
10811
10812 if (!TLI.isVectorLoadExtDesirable(SDValue(N, 0)))
10813 return SDValue();
10814
10815 SDLoc dl(Ld);
10816 SDValue PassThru = DAG.getNode(ExtOpc, dl, VT, Ld->getPassThru());
10817 SDValue NewLoad = DAG.getMaskedLoad(
10818 VT, dl, Ld->getChain(), Ld->getBasePtr(), Ld->getOffset(), Ld->getMask(),
10819 PassThru, Ld->getMemoryVT(), Ld->getMemOperand(), Ld->getAddressingMode(),
10820 ExtLoadType, Ld->isExpandingLoad());
10821 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), SDValue(NewLoad.getNode(), 1));
10822 return NewLoad;
10823}
10824
10825static SDValue foldExtendedSignBitTest(SDNode *N, SelectionDAG &DAG,
10826 bool LegalOperations) {
10827 assert((N->getOpcode() == ISD::SIGN_EXTEND ||(static_cast <bool> ((N->getOpcode() == ISD::SIGN_EXTEND
|| N->getOpcode() == ISD::ZERO_EXTEND) && "Expected sext or zext"
) ? void (0) : __assert_fail ("(N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND) && \"Expected sext or zext\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 10828, __extension__ __PRETTY_FUNCTION__))
10828 N->getOpcode() == ISD::ZERO_EXTEND) && "Expected sext or zext")(static_cast <bool> ((N->getOpcode() == ISD::SIGN_EXTEND
|| N->getOpcode() == ISD::ZERO_EXTEND) && "Expected sext or zext"
) ? void (0) : __assert_fail ("(N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND) && \"Expected sext or zext\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 10828, __extension__ __PRETTY_FUNCTION__))
;
10829
10830 SDValue SetCC = N->getOperand(0);
10831 if (LegalOperations || SetCC.getOpcode() != ISD::SETCC ||
10832 !SetCC.hasOneUse() || SetCC.getValueType() != MVT::i1)
10833 return SDValue();
10834
10835 SDValue X = SetCC.getOperand(0);
10836 SDValue Ones = SetCC.getOperand(1);
10837 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
10838 EVT VT = N->getValueType(0);
10839 EVT XVT = X.getValueType();
10840 // setge X, C is canonicalized to setgt, so we do not need to match that
10841 // pattern. The setlt sibling is folded in SimplifySelectCC() because it does
10842 // not require the 'not' op.
10843 if (CC == ISD::SETGT && isAllOnesConstant(Ones) && VT == XVT) {
10844 // Invert and smear/shift the sign bit:
10845 // sext i1 (setgt iN X, -1) --> sra (not X), (N - 1)
10846 // zext i1 (setgt iN X, -1) --> srl (not X), (N - 1)
10847 SDLoc DL(N);
10848 unsigned ShCt = VT.getSizeInBits() - 1;
10849 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10850 if (!TLI.shouldAvoidTransformToShift(VT, ShCt)) {
10851 SDValue NotX = DAG.getNOT(DL, X, VT);
10852 SDValue ShiftAmount = DAG.getConstant(ShCt, DL, VT);
10853 auto ShiftOpcode =
10854 N->getOpcode() == ISD::SIGN_EXTEND ? ISD::SRA : ISD::SRL;
10855 return DAG.getNode(ShiftOpcode, DL, VT, NotX, ShiftAmount);
10856 }
10857 }
10858 return SDValue();
10859}
10860
10861SDValue DAGCombiner::foldSextSetcc(SDNode *N) {
10862 SDValue N0 = N->getOperand(0);
10863 if (N0.getOpcode() != ISD::SETCC)
10864 return SDValue();
10865
10866 SDValue N00 = N0.getOperand(0);
10867 SDValue N01 = N0.getOperand(1);
10868 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
10869 EVT VT = N->getValueType(0);
10870 EVT N00VT = N00.getValueType();
10871 SDLoc DL(N);
10872
10873 // On some architectures (such as SSE/NEON/etc) the SETCC result type is
10874 // the same size as the compared operands. Try to optimize sext(setcc())
10875 // if this is the case.
10876 if (VT.isVector() && !LegalOperations &&
10877 TLI.getBooleanContents(N00VT) ==
10878 TargetLowering::ZeroOrNegativeOneBooleanContent) {
10879 EVT SVT = getSetCCResultType(N00VT);
10880
10881 // If we already have the desired type, don't change it.
10882 if (SVT != N0.getValueType()) {
10883 // We know that the # elements of the results is the same as the
10884 // # elements of the compare (and the # elements of the compare result
10885 // for that matter). Check to see that they are the same size. If so,
10886 // we know that the element size of the sext'd result matches the
10887 // element size of the compare operands.
10888 if (VT.getSizeInBits() == SVT.getSizeInBits())
10889 return DAG.getSetCC(DL, VT, N00, N01, CC);
10890
10891 // If the desired elements are smaller or larger than the source
10892 // elements, we can use a matching integer vector type and then
10893 // truncate/sign extend.
10894 EVT MatchingVecType = N00VT.changeVectorElementTypeToInteger();
10895 if (SVT == MatchingVecType) {
10896 SDValue VsetCC = DAG.getSetCC(DL, MatchingVecType, N00, N01, CC);
10897 return DAG.getSExtOrTrunc(VsetCC, DL, VT);
10898 }
10899 }
10900
10901 // Try to eliminate the sext of a setcc by zexting the compare operands.
10902 if (N0.hasOneUse() && TLI.isOperationLegalOrCustom(ISD::SETCC, VT) &&
10903 !TLI.isOperationLegalOrCustom(ISD::SETCC, SVT)) {
10904 bool IsSignedCmp = ISD::isSignedIntSetCC(CC);
10905 unsigned LoadOpcode = IsSignedCmp ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
10906 unsigned ExtOpcode = IsSignedCmp ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
10907
10908 // We have an unsupported narrow vector compare op that would be legal
10909 // if extended to the destination type. See if the compare operands
10910 // can be freely extended to the destination type.
10911 auto IsFreeToExtend = [&](SDValue V) {
10912 if (isConstantOrConstantVector(V, /*NoOpaques*/ true))
10913 return true;
10914 // Match a simple, non-extended load that can be converted to a
10915 // legal {z/s}ext-load.
10916 // TODO: Allow widening of an existing {z/s}ext-load?
10917 if (!(ISD::isNON_EXTLoad(V.getNode()) &&
10918 ISD::isUNINDEXEDLoad(V.getNode()) &&
10919 cast<LoadSDNode>(V)->isSimple() &&
10920 TLI.isLoadExtLegal(LoadOpcode, VT, V.getValueType())))
10921 return false;
10922
10923 // Non-chain users of this value must either be the setcc in this
10924 // sequence or extends that can be folded into the new {z/s}ext-load.
10925 for (SDNode::use_iterator UI = V->use_begin(), UE = V->use_end();
10926 UI != UE; ++UI) {
10927 // Skip uses of the chain and the setcc.
10928 SDNode *User = *UI;
10929 if (UI.getUse().getResNo() != 0 || User == N0.getNode())
10930 continue;
10931 // Extra users must have exactly the same cast we are about to create.
10932 // TODO: This restriction could be eased if ExtendUsesToFormExtLoad()
10933 // is enhanced similarly.
10934 if (User->getOpcode() != ExtOpcode || User->getValueType(0) != VT)
10935 return false;
10936 }
10937 return true;
10938 };
10939
10940 if (IsFreeToExtend(N00) && IsFreeToExtend(N01)) {
10941 SDValue Ext0 = DAG.getNode(ExtOpcode, DL, VT, N00);
10942 SDValue Ext1 = DAG.getNode(ExtOpcode, DL, VT, N01);
10943 return DAG.getSetCC(DL, VT, Ext0, Ext1, CC);
10944 }
10945 }
10946 }
10947
10948 // sext(setcc x, y, cc) -> (select (setcc x, y, cc), T, 0)
10949 // Here, T can be 1 or -1, depending on the type of the setcc and
10950 // getBooleanContents().
10951 unsigned SetCCWidth = N0.getScalarValueSizeInBits();
10952
10953 // To determine the "true" side of the select, we need to know the high bit
10954 // of the value returned by the setcc if it evaluates to true.
10955 // If the type of the setcc is i1, then the true case of the select is just
10956 // sext(i1 1), that is, -1.
10957 // If the type of the setcc is larger (say, i8) then the value of the high
10958 // bit depends on getBooleanContents(), so ask TLI for a real "true" value
10959 // of the appropriate width.
10960 SDValue ExtTrueVal = (SetCCWidth == 1)
10961 ? DAG.getAllOnesConstant(DL, VT)
10962 : DAG.getBoolConstant(true, DL, VT, N00VT);
10963 SDValue Zero = DAG.getConstant(0, DL, VT);
10964 if (SDValue SCC = SimplifySelectCC(DL, N00, N01, ExtTrueVal, Zero, CC, true))
10965 return SCC;
10966
10967 if (!VT.isVector() && !TLI.convertSelectOfConstantsToMath(VT)) {
10968 EVT SetCCVT = getSetCCResultType(N00VT);
10969 // Don't do this transform for i1 because there's a select transform
10970 // that would reverse it.
10971 // TODO: We should not do this transform at all without a target hook
10972 // because a sext is likely cheaper than a select?
10973 if (SetCCVT.getScalarSizeInBits() != 1 &&
10974 (!LegalOperations || TLI.isOperationLegal(ISD::SETCC, N00VT))) {
10975 SDValue SetCC = DAG.getSetCC(DL, SetCCVT, N00, N01, CC);
10976 return DAG.getSelect(DL, VT, SetCC, ExtTrueVal, Zero);
10977 }
10978 }
10979
10980 return SDValue();
10981}
10982
10983SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
10984 SDValue N0 = N->getOperand(0);
10985 EVT VT = N->getValueType(0);
10986 SDLoc DL(N);
10987
10988 if (SDValue Res = tryToFoldExtendOfConstant(N, TLI, DAG, LegalTypes))
10989 return Res;
10990
10991 // fold (sext (sext x)) -> (sext x)
10992 // fold (sext (aext x)) -> (sext x)
10993 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
10994 return DAG.getNode(ISD::SIGN_EXTEND, DL, VT, N0.getOperand(0));
10995
10996 if (N0.getOpcode() == ISD::TRUNCATE) {
10997 // fold (sext (truncate (load x))) -> (sext (smaller load x))
10998 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
10999 if (SDValue NarrowLoad = ReduceLoadWidth(N0.getNode())) {
11000 SDNode *oye = N0.getOperand(0).getNode();
11001 if (NarrowLoad.getNode() != N0.getNode()) {
11002 CombineTo(N0.getNode(), NarrowLoad);
11003 // CombineTo deleted the truncate, if needed, but not what's under it.
11004 AddToWorklist(oye);
11005 }
11006 return SDValue(N, 0); // Return N so it doesn't get rechecked!
11007 }
11008
11009 // See if the value being truncated is already sign extended. If so, just
11010 // eliminate the trunc/sext pair.
11011 SDValue Op = N0.getOperand(0);
11012 unsigned OpBits = Op.getScalarValueSizeInBits();
11013 unsigned MidBits = N0.getScalarValueSizeInBits();
11014 unsigned DestBits = VT.getScalarSizeInBits();
11015 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
11016
11017 if (OpBits == DestBits) {
11018 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
11019 // bits, it is already ready.
11020 if (NumSignBits > DestBits-MidBits)
11021 return Op;
11022 } else if (OpBits < DestBits) {
11023 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
11024 // bits, just sext from i32.
11025 if (NumSignBits > OpBits-MidBits)
11026 return DAG.getNode(ISD::SIGN_EXTEND, DL, VT, Op);
11027 } else {
11028 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
11029 // bits, just truncate to i32.
11030 if (NumSignBits > OpBits-MidBits)
11031 return DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
11032 }
11033
11034 // fold (sext (truncate x)) -> (sextinreg x).
11035 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
11036 N0.getValueType())) {
11037 if (OpBits < DestBits)
11038 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N0), VT, Op);
11039 else if (OpBits > DestBits)
11040 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT, Op);
11041 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, Op,
11042 DAG.getValueType(N0.getValueType()));
11043 }
11044 }
11045
11046 // Try to simplify (sext (load x)).
11047 if (SDValue foldedExt =
11048 tryToFoldExtOfLoad(DAG, *this, TLI, VT, LegalOperations, N, N0,
11049 ISD::SEXTLOAD, ISD::SIGN_EXTEND))
11050 return foldedExt;
11051
11052 if (SDValue foldedExt =
11053 tryToFoldExtOfMaskedLoad(DAG, TLI, VT, N, N0, ISD::SEXTLOAD,
11054 ISD::SIGN_EXTEND))
11055 return foldedExt;
11056
11057 // fold (sext (load x)) to multiple smaller sextloads.
11058 // Only on illegal but splittable vectors.
11059 if (SDValue ExtLoad = CombineExtLoad(N))
11060 return ExtLoad;
11061
11062 // Try to simplify (sext (sextload x)).
11063 if (SDValue foldedExt = tryToFoldExtOfExtload(
11064 DAG, *this, TLI, VT, LegalOperations, N, N0, ISD::SEXTLOAD))
11065 return foldedExt;
11066
11067 // fold (sext (and/or/xor (load x), cst)) ->
11068 // (and/or/xor (sextload x), (sext cst))
11069 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
11070 N0.getOpcode() == ISD::XOR) &&
11071 isa<LoadSDNode>(N0.getOperand(0)) &&
11072 N0.getOperand(1).getOpcode() == ISD::Constant &&
11073 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
11074 LoadSDNode *LN00 = cast<LoadSDNode>(N0.getOperand(0));
11075 EVT MemVT = LN00->getMemoryVT();
11076 if (TLI.isLoadExtLegal(ISD::SEXTLOAD, VT, MemVT) &&
11077 LN00->getExtensionType() != ISD::ZEXTLOAD && LN00->isUnindexed()) {
11078 SmallVector<SDNode*, 4> SetCCs;
11079 bool DoXform = ExtendUsesToFormExtLoad(VT, N0.getNode(), N0.getOperand(0),
11080 ISD::SIGN_EXTEND, SetCCs, TLI);
11081 if (DoXform) {
11082 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(LN00), VT,
11083 LN00->getChain(), LN00->getBasePtr(),
11084 LN00->getMemoryVT(),
11085 LN00->getMemOperand());
11086 APInt Mask = N0.getConstantOperandAPInt(1).sext(VT.getSizeInBits());
11087 SDValue And = DAG.getNode(N0.getOpcode(), DL, VT,
11088 ExtLoad, DAG.getConstant(Mask, DL, VT));
11089 ExtendSetCCUses(SetCCs, N0.getOperand(0), ExtLoad, ISD::SIGN_EXTEND);
11090 bool NoReplaceTruncAnd = !N0.hasOneUse();
11091 bool NoReplaceTrunc = SDValue(LN00, 0).hasOneUse();
11092 CombineTo(N, And);
11093 // If N0 has multiple uses, change other uses as well.
11094 if (NoReplaceTruncAnd) {
11095 SDValue TruncAnd =
11096 DAG.getNode(ISD::TRUNCATE, DL, N0.getValueType(), And);
11097 CombineTo(N0.getNode(), TruncAnd);
11098 }
11099 if (NoReplaceTrunc) {
11100 DAG.ReplaceAllUsesOfValueWith(SDValue(LN00, 1), ExtLoad.getValue(1));
11101 } else {
11102 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(LN00),
11103 LN00->getValueType(0), ExtLoad);
11104 CombineTo(LN00, Trunc, ExtLoad.getValue(1));
11105 }
11106 return SDValue(N,0); // Return N so it doesn't get rechecked!
11107 }
11108 }
11109 }
11110
11111 if (SDValue V = foldExtendedSignBitTest(N, DAG, LegalOperations))
11112 return V;
11113
11114 if (SDValue V = foldSextSetcc(N))
11115 return V;
11116
11117 // fold (sext x) -> (zext x) if the sign bit is known zero.
11118 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
11119 DAG.SignBitIsZero(N0))
11120 return DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0);
11121
11122 if (SDValue NewVSel = matchVSelectOpSizesWithSetCC(N))
11123 return NewVSel;
11124
11125 // Eliminate this sign extend by doing a negation in the destination type:
11126 // sext i32 (0 - (zext i8 X to i32)) to i64 --> 0 - (zext i8 X to i64)
11127 if (N0.getOpcode() == ISD::SUB && N0.hasOneUse() &&
11128 isNullOrNullSplat(N0.getOperand(0)) &&
11129 N0.getOperand(1).getOpcode() == ISD::ZERO_EXTEND &&
11130 TLI.isOperationLegalOrCustom(ISD::SUB, VT)) {
11131 SDValue Zext = DAG.getZExtOrTrunc(N0.getOperand(1).getOperand(0), DL, VT);
11132 return DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), Zext);
11133 }
11134 // Eliminate this sign extend by doing a decrement in the destination type:
11135 // sext i32 ((zext i8 X to i32) + (-1)) to i64 --> (zext i8 X to i64) + (-1)
11136 if (N0.getOpcode() == ISD::ADD && N0.hasOneUse() &&
11137 isAllOnesOrAllOnesSplat(N0.getOperand(1)) &&
11138 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
11139 TLI.isOperationLegalOrCustom(ISD::ADD, VT)) {
11140 SDValue Zext = DAG.getZExtOrTrunc(N0.getOperand(0).getOperand(0), DL, VT);
11141 return DAG.getNode(ISD::ADD, DL, VT, Zext, DAG.getAllOnesConstant(DL, VT));
11142 }
11143
11144 // fold sext (not i1 X) -> add (zext i1 X), -1
11145 // TODO: This could be extended to handle bool vectors.
11146 if (N0.getValueType() == MVT::i1 && isBitwiseNot(N0) && N0.hasOneUse() &&
11147 (!LegalOperations || (TLI.isOperationLegal(ISD::ZERO_EXTEND, VT) &&
11148 TLI.isOperationLegal(ISD::ADD, VT)))) {
11149 // If we can eliminate the 'not', the sext form should be better
11150 if (SDValue NewXor = visitXOR(N0.getNode())) {
11151 // Returning N0 is a form of in-visit replacement that may have
11152 // invalidated N0.
11153 if (NewXor.getNode() == N0.getNode()) {
11154 // Return SDValue here as the xor should have already been replaced in
11155 // this sext.
11156 return SDValue();
11157 } else {
11158 // Return a new sext with the new xor.
11159 return DAG.getNode(ISD::SIGN_EXTEND, DL, VT, NewXor);
11160 }
11161 }
11162
11163 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0));
11164 return DAG.getNode(ISD::ADD, DL, VT, Zext, DAG.getAllOnesConstant(DL, VT));
11165 }
11166
11167 if (SDValue Res = tryToFoldExtendSelectLoad(N, TLI, DAG))
11168 return Res;
11169
11170 return SDValue();
11171}
11172
11173// isTruncateOf - If N is a truncate of some other value, return true, record
11174// the value being truncated in Op and which of Op's bits are zero/one in Known.
11175// This function computes KnownBits to avoid a duplicated call to
11176// computeKnownBits in the caller.
11177static bool isTruncateOf(SelectionDAG &DAG, SDValue N, SDValue &Op,
11178 KnownBits &Known) {
11179 if (N->getOpcode() == ISD::TRUNCATE) {
11180 Op = N->getOperand(0);
11181 Known = DAG.computeKnownBits(Op);
11182 return true;
11183 }
11184
11185 if (N.getOpcode() != ISD::SETCC ||
11186 N.getValueType().getScalarType() != MVT::i1 ||
11187 cast<CondCodeSDNode>(N.getOperand(2))->get() != ISD::SETNE)
11188 return false;
11189
11190 SDValue Op0 = N->getOperand(0);
11191 SDValue Op1 = N->getOperand(1);
11192 assert(Op0.getValueType() == Op1.getValueType())(static_cast <bool> (Op0.getValueType() == Op1.getValueType
()) ? void (0) : __assert_fail ("Op0.getValueType() == Op1.getValueType()"
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 11192, __extension__ __PRETTY_FUNCTION__))
;
11193
11194 if (isNullOrNullSplat(Op0))
11195 Op = Op1;
11196 else if (isNullOrNullSplat(Op1))
11197 Op = Op0;
11198 else
11199 return false;
11200
11201 Known = DAG.computeKnownBits(Op);
11202
11203 return (Known.Zero | 1).isAllOnesValue();
11204}
11205
11206/// Given an extending node with a pop-count operand, if the target does not
11207/// support a pop-count in the narrow source type but does support it in the
11208/// destination type, widen the pop-count to the destination type.
11209static SDValue widenCtPop(SDNode *Extend, SelectionDAG &DAG) {
11210 assert((Extend->getOpcode() == ISD::ZERO_EXTEND ||(static_cast <bool> ((Extend->getOpcode() == ISD::ZERO_EXTEND
|| Extend->getOpcode() == ISD::ANY_EXTEND) && "Expected extend op"
) ? void (0) : __assert_fail ("(Extend->getOpcode() == ISD::ZERO_EXTEND || Extend->getOpcode() == ISD::ANY_EXTEND) && \"Expected extend op\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 11211, __extension__ __PRETTY_FUNCTION__))
11211 Extend->getOpcode() == ISD::ANY_EXTEND) && "Expected extend op")(static_cast <bool> ((Extend->getOpcode() == ISD::ZERO_EXTEND
|| Extend->getOpcode() == ISD::ANY_EXTEND) && "Expected extend op"
) ? void (0) : __assert_fail ("(Extend->getOpcode() == ISD::ZERO_EXTEND || Extend->getOpcode() == ISD::ANY_EXTEND) && \"Expected extend op\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 11211, __extension__ __PRETTY_FUNCTION__))
;
11212
11213 SDValue CtPop = Extend->getOperand(0);
11214 if (CtPop.getOpcode() != ISD::CTPOP || !CtPop.hasOneUse())
11215 return SDValue();
11216
11217 EVT VT = Extend->getValueType(0);
11218 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11219 if (TLI.isOperationLegalOrCustom(ISD::CTPOP, CtPop.getValueType()) ||
11220 !TLI.isOperationLegalOrCustom(ISD::CTPOP, VT))
11221 return SDValue();
11222
11223 // zext (ctpop X) --> ctpop (zext X)
11224 SDLoc DL(Extend);
11225 SDValue NewZext = DAG.getZExtOrTrunc(CtPop.getOperand(0), DL, VT);
11226 return DAG.getNode(ISD::CTPOP, DL, VT, NewZext);
11227}
11228
11229SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
11230 SDValue N0 = N->getOperand(0);
11231 EVT VT = N->getValueType(0);
11232
11233 if (SDValue Res = tryToFoldExtendOfConstant(N, TLI, DAG, LegalTypes))
11234 return Res;
11235
11236 // fold (zext (zext x)) -> (zext x)
11237 // fold (zext (aext x)) -> (zext x)
11238 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
11239 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT,
11240 N0.getOperand(0));
11241
11242 // fold (zext (truncate x)) -> (zext x) or
11243 // (zext (truncate x)) -> (truncate x)
11244 // This is valid when the truncated bits of x are already zero.
11245 SDValue Op;
11246 KnownBits Known;
11247 if (isTruncateOf(DAG, N0, Op, Known)) {
11248 APInt TruncatedBits =
11249 (Op.getScalarValueSizeInBits() == N0.getScalarValueSizeInBits()) ?
11250 APInt(Op.getScalarValueSizeInBits(), 0) :
11251 APInt::getBitsSet(Op.getScalarValueSizeInBits(),
11252 N0.getScalarValueSizeInBits(),
11253 std::min(Op.getScalarValueSizeInBits(),
11254 VT.getScalarSizeInBits()));
11255 if (TruncatedBits.isSubsetOf(Known.Zero))
11256 return DAG.getZExtOrTrunc(Op, SDLoc(N), VT);
11257 }
11258
11259 // fold (zext (truncate x)) -> (and x, mask)
11260 if (N0.getOpcode() == ISD::TRUNCATE) {
11261 // fold (zext (truncate (load x))) -> (zext (smaller load x))
11262 // fold (zext (truncate (srl (load x), c))) -> (zext (smaller load (x+c/n)))
11263 if (SDValue NarrowLoad = ReduceLoadWidth(N0.getNode())) {
11264 SDNode *oye = N0.getOperand(0).getNode();
11265 if (NarrowLoad.getNode() != N0.getNode()) {
11266 CombineTo(N0.getNode(), NarrowLoad);
11267 // CombineTo deleted the truncate, if needed, but not what's under it.
11268 AddToWorklist(oye);
11269 }
11270 return SDValue(N, 0); // Return N so it doesn't get rechecked!
11271 }
11272
11273 EVT SrcVT = N0.getOperand(0).getValueType();
11274 EVT MinVT = N0.getValueType();
11275
11276 // Try to mask before the extension to avoid having to generate a larger mask,
11277 // possibly over several sub-vectors.
11278 if (SrcVT.bitsLT(VT) && VT.isVector()) {
11279 if (!LegalOperations || (TLI.isOperationLegal(ISD::AND, SrcVT) &&
11280 TLI.isOperationLegal(ISD::ZERO_EXTEND, VT))) {
11281 SDValue Op = N0.getOperand(0);
11282 Op = DAG.getZeroExtendInReg(Op, SDLoc(N), MinVT);
11283 AddToWorklist(Op.getNode());
11284 SDValue ZExtOrTrunc = DAG.getZExtOrTrunc(Op, SDLoc(N), VT);
11285 // Transfer the debug info; the new node is equivalent to N0.
11286 DAG.transferDbgValues(N0, ZExtOrTrunc);
11287 return ZExtOrTrunc;
11288 }
11289 }
11290
11291 if (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT)) {
11292 SDValue Op = DAG.getAnyExtOrTrunc(N0.getOperand(0), SDLoc(N), VT);
11293 AddToWorklist(Op.getNode());
11294 SDValue And = DAG.getZeroExtendInReg(Op, SDLoc(N), MinVT);
11295 // We may safely transfer the debug info describing the truncate node over
11296 // to the equivalent and operation.
11297 DAG.transferDbgValues(N0, And);
11298 return And;
11299 }
11300 }
11301
11302 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
11303 // if either of the casts is not free.
11304 if (N0.getOpcode() == ISD::AND &&
11305 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
11306 N0.getOperand(1).getOpcode() == ISD::Constant &&
11307 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
11308 N0.getValueType()) ||
11309 !TLI.isZExtFree(N0.getValueType(), VT))) {
11310 SDValue X = N0.getOperand(0).getOperand(0);
11311 X = DAG.getAnyExtOrTrunc(X, SDLoc(X), VT);
11312 APInt Mask = N0.getConstantOperandAPInt(1).zext(VT.getSizeInBits());
11313 SDLoc DL(N);
11314 return DAG.getNode(ISD::AND, DL, VT,
11315 X, DAG.getConstant(Mask, DL, VT));
11316 }
11317
11318 // Try to simplify (zext (load x)).
11319 if (SDValue foldedExt =
11320 tryToFoldExtOfLoad(DAG, *this, TLI, VT, LegalOperations, N, N0,
11321 ISD::ZEXTLOAD, ISD::ZERO_EXTEND))
11322 return foldedExt;
11323
11324 if (SDValue foldedExt =
11325 tryToFoldExtOfMaskedLoad(DAG, TLI, VT, N, N0, ISD::ZEXTLOAD,
11326 ISD::ZERO_EXTEND))
11327 return foldedExt;
11328
11329 // fold (zext (load x)) to multiple smaller zextloads.
11330 // Only on illegal but splittable vectors.
11331 if (SDValue ExtLoad = CombineExtLoad(N))
11332 return ExtLoad;
11333
11334 // fold (zext (and/or/xor (load x), cst)) ->
11335 // (and/or/xor (zextload x), (zext cst))
11336 // Unless (and (load x) cst) will match as a zextload already and has
11337 // additional users.
11338 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
11339 N0.getOpcode() == ISD::XOR) &&
11340 isa<LoadSDNode>(N0.getOperand(0)) &&
11341 N0.getOperand(1).getOpcode() == ISD::Constant &&
11342 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
11343 LoadSDNode *LN00 = cast<LoadSDNode>(N0.getOperand(0));
11344 EVT MemVT = LN00->getMemoryVT();
11345 if (TLI.isLoadExtLegal(ISD::ZEXTLOAD, VT, MemVT) &&
11346 LN00->getExtensionType() != ISD::SEXTLOAD && LN00->isUnindexed()) {
11347 bool DoXform = true;
11348 SmallVector<SDNode*, 4> SetCCs;
11349 if (!N0.hasOneUse()) {
11350 if (N0.getOpcode() == ISD::AND) {
11351 auto *AndC = cast<ConstantSDNode>(N0.getOperand(1));
11352 EVT LoadResultTy = AndC->getValueType(0);
11353 EVT ExtVT;
11354 if (isAndLoadExtLoad(AndC, LN00, LoadResultTy, ExtVT))
11355 DoXform = false;
11356 }
11357 }
11358 if (DoXform)
11359 DoXform = ExtendUsesToFormExtLoad(VT, N0.getNode(), N0.getOperand(0),
11360 ISD::ZERO_EXTEND, SetCCs, TLI);
11361 if (DoXform) {
11362 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN00), VT,
11363 LN00->getChain(), LN00->getBasePtr(),
11364 LN00->getMemoryVT(),
11365 LN00->getMemOperand());
11366 APInt Mask = N0.getConstantOperandAPInt(1).zext(VT.getSizeInBits());
11367 SDLoc DL(N);
11368 SDValue And = DAG.getNode(N0.getOpcode(), DL, VT,
11369 ExtLoad, DAG.getConstant(Mask, DL, VT));
11370 ExtendSetCCUses(SetCCs, N0.getOperand(0), ExtLoad, ISD::ZERO_EXTEND);
11371 bool NoReplaceTruncAnd = !N0.hasOneUse();
11372 bool NoReplaceTrunc = SDValue(LN00, 0).hasOneUse();
11373 CombineTo(N, And);
11374 // If N0 has multiple uses, change other uses as well.
11375 if (NoReplaceTruncAnd) {
11376 SDValue TruncAnd =
11377 DAG.getNode(ISD::TRUNCATE, DL, N0.getValueType(), And);
11378 CombineTo(N0.getNode(), TruncAnd);
11379 }
11380 if (NoReplaceTrunc) {
11381 DAG.ReplaceAllUsesOfValueWith(SDValue(LN00, 1), ExtLoad.getValue(1));
11382 } else {
11383 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(LN00),
11384 LN00->getValueType(0), ExtLoad);
11385 CombineTo(LN00, Trunc, ExtLoad.getValue(1));
11386 }
11387 return SDValue(N,0); // Return N so it doesn't get rechecked!
11388 }
11389 }
11390 }
11391
11392 // fold (zext (and/or/xor (shl/shr (load x), cst), cst)) ->
11393 // (and/or/xor (shl/shr (zextload x), (zext cst)), (zext cst))
11394 if (SDValue ZExtLoad = CombineZExtLogicopShiftLoad(N))
11395 return ZExtLoad;
11396
11397 // Try to simplify (zext (zextload x)).
11398 if (SDValue foldedExt = tryToFoldExtOfExtload(
11399 DAG, *this, TLI, VT, LegalOperations, N, N0, ISD::ZEXTLOAD))
11400 return foldedExt;
11401
11402 if (SDValue V = foldExtendedSignBitTest(N, DAG, LegalOperations))
11403 return V;
11404
11405 if (N0.getOpcode() == ISD::SETCC) {
11406 // Only do this before legalize for now.
11407 if (!LegalOperations && VT.isVector() &&
11408 N0.getValueType().getVectorElementType() == MVT::i1) {
11409 EVT N00VT = N0.getOperand(0).getValueType();
11410 if (getSetCCResultType(N00VT) == N0.getValueType())
11411 return SDValue();
11412
11413 // We know that the # elements of the results is the same as the #
11414 // elements of the compare (and the # elements of the compare result for
11415 // that matter). Check to see that they are the same size. If so, we know
11416 // that the element size of the sext'd result matches the element size of
11417 // the compare operands.
11418 SDLoc DL(N);
11419 if (VT.getSizeInBits() == N00VT.getSizeInBits()) {
11420 // zext(setcc) -> zext_in_reg(vsetcc) for vectors.
11421 SDValue VSetCC = DAG.getNode(ISD::SETCC, DL, VT, N0.getOperand(0),
11422 N0.getOperand(1), N0.getOperand(2));
11423 return DAG.getZeroExtendInReg(VSetCC, DL, N0.getValueType());
11424 }
11425
11426 // If the desired elements are smaller or larger than the source
11427 // elements we can use a matching integer vector type and then
11428 // truncate/any extend followed by zext_in_reg.
11429 EVT MatchingVectorType = N00VT.changeVectorElementTypeToInteger();
11430 SDValue VsetCC =
11431 DAG.getNode(ISD::SETCC, DL, MatchingVectorType, N0.getOperand(0),
11432 N0.getOperand(1), N0.getOperand(2));
11433 return DAG.getZeroExtendInReg(DAG.getAnyExtOrTrunc(VsetCC, DL, VT), DL,
11434 N0.getValueType());
11435 }
11436
11437 // zext(setcc x,y,cc) -> zext(select x, y, true, false, cc)
11438 SDLoc DL(N);
11439 EVT N0VT = N0.getValueType();
11440 EVT N00VT = N0.getOperand(0).getValueType();
11441 if (SDValue SCC = SimplifySelectCC(
11442 DL, N0.getOperand(0), N0.getOperand(1),
11443 DAG.getBoolConstant(true, DL, N0VT, N00VT),
11444 DAG.getBoolConstant(false, DL, N0VT, N00VT),
11445 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true))
11446 return DAG.getNode(ISD::ZERO_EXTEND, DL, VT, SCC);
11447 }
11448
11449 // (zext (shl (zext x), cst)) -> (shl (zext x), cst)
11450 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) &&
11451 isa<ConstantSDNode>(N0.getOperand(1)) &&
11452 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
11453 N0.hasOneUse()) {
11454 SDValue ShAmt = N0.getOperand(1);
11455 if (N0.getOpcode() == ISD::SHL) {
11456 SDValue InnerZExt = N0.getOperand(0);
11457 // If the original shl may be shifting out bits, do not perform this
11458 // transformation.
11459 unsigned KnownZeroBits = InnerZExt.getValueSizeInBits() -
11460 InnerZExt.getOperand(0).getValueSizeInBits();
11461 if (cast<ConstantSDNode>(ShAmt)->getAPIntValue().ugt(KnownZeroBits))
11462 return SDValue();
11463 }
11464
11465 SDLoc DL(N);
11466
11467 // Ensure that the shift amount is wide enough for the shifted value.
11468 if (Log2_32_Ceil(VT.getSizeInBits()) > ShAmt.getValueSizeInBits())
11469 ShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, ShAmt);
11470
11471 return DAG.getNode(N0.getOpcode(), DL, VT,
11472 DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)),
11473 ShAmt);
11474 }
11475
11476 if (SDValue NewVSel = matchVSelectOpSizesWithSetCC(N))
11477 return NewVSel;
11478
11479 if (SDValue NewCtPop = widenCtPop(N, DAG))
11480 return NewCtPop;
11481
11482 if (SDValue Res = tryToFoldExtendSelectLoad(N, TLI, DAG))
11483 return Res;
11484
11485 return SDValue();
11486}
11487
11488SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
11489 SDValue N0 = N->getOperand(0);
11490 EVT VT = N->getValueType(0);
11491
11492 if (SDValue Res = tryToFoldExtendOfConstant(N, TLI, DAG, LegalTypes))
11493 return Res;
11494
11495 // fold (aext (aext x)) -> (aext x)
11496 // fold (aext (zext x)) -> (zext x)
11497 // fold (aext (sext x)) -> (sext x)
11498 if (N0.getOpcode() == ISD::ANY_EXTEND ||
11499 N0.getOpcode() == ISD::ZERO_EXTEND ||
11500 N0.getOpcode() == ISD::SIGN_EXTEND)
11501 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, N0.getOperand(0));
11502
11503 // fold (aext (truncate (load x))) -> (aext (smaller load x))
11504 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
11505 if (N0.getOpcode() == ISD::TRUNCATE) {
11506 if (SDValue NarrowLoad = ReduceLoadWidth(N0.getNode())) {
11507 SDNode *oye = N0.getOperand(0).getNode();
11508 if (NarrowLoad.getNode() != N0.getNode()) {
11509 CombineTo(N0.getNode(), NarrowLoad);
11510 // CombineTo deleted the truncate, if needed, but not what's under it.
11511 AddToWorklist(oye);
11512 }
11513 return SDValue(N, 0); // Return N so it doesn't get rechecked!
11514 }
11515 }
11516
11517 // fold (aext (truncate x))
11518 if (N0.getOpcode() == ISD::TRUNCATE)
11519 return DAG.getAnyExtOrTrunc(N0.getOperand(0), SDLoc(N), VT);
11520
11521 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
11522 // if the trunc is not free.
11523 if (N0.getOpcode() == ISD::AND &&
11524 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
11525 N0.getOperand(1).getOpcode() == ISD::Constant &&
11526 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
11527 N0.getValueType())) {
11528 SDLoc DL(N);
11529 SDValue X = N0.getOperand(0).getOperand(0);
11530 X = DAG.getAnyExtOrTrunc(X, DL, VT);
11531 APInt Mask = N0.getConstantOperandAPInt(1).zext(VT.getSizeInBits());
11532 return DAG.getNode(ISD::AND, DL, VT,
11533 X, DAG.getConstant(Mask, DL, VT));
11534 }
11535
11536 // fold (aext (load x)) -> (aext (truncate (extload x)))
11537 // None of the supported targets knows how to perform load and any_ext
11538 // on vectors in one instruction, so attempt to fold to zext instead.
11539 if (VT.isVector()) {
11540 // Try to simplify (zext (load x)).
11541 if (SDValue foldedExt =
11542 tryToFoldExtOfLoad(DAG, *this, TLI, VT, LegalOperations, N, N0,
11543 ISD::ZEXTLOAD, ISD::ZERO_EXTEND))
11544 return foldedExt;
11545 } else if (ISD::isNON_EXTLoad(N0.getNode()) &&
11546 ISD::isUNINDEXEDLoad(N0.getNode()) &&
11547 TLI.isLoadExtLegal(ISD::EXTLOAD, VT, N0.getValueType())) {
11548 bool DoXform = true;
11549 SmallVector<SDNode *, 4> SetCCs;
11550 if (!N0.hasOneUse())
11551 DoXform =
11552 ExtendUsesToFormExtLoad(VT, N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
11553 if (DoXform) {
11554 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
11555 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
11556 LN0->getChain(), LN0->getBasePtr(),
11557 N0.getValueType(), LN0->getMemOperand());
11558 ExtendSetCCUses(SetCCs, N0, ExtLoad, ISD::ANY_EXTEND);
11559 // If the load value is used only by N, replace it via CombineTo N.
11560 bool NoReplaceTrunc = N0.hasOneUse();
11561 CombineTo(N, ExtLoad);
11562 if (NoReplaceTrunc) {
11563 DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1), ExtLoad.getValue(1));
11564 recursivelyDeleteUnusedNodes(LN0);
11565 } else {
11566 SDValue Trunc =
11567 DAG.getNode(ISD::TRUNCATE, SDLoc(N0), N0.getValueType(), ExtLoad);
11568 CombineTo(LN0, Trunc, ExtLoad.getValue(1));
11569 }
11570 return SDValue(N, 0); // Return N so it doesn't get rechecked!
11571 }
11572 }
11573
11574 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
11575 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
11576 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
11577 if (N0.getOpcode() == ISD::LOAD && !ISD::isNON_EXTLoad(N0.getNode()) &&
11578 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
11579 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
11580 ISD::LoadExtType ExtType = LN0->getExtensionType();
11581 EVT MemVT = LN0->getMemoryVT();
11582 if (!LegalOperations || TLI.isLoadExtLegal(ExtType, VT, MemVT)) {
11583 SDValue ExtLoad = DAG.getExtLoad(ExtType, SDLoc(N),
11584 VT, LN0->getChain(), LN0->getBasePtr(),
11585 MemVT, LN0->getMemOperand());
11586 CombineTo(N, ExtLoad);
11587 DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1), ExtLoad.getValue(1));
11588 recursivelyDeleteUnusedNodes(LN0);
11589 return SDValue(N, 0); // Return N so it doesn't get rechecked!
11590 }
11591 }
11592
11593 if (N0.getOpcode() == ISD::SETCC) {
11594 // For vectors:
11595 // aext(setcc) -> vsetcc
11596 // aext(setcc) -> truncate(vsetcc)
11597 // aext(setcc) -> aext(vsetcc)
11598 // Only do this before legalize for now.
11599 if (VT.isVector() && !LegalOperations) {
11600 EVT N00VT = N0.getOperand(0).getValueType();
11601 if (getSetCCResultType(N00VT) == N0.getValueType())
11602 return SDValue();
11603
11604 // We know that the # elements of the results is the same as the
11605 // # elements of the compare (and the # elements of the compare result
11606 // for that matter). Check to see that they are the same size. If so,
11607 // we know that the element size of the sext'd result matches the
11608 // element size of the compare operands.
11609 if (VT.getSizeInBits() == N00VT.getSizeInBits())
11610 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
11611 N0.getOperand(1),
11612 cast<CondCodeSDNode>(N0.getOperand(2))->get());
11613
11614 // If the desired elements are smaller or larger than the source
11615 // elements we can use a matching integer vector type and then
11616 // truncate/any extend
11617 EVT MatchingVectorType = N00VT.changeVectorElementTypeToInteger();
11618 SDValue VsetCC =
11619 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0),
11620 N0.getOperand(1),
11621 cast<CondCodeSDNode>(N0.getOperand(2))->get());
11622 return DAG.getAnyExtOrTrunc(VsetCC, SDLoc(N), VT);
11623 }
11624
11625 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
11626 SDLoc DL(N);
11627 if (SDValue SCC = SimplifySelectCC(
11628 DL, N0.getOperand(0), N0.getOperand(1), DAG.getConstant(1, DL, VT),
11629 DAG.getConstant(0, DL, VT),
11630 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true))
11631 return SCC;
11632 }
11633
11634 if (SDValue NewCtPop = widenCtPop(N, DAG))
11635 return NewCtPop;
11636
11637 if (SDValue Res = tryToFoldExtendSelectLoad(N, TLI, DAG))
11638 return Res;
11639
11640 return SDValue();
11641}
11642
11643SDValue DAGCombiner::visitAssertExt(SDNode *N) {
11644 unsigned Opcode = N->getOpcode();
11645 SDValue N0 = N->getOperand(0);
11646 SDValue N1 = N->getOperand(1);
11647 EVT AssertVT = cast<VTSDNode>(N1)->getVT();
11648
11649 // fold (assert?ext (assert?ext x, vt), vt) -> (assert?ext x, vt)
11650 if (N0.getOpcode() == Opcode &&
11651 AssertVT == cast<VTSDNode>(N0.getOperand(1))->getVT())
11652 return N0;
11653
11654 if (N0.getOpcode() == ISD::TRUNCATE && N0.hasOneUse() &&
11655 N0.getOperand(0).getOpcode() == Opcode) {
11656 // We have an assert, truncate, assert sandwich. Make one stronger assert
11657 // by asserting on the smallest asserted type to the larger source type.
11658 // This eliminates the later assert:
11659 // assert (trunc (assert X, i8) to iN), i1 --> trunc (assert X, i1) to iN
11660 // assert (trunc (assert X, i1) to iN), i8 --> trunc (assert X, i1) to iN
11661 SDValue BigA = N0.getOperand(0);
11662 EVT BigA_AssertVT = cast<VTSDNode>(BigA.getOperand(1))->getVT();
11663 assert(BigA_AssertVT.bitsLE(N0.getValueType()) &&(static_cast <bool> (BigA_AssertVT.bitsLE(N0.getValueType
()) && "Asserting zero/sign-extended bits to a type larger than the "
"truncated destination does not provide information") ? void
(0) : __assert_fail ("BigA_AssertVT.bitsLE(N0.getValueType()) && \"Asserting zero/sign-extended bits to a type larger than the \" \"truncated destination does not provide information\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 11665, __extension__ __PRETTY_FUNCTION__))
11664 "Asserting zero/sign-extended bits to a type larger than the "(static_cast <bool> (BigA_AssertVT.bitsLE(N0.getValueType
()) && "Asserting zero/sign-extended bits to a type larger than the "
"truncated destination does not provide information") ? void
(0) : __assert_fail ("BigA_AssertVT.bitsLE(N0.getValueType()) && \"Asserting zero/sign-extended bits to a type larger than the \" \"truncated destination does not provide information\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 11665, __extension__ __PRETTY_FUNCTION__))
11665 "truncated destination does not provide information")(static_cast <bool> (BigA_AssertVT.bitsLE(N0.getValueType
()) && "Asserting zero/sign-extended bits to a type larger than the "
"truncated destination does not provide information") ? void
(0) : __assert_fail ("BigA_AssertVT.bitsLE(N0.getValueType()) && \"Asserting zero/sign-extended bits to a type larger than the \" \"truncated destination does not provide information\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 11665, __extension__ __PRETTY_FUNCTION__))
;
11666
11667 SDLoc DL(N);
11668 EVT MinAssertVT = AssertVT.bitsLT(BigA_AssertVT) ? AssertVT : BigA_AssertVT;
11669 SDValue MinAssertVTVal = DAG.getValueType(MinAssertVT);
11670 SDValue NewAssert = DAG.getNode(Opcode, DL, BigA.getValueType(),
11671 BigA.getOperand(0), MinAssertVTVal);
11672 return DAG.getNode(ISD::TRUNCATE, DL, N->getValueType(0), NewAssert);
11673 }
11674
11675 // If we have (AssertZext (truncate (AssertSext X, iX)), iY) and Y is smaller
11676 // than X. Just move the AssertZext in front of the truncate and drop the
11677 // AssertSExt.
11678 if (N0.getOpcode() == ISD::TRUNCATE && N0.hasOneUse() &&
11679 N0.getOperand(0).getOpcode() == ISD::AssertSext &&
11680 Opcode == ISD::AssertZext) {
11681 SDValue BigA = N0.getOperand(0);
11682 EVT BigA_AssertVT = cast<VTSDNode>(BigA.getOperand(1))->getVT();
11683 assert(BigA_AssertVT.bitsLE(N0.getValueType()) &&(static_cast <bool> (BigA_AssertVT.bitsLE(N0.getValueType
()) && "Asserting zero/sign-extended bits to a type larger than the "
"truncated destination does not provide information") ? void
(0) : __assert_fail ("BigA_AssertVT.bitsLE(N0.getValueType()) && \"Asserting zero/sign-extended bits to a type larger than the \" \"truncated destination does not provide information\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 11685, __extension__ __PRETTY_FUNCTION__))
11684 "Asserting zero/sign-extended bits to a type larger than the "(static_cast <bool> (BigA_AssertVT.bitsLE(N0.getValueType
()) && "Asserting zero/sign-extended bits to a type larger than the "
"truncated destination does not provide information") ? void
(0) : __assert_fail ("BigA_AssertVT.bitsLE(N0.getValueType()) && \"Asserting zero/sign-extended bits to a type larger than the \" \"truncated destination does not provide information\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 11685, __extension__ __PRETTY_FUNCTION__))
11685 "truncated destination does not provide information")(static_cast <bool> (BigA_AssertVT.bitsLE(N0.getValueType
()) && "Asserting zero/sign-extended bits to a type larger than the "
"truncated destination does not provide information") ? void
(0) : __assert_fail ("BigA_AssertVT.bitsLE(N0.getValueType()) && \"Asserting zero/sign-extended bits to a type larger than the \" \"truncated destination does not provide information\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 11685, __extension__ __PRETTY_FUNCTION__))
;
11686
11687 if (AssertVT.bitsLT(BigA_AssertVT)) {
11688 SDLoc DL(N);
11689 SDValue NewAssert = DAG.getNode(Opcode, DL, BigA.getValueType(),
11690 BigA.getOperand(0), N1);
11691 return DAG.getNode(ISD::TRUNCATE, DL, N->getValueType(0), NewAssert);
11692 }
11693 }
11694
11695 return SDValue();
11696}
11697
11698SDValue DAGCombiner::visitAssertAlign(SDNode *N) {
11699 SDLoc DL(N);
11700
11701 Align AL = cast<AssertAlignSDNode>(N)->getAlign();
11702 SDValue N0 = N->getOperand(0);
11703
11704 // Fold (assertalign (assertalign x, AL0), AL1) ->
11705 // (assertalign x, max(AL0, AL1))
11706 if (auto *AAN = dyn_cast<AssertAlignSDNode>(N0))
11707 return DAG.getAssertAlign(DL, N0.getOperand(0),
11708 std::max(AL, AAN->getAlign()));
11709
11710 // In rare cases, there are trivial arithmetic ops in source operands. Sink
11711 // this assert down to source operands so that those arithmetic ops could be
11712 // exposed to the DAG combining.
11713 switch (N0.getOpcode()) {
11714 default:
11715 break;
11716 case ISD::ADD:
11717 case ISD::SUB: {
11718 unsigned AlignShift = Log2(AL);
11719 SDValue LHS = N0.getOperand(0);
11720 SDValue RHS = N0.getOperand(1);
11721 unsigned LHSAlignShift = DAG.computeKnownBits(LHS).countMinTrailingZeros();
11722 unsigned RHSAlignShift = DAG.computeKnownBits(RHS).countMinTrailingZeros();
11723 if (LHSAlignShift >= AlignShift || RHSAlignShift >= AlignShift) {
11724 if (LHSAlignShift < AlignShift)
11725 LHS = DAG.getAssertAlign(DL, LHS, AL);
11726 if (RHSAlignShift < AlignShift)
11727 RHS = DAG.getAssertAlign(DL, RHS, AL);
11728 return DAG.getNode(N0.getOpcode(), DL, N0.getValueType(), LHS, RHS);
11729 }
11730 break;
11731 }
11732 }
11733
11734 return SDValue();
11735}
11736
11737/// If the result of a wider load is shifted to right of N bits and then
11738/// truncated to a narrower type and where N is a multiple of number of bits of
11739/// the narrower type, transform it to a narrower load from address + N / num of
11740/// bits of new type. Also narrow the load if the result is masked with an AND
11741/// to effectively produce a smaller type. If the result is to be extended, also
11742/// fold the extension to form a extending load.
11743SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
11744 unsigned Opc = N->getOpcode();
11745
11746 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
11747 SDValue N0 = N->getOperand(0);
11748 EVT VT = N->getValueType(0);
11749 EVT ExtVT = VT;
11750
11751 // This transformation isn't valid for vector loads.
11752 if (VT.isVector())
11753 return SDValue();
11754
11755 unsigned ShAmt = 0;
11756 bool HasShiftedOffset = false;
11757 // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then
11758 // extended to VT.
11759 if (Opc == ISD::SIGN_EXTEND_INREG) {
11760 ExtType = ISD::SEXTLOAD;
11761 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
11762 } else if (Opc == ISD::SRL) {
11763 // Another special-case: SRL is basically zero-extending a narrower value,
11764 // or it maybe shifting a higher subword, half or byte into the lowest
11765 // bits.
11766 ExtType = ISD::ZEXTLOAD;
11767 N0 = SDValue(N, 0);
11768
11769 auto *LN0 = dyn_cast<LoadSDNode>(N0.getOperand(0));
11770 auto *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1));
11771 if (!N01 || !LN0)
11772 return SDValue();
11773
11774 uint64_t ShiftAmt = N01->getZExtValue();
11775 uint64_t MemoryWidth = LN0->getMemoryVT().getScalarSizeInBits();
11776 if (LN0->getExtensionType() != ISD::SEXTLOAD && MemoryWidth > ShiftAmt)
11777 ExtVT = EVT::getIntegerVT(*DAG.getContext(), MemoryWidth - ShiftAmt);
11778 else
11779 ExtVT = EVT::getIntegerVT(*DAG.getContext(),
11780 VT.getScalarSizeInBits() - ShiftAmt);
11781 } else if (Opc == ISD::AND) {
11782 // An AND with a constant mask is the same as a truncate + zero-extend.
11783 auto AndC = dyn_cast<ConstantSDNode>(N->getOperand(1));
11784 if (!AndC)
11785 return SDValue();
11786
11787 const APInt &Mask = AndC->getAPIntValue();
11788 unsigned ActiveBits = 0;
11789 if (Mask.isMask()) {
11790 ActiveBits = Mask.countTrailingOnes();
11791 } else if (Mask.isShiftedMask()) {
11792 ShAmt = Mask.countTrailingZeros();
11793 APInt ShiftedMask = Mask.lshr(ShAmt);
11794 ActiveBits = ShiftedMask.countTrailingOnes();
11795 HasShiftedOffset = true;
11796 } else
11797 return SDValue();
11798
11799 ExtType = ISD::ZEXTLOAD;
11800 ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
11801 }
11802
11803 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
11804 SDValue SRL = N0;
11805 if (auto *ConstShift = dyn_cast<ConstantSDNode>(SRL.getOperand(1))) {
11806 ShAmt = ConstShift->getZExtValue();
11807 unsigned EVTBits = ExtVT.getScalarSizeInBits();
11808 // Is the shift amount a multiple of size of VT?
11809 if ((ShAmt & (EVTBits-1)) == 0) {
11810 N0 = N0.getOperand(0);
11811 // Is the load width a multiple of size of VT?
11812 if ((N0.getScalarValueSizeInBits() & (EVTBits - 1)) != 0)
11813 return SDValue();
11814 }
11815
11816 // At this point, we must have a load or else we can't do the transform.
11817 auto *LN0 = dyn_cast<LoadSDNode>(N0);
11818 if (!LN0) return SDValue();
11819
11820 // Because a SRL must be assumed to *need* to zero-extend the high bits
11821 // (as opposed to anyext the high bits), we can't combine the zextload
11822 // lowering of SRL and an sextload.
11823 if (LN0->getExtensionType() == ISD::SEXTLOAD)
11824 return SDValue();
11825
11826 // If the shift amount is larger than the input type then we're not
11827 // accessing any of the loaded bytes. If the load was a zextload/extload
11828 // then the result of the shift+trunc is zero/undef (handled elsewhere).
11829 if (ShAmt >= LN0->getMemoryVT().getSizeInBits())
11830 return SDValue();
11831
11832 // If the SRL is only used by a masking AND, we may be able to adjust
11833 // the ExtVT to make the AND redundant.
11834 SDNode *Mask = *(SRL->use_begin());
11835 if (Mask->getOpcode() == ISD::AND &&
11836 isa<ConstantSDNode>(Mask->getOperand(1))) {
11837 const APInt& ShiftMask = Mask->getConstantOperandAPInt(1);
11838 if (ShiftMask.isMask()) {
11839 EVT MaskedVT = EVT::getIntegerVT(*DAG.getContext(),
11840 ShiftMask.countTrailingOnes());
11841 // If the mask is smaller, recompute the type.
11842 if ((ExtVT.getScalarSizeInBits() > MaskedVT.getScalarSizeInBits()) &&
11843 TLI.isLoadExtLegal(ExtType, N0.getValueType(), MaskedVT))
11844 ExtVT = MaskedVT;
11845 }
11846 }
11847 }
11848 }
11849
11850 // If the load is shifted left (and the result isn't shifted back right),
11851 // we can fold the truncate through the shift.
11852 unsigned ShLeftAmt = 0;
11853 if (ShAmt == 0 && N0.getOpcode() == ISD::SHL && N0.hasOneUse() &&
11854 ExtVT == VT && TLI.isNarrowingProfitable(N0.getValueType(), VT)) {
11855 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
11856 ShLeftAmt = N01->getZExtValue();
11857 N0 = N0.getOperand(0);
11858 }
11859 }
11860
11861 // If we haven't found a load, we can't narrow it.
11862 if (!isa<LoadSDNode>(N0))
11863 return SDValue();
11864
11865 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
11866 // Reducing the width of a volatile load is illegal. For atomics, we may be
11867 // able to reduce the width provided we never widen again. (see D66309)
11868 if (!LN0->isSimple() ||
11869 !isLegalNarrowLdSt(LN0, ExtType, ExtVT, ShAmt))
11870 return SDValue();
11871
11872 auto AdjustBigEndianShift = [&](unsigned ShAmt) {
11873 unsigned LVTStoreBits =
11874 LN0->getMemoryVT().getStoreSizeInBits().getFixedSize();
11875 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits().getFixedSize();
11876 return LVTStoreBits - EVTStoreBits - ShAmt;
11877 };
11878
11879 // For big endian targets, we need to adjust the offset to the pointer to
11880 // load the correct bytes.
11881 if (DAG.getDataLayout().isBigEndian())
11882 ShAmt = AdjustBigEndianShift(ShAmt);
11883
11884 uint64_t PtrOff = ShAmt / 8;
11885 Align NewAlign = commonAlignment(LN0->getAlign(), PtrOff);
11886 SDLoc DL(LN0);
11887 // The original load itself didn't wrap, so an offset within it doesn't.
11888 SDNodeFlags Flags;
11889 Flags.setNoUnsignedWrap(true);
11890 SDValue NewPtr = DAG.getMemBasePlusOffset(LN0->getBasePtr(),
11891 TypeSize::Fixed(PtrOff), DL, Flags);
11892 AddToWorklist(NewPtr.getNode());
11893
11894 SDValue Load;
11895 if (ExtType == ISD::NON_EXTLOAD)
11896 Load = DAG.getLoad(VT, DL, LN0->getChain(), NewPtr,
11897 LN0->getPointerInfo().getWithOffset(PtrOff), NewAlign,
11898 LN0->getMemOperand()->getFlags(), LN0->getAAInfo());
11899 else
11900 Load = DAG.getExtLoad(ExtType, DL, VT, LN0->getChain(), NewPtr,
11901 LN0->getPointerInfo().getWithOffset(PtrOff), ExtVT,
11902 NewAlign, LN0->getMemOperand()->getFlags(),
11903 LN0->getAAInfo());
11904
11905 // Replace the old load's chain with the new load's chain.
11906 WorklistRemover DeadNodes(*this);
11907 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1));
11908
11909 // Shift the result left, if we've swallowed a left shift.
11910 SDValue Result = Load;
11911 if (ShLeftAmt != 0) {
11912 EVT ShImmTy = getShiftAmountTy(Result.getValueType());
11913 if (!isUIntN(ShImmTy.getScalarSizeInBits(), ShLeftAmt))
11914 ShImmTy = VT;
11915 // If the shift amount is as large as the result size (but, presumably,
11916 // no larger than the source) then the useful bits of the result are
11917 // zero; we can't simply return the shortened shift, because the result
11918 // of that operation is undefined.
11919 if (ShLeftAmt >= VT.getScalarSizeInBits())
11920 Result = DAG.getConstant(0, DL, VT);
11921 else
11922 Result = DAG.getNode(ISD::SHL, DL, VT,
11923 Result, DAG.getConstant(ShLeftAmt, DL, ShImmTy));
11924 }
11925
11926 if (HasShiftedOffset) {
11927 // Recalculate the shift amount after it has been altered to calculate
11928 // the offset.
11929 if (DAG.getDataLayout().isBigEndian())
11930 ShAmt = AdjustBigEndianShift(ShAmt);
11931
11932 // We're using a shifted mask, so the load now has an offset. This means
11933 // that data has been loaded into the lower bytes than it would have been
11934 // before, so we need to shl the loaded data into the correct position in the
11935 // register.
11936 SDValue ShiftC = DAG.getConstant(ShAmt, DL, VT);
11937 Result = DAG.getNode(ISD::SHL, DL, VT, Result, ShiftC);
11938 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
11939 }
11940
11941 // Return the new loaded value.
11942 return Result;
11943}
11944
11945SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
11946 SDValue N0 = N->getOperand(0);
11947 SDValue N1 = N->getOperand(1);
11948 EVT VT = N->getValueType(0);
11949 EVT ExtVT = cast<VTSDNode>(N1)->getVT();
11950 unsigned VTBits = VT.getScalarSizeInBits();
11951 unsigned ExtVTBits = ExtVT.getScalarSizeInBits();
11952
11953 // sext_vector_inreg(undef) = 0 because the top bit will all be the same.
11954 if (N0.isUndef())
11955 return DAG.getConstant(0, SDLoc(N), VT);
11956
11957 // fold (sext_in_reg c1) -> c1
11958 if (DAG.isConstantIntBuildVectorOrConstantInt(N0))
11959 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, N0, N1);
11960
11961 // If the input is already sign extended, just drop the extension.
11962 if (DAG.ComputeNumSignBits(N0) >= (VTBits - ExtVTBits + 1))
11963 return N0;
11964
11965 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
11966 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
11967 ExtVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT()))
11968 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, N0.getOperand(0),
11969 N1);
11970
11971 // fold (sext_in_reg (sext x)) -> (sext x)
11972 // fold (sext_in_reg (aext x)) -> (sext x)
11973 // if x is small enough or if we know that x has more than 1 sign bit and the
11974 // sign_extend_inreg is extending from one of them.
11975 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
11976 SDValue N00 = N0.getOperand(0);
11977 unsigned N00Bits = N00.getScalarValueSizeInBits();
11978 if ((N00Bits <= ExtVTBits ||
11979 (N00Bits - DAG.ComputeNumSignBits(N00)) < ExtVTBits) &&
11980 (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT)))
11981 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N00);
11982 }
11983
11984 // fold (sext_in_reg (*_extend_vector_inreg x)) -> (sext_vector_inreg x)
11985 // if x is small enough or if we know that x has more than 1 sign bit and the
11986 // sign_extend_inreg is extending from one of them.
11987 if (N0.getOpcode() == ISD::ANY_EXTEND_VECTOR_INREG ||
11988 N0.getOpcode() == ISD::SIGN_EXTEND_VECTOR_INREG ||
11989 N0.getOpcode() == ISD::ZERO_EXTEND_VECTOR_INREG) {
11990 SDValue N00 = N0.getOperand(0);
11991 unsigned N00Bits = N00.getScalarValueSizeInBits();
11992 unsigned DstElts = N0.getValueType().getVectorMinNumElements();
11993 unsigned SrcElts = N00.getValueType().getVectorMinNumElements();
11994 bool IsZext = N0.getOpcode() == ISD::ZERO_EXTEND_VECTOR_INREG;
11995 APInt DemandedSrcElts = APInt::getLowBitsSet(SrcElts, DstElts);
11996 if ((N00Bits == ExtVTBits ||
11997 (!IsZext && (N00Bits < ExtVTBits ||
11998 (N00Bits - DAG.ComputeNumSignBits(N00, DemandedSrcElts)) <
11999 ExtVTBits))) &&
12000 (!LegalOperations ||
12001 TLI.isOperationLegal(ISD::SIGN_EXTEND_VECTOR_INREG, VT)))
12002 return DAG.getNode(ISD::SIGN_EXTEND_VECTOR_INREG, SDLoc(N), VT, N00);
12003 }
12004
12005 // fold (sext_in_reg (zext x)) -> (sext x)
12006 // iff we are extending the source sign bit.
12007 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
12008 SDValue N00 = N0.getOperand(0);
12009 if (N00.getScalarValueSizeInBits() == ExtVTBits &&
12010 (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT)))
12011 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N00, N1);
12012 }
12013
12014 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
12015 if (DAG.MaskedValueIsZero(N0, APInt::getOneBitSet(VTBits, ExtVTBits - 1)))
12016 return DAG.getZeroExtendInReg(N0, SDLoc(N), ExtVT);
12017
12018 // fold operands of sext_in_reg based on knowledge that the top bits are not
12019 // demanded.
12020 if (SimplifyDemandedBits(SDValue(N, 0)))
12021 return SDValue(N, 0);
12022
12023 // fold (sext_in_reg (load x)) -> (smaller sextload x)
12024 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
12025 if (SDValue NarrowLoad = ReduceLoadWidth(N))
12026 return NarrowLoad;
12027
12028 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
12029 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
12030 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
12031 if (N0.getOpcode() == ISD::SRL) {
12032 if (auto *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
12033 if (ShAmt->getAPIntValue().ule(VTBits - ExtVTBits)) {
12034 // We can turn this into an SRA iff the input to the SRL is already sign
12035 // extended enough.
12036 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
12037 if (((VTBits - ExtVTBits) - ShAmt->getZExtValue()) < InSignBits)
12038 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0.getOperand(0),
12039 N0.getOperand(1));
12040 }
12041 }
12042
12043 // fold (sext_inreg (extload x)) -> (sextload x)
12044 // If sextload is not supported by target, we can only do the combine when
12045 // load has one use. Doing otherwise can block folding the extload with other
12046 // extends that the target does support.
12047 if (ISD::isEXTLoad(N0.getNode()) &&
12048 ISD::isUNINDEXEDLoad(N0.getNode()) &&
12049 ExtVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
12050 ((!LegalOperations && cast<LoadSDNode>(N0)->isSimple() &&
12051 N0.hasOneUse()) ||
12052 TLI.isLoadExtLegal(ISD::SEXTLOAD, VT, ExtVT))) {
12053 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
12054 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
12055 LN0->getChain(),
12056 LN0->getBasePtr(), ExtVT,
12057 LN0->getMemOperand());
12058 CombineTo(N, ExtLoad);
12059 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
12060 AddToWorklist(ExtLoad.getNode());
12061 return SDValue(N, 0); // Return N so it doesn't get rechecked!
12062 }
12063
12064 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
12065 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
12066 N0.hasOneUse() &&
12067 ExtVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
12068 ((!LegalOperations && cast<LoadSDNode>(N0)->isSimple()) &&
12069 TLI.isLoadExtLegal(ISD::SEXTLOAD, VT, ExtVT))) {
12070 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
12071 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
12072 LN0->getChain(),
12073 LN0->getBasePtr(), ExtVT,
12074 LN0->getMemOperand());
12075 CombineTo(N, ExtLoad);
12076 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
12077 return SDValue(N, 0); // Return N so it doesn't get rechecked!
12078 }
12079
12080 // fold (sext_inreg (masked_load x)) -> (sext_masked_load x)
12081 // ignore it if the masked load is already sign extended
12082 if (MaskedLoadSDNode *Ld = dyn_cast<MaskedLoadSDNode>(N0)) {
12083 if (ExtVT == Ld->getMemoryVT() && N0.hasOneUse() &&
12084 Ld->getExtensionType() != ISD::LoadExtType::NON_EXTLOAD &&
12085 TLI.isLoadExtLegal(ISD::SEXTLOAD, VT, ExtVT)) {
12086 SDValue ExtMaskedLoad = DAG.getMaskedLoad(
12087 VT, SDLoc(N), Ld->getChain(), Ld->getBasePtr(), Ld->getOffset(),
12088 Ld->getMask(), Ld->getPassThru(), ExtVT, Ld->getMemOperand(),
12089 Ld->getAddressingMode(), ISD::SEXTLOAD, Ld->isExpandingLoad());
12090 CombineTo(N, ExtMaskedLoad);
12091 CombineTo(N0.getNode(), ExtMaskedLoad, ExtMaskedLoad.getValue(1));
12092 return SDValue(N, 0); // Return N so it doesn't get rechecked!
12093 }
12094 }
12095
12096 // fold (sext_inreg (masked_gather x)) -> (sext_masked_gather x)
12097 if (auto *GN0 = dyn_cast<MaskedGatherSDNode>(N0)) {
12098 if (SDValue(GN0, 0).hasOneUse() &&
12099 ExtVT == GN0->getMemoryVT() &&
12100 TLI.isVectorLoadExtDesirable(SDValue(SDValue(GN0, 0)))) {
12101 SDValue Ops[] = {GN0->getChain(), GN0->getPassThru(), GN0->getMask(),
12102 GN0->getBasePtr(), GN0->getIndex(), GN0->getScale()};
12103
12104 SDValue ExtLoad = DAG.getMaskedGather(
12105 DAG.getVTList(VT, MVT::Other), ExtVT, SDLoc(N), Ops,
12106 GN0->getMemOperand(), GN0->getIndexType(), ISD::SEXTLOAD);
12107
12108 CombineTo(N, ExtLoad);
12109 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
12110 AddToWorklist(ExtLoad.getNode());
12111 return SDValue(N, 0); // Return N so it doesn't get rechecked!
12112 }
12113 }
12114
12115 // Form (sext_inreg (bswap >> 16)) or (sext_inreg (rotl (bswap) 16))
12116 if (ExtVTBits <= 16 && N0.getOpcode() == ISD::OR) {
12117 if (SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
12118 N0.getOperand(1), false))
12119 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, BSwap, N1);
12120 }
12121
12122 return SDValue();
12123}
12124
12125SDValue DAGCombiner::visitEXTEND_VECTOR_INREG(SDNode *N) {
12126 SDValue N0 = N->getOperand(0);
12127 EVT VT = N->getValueType(0);
12128
12129 // {s/z}ext_vector_inreg(undef) = 0 because the top bits must be the same.
12130 if (N0.isUndef())
12131 return DAG.getConstant(0, SDLoc(N), VT);
12132
12133 if (SDValue Res = tryToFoldExtendOfConstant(N, TLI, DAG, LegalTypes))
12134 return Res;
12135
12136 if (SimplifyDemandedVectorElts(SDValue(N, 0)))
12137 return SDValue(N, 0);
12138
12139 return SDValue();
12140}
12141
12142SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
12143 SDValue N0 = N->getOperand(0);
12144 EVT VT = N->getValueType(0);
12145 EVT SrcVT = N0.getValueType();
12146 bool isLE = DAG.getDataLayout().isLittleEndian();
12147
12148 // noop truncate
12149 if (SrcVT == VT)
12150 return N0;
12151
12152 // fold (truncate (truncate x)) -> (truncate x)
12153 if (N0.getOpcode() == ISD::TRUNCATE)
12154 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
12155
12156 // fold (truncate c1) -> c1
12157 if (DAG.isConstantIntBuildVectorOrConstantInt(N0)) {
12158 SDValue C = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0);
12159 if (C.getNode() != N)
12160 return C;
12161 }
12162
12163 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
12164 if (N0.getOpcode() == ISD::ZERO_EXTEND ||
12165 N0.getOpcode() == ISD::SIGN_EXTEND ||
12166 N0.getOpcode() == ISD::ANY_EXTEND) {
12167 // if the source is smaller than the dest, we still need an extend.
12168 if (N0.getOperand(0).getValueType().bitsLT(VT))
12169 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, N0.getOperand(0));
12170 // if the source is larger than the dest, than we just need the truncate.
12171 if (N0.getOperand(0).getValueType().bitsGT(VT))
12172 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
12173 // if the source and dest are the same type, we can drop both the extend
12174 // and the truncate.
12175 return N0.getOperand(0);
12176 }
12177
12178 // If this is anyext(trunc), don't fold it, allow ourselves to be folded.
12179 if (N->hasOneUse() && (N->use_begin()->getOpcode() == ISD::ANY_EXTEND))
12180 return SDValue();
12181
12182 // Fold extract-and-trunc into a narrow extract. For example:
12183 // i64 x = EXTRACT_VECTOR_ELT(v2i64 val, i32 1)
12184 // i32 y = TRUNCATE(i64 x)
12185 // -- becomes --
12186 // v16i8 b = BITCAST (v2i64 val)
12187 // i8 x = EXTRACT_VECTOR_ELT(v16i8 b, i32 8)
12188 //
12189 // Note: We only run this optimization after type legalization (which often
12190 // creates this pattern) and before operation legalization after which
12191 // we need to be more careful about the vector instructions that we generate.
12192 if (N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
12193 LegalTypes && !LegalOperations && N0->hasOneUse() && VT != MVT::i1) {
12194 EVT VecTy = N0.getOperand(0).getValueType();
12195 EVT ExTy = N0.getValueType();
12196 EVT TrTy = N->getValueType(0);
12197
12198 auto EltCnt = VecTy.getVectorElementCount();
12199 unsigned SizeRatio = ExTy.getSizeInBits()/TrTy.getSizeInBits();
12200 auto NewEltCnt = EltCnt * SizeRatio;
12201
12202 EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, NewEltCnt);
12203 assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size")(static_cast <bool> (NVT.getSizeInBits() == VecTy.getSizeInBits
() && "Invalid Size") ? void (0) : __assert_fail ("NVT.getSizeInBits() == VecTy.getSizeInBits() && \"Invalid Size\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 12203, __extension__ __PRETTY_FUNCTION__))
;
12204
12205 SDValue EltNo = N0->getOperand(1);
12206 if (isa<ConstantSDNode>(EltNo) && isTypeLegal(NVT)) {
12207 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
12208 int Index = isLE ? (Elt*SizeRatio) : (Elt*SizeRatio + (SizeRatio-1));
12209
12210 SDLoc DL(N);
12211 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, TrTy,
12212 DAG.getBitcast(NVT, N0.getOperand(0)),
12213 DAG.getVectorIdxConstant(Index, DL));
12214 }
12215 }
12216
12217 // trunc (select c, a, b) -> select c, (trunc a), (trunc b)
12218 if (N0.getOpcode() == ISD::SELECT && N0.hasOneUse()) {
12219 if ((!LegalOperations || TLI.isOperationLegal(ISD::SELECT, SrcVT)) &&
12220 TLI.isTruncateFree(SrcVT, VT)) {
12221 SDLoc SL(N0);
12222 SDValue Cond = N0.getOperand(0);
12223 SDValue TruncOp0 = DAG.getNode(ISD::TRUNCATE, SL, VT, N0.getOperand(1));
12224 SDValue TruncOp1 = DAG.getNode(ISD::TRUNCATE, SL, VT, N0.getOperand(2));
12225 return DAG.getNode(ISD::SELECT, SDLoc(N), VT, Cond, TruncOp0, TruncOp1);
12226 }
12227 }
12228
12229 // trunc (shl x, K) -> shl (trunc x), K => K < VT.getScalarSizeInBits()
12230 if (N0.getOpcode() == ISD::SHL && N0.hasOneUse() &&
12231 (!LegalOperations || TLI.isOperationLegal(ISD::SHL, VT)) &&
12232 TLI.isTypeDesirableForOp(ISD::SHL, VT)) {
12233 SDValue Amt = N0.getOperand(1);
12234 KnownBits Known = DAG.computeKnownBits(Amt);
12235 unsigned Size = VT.getScalarSizeInBits();
12236 if (Known.getBitWidth() - Known.countMinLeadingZeros() <= Log2_32(Size)) {
12237 SDLoc SL(N);
12238 EVT AmtVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
12239
12240 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SL, VT, N0.getOperand(0));
12241 if (AmtVT != Amt.getValueType()) {
12242 Amt = DAG.getZExtOrTrunc(Amt, SL, AmtVT);
12243 AddToWorklist(Amt.getNode());
12244 }
12245 return DAG.getNode(ISD::SHL, SL, VT, Trunc, Amt);
12246 }
12247 }
12248
12249 if (SDValue V = foldSubToUSubSat(VT, N0.getNode()))
12250 return V;
12251
12252 // Attempt to pre-truncate BUILD_VECTOR sources.
12253 if (N0.getOpcode() == ISD::BUILD_VECTOR && !LegalOperations &&
12254 TLI.isTruncateFree(SrcVT.getScalarType(), VT.getScalarType()) &&
12255 // Avoid creating illegal types if running after type legalizer.
12256 (!LegalTypes || TLI.isTypeLegal(VT.getScalarType()))) {
12257 SDLoc DL(N);
12258 EVT SVT = VT.getScalarType();
12259 SmallVector<SDValue, 8> TruncOps;
12260 for (const SDValue &Op : N0->op_values()) {
12261 SDValue TruncOp = DAG.getNode(ISD::TRUNCATE, DL, SVT, Op);
12262 TruncOps.push_back(TruncOp);
12263 }
12264 return DAG.getBuildVector(VT, DL, TruncOps);
12265 }
12266
12267 // Fold a series of buildvector, bitcast, and truncate if possible.
12268 // For example fold
12269 // (2xi32 trunc (bitcast ((4xi32)buildvector x, x, y, y) 2xi64)) to
12270 // (2xi32 (buildvector x, y)).
12271 if (Level == AfterLegalizeVectorOps && VT.isVector() &&
12272 N0.getOpcode() == ISD::BITCAST && N0.hasOneUse() &&
12273 N0.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
12274 N0.getOperand(0).hasOneUse()) {
12275 SDValue BuildVect = N0.getOperand(0);
12276 EVT BuildVectEltTy = BuildVect.getValueType().getVectorElementType();
12277 EVT TruncVecEltTy = VT.getVectorElementType();
12278
12279 // Check that the element types match.
12280 if (BuildVectEltTy == TruncVecEltTy) {
12281 // Now we only need to compute the offset of the truncated elements.
12282 unsigned BuildVecNumElts = BuildVect.getNumOperands();
12283 unsigned TruncVecNumElts = VT.getVectorNumElements();
12284 unsigned TruncEltOffset = BuildVecNumElts / TruncVecNumElts;
12285
12286 assert((BuildVecNumElts % TruncVecNumElts) == 0 &&(static_cast <bool> ((BuildVecNumElts % TruncVecNumElts
) == 0 && "Invalid number of elements") ? void (0) : __assert_fail
("(BuildVecNumElts % TruncVecNumElts) == 0 && \"Invalid number of elements\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 12287, __extension__ __PRETTY_FUNCTION__))
12287 "Invalid number of elements")(static_cast <bool> ((BuildVecNumElts % TruncVecNumElts
) == 0 && "Invalid number of elements") ? void (0) : __assert_fail
("(BuildVecNumElts % TruncVecNumElts) == 0 && \"Invalid number of elements\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 12287, __extension__ __PRETTY_FUNCTION__))
;
12288
12289 SmallVector<SDValue, 8> Opnds;
12290 for (unsigned i = 0, e = BuildVecNumElts; i != e; i += TruncEltOffset)
12291 Opnds.push_back(BuildVect.getOperand(i));
12292
12293 return DAG.getBuildVector(VT, SDLoc(N), Opnds);
12294 }
12295 }
12296
12297 // See if we can simplify the input to this truncate through knowledge that
12298 // only the low bits are being used.
12299 // For example "trunc (or (shl x, 8), y)" // -> trunc y
12300 // Currently we only perform this optimization on scalars because vectors
12301 // may have different active low bits.
12302 if (!VT.isVector()) {
12303 APInt Mask =
12304 APInt::getLowBitsSet(N0.getValueSizeInBits(), VT.getSizeInBits());
12305 if (SDValue Shorter = DAG.GetDemandedBits(N0, Mask))
12306 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Shorter);
12307 }
12308
12309 // fold (truncate (load x)) -> (smaller load x)
12310 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
12311 if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT)) {
12312 if (SDValue Reduced = ReduceLoadWidth(N))
12313 return Reduced;
12314
12315 // Handle the case where the load remains an extending load even
12316 // after truncation.
12317 if (N0.hasOneUse() && ISD::isUNINDEXEDLoad(N0.getNode())) {
12318 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
12319 if (LN0->isSimple() && LN0->getMemoryVT().bitsLT(VT)) {
12320 SDValue NewLoad = DAG.getExtLoad(LN0->getExtensionType(), SDLoc(LN0),
12321 VT, LN0->getChain(), LN0->getBasePtr(),
12322 LN0->getMemoryVT(),
12323 LN0->getMemOperand());
12324 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLoad.getValue(1));
12325 return NewLoad;
12326 }
12327 }
12328 }
12329
12330 // fold (trunc (concat ... x ...)) -> (concat ..., (trunc x), ...)),
12331 // where ... are all 'undef'.
12332 if (N0.getOpcode() == ISD::CONCAT_VECTORS && !LegalTypes) {
12333 SmallVector<EVT, 8> VTs;
12334 SDValue V;
12335 unsigned Idx = 0;
12336 unsigned NumDefs = 0;
12337
12338 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
12339 SDValue X = N0.getOperand(i);
12340 if (!X.isUndef()) {
12341 V = X;
12342 Idx = i;
12343 NumDefs++;
12344 }
12345 // Stop if more than one members are non-undef.
12346 if (NumDefs > 1)
12347 break;
12348
12349 VTs.push_back(EVT::getVectorVT(*DAG.getContext(),
12350 VT.getVectorElementType(),
12351 X.getValueType().getVectorElementCount()));
12352 }
12353
12354 if (NumDefs == 0)
12355 return DAG.getUNDEF(VT);
12356
12357 if (NumDefs == 1) {
12358 assert(V.getNode() && "The single defined operand is empty!")(static_cast <bool> (V.getNode() && "The single defined operand is empty!"
) ? void (0) : __assert_fail ("V.getNode() && \"The single defined operand is empty!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 12358, __extension__ __PRETTY_FUNCTION__))
;
12359 SmallVector<SDValue, 8> Opnds;
12360 for (unsigned i = 0, e = VTs.size(); i != e; ++i) {
12361 if (i != Idx) {
12362 Opnds.push_back(DAG.getUNDEF(VTs[i]));
12363 continue;
12364 }
12365 SDValue NV = DAG.getNode(ISD::TRUNCATE, SDLoc(V), VTs[i], V);
12366 AddToWorklist(NV.getNode());
12367 Opnds.push_back(NV);
12368 }
12369 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, Opnds);
12370 }
12371 }
12372
12373 // Fold truncate of a bitcast of a vector to an extract of the low vector
12374 // element.
12375 //
12376 // e.g. trunc (i64 (bitcast v2i32:x)) -> extract_vector_elt v2i32:x, idx
12377 if (N0.getOpcode() == ISD::BITCAST && !VT.isVector()) {
12378 SDValue VecSrc = N0.getOperand(0);
12379 EVT VecSrcVT = VecSrc.getValueType();
12380 if (VecSrcVT.isVector() && VecSrcVT.getScalarType() == VT &&
12381 (!LegalOperations ||
12382 TLI.isOperationLegal(ISD::EXTRACT_VECTOR_ELT, VecSrcVT))) {
12383 SDLoc SL(N);
12384
12385 unsigned Idx = isLE ? 0 : VecSrcVT.getVectorNumElements() - 1;
12386 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, VT, VecSrc,
12387 DAG.getVectorIdxConstant(Idx, SL));
12388 }
12389 }
12390
12391 // Simplify the operands using demanded-bits information.
12392 if (SimplifyDemandedBits(SDValue(N, 0)))
12393 return SDValue(N, 0);
12394
12395 // (trunc adde(X, Y, Carry)) -> (adde trunc(X), trunc(Y), Carry)
12396 // (trunc addcarry(X, Y, Carry)) -> (addcarry trunc(X), trunc(Y), Carry)
12397 // When the adde's carry is not used.
12398 if ((N0.getOpcode() == ISD::ADDE || N0.getOpcode() == ISD::ADDCARRY) &&
12399 N0.hasOneUse() && !N0.getNode()->hasAnyUseOfValue(1) &&
12400 // We only do for addcarry before legalize operation
12401 ((!LegalOperations && N0.getOpcode() == ISD::ADDCARRY) ||
12402 TLI.isOperationLegal(N0.getOpcode(), VT))) {
12403 SDLoc SL(N);
12404 auto X = DAG.getNode(ISD::TRUNCATE, SL, VT, N0.getOperand(0));
12405 auto Y = DAG.getNode(ISD::TRUNCATE, SL, VT, N0.getOperand(1));
12406 auto VTs = DAG.getVTList(VT, N0->getValueType(1));
12407 return DAG.getNode(N0.getOpcode(), SL, VTs, X, Y, N0.getOperand(2));
12408 }
12409
12410 // fold (truncate (extract_subvector(ext x))) ->
12411 // (extract_subvector x)
12412 // TODO: This can be generalized to cover cases where the truncate and extract
12413 // do not fully cancel each other out.
12414 if (!LegalTypes && N0.getOpcode() == ISD::EXTRACT_SUBVECTOR) {
12415 SDValue N00 = N0.getOperand(0);
12416 if (N00.getOpcode() == ISD::SIGN_EXTEND ||
12417 N00.getOpcode() == ISD::ZERO_EXTEND ||
12418 N00.getOpcode() == ISD::ANY_EXTEND) {
12419 if (N00.getOperand(0)->getValueType(0).getVectorElementType() ==
12420 VT.getVectorElementType())
12421 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(N0->getOperand(0)), VT,
12422 N00.getOperand(0), N0.getOperand(1));
12423 }
12424 }
12425
12426 if (SDValue NewVSel = matchVSelectOpSizesWithSetCC(N))
12427 return NewVSel;
12428
12429 // Narrow a suitable binary operation with a non-opaque constant operand by
12430 // moving it ahead of the truncate. This is limited to pre-legalization
12431 // because targets may prefer a wider type during later combines and invert
12432 // this transform.
12433 switch (N0.getOpcode()) {
12434 case ISD::ADD:
12435 case ISD::SUB:
12436 case ISD::MUL:
12437 case ISD::AND:
12438 case ISD::OR:
12439 case ISD::XOR:
12440 if (!LegalOperations && N0.hasOneUse() &&
12441 (isConstantOrConstantVector(N0.getOperand(0), true) ||
12442 isConstantOrConstantVector(N0.getOperand(1), true))) {
12443 // TODO: We already restricted this to pre-legalization, but for vectors
12444 // we are extra cautious to not create an unsupported operation.
12445 // Target-specific changes are likely needed to avoid regressions here.
12446 if (VT.isScalarInteger() || TLI.isOperationLegal(N0.getOpcode(), VT)) {
12447 SDLoc DL(N);
12448 SDValue NarrowL = DAG.getNode(ISD::TRUNCATE, DL, VT, N0.getOperand(0));
12449 SDValue NarrowR = DAG.getNode(ISD::TRUNCATE, DL, VT, N0.getOperand(1));
12450 return DAG.getNode(N0.getOpcode(), DL, VT, NarrowL, NarrowR);
12451 }
12452 }
12453 break;
12454 case ISD::USUBSAT:
12455 // Truncate the USUBSAT only if LHS is a known zero-extension, its not
12456 // enough to know that the upper bits are zero we must ensure that we don't
12457 // introduce an extra truncate.
12458 if (!LegalOperations && N0.hasOneUse() &&
12459 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
12460 N0.getOperand(0).getOperand(0).getScalarValueSizeInBits() <=
12461 VT.getScalarSizeInBits() &&
12462 hasOperation(N0.getOpcode(), VT)) {
12463 return getTruncatedUSUBSAT(VT, SrcVT, N0.getOperand(0), N0.getOperand(1),
12464 DAG, SDLoc(N));
12465 }
12466 break;
12467 }
12468
12469 return SDValue();
12470}
12471
12472static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
12473 SDValue Elt = N->getOperand(i);
12474 if (Elt.getOpcode() != ISD::MERGE_VALUES)
12475 return Elt.getNode();
12476 return Elt.getOperand(Elt.getResNo()).getNode();
12477}
12478
12479/// build_pair (load, load) -> load
12480/// if load locations are consecutive.
12481SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
12482 assert(N->getOpcode() == ISD::BUILD_PAIR)(static_cast <bool> (N->getOpcode() == ISD::BUILD_PAIR
) ? void (0) : __assert_fail ("N->getOpcode() == ISD::BUILD_PAIR"
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 12482, __extension__ __PRETTY_FUNCTION__))
;
12483
12484 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
12485 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
12486
12487 // A BUILD_PAIR is always having the least significant part in elt 0 and the
12488 // most significant part in elt 1. So when combining into one large load, we
12489 // need to consider the endianness.
12490 if (DAG.getDataLayout().isBigEndian())
12491 std::swap(LD1, LD2);
12492
12493 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse() ||
12494 LD1->getAddressSpace() != LD2->getAddressSpace())
12495 return SDValue();
12496 EVT LD1VT = LD1->getValueType(0);
12497 unsigned LD1Bytes = LD1VT.getStoreSize();
12498 if (ISD::isNON_EXTLoad(LD2) && LD2->hasOneUse() &&
12499 DAG.areNonVolatileConsecutiveLoads(LD2, LD1, LD1Bytes, 1)) {
12500 Align Alignment = LD1->getAlign();
12501 Align NewAlign = DAG.getDataLayout().getABITypeAlign(
12502 VT.getTypeForEVT(*DAG.getContext()));
12503
12504 if (NewAlign <= Alignment &&
12505 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
12506 return DAG.getLoad(VT, SDLoc(N), LD1->getChain(), LD1->getBasePtr(),
12507 LD1->getPointerInfo(), Alignment);
12508 }
12509
12510 return SDValue();
12511}
12512
12513static unsigned getPPCf128HiElementSelector(const SelectionDAG &DAG) {
12514 // On little-endian machines, bitcasting from ppcf128 to i128 does swap the Hi
12515 // and Lo parts; on big-endian machines it doesn't.
12516 return DAG.getDataLayout().isBigEndian() ? 1 : 0;
12517}
12518
12519static SDValue foldBitcastedFPLogic(SDNode *N, SelectionDAG &DAG,
12520 const TargetLowering &TLI) {
12521 // If this is not a bitcast to an FP type or if the target doesn't have
12522 // IEEE754-compliant FP logic, we're done.
12523 EVT VT = N->getValueType(0);
12524 if (!VT.isFloatingPoint() || !TLI.hasBitPreservingFPLogic(VT))
12525 return SDValue();
12526
12527 // TODO: Handle cases where the integer constant is a different scalar
12528 // bitwidth to the FP.
12529 SDValue N0 = N->getOperand(0);
12530 EVT SourceVT = N0.getValueType();
12531 if (VT.getScalarSizeInBits() != SourceVT.getScalarSizeInBits())
12532 return SDValue();
12533
12534 unsigned FPOpcode;
12535 APInt SignMask;
12536 switch (N0.getOpcode()) {
12537 case ISD::AND:
12538 FPOpcode = ISD::FABS;
12539 SignMask = ~APInt::getSignMask(SourceVT.getScalarSizeInBits());
12540 break;
12541 case ISD::XOR:
12542 FPOpcode = ISD::FNEG;
12543 SignMask = APInt::getSignMask(SourceVT.getScalarSizeInBits());
12544 break;
12545 case ISD::OR:
12546 FPOpcode = ISD::FABS;
12547 SignMask = APInt::getSignMask(SourceVT.getScalarSizeInBits());
12548 break;
12549 default:
12550 return SDValue();
12551 }
12552
12553 // Fold (bitcast int (and (bitcast fp X to int), 0x7fff...) to fp) -> fabs X
12554 // Fold (bitcast int (xor (bitcast fp X to int), 0x8000...) to fp) -> fneg X
12555 // Fold (bitcast int (or (bitcast fp X to int), 0x8000...) to fp) ->
12556 // fneg (fabs X)
12557 SDValue LogicOp0 = N0.getOperand(0);
12558 ConstantSDNode *LogicOp1 = isConstOrConstSplat(N0.getOperand(1), true);
12559 if (LogicOp1 && LogicOp1->getAPIntValue() == SignMask &&
12560 LogicOp0.getOpcode() == ISD::BITCAST &&
12561 LogicOp0.getOperand(0).getValueType() == VT) {
12562 SDValue FPOp = DAG.getNode(FPOpcode, SDLoc(N), VT, LogicOp0.getOperand(0));
12563 NumFPLogicOpsConv++;
12564 if (N0.getOpcode() == ISD::OR)
12565 return DAG.getNode(ISD::FNEG, SDLoc(N), VT, FPOp);
12566 return FPOp;
12567 }
12568
12569 return SDValue();
12570}
12571
12572SDValue DAGCombiner::visitBITCAST(SDNode *N) {
12573 SDValue N0 = N->getOperand(0);
12574 EVT VT = N->getValueType(0);
12575
12576 if (N0.isUndef())
12577 return DAG.getUNDEF(VT);
12578
12579 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
12580 // Only do this before legalize types, unless both types are integer and the
12581 // scalar type is legal. Only do this before legalize ops, since the target
12582 // maybe depending on the bitcast.
12583 // First check to see if this is all constant.
12584 // TODO: Support FP bitcasts after legalize types.
12585 if (VT.isVector() &&
12586 (!LegalTypes ||
12587 (!LegalOperations && VT.isInteger() && N0.getValueType().isInteger() &&
12588 TLI.isTypeLegal(VT.getVectorElementType()))) &&
12589 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
12590 cast<BuildVectorSDNode>(N0)->isConstant())
12591 return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(),
12592 VT.getVectorElementType());
12593
12594 // If the input is a constant, let getNode fold it.
12595 if (isIntOrFPConstant(N0)) {
12596 // If we can't allow illegal operations, we need to check that this is just
12597 // a fp -> int or int -> conversion and that the resulting operation will
12598 // be legal.
12599 if (!LegalOperations ||
12600 (isa<ConstantSDNode>(N0) && VT.isFloatingPoint() && !VT.isVector() &&
12601 TLI.isOperationLegal(ISD::ConstantFP, VT)) ||
12602 (isa<ConstantFPSDNode>(N0) && VT.isInteger() && !VT.isVector() &&
12603 TLI.isOperationLegal(ISD::Constant, VT))) {
12604 SDValue C = DAG.getBitcast(VT, N0);
12605 if (C.getNode() != N)
12606 return C;
12607 }
12608 }
12609
12610 // (conv (conv x, t1), t2) -> (conv x, t2)
12611 if (N0.getOpcode() == ISD::BITCAST)
12612 return DAG.getBitcast(VT, N0.getOperand(0));
12613
12614 // fold (conv (load x)) -> (load (conv*)x)
12615 // If the resultant load doesn't need a higher alignment than the original!
12616 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
12617 // Do not remove the cast if the types differ in endian layout.
12618 TLI.hasBigEndianPartOrdering(N0.getValueType(), DAG.getDataLayout()) ==
12619 TLI.hasBigEndianPartOrdering(VT, DAG.getDataLayout()) &&
12620 // If the load is volatile, we only want to change the load type if the
12621 // resulting load is legal. Otherwise we might increase the number of
12622 // memory accesses. We don't care if the original type was legal or not
12623 // as we assume software couldn't rely on the number of accesses of an
12624 // illegal type.
12625 ((!LegalOperations && cast<LoadSDNode>(N0)->isSimple()) ||
12626 TLI.isOperationLegal(ISD::LOAD, VT))) {
12627 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
12628
12629 if (TLI.isLoadBitCastBeneficial(N0.getValueType(), VT, DAG,
12630 *LN0->getMemOperand())) {
12631 SDValue Load =
12632 DAG.getLoad(VT, SDLoc(N), LN0->getChain(), LN0->getBasePtr(),
12633 LN0->getPointerInfo(), LN0->getAlign(),
12634 LN0->getMemOperand()->getFlags(), LN0->getAAInfo());
12635 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1));
12636 return Load;
12637 }
12638 }
12639
12640 if (SDValue V = foldBitcastedFPLogic(N, DAG, TLI))
12641 return V;
12642
12643 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
12644 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
12645 //
12646 // For ppc_fp128:
12647 // fold (bitcast (fneg x)) ->
12648 // flipbit = signbit
12649 // (xor (bitcast x) (build_pair flipbit, flipbit))
12650 //
12651 // fold (bitcast (fabs x)) ->
12652 // flipbit = (and (extract_element (bitcast x), 0), signbit)
12653 // (xor (bitcast x) (build_pair flipbit, flipbit))
12654 // This often reduces constant pool loads.
12655 if (((N0.getOpcode() == ISD::FNEG && !TLI.isFNegFree(N0.getValueType())) ||
12656 (N0.getOpcode() == ISD::FABS && !TLI.isFAbsFree(N0.getValueType()))) &&
12657 N0.getNode()->hasOneUse() && VT.isInteger() &&
12658 !VT.isVector() && !N0.getValueType().isVector()) {
12659 SDValue NewConv = DAG.getBitcast(VT, N0.getOperand(0));
12660 AddToWorklist(NewConv.getNode());
12661
12662 SDLoc DL(N);
12663 if (N0.getValueType() == MVT::ppcf128 && !LegalTypes) {
12664 assert(VT.getSizeInBits() == 128)(static_cast <bool> (VT.getSizeInBits() == 128) ? void (
0) : __assert_fail ("VT.getSizeInBits() == 128", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 12664, __extension__ __PRETTY_FUNCTION__))
;
12665 SDValue SignBit = DAG.getConstant(
12666 APInt::getSignMask(VT.getSizeInBits() / 2), SDLoc(N0), MVT::i64);
12667 SDValue FlipBit;
12668 if (N0.getOpcode() == ISD::FNEG) {
12669 FlipBit = SignBit;
12670 AddToWorklist(FlipBit.getNode());
12671 } else {
12672 assert(N0.getOpcode() == ISD::FABS)(static_cast <bool> (N0.getOpcode() == ISD::FABS) ? void
(0) : __assert_fail ("N0.getOpcode() == ISD::FABS", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 12672, __extension__ __PRETTY_FUNCTION__))
;
12673 SDValue Hi =
12674 DAG.getNode(ISD::EXTRACT_ELEMENT, SDLoc(NewConv), MVT::i64, NewConv,
12675 DAG.getIntPtrConstant(getPPCf128HiElementSelector(DAG),
12676 SDLoc(NewConv)));
12677 AddToWorklist(Hi.getNode());
12678 FlipBit = DAG.getNode(ISD::AND, SDLoc(N0), MVT::i64, Hi, SignBit);
12679 AddToWorklist(FlipBit.getNode());
12680 }
12681 SDValue FlipBits =
12682 DAG.getNode(ISD::BUILD_PAIR, SDLoc(N0), VT, FlipBit, FlipBit);
12683 AddToWorklist(FlipBits.getNode());
12684 return DAG.getNode(ISD::XOR, DL, VT, NewConv, FlipBits);
12685 }
12686 APInt SignBit = APInt::getSignMask(VT.getSizeInBits());
12687 if (N0.getOpcode() == ISD::FNEG)
12688 return DAG.getNode(ISD::XOR, DL, VT,
12689 NewConv, DAG.getConstant(SignBit, DL, VT));
12690 assert(N0.getOpcode() == ISD::FABS)(static_cast <bool> (N0.getOpcode() == ISD::FABS) ? void
(0) : __assert_fail ("N0.getOpcode() == ISD::FABS", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 12690, __extension__ __PRETTY_FUNCTION__))
;
12691 return DAG.getNode(ISD::AND, DL, VT,
12692 NewConv, DAG.getConstant(~SignBit, DL, VT));
12693 }
12694
12695 // fold (bitconvert (fcopysign cst, x)) ->
12696 // (or (and (bitconvert x), sign), (and cst, (not sign)))
12697 // Note that we don't handle (copysign x, cst) because this can always be
12698 // folded to an fneg or fabs.
12699 //
12700 // For ppc_fp128:
12701 // fold (bitcast (fcopysign cst, x)) ->
12702 // flipbit = (and (extract_element
12703 // (xor (bitcast cst), (bitcast x)), 0),
12704 // signbit)
12705 // (xor (bitcast cst) (build_pair flipbit, flipbit))
12706 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
12707 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
12708 VT.isInteger() && !VT.isVector()) {
12709 unsigned OrigXWidth = N0.getOperand(1).getValueSizeInBits();
12710 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth);
12711 if (isTypeLegal(IntXVT)) {
12712 SDValue X = DAG.getBitcast(IntXVT, N0.getOperand(1));
12713 AddToWorklist(X.getNode());
12714
12715 // If X has a different width than the result/lhs, sext it or truncate it.
12716 unsigned VTWidth = VT.getSizeInBits();
12717 if (OrigXWidth < VTWidth) {
12718 X = DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, X);
12719 AddToWorklist(X.getNode());
12720 } else if (OrigXWidth > VTWidth) {
12721 // To get the sign bit in the right place, we have to shift it right
12722 // before truncating.
12723 SDLoc DL(X);
12724 X = DAG.getNode(ISD::SRL, DL,
12725 X.getValueType(), X,
12726 DAG.getConstant(OrigXWidth-VTWidth, DL,
12727 X.getValueType()));
12728 AddToWorklist(X.getNode());
12729 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
12730 AddToWorklist(X.getNode());
12731 }
12732
12733 if (N0.getValueType() == MVT::ppcf128 && !LegalTypes) {
12734 APInt SignBit = APInt::getSignMask(VT.getSizeInBits() / 2);
12735 SDValue Cst = DAG.getBitcast(VT, N0.getOperand(0));
12736 AddToWorklist(Cst.getNode());
12737 SDValue X = DAG.getBitcast(VT, N0.getOperand(1));
12738 AddToWorklist(X.getNode());
12739 SDValue XorResult = DAG.getNode(ISD::XOR, SDLoc(N0), VT, Cst, X);
12740 AddToWorklist(XorResult.getNode());
12741 SDValue XorResult64 = DAG.getNode(
12742 ISD::EXTRACT_ELEMENT, SDLoc(XorResult), MVT::i64, XorResult,
12743 DAG.getIntPtrConstant(getPPCf128HiElementSelector(DAG),
12744 SDLoc(XorResult)));
12745 AddToWorklist(XorResult64.getNode());
12746 SDValue FlipBit =
12747 DAG.getNode(ISD::AND, SDLoc(XorResult64), MVT::i64, XorResult64,
12748 DAG.getConstant(SignBit, SDLoc(XorResult64), MVT::i64));
12749 AddToWorklist(FlipBit.getNode());
12750 SDValue FlipBits =
12751 DAG.getNode(ISD::BUILD_PAIR, SDLoc(N0), VT, FlipBit, FlipBit);
12752 AddToWorklist(FlipBits.getNode());
12753 return DAG.getNode(ISD::XOR, SDLoc(N), VT, Cst, FlipBits);
12754 }
12755 APInt SignBit = APInt::getSignMask(VT.getSizeInBits());
12756 X = DAG.getNode(ISD::AND, SDLoc(X), VT,
12757 X, DAG.getConstant(SignBit, SDLoc(X), VT));
12758 AddToWorklist(X.getNode());
12759
12760 SDValue Cst = DAG.getBitcast(VT, N0.getOperand(0));
12761 Cst = DAG.getNode(ISD::AND, SDLoc(Cst), VT,
12762 Cst, DAG.getConstant(~SignBit, SDLoc(Cst), VT));
12763 AddToWorklist(Cst.getNode());
12764
12765 return DAG.getNode(ISD::OR, SDLoc(N), VT, X, Cst);
12766 }
12767 }
12768
12769 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
12770 if (N0.getOpcode() == ISD::BUILD_PAIR)
12771 if (SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT))
12772 return CombineLD;
12773
12774 // Remove double bitcasts from shuffles - this is often a legacy of
12775 // XformToShuffleWithZero being used to combine bitmaskings (of
12776 // float vectors bitcast to integer vectors) into shuffles.
12777 // bitcast(shuffle(bitcast(s0),bitcast(s1))) -> shuffle(s0,s1)
12778 if (Level < AfterLegalizeDAG && TLI.isTypeLegal(VT) && VT.isVector() &&
12779 N0->getOpcode() == ISD::VECTOR_SHUFFLE && N0.hasOneUse() &&
12780 VT.getVectorNumElements() >= N0.getValueType().getVectorNumElements() &&
12781 !(VT.getVectorNumElements() % N0.getValueType().getVectorNumElements())) {
12782 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N0);
12783
12784 // If operands are a bitcast, peek through if it casts the original VT.
12785 // If operands are a constant, just bitcast back to original VT.
12786 auto PeekThroughBitcast = [&](SDValue Op) {
12787 if (Op.getOpcode() == ISD::BITCAST &&
12788 Op.getOperand(0).getValueType() == VT)
12789 return SDValue(Op.getOperand(0));
12790 if (Op.isUndef() || ISD::isBuildVectorOfConstantSDNodes(Op.getNode()) ||
12791 ISD::isBuildVectorOfConstantFPSDNodes(Op.getNode()))
12792 return DAG.getBitcast(VT, Op);
12793 return SDValue();
12794 };
12795
12796 // FIXME: If either input vector is bitcast, try to convert the shuffle to
12797 // the result type of this bitcast. This would eliminate at least one
12798 // bitcast. See the transform in InstCombine.
12799 SDValue SV0 = PeekThroughBitcast(N0->getOperand(0));
12800 SDValue SV1 = PeekThroughBitcast(N0->getOperand(1));
12801 if (!(SV0 && SV1))
12802 return SDValue();
12803
12804 int MaskScale =
12805 VT.getVectorNumElements() / N0.getValueType().getVectorNumElements();
12806 SmallVector<int, 8> NewMask;
12807 for (int M : SVN->getMask())
12808 for (int i = 0; i != MaskScale; ++i)
12809 NewMask.push_back(M < 0 ? -1 : M * MaskScale + i);
12810
12811 SDValue LegalShuffle =
12812 TLI.buildLegalVectorShuffle(VT, SDLoc(N), SV0, SV1, NewMask, DAG);
12813 if (LegalShuffle)
12814 return LegalShuffle;
12815 }
12816
12817 return SDValue();
12818}
12819
12820SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
12821 EVT VT = N->getValueType(0);
12822 return CombineConsecutiveLoads(N, VT);
12823}
12824
12825SDValue DAGCombiner::visitFREEZE(SDNode *N) {
12826 SDValue N0 = N->getOperand(0);
12827
12828 if (DAG.isGuaranteedNotToBeUndefOrPoison(N0, /*PoisonOnly*/ false))
12829 return N0;
12830
12831 return SDValue();
12832}
12833
12834/// We know that BV is a build_vector node with Constant, ConstantFP or Undef
12835/// operands. DstEltVT indicates the destination element value type.
12836SDValue DAGCombiner::
12837ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
12838 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
12839
12840 // If this is already the right type, we're done.
12841 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
12842
12843 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
12844 unsigned DstBitSize = DstEltVT.getSizeInBits();
12845
12846 // If this is a conversion of N elements of one type to N elements of another
12847 // type, convert each element. This handles FP<->INT cases.
12848 if (SrcBitSize == DstBitSize) {
12849 SmallVector<SDValue, 8> Ops;
12850 for (SDValue Op : BV->op_values()) {
12851 // If the vector element type is not legal, the BUILD_VECTOR operands
12852 // are promoted and implicitly truncated. Make that explicit here.
12853 if (Op.getValueType() != SrcEltVT)
12854 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(BV), SrcEltVT, Op);
12855 Ops.push_back(DAG.getBitcast(DstEltVT, Op));
12856 AddToWorklist(Ops.back().getNode());
12857 }
12858 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
12859 BV->getValueType(0).getVectorNumElements());
12860 return DAG.getBuildVector(VT, SDLoc(BV), Ops);
12861 }
12862
12863 // Otherwise, we're growing or shrinking the elements. To avoid having to
12864 // handle annoying details of growing/shrinking FP values, we convert them to
12865 // int first.
12866 if (SrcEltVT.isFloatingPoint()) {
12867 // Convert the input float vector to a int vector where the elements are the
12868 // same sizes.
12869 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits());
12870 BV = ConstantFoldBITCASTofBUILD_VECTOR(BV, IntVT).getNode();
12871 SrcEltVT = IntVT;
12872 }
12873
12874 // Now we know the input is an integer vector. If the output is a FP type,
12875 // convert to integer first, then to FP of the right size.
12876 if (DstEltVT.isFloatingPoint()) {
12877 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits());
12878 SDNode *Tmp = ConstantFoldBITCASTofBUILD_VECTOR(BV, TmpVT).getNode();
12879
12880 // Next, convert to FP elements of the same size.
12881 return ConstantFoldBITCASTofBUILD_VECTOR(Tmp, DstEltVT);
12882 }
12883
12884 SDLoc DL(BV);
12885
12886 // Okay, we know the src/dst types are both integers of differing types.
12887 // Handling growing first.
12888 assert(SrcEltVT.isInteger() && DstEltVT.isInteger())(static_cast <bool> (SrcEltVT.isInteger() && DstEltVT
.isInteger()) ? void (0) : __assert_fail ("SrcEltVT.isInteger() && DstEltVT.isInteger()"
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 12888, __extension__ __PRETTY_FUNCTION__))
;
12889 if (SrcBitSize < DstBitSize) {
12890 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
12891
12892 SmallVector<SDValue, 8> Ops;
12893 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
12894 i += NumInputsPerOutput) {
12895 bool isLE = DAG.getDataLayout().isLittleEndian();
12896 APInt NewBits = APInt(DstBitSize, 0);
12897 bool EltIsUndef = true;
12898 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
12899 // Shift the previously computed bits over.
12900 NewBits <<= SrcBitSize;
12901 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
12902 if (Op.isUndef()) continue;
12903 EltIsUndef = false;
12904
12905 NewBits |= cast<ConstantSDNode>(Op)->getAPIntValue().
12906 zextOrTrunc(SrcBitSize).zext(DstBitSize);
12907 }
12908
12909 if (EltIsUndef)
12910 Ops.push_back(DAG.getUNDEF(DstEltVT));
12911 else
12912 Ops.push_back(DAG.getConstant(NewBits, DL, DstEltVT));
12913 }
12914
12915 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size());
12916 return DAG.getBuildVector(VT, DL, Ops);
12917 }
12918
12919 // Finally, this must be the case where we are shrinking elements: each input
12920 // turns into multiple outputs.
12921 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
12922 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
12923 NumOutputsPerInput*BV->getNumOperands());
12924 SmallVector<SDValue, 8> Ops;
12925
12926 for (const SDValue &Op : BV->op_values()) {
12927 if (Op.isUndef()) {
12928 Ops.append(NumOutputsPerInput, DAG.getUNDEF(DstEltVT));
12929 continue;
12930 }
12931
12932 APInt OpVal = cast<ConstantSDNode>(Op)->
12933 getAPIntValue().zextOrTrunc(SrcBitSize);
12934
12935 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
12936 APInt ThisVal = OpVal.trunc(DstBitSize);
12937 Ops.push_back(DAG.getConstant(ThisVal, DL, DstEltVT));
12938 OpVal.lshrInPlace(DstBitSize);
12939 }
12940
12941 // For big endian targets, swap the order of the pieces of each element.
12942 if (DAG.getDataLayout().isBigEndian())
12943 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
12944 }
12945
12946 return DAG.getBuildVector(VT, DL, Ops);
12947}
12948
12949/// Try to perform FMA combining on a given FADD node.
12950SDValue DAGCombiner::visitFADDForFMACombine(SDNode *N) {
12951 SDValue N0 = N->getOperand(0);
12952 SDValue N1 = N->getOperand(1);
12953 EVT VT = N->getValueType(0);
12954 SDLoc SL(N);
12955
12956 const TargetOptions &Options = DAG.getTarget().Options;
12957
12958 // Floating-point multiply-add with intermediate rounding.
12959 bool HasFMAD = (LegalOperations && TLI.isFMADLegal(DAG, N));
12960
12961 // Floating-point multiply-add without intermediate rounding.
12962 bool HasFMA =
12963 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT) &&
12964 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT));
12965
12966 // No valid opcode, do not combine.
12967 if (!HasFMAD && !HasFMA)
12968 return SDValue();
12969
12970 bool CanReassociate =
12971 Options.UnsafeFPMath || N->getFlags().hasAllowReassociation();
12972 bool AllowFusionGlobally = (Options.AllowFPOpFusion == FPOpFusion::Fast ||
12973 Options.UnsafeFPMath || HasFMAD);
12974 // If the addition is not contractable, do not combine.
12975 if (!AllowFusionGlobally && !N->getFlags().hasAllowContract())
12976 return SDValue();
12977
12978 if (TLI.generateFMAsInMachineCombiner(VT, OptLevel))
12979 return SDValue();
12980
12981 // Always prefer FMAD to FMA for precision.
12982 unsigned PreferredFusedOpcode = HasFMAD ? ISD::FMAD : ISD::FMA;
12983 bool Aggressive = TLI.enableAggressiveFMAFusion(VT);
12984
12985 // Is the node an FMUL and contractable either due to global flags or
12986 // SDNodeFlags.
12987 auto isContractableFMUL = [AllowFusionGlobally](SDValue N) {
12988 if (N.getOpcode() != ISD::FMUL)
12989 return false;
12990 return AllowFusionGlobally || N->getFlags().hasAllowContract();
12991 };
12992 // If we have two choices trying to fold (fadd (fmul u, v), (fmul x, y)),
12993 // prefer to fold the multiply with fewer uses.
12994 if (Aggressive && isContractableFMUL(N0) && isContractableFMUL(N1)) {
12995 if (N0.getNode()->use_size() > N1.getNode()->use_size())
12996 std::swap(N0, N1);
12997 }
12998
12999 // fold (fadd (fmul x, y), z) -> (fma x, y, z)
13000 if (isContractableFMUL(N0) && (Aggressive || N0->hasOneUse())) {
13001 return DAG.getNode(PreferredFusedOpcode, SL, VT, N0.getOperand(0),
13002 N0.getOperand(1), N1);
13003 }
13004
13005 // fold (fadd x, (fmul y, z)) -> (fma y, z, x)
13006 // Note: Commutes FADD operands.
13007 if (isContractableFMUL(N1) && (Aggressive || N1->hasOneUse())) {
13008 return DAG.getNode(PreferredFusedOpcode, SL, VT, N1.getOperand(0),
13009 N1.getOperand(1), N0);
13010 }
13011
13012 // fadd (fma A, B, (fmul C, D)), E --> fma A, B, (fma C, D, E)
13013 // fadd E, (fma A, B, (fmul C, D)) --> fma A, B, (fma C, D, E)
13014 // This requires reassociation because it changes the order of operations.
13015 SDValue FMA, E;
13016 if (CanReassociate && N0.getOpcode() == PreferredFusedOpcode &&
13017 N0.getOperand(2).getOpcode() == ISD::FMUL && N0.hasOneUse() &&
13018 N0.getOperand(2).hasOneUse()) {
13019 FMA = N0;
13020 E = N1;
13021 } else if (CanReassociate && N1.getOpcode() == PreferredFusedOpcode &&
13022 N1.getOperand(2).getOpcode() == ISD::FMUL && N1.hasOneUse() &&
13023 N1.getOperand(2).hasOneUse()) {
13024 FMA = N1;
13025 E = N0;
13026 }
13027 if (FMA && E) {
13028 SDValue A = FMA.getOperand(0);
13029 SDValue B = FMA.getOperand(1);
13030 SDValue C = FMA.getOperand(2).getOperand(0);
13031 SDValue D = FMA.getOperand(2).getOperand(1);
13032 SDValue CDE = DAG.getNode(PreferredFusedOpcode, SL, VT, C, D, E);
13033 return DAG.getNode(PreferredFusedOpcode, SL, VT, A, B, CDE);
13034 }
13035
13036 // Look through FP_EXTEND nodes to do more combining.
13037
13038 // fold (fadd (fpext (fmul x, y)), z) -> (fma (fpext x), (fpext y), z)
13039 if (N0.getOpcode() == ISD::FP_EXTEND) {
13040 SDValue N00 = N0.getOperand(0);
13041 if (isContractableFMUL(N00) &&
13042 TLI.isFPExtFoldable(DAG, PreferredFusedOpcode, VT,
13043 N00.getValueType())) {
13044 return DAG.getNode(PreferredFusedOpcode, SL, VT,
13045 DAG.getNode(ISD::FP_EXTEND, SL, VT, N00.getOperand(0)),
13046 DAG.getNode(ISD::FP_EXTEND, SL, VT, N00.getOperand(1)),
13047 N1);
13048 }
13049 }
13050
13051 // fold (fadd x, (fpext (fmul y, z))) -> (fma (fpext y), (fpext z), x)
13052 // Note: Commutes FADD operands.
13053 if (N1.getOpcode() == ISD::FP_EXTEND) {
13054 SDValue N10 = N1.getOperand(0);
13055 if (isContractableFMUL(N10) &&
13056 TLI.isFPExtFoldable(DAG, PreferredFusedOpcode, VT,
13057 N10.getValueType())) {
13058 return DAG.getNode(PreferredFusedOpcode, SL, VT,
13059 DAG.getNode(ISD::FP_EXTEND, SL, VT, N10.getOperand(0)),
13060 DAG.getNode(ISD::FP_EXTEND, SL, VT, N10.getOperand(1)),
13061 N0);
13062 }
13063 }
13064
13065 // More folding opportunities when target permits.
13066 if (Aggressive) {
13067 // fold (fadd (fma x, y, (fpext (fmul u, v))), z)
13068 // -> (fma x, y, (fma (fpext u), (fpext v), z))
13069 auto FoldFAddFMAFPExtFMul = [&](SDValue X, SDValue Y, SDValue U, SDValue V,
13070 SDValue Z) {
13071 return DAG.getNode(PreferredFusedOpcode, SL, VT, X, Y,
13072 DAG.getNode(PreferredFusedOpcode, SL, VT,
13073 DAG.getNode(ISD::FP_EXTEND, SL, VT, U),
13074 DAG.getNode(ISD::FP_EXTEND, SL, VT, V),
13075 Z));
13076 };
13077 if (N0.getOpcode() == PreferredFusedOpcode) {
13078 SDValue N02 = N0.getOperand(2);
13079 if (N02.getOpcode() == ISD::FP_EXTEND) {
13080 SDValue N020 = N02.getOperand(0);
13081 if (isContractableFMUL(N020) &&
13082 TLI.isFPExtFoldable(DAG, PreferredFusedOpcode, VT,
13083 N020.getValueType())) {
13084 return FoldFAddFMAFPExtFMul(N0.getOperand(0), N0.getOperand(1),
13085 N020.getOperand(0), N020.getOperand(1),
13086 N1);
13087 }
13088 }
13089 }
13090
13091 // fold (fadd (fpext (fma x, y, (fmul u, v))), z)
13092 // -> (fma (fpext x), (fpext y), (fma (fpext u), (fpext v), z))
13093 // FIXME: This turns two single-precision and one double-precision
13094 // operation into two double-precision operations, which might not be
13095 // interesting for all targets, especially GPUs.
13096 auto FoldFAddFPExtFMAFMul = [&](SDValue X, SDValue Y, SDValue U, SDValue V,
13097 SDValue Z) {
13098 return DAG.getNode(
13099 PreferredFusedOpcode, SL, VT, DAG.getNode(ISD::FP_EXTEND, SL, VT, X),
13100 DAG.getNode(ISD::FP_EXTEND, SL, VT, Y),
13101 DAG.getNode(PreferredFusedOpcode, SL, VT,
13102 DAG.getNode(ISD::FP_EXTEND, SL, VT, U),
13103 DAG.getNode(ISD::FP_EXTEND, SL, VT, V), Z));
13104 };
13105 if (N0.getOpcode() == ISD::FP_EXTEND) {
13106 SDValue N00 = N0.getOperand(0);
13107 if (N00.getOpcode() == PreferredFusedOpcode) {
13108 SDValue N002 = N00.getOperand(2);
13109 if (isContractableFMUL(N002) &&
13110 TLI.isFPExtFoldable(DAG, PreferredFusedOpcode, VT,
13111 N00.getValueType())) {
13112 return FoldFAddFPExtFMAFMul(N00.getOperand(0), N00.getOperand(1),
13113 N002.getOperand(0), N002.getOperand(1),
13114 N1);
13115 }
13116 }
13117 }
13118
13119 // fold (fadd x, (fma y, z, (fpext (fmul u, v)))
13120 // -> (fma y, z, (fma (fpext u), (fpext v), x))
13121 if (N1.getOpcode() == PreferredFusedOpcode) {
13122 SDValue N12 = N1.getOperand(2);
13123 if (N12.getOpcode() == ISD::FP_EXTEND) {
13124 SDValue N120 = N12.getOperand(0);
13125 if (isContractableFMUL(N120) &&
13126 TLI.isFPExtFoldable(DAG, PreferredFusedOpcode, VT,
13127 N120.getValueType())) {
13128 return FoldFAddFMAFPExtFMul(N1.getOperand(0), N1.getOperand(1),
13129 N120.getOperand(0), N120.getOperand(1),
13130 N0);
13131 }
13132 }
13133 }
13134
13135 // fold (fadd x, (fpext (fma y, z, (fmul u, v)))
13136 // -> (fma (fpext y), (fpext z), (fma (fpext u), (fpext v), x))
13137 // FIXME: This turns two single-precision and one double-precision
13138 // operation into two double-precision operations, which might not be
13139 // interesting for all targets, especially GPUs.
13140 if (N1.getOpcode() == ISD::FP_EXTEND) {
13141 SDValue N10 = N1.getOperand(0);
13142 if (N10.getOpcode() == PreferredFusedOpcode) {
13143 SDValue N102 = N10.getOperand(2);
13144 if (isContractableFMUL(N102) &&
13145 TLI.isFPExtFoldable(DAG, PreferredFusedOpcode, VT,
13146 N10.getValueType())) {
13147 return FoldFAddFPExtFMAFMul(N10.getOperand(0), N10.getOperand(1),
13148 N102.getOperand(0), N102.getOperand(1),
13149 N0);
13150 }
13151 }
13152 }
13153 }
13154
13155 return SDValue();
13156}
13157
13158/// Try to perform FMA combining on a given FSUB node.
13159SDValue DAGCombiner::visitFSUBForFMACombine(SDNode *N) {
13160 SDValue N0 = N->getOperand(0);
13161 SDValue N1 = N->getOperand(1);
13162 EVT VT = N->getValueType(0);
13163 SDLoc SL(N);
13164
13165 const TargetOptions &Options = DAG.getTarget().Options;
13166 // Floating-point multiply-add with intermediate rounding.
13167 bool HasFMAD = (LegalOperations && TLI.isFMADLegal(DAG, N));
13168
13169 // Floating-point multiply-add without intermediate rounding.
13170 bool HasFMA =
13171 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT) &&
13172 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT));
13173
13174 // No valid opcode, do not combine.
13175 if (!HasFMAD && !HasFMA)
13176 return SDValue();
13177
13178 const SDNodeFlags Flags = N->getFlags();
13179 bool AllowFusionGlobally = (Options.AllowFPOpFusion == FPOpFusion::Fast ||
13180 Options.UnsafeFPMath || HasFMAD);
13181
13182 // If the subtraction is not contractable, do not combine.
13183 if (!AllowFusionGlobally && !N->getFlags().hasAllowContract())
13184 return SDValue();
13185
13186 if (TLI.generateFMAsInMachineCombiner(VT, OptLevel))
13187 return SDValue();
13188
13189 // Always prefer FMAD to FMA for precision.
13190 unsigned PreferredFusedOpcode = HasFMAD ? ISD::FMAD : ISD::FMA;
13191 bool Aggressive = TLI.enableAggressiveFMAFusion(VT);
13192 bool NoSignedZero = Options.NoSignedZerosFPMath || Flags.hasNoSignedZeros();
13193
13194 // Is the node an FMUL and contractable either due to global flags or
13195 // SDNodeFlags.
13196 auto isContractableFMUL = [AllowFusionGlobally](SDValue N) {
13197 if (N.getOpcode() != ISD::FMUL)
13198 return false;
13199 return AllowFusionGlobally || N->getFlags().hasAllowContract();
13200 };
13201
13202 // fold (fsub (fmul x, y), z) -> (fma x, y, (fneg z))
13203 auto tryToFoldXYSubZ = [&](SDValue XY, SDValue Z) {
13204 if (isContractableFMUL(XY) && (Aggressive || XY->hasOneUse())) {
13205 return DAG.getNode(PreferredFusedOpcode, SL, VT, XY.getOperand(0),
13206 XY.getOperand(1), DAG.getNode(ISD::FNEG, SL, VT, Z));
13207 }
13208 return SDValue();
13209 };
13210
13211 // fold (fsub x, (fmul y, z)) -> (fma (fneg y), z, x)
13212 // Note: Commutes FSUB operands.
13213 auto tryToFoldXSubYZ = [&](SDValue X, SDValue YZ) {
13214 if (isContractableFMUL(YZ) && (Aggressive || YZ->hasOneUse())) {
13215 return DAG.getNode(PreferredFusedOpcode, SL, VT,
13216 DAG.getNode(ISD::FNEG, SL, VT, YZ.getOperand(0)),
13217 YZ.getOperand(1), X);
13218 }
13219 return SDValue();
13220 };
13221
13222 // If we have two choices trying to fold (fsub (fmul u, v), (fmul x, y)),
13223 // prefer to fold the multiply with fewer uses.
13224 if (isContractableFMUL(N0) && isContractableFMUL(N1) &&
13225 (N0.getNode()->use_size() > N1.getNode()->use_size())) {
13226 // fold (fsub (fmul a, b), (fmul c, d)) -> (fma (fneg c), d, (fmul a, b))
13227 if (SDValue V = tryToFoldXSubYZ(N0, N1))
13228 return V;
13229 // fold (fsub (fmul a, b), (fmul c, d)) -> (fma a, b, (fneg (fmul c, d)))
13230 if (SDValue V = tryToFoldXYSubZ(N0, N1))
13231 return V;
13232 } else {
13233 // fold (fsub (fmul x, y), z) -> (fma x, y, (fneg z))
13234 if (SDValue V = tryToFoldXYSubZ(N0, N1))
13235 return V;
13236 // fold (fsub x, (fmul y, z)) -> (fma (fneg y), z, x)
13237 if (SDValue V = tryToFoldXSubYZ(N0, N1))
13238 return V;
13239 }
13240
13241 // fold (fsub (fneg (fmul, x, y)), z) -> (fma (fneg x), y, (fneg z))
13242 if (N0.getOpcode() == ISD::FNEG && isContractableFMUL(N0.getOperand(0)) &&
13243 (Aggressive || (N0->hasOneUse() && N0.getOperand(0).hasOneUse()))) {
13244 SDValue N00 = N0.getOperand(0).getOperand(0);
13245 SDValue N01 = N0.getOperand(0).getOperand(1);
13246 return DAG.getNode(PreferredFusedOpcode, SL, VT,
13247 DAG.getNode(ISD::FNEG, SL, VT, N00), N01,
13248 DAG.getNode(ISD::FNEG, SL, VT, N1));
13249 }
13250
13251 // Look through FP_EXTEND nodes to do more combining.
13252
13253 // fold (fsub (fpext (fmul x, y)), z)
13254 // -> (fma (fpext x), (fpext y), (fneg z))
13255 if (N0.getOpcode() == ISD::FP_EXTEND) {
13256 SDValue N00 = N0.getOperand(0);
13257 if (isContractableFMUL(N00) &&
13258 TLI.isFPExtFoldable(DAG, PreferredFusedOpcode, VT,
13259 N00.getValueType())) {
13260 return DAG.getNode(PreferredFusedOpcode, SL, VT,
13261 DAG.getNode(ISD::FP_EXTEND, SL, VT, N00.getOperand(0)),
13262 DAG.getNode(ISD::FP_EXTEND, SL, VT, N00.getOperand(1)),
13263 DAG.getNode(ISD::FNEG, SL, VT, N1));
13264 }
13265 }
13266
13267 // fold (fsub x, (fpext (fmul y, z)))
13268 // -> (fma (fneg (fpext y)), (fpext z), x)
13269 // Note: Commutes FSUB operands.
13270 if (N1.getOpcode() == ISD::FP_EXTEND) {
13271 SDValue N10 = N1.getOperand(0);
13272 if (isContractableFMUL(N10) &&
13273 TLI.isFPExtFoldable(DAG, PreferredFusedOpcode, VT,
13274 N10.getValueType())) {
13275 return DAG.getNode(
13276 PreferredFusedOpcode, SL, VT,
13277 DAG.getNode(ISD::FNEG, SL, VT,
13278 DAG.getNode(ISD::FP_EXTEND, SL, VT, N10.getOperand(0))),
13279 DAG.getNode(ISD::FP_EXTEND, SL, VT, N10.getOperand(1)), N0);
13280 }
13281 }
13282
13283 // fold (fsub (fpext (fneg (fmul, x, y))), z)
13284 // -> (fneg (fma (fpext x), (fpext y), z))
13285 // Note: This could be removed with appropriate canonicalization of the
13286 // input expression into (fneg (fadd (fpext (fmul, x, y)), z). However, the
13287 // orthogonal flags -fp-contract=fast and -enable-unsafe-fp-math prevent
13288 // from implementing the canonicalization in visitFSUB.
13289 if (N0.getOpcode() == ISD::FP_EXTEND) {
13290 SDValue N00 = N0.getOperand(0);
13291 if (N00.getOpcode() == ISD::FNEG) {
13292 SDValue N000 = N00.getOperand(0);
13293 if (isContractableFMUL(N000) &&
13294 TLI.isFPExtFoldable(DAG, PreferredFusedOpcode, VT,
13295 N00.getValueType())) {
13296 return DAG.getNode(
13297 ISD::FNEG, SL, VT,
13298 DAG.getNode(PreferredFusedOpcode, SL, VT,
13299 DAG.getNode(ISD::FP_EXTEND, SL, VT, N000.getOperand(0)),
13300 DAG.getNode(ISD::FP_EXTEND, SL, VT, N000.getOperand(1)),
13301 N1));
13302 }
13303 }
13304 }
13305
13306 // fold (fsub (fneg (fpext (fmul, x, y))), z)
13307 // -> (fneg (fma (fpext x)), (fpext y), z)
13308 // Note: This could be removed with appropriate canonicalization of the
13309 // input expression into (fneg (fadd (fpext (fmul, x, y)), z). However, the
13310 // orthogonal flags -fp-contract=fast and -enable-unsafe-fp-math prevent
13311 // from implementing the canonicalization in visitFSUB.
13312 if (N0.getOpcode() == ISD::FNEG) {
13313 SDValue N00 = N0.getOperand(0);
13314 if (N00.getOpcode() == ISD::FP_EXTEND) {
13315 SDValue N000 = N00.getOperand(0);
13316 if (isContractableFMUL(N000) &&
13317 TLI.isFPExtFoldable(DAG, PreferredFusedOpcode, VT,
13318 N000.getValueType())) {
13319 return DAG.getNode(
13320 ISD::FNEG, SL, VT,
13321 DAG.getNode(PreferredFusedOpcode, SL, VT,
13322 DAG.getNode(ISD::FP_EXTEND, SL, VT, N000.getOperand(0)),
13323 DAG.getNode(ISD::FP_EXTEND, SL, VT, N000.getOperand(1)),
13324 N1));
13325 }
13326 }
13327 }
13328
13329 auto isReassociable = [Options](SDNode *N) {
13330 return Options.UnsafeFPMath || N->getFlags().hasAllowReassociation();
13331 };
13332
13333 auto isContractableAndReassociableFMUL = [isContractableFMUL,
13334 isReassociable](SDValue N) {
13335 return isContractableFMUL(N) && isReassociable(N.getNode());
13336 };
13337
13338 // More folding opportunities when target permits.
13339 if (Aggressive && isReassociable(N)) {
13340 bool CanFuse = Options.UnsafeFPMath || N->getFlags().hasAllowContract();
13341 // fold (fsub (fma x, y, (fmul u, v)), z)
13342 // -> (fma x, y (fma u, v, (fneg z)))
13343 if (CanFuse && N0.getOpcode() == PreferredFusedOpcode &&
13344 isContractableAndReassociableFMUL(N0.getOperand(2)) &&
13345 N0->hasOneUse() && N0.getOperand(2)->hasOneUse()) {
13346 return DAG.getNode(PreferredFusedOpcode, SL, VT, N0.getOperand(0),
13347 N0.getOperand(1),
13348 DAG.getNode(PreferredFusedOpcode, SL, VT,
13349 N0.getOperand(2).getOperand(0),
13350 N0.getOperand(2).getOperand(1),
13351 DAG.getNode(ISD::FNEG, SL, VT, N1)));
13352 }
13353
13354 // fold (fsub x, (fma y, z, (fmul u, v)))
13355 // -> (fma (fneg y), z, (fma (fneg u), v, x))
13356 if (CanFuse && N1.getOpcode() == PreferredFusedOpcode &&
13357 isContractableAndReassociableFMUL(N1.getOperand(2)) &&
13358 N1->hasOneUse() && NoSignedZero) {
13359 SDValue N20 = N1.getOperand(2).getOperand(0);
13360 SDValue N21 = N1.getOperand(2).getOperand(1);
13361 return DAG.getNode(
13362 PreferredFusedOpcode, SL, VT,
13363 DAG.getNode(ISD::FNEG, SL, VT, N1.getOperand(0)), N1.getOperand(1),
13364 DAG.getNode(PreferredFusedOpcode, SL, VT,
13365 DAG.getNode(ISD::FNEG, SL, VT, N20), N21, N0));
13366 }
13367
13368 // fold (fsub (fma x, y, (fpext (fmul u, v))), z)
13369 // -> (fma x, y (fma (fpext u), (fpext v), (fneg z)))
13370 if (N0.getOpcode() == PreferredFusedOpcode &&
13371 N0->hasOneUse()) {
13372 SDValue N02 = N0.getOperand(2);
13373 if (N02.getOpcode() == ISD::FP_EXTEND) {
13374 SDValue N020 = N02.getOperand(0);
13375 if (isContractableAndReassociableFMUL(N020) &&
13376 TLI.isFPExtFoldable(DAG, PreferredFusedOpcode, VT,
13377 N020.getValueType())) {
13378 return DAG.getNode(
13379 PreferredFusedOpcode, SL, VT, N0.getOperand(0), N0.getOperand(1),
13380 DAG.getNode(
13381 PreferredFusedOpcode, SL, VT,
13382 DAG.getNode(ISD::FP_EXTEND, SL, VT, N020.getOperand(0)),
13383 DAG.getNode(ISD::FP_EXTEND, SL, VT, N020.getOperand(1)),
13384 DAG.getNode(ISD::FNEG, SL, VT, N1)));
13385 }
13386 }
13387 }
13388
13389 // fold (fsub (fpext (fma x, y, (fmul u, v))), z)
13390 // -> (fma (fpext x), (fpext y),
13391 // (fma (fpext u), (fpext v), (fneg z)))
13392 // FIXME: This turns two single-precision and one double-precision
13393 // operation into two double-precision operations, which might not be
13394 // interesting for all targets, especially GPUs.
13395 if (N0.getOpcode() == ISD::FP_EXTEND) {
13396 SDValue N00 = N0.getOperand(0);
13397 if (N00.getOpcode() == PreferredFusedOpcode) {
13398 SDValue N002 = N00.getOperand(2);
13399 if (isContractableAndReassociableFMUL(N002) &&
13400 TLI.isFPExtFoldable(DAG, PreferredFusedOpcode, VT,
13401 N00.getValueType())) {
13402 return DAG.getNode(
13403 PreferredFusedOpcode, SL, VT,
13404 DAG.getNode(ISD::FP_EXTEND, SL, VT, N00.getOperand(0)),
13405 DAG.getNode(ISD::FP_EXTEND, SL, VT, N00.getOperand(1)),
13406 DAG.getNode(
13407 PreferredFusedOpcode, SL, VT,
13408 DAG.getNode(ISD::FP_EXTEND, SL, VT, N002.getOperand(0)),
13409 DAG.getNode(ISD::FP_EXTEND, SL, VT, N002.getOperand(1)),
13410 DAG.getNode(ISD::FNEG, SL, VT, N1)));
13411 }
13412 }
13413 }
13414
13415 // fold (fsub x, (fma y, z, (fpext (fmul u, v))))
13416 // -> (fma (fneg y), z, (fma (fneg (fpext u)), (fpext v), x))
13417 if (N1.getOpcode() == PreferredFusedOpcode &&
13418 N1.getOperand(2).getOpcode() == ISD::FP_EXTEND &&
13419 N1->hasOneUse()) {
13420 SDValue N120 = N1.getOperand(2).getOperand(0);
13421 if (isContractableAndReassociableFMUL(N120) &&
13422 TLI.isFPExtFoldable(DAG, PreferredFusedOpcode, VT,
13423 N120.getValueType())) {
13424 SDValue N1200 = N120.getOperand(0);
13425 SDValue N1201 = N120.getOperand(1);
13426 return DAG.getNode(
13427 PreferredFusedOpcode, SL, VT,
13428 DAG.getNode(ISD::FNEG, SL, VT, N1.getOperand(0)), N1.getOperand(1),
13429 DAG.getNode(PreferredFusedOpcode, SL, VT,
13430 DAG.getNode(ISD::FNEG, SL, VT,
13431 DAG.getNode(ISD::FP_EXTEND, SL, VT, N1200)),
13432 DAG.getNode(ISD::FP_EXTEND, SL, VT, N1201), N0));
13433 }
13434 }
13435
13436 // fold (fsub x, (fpext (fma y, z, (fmul u, v))))
13437 // -> (fma (fneg (fpext y)), (fpext z),
13438 // (fma (fneg (fpext u)), (fpext v), x))
13439 // FIXME: This turns two single-precision and one double-precision
13440 // operation into two double-precision operations, which might not be
13441 // interesting for all targets, especially GPUs.
13442 if (N1.getOpcode() == ISD::FP_EXTEND &&
13443 N1.getOperand(0).getOpcode() == PreferredFusedOpcode) {
13444 SDValue CvtSrc = N1.getOperand(0);
13445 SDValue N100 = CvtSrc.getOperand(0);
13446 SDValue N101 = CvtSrc.getOperand(1);
13447 SDValue N102 = CvtSrc.getOperand(2);
13448 if (isContractableAndReassociableFMUL(N102) &&
13449 TLI.isFPExtFoldable(DAG, PreferredFusedOpcode, VT,
13450 CvtSrc.getValueType())) {
13451 SDValue N1020 = N102.getOperand(0);
13452 SDValue N1021 = N102.getOperand(1);
13453 return DAG.getNode(
13454 PreferredFusedOpcode, SL, VT,
13455 DAG.getNode(ISD::FNEG, SL, VT,
13456 DAG.getNode(ISD::FP_EXTEND, SL, VT, N100)),
13457 DAG.getNode(ISD::FP_EXTEND, SL, VT, N101),
13458 DAG.getNode(PreferredFusedOpcode, SL, VT,
13459 DAG.getNode(ISD::FNEG, SL, VT,
13460 DAG.getNode(ISD::FP_EXTEND, SL, VT, N1020)),
13461 DAG.getNode(ISD::FP_EXTEND, SL, VT, N1021), N0));
13462 }
13463 }
13464 }
13465
13466 return SDValue();
13467}
13468
13469/// Try to perform FMA combining on a given FMUL node based on the distributive
13470/// law x * (y + 1) = x * y + x and variants thereof (commuted versions,
13471/// subtraction instead of addition).
13472SDValue DAGCombiner::visitFMULForFMADistributiveCombine(SDNode *N) {
13473 SDValue N0 = N->getOperand(0);
13474 SDValue N1 = N->getOperand(1);
13475 EVT VT = N->getValueType(0);
13476 SDLoc SL(N);
13477
13478 assert(N->getOpcode() == ISD::FMUL && "Expected FMUL Operation")(static_cast <bool> (N->getOpcode() == ISD::FMUL &&
"Expected FMUL Operation") ? void (0) : __assert_fail ("N->getOpcode() == ISD::FMUL && \"Expected FMUL Operation\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 13478, __extension__ __PRETTY_FUNCTION__))
;
13479
13480 const TargetOptions &Options = DAG.getTarget().Options;
13481
13482 // The transforms below are incorrect when x == 0 and y == inf, because the
13483 // intermediate multiplication produces a nan.
13484 if (!Options.NoInfsFPMath)
13485 return SDValue();
13486
13487 // Floating-point multiply-add without intermediate rounding.
13488 bool HasFMA =
13489 (Options.AllowFPOpFusion == FPOpFusion::Fast || Options.UnsafeFPMath) &&
13490 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT) &&
13491 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT));
13492
13493 // Floating-point multiply-add with intermediate rounding. This can result
13494 // in a less precise result due to the changed rounding order.
13495 bool HasFMAD = Options.UnsafeFPMath &&
13496 (LegalOperations && TLI.isFMADLegal(DAG, N));
13497
13498 // No valid opcode, do not combine.
13499 if (!HasFMAD && !HasFMA)
13500 return SDValue();
13501
13502 // Always prefer FMAD to FMA for precision.
13503 unsigned PreferredFusedOpcode = HasFMAD ? ISD::FMAD : ISD::FMA;
13504 bool Aggressive = TLI.enableAggressiveFMAFusion(VT);
13505
13506 // fold (fmul (fadd x0, +1.0), y) -> (fma x0, y, y)
13507 // fold (fmul (fadd x0, -1.0), y) -> (fma x0, y, (fneg y))
13508 auto FuseFADD = [&](SDValue X, SDValue Y) {
13509 if (X.getOpcode() == ISD::FADD && (Aggressive || X->hasOneUse())) {
13510 if (auto *C = isConstOrConstSplatFP(X.getOperand(1), true)) {
13511 if (C->isExactlyValue(+1.0))
13512 return DAG.getNode(PreferredFusedOpcode, SL, VT, X.getOperand(0), Y,
13513 Y);
13514 if (C->isExactlyValue(-1.0))
13515 return DAG.getNode(PreferredFusedOpcode, SL, VT, X.getOperand(0), Y,
13516 DAG.getNode(ISD::FNEG, SL, VT, Y));
13517 }
13518 }
13519 return SDValue();
13520 };
13521
13522 if (SDValue FMA = FuseFADD(N0, N1))
13523 return FMA;
13524 if (SDValue FMA = FuseFADD(N1, N0))
13525 return FMA;
13526
13527 // fold (fmul (fsub +1.0, x1), y) -> (fma (fneg x1), y, y)
13528 // fold (fmul (fsub -1.0, x1), y) -> (fma (fneg x1), y, (fneg y))
13529 // fold (fmul (fsub x0, +1.0), y) -> (fma x0, y, (fneg y))
13530 // fold (fmul (fsub x0, -1.0), y) -> (fma x0, y, y)
13531 auto FuseFSUB = [&](SDValue X, SDValue Y) {
13532 if (X.getOpcode() == ISD::FSUB && (Aggressive || X->hasOneUse())) {
13533 if (auto *C0 = isConstOrConstSplatFP(X.getOperand(0), true)) {
13534 if (C0->isExactlyValue(+1.0))
13535 return DAG.getNode(PreferredFusedOpcode, SL, VT,
13536 DAG.getNode(ISD::FNEG, SL, VT, X.getOperand(1)), Y,
13537 Y);
13538 if (C0->isExactlyValue(-1.0))
13539 return DAG.getNode(PreferredFusedOpcode, SL, VT,
13540 DAG.getNode(ISD::FNEG, SL, VT, X.getOperand(1)), Y,
13541 DAG.getNode(ISD::FNEG, SL, VT, Y));
13542 }
13543 if (auto *C1 = isConstOrConstSplatFP(X.getOperand(1), true)) {
13544 if (C1->isExactlyValue(+1.0))
13545 return DAG.getNode(PreferredFusedOpcode, SL, VT, X.getOperand(0), Y,
13546 DAG.getNode(ISD::FNEG, SL, VT, Y));
13547 if (C1->isExactlyValue(-1.0))
13548 return DAG.getNode(PreferredFusedOpcode, SL, VT, X.getOperand(0), Y,
13549 Y);
13550 }
13551 }
13552 return SDValue();
13553 };
13554
13555 if (SDValue FMA = FuseFSUB(N0, N1))
13556 return FMA;
13557 if (SDValue FMA = FuseFSUB(N1, N0))
13558 return FMA;
13559
13560 return SDValue();
13561}
13562
13563SDValue DAGCombiner::visitFADD(SDNode *N) {
13564 SDValue N0 = N->getOperand(0);
13565 SDValue N1 = N->getOperand(1);
13566 bool N0CFP = DAG.isConstantFPBuildVectorOrConstantFP(N0);
13567 bool N1CFP = DAG.isConstantFPBuildVectorOrConstantFP(N1);
13568 EVT VT = N->getValueType(0);
13569 SDLoc DL(N);
13570 const TargetOptions &Options = DAG.getTarget().Options;
13571 SDNodeFlags Flags = N->getFlags();
13572 SelectionDAG::FlagInserter FlagsInserter(DAG, N);
13573
13574 if (SDValue R = DAG.simplifyFPBinop(N->getOpcode(), N0, N1, Flags))
13575 return R;
13576
13577 // fold vector ops
13578 if (VT.isVector())
13579 if (SDValue FoldedVOp = SimplifyVBinOp(N))
13580 return FoldedVOp;
13581
13582 // fold (fadd c1, c2) -> c1 + c2
13583 if (N0CFP && N1CFP)
13584 return DAG.getNode(ISD::FADD, DL, VT, N0, N1);
13585
13586 // canonicalize constant to RHS
13587 if (N0CFP && !N1CFP)
13588 return DAG.getNode(ISD::FADD, DL, VT, N1, N0);
13589
13590 // N0 + -0.0 --> N0 (also allowed with +0.0 and fast-math)
13591 ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, true);
13592 if (N1C && N1C->isZero())
13593 if (N1C->isNegative() || Options.NoSignedZerosFPMath || Flags.hasNoSignedZeros())
13594 return N0;
13595
13596 if (SDValue NewSel = foldBinOpIntoSelect(N))
13597 return NewSel;
13598
13599 // fold (fadd A, (fneg B)) -> (fsub A, B)
13600 if (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT))
13601 if (SDValue NegN1 = TLI.getCheaperNegatedExpression(
13602 N1, DAG, LegalOperations, ForCodeSize))
13603 return DAG.getNode(ISD::FSUB, DL, VT, N0, NegN1);
13604
13605 // fold (fadd (fneg A), B) -> (fsub B, A)
13606 if (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT))
13607 if (SDValue NegN0 = TLI.getCheaperNegatedExpression(
13608 N0, DAG, LegalOperations, ForCodeSize))
13609 return DAG.getNode(ISD::FSUB, DL, VT, N1, NegN0);
13610
13611 auto isFMulNegTwo = [](SDValue FMul) {
13612 if (!FMul.hasOneUse() || FMul.getOpcode() != ISD::FMUL)
13613 return false;
13614 auto *C = isConstOrConstSplatFP(FMul.getOperand(1), true);
13615 return C && C->isExactlyValue(-2.0);
13616 };
13617
13618 // fadd (fmul B, -2.0), A --> fsub A, (fadd B, B)
13619 if (isFMulNegTwo(N0)) {
13620 SDValue B = N0.getOperand(0);
13621 SDValue Add = DAG.getNode(ISD::FADD, DL, VT, B, B);
13622 return DAG.getNode(ISD::FSUB, DL, VT, N1, Add);
13623 }
13624 // fadd A, (fmul B, -2.0) --> fsub A, (fadd B, B)
13625 if (isFMulNegTwo(N1)) {
13626 SDValue B = N1.getOperand(0);
13627 SDValue Add = DAG.getNode(ISD::FADD, DL, VT, B, B);
13628 return DAG.getNode(ISD::FSUB, DL, VT, N0, Add);
13629 }
13630
13631 // No FP constant should be created after legalization as Instruction
13632 // Selection pass has a hard time dealing with FP constants.
13633 bool AllowNewConst = (Level < AfterLegalizeDAG);
13634
13635 // If nnan is enabled, fold lots of things.
13636 if ((Options.NoNaNsFPMath || Flags.hasNoNaNs()) && AllowNewConst) {
13637 // If allowed, fold (fadd (fneg x), x) -> 0.0
13638 if (N0.getOpcode() == ISD::FNEG && N0.getOperand(0) == N1)
13639 return DAG.getConstantFP(0.0, DL, VT);
13640
13641 // If allowed, fold (fadd x, (fneg x)) -> 0.0
13642 if (N1.getOpcode() == ISD::FNEG && N1.getOperand(0) == N0)
13643 return DAG.getConstantFP(0.0, DL, VT);
13644 }
13645
13646 // If 'unsafe math' or reassoc and nsz, fold lots of things.
13647 // TODO: break out portions of the transformations below for which Unsafe is
13648 // considered and which do not require both nsz and reassoc
13649 if (((Options.UnsafeFPMath && Options.NoSignedZerosFPMath) ||
13650 (Flags.hasAllowReassociation() && Flags.hasNoSignedZeros())) &&
13651 AllowNewConst) {
13652 // fadd (fadd x, c1), c2 -> fadd x, c1 + c2
13653 if (N1CFP && N0.getOpcode() == ISD::FADD &&
13654 DAG.isConstantFPBuildVectorOrConstantFP(N0.getOperand(1))) {
13655 SDValue NewC = DAG.getNode(ISD::FADD, DL, VT, N0.getOperand(1), N1);
13656 return DAG.getNode(ISD::FADD, DL, VT, N0.getOperand(0), NewC);
13657 }
13658
13659 // We can fold chains of FADD's of the same value into multiplications.
13660 // This transform is not safe in general because we are reducing the number
13661 // of rounding steps.
13662 if (TLI.isOperationLegalOrCustom(ISD::FMUL, VT) && !N0CFP && !N1CFP) {
13663 if (N0.getOpcode() == ISD::FMUL) {
13664 bool CFP00 = DAG.isConstantFPBuildVectorOrConstantFP(N0.getOperand(0));
13665 bool CFP01 = DAG.isConstantFPBuildVectorOrConstantFP(N0.getOperand(1));
13666
13667 // (fadd (fmul x, c), x) -> (fmul x, c+1)
13668 if (CFP01 && !CFP00 && N0.getOperand(0) == N1) {
13669 SDValue NewCFP = DAG.getNode(ISD::FADD, DL, VT, N0.getOperand(1),
13670 DAG.getConstantFP(1.0, DL, VT));
13671 return DAG.getNode(ISD::FMUL, DL, VT, N1, NewCFP);
13672 }
13673
13674 // (fadd (fmul x, c), (fadd x, x)) -> (fmul x, c+2)
13675 if (CFP01 && !CFP00 && N1.getOpcode() == ISD::FADD &&
13676 N1.getOperand(0) == N1.getOperand(1) &&
13677 N0.getOperand(0) == N1.getOperand(0)) {
13678 SDValue NewCFP = DAG.getNode(ISD::FADD, DL, VT, N0.getOperand(1),
13679 DAG.getConstantFP(2.0, DL, VT));
13680 return DAG.getNode(ISD::FMUL, DL, VT, N0.getOperand(0), NewCFP);
13681 }
13682 }
13683
13684 if (N1.getOpcode() == ISD::FMUL) {
13685 bool CFP10 = DAG.isConstantFPBuildVectorOrConstantFP(N1.getOperand(0));
13686 bool CFP11 = DAG.isConstantFPBuildVectorOrConstantFP(N1.getOperand(1));
13687
13688 // (fadd x, (fmul x, c)) -> (fmul x, c+1)
13689 if (CFP11 && !CFP10 && N1.getOperand(0) == N0) {
13690 SDValue NewCFP = DAG.getNode(ISD::FADD, DL, VT, N1.getOperand(1),
13691 DAG.getConstantFP(1.0, DL, VT));
13692 return DAG.getNode(ISD::FMUL, DL, VT, N0, NewCFP);
13693 }
13694
13695 // (fadd (fadd x, x), (fmul x, c)) -> (fmul x, c+2)
13696 if (CFP11 && !CFP10 && N0.getOpcode() == ISD::FADD &&
13697 N0.getOperand(0) == N0.getOperand(1) &&
13698 N1.getOperand(0) == N0.getOperand(0)) {
13699 SDValue NewCFP = DAG.getNode(ISD::FADD, DL, VT, N1.getOperand(1),
13700 DAG.getConstantFP(2.0, DL, VT));
13701 return DAG.getNode(ISD::FMUL, DL, VT, N1.getOperand(0), NewCFP);
13702 }
13703 }
13704
13705 if (N0.getOpcode() == ISD::FADD) {
13706 bool CFP00 = DAG.isConstantFPBuildVectorOrConstantFP(N0.getOperand(0));
13707 // (fadd (fadd x, x), x) -> (fmul x, 3.0)
13708 if (!CFP00 && N0.getOperand(0) == N0.getOperand(1) &&
13709 (N0.getOperand(0) == N1)) {
13710 return DAG.getNode(ISD::FMUL, DL, VT, N1,
13711 DAG.getConstantFP(3.0, DL, VT));
13712 }
13713 }
13714
13715 if (N1.getOpcode() == ISD::FADD) {
13716 bool CFP10 = DAG.isConstantFPBuildVectorOrConstantFP(N1.getOperand(0));
13717 // (fadd x, (fadd x, x)) -> (fmul x, 3.0)
13718 if (!CFP10 && N1.getOperand(0) == N1.getOperand(1) &&
13719 N1.getOperand(0) == N0) {
13720 return DAG.getNode(ISD::FMUL, DL, VT, N0,
13721 DAG.getConstantFP(3.0, DL, VT));
13722 }
13723 }
13724
13725 // (fadd (fadd x, x), (fadd x, x)) -> (fmul x, 4.0)
13726 if (N0.getOpcode() == ISD::FADD && N1.getOpcode() == ISD::FADD &&
13727 N0.getOperand(0) == N0.getOperand(1) &&
13728 N1.getOperand(0) == N1.getOperand(1) &&
13729 N0.getOperand(0) == N1.getOperand(0)) {
13730 return DAG.getNode(ISD::FMUL, DL, VT, N0.getOperand(0),
13731 DAG.getConstantFP(4.0, DL, VT));
13732 }
13733 }
13734 } // enable-unsafe-fp-math
13735
13736 // FADD -> FMA combines:
13737 if (SDValue Fused = visitFADDForFMACombine(N)) {
13738 AddToWorklist(Fused.getNode());
13739 return Fused;
13740 }
13741 return SDValue();
13742}
13743
13744SDValue DAGCombiner::visitSTRICT_FADD(SDNode *N) {
13745 SDValue Chain = N->getOperand(0);
13746 SDValue N0 = N->getOperand(1);
13747 SDValue N1 = N->getOperand(2);
13748 EVT VT = N->getValueType(0);
13749 EVT ChainVT = N->getValueType(1);
13750 SDLoc DL(N);
13751 SelectionDAG::FlagInserter FlagsInserter(DAG, N);
13752
13753 // fold (strict_fadd A, (fneg B)) -> (strict_fsub A, B)
13754 if (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::STRICT_FSUB, VT))
13755 if (SDValue NegN1 = TLI.getCheaperNegatedExpression(
13756 N1, DAG, LegalOperations, ForCodeSize)) {
13757 return DAG.getNode(ISD::STRICT_FSUB, DL, DAG.getVTList(VT, ChainVT),
13758 {Chain, N0, NegN1});
13759 }
13760
13761 // fold (strict_fadd (fneg A), B) -> (strict_fsub B, A)
13762 if (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::STRICT_FSUB, VT))
13763 if (SDValue NegN0 = TLI.getCheaperNegatedExpression(
13764 N0, DAG, LegalOperations, ForCodeSize)) {
13765 return DAG.getNode(ISD::STRICT_FSUB, DL, DAG.getVTList(VT, ChainVT),
13766 {Chain, N1, NegN0});
13767 }
13768 return SDValue();
13769}
13770
13771SDValue DAGCombiner::visitFSUB(SDNode *N) {
13772 SDValue N0 = N->getOperand(0);
13773 SDValue N1 = N->getOperand(1);
13774 ConstantFPSDNode *N0CFP = isConstOrConstSplatFP(N0, true);
13775 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, true);
13776 EVT VT = N->getValueType(0);
13777 SDLoc DL(N);
13778 const TargetOptions &Options = DAG.getTarget().Options;
13779 const SDNodeFlags Flags = N->getFlags();
13780 SelectionDAG::FlagInserter FlagsInserter(DAG, N);
13781
13782 if (SDValue R = DAG.simplifyFPBinop(N->getOpcode(), N0, N1, Flags))
13783 return R;
13784
13785 // fold vector ops
13786 if (VT.isVector())
13787 if (SDValue FoldedVOp = SimplifyVBinOp(N))
13788 return FoldedVOp;
13789
13790 // fold (fsub c1, c2) -> c1-c2
13791 if (N0CFP && N1CFP)
13792 return DAG.getNode(ISD::FSUB, DL, VT, N0, N1);
13793
13794 if (SDValue NewSel = foldBinOpIntoSelect(N))
13795 return NewSel;
13796
13797 // (fsub A, 0) -> A
13798 if (N1CFP && N1CFP->isZero()) {
13799 if (!N1CFP->isNegative() || Options.NoSignedZerosFPMath ||
13800 Flags.hasNoSignedZeros()) {
13801 return N0;
13802 }
13803 }
13804
13805 if (N0 == N1) {
13806 // (fsub x, x) -> 0.0
13807 if (Options.NoNaNsFPMath || Flags.hasNoNaNs())
13808 return DAG.getConstantFP(0.0f, DL, VT);
13809 }
13810
13811 // (fsub -0.0, N1) -> -N1
13812 if (N0CFP && N0CFP->isZero()) {
13813 if (N0CFP->isNegative() ||
13814 (Options.NoSignedZerosFPMath || Flags.hasNoSignedZeros())) {
13815 // We cannot replace an FSUB(+-0.0,X) with FNEG(X) when denormals are
13816 // flushed to zero, unless all users treat denorms as zero (DAZ).
13817 // FIXME: This transform will change the sign of a NaN and the behavior
13818 // of a signaling NaN. It is only valid when a NoNaN flag is present.
13819 DenormalMode DenormMode = DAG.getDenormalMode(VT);
13820 if (DenormMode == DenormalMode::getIEEE()) {
13821 if (SDValue NegN1 =
13822 TLI.getNegatedExpression(N1, DAG, LegalOperations, ForCodeSize))
13823 return NegN1;
13824 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
13825 return DAG.getNode(ISD::FNEG, DL, VT, N1);
13826 }
13827 }
13828 }
13829
13830 if (((Options.UnsafeFPMath && Options.NoSignedZerosFPMath) ||
13831 (Flags.hasAllowReassociation() && Flags.hasNoSignedZeros())) &&
13832 N1.getOpcode() == ISD::FADD) {
13833 // X - (X + Y) -> -Y
13834 if (N0 == N1->getOperand(0))
13835 return DAG.getNode(ISD::FNEG, DL, VT, N1->getOperand(1));
13836 // X - (Y + X) -> -Y
13837 if (N0 == N1->getOperand(1))
13838 return DAG.getNode(ISD::FNEG, DL, VT, N1->getOperand(0));
13839 }
13840
13841 // fold (fsub A, (fneg B)) -> (fadd A, B)
13842 if (SDValue NegN1 =
13843 TLI.getNegatedExpression(N1, DAG, LegalOperations, ForCodeSize))
13844 return DAG.getNode(ISD::FADD, DL, VT, N0, NegN1);
13845
13846 // FSUB -> FMA combines:
13847 if (SDValue Fused = visitFSUBForFMACombine(N)) {
13848 AddToWorklist(Fused.getNode());
13849 return Fused;
13850 }
13851
13852 return SDValue();
13853}
13854
13855SDValue DAGCombiner::visitFMUL(SDNode *N) {
13856 SDValue N0 = N->getOperand(0);
13857 SDValue N1 = N->getOperand(1);
13858 ConstantFPSDNode *N0CFP = isConstOrConstSplatFP(N0, true);
13859 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, true);
13860 EVT VT = N->getValueType(0);
13861 SDLoc DL(N);
13862 const TargetOptions &Options = DAG.getTarget().Options;
13863 const SDNodeFlags Flags = N->getFlags();
13864 SelectionDAG::FlagInserter FlagsInserter(DAG, N);
13865
13866 if (SDValue R = DAG.simplifyFPBinop(N->getOpcode(), N0, N1, Flags))
13867 return R;
13868
13869 // fold vector ops
13870 if (VT.isVector()) {
13871 // This just handles C1 * C2 for vectors. Other vector folds are below.
13872 if (SDValue FoldedVOp = SimplifyVBinOp(N))
13873 return FoldedVOp;
13874 }
13875
13876 // fold (fmul c1, c2) -> c1*c2
13877 if (N0CFP && N1CFP)
13878 return DAG.getNode(ISD::FMUL, DL, VT, N0, N1);
13879
13880 // canonicalize constant to RHS
13881 if (DAG.isConstantFPBuildVectorOrConstantFP(N0) &&
13882 !DAG.isConstantFPBuildVectorOrConstantFP(N1))
13883 return DAG.getNode(ISD::FMUL, DL, VT, N1, N0);
13884
13885 if (SDValue NewSel = foldBinOpIntoSelect(N))
13886 return NewSel;
13887
13888 if (Options.UnsafeFPMath || Flags.hasAllowReassociation()) {
13889 // fmul (fmul X, C1), C2 -> fmul X, C1 * C2
13890 if (DAG.isConstantFPBuildVectorOrConstantFP(N1) &&
13891 N0.getOpcode() == ISD::FMUL) {
13892 SDValue N00 = N0.getOperand(0);
13893 SDValue N01 = N0.getOperand(1);
13894 // Avoid an infinite loop by making sure that N00 is not a constant
13895 // (the inner multiply has not been constant folded yet).
13896 if (DAG.isConstantFPBuildVectorOrConstantFP(N01) &&
13897 !DAG.isConstantFPBuildVectorOrConstantFP(N00)) {
13898 SDValue MulConsts = DAG.getNode(ISD::FMUL, DL, VT, N01, N1);
13899 return DAG.getNode(ISD::FMUL, DL, VT, N00, MulConsts);
13900 }
13901 }
13902
13903 // Match a special-case: we convert X * 2.0 into fadd.
13904 // fmul (fadd X, X), C -> fmul X, 2.0 * C
13905 if (N0.getOpcode() == ISD::FADD && N0.hasOneUse() &&
13906 N0.getOperand(0) == N0.getOperand(1)) {
13907 const SDValue Two = DAG.getConstantFP(2.0, DL, VT);
13908 SDValue MulConsts = DAG.getNode(ISD::FMUL, DL, VT, Two, N1);
13909 return DAG.getNode(ISD::FMUL, DL, VT, N0.getOperand(0), MulConsts);
13910 }
13911 }
13912
13913 // fold (fmul X, 2.0) -> (fadd X, X)
13914 if (N1CFP && N1CFP->isExactlyValue(+2.0))
13915 return DAG.getNode(ISD::FADD, DL, VT, N0, N0);
13916
13917 // fold (fmul X, -1.0) -> (fneg X)
13918 if (N1CFP && N1CFP->isExactlyValue(-1.0))
13919 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
13920 return DAG.getNode(ISD::FNEG, DL, VT, N0);
13921
13922 // -N0 * -N1 --> N0 * N1
13923 TargetLowering::NegatibleCost CostN0 =
13924 TargetLowering::NegatibleCost::Expensive;
13925 TargetLowering::NegatibleCost CostN1 =
13926 TargetLowering::NegatibleCost::Expensive;
13927 SDValue NegN0 =
13928 TLI.getNegatedExpression(N0, DAG, LegalOperations, ForCodeSize, CostN0);
13929 SDValue NegN1 =
13930 TLI.getNegatedExpression(N1, DAG, LegalOperations, ForCodeSize, CostN1);
13931 if (NegN0 && NegN1 &&
13932 (CostN0 == TargetLowering::NegatibleCost::Cheaper ||
13933 CostN1 == TargetLowering::NegatibleCost::Cheaper))
13934 return DAG.getNode(ISD::FMUL, DL, VT, NegN0, NegN1);
13935
13936 // fold (fmul X, (select (fcmp X > 0.0), -1.0, 1.0)) -> (fneg (fabs X))
13937 // fold (fmul X, (select (fcmp X > 0.0), 1.0, -1.0)) -> (fabs X)
13938 if (Flags.hasNoNaNs() && Flags.hasNoSignedZeros() &&
13939 (N0.getOpcode() == ISD::SELECT || N1.getOpcode() == ISD::SELECT) &&
13940 TLI.isOperationLegal(ISD::FABS, VT)) {
13941 SDValue Select = N0, X = N1;
13942 if (Select.getOpcode() != ISD::SELECT)
13943 std::swap(Select, X);
13944
13945 SDValue Cond = Select.getOperand(0);
13946 auto TrueOpnd = dyn_cast<ConstantFPSDNode>(Select.getOperand(1));
13947 auto FalseOpnd = dyn_cast<ConstantFPSDNode>(Select.getOperand(2));
13948
13949 if (TrueOpnd && FalseOpnd &&
13950 Cond.getOpcode() == ISD::SETCC && Cond.getOperand(0) == X &&
13951 isa<ConstantFPSDNode>(Cond.getOperand(1)) &&
13952 cast<ConstantFPSDNode>(Cond.getOperand(1))->isExactlyValue(0.0)) {
13953 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
13954 switch (CC) {
13955 default: break;
13956 case ISD::SETOLT:
13957 case ISD::SETULT:
13958 case ISD::SETOLE:
13959 case ISD::SETULE:
13960 case ISD::SETLT:
13961 case ISD::SETLE:
13962 std::swap(TrueOpnd, FalseOpnd);
13963 LLVM_FALLTHROUGH[[gnu::fallthrough]];
13964 case ISD::SETOGT:
13965 case ISD::SETUGT:
13966 case ISD::SETOGE:
13967 case ISD::SETUGE:
13968 case ISD::SETGT:
13969 case ISD::SETGE:
13970 if (TrueOpnd->isExactlyValue(-1.0) && FalseOpnd->isExactlyValue(1.0) &&
13971 TLI.isOperationLegal(ISD::FNEG, VT))
13972 return DAG.getNode(ISD::FNEG, DL, VT,
13973 DAG.getNode(ISD::FABS, DL, VT, X));
13974 if (TrueOpnd->isExactlyValue(1.0) && FalseOpnd->isExactlyValue(-1.0))
13975 return DAG.getNode(ISD::FABS, DL, VT, X);
13976
13977 break;
13978 }
13979 }
13980 }
13981
13982 // FMUL -> FMA combines:
13983 if (SDValue Fused = visitFMULForFMADistributiveCombine(N)) {
13984 AddToWorklist(Fused.getNode());
13985 return Fused;
13986 }
13987
13988 return SDValue();
13989}
13990
13991SDValue DAGCombiner::visitFMA(SDNode *N) {
13992 SDValue N0 = N->getOperand(0);
13993 SDValue N1 = N->getOperand(1);
13994 SDValue N2 = N->getOperand(2);
13995 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
13996 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
13997 EVT VT = N->getValueType(0);
13998 SDLoc DL(N);
13999 const TargetOptions &Options = DAG.getTarget().Options;
14000 // FMA nodes have flags that propagate to the created nodes.
14001 SelectionDAG::FlagInserter FlagsInserter(DAG, N);
14002
14003 bool UnsafeFPMath =
14004 Options.UnsafeFPMath || N->getFlags().hasAllowReassociation();
14005
14006 // Constant fold FMA.
14007 if (isa<ConstantFPSDNode>(N0) &&
14008 isa<ConstantFPSDNode>(N1) &&
14009 isa<ConstantFPSDNode>(N2)) {
14010 return DAG.getNode(ISD::FMA, DL, VT, N0, N1, N2);
14011 }
14012
14013 // (-N0 * -N1) + N2 --> (N0 * N1) + N2
14014 TargetLowering::NegatibleCost CostN0 =
14015 TargetLowering::NegatibleCost::Expensive;
14016 TargetLowering::NegatibleCost CostN1 =
14017 TargetLowering::NegatibleCost::Expensive;
14018 SDValue NegN0 =
14019 TLI.getNegatedExpression(N0, DAG, LegalOperations, ForCodeSize, CostN0);
14020 SDValue NegN1 =
14021 TLI.getNegatedExpression(N1, DAG, LegalOperations, ForCodeSize, CostN1);
14022 if (NegN0 && NegN1 &&
14023 (CostN0 == TargetLowering::NegatibleCost::Cheaper ||
14024 CostN1 == TargetLowering::NegatibleCost::Cheaper))
14025 return DAG.getNode(ISD::FMA, DL, VT, NegN0, NegN1, N2);
14026
14027 if (UnsafeFPMath) {
14028 if (N0CFP && N0CFP->isZero())
14029 return N2;
14030 if (N1CFP && N1CFP->isZero())
14031 return N2;
14032 }
14033
14034 if (N0CFP && N0CFP->isExactlyValue(1.0))
14035 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N2);
14036 if (N1CFP && N1CFP->isExactlyValue(1.0))
14037 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N2);
14038
14039 // Canonicalize (fma c, x, y) -> (fma x, c, y)
14040 if (DAG.isConstantFPBuildVectorOrConstantFP(N0) &&
14041 !DAG.isConstantFPBuildVectorOrConstantFP(N1))
14042 return DAG.getNode(ISD::FMA, SDLoc(N), VT, N1, N0, N2);
14043
14044 if (UnsafeFPMath) {
14045 // (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2)
14046 if (N2.getOpcode() == ISD::FMUL && N0 == N2.getOperand(0) &&
14047 DAG.isConstantFPBuildVectorOrConstantFP(N1) &&
14048 DAG.isConstantFPBuildVectorOrConstantFP(N2.getOperand(1))) {
14049 return DAG.getNode(ISD::FMUL, DL, VT, N0,
14050 DAG.getNode(ISD::FADD, DL, VT, N1, N2.getOperand(1)));
14051 }
14052
14053 // (fma (fmul x, c1), c2, y) -> (fma x, c1*c2, y)
14054 if (N0.getOpcode() == ISD::FMUL &&
14055 DAG.isConstantFPBuildVectorOrConstantFP(N1) &&
14056 DAG.isConstantFPBuildVectorOrConstantFP(N0.getOperand(1))) {
14057 return DAG.getNode(ISD::FMA, DL, VT, N0.getOperand(0),
14058 DAG.getNode(ISD::FMUL, DL, VT, N1, N0.getOperand(1)),
14059 N2);
14060 }
14061 }
14062
14063 // (fma x, -1, y) -> (fadd (fneg x), y)
14064 if (N1CFP) {
14065 if (N1CFP->isExactlyValue(1.0))
14066 return DAG.getNode(ISD::FADD, DL, VT, N0, N2);
14067
14068 if (N1CFP->isExactlyValue(-1.0) &&
14069 (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))) {
14070 SDValue RHSNeg = DAG.getNode(ISD::FNEG, DL, VT, N0);
14071 AddToWorklist(RHSNeg.getNode());
14072 return DAG.getNode(ISD::FADD, DL, VT, N2, RHSNeg);
14073 }
14074
14075 // fma (fneg x), K, y -> fma x -K, y
14076 if (N0.getOpcode() == ISD::FNEG &&
14077 (TLI.isOperationLegal(ISD::ConstantFP, VT) ||
14078 (N1.hasOneUse() && !TLI.isFPImmLegal(N1CFP->getValueAPF(), VT,
14079 ForCodeSize)))) {
14080 return DAG.getNode(ISD::FMA, DL, VT, N0.getOperand(0),
14081 DAG.getNode(ISD::FNEG, DL, VT, N1), N2);
14082 }
14083 }
14084
14085 if (UnsafeFPMath) {
14086 // (fma x, c, x) -> (fmul x, (c+1))
14087 if (N1CFP && N0 == N2) {
14088 return DAG.getNode(
14089 ISD::FMUL, DL, VT, N0,
14090 DAG.getNode(ISD::FADD, DL, VT, N1, DAG.getConstantFP(1.0, DL, VT)));
14091 }
14092
14093 // (fma x, c, (fneg x)) -> (fmul x, (c-1))
14094 if (N1CFP && N2.getOpcode() == ISD::FNEG && N2.getOperand(0) == N0) {
14095 return DAG.getNode(
14096 ISD::FMUL, DL, VT, N0,
14097 DAG.getNode(ISD::FADD, DL, VT, N1, DAG.getConstantFP(-1.0, DL, VT)));
14098 }
14099 }
14100
14101 // fold ((fma (fneg X), Y, (fneg Z)) -> fneg (fma X, Y, Z))
14102 // fold ((fma X, (fneg Y), (fneg Z)) -> fneg (fma X, Y, Z))
14103 if (!TLI.isFNegFree(VT))
14104 if (SDValue Neg = TLI.getCheaperNegatedExpression(
14105 SDValue(N, 0), DAG, LegalOperations, ForCodeSize))
14106 return DAG.getNode(ISD::FNEG, DL, VT, Neg);
14107 return SDValue();
14108}
14109
14110// Combine multiple FDIVs with the same divisor into multiple FMULs by the
14111// reciprocal.
14112// E.g., (a / D; b / D;) -> (recip = 1.0 / D; a * recip; b * recip)
14113// Notice that this is not always beneficial. One reason is different targets
14114// may have different costs for FDIV and FMUL, so sometimes the cost of two
14115// FDIVs may be lower than the cost of one FDIV and two FMULs. Another reason
14116// is the critical path is increased from "one FDIV" to "one FDIV + one FMUL".
14117SDValue DAGCombiner::combineRepeatedFPDivisors(SDNode *N) {
14118 // TODO: Limit this transform based on optsize/minsize - it always creates at
14119 // least 1 extra instruction. But the perf win may be substantial enough
14120 // that only minsize should restrict this.
14121 bool UnsafeMath = DAG.getTarget().Options.UnsafeFPMath;
14122 const SDNodeFlags Flags = N->getFlags();
14123 if (LegalDAG || (!UnsafeMath && !Flags.hasAllowReciprocal()))
14124 return SDValue();
14125
14126 // Skip if current node is a reciprocal/fneg-reciprocal.
14127 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
14128 ConstantFPSDNode *N0CFP = isConstOrConstSplatFP(N0, /* AllowUndefs */ true);
14129 if (N0CFP && (N0CFP->isExactlyValue(1.0) || N0CFP->isExactlyValue(-1.0)))
14130 return SDValue();
14131
14132 // Exit early if the target does not want this transform or if there can't
14133 // possibly be enough uses of the divisor to make the transform worthwhile.
14134 unsigned MinUses = TLI.combineRepeatedFPDivisors();
14135
14136 // For splat vectors, scale the number of uses by the splat factor. If we can
14137 // convert the division into a scalar op, that will likely be much faster.
14138 unsigned NumElts = 1;
14139 EVT VT = N->getValueType(0);
14140 if (VT.isVector() && DAG.isSplatValue(N1))
14141 NumElts = VT.getVectorNumElements();
14142
14143 if (!MinUses || (N1->use_size() * NumElts) < MinUses)
14144 return SDValue();
14145
14146 // Find all FDIV users of the same divisor.
14147 // Use a set because duplicates may be present in the user list.
14148 SetVector<SDNode *> Users;
14149 for (auto *U : N1->uses()) {
14150 if (U->getOpcode() == ISD::FDIV && U->getOperand(1) == N1) {
14151 // Skip X/sqrt(X) that has not been simplified to sqrt(X) yet.
14152 if (U->getOperand(1).getOpcode() == ISD::FSQRT &&
14153 U->getOperand(0) == U->getOperand(1).getOperand(0) &&
14154 U->getFlags().hasAllowReassociation() &&
14155 U->getFlags().hasNoSignedZeros())
14156 continue;
14157
14158 // This division is eligible for optimization only if global unsafe math
14159 // is enabled or if this division allows reciprocal formation.
14160 if (UnsafeMath || U->getFlags().hasAllowReciprocal())
14161 Users.insert(U);
14162 }
14163 }
14164
14165 // Now that we have the actual number of divisor uses, make sure it meets
14166 // the minimum threshold specified by the target.
14167 if ((Users.size() * NumElts) < MinUses)
14168 return SDValue();
14169
14170 SDLoc DL(N);
14171 SDValue FPOne = DAG.getConstantFP(1.0, DL, VT);
14172 SDValue Reciprocal = DAG.getNode(ISD::FDIV, DL, VT, FPOne, N1, Flags);
14173
14174 // Dividend / Divisor -> Dividend * Reciprocal
14175 for (auto *U : Users) {
14176 SDValue Dividend = U->getOperand(0);
14177 if (Dividend != FPOne) {
14178 SDValue NewNode = DAG.getNode(ISD::FMUL, SDLoc(U), VT, Dividend,
14179 Reciprocal, Flags);
14180 CombineTo(U, NewNode);
14181 } else if (U != Reciprocal.getNode()) {
14182 // In the absence of fast-math-flags, this user node is always the
14183 // same node as Reciprocal, but with FMF they may be different nodes.
14184 CombineTo(U, Reciprocal);
14185 }
14186 }
14187 return SDValue(N, 0); // N was replaced.
14188}
14189
14190SDValue DAGCombiner::visitFDIV(SDNode *N) {
14191 SDValue N0 = N->getOperand(0);
14192 SDValue N1 = N->getOperand(1);
14193 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
14194 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
14195 EVT VT = N->getValueType(0);
14196 SDLoc DL(N);
14197 const TargetOptions &Options = DAG.getTarget().Options;
14198 SDNodeFlags Flags = N->getFlags();
14199 SelectionDAG::FlagInserter FlagsInserter(DAG, N);
14200
14201 if (SDValue R = DAG.simplifyFPBinop(N->getOpcode(), N0, N1, Flags))
14202 return R;
14203
14204 // fold vector ops
14205 if (VT.isVector())
14206 if (SDValue FoldedVOp = SimplifyVBinOp(N))
14207 return FoldedVOp;
14208
14209 // fold (fdiv c1, c2) -> c1/c2
14210 if (N0CFP && N1CFP)
14211 return DAG.getNode(ISD::FDIV, SDLoc(N), VT, N0, N1);
14212
14213 if (SDValue NewSel = foldBinOpIntoSelect(N))
14214 return NewSel;
14215
14216 if (SDValue V = combineRepeatedFPDivisors(N))
14217 return V;
14218
14219 if (Options.UnsafeFPMath || Flags.hasAllowReciprocal()) {
14220 // fold (fdiv X, c2) -> fmul X, 1/c2 if losing precision is acceptable.
14221 if (N1CFP) {
14222 // Compute the reciprocal 1.0 / c2.
14223 const APFloat &N1APF = N1CFP->getValueAPF();
14224 APFloat Recip(N1APF.getSemantics(), 1); // 1.0
14225 APFloat::opStatus st = Recip.divide(N1APF, APFloat::rmNearestTiesToEven);
14226 // Only do the transform if the reciprocal is a legal fp immediate that
14227 // isn't too nasty (eg NaN, denormal, ...).
14228 if ((st == APFloat::opOK || st == APFloat::opInexact) && // Not too nasty
14229 (!LegalOperations ||
14230 // FIXME: custom lowering of ConstantFP might fail (see e.g. ARM
14231 // backend)... we should handle this gracefully after Legalize.
14232 // TLI.isOperationLegalOrCustom(ISD::ConstantFP, VT) ||
14233 TLI.isOperationLegal(ISD::ConstantFP, VT) ||
14234 TLI.isFPImmLegal(Recip, VT, ForCodeSize)))
14235 return DAG.getNode(ISD::FMUL, DL, VT, N0,
14236 DAG.getConstantFP(Recip, DL, VT));
14237 }
14238
14239 // If this FDIV is part of a reciprocal square root, it may be folded
14240 // into a target-specific square root estimate instruction.
14241 if (N1.getOpcode() == ISD::FSQRT) {
14242 if (SDValue RV = buildRsqrtEstimate(N1.getOperand(0), Flags))
14243 return DAG.getNode(ISD::FMUL, DL, VT, N0, RV);
14244 } else if (N1.getOpcode() == ISD::FP_EXTEND &&
14245 N1.getOperand(0).getOpcode() == ISD::FSQRT) {
14246 if (SDValue RV =
14247 buildRsqrtEstimate(N1.getOperand(0).getOperand(0), Flags)) {
14248 RV = DAG.getNode(ISD::FP_EXTEND, SDLoc(N1), VT, RV);
14249 AddToWorklist(RV.getNode());
14250 return DAG.getNode(ISD::FMUL, DL, VT, N0, RV);
14251 }
14252 } else if (N1.getOpcode() == ISD::FP_ROUND &&
14253 N1.getOperand(0).getOpcode() == ISD::FSQRT) {
14254 if (SDValue RV =
14255 buildRsqrtEstimate(N1.getOperand(0).getOperand(0), Flags)) {
14256 RV = DAG.getNode(ISD::FP_ROUND, SDLoc(N1), VT, RV, N1.getOperand(1));
14257 AddToWorklist(RV.getNode());
14258 return DAG.getNode(ISD::FMUL, DL, VT, N0, RV);
14259 }
14260 } else if (N1.getOpcode() == ISD::FMUL) {
14261 // Look through an FMUL. Even though this won't remove the FDIV directly,
14262 // it's still worthwhile to get rid of the FSQRT if possible.
14263 SDValue Sqrt, Y;
14264 if (N1.getOperand(0).getOpcode() == ISD::FSQRT) {
14265 Sqrt = N1.getOperand(0);
14266 Y = N1.getOperand(1);
14267 } else if (N1.getOperand(1).getOpcode() == ISD::FSQRT) {
14268 Sqrt = N1.getOperand(1);
14269 Y = N1.getOperand(0);
14270 }
14271 if (Sqrt.getNode()) {
14272 // If the other multiply operand is known positive, pull it into the
14273 // sqrt. That will eliminate the division if we convert to an estimate.
14274 if (Flags.hasAllowReassociation() && N1.hasOneUse() &&
14275 N1->getFlags().hasAllowReassociation() && Sqrt.hasOneUse()) {
14276 SDValue A;
14277 if (Y.getOpcode() == ISD::FABS && Y.hasOneUse())
14278 A = Y.getOperand(0);
14279 else if (Y == Sqrt.getOperand(0))
14280 A = Y;
14281 if (A) {
14282 // X / (fabs(A) * sqrt(Z)) --> X / sqrt(A*A*Z) --> X * rsqrt(A*A*Z)
14283 // X / (A * sqrt(A)) --> X / sqrt(A*A*A) --> X * rsqrt(A*A*A)
14284 SDValue AA = DAG.getNode(ISD::FMUL, DL, VT, A, A);
14285 SDValue AAZ =
14286 DAG.getNode(ISD::FMUL, DL, VT, AA, Sqrt.getOperand(0));
14287 if (SDValue Rsqrt = buildRsqrtEstimate(AAZ, Flags))
14288 return DAG.getNode(ISD::FMUL, DL, VT, N0, Rsqrt);
14289
14290 // Estimate creation failed. Clean up speculatively created nodes.
14291 recursivelyDeleteUnusedNodes(AAZ.getNode());
14292 }
14293 }
14294
14295 // We found a FSQRT, so try to make this fold:
14296 // X / (Y * sqrt(Z)) -> X * (rsqrt(Z) / Y)
14297 if (SDValue Rsqrt = buildRsqrtEstimate(Sqrt.getOperand(0), Flags)) {
14298 SDValue Div = DAG.getNode(ISD::FDIV, SDLoc(N1), VT, Rsqrt, Y);
14299 AddToWorklist(Div.getNode());
14300 return DAG.getNode(ISD::FMUL, DL, VT, N0, Div);
14301 }
14302 }
14303 }
14304
14305 // Fold into a reciprocal estimate and multiply instead of a real divide.
14306 if (Options.NoInfsFPMath || Flags.hasNoInfs())
14307 if (SDValue RV = BuildDivEstimate(N0, N1, Flags))
14308 return RV;
14309 }
14310
14311 // Fold X/Sqrt(X) -> Sqrt(X)
14312 if ((Options.NoSignedZerosFPMath || Flags.hasNoSignedZeros()) &&
14313 (Options.UnsafeFPMath || Flags.hasAllowReassociation()))
14314 if (N1.getOpcode() == ISD::FSQRT && N0 == N1.getOperand(0))
14315 return N1;
14316
14317 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
14318 TargetLowering::NegatibleCost CostN0 =
14319 TargetLowering::NegatibleCost::Expensive;
14320 TargetLowering::NegatibleCost CostN1 =
14321 TargetLowering::NegatibleCost::Expensive;
14322 SDValue NegN0 =
14323 TLI.getNegatedExpression(N0, DAG, LegalOperations, ForCodeSize, CostN0);
14324 SDValue NegN1 =
14325 TLI.getNegatedExpression(N1, DAG, LegalOperations, ForCodeSize, CostN1);
14326 if (NegN0 && NegN1 &&
14327 (CostN0 == TargetLowering::NegatibleCost::Cheaper ||
14328 CostN1 == TargetLowering::NegatibleCost::Cheaper))
14329 return DAG.getNode(ISD::FDIV, SDLoc(N), VT, NegN0, NegN1);
14330
14331 return SDValue();
14332}
14333
14334SDValue DAGCombiner::visitFREM(SDNode *N) {
14335 SDValue N0 = N->getOperand(0);
14336 SDValue N1 = N->getOperand(1);
14337 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
14338 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
14339 EVT VT = N->getValueType(0);
14340 SDNodeFlags Flags = N->getFlags();
14341 SelectionDAG::FlagInserter FlagsInserter(DAG, N);
14342
14343 if (SDValue R = DAG.simplifyFPBinop(N->getOpcode(), N0, N1, Flags))
14344 return R;
14345
14346 // fold (frem c1, c2) -> fmod(c1,c2)
14347 if (N0CFP && N1CFP)
14348 return DAG.getNode(ISD::FREM, SDLoc(N), VT, N0, N1);
14349
14350 if (SDValue NewSel = foldBinOpIntoSelect(N))
14351 return NewSel;
14352
14353 return SDValue();
14354}
14355
14356SDValue DAGCombiner::visitFSQRT(SDNode *N) {
14357 SDNodeFlags Flags = N->getFlags();
14358 const TargetOptions &Options = DAG.getTarget().Options;
14359
14360 // Require 'ninf' flag since sqrt(+Inf) = +Inf, but the estimation goes as:
14361 // sqrt(+Inf) == rsqrt(+Inf) * +Inf = 0 * +Inf = NaN
14362 if (!Flags.hasApproximateFuncs() ||
14363 (!Options.NoInfsFPMath && !Flags.hasNoInfs()))
14364 return SDValue();
14365
14366 SDValue N0 = N->getOperand(0);
14367 if (TLI.isFsqrtCheap(N0, DAG))
14368 return SDValue();
14369
14370 // FSQRT nodes have flags that propagate to the created nodes.
14371 // TODO: If this is N0/sqrt(N0), and we reach this node before trying to
14372 // transform the fdiv, we may produce a sub-optimal estimate sequence
14373 // because the reciprocal calculation may not have to filter out a
14374 // 0.0 input.
14375 return buildSqrtEstimate(N0, Flags);
14376}
14377
14378/// copysign(x, fp_extend(y)) -> copysign(x, y)
14379/// copysign(x, fp_round(y)) -> copysign(x, y)
14380static inline bool CanCombineFCOPYSIGN_EXTEND_ROUND(SDNode *N) {
14381 SDValue N1 = N->getOperand(1);
14382 if ((N1.getOpcode() == ISD::FP_EXTEND ||
14383 N1.getOpcode() == ISD::FP_ROUND)) {
14384 EVT N1VT = N1->getValueType(0);
14385 EVT N1Op0VT = N1->getOperand(0).getValueType();
14386
14387 // Always fold no-op FP casts.
14388 if (N1VT == N1Op0VT)
14389 return true;
14390
14391 // Do not optimize out type conversion of f128 type yet.
14392 // For some targets like x86_64, configuration is changed to keep one f128
14393 // value in one SSE register, but instruction selection cannot handle
14394 // FCOPYSIGN on SSE registers yet.
14395 if (N1Op0VT == MVT::f128)
14396 return false;
14397
14398 // Avoid mismatched vector operand types, for better instruction selection.
14399 if (N1Op0VT.isVector())
14400 return false;
14401
14402 return true;
14403 }
14404 return false;
14405}
14406
14407SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
14408 SDValue N0 = N->getOperand(0);
14409 SDValue N1 = N->getOperand(1);
14410 bool N0CFP = DAG.isConstantFPBuildVectorOrConstantFP(N0);
14411 bool N1CFP = DAG.isConstantFPBuildVectorOrConstantFP(N1);
14412 EVT VT = N->getValueType(0);
14413
14414 if (N0CFP && N1CFP) // Constant fold
14415 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, N0, N1);
14416
14417 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N->getOperand(1))) {
14418 const APFloat &V = N1C->getValueAPF();
14419 // copysign(x, c1) -> fabs(x) iff ispos(c1)
14420 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
14421 if (!V.isNegative()) {
14422 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
14423 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
14424 } else {
14425 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
14426 return DAG.getNode(ISD::FNEG, SDLoc(N), VT,
14427 DAG.getNode(ISD::FABS, SDLoc(N0), VT, N0));
14428 }
14429 }
14430
14431 // copysign(fabs(x), y) -> copysign(x, y)
14432 // copysign(fneg(x), y) -> copysign(x, y)
14433 // copysign(copysign(x,z), y) -> copysign(x, y)
14434 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
14435 N0.getOpcode() == ISD::FCOPYSIGN)
14436 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, N0.getOperand(0), N1);
14437
14438 // copysign(x, abs(y)) -> abs(x)
14439 if (N1.getOpcode() == ISD::FABS)
14440 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
14441
14442 // copysign(x, copysign(y,z)) -> copysign(x, z)
14443 if (N1.getOpcode() == ISD::FCOPYSIGN)
14444 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, N0, N1.getOperand(1));
14445
14446 // copysign(x, fp_extend(y)) -> copysign(x, y)
14447 // copysign(x, fp_round(y)) -> copysign(x, y)
14448 if (CanCombineFCOPYSIGN_EXTEND_ROUND(N))
14449 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, N0, N1.getOperand(0));
14450
14451 return SDValue();
14452}
14453
14454SDValue DAGCombiner::visitFPOW(SDNode *N) {
14455 ConstantFPSDNode *ExponentC = isConstOrConstSplatFP(N->getOperand(1));
14456 if (!ExponentC)
14457 return SDValue();
14458 SelectionDAG::FlagInserter FlagsInserter(DAG, N);
14459
14460 // Try to convert x ** (1/3) into cube root.
14461 // TODO: Handle the various flavors of long double.
14462 // TODO: Since we're approximating, we don't need an exact 1/3 exponent.
14463 // Some range near 1/3 should be fine.
14464 EVT VT = N->getValueType(0);
14465 if ((VT == MVT::f32 && ExponentC->getValueAPF().isExactlyValue(1.0f/3.0f)) ||
14466 (VT == MVT::f64 && ExponentC->getValueAPF().isExactlyValue(1.0/3.0))) {
14467 // pow(-0.0, 1/3) = +0.0; cbrt(-0.0) = -0.0.
14468 // pow(-inf, 1/3) = +inf; cbrt(-inf) = -inf.
14469 // pow(-val, 1/3) = nan; cbrt(-val) = -num.
14470 // For regular numbers, rounding may cause the results to differ.
14471 // Therefore, we require { nsz ninf nnan afn } for this transform.
14472 // TODO: We could select out the special cases if we don't have nsz/ninf.
14473 SDNodeFlags Flags = N->getFlags();
14474 if (!Flags.hasNoSignedZeros() || !Flags.hasNoInfs() || !Flags.hasNoNaNs() ||
14475 !Flags.hasApproximateFuncs())
14476 return SDValue();
14477
14478 // Do not create a cbrt() libcall if the target does not have it, and do not
14479 // turn a pow that has lowering support into a cbrt() libcall.
14480 if (!DAG.getLibInfo().has(LibFunc_cbrt) ||
14481 (!DAG.getTargetLoweringInfo().isOperationExpand(ISD::FPOW, VT) &&
14482 DAG.getTargetLoweringInfo().isOperationExpand(ISD::FCBRT, VT)))
14483 return SDValue();
14484
14485 return DAG.getNode(ISD::FCBRT, SDLoc(N), VT, N->getOperand(0));
14486 }
14487
14488 // Try to convert x ** (1/4) and x ** (3/4) into square roots.
14489 // x ** (1/2) is canonicalized to sqrt, so we do not bother with that case.
14490 // TODO: This could be extended (using a target hook) to handle smaller
14491 // power-of-2 fractional exponents.
14492 bool ExponentIs025 = ExponentC->getValueAPF().isExactlyValue(0.25);
14493 bool ExponentIs075 = ExponentC->getValueAPF().isExactlyValue(0.75);
14494 if (ExponentIs025 || ExponentIs075) {
14495 // pow(-0.0, 0.25) = +0.0; sqrt(sqrt(-0.0)) = -0.0.
14496 // pow(-inf, 0.25) = +inf; sqrt(sqrt(-inf)) = NaN.
14497 // pow(-0.0, 0.75) = +0.0; sqrt(-0.0) * sqrt(sqrt(-0.0)) = +0.0.
14498 // pow(-inf, 0.75) = +inf; sqrt(-inf) * sqrt(sqrt(-inf)) = NaN.
14499 // For regular numbers, rounding may cause the results to differ.
14500 // Therefore, we require { nsz ninf afn } for this transform.
14501 // TODO: We could select out the special cases if we don't have nsz/ninf.
14502 SDNodeFlags Flags = N->getFlags();
14503
14504 // We only need no signed zeros for the 0.25 case.
14505 if ((!Flags.hasNoSignedZeros() && ExponentIs025) || !Flags.hasNoInfs() ||
14506 !Flags.hasApproximateFuncs())
14507 return SDValue();
14508
14509 // Don't double the number of libcalls. We are trying to inline fast code.
14510 if (!DAG.getTargetLoweringInfo().isOperationLegalOrCustom(ISD::FSQRT, VT))
14511 return SDValue();
14512
14513 // Assume that libcalls are the smallest code.
14514 // TODO: This restriction should probably be lifted for vectors.
14515 if (ForCodeSize)
14516 return SDValue();
14517
14518 // pow(X, 0.25) --> sqrt(sqrt(X))
14519 SDLoc DL(N);
14520 SDValue Sqrt = DAG.getNode(ISD::FSQRT, DL, VT, N->getOperand(0));
14521 SDValue SqrtSqrt = DAG.getNode(ISD::FSQRT, DL, VT, Sqrt);
14522 if (ExponentIs025)
14523 return SqrtSqrt;
14524 // pow(X, 0.75) --> sqrt(X) * sqrt(sqrt(X))
14525 return DAG.getNode(ISD::FMUL, DL, VT, Sqrt, SqrtSqrt);
14526 }
14527
14528 return SDValue();
14529}
14530
14531static SDValue foldFPToIntToFP(SDNode *N, SelectionDAG &DAG,
14532 const TargetLowering &TLI) {
14533 // This optimization is guarded by a function attribute because it may produce
14534 // unexpected results. Ie, programs may be relying on the platform-specific
14535 // undefined behavior when the float-to-int conversion overflows.
14536 const Function &F = DAG.getMachineFunction().getFunction();
14537 Attribute StrictOverflow = F.getFnAttribute("strict-float-cast-overflow");
14538 if (StrictOverflow.getValueAsString().equals("false"))
14539 return SDValue();
14540
14541 // We only do this if the target has legal ftrunc. Otherwise, we'd likely be
14542 // replacing casts with a libcall. We also must be allowed to ignore -0.0
14543 // because FTRUNC will return -0.0 for (-1.0, -0.0), but using integer
14544 // conversions would return +0.0.
14545 // FIXME: We should be able to use node-level FMF here.
14546 // TODO: If strict math, should we use FABS (+ range check for signed cast)?
14547 EVT VT = N->getValueType(0);
14548 if (!TLI.isOperationLegal(ISD::FTRUNC, VT) ||
14549 !DAG.getTarget().Options.NoSignedZerosFPMath)
14550 return SDValue();
14551
14552 // fptosi/fptoui round towards zero, so converting from FP to integer and
14553 // back is the same as an 'ftrunc': [us]itofp (fpto[us]i X) --> ftrunc X
14554 SDValue N0 = N->getOperand(0);
14555 if (N->getOpcode() == ISD::SINT_TO_FP && N0.getOpcode() == ISD::FP_TO_SINT &&
14556 N0.getOperand(0).getValueType() == VT)
14557 return DAG.getNode(ISD::FTRUNC, SDLoc(N), VT, N0.getOperand(0));
14558
14559 if (N->getOpcode() == ISD::UINT_TO_FP && N0.getOpcode() == ISD::FP_TO_UINT &&
14560 N0.getOperand(0).getValueType() == VT)
14561 return DAG.getNode(ISD::FTRUNC, SDLoc(N), VT, N0.getOperand(0));
14562
14563 return SDValue();
14564}
14565
14566SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
14567 SDValue N0 = N->getOperand(0);
14568 EVT VT = N->getValueType(0);
14569 EVT OpVT = N0.getValueType();
14570
14571 // [us]itofp(undef) = 0, because the result value is bounded.
14572 if (N0.isUndef())
14573 return DAG.getConstantFP(0.0, SDLoc(N), VT);
14574
14575 // fold (sint_to_fp c1) -> c1fp
14576 if (DAG.isConstantIntBuildVectorOrConstantInt(N0) &&
14577 // ...but only if the target supports immediate floating-point values
14578 (!LegalOperations ||
14579 TLI.isOperationLegalOrCustom(ISD::ConstantFP, VT)))
14580 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
14581
14582 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
14583 // but UINT_TO_FP is legal on this target, try to convert.
14584 if (!hasOperation(ISD::SINT_TO_FP, OpVT) &&
14585 hasOperation(ISD::UINT_TO_FP, OpVT)) {
14586 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
14587 if (DAG.SignBitIsZero(N0))
14588 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
14589 }
14590
14591 // The next optimizations are desirable only if SELECT_CC can be lowered.
14592 // fold (sint_to_fp (setcc x, y, cc)) -> (select (setcc x, y, cc), -1.0, 0.0)
14593 if (N0.getOpcode() == ISD::SETCC && N0.getValueType() == MVT::i1 &&
14594 !VT.isVector() &&
14595 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::ConstantFP, VT))) {
14596 SDLoc DL(N);
14597 return DAG.getSelect(DL, VT, N0, DAG.getConstantFP(-1.0, DL, VT),
14598 DAG.getConstantFP(0.0, DL, VT));
14599 }
14600
14601 // fold (sint_to_fp (zext (setcc x, y, cc))) ->
14602 // (select (setcc x, y, cc), 1.0, 0.0)
14603 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
14604 N0.getOperand(0).getOpcode() == ISD::SETCC && !VT.isVector() &&
14605 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::ConstantFP, VT))) {
14606 SDLoc DL(N);
14607 return DAG.getSelect(DL, VT, N0.getOperand(0),
14608 DAG.getConstantFP(1.0, DL, VT),
14609 DAG.getConstantFP(0.0, DL, VT));
14610 }
14611
14612 if (SDValue FTrunc = foldFPToIntToFP(N, DAG, TLI))
14613 return FTrunc;
14614
14615 return SDValue();
14616}
14617
14618SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
14619 SDValue N0 = N->getOperand(0);
14620 EVT VT = N->getValueType(0);
14621 EVT OpVT = N0.getValueType();
14622
14623 // [us]itofp(undef) = 0, because the result value is bounded.
14624 if (N0.isUndef())
14625 return DAG.getConstantFP(0.0, SDLoc(N), VT);
14626
14627 // fold (uint_to_fp c1) -> c1fp
14628 if (DAG.isConstantIntBuildVectorOrConstantInt(N0) &&
14629 // ...but only if the target supports immediate floating-point values
14630 (!LegalOperations ||
14631 TLI.isOperationLegalOrCustom(ISD::ConstantFP, VT)))
14632 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
14633
14634 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
14635 // but SINT_TO_FP is legal on this target, try to convert.
14636 if (!hasOperation(ISD::UINT_TO_FP, OpVT) &&
14637 hasOperation(ISD::SINT_TO_FP, OpVT)) {
14638 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
14639 if (DAG.SignBitIsZero(N0))
14640 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
14641 }
14642
14643 // fold (uint_to_fp (setcc x, y, cc)) -> (select (setcc x, y, cc), 1.0, 0.0)
14644 if (N0.getOpcode() == ISD::SETCC && !VT.isVector() &&
14645 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::ConstantFP, VT))) {
14646 SDLoc DL(N);
14647 return DAG.getSelect(DL, VT, N0, DAG.getConstantFP(1.0, DL, VT),
14648 DAG.getConstantFP(0.0, DL, VT));
14649 }
14650
14651 if (SDValue FTrunc = foldFPToIntToFP(N, DAG, TLI))
14652 return FTrunc;
14653
14654 return SDValue();
14655}
14656
14657// Fold (fp_to_{s/u}int ({s/u}int_to_fpx)) -> zext x, sext x, trunc x, or x
14658static SDValue FoldIntToFPToInt(SDNode *N, SelectionDAG &DAG) {
14659 SDValue N0 = N->getOperand(0);
14660 EVT VT = N->getValueType(0);
14661
14662 if (N0.getOpcode() != ISD::UINT_TO_FP && N0.getOpcode() != ISD::SINT_TO_FP)
14663 return SDValue();
14664
14665 SDValue Src = N0.getOperand(0);
14666 EVT SrcVT = Src.getValueType();
14667 bool IsInputSigned = N0.getOpcode() == ISD::SINT_TO_FP;
14668 bool IsOutputSigned = N->getOpcode() == ISD::FP_TO_SINT;
14669
14670 // We can safely assume the conversion won't overflow the output range,
14671 // because (for example) (uint8_t)18293.f is undefined behavior.
14672
14673 // Since we can assume the conversion won't overflow, our decision as to
14674 // whether the input will fit in the float should depend on the minimum
14675 // of the input range and output range.
14676
14677 // This means this is also safe for a signed input and unsigned output, since
14678 // a negative input would lead to undefined behavior.
14679 unsigned InputSize = (int)SrcVT.getScalarSizeInBits() - IsInputSigned;
14680 unsigned OutputSize = (int)VT.getScalarSizeInBits() - IsOutputSigned;
14681 unsigned ActualSize = std::min(InputSize, OutputSize);
14682 const fltSemantics &sem = DAG.EVTToAPFloatSemantics(N0.getValueType());
14683
14684 // We can only fold away the float conversion if the input range can be
14685 // represented exactly in the float range.
14686 if (APFloat::semanticsPrecision(sem) >= ActualSize) {
14687 if (VT.getScalarSizeInBits() > SrcVT.getScalarSizeInBits()) {
14688 unsigned ExtOp = IsInputSigned && IsOutputSigned ? ISD::SIGN_EXTEND
14689 : ISD::ZERO_EXTEND;
14690 return DAG.getNode(ExtOp, SDLoc(N), VT, Src);
14691 }
14692 if (VT.getScalarSizeInBits() < SrcVT.getScalarSizeInBits())
14693 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Src);
14694 return DAG.getBitcast(VT, Src);
14695 }
14696 return SDValue();
14697}
14698
14699SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
14700 SDValue N0 = N->getOperand(0);
14701 EVT VT = N->getValueType(0);
14702
14703 // fold (fp_to_sint undef) -> undef
14704 if (N0.isUndef())
14705 return DAG.getUNDEF(VT);
14706
14707 // fold (fp_to_sint c1fp) -> c1
14708 if (DAG.isConstantFPBuildVectorOrConstantFP(N0))
14709 return DAG.getNode(ISD::FP_TO_SINT, SDLoc(N), VT, N0);
14710
14711 return FoldIntToFPToInt(N, DAG);
14712}
14713
14714SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
14715 SDValue N0 = N->getOperand(0);
14716 EVT VT = N->getValueType(0);
14717
14718 // fold (fp_to_uint undef) -> undef
14719 if (N0.isUndef())
14720 return DAG.getUNDEF(VT);
14721
14722 // fold (fp_to_uint c1fp) -> c1
14723 if (DAG.isConstantFPBuildVectorOrConstantFP(N0))
14724 return DAG.getNode(ISD::FP_TO_UINT, SDLoc(N), VT, N0);
14725
14726 return FoldIntToFPToInt(N, DAG);
14727}
14728
14729SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
14730 SDValue N0 = N->getOperand(0);
14731 SDValue N1 = N->getOperand(1);
14732 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
14733 EVT VT = N->getValueType(0);
14734
14735 // fold (fp_round c1fp) -> c1fp
14736 if (N0CFP)
14737 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0, N1);
14738
14739 // fold (fp_round (fp_extend x)) -> x
14740 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
14741 return N0.getOperand(0);
14742
14743 // fold (fp_round (fp_round x)) -> (fp_round x)
14744 if (N0.getOpcode() == ISD::FP_ROUND) {
14745 const bool NIsTrunc = N->getConstantOperandVal(1) == 1;
14746 const bool N0IsTrunc = N0.getConstantOperandVal(1) == 1;
14747
14748 // Skip this folding if it results in an fp_round from f80 to f16.
14749 //
14750 // f80 to f16 always generates an expensive (and as yet, unimplemented)
14751 // libcall to __truncxfhf2 instead of selecting native f16 conversion
14752 // instructions from f32 or f64. Moreover, the first (value-preserving)
14753 // fp_round from f80 to either f32 or f64 may become a NOP in platforms like
14754 // x86.
14755 if (N0.getOperand(0).getValueType() == MVT::f80 && VT == MVT::f16)
14756 return SDValue();
14757
14758 // If the first fp_round isn't a value preserving truncation, it might
14759 // introduce a tie in the second fp_round, that wouldn't occur in the
14760 // single-step fp_round we want to fold to.
14761 // In other words, double rounding isn't the same as rounding.
14762 // Also, this is a value preserving truncation iff both fp_round's are.
14763 if (DAG.getTarget().Options.UnsafeFPMath || N0IsTrunc) {
14764 SDLoc DL(N);
14765 return DAG.getNode(ISD::FP_ROUND, DL, VT, N0.getOperand(0),
14766 DAG.getIntPtrConstant(NIsTrunc && N0IsTrunc, DL));
14767 }
14768 }
14769
14770 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
14771 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
14772 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, SDLoc(N0), VT,
14773 N0.getOperand(0), N1);
14774 AddToWorklist(Tmp.getNode());
14775 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
14776 Tmp, N0.getOperand(1));
14777 }
14778
14779 if (SDValue NewVSel = matchVSelectOpSizesWithSetCC(N))
14780 return NewVSel;
14781
14782 return SDValue();
14783}
14784
14785SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
14786 SDValue N0 = N->getOperand(0);
14787 EVT VT = N->getValueType(0);
14788
14789 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
14790 if (N->hasOneUse() &&
14791 N->use_begin()->getOpcode() == ISD::FP_ROUND)
14792 return SDValue();
14793
14794 // fold (fp_extend c1fp) -> c1fp
14795 if (DAG.isConstantFPBuildVectorOrConstantFP(N0))
14796 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, N0);
14797
14798 // fold (fp_extend (fp16_to_fp op)) -> (fp16_to_fp op)
14799 if (N0.getOpcode() == ISD::FP16_TO_FP &&
14800 TLI.getOperationAction(ISD::FP16_TO_FP, VT) == TargetLowering::Legal)
14801 return DAG.getNode(ISD::FP16_TO_FP, SDLoc(N), VT, N0.getOperand(0));
14802
14803 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
14804 // value of X.
14805 if (N0.getOpcode() == ISD::FP_ROUND
14806 && N0.getConstantOperandVal(1) == 1) {
14807 SDValue In = N0.getOperand(0);
14808 if (In.getValueType() == VT) return In;
14809 if (VT.bitsLT(In.getValueType()))
14810 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT,
14811 In, N0.getOperand(1));
14812 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, In);
14813 }
14814
14815 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
14816 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
14817 TLI.isLoadExtLegal(ISD::EXTLOAD, VT, N0.getValueType())) {
14818 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
14819 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
14820 LN0->getChain(),
14821 LN0->getBasePtr(), N0.getValueType(),
14822 LN0->getMemOperand());
14823 CombineTo(N, ExtLoad);
14824 CombineTo(N0.getNode(),
14825 DAG.getNode(ISD::FP_ROUND, SDLoc(N0),
14826 N0.getValueType(), ExtLoad,
14827 DAG.getIntPtrConstant(1, SDLoc(N0))),
14828 ExtLoad.getValue(1));
14829 return SDValue(N, 0); // Return N so it doesn't get rechecked!
14830 }
14831
14832 if (SDValue NewVSel = matchVSelectOpSizesWithSetCC(N))
14833 return NewVSel;
14834
14835 return SDValue();
14836}
14837
14838SDValue DAGCombiner::visitFCEIL(SDNode *N) {
14839 SDValue N0 = N->getOperand(0);
14840 EVT VT = N->getValueType(0);
14841
14842 // fold (fceil c1) -> fceil(c1)
14843 if (DAG.isConstantFPBuildVectorOrConstantFP(N0))
14844 return DAG.getNode(ISD::FCEIL, SDLoc(N), VT, N0);
14845
14846 return SDValue();
14847}
14848
14849SDValue DAGCombiner::visitFTRUNC(SDNode *N) {
14850 SDValue N0 = N->getOperand(0);
14851 EVT VT = N->getValueType(0);
14852
14853 // fold (ftrunc c1) -> ftrunc(c1)
14854 if (DAG.isConstantFPBuildVectorOrConstantFP(N0))
14855 return DAG.getNode(ISD::FTRUNC, SDLoc(N), VT, N0);
14856
14857 // fold ftrunc (known rounded int x) -> x
14858 // ftrunc is a part of fptosi/fptoui expansion on some targets, so this is
14859 // likely to be generated to extract integer from a rounded floating value.
14860 switch (N0.getOpcode()) {
14861 default: break;
14862 case ISD::FRINT:
14863 case ISD::FTRUNC:
14864 case ISD::FNEARBYINT:
14865 case ISD::FFLOOR:
14866 case ISD::FCEIL:
14867 return N0;
14868 }
14869
14870 return SDValue();
14871}
14872
14873SDValue DAGCombiner::visitFFLOOR(SDNode *N) {
14874 SDValue N0 = N->getOperand(0);
14875 EVT VT = N->getValueType(0);
14876
14877 // fold (ffloor c1) -> ffloor(c1)
14878 if (DAG.isConstantFPBuildVectorOrConstantFP(N0))
14879 return DAG.getNode(ISD::FFLOOR, SDLoc(N), VT, N0);
14880
14881 return SDValue();
14882}
14883
14884SDValue DAGCombiner::visitFNEG(SDNode *N) {
14885 SDValue N0 = N->getOperand(0);
14886 EVT VT = N->getValueType(0);
14887 SelectionDAG::FlagInserter FlagsInserter(DAG, N);
14888
14889 // Constant fold FNEG.
14890 if (DAG.isConstantFPBuildVectorOrConstantFP(N0))
14891 return DAG.getNode(ISD::FNEG, SDLoc(N), VT, N0);
14892
14893 if (SDValue NegN0 =
14894 TLI.getNegatedExpression(N0, DAG, LegalOperations, ForCodeSize))
14895 return NegN0;
14896
14897 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
14898 // FIXME: This is duplicated in getNegatibleCost, but getNegatibleCost doesn't
14899 // know it was called from a context with a nsz flag if the input fsub does
14900 // not.
14901 if (N0.getOpcode() == ISD::FSUB &&
14902 (DAG.getTarget().Options.NoSignedZerosFPMath ||
14903 N->getFlags().hasNoSignedZeros()) && N0.hasOneUse()) {
14904 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0.getOperand(1),
14905 N0.getOperand(0));
14906 }
14907
14908 if (SDValue Cast = foldSignChangeInBitcast(N))
14909 return Cast;
14910
14911 return SDValue();
14912}
14913
14914static SDValue visitFMinMax(SelectionDAG &DAG, SDNode *N,
14915 APFloat (*Op)(const APFloat &, const APFloat &)) {
14916 SDValue N0 = N->getOperand(0);
14917 SDValue N1 = N->getOperand(1);
14918 EVT VT = N->getValueType(0);
14919 const ConstantFPSDNode *N0CFP = isConstOrConstSplatFP(N0);
14920 const ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1);
14921 const SDNodeFlags Flags = N->getFlags();
14922 unsigned Opc = N->getOpcode();
14923 bool PropagatesNaN = Opc == ISD::FMINIMUM || Opc == ISD::FMAXIMUM;
14924 bool IsMin = Opc == ISD::FMINNUM || Opc == ISD::FMINIMUM;
14925 SelectionDAG::FlagInserter FlagsInserter(DAG, N);
14926
14927 if (N0CFP && N1CFP) {
14928 const APFloat &C0 = N0CFP->getValueAPF();
14929 const APFloat &C1 = N1CFP->getValueAPF();
14930 return DAG.getConstantFP(Op(C0, C1), SDLoc(N), VT);
14931 }
14932
14933 // Canonicalize to constant on RHS.
14934 if (DAG.isConstantFPBuildVectorOrConstantFP(N0) &&
14935 !DAG.isConstantFPBuildVectorOrConstantFP(N1))
14936 return DAG.getNode(N->getOpcode(), SDLoc(N), VT, N1, N0);
14937
14938 if (N1CFP) {
14939 const APFloat &AF = N1CFP->getValueAPF();
14940
14941 // minnum(X, nan) -> X
14942 // maxnum(X, nan) -> X
14943 // minimum(X, nan) -> nan
14944 // maximum(X, nan) -> nan
14945 if (AF.isNaN())
14946 return PropagatesNaN ? N->getOperand(1) : N->getOperand(0);
14947
14948 // In the following folds, inf can be replaced with the largest finite
14949 // float, if the ninf flag is set.
14950 if (AF.isInfinity() || (Flags.hasNoInfs() && AF.isLargest())) {
14951 // minnum(X, -inf) -> -inf
14952 // maxnum(X, +inf) -> +inf
14953 // minimum(X, -inf) -> -inf if nnan
14954 // maximum(X, +inf) -> +inf if nnan
14955 if (IsMin == AF.isNegative() && (!PropagatesNaN || Flags.hasNoNaNs()))
14956 return N->getOperand(1);
14957
14958 // minnum(X, +inf) -> X if nnan
14959 // maxnum(X, -inf) -> X if nnan
14960 // minimum(X, +inf) -> X
14961 // maximum(X, -inf) -> X
14962 if (IsMin != AF.isNegative() && (PropagatesNaN || Flags.hasNoNaNs()))
14963 return N->getOperand(0);
14964 }
14965 }
14966
14967 return SDValue();
14968}
14969
14970SDValue DAGCombiner::visitFMINNUM(SDNode *N) {
14971 return visitFMinMax(DAG, N, minnum);
14972}
14973
14974SDValue DAGCombiner::visitFMAXNUM(SDNode *N) {
14975 return visitFMinMax(DAG, N, maxnum);
14976}
14977
14978SDValue DAGCombiner::visitFMINIMUM(SDNode *N) {
14979 return visitFMinMax(DAG, N, minimum);
14980}
14981
14982SDValue DAGCombiner::visitFMAXIMUM(SDNode *N) {
14983 return visitFMinMax(DAG, N, maximum);
14984}
14985
14986SDValue DAGCombiner::visitFABS(SDNode *N) {
14987 SDValue N0 = N->getOperand(0);
14988 EVT VT = N->getValueType(0);
14989
14990 // fold (fabs c1) -> fabs(c1)
14991 if (DAG.isConstantFPBuildVectorOrConstantFP(N0))
14992 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
14993
14994 // fold (fabs (fabs x)) -> (fabs x)
14995 if (N0.getOpcode() == ISD::FABS)
14996 return N->getOperand(0);
14997
14998 // fold (fabs (fneg x)) -> (fabs x)
14999 // fold (fabs (fcopysign x, y)) -> (fabs x)
15000 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
15001 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0.getOperand(0));
15002
15003 if (SDValue Cast = foldSignChangeInBitcast(N))
15004 return Cast;
15005
15006 return SDValue();
15007}
15008
15009SDValue DAGCombiner::visitBRCOND(SDNode *N) {
15010 SDValue Chain = N->getOperand(0);
15011 SDValue N1 = N->getOperand(1);
15012 SDValue N2 = N->getOperand(2);
15013
15014 // BRCOND(FREEZE(cond)) is equivalent to BRCOND(cond) (both are
15015 // nondeterministic jumps).
15016 if (N1->getOpcode() == ISD::FREEZE && N1.hasOneUse()) {
15017 return DAG.getNode(ISD::BRCOND, SDLoc(N), MVT::Other, Chain,
15018 N1->getOperand(0), N2);
15019 }
15020
15021 // If N is a constant we could fold this into a fallthrough or unconditional
15022 // branch. However that doesn't happen very often in normal code, because
15023 // Instcombine/SimplifyCFG should have handled the available opportunities.
15024 // If we did this folding here, it would be necessary to update the
15025 // MachineBasicBlock CFG, which is awkward.
15026
15027 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
15028 // on the target.
15029 if (N1.getOpcode() == ISD::SETCC &&
15030 TLI.isOperationLegalOrCustom(ISD::BR_CC,
15031 N1.getOperand(0).getValueType())) {
15032 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
15033 Chain, N1.getOperand(2),
15034 N1.getOperand(0), N1.getOperand(1), N2);
15035 }
15036
15037 if (N1.hasOneUse()) {
15038 // rebuildSetCC calls visitXor which may change the Chain when there is a
15039 // STRICT_FSETCC/STRICT_FSETCCS involved. Use a handle to track changes.
15040 HandleSDNode ChainHandle(Chain);
15041 if (SDValue NewN1 = rebuildSetCC(N1))
15042 return DAG.getNode(ISD::BRCOND, SDLoc(N), MVT::Other,
15043 ChainHandle.getValue(), NewN1, N2);
15044 }
15045
15046 return SDValue();
15047}
15048
15049SDValue DAGCombiner::rebuildSetCC(SDValue N) {
15050 if (N.getOpcode() == ISD::SRL ||
15051 (N.getOpcode() == ISD::TRUNCATE &&
15052 (N.getOperand(0).hasOneUse() &&
15053 N.getOperand(0).getOpcode() == ISD::SRL))) {
15054 // Look pass the truncate.
15055 if (N.getOpcode() == ISD::TRUNCATE)
15056 N = N.getOperand(0);
15057
15058 // Match this pattern so that we can generate simpler code:
15059 //
15060 // %a = ...
15061 // %b = and i32 %a, 2
15062 // %c = srl i32 %b, 1
15063 // brcond i32 %c ...
15064 //
15065 // into
15066 //
15067 // %a = ...
15068 // %b = and i32 %a, 2
15069 // %c = setcc eq %b, 0
15070 // brcond %c ...
15071 //
15072 // This applies only when the AND constant value has one bit set and the
15073 // SRL constant is equal to the log2 of the AND constant. The back-end is
15074 // smart enough to convert the result into a TEST/JMP sequence.
15075 SDValue Op0 = N.getOperand(0);
15076 SDValue Op1 = N.getOperand(1);
15077
15078 if (Op0.getOpcode() == ISD::AND && Op1.getOpcode() == ISD::Constant) {
15079 SDValue AndOp1 = Op0.getOperand(1);
15080
15081 if (AndOp1.getOpcode() == ISD::Constant) {
15082 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
15083
15084 if (AndConst.isPowerOf2() &&
15085 cast<ConstantSDNode>(Op1)->getAPIntValue() == AndConst.logBase2()) {
15086 SDLoc DL(N);
15087 return DAG.getSetCC(DL, getSetCCResultType(Op0.getValueType()),
15088 Op0, DAG.getConstant(0, DL, Op0.getValueType()),
15089 ISD::SETNE);
15090 }
15091 }
15092 }
15093 }
15094
15095 // Transform (brcond (xor x, y)) -> (brcond (setcc, x, y, ne))
15096 // Transform (brcond (xor (xor x, y), -1)) -> (brcond (setcc, x, y, eq))
15097 if (N.getOpcode() == ISD::XOR) {
15098 // Because we may call this on a speculatively constructed
15099 // SimplifiedSetCC Node, we need to simplify this node first.
15100 // Ideally this should be folded into SimplifySetCC and not
15101 // here. For now, grab a handle to N so we don't lose it from
15102 // replacements interal to the visit.
15103 HandleSDNode XORHandle(N);
15104 while (N.getOpcode() == ISD::XOR) {
15105 SDValue Tmp = visitXOR(N.getNode());
15106 // No simplification done.
15107 if (!Tmp.getNode())
15108 break;
15109 // Returning N is form in-visit replacement that may invalidated
15110 // N. Grab value from Handle.
15111 if (Tmp.getNode() == N.getNode())
15112 N = XORHandle.getValue();
15113 else // Node simplified. Try simplifying again.
15114 N = Tmp;
15115 }
15116
15117 if (N.getOpcode() != ISD::XOR)
15118 return N;
15119
15120 SDValue Op0 = N->getOperand(0);
15121 SDValue Op1 = N->getOperand(1);
15122
15123 if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) {
15124 bool Equal = false;
15125 // (brcond (xor (xor x, y), -1)) -> (brcond (setcc x, y, eq))
15126 if (isBitwiseNot(N) && Op0.hasOneUse() && Op0.getOpcode() == ISD::XOR &&
15127 Op0.getValueType() == MVT::i1) {
15128 N = Op0;
15129 Op0 = N->getOperand(0);
15130 Op1 = N->getOperand(1);
15131 Equal = true;
15132 }
15133
15134 EVT SetCCVT = N.getValueType();
15135 if (LegalTypes)
15136 SetCCVT = getSetCCResultType(SetCCVT);
15137 // Replace the uses of XOR with SETCC
15138 return DAG.getSetCC(SDLoc(N), SetCCVT, Op0, Op1,
15139 Equal ? ISD::SETEQ : ISD::SETNE);
15140 }
15141 }
15142
15143 return SDValue();
15144}
15145
15146// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
15147//
15148SDValue DAGCombiner::visitBR_CC(SDNode *N) {
15149 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
15150 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
15151
15152 // If N is a constant we could fold this into a fallthrough or unconditional
15153 // branch. However that doesn't happen very often in normal code, because
15154 // Instcombine/SimplifyCFG should have handled the available opportunities.
15155 // If we did this folding here, it would be necessary to update the
15156 // MachineBasicBlock CFG, which is awkward.
15157
15158 // Use SimplifySetCC to simplify SETCC's.
15159 SDValue Simp = SimplifySetCC(getSetCCResultType(CondLHS.getValueType()),
15160 CondLHS, CondRHS, CC->get(), SDLoc(N),
15161 false);
15162 if (Simp.getNode()) AddToWorklist(Simp.getNode());
15163
15164 // fold to a simpler setcc
15165 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
15166 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
15167 N->getOperand(0), Simp.getOperand(2),
15168 Simp.getOperand(0), Simp.getOperand(1),
15169 N->getOperand(4));
15170
15171 return SDValue();
15172}
15173
15174static bool getCombineLoadStoreParts(SDNode *N, unsigned Inc, unsigned Dec,
15175 bool &IsLoad, bool &IsMasked, SDValue &Ptr,
15176 const TargetLowering &TLI) {
15177 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
15178 if (LD->isIndexed())
15179 return false;
15180 EVT VT = LD->getMemoryVT();
15181 if (!TLI.isIndexedLoadLegal(Inc, VT) && !TLI.isIndexedLoadLegal(Dec, VT))
15182 return false;
15183 Ptr = LD->getBasePtr();
15184 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
15185 if (ST->isIndexed())
15186 return false;
15187 EVT VT = ST->getMemoryVT();
15188 if (!TLI.isIndexedStoreLegal(Inc, VT) && !TLI.isIndexedStoreLegal(Dec, VT))
15189 return false;
15190 Ptr = ST->getBasePtr();
15191 IsLoad = false;
15192 } else if (MaskedLoadSDNode *LD = dyn_cast<MaskedLoadSDNode>(N)) {
15193 if (LD->isIndexed())
15194 return false;
15195 EVT VT = LD->getMemoryVT();
15196 if (!TLI.isIndexedMaskedLoadLegal(Inc, VT) &&
15197 !TLI.isIndexedMaskedLoadLegal(Dec, VT))
15198 return false;
15199 Ptr = LD->getBasePtr();
15200 IsMasked = true;
15201 } else if (MaskedStoreSDNode *ST = dyn_cast<MaskedStoreSDNode>(N)) {
15202 if (ST->isIndexed())
15203 return false;
15204 EVT VT = ST->getMemoryVT();
15205 if (!TLI.isIndexedMaskedStoreLegal(Inc, VT) &&
15206 !TLI.isIndexedMaskedStoreLegal(Dec, VT))
15207 return false;
15208 Ptr = ST->getBasePtr();
15209 IsLoad = false;
15210 IsMasked = true;
15211 } else {
15212 return false;
15213 }
15214 return true;
15215}
15216
15217/// Try turning a load/store into a pre-indexed load/store when the base
15218/// pointer is an add or subtract and it has other uses besides the load/store.
15219/// After the transformation, the new indexed load/store has effectively folded
15220/// the add/subtract in and all of its other uses are redirected to the
15221/// new load/store.
15222bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
15223 if (Level < AfterLegalizeDAG)
15224 return false;
15225
15226 bool IsLoad = true;
15227 bool IsMasked = false;
15228 SDValue Ptr;
15229 if (!getCombineLoadStoreParts(N, ISD::PRE_INC, ISD::PRE_DEC, IsLoad, IsMasked,
15230 Ptr, TLI))
15231 return false;
15232
15233 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
15234 // out. There is no reason to make this a preinc/predec.
15235 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
15236 Ptr.getNode()->hasOneUse())
15237 return false;
15238
15239 // Ask the target to do addressing mode selection.
15240 SDValue BasePtr;
15241 SDValue Offset;
15242 ISD::MemIndexedMode AM = ISD::UNINDEXED;
15243 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
15244 return false;
15245
15246 // Backends without true r+i pre-indexed forms may need to pass a
15247 // constant base with a variable offset so that constant coercion
15248 // will work with the patterns in canonical form.
15249 bool Swapped = false;
15250 if (isa<ConstantSDNode>(BasePtr)) {
15251 std::swap(BasePtr, Offset);
15252 Swapped = true;
15253 }
15254
15255 // Don't create a indexed load / store with zero offset.
15256 if (isNullConstant(Offset))
15257 return false;
15258
15259 // Try turning it into a pre-indexed load / store except when:
15260 // 1) The new base ptr is a frame index.
15261 // 2) If N is a store and the new base ptr is either the same as or is a
15262 // predecessor of the value being stored.
15263 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
15264 // that would create a cycle.
15265 // 4) All uses are load / store ops that use it as old base ptr.
15266
15267 // Check #1. Preinc'ing a frame index would require copying the stack pointer
15268 // (plus the implicit offset) to a register to preinc anyway.
15269 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
15270 return false;
15271
15272 // Check #2.
15273 if (!IsLoad) {
15274 SDValue Val = IsMasked ? cast<MaskedStoreSDNode>(N)->getValue()
15275 : cast<StoreSDNode>(N)->getValue();
15276
15277 // Would require a copy.
15278 if (Val == BasePtr)
15279 return false;
15280
15281 // Would create a cycle.
15282 if (Val == Ptr || Ptr->isPredecessorOf(Val.getNode()))
15283 return false;
15284 }
15285
15286 // Caches for hasPredecessorHelper.
15287 SmallPtrSet<const SDNode *, 32> Visited;
15288 SmallVector<const SDNode *, 16> Worklist;
15289 Worklist.push_back(N);
15290
15291 // If the offset is a constant, there may be other adds of constants that
15292 // can be folded with this one. We should do this to avoid having to keep
15293 // a copy of the original base pointer.
15294 SmallVector<SDNode *, 16> OtherUses;
15295 if (isa<ConstantSDNode>(Offset))
15296 for (SDNode::use_iterator UI = BasePtr.getNode()->use_begin(),
15297 UE = BasePtr.getNode()->use_end();
15298 UI != UE; ++UI) {
15299 SDUse &Use = UI.getUse();
15300 // Skip the use that is Ptr and uses of other results from BasePtr's
15301 // node (important for nodes that return multiple results).
15302 if (Use.getUser() == Ptr.getNode() || Use != BasePtr)
15303 continue;
15304
15305 if (SDNode::hasPredecessorHelper(Use.getUser(), Visited, Worklist))
15306 continue;
15307
15308 if (Use.getUser()->getOpcode() != ISD::ADD &&
15309 Use.getUser()->getOpcode() != ISD::SUB) {
15310 OtherUses.clear();
15311 break;
15312 }
15313
15314 SDValue Op1 = Use.getUser()->getOperand((UI.getOperandNo() + 1) & 1);
15315 if (!isa<ConstantSDNode>(Op1)) {
15316 OtherUses.clear();
15317 break;
15318 }
15319
15320 // FIXME: In some cases, we can be smarter about this.
15321 if (Op1.getValueType() != Offset.getValueType()) {
15322 OtherUses.clear();
15323 break;
15324 }
15325
15326 OtherUses.push_back(Use.getUser());
15327 }
15328
15329 if (Swapped)
15330 std::swap(BasePtr, Offset);
15331
15332 // Now check for #3 and #4.
15333 bool RealUse = false;
15334
15335 for (SDNode *Use : Ptr.getNode()->uses()) {
15336 if (Use == N)
15337 continue;
15338 if (SDNode::hasPredecessorHelper(Use, Visited, Worklist))
15339 return false;
15340
15341 // If Ptr may be folded in addressing mode of other use, then it's
15342 // not profitable to do this transformation.
15343 if (!canFoldInAddressingMode(Ptr.getNode(), Use, DAG, TLI))
15344 RealUse = true;
15345 }
15346
15347 if (!RealUse)
15348 return false;
15349
15350 SDValue Result;
15351 if (!IsMasked) {
15352 if (IsLoad)
15353 Result = DAG.getIndexedLoad(SDValue(N, 0), SDLoc(N), BasePtr, Offset, AM);
15354 else
15355 Result =
15356 DAG.getIndexedStore(SDValue(N, 0), SDLoc(N), BasePtr, Offset, AM);
15357 } else {
15358 if (IsLoad)
15359 Result = DAG.getIndexedMaskedLoad(SDValue(N, 0), SDLoc(N), BasePtr,
15360 Offset, AM);
15361 else
15362 Result = DAG.getIndexedMaskedStore(SDValue(N, 0), SDLoc(N), BasePtr,
15363 Offset, AM);
15364 }
15365 ++PreIndexedNodes;
15366 ++NodesCombined;
15367 LLVM_DEBUG(dbgs() << "\nReplacing.4 "; N->dump(&DAG); dbgs() << "\nWith: ";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nReplacing.4 "; N->dump
(&DAG); dbgs() << "\nWith: "; Result.getNode()->
dump(&DAG); dbgs() << '\n'; } } while (false)
15368 Result.getNode()->dump(&DAG); dbgs() << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nReplacing.4 "; N->dump
(&DAG); dbgs() << "\nWith: "; Result.getNode()->
dump(&DAG); dbgs() << '\n'; } } while (false)
;
15369 WorklistRemover DeadNodes(*this);
15370 if (IsLoad) {
15371 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
15372 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
15373 } else {
15374 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
15375 }
15376
15377 // Finally, since the node is now dead, remove it from the graph.
15378 deleteAndRecombine(N);
15379
15380 if (Swapped)
15381 std::swap(BasePtr, Offset);
15382
15383 // Replace other uses of BasePtr that can be updated to use Ptr
15384 for (unsigned i = 0, e = OtherUses.size(); i != e; ++i) {
15385 unsigned OffsetIdx = 1;
15386 if (OtherUses[i]->getOperand(OffsetIdx).getNode() == BasePtr.getNode())
15387 OffsetIdx = 0;
15388 assert(OtherUses[i]->getOperand(!OffsetIdx).getNode() ==(static_cast <bool> (OtherUses[i]->getOperand(!OffsetIdx
).getNode() == BasePtr.getNode() && "Expected BasePtr operand"
) ? void (0) : __assert_fail ("OtherUses[i]->getOperand(!OffsetIdx).getNode() == BasePtr.getNode() && \"Expected BasePtr operand\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 15389, __extension__ __PRETTY_FUNCTION__))
15389 BasePtr.getNode() && "Expected BasePtr operand")(static_cast <bool> (OtherUses[i]->getOperand(!OffsetIdx
).getNode() == BasePtr.getNode() && "Expected BasePtr operand"
) ? void (0) : __assert_fail ("OtherUses[i]->getOperand(!OffsetIdx).getNode() == BasePtr.getNode() && \"Expected BasePtr operand\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 15389, __extension__ __PRETTY_FUNCTION__))
;
15390
15391 // We need to replace ptr0 in the following expression:
15392 // x0 * offset0 + y0 * ptr0 = t0
15393 // knowing that
15394 // x1 * offset1 + y1 * ptr0 = t1 (the indexed load/store)
15395 //
15396 // where x0, x1, y0 and y1 in {-1, 1} are given by the types of the
15397 // indexed load/store and the expression that needs to be re-written.
15398 //
15399 // Therefore, we have:
15400 // t0 = (x0 * offset0 - x1 * y0 * y1 *offset1) + (y0 * y1) * t1
15401
15402 auto *CN = cast<ConstantSDNode>(OtherUses[i]->getOperand(OffsetIdx));
15403 const APInt &Offset0 = CN->getAPIntValue();
15404 const APInt &Offset1 = cast<ConstantSDNode>(Offset)->getAPIntValue();
15405 int X0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 1) ? -1 : 1;
15406 int Y0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 0) ? -1 : 1;
15407 int X1 = (AM == ISD::PRE_DEC && !Swapped) ? -1 : 1;
15408 int Y1 = (AM == ISD::PRE_DEC && Swapped) ? -1 : 1;
15409
15410 unsigned Opcode = (Y0 * Y1 < 0) ? ISD::SUB : ISD::ADD;
15411
15412 APInt CNV = Offset0;
15413 if (X0 < 0) CNV = -CNV;
15414 if (X1 * Y0 * Y1 < 0) CNV = CNV + Offset1;
15415 else CNV = CNV - Offset1;
15416
15417 SDLoc DL(OtherUses[i]);
15418
15419 // We can now generate the new expression.
15420 SDValue NewOp1 = DAG.getConstant(CNV, DL, CN->getValueType(0));
15421 SDValue NewOp2 = Result.getValue(IsLoad ? 1 : 0);
15422
15423 SDValue NewUse = DAG.getNode(Opcode,
15424 DL,
15425 OtherUses[i]->getValueType(0), NewOp1, NewOp2);
15426 DAG.ReplaceAllUsesOfValueWith(SDValue(OtherUses[i], 0), NewUse);
15427 deleteAndRecombine(OtherUses[i]);
15428 }
15429
15430 // Replace the uses of Ptr with uses of the updated base value.
15431 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(IsLoad ? 1 : 0));
15432 deleteAndRecombine(Ptr.getNode());
15433 AddToWorklist(Result.getNode());
15434
15435 return true;
15436}
15437
15438static bool shouldCombineToPostInc(SDNode *N, SDValue Ptr, SDNode *PtrUse,
15439 SDValue &BasePtr, SDValue &Offset,
15440 ISD::MemIndexedMode &AM,
15441 SelectionDAG &DAG,
15442 const TargetLowering &TLI) {
15443 if (PtrUse == N ||
15444 (PtrUse->getOpcode() != ISD::ADD && PtrUse->getOpcode() != ISD::SUB))
15445 return false;
15446
15447 if (!TLI.getPostIndexedAddressParts(N, PtrUse, BasePtr, Offset, AM, DAG))
15448 return false;
15449
15450 // Don't create a indexed load / store with zero offset.
15451 if (isNullConstant(Offset))
15452 return false;
15453
15454 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
15455 return false;
15456
15457 SmallPtrSet<const SDNode *, 32> Visited;
15458 for (SDNode *Use : BasePtr.getNode()->uses()) {
15459 if (Use == Ptr.getNode())
15460 continue;
15461
15462 // No if there's a later user which could perform the index instead.
15463 if (isa<MemSDNode>(Use)) {
15464 bool IsLoad = true;
15465 bool IsMasked = false;
15466 SDValue OtherPtr;
15467 if (getCombineLoadStoreParts(Use, ISD::POST_INC, ISD::POST_DEC, IsLoad,
15468 IsMasked, OtherPtr, TLI)) {
15469 SmallVector<const SDNode *, 2> Worklist;
15470 Worklist.push_back(Use);
15471 if (SDNode::hasPredecessorHelper(N, Visited, Worklist))
15472 return false;
15473 }
15474 }
15475
15476 // If all the uses are load / store addresses, then don't do the
15477 // transformation.
15478 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB) {
15479 for (SDNode *UseUse : Use->uses())
15480 if (canFoldInAddressingMode(Use, UseUse, DAG, TLI))
15481 return false;
15482 }
15483 }
15484 return true;
15485}
15486
15487static SDNode *getPostIndexedLoadStoreOp(SDNode *N, bool &IsLoad,
15488 bool &IsMasked, SDValue &Ptr,
15489 SDValue &BasePtr, SDValue &Offset,
15490 ISD::MemIndexedMode &AM,
15491 SelectionDAG &DAG,
15492 const TargetLowering &TLI) {
15493 if (!getCombineLoadStoreParts(N, ISD::POST_INC, ISD::POST_DEC, IsLoad,
15494 IsMasked, Ptr, TLI) ||
15495 Ptr.getNode()->hasOneUse())
15496 return nullptr;
15497
15498 // Try turning it into a post-indexed load / store except when
15499 // 1) All uses are load / store ops that use it as base ptr (and
15500 // it may be folded as addressing mmode).
15501 // 2) Op must be independent of N, i.e. Op is neither a predecessor
15502 // nor a successor of N. Otherwise, if Op is folded that would
15503 // create a cycle.
15504 for (SDNode *Op : Ptr->uses()) {
15505 // Check for #1.
15506 if (!shouldCombineToPostInc(N, Ptr, Op, BasePtr, Offset, AM, DAG, TLI))
15507 continue;
15508
15509 // Check for #2.
15510 SmallPtrSet<const SDNode *, 32> Visited;
15511 SmallVector<const SDNode *, 8> Worklist;
15512 // Ptr is predecessor to both N and Op.
15513 Visited.insert(Ptr.getNode());
15514 Worklist.push_back(N);
15515 Worklist.push_back(Op);
15516 if (!SDNode::hasPredecessorHelper(N, Visited, Worklist) &&
15517 !SDNode::hasPredecessorHelper(Op, Visited, Worklist))
15518 return Op;
15519 }
15520 return nullptr;
15521}
15522
15523/// Try to combine a load/store with a add/sub of the base pointer node into a
15524/// post-indexed load/store. The transformation folded the add/subtract into the
15525/// new indexed load/store effectively and all of its uses are redirected to the
15526/// new load/store.
15527bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
15528 if (Level < AfterLegalizeDAG)
15529 return false;
15530
15531 bool IsLoad = true;
15532 bool IsMasked = false;
15533 SDValue Ptr;
15534 SDValue BasePtr;
15535 SDValue Offset;
15536 ISD::MemIndexedMode AM = ISD::UNINDEXED;
15537 SDNode *Op = getPostIndexedLoadStoreOp(N, IsLoad, IsMasked, Ptr, BasePtr,
15538 Offset, AM, DAG, TLI);
15539 if (!Op)
15540 return false;
15541
15542 SDValue Result;
15543 if (!IsMasked)
15544 Result = IsLoad ? DAG.getIndexedLoad(SDValue(N, 0), SDLoc(N), BasePtr,
15545 Offset, AM)
15546 : DAG.getIndexedStore(SDValue(N, 0), SDLoc(N),
15547 BasePtr, Offset, AM);
15548 else
15549 Result = IsLoad ? DAG.getIndexedMaskedLoad(SDValue(N, 0), SDLoc(N),
15550 BasePtr, Offset, AM)
15551 : DAG.getIndexedMaskedStore(SDValue(N, 0), SDLoc(N),
15552 BasePtr, Offset, AM);
15553 ++PostIndexedNodes;
15554 ++NodesCombined;
15555 LLVM_DEBUG(dbgs() << "\nReplacing.5 "; N->dump(&DAG);do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nReplacing.5 "; N->dump
(&DAG); dbgs() << "\nWith: "; Result.getNode()->
dump(&DAG); dbgs() << '\n'; } } while (false)
15556 dbgs() << "\nWith: "; Result.getNode()->dump(&DAG);do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nReplacing.5 "; N->dump
(&DAG); dbgs() << "\nWith: "; Result.getNode()->
dump(&DAG); dbgs() << '\n'; } } while (false)
15557 dbgs() << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nReplacing.5 "; N->dump
(&DAG); dbgs() << "\nWith: "; Result.getNode()->
dump(&DAG); dbgs() << '\n'; } } while (false)
;
15558 WorklistRemover DeadNodes(*this);
15559 if (IsLoad) {
15560 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
15561 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
15562 } else {
15563 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
15564 }
15565
15566 // Finally, since the node is now dead, remove it from the graph.
15567 deleteAndRecombine(N);
15568
15569 // Replace the uses of Use with uses of the updated base value.
15570 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
15571 Result.getValue(IsLoad ? 1 : 0));
15572 deleteAndRecombine(Op);
15573 return true;
15574}
15575
15576/// Return the base-pointer arithmetic from an indexed \p LD.
15577SDValue DAGCombiner::SplitIndexingFromLoad(LoadSDNode *LD) {
15578 ISD::MemIndexedMode AM = LD->getAddressingMode();
15579 assert(AM != ISD::UNINDEXED)(static_cast <bool> (AM != ISD::UNINDEXED) ? void (0) :
__assert_fail ("AM != ISD::UNINDEXED", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 15579, __extension__ __PRETTY_FUNCTION__))
;
15580 SDValue BP = LD->getOperand(1);
15581 SDValue Inc = LD->getOperand(2);
15582
15583 // Some backends use TargetConstants for load offsets, but don't expect
15584 // TargetConstants in general ADD nodes. We can convert these constants into
15585 // regular Constants (if the constant is not opaque).
15586 assert((Inc.getOpcode() != ISD::TargetConstant ||(static_cast <bool> ((Inc.getOpcode() != ISD::TargetConstant
|| !cast<ConstantSDNode>(Inc)->isOpaque()) &&
"Cannot split out indexing using opaque target constants") ?
void (0) : __assert_fail ("(Inc.getOpcode() != ISD::TargetConstant || !cast<ConstantSDNode>(Inc)->isOpaque()) && \"Cannot split out indexing using opaque target constants\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 15588, __extension__ __PRETTY_FUNCTION__))
15587 !cast<ConstantSDNode>(Inc)->isOpaque()) &&(static_cast <bool> ((Inc.getOpcode() != ISD::TargetConstant
|| !cast<ConstantSDNode>(Inc)->isOpaque()) &&
"Cannot split out indexing using opaque target constants") ?
void (0) : __assert_fail ("(Inc.getOpcode() != ISD::TargetConstant || !cast<ConstantSDNode>(Inc)->isOpaque()) && \"Cannot split out indexing using opaque target constants\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 15588, __extension__ __PRETTY_FUNCTION__))
15588 "Cannot split out indexing using opaque target constants")(static_cast <bool> ((Inc.getOpcode() != ISD::TargetConstant
|| !cast<ConstantSDNode>(Inc)->isOpaque()) &&
"Cannot split out indexing using opaque target constants") ?
void (0) : __assert_fail ("(Inc.getOpcode() != ISD::TargetConstant || !cast<ConstantSDNode>(Inc)->isOpaque()) && \"Cannot split out indexing using opaque target constants\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 15588, __extension__ __PRETTY_FUNCTION__))
;
15589 if (Inc.getOpcode() == ISD::TargetConstant) {
15590 ConstantSDNode *ConstInc = cast<ConstantSDNode>(Inc);
15591 Inc = DAG.getConstant(*ConstInc->getConstantIntValue(), SDLoc(Inc),
15592 ConstInc->getValueType(0));
15593 }
15594
15595 unsigned Opc =
15596 (AM == ISD::PRE_INC || AM == ISD::POST_INC ? ISD::ADD : ISD::SUB);
15597 return DAG.getNode(Opc, SDLoc(LD), BP.getSimpleValueType(), BP, Inc);
15598}
15599
15600static inline ElementCount numVectorEltsOrZero(EVT T) {
15601 return T.isVector() ? T.getVectorElementCount() : ElementCount::getFixed(0);
15602}
15603
15604bool DAGCombiner::getTruncatedStoreValue(StoreSDNode *ST, SDValue &Val) {
15605 Val = ST->getValue();
15606 EVT STType = Val.getValueType();
15607 EVT STMemType = ST->getMemoryVT();
15608 if (STType == STMemType)
15609 return true;
15610 if (isTypeLegal(STMemType))
15611 return false; // fail.
15612 if (STType.isFloatingPoint() && STMemType.isFloatingPoint() &&
15613 TLI.isOperationLegal(ISD::FTRUNC, STMemType)) {
15614 Val = DAG.getNode(ISD::FTRUNC, SDLoc(ST), STMemType, Val);
15615 return true;
15616 }
15617 if (numVectorEltsOrZero(STType) == numVectorEltsOrZero(STMemType) &&
15618 STType.isInteger() && STMemType.isInteger()) {
15619 Val = DAG.getNode(ISD::TRUNCATE, SDLoc(ST), STMemType, Val);
15620 return true;
15621 }
15622 if (STType.getSizeInBits() == STMemType.getSizeInBits()) {
15623 Val = DAG.getBitcast(STMemType, Val);
15624 return true;
15625 }
15626 return false; // fail.
15627}
15628
15629bool DAGCombiner::extendLoadedValueToExtension(LoadSDNode *LD, SDValue &Val) {
15630 EVT LDMemType = LD->getMemoryVT();
15631 EVT LDType = LD->getValueType(0);
15632 assert(Val.getValueType() == LDMemType &&(static_cast <bool> (Val.getValueType() == LDMemType &&
"Attempting to extend value of non-matching type") ? void (0
) : __assert_fail ("Val.getValueType() == LDMemType && \"Attempting to extend value of non-matching type\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 15633, __extension__ __PRETTY_FUNCTION__))
15633 "Attempting to extend value of non-matching type")(static_cast <bool> (Val.getValueType() == LDMemType &&
"Attempting to extend value of non-matching type") ? void (0
) : __assert_fail ("Val.getValueType() == LDMemType && \"Attempting to extend value of non-matching type\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 15633, __extension__ __PRETTY_FUNCTION__))
;
15634 if (LDType == LDMemType)
15635 return true;
15636 if (LDMemType.isInteger() && LDType.isInteger()) {
15637 switch (LD->getExtensionType()) {
15638 case ISD::NON_EXTLOAD:
15639 Val = DAG.getBitcast(LDType, Val);
15640 return true;
15641 case ISD::EXTLOAD:
15642 Val = DAG.getNode(ISD::ANY_EXTEND, SDLoc(LD), LDType, Val);
15643 return true;
15644 case ISD::SEXTLOAD:
15645 Val = DAG.getNode(ISD::SIGN_EXTEND, SDLoc(LD), LDType, Val);
15646 return true;
15647 case ISD::ZEXTLOAD:
15648 Val = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(LD), LDType, Val);
15649 return true;
15650 }
15651 }
15652 return false;
15653}
15654
15655SDValue DAGCombiner::ForwardStoreValueToDirectLoad(LoadSDNode *LD) {
15656 if (OptLevel == CodeGenOpt::None || !LD->isSimple())
15657 return SDValue();
15658 SDValue Chain = LD->getOperand(0);
15659 StoreSDNode *ST = dyn_cast<StoreSDNode>(Chain.getNode());
15660 // TODO: Relax this restriction for unordered atomics (see D66309)
15661 if (!ST || !ST->isSimple())
15662 return SDValue();
15663
15664 EVT LDType = LD->getValueType(0);
15665 EVT LDMemType = LD->getMemoryVT();
15666 EVT STMemType = ST->getMemoryVT();
15667 EVT STType = ST->getValue().getValueType();
15668
15669 // There are two cases to consider here:
15670 // 1. The store is fixed width and the load is scalable. In this case we
15671 // don't know at compile time if the store completely envelops the load
15672 // so we abandon the optimisation.
15673 // 2. The store is scalable and the load is fixed width. We could
15674 // potentially support a limited number of cases here, but there has been
15675 // no cost-benefit analysis to prove it's worth it.
15676 bool LdStScalable = LDMemType.isScalableVector();
15677 if (LdStScalable != STMemType.isScalableVector())
15678 return SDValue();
15679
15680 // If we are dealing with scalable vectors on a big endian platform the
15681 // calculation of offsets below becomes trickier, since we do not know at
15682 // compile time the absolute size of the vector. Until we've done more
15683 // analysis on big-endian platforms it seems better to bail out for now.
15684 if (LdStScalable && DAG.getDataLayout().isBigEndian())
15685 return SDValue();
15686
15687 BaseIndexOffset BasePtrLD = BaseIndexOffset::match(LD, DAG);
15688 BaseIndexOffset BasePtrST = BaseIndexOffset::match(ST, DAG);
15689 int64_t Offset;
15690 if (!BasePtrST.equalBaseIndex(BasePtrLD, DAG, Offset))
15691 return SDValue();
15692
15693 // Normalize for Endianness. After this Offset=0 will denote that the least
15694 // significant bit in the loaded value maps to the least significant bit in
15695 // the stored value). With Offset=n (for n > 0) the loaded value starts at the
15696 // n:th least significant byte of the stored value.
15697 if (DAG.getDataLayout().isBigEndian())
15698 Offset = ((int64_t)STMemType.getStoreSizeInBits().getFixedSize() -
15699 (int64_t)LDMemType.getStoreSizeInBits().getFixedSize()) /
15700 8 -
15701 Offset;
15702
15703 // Check that the stored value cover all bits that are loaded.
15704 bool STCoversLD;
15705
15706 TypeSize LdMemSize = LDMemType.getSizeInBits();
15707 TypeSize StMemSize = STMemType.getSizeInBits();
15708 if (LdStScalable)
15709 STCoversLD = (Offset == 0) && LdMemSize == StMemSize;
15710 else
15711 STCoversLD = (Offset >= 0) && (Offset * 8 + LdMemSize.getFixedSize() <=
15712 StMemSize.getFixedSize());
15713
15714 auto ReplaceLd = [&](LoadSDNode *LD, SDValue Val, SDValue Chain) -> SDValue {
15715 if (LD->isIndexed()) {
15716 // Cannot handle opaque target constants and we must respect the user's
15717 // request not to split indexes from loads.
15718 if (!canSplitIdx(LD))
15719 return SDValue();
15720 SDValue Idx = SplitIndexingFromLoad(LD);
15721 SDValue Ops[] = {Val, Idx, Chain};
15722 return CombineTo(LD, Ops, 3);
15723 }
15724 return CombineTo(LD, Val, Chain);
15725 };
15726
15727 if (!STCoversLD)
15728 return SDValue();
15729
15730 // Memory as copy space (potentially masked).
15731 if (Offset == 0 && LDType == STType && STMemType == LDMemType) {
15732 // Simple case: Direct non-truncating forwarding
15733 if (LDType.getSizeInBits() == LdMemSize)
15734 return ReplaceLd(LD, ST->getValue(), Chain);
15735 // Can we model the truncate and extension with an and mask?
15736 if (STType.isInteger() && LDMemType.isInteger() && !STType.isVector() &&
15737 !LDMemType.isVector() && LD->getExtensionType() != ISD::SEXTLOAD) {
15738 // Mask to size of LDMemType
15739 auto Mask =
15740 DAG.getConstant(APInt::getLowBitsSet(STType.getFixedSizeInBits(),
15741 StMemSize.getFixedSize()),
15742 SDLoc(ST), STType);
15743 auto Val = DAG.getNode(ISD::AND, SDLoc(LD), LDType, ST->getValue(), Mask);
15744 return ReplaceLd(LD, Val, Chain);
15745 }
15746 }
15747
15748 // TODO: Deal with nonzero offset.
15749 if (LD->getBasePtr().isUndef() || Offset != 0)
15750 return SDValue();
15751 // Model necessary truncations / extenstions.
15752 SDValue Val;
15753 // Truncate Value To Stored Memory Size.
15754 do {
15755 if (!getTruncatedStoreValue(ST, Val))
15756 continue;
15757 if (!isTypeLegal(LDMemType))
15758 continue;
15759 if (STMemType != LDMemType) {
15760 // TODO: Support vectors? This requires extract_subvector/bitcast.
15761 if (!STMemType.isVector() && !LDMemType.isVector() &&
15762 STMemType.isInteger() && LDMemType.isInteger())
15763 Val = DAG.getNode(ISD::TRUNCATE, SDLoc(LD), LDMemType, Val);
15764 else
15765 continue;
15766 }
15767 if (!extendLoadedValueToExtension(LD, Val))
15768 continue;
15769 return ReplaceLd(LD, Val, Chain);
15770 } while (false);
15771
15772 // On failure, cleanup dead nodes we may have created.
15773 if (Val->use_empty())
15774 deleteAndRecombine(Val.getNode());
15775 return SDValue();
15776}
15777
15778SDValue DAGCombiner::visitLOAD(SDNode *N) {
15779 LoadSDNode *LD = cast<LoadSDNode>(N);
15780 SDValue Chain = LD->getChain();
15781 SDValue Ptr = LD->getBasePtr();
15782
15783 // If load is not volatile and there are no uses of the loaded value (and
15784 // the updated indexed value in case of indexed loads), change uses of the
15785 // chain value into uses of the chain input (i.e. delete the dead load).
15786 // TODO: Allow this for unordered atomics (see D66309)
15787 if (LD->isSimple()) {
15788 if (N->getValueType(1) == MVT::Other) {
15789 // Unindexed loads.
15790 if (!N->hasAnyUseOfValue(0)) {
15791 // It's not safe to use the two value CombineTo variant here. e.g.
15792 // v1, chain2 = load chain1, loc
15793 // v2, chain3 = load chain2, loc
15794 // v3 = add v2, c
15795 // Now we replace use of chain2 with chain1. This makes the second load
15796 // isomorphic to the one we are deleting, and thus makes this load live.
15797 LLVM_DEBUG(dbgs() << "\nReplacing.6 "; N->dump(&DAG);do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nReplacing.6 "; N->dump
(&DAG); dbgs() << "\nWith chain: "; Chain.getNode()
->dump(&DAG); dbgs() << "\n"; } } while (false)
15798 dbgs() << "\nWith chain: "; Chain.getNode()->dump(&DAG);do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nReplacing.6 "; N->dump
(&DAG); dbgs() << "\nWith chain: "; Chain.getNode()
->dump(&DAG); dbgs() << "\n"; } } while (false)
15799 dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nReplacing.6 "; N->dump
(&DAG); dbgs() << "\nWith chain: "; Chain.getNode()
->dump(&DAG); dbgs() << "\n"; } } while (false)
;
15800 WorklistRemover DeadNodes(*this);
15801 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
15802 AddUsersToWorklist(Chain.getNode());
15803 if (N->use_empty())
15804 deleteAndRecombine(N);
15805
15806 return SDValue(N, 0); // Return N so it doesn't get rechecked!
15807 }
15808 } else {
15809 // Indexed loads.
15810 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?")(static_cast <bool> (N->getValueType(2) == MVT::Other
&& "Malformed indexed loads?") ? void (0) : __assert_fail
("N->getValueType(2) == MVT::Other && \"Malformed indexed loads?\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 15810, __extension__ __PRETTY_FUNCTION__))
;
15811
15812 // If this load has an opaque TargetConstant offset, then we cannot split
15813 // the indexing into an add/sub directly (that TargetConstant may not be
15814 // valid for a different type of node, and we cannot convert an opaque
15815 // target constant into a regular constant).
15816 bool CanSplitIdx = canSplitIdx(LD);
15817
15818 if (!N->hasAnyUseOfValue(0) && (CanSplitIdx || !N->hasAnyUseOfValue(1))) {
15819 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
15820 SDValue Index;
15821 if (N->hasAnyUseOfValue(1) && CanSplitIdx) {
15822 Index = SplitIndexingFromLoad(LD);
15823 // Try to fold the base pointer arithmetic into subsequent loads and
15824 // stores.
15825 AddUsersToWorklist(N);
15826 } else
15827 Index = DAG.getUNDEF(N->getValueType(1));
15828 LLVM_DEBUG(dbgs() << "\nReplacing.7 "; N->dump(&DAG);do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nReplacing.7 "; N->dump
(&DAG); dbgs() << "\nWith: "; Undef.getNode()->dump
(&DAG); dbgs() << " and 2 other values\n"; } } while
(false)
15829 dbgs() << "\nWith: "; Undef.getNode()->dump(&DAG);do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nReplacing.7 "; N->dump
(&DAG); dbgs() << "\nWith: "; Undef.getNode()->dump
(&DAG); dbgs() << " and 2 other values\n"; } } while
(false)
15830 dbgs() << " and 2 other values\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nReplacing.7 "; N->dump
(&DAG); dbgs() << "\nWith: "; Undef.getNode()->dump
(&DAG); dbgs() << " and 2 other values\n"; } } while
(false)
;
15831 WorklistRemover DeadNodes(*this);
15832 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef);
15833 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Index);
15834 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain);
15835 deleteAndRecombine(N);
15836 return SDValue(N, 0); // Return N so it doesn't get rechecked!
15837 }
15838 }
15839 }
15840
15841 // If this load is directly stored, replace the load value with the stored
15842 // value.
15843 if (auto V = ForwardStoreValueToDirectLoad(LD))
15844 return V;
15845
15846 // Try to infer better alignment information than the load already has.
15847 if (OptLevel != CodeGenOpt::None && LD->isUnindexed() && !LD->isAtomic()) {
15848 if (MaybeAlign Alignment = DAG.InferPtrAlign(Ptr)) {
15849 if (*Alignment > LD->getAlign() &&
15850 isAligned(*Alignment, LD->getSrcValueOffset())) {
15851 SDValue NewLoad = DAG.getExtLoad(
15852 LD->getExtensionType(), SDLoc(N), LD->getValueType(0), Chain, Ptr,
15853 LD->getPointerInfo(), LD->getMemoryVT(), *Alignment,
15854 LD->getMemOperand()->getFlags(), LD->getAAInfo());
15855 // NewLoad will always be N as we are only refining the alignment
15856 assert(NewLoad.getNode() == N)(static_cast <bool> (NewLoad.getNode() == N) ? void (0)
: __assert_fail ("NewLoad.getNode() == N", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 15856, __extension__ __PRETTY_FUNCTION__))
;
15857 (void)NewLoad;
15858 }
15859 }
15860 }
15861
15862 if (LD->isUnindexed()) {
15863 // Walk up chain skipping non-aliasing memory nodes.
15864 SDValue BetterChain = FindBetterChain(LD, Chain);
15865
15866 // If there is a better chain.
15867 if (Chain != BetterChain) {
15868 SDValue ReplLoad;
15869
15870 // Replace the chain to void dependency.
15871 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
15872 ReplLoad = DAG.getLoad(N->getValueType(0), SDLoc(LD),
15873 BetterChain, Ptr, LD->getMemOperand());
15874 } else {
15875 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD),
15876 LD->getValueType(0),
15877 BetterChain, Ptr, LD->getMemoryVT(),
15878 LD->getMemOperand());
15879 }
15880
15881 // Create token factor to keep old chain connected.
15882 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N),
15883 MVT::Other, Chain, ReplLoad.getValue(1));
15884
15885 // Replace uses with load result and token factor
15886 return CombineTo(N, ReplLoad.getValue(0), Token);
15887 }
15888 }
15889
15890 // Try transforming N to an indexed load.
15891 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
15892 return SDValue(N, 0);
15893
15894 // Try to slice up N to more direct loads if the slices are mapped to
15895 // different register banks or pairing can take place.
15896 if (SliceUpLoad(N))
15897 return SDValue(N, 0);
15898
15899 return SDValue();
15900}
15901
15902namespace {
15903
15904/// Helper structure used to slice a load in smaller loads.
15905/// Basically a slice is obtained from the following sequence:
15906/// Origin = load Ty1, Base
15907/// Shift = srl Ty1 Origin, CstTy Amount
15908/// Inst = trunc Shift to Ty2
15909///
15910/// Then, it will be rewritten into:
15911/// Slice = load SliceTy, Base + SliceOffset
15912/// [Inst = zext Slice to Ty2], only if SliceTy <> Ty2
15913///
15914/// SliceTy is deduced from the number of bits that are actually used to
15915/// build Inst.
15916struct LoadedSlice {
15917 /// Helper structure used to compute the cost of a slice.
15918 struct Cost {
15919 /// Are we optimizing for code size.
15920 bool ForCodeSize = false;
15921
15922 /// Various cost.
15923 unsigned Loads = 0;
15924 unsigned Truncates = 0;
15925 unsigned CrossRegisterBanksCopies = 0;
15926 unsigned ZExts = 0;
15927 unsigned Shift = 0;
15928
15929 explicit Cost(bool ForCodeSize) : ForCodeSize(ForCodeSize) {}
15930
15931 /// Get the cost of one isolated slice.
15932 Cost(const LoadedSlice &LS, bool ForCodeSize)
15933 : ForCodeSize(ForCodeSize), Loads(1) {
15934 EVT TruncType = LS.Inst->getValueType(0);
15935 EVT LoadedType = LS.getLoadedType();
15936 if (TruncType != LoadedType &&
15937 !LS.DAG->getTargetLoweringInfo().isZExtFree(LoadedType, TruncType))
15938 ZExts = 1;
15939 }
15940
15941 /// Account for slicing gain in the current cost.
15942 /// Slicing provide a few gains like removing a shift or a
15943 /// truncate. This method allows to grow the cost of the original
15944 /// load with the gain from this slice.
15945 void addSliceGain(const LoadedSlice &LS) {
15946 // Each slice saves a truncate.
15947 const TargetLowering &TLI = LS.DAG->getTargetLoweringInfo();
15948 if (!TLI.isTruncateFree(LS.Inst->getOperand(0).getValueType(),
15949 LS.Inst->getValueType(0)))
15950 ++Truncates;
15951 // If there is a shift amount, this slice gets rid of it.
15952 if (LS.Shift)
15953 ++Shift;
15954 // If this slice can merge a cross register bank copy, account for it.
15955 if (LS.canMergeExpensiveCrossRegisterBankCopy())
15956 ++CrossRegisterBanksCopies;
15957 }
15958
15959 Cost &operator+=(const Cost &RHS) {
15960 Loads += RHS.Loads;
15961 Truncates += RHS.Truncates;
15962 CrossRegisterBanksCopies += RHS.CrossRegisterBanksCopies;
15963 ZExts += RHS.ZExts;
15964 Shift += RHS.Shift;
15965 return *this;
15966 }
15967
15968 bool operator==(const Cost &RHS) const {
15969 return Loads == RHS.Loads && Truncates == RHS.Truncates &&
15970 CrossRegisterBanksCopies == RHS.CrossRegisterBanksCopies &&
15971 ZExts == RHS.ZExts && Shift == RHS.Shift;
15972 }
15973
15974 bool operator!=(const Cost &RHS) const { return !(*this == RHS); }
15975
15976 bool operator<(const Cost &RHS) const {
15977 // Assume cross register banks copies are as expensive as loads.
15978 // FIXME: Do we want some more target hooks?
15979 unsigned ExpensiveOpsLHS = Loads + CrossRegisterBanksCopies;
15980 unsigned ExpensiveOpsRHS = RHS.Loads + RHS.CrossRegisterBanksCopies;
15981 // Unless we are optimizing for code size, consider the
15982 // expensive operation first.
15983 if (!ForCodeSize && ExpensiveOpsLHS != ExpensiveOpsRHS)
15984 return ExpensiveOpsLHS < ExpensiveOpsRHS;
15985 return (Truncates + ZExts + Shift + ExpensiveOpsLHS) <
15986 (RHS.Truncates + RHS.ZExts + RHS.Shift + ExpensiveOpsRHS);
15987 }
15988
15989 bool operator>(const Cost &RHS) const { return RHS < *this; }
15990
15991 bool operator<=(const Cost &RHS) const { return !(RHS < *this); }
15992
15993 bool operator>=(const Cost &RHS) const { return !(*this < RHS); }
15994 };
15995
15996 // The last instruction that represent the slice. This should be a
15997 // truncate instruction.
15998 SDNode *Inst;
15999
16000 // The original load instruction.
16001 LoadSDNode *Origin;
16002
16003 // The right shift amount in bits from the original load.
16004 unsigned Shift;
16005
16006 // The DAG from which Origin came from.
16007 // This is used to get some contextual information about legal types, etc.
16008 SelectionDAG *DAG;
16009
16010 LoadedSlice(SDNode *Inst = nullptr, LoadSDNode *Origin = nullptr,
16011 unsigned Shift = 0, SelectionDAG *DAG = nullptr)
16012 : Inst(Inst), Origin(Origin), Shift(Shift), DAG(DAG) {}
16013
16014 /// Get the bits used in a chunk of bits \p BitWidth large.
16015 /// \return Result is \p BitWidth and has used bits set to 1 and
16016 /// not used bits set to 0.
16017 APInt getUsedBits() const {
16018 // Reproduce the trunc(lshr) sequence:
16019 // - Start from the truncated value.
16020 // - Zero extend to the desired bit width.
16021 // - Shift left.
16022 assert(Origin && "No original load to compare against.")(static_cast <bool> (Origin && "No original load to compare against."
) ? void (0) : __assert_fail ("Origin && \"No original load to compare against.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16022, __extension__ __PRETTY_FUNCTION__))
;
16023 unsigned BitWidth = Origin->getValueSizeInBits(0);
16024 assert(Inst && "This slice is not bound to an instruction")(static_cast <bool> (Inst && "This slice is not bound to an instruction"
) ? void (0) : __assert_fail ("Inst && \"This slice is not bound to an instruction\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16024, __extension__ __PRETTY_FUNCTION__))
;
16025 assert(Inst->getValueSizeInBits(0) <= BitWidth &&(static_cast <bool> (Inst->getValueSizeInBits(0) <=
BitWidth && "Extracted slice is bigger than the whole type!"
) ? void (0) : __assert_fail ("Inst->getValueSizeInBits(0) <= BitWidth && \"Extracted slice is bigger than the whole type!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16026, __extension__ __PRETTY_FUNCTION__))
16026 "Extracted slice is bigger than the whole type!")(static_cast <bool> (Inst->getValueSizeInBits(0) <=
BitWidth && "Extracted slice is bigger than the whole type!"
) ? void (0) : __assert_fail ("Inst->getValueSizeInBits(0) <= BitWidth && \"Extracted slice is bigger than the whole type!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16026, __extension__ __PRETTY_FUNCTION__))
;
16027 APInt UsedBits(Inst->getValueSizeInBits(0), 0);
16028 UsedBits.setAllBits();
16029 UsedBits = UsedBits.zext(BitWidth);
16030 UsedBits <<= Shift;
16031 return UsedBits;
16032 }
16033
16034 /// Get the size of the slice to be loaded in bytes.
16035 unsigned getLoadedSize() const {
16036 unsigned SliceSize = getUsedBits().countPopulation();
16037 assert(!(SliceSize & 0x7) && "Size is not a multiple of a byte.")(static_cast <bool> (!(SliceSize & 0x7) && "Size is not a multiple of a byte."
) ? void (0) : __assert_fail ("!(SliceSize & 0x7) && \"Size is not a multiple of a byte.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16037, __extension__ __PRETTY_FUNCTION__))
;
16038 return SliceSize / 8;
16039 }
16040
16041 /// Get the type that will be loaded for this slice.
16042 /// Note: This may not be the final type for the slice.
16043 EVT getLoadedType() const {
16044 assert(DAG && "Missing context")(static_cast <bool> (DAG && "Missing context") ?
void (0) : __assert_fail ("DAG && \"Missing context\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16044, __extension__ __PRETTY_FUNCTION__))
;
16045 LLVMContext &Ctxt = *DAG->getContext();
16046 return EVT::getIntegerVT(Ctxt, getLoadedSize() * 8);
16047 }
16048
16049 /// Get the alignment of the load used for this slice.
16050 Align getAlign() const {
16051 Align Alignment = Origin->getAlign();
16052 uint64_t Offset = getOffsetFromBase();
16053 if (Offset != 0)
16054 Alignment = commonAlignment(Alignment, Alignment.value() + Offset);
16055 return Alignment;
16056 }
16057
16058 /// Check if this slice can be rewritten with legal operations.
16059 bool isLegal() const {
16060 // An invalid slice is not legal.
16061 if (!Origin || !Inst || !DAG)
16062 return false;
16063
16064 // Offsets are for indexed load only, we do not handle that.
16065 if (!Origin->getOffset().isUndef())
16066 return false;
16067
16068 const TargetLowering &TLI = DAG->getTargetLoweringInfo();
16069
16070 // Check that the type is legal.
16071 EVT SliceType = getLoadedType();
16072 if (!TLI.isTypeLegal(SliceType))
16073 return false;
16074
16075 // Check that the load is legal for this type.
16076 if (!TLI.isOperationLegal(ISD::LOAD, SliceType))
16077 return false;
16078
16079 // Check that the offset can be computed.
16080 // 1. Check its type.
16081 EVT PtrType = Origin->getBasePtr().getValueType();
16082 if (PtrType == MVT::Untyped || PtrType.isExtended())
16083 return false;
16084
16085 // 2. Check that it fits in the immediate.
16086 if (!TLI.isLegalAddImmediate(getOffsetFromBase()))
16087 return false;
16088
16089 // 3. Check that the computation is legal.
16090 if (!TLI.isOperationLegal(ISD::ADD, PtrType))
16091 return false;
16092
16093 // Check that the zext is legal if it needs one.
16094 EVT TruncateType = Inst->getValueType(0);
16095 if (TruncateType != SliceType &&
16096 !TLI.isOperationLegal(ISD::ZERO_EXTEND, TruncateType))
16097 return false;
16098
16099 return true;
16100 }
16101
16102 /// Get the offset in bytes of this slice in the original chunk of
16103 /// bits.
16104 /// \pre DAG != nullptr.
16105 uint64_t getOffsetFromBase() const {
16106 assert(DAG && "Missing context.")(static_cast <bool> (DAG && "Missing context.")
? void (0) : __assert_fail ("DAG && \"Missing context.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16106, __extension__ __PRETTY_FUNCTION__))
;
16107 bool IsBigEndian = DAG->getDataLayout().isBigEndian();
16108 assert(!(Shift & 0x7) && "Shifts not aligned on Bytes are not supported.")(static_cast <bool> (!(Shift & 0x7) && "Shifts not aligned on Bytes are not supported."
) ? void (0) : __assert_fail ("!(Shift & 0x7) && \"Shifts not aligned on Bytes are not supported.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16108, __extension__ __PRETTY_FUNCTION__))
;
16109 uint64_t Offset = Shift / 8;
16110 unsigned TySizeInBytes = Origin->getValueSizeInBits(0) / 8;
16111 assert(!(Origin->getValueSizeInBits(0) & 0x7) &&(static_cast <bool> (!(Origin->getValueSizeInBits(0)
& 0x7) && "The size of the original loaded type is not a multiple of a"
" byte.") ? void (0) : __assert_fail ("!(Origin->getValueSizeInBits(0) & 0x7) && \"The size of the original loaded type is not a multiple of a\" \" byte.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16113, __extension__ __PRETTY_FUNCTION__))
16112 "The size of the original loaded type is not a multiple of a"(static_cast <bool> (!(Origin->getValueSizeInBits(0)
& 0x7) && "The size of the original loaded type is not a multiple of a"
" byte.") ? void (0) : __assert_fail ("!(Origin->getValueSizeInBits(0) & 0x7) && \"The size of the original loaded type is not a multiple of a\" \" byte.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16113, __extension__ __PRETTY_FUNCTION__))
16113 " byte.")(static_cast <bool> (!(Origin->getValueSizeInBits(0)
& 0x7) && "The size of the original loaded type is not a multiple of a"
" byte.") ? void (0) : __assert_fail ("!(Origin->getValueSizeInBits(0) & 0x7) && \"The size of the original loaded type is not a multiple of a\" \" byte.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16113, __extension__ __PRETTY_FUNCTION__))
;
16114 // If Offset is bigger than TySizeInBytes, it means we are loading all
16115 // zeros. This should have been optimized before in the process.
16116 assert(TySizeInBytes > Offset &&(static_cast <bool> (TySizeInBytes > Offset &&
"Invalid shift amount for given loaded size") ? void (0) : __assert_fail
("TySizeInBytes > Offset && \"Invalid shift amount for given loaded size\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16117, __extension__ __PRETTY_FUNCTION__))
16117 "Invalid shift amount for given loaded size")(static_cast <bool> (TySizeInBytes > Offset &&
"Invalid shift amount for given loaded size") ? void (0) : __assert_fail
("TySizeInBytes > Offset && \"Invalid shift amount for given loaded size\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16117, __extension__ __PRETTY_FUNCTION__))
;
16118 if (IsBigEndian)
16119 Offset = TySizeInBytes - Offset - getLoadedSize();
16120 return Offset;
16121 }
16122
16123 /// Generate the sequence of instructions to load the slice
16124 /// represented by this object and redirect the uses of this slice to
16125 /// this new sequence of instructions.
16126 /// \pre this->Inst && this->Origin are valid Instructions and this
16127 /// object passed the legal check: LoadedSlice::isLegal returned true.
16128 /// \return The last instruction of the sequence used to load the slice.
16129 SDValue loadSlice() const {
16130 assert(Inst && Origin && "Unable to replace a non-existing slice.")(static_cast <bool> (Inst && Origin && "Unable to replace a non-existing slice."
) ? void (0) : __assert_fail ("Inst && Origin && \"Unable to replace a non-existing slice.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16130, __extension__ __PRETTY_FUNCTION__))
;
16131 const SDValue &OldBaseAddr = Origin->getBasePtr();
16132 SDValue BaseAddr = OldBaseAddr;
16133 // Get the offset in that chunk of bytes w.r.t. the endianness.
16134 int64_t Offset = static_cast<int64_t>(getOffsetFromBase());
16135 assert(Offset >= 0 && "Offset too big to fit in int64_t!")(static_cast <bool> (Offset >= 0 && "Offset too big to fit in int64_t!"
) ? void (0) : __assert_fail ("Offset >= 0 && \"Offset too big to fit in int64_t!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16135, __extension__ __PRETTY_FUNCTION__))
;
16136 if (Offset) {
16137 // BaseAddr = BaseAddr + Offset.
16138 EVT ArithType = BaseAddr.getValueType();
16139 SDLoc DL(Origin);
16140 BaseAddr = DAG->getNode(ISD::ADD, DL, ArithType, BaseAddr,
16141 DAG->getConstant(Offset, DL, ArithType));
16142 }
16143
16144 // Create the type of the loaded slice according to its size.
16145 EVT SliceType = getLoadedType();
16146
16147 // Create the load for the slice.
16148 SDValue LastInst =
16149 DAG->getLoad(SliceType, SDLoc(Origin), Origin->getChain(), BaseAddr,
16150 Origin->getPointerInfo().getWithOffset(Offset), getAlign(),
16151 Origin->getMemOperand()->getFlags());
16152 // If the final type is not the same as the loaded type, this means that
16153 // we have to pad with zero. Create a zero extend for that.
16154 EVT FinalType = Inst->getValueType(0);
16155 if (SliceType != FinalType)
16156 LastInst =
16157 DAG->getNode(ISD::ZERO_EXTEND, SDLoc(LastInst), FinalType, LastInst);
16158 return LastInst;
16159 }
16160
16161 /// Check if this slice can be merged with an expensive cross register
16162 /// bank copy. E.g.,
16163 /// i = load i32
16164 /// f = bitcast i32 i to float
16165 bool canMergeExpensiveCrossRegisterBankCopy() const {
16166 if (!Inst || !Inst->hasOneUse())
16167 return false;
16168 SDNode *Use = *Inst->use_begin();
16169 if (Use->getOpcode() != ISD::BITCAST)
16170 return false;
16171 assert(DAG && "Missing context")(static_cast <bool> (DAG && "Missing context") ?
void (0) : __assert_fail ("DAG && \"Missing context\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16171, __extension__ __PRETTY_FUNCTION__))
;
16172 const TargetLowering &TLI = DAG->getTargetLoweringInfo();
16173 EVT ResVT = Use->getValueType(0);
16174 const TargetRegisterClass *ResRC =
16175 TLI.getRegClassFor(ResVT.getSimpleVT(), Use->isDivergent());
16176 const TargetRegisterClass *ArgRC =
16177 TLI.getRegClassFor(Use->getOperand(0).getValueType().getSimpleVT(),
16178 Use->getOperand(0)->isDivergent());
16179 if (ArgRC == ResRC || !TLI.isOperationLegal(ISD::LOAD, ResVT))
16180 return false;
16181
16182 // At this point, we know that we perform a cross-register-bank copy.
16183 // Check if it is expensive.
16184 const TargetRegisterInfo *TRI = DAG->getSubtarget().getRegisterInfo();
16185 // Assume bitcasts are cheap, unless both register classes do not
16186 // explicitly share a common sub class.
16187 if (!TRI || TRI->getCommonSubClass(ArgRC, ResRC))
16188 return false;
16189
16190 // Check if it will be merged with the load.
16191 // 1. Check the alignment constraint.
16192 Align RequiredAlignment = DAG->getDataLayout().getABITypeAlign(
16193 ResVT.getTypeForEVT(*DAG->getContext()));
16194
16195 if (RequiredAlignment > getAlign())
16196 return false;
16197
16198 // 2. Check that the load is a legal operation for that type.
16199 if (!TLI.isOperationLegal(ISD::LOAD, ResVT))
16200 return false;
16201
16202 // 3. Check that we do not have a zext in the way.
16203 if (Inst->getValueType(0) != getLoadedType())
16204 return false;
16205
16206 return true;
16207 }
16208};
16209
16210} // end anonymous namespace
16211
16212/// Check that all bits set in \p UsedBits form a dense region, i.e.,
16213/// \p UsedBits looks like 0..0 1..1 0..0.
16214static bool areUsedBitsDense(const APInt &UsedBits) {
16215 // If all the bits are one, this is dense!
16216 if (UsedBits.isAllOnesValue())
17
Assuming the condition is false
18
Taking false branch
16217 return true;
16218
16219 // Get rid of the unused bits on the right.
16220 APInt NarrowedUsedBits = UsedBits.lshr(UsedBits.countTrailingZeros());
19
Calling 'APInt::countTrailingZeros'
34
Returning from 'APInt::countTrailingZeros'
35
Passing the value 64 via 1st parameter 'shiftAmt'
36
Calling 'APInt::lshr'
16221 // Get rid of the unused bits on the left.
16222 if (NarrowedUsedBits.countLeadingZeros())
16223 NarrowedUsedBits = NarrowedUsedBits.trunc(NarrowedUsedBits.getActiveBits());
16224 // Check that the chunk of bits is completely used.
16225 return NarrowedUsedBits.isAllOnesValue();
16226}
16227
16228/// Check whether or not \p First and \p Second are next to each other
16229/// in memory. This means that there is no hole between the bits loaded
16230/// by \p First and the bits loaded by \p Second.
16231static bool areSlicesNextToEachOther(const LoadedSlice &First,
16232 const LoadedSlice &Second) {
16233 assert(First.Origin == Second.Origin && First.Origin &&(static_cast <bool> (First.Origin == Second.Origin &&
First.Origin && "Unable to match different memory origins."
) ? void (0) : __assert_fail ("First.Origin == Second.Origin && First.Origin && \"Unable to match different memory origins.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16234, __extension__ __PRETTY_FUNCTION__))
13
Assuming 'First.Origin' is equal to 'Second.Origin'
14
'?' condition is true
16234 "Unable to match different memory origins.")(static_cast <bool> (First.Origin == Second.Origin &&
First.Origin && "Unable to match different memory origins."
) ? void (0) : __assert_fail ("First.Origin == Second.Origin && First.Origin && \"Unable to match different memory origins.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16234, __extension__ __PRETTY_FUNCTION__))
;
16235 APInt UsedBits = First.getUsedBits();
16236 assert((UsedBits & Second.getUsedBits()) == 0 &&(static_cast <bool> ((UsedBits & Second.getUsedBits
()) == 0 && "Slices are not supposed to overlap.") ? void
(0) : __assert_fail ("(UsedBits & Second.getUsedBits()) == 0 && \"Slices are not supposed to overlap.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16237, __extension__ __PRETTY_FUNCTION__))
15
'?' condition is true
16237 "Slices are not supposed to overlap.")(static_cast <bool> ((UsedBits & Second.getUsedBits
()) == 0 && "Slices are not supposed to overlap.") ? void
(0) : __assert_fail ("(UsedBits & Second.getUsedBits()) == 0 && \"Slices are not supposed to overlap.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16237, __extension__ __PRETTY_FUNCTION__))
;
16238 UsedBits |= Second.getUsedBits();
16239 return areUsedBitsDense(UsedBits);
16
Calling 'areUsedBitsDense'
16240}
16241
16242/// Adjust the \p GlobalLSCost according to the target
16243/// paring capabilities and the layout of the slices.
16244/// \pre \p GlobalLSCost should account for at least as many loads as
16245/// there is in the slices in \p LoadedSlices.
16246static void adjustCostForPairing(SmallVectorImpl<LoadedSlice> &LoadedSlices,
16247 LoadedSlice::Cost &GlobalLSCost) {
16248 unsigned NumberOfSlices = LoadedSlices.size();
16249 // If there is less than 2 elements, no pairing is possible.
16250 if (NumberOfSlices < 2)
1
Assuming 'NumberOfSlices' is >= 2
2
Taking false branch
16251 return;
16252
16253 // Sort the slices so that elements that are likely to be next to each
16254 // other in memory are next to each other in the list.
16255 llvm::sort(LoadedSlices, [](const LoadedSlice &LHS, const LoadedSlice &RHS) {
16256 assert(LHS.Origin == RHS.Origin && "Different bases not implemented.")(static_cast <bool> (LHS.Origin == RHS.Origin &&
"Different bases not implemented.") ? void (0) : __assert_fail
("LHS.Origin == RHS.Origin && \"Different bases not implemented.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16256, __extension__ __PRETTY_FUNCTION__))
;
16257 return LHS.getOffsetFromBase() < RHS.getOffsetFromBase();
16258 });
16259 const TargetLowering &TLI = LoadedSlices[0].DAG->getTargetLoweringInfo();
16260 // First (resp. Second) is the first (resp. Second) potentially candidate
16261 // to be placed in a paired load.
16262 const LoadedSlice *First = nullptr;
16263 const LoadedSlice *Second = nullptr;
16264 for (unsigned CurrSlice = 0; CurrSlice
2.1
'CurrSlice' is < 'NumberOfSlices'
5.1
'CurrSlice' is < 'NumberOfSlices'
2.1
'CurrSlice' is < 'NumberOfSlices'
5.1
'CurrSlice' is < 'NumberOfSlices'
2.1
'CurrSlice' is < 'NumberOfSlices'
5.1
'CurrSlice' is < 'NumberOfSlices'
< NumberOfSlices; ++CurrSlice,
3
Loop condition is true. Entering loop body
6
Loop condition is true. Entering loop body
16265 // Set the beginning of the pair.
16266 First = Second) {
16267 Second = &LoadedSlices[CurrSlice];
16268
16269 // If First is NULL, it means we start a new pair.
16270 // Get to the next slice.
16271 if (!First
3.1
'First' is null
6.1
'First' is non-null
3.1
'First' is null
6.1
'First' is non-null
3.1
'First' is null
6.1
'First' is non-null
)
4
Taking true branch
7
Taking false branch
16272 continue;
5
Execution continues on line 16264
16273
16274 EVT LoadedType = First->getLoadedType();
16275
16276 // If the types of the slices are different, we cannot pair them.
16277 if (LoadedType != Second->getLoadedType())
8
Taking false branch
16278 continue;
16279
16280 // Check if the target supplies paired loads for this type.
16281 Align RequiredAlignment;
16282 if (!TLI.hasPairedLoad(LoadedType, RequiredAlignment)) {
9
Assuming the condition is false
10
Taking false branch
16283 // move to the next pair, this type is hopeless.
16284 Second = nullptr;
16285 continue;
16286 }
16287 // Check if we meet the alignment requirement.
16288 if (First->getAlign() < RequiredAlignment)
11
Taking false branch
16289 continue;
16290
16291 // Check that both loads are next to each other in memory.
16292 if (!areSlicesNextToEachOther(*First, *Second))
12
Calling 'areSlicesNextToEachOther'
16293 continue;
16294
16295 assert(GlobalLSCost.Loads > 0 && "We save more loads than we created!")(static_cast <bool> (GlobalLSCost.Loads > 0 &&
"We save more loads than we created!") ? void (0) : __assert_fail
("GlobalLSCost.Loads > 0 && \"We save more loads than we created!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16295, __extension__ __PRETTY_FUNCTION__))
;
16296 --GlobalLSCost.Loads;
16297 // Move to the next pair.
16298 Second = nullptr;
16299 }
16300}
16301
16302/// Check the profitability of all involved LoadedSlice.
16303/// Currently, it is considered profitable if there is exactly two
16304/// involved slices (1) which are (2) next to each other in memory, and
16305/// whose cost (\see LoadedSlice::Cost) is smaller than the original load (3).
16306///
16307/// Note: The order of the elements in \p LoadedSlices may be modified, but not
16308/// the elements themselves.
16309///
16310/// FIXME: When the cost model will be mature enough, we can relax
16311/// constraints (1) and (2).
16312static bool isSlicingProfitable(SmallVectorImpl<LoadedSlice> &LoadedSlices,
16313 const APInt &UsedBits, bool ForCodeSize) {
16314 unsigned NumberOfSlices = LoadedSlices.size();
16315 if (StressLoadSlicing)
16316 return NumberOfSlices > 1;
16317
16318 // Check (1).
16319 if (NumberOfSlices != 2)
16320 return false;
16321
16322 // Check (2).
16323 if (!areUsedBitsDense(UsedBits))
16324 return false;
16325
16326 // Check (3).
16327 LoadedSlice::Cost OrigCost(ForCodeSize), GlobalSlicingCost(ForCodeSize);
16328 // The original code has one big load.
16329 OrigCost.Loads = 1;
16330 for (unsigned CurrSlice = 0; CurrSlice < NumberOfSlices; ++CurrSlice) {
16331 const LoadedSlice &LS = LoadedSlices[CurrSlice];
16332 // Accumulate the cost of all the slices.
16333 LoadedSlice::Cost SliceCost(LS, ForCodeSize);
16334 GlobalSlicingCost += SliceCost;
16335
16336 // Account as cost in the original configuration the gain obtained
16337 // with the current slices.
16338 OrigCost.addSliceGain(LS);
16339 }
16340
16341 // If the target supports paired load, adjust the cost accordingly.
16342 adjustCostForPairing(LoadedSlices, GlobalSlicingCost);
16343 return OrigCost > GlobalSlicingCost;
16344}
16345
16346/// If the given load, \p LI, is used only by trunc or trunc(lshr)
16347/// operations, split it in the various pieces being extracted.
16348///
16349/// This sort of thing is introduced by SROA.
16350/// This slicing takes care not to insert overlapping loads.
16351/// \pre LI is a simple load (i.e., not an atomic or volatile load).
16352bool DAGCombiner::SliceUpLoad(SDNode *N) {
16353 if (Level < AfterLegalizeDAG)
16354 return false;
16355
16356 LoadSDNode *LD = cast<LoadSDNode>(N);
16357 if (!LD->isSimple() || !ISD::isNormalLoad(LD) ||
16358 !LD->getValueType(0).isInteger())
16359 return false;
16360
16361 // The algorithm to split up a load of a scalable vector into individual
16362 // elements currently requires knowing the length of the loaded type,
16363 // so will need adjusting to work on scalable vectors.
16364 if (LD->getValueType(0).isScalableVector())
16365 return false;
16366
16367 // Keep track of already used bits to detect overlapping values.
16368 // In that case, we will just abort the transformation.
16369 APInt UsedBits(LD->getValueSizeInBits(0), 0);
16370
16371 SmallVector<LoadedSlice, 4> LoadedSlices;
16372
16373 // Check if this load is used as several smaller chunks of bits.
16374 // Basically, look for uses in trunc or trunc(lshr) and record a new chain
16375 // of computation for each trunc.
16376 for (SDNode::use_iterator UI = LD->use_begin(), UIEnd = LD->use_end();
16377 UI != UIEnd; ++UI) {
16378 // Skip the uses of the chain.
16379 if (UI.getUse().getResNo() != 0)
16380 continue;
16381
16382 SDNode *User = *UI;
16383 unsigned Shift = 0;
16384
16385 // Check if this is a trunc(lshr).
16386 if (User->getOpcode() == ISD::SRL && User->hasOneUse() &&
16387 isa<ConstantSDNode>(User->getOperand(1))) {
16388 Shift = User->getConstantOperandVal(1);
16389 User = *User->use_begin();
16390 }
16391
16392 // At this point, User is a Truncate, iff we encountered, trunc or
16393 // trunc(lshr).
16394 if (User->getOpcode() != ISD::TRUNCATE)
16395 return false;
16396
16397 // The width of the type must be a power of 2 and greater than 8-bits.
16398 // Otherwise the load cannot be represented in LLVM IR.
16399 // Moreover, if we shifted with a non-8-bits multiple, the slice
16400 // will be across several bytes. We do not support that.
16401 unsigned Width = User->getValueSizeInBits(0);
16402 if (Width < 8 || !isPowerOf2_32(Width) || (Shift & 0x7))
16403 return false;
16404
16405 // Build the slice for this chain of computations.
16406 LoadedSlice LS(User, LD, Shift, &DAG);
16407 APInt CurrentUsedBits = LS.getUsedBits();
16408
16409 // Check if this slice overlaps with another.
16410 if ((CurrentUsedBits & UsedBits) != 0)
16411 return false;
16412 // Update the bits used globally.
16413 UsedBits |= CurrentUsedBits;
16414
16415 // Check if the new slice would be legal.
16416 if (!LS.isLegal())
16417 return false;
16418
16419 // Record the slice.
16420 LoadedSlices.push_back(LS);
16421 }
16422
16423 // Abort slicing if it does not seem to be profitable.
16424 if (!isSlicingProfitable(LoadedSlices, UsedBits, ForCodeSize))
16425 return false;
16426
16427 ++SlicedLoads;
16428
16429 // Rewrite each chain to use an independent load.
16430 // By construction, each chain can be represented by a unique load.
16431
16432 // Prepare the argument for the new token factor for all the slices.
16433 SmallVector<SDValue, 8> ArgChains;
16434 for (const LoadedSlice &LS : LoadedSlices) {
16435 SDValue SliceInst = LS.loadSlice();
16436 CombineTo(LS.Inst, SliceInst, true);
16437 if (SliceInst.getOpcode() != ISD::LOAD)
16438 SliceInst = SliceInst.getOperand(0);
16439 assert(SliceInst->getOpcode() == ISD::LOAD &&(static_cast <bool> (SliceInst->getOpcode() == ISD::
LOAD && "It takes more than a zext to get to the loaded slice!!"
) ? void (0) : __assert_fail ("SliceInst->getOpcode() == ISD::LOAD && \"It takes more than a zext to get to the loaded slice!!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16440, __extension__ __PRETTY_FUNCTION__))
16440 "It takes more than a zext to get to the loaded slice!!")(static_cast <bool> (SliceInst->getOpcode() == ISD::
LOAD && "It takes more than a zext to get to the loaded slice!!"
) ? void (0) : __assert_fail ("SliceInst->getOpcode() == ISD::LOAD && \"It takes more than a zext to get to the loaded slice!!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16440, __extension__ __PRETTY_FUNCTION__))
;
16441 ArgChains.push_back(SliceInst.getValue(1));
16442 }
16443
16444 SDValue Chain = DAG.getNode(ISD::TokenFactor, SDLoc(LD), MVT::Other,
16445 ArgChains);
16446 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
16447 AddToWorklist(Chain.getNode());
16448 return true;
16449}
16450
16451/// Check to see if V is (and load (ptr), imm), where the load is having
16452/// specific bytes cleared out. If so, return the byte size being masked out
16453/// and the shift amount.
16454static std::pair<unsigned, unsigned>
16455CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) {
16456 std::pair<unsigned, unsigned> Result(0, 0);
16457
16458 // Check for the structure we're looking for.
16459 if (V->getOpcode() != ISD::AND ||
16460 !isa<ConstantSDNode>(V->getOperand(1)) ||
16461 !ISD::isNormalLoad(V->getOperand(0).getNode()))
16462 return Result;
16463
16464 // Check the chain and pointer.
16465 LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0));
16466 if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer.
16467
16468 // This only handles simple types.
16469 if (V.getValueType() != MVT::i16 &&
16470 V.getValueType() != MVT::i32 &&
16471 V.getValueType() != MVT::i64)
16472 return Result;
16473
16474 // Check the constant mask. Invert it so that the bits being masked out are
16475 // 0 and the bits being kept are 1. Use getSExtValue so that leading bits
16476 // follow the sign bit for uniformity.
16477 uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue();
16478 unsigned NotMaskLZ = countLeadingZeros(NotMask);
16479 if (NotMaskLZ & 7) return Result; // Must be multiple of a byte.
16480 unsigned NotMaskTZ = countTrailingZeros(NotMask);
16481 if (NotMaskTZ & 7) return Result; // Must be multiple of a byte.
16482 if (NotMaskLZ == 64) return Result; // All zero mask.
16483
16484 // See if we have a continuous run of bits. If so, we have 0*1+0*
16485 if (countTrailingOnes(NotMask >> NotMaskTZ) + NotMaskTZ + NotMaskLZ != 64)
16486 return Result;
16487
16488 // Adjust NotMaskLZ down to be from the actual size of the int instead of i64.
16489 if (V.getValueType() != MVT::i64 && NotMaskLZ)
16490 NotMaskLZ -= 64-V.getValueSizeInBits();
16491
16492 unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8;
16493 switch (MaskedBytes) {
16494 case 1:
16495 case 2:
16496 case 4: break;
16497 default: return Result; // All one mask, or 5-byte mask.
16498 }
16499
16500 // Verify that the first bit starts at a multiple of mask so that the access
16501 // is aligned the same as the access width.
16502 if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result;
16503
16504 // For narrowing to be valid, it must be the case that the load the
16505 // immediately preceding memory operation before the store.
16506 if (LD == Chain.getNode())
16507 ; // ok.
16508 else if (Chain->getOpcode() == ISD::TokenFactor &&
16509 SDValue(LD, 1).hasOneUse()) {
16510 // LD has only 1 chain use so they are no indirect dependencies.
16511 if (!LD->isOperandOf(Chain.getNode()))
16512 return Result;
16513 } else
16514 return Result; // Fail.
16515
16516 Result.first = MaskedBytes;
16517 Result.second = NotMaskTZ/8;
16518 return Result;
16519}
16520
16521/// Check to see if IVal is something that provides a value as specified by
16522/// MaskInfo. If so, replace the specified store with a narrower store of
16523/// truncated IVal.
16524static SDValue
16525ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo,
16526 SDValue IVal, StoreSDNode *St,
16527 DAGCombiner *DC) {
16528 unsigned NumBytes = MaskInfo.first;
16529 unsigned ByteShift = MaskInfo.second;
16530 SelectionDAG &DAG = DC->getDAG();
16531
16532 // Check to see if IVal is all zeros in the part being masked in by the 'or'
16533 // that uses this. If not, this is not a replacement.
16534 APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(),
16535 ByteShift*8, (ByteShift+NumBytes)*8);
16536 if (!DAG.MaskedValueIsZero(IVal, Mask)) return SDValue();
16537
16538 // Check that it is legal on the target to do this. It is legal if the new
16539 // VT we're shrinking to (i8/i16/i32) is legal or we're still before type
16540 // legalization (and the target doesn't explicitly think this is a bad idea).
16541 MVT VT = MVT::getIntegerVT(NumBytes * 8);
16542 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16543 if (!DC->isTypeLegal(VT))
16544 return SDValue();
16545 if (St->getMemOperand() &&
16546 !TLI.allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), VT,
16547 *St->getMemOperand()))
16548 return SDValue();
16549
16550 // Okay, we can do this! Replace the 'St' store with a store of IVal that is
16551 // shifted by ByteShift and truncated down to NumBytes.
16552 if (ByteShift) {
16553 SDLoc DL(IVal);
16554 IVal = DAG.getNode(ISD::SRL, DL, IVal.getValueType(), IVal,
16555 DAG.getConstant(ByteShift*8, DL,
16556 DC->getShiftAmountTy(IVal.getValueType())));
16557 }
16558
16559 // Figure out the offset for the store and the alignment of the access.
16560 unsigned StOffset;
16561 if (DAG.getDataLayout().isLittleEndian())
16562 StOffset = ByteShift;
16563 else
16564 StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes;
16565
16566 SDValue Ptr = St->getBasePtr();
16567 if (StOffset) {
16568 SDLoc DL(IVal);
16569 Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::Fixed(StOffset), DL);
16570 }
16571
16572 // Truncate down to the new size.
16573 IVal = DAG.getNode(ISD::TRUNCATE, SDLoc(IVal), VT, IVal);
16574
16575 ++OpsNarrowed;
16576 return DAG
16577 .getStore(St->getChain(), SDLoc(St), IVal, Ptr,
16578 St->getPointerInfo().getWithOffset(StOffset),
16579 St->getOriginalAlign());
16580}
16581
16582/// Look for sequence of load / op / store where op is one of 'or', 'xor', and
16583/// 'and' of immediates. If 'op' is only touching some of the loaded bits, try
16584/// narrowing the load and store if it would end up being a win for performance
16585/// or code size.
16586SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
16587 StoreSDNode *ST = cast<StoreSDNode>(N);
16588 if (!ST->isSimple())
16589 return SDValue();
16590
16591 SDValue Chain = ST->getChain();
16592 SDValue Value = ST->getValue();
16593 SDValue Ptr = ST->getBasePtr();
16594 EVT VT = Value.getValueType();
16595
16596 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
16597 return SDValue();
16598
16599 unsigned Opc = Value.getOpcode();
16600
16601 // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst
16602 // is a byte mask indicating a consecutive number of bytes, check to see if
16603 // Y is known to provide just those bytes. If so, we try to replace the
16604 // load + replace + store sequence with a single (narrower) store, which makes
16605 // the load dead.
16606 if (Opc == ISD::OR && EnableShrinkLoadReplaceStoreWithStore) {
16607 std::pair<unsigned, unsigned> MaskedLoad;
16608 MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain);
16609 if (MaskedLoad.first)
16610 if (SDValue NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
16611 Value.getOperand(1), ST,this))
16612 return NewST;
16613
16614 // Or is commutative, so try swapping X and Y.
16615 MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain);
16616 if (MaskedLoad.first)
16617 if (SDValue NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
16618 Value.getOperand(0), ST,this))
16619 return NewST;
16620 }
16621
16622 if (!EnableReduceLoadOpStoreWidth)
16623 return SDValue();
16624
16625 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
16626 Value.getOperand(1).getOpcode() != ISD::Constant)
16627 return SDValue();
16628
16629 SDValue N0 = Value.getOperand(0);
16630 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
16631 Chain == SDValue(N0.getNode(), 1)) {
16632 LoadSDNode *LD = cast<LoadSDNode>(N0);
16633 if (LD->getBasePtr() != Ptr ||
16634 LD->getPointerInfo().getAddrSpace() !=
16635 ST->getPointerInfo().getAddrSpace())
16636 return SDValue();
16637
16638 // Find the type to narrow it the load / op / store to.
16639 SDValue N1 = Value.getOperand(1);
16640 unsigned BitWidth = N1.getValueSizeInBits();
16641 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
16642 if (Opc == ISD::AND)
16643 Imm ^= APInt::getAllOnesValue(BitWidth);
16644 if (Imm == 0 || Imm.isAllOnesValue())
16645 return SDValue();
16646 unsigned ShAmt = Imm.countTrailingZeros();
16647 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
16648 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
16649 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
16650 // The narrowing should be profitable, the load/store operation should be
16651 // legal (or custom) and the store size should be equal to the NewVT width.
16652 while (NewBW < BitWidth &&
16653 (NewVT.getStoreSizeInBits() != NewBW ||
16654 !TLI.isOperationLegalOrCustom(Opc, NewVT) ||
16655 !TLI.isNarrowingProfitable(VT, NewVT))) {
16656 NewBW = NextPowerOf2(NewBW);
16657 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
16658 }
16659 if (NewBW >= BitWidth)
16660 return SDValue();
16661
16662 // If the lsb changed does not start at the type bitwidth boundary,
16663 // start at the previous one.
16664 if (ShAmt % NewBW)
16665 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
16666 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt,
16667 std::min(BitWidth, ShAmt + NewBW));
16668 if ((Imm & Mask) == Imm) {
16669 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
16670 if (Opc == ISD::AND)
16671 NewImm ^= APInt::getAllOnesValue(NewBW);
16672 uint64_t PtrOff = ShAmt / 8;
16673 // For big endian targets, we need to adjust the offset to the pointer to
16674 // load the correct bytes.
16675 if (DAG.getDataLayout().isBigEndian())
16676 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
16677
16678 Align NewAlign = commonAlignment(LD->getAlign(), PtrOff);
16679 Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext());
16680 if (NewAlign < DAG.getDataLayout().getABITypeAlign(NewVTTy))
16681 return SDValue();
16682
16683 SDValue NewPtr =
16684 DAG.getMemBasePlusOffset(Ptr, TypeSize::Fixed(PtrOff), SDLoc(LD));
16685 SDValue NewLD =
16686 DAG.getLoad(NewVT, SDLoc(N0), LD->getChain(), NewPtr,
16687 LD->getPointerInfo().getWithOffset(PtrOff), NewAlign,
16688 LD->getMemOperand()->getFlags(), LD->getAAInfo());
16689 SDValue NewVal = DAG.getNode(Opc, SDLoc(Value), NewVT, NewLD,
16690 DAG.getConstant(NewImm, SDLoc(Value),
16691 NewVT));
16692 SDValue NewST =
16693 DAG.getStore(Chain, SDLoc(N), NewVal, NewPtr,
16694 ST->getPointerInfo().getWithOffset(PtrOff), NewAlign);
16695
16696 AddToWorklist(NewPtr.getNode());
16697 AddToWorklist(NewLD.getNode());
16698 AddToWorklist(NewVal.getNode());
16699 WorklistRemover DeadNodes(*this);
16700 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1));
16701 ++OpsNarrowed;
16702 return NewST;
16703 }
16704 }
16705
16706 return SDValue();
16707}
16708
16709/// For a given floating point load / store pair, if the load value isn't used
16710/// by any other operations, then consider transforming the pair to integer
16711/// load / store operations if the target deems the transformation profitable.
16712SDValue DAGCombiner::TransformFPLoadStorePair(SDNode *N) {
16713 StoreSDNode *ST = cast<StoreSDNode>(N);
16714 SDValue Value = ST->getValue();
16715 if (ISD::isNormalStore(ST) && ISD::isNormalLoad(Value.getNode()) &&
16716 Value.hasOneUse()) {
16717 LoadSDNode *LD = cast<LoadSDNode>(Value);
16718 EVT VT = LD->getMemoryVT();
16719 if (!VT.isFloatingPoint() ||
16720 VT != ST->getMemoryVT() ||
16721 LD->isNonTemporal() ||
16722 ST->isNonTemporal() ||
16723 LD->getPointerInfo().getAddrSpace() != 0 ||
16724 ST->getPointerInfo().getAddrSpace() != 0)
16725 return SDValue();
16726
16727 TypeSize VTSize = VT.getSizeInBits();
16728
16729 // We don't know the size of scalable types at compile time so we cannot
16730 // create an integer of the equivalent size.
16731 if (VTSize.isScalable())
16732 return SDValue();
16733
16734 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VTSize.getFixedSize());
16735 if (!TLI.isOperationLegal(ISD::LOAD, IntVT) ||
16736 !TLI.isOperationLegal(ISD::STORE, IntVT) ||
16737 !TLI.isDesirableToTransformToIntegerOp(ISD::LOAD, VT) ||
16738 !TLI.isDesirableToTransformToIntegerOp(ISD::STORE, VT))
16739 return SDValue();
16740
16741 Align LDAlign = LD->getAlign();
16742 Align STAlign = ST->getAlign();
16743 Type *IntVTTy = IntVT.getTypeForEVT(*DAG.getContext());
16744 Align ABIAlign = DAG.getDataLayout().getABITypeAlign(IntVTTy);
16745 if (LDAlign < ABIAlign || STAlign < ABIAlign)
16746 return SDValue();
16747
16748 SDValue NewLD =
16749 DAG.getLoad(IntVT, SDLoc(Value), LD->getChain(), LD->getBasePtr(),
16750 LD->getPointerInfo(), LDAlign);
16751
16752 SDValue NewST =
16753 DAG.getStore(ST->getChain(), SDLoc(N), NewLD, ST->getBasePtr(),
16754 ST->getPointerInfo(), STAlign);
16755
16756 AddToWorklist(NewLD.getNode());
16757 AddToWorklist(NewST.getNode());
16758 WorklistRemover DeadNodes(*this);
16759 DAG.ReplaceAllUsesOfValueWith(Value.getValue(1), NewLD.getValue(1));
16760 ++LdStFP2Int;
16761 return NewST;
16762 }
16763
16764 return SDValue();
16765}
16766
16767// This is a helper function for visitMUL to check the profitability
16768// of folding (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2).
16769// MulNode is the original multiply, AddNode is (add x, c1),
16770// and ConstNode is c2.
16771//
16772// If the (add x, c1) has multiple uses, we could increase
16773// the number of adds if we make this transformation.
16774// It would only be worth doing this if we can remove a
16775// multiply in the process. Check for that here.
16776// To illustrate:
16777// (A + c1) * c3
16778// (A + c2) * c3
16779// We're checking for cases where we have common "c3 * A" expressions.
16780bool DAGCombiner::isMulAddWithConstProfitable(SDNode *MulNode,
16781 SDValue &AddNode,
16782 SDValue &ConstNode) {
16783 APInt Val;
16784
16785 // If the add only has one use, this would be OK to do.
16786 if (AddNode.getNode()->hasOneUse())
16787 return true;
16788
16789 // Walk all the users of the constant with which we're multiplying.
16790 for (SDNode *Use : ConstNode->uses()) {
16791 if (Use == MulNode) // This use is the one we're on right now. Skip it.
16792 continue;
16793
16794 if (Use->getOpcode() == ISD::MUL) { // We have another multiply use.
16795 SDNode *OtherOp;
16796 SDNode *MulVar = AddNode.getOperand(0).getNode();
16797
16798 // OtherOp is what we're multiplying against the constant.
16799 if (Use->getOperand(0) == ConstNode)
16800 OtherOp = Use->getOperand(1).getNode();
16801 else
16802 OtherOp = Use->getOperand(0).getNode();
16803
16804 // Check to see if multiply is with the same operand of our "add".
16805 //
16806 // ConstNode = CONST
16807 // Use = ConstNode * A <-- visiting Use. OtherOp is A.
16808 // ...
16809 // AddNode = (A + c1) <-- MulVar is A.
16810 // = AddNode * ConstNode <-- current visiting instruction.
16811 //
16812 // If we make this transformation, we will have a common
16813 // multiply (ConstNode * A) that we can save.
16814 if (OtherOp == MulVar)
16815 return true;
16816
16817 // Now check to see if a future expansion will give us a common
16818 // multiply.
16819 //
16820 // ConstNode = CONST
16821 // AddNode = (A + c1)
16822 // ... = AddNode * ConstNode <-- current visiting instruction.
16823 // ...
16824 // OtherOp = (A + c2)
16825 // Use = OtherOp * ConstNode <-- visiting Use.
16826 //
16827 // If we make this transformation, we will have a common
16828 // multiply (CONST * A) after we also do the same transformation
16829 // to the "t2" instruction.
16830 if (OtherOp->getOpcode() == ISD::ADD &&
16831 DAG.isConstantIntBuildVectorOrConstantInt(OtherOp->getOperand(1)) &&
16832 OtherOp->getOperand(0).getNode() == MulVar)
16833 return true;
16834 }
16835 }
16836
16837 // Didn't find a case where this would be profitable.
16838 return false;
16839}
16840
16841SDValue DAGCombiner::getMergeStoreChains(SmallVectorImpl<MemOpLink> &StoreNodes,
16842 unsigned NumStores) {
16843 SmallVector<SDValue, 8> Chains;
16844 SmallPtrSet<const SDNode *, 8> Visited;
16845 SDLoc StoreDL(StoreNodes[0].MemNode);
16846
16847 for (unsigned i = 0; i < NumStores; ++i) {
16848 Visited.insert(StoreNodes[i].MemNode);
16849 }
16850
16851 // don't include nodes that are children or repeated nodes.
16852 for (unsigned i = 0; i < NumStores; ++i) {
16853 if (Visited.insert(StoreNodes[i].MemNode->getChain().getNode()).second)
16854 Chains.push_back(StoreNodes[i].MemNode->getChain());
16855 }
16856
16857 assert(Chains.size() > 0 && "Chain should have generated a chain")(static_cast <bool> (Chains.size() > 0 && "Chain should have generated a chain"
) ? void (0) : __assert_fail ("Chains.size() > 0 && \"Chain should have generated a chain\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16857, __extension__ __PRETTY_FUNCTION__))
;
16858 return DAG.getTokenFactor(StoreDL, Chains);
16859}
16860
16861bool DAGCombiner::mergeStoresOfConstantsOrVecElts(
16862 SmallVectorImpl<MemOpLink> &StoreNodes, EVT MemVT, unsigned NumStores,
16863 bool IsConstantSrc, bool UseVector, bool UseTrunc) {
16864 // Make sure we have something to merge.
16865 if (NumStores < 2)
16866 return false;
16867
16868 assert((!UseTrunc || !UseVector) &&(static_cast <bool> ((!UseTrunc || !UseVector) &&
"This optimization cannot emit a vector truncating store") ?
void (0) : __assert_fail ("(!UseTrunc || !UseVector) && \"This optimization cannot emit a vector truncating store\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16869, __extension__ __PRETTY_FUNCTION__))
16869 "This optimization cannot emit a vector truncating store")(static_cast <bool> ((!UseTrunc || !UseVector) &&
"This optimization cannot emit a vector truncating store") ?
void (0) : __assert_fail ("(!UseTrunc || !UseVector) && \"This optimization cannot emit a vector truncating store\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16869, __extension__ __PRETTY_FUNCTION__))
;
16870
16871 // The latest Node in the DAG.
16872 SDLoc DL(StoreNodes[0].MemNode);
16873
16874 TypeSize ElementSizeBits = MemVT.getStoreSizeInBits();
16875 unsigned SizeInBits = NumStores * ElementSizeBits;
16876 unsigned NumMemElts = MemVT.isVector() ? MemVT.getVectorNumElements() : 1;
16877
16878 EVT StoreTy;
16879 if (UseVector) {
16880 unsigned Elts = NumStores * NumMemElts;
16881 // Get the type for the merged vector store.
16882 StoreTy = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(), Elts);
16883 } else
16884 StoreTy = EVT::getIntegerVT(*DAG.getContext(), SizeInBits);
16885
16886 SDValue StoredVal;
16887 if (UseVector) {
16888 if (IsConstantSrc) {
16889 SmallVector<SDValue, 8> BuildVector;
16890 for (unsigned I = 0; I != NumStores; ++I) {
16891 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[I].MemNode);
16892 SDValue Val = St->getValue();
16893 // If constant is of the wrong type, convert it now.
16894 if (MemVT != Val.getValueType()) {
16895 Val = peekThroughBitcasts(Val);
16896 // Deal with constants of wrong size.
16897 if (ElementSizeBits != Val.getValueSizeInBits()) {
16898 EVT IntMemVT =
16899 EVT::getIntegerVT(*DAG.getContext(), MemVT.getSizeInBits());
16900 if (isa<ConstantFPSDNode>(Val)) {
16901 // Not clear how to truncate FP values.
16902 return false;
16903 } else if (auto *C = dyn_cast<ConstantSDNode>(Val))
16904 Val = DAG.getConstant(C->getAPIntValue()
16905 .zextOrTrunc(Val.getValueSizeInBits())
16906 .zextOrTrunc(ElementSizeBits),
16907 SDLoc(C), IntMemVT);
16908 }
16909 // Make sure correctly size type is the correct type.
16910 Val = DAG.getBitcast(MemVT, Val);
16911 }
16912 BuildVector.push_back(Val);
16913 }
16914 StoredVal = DAG.getNode(MemVT.isVector() ? ISD::CONCAT_VECTORS
16915 : ISD::BUILD_VECTOR,
16916 DL, StoreTy, BuildVector);
16917 } else {
16918 SmallVector<SDValue, 8> Ops;
16919 for (unsigned i = 0; i < NumStores; ++i) {
16920 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
16921 SDValue Val = peekThroughBitcasts(St->getValue());
16922 // All operands of BUILD_VECTOR / CONCAT_VECTOR must be of
16923 // type MemVT. If the underlying value is not the correct
16924 // type, but it is an extraction of an appropriate vector we
16925 // can recast Val to be of the correct type. This may require
16926 // converting between EXTRACT_VECTOR_ELT and
16927 // EXTRACT_SUBVECTOR.
16928 if ((MemVT != Val.getValueType()) &&
16929 (Val.getOpcode() == ISD::EXTRACT_VECTOR_ELT ||
16930 Val.getOpcode() == ISD::EXTRACT_SUBVECTOR)) {
16931 EVT MemVTScalarTy = MemVT.getScalarType();
16932 // We may need to add a bitcast here to get types to line up.
16933 if (MemVTScalarTy != Val.getValueType().getScalarType()) {
16934 Val = DAG.getBitcast(MemVT, Val);
16935 } else {
16936 unsigned OpC = MemVT.isVector() ? ISD::EXTRACT_SUBVECTOR
16937 : ISD::EXTRACT_VECTOR_ELT;
16938 SDValue Vec = Val.getOperand(0);
16939 SDValue Idx = Val.getOperand(1);
16940 Val = DAG.getNode(OpC, SDLoc(Val), MemVT, Vec, Idx);
16941 }
16942 }
16943 Ops.push_back(Val);
16944 }
16945
16946 // Build the extracted vector elements back into a vector.
16947 StoredVal = DAG.getNode(MemVT.isVector() ? ISD::CONCAT_VECTORS
16948 : ISD::BUILD_VECTOR,
16949 DL, StoreTy, Ops);
16950 }
16951 } else {
16952 // We should always use a vector store when merging extracted vector
16953 // elements, so this path implies a store of constants.
16954 assert(IsConstantSrc && "Merged vector elements should use vector store")(static_cast <bool> (IsConstantSrc && "Merged vector elements should use vector store"
) ? void (0) : __assert_fail ("IsConstantSrc && \"Merged vector elements should use vector store\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16954, __extension__ __PRETTY_FUNCTION__))
;
16955
16956 APInt StoreInt(SizeInBits, 0);
16957
16958 // Construct a single integer constant which is made of the smaller
16959 // constant inputs.
16960 bool IsLE = DAG.getDataLayout().isLittleEndian();
16961 for (unsigned i = 0; i < NumStores; ++i) {
16962 unsigned Idx = IsLE ? (NumStores - 1 - i) : i;
16963 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[Idx].MemNode);
16964
16965 SDValue Val = St->getValue();
16966 Val = peekThroughBitcasts(Val);
16967 StoreInt <<= ElementSizeBits;
16968 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val)) {
16969 StoreInt |= C->getAPIntValue()
16970 .zextOrTrunc(ElementSizeBits)
16971 .zextOrTrunc(SizeInBits);
16972 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Val)) {
16973 StoreInt |= C->getValueAPF()
16974 .bitcastToAPInt()
16975 .zextOrTrunc(ElementSizeBits)
16976 .zextOrTrunc(SizeInBits);
16977 // If fp truncation is necessary give up for now.
16978 if (MemVT.getSizeInBits() != ElementSizeBits)
16979 return false;
16980 } else {
16981 llvm_unreachable("Invalid constant element type")::llvm::llvm_unreachable_internal("Invalid constant element type"
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 16981)
;
16982 }
16983 }
16984
16985 // Create the new Load and Store operations.
16986 StoredVal = DAG.getConstant(StoreInt, DL, StoreTy);
16987 }
16988
16989 LSBaseSDNode *FirstInChain = StoreNodes[0].MemNode;
16990 SDValue NewChain = getMergeStoreChains(StoreNodes, NumStores);
16991
16992 // make sure we use trunc store if it's necessary to be legal.
16993 SDValue NewStore;
16994 if (!UseTrunc) {
16995 NewStore =
16996 DAG.getStore(NewChain, DL, StoredVal, FirstInChain->getBasePtr(),
16997 FirstInChain->getPointerInfo(), FirstInChain->getAlign());
16998 } else { // Must be realized as a trunc store
16999 EVT LegalizedStoredValTy =
17000 TLI.getTypeToTransformTo(*DAG.getContext(), StoredVal.getValueType());
17001 unsigned LegalizedStoreSize = LegalizedStoredValTy.getSizeInBits();
17002 ConstantSDNode *C = cast<ConstantSDNode>(StoredVal);
17003 SDValue ExtendedStoreVal =
17004 DAG.getConstant(C->getAPIntValue().zextOrTrunc(LegalizedStoreSize), DL,
17005 LegalizedStoredValTy);
17006 NewStore = DAG.getTruncStore(
17007 NewChain, DL, ExtendedStoreVal, FirstInChain->getBasePtr(),
17008 FirstInChain->getPointerInfo(), StoredVal.getValueType() /*TVT*/,
17009 FirstInChain->getAlign(), FirstInChain->getMemOperand()->getFlags());
17010 }
17011
17012 // Replace all merged stores with the new store.
17013 for (unsigned i = 0; i < NumStores; ++i)
17014 CombineTo(StoreNodes[i].MemNode, NewStore);
17015
17016 AddToWorklist(NewChain.getNode());
17017 return true;
17018}
17019
17020void DAGCombiner::getStoreMergeCandidates(
17021 StoreSDNode *St, SmallVectorImpl<MemOpLink> &StoreNodes,
17022 SDNode *&RootNode) {
17023 // This holds the base pointer, index, and the offset in bytes from the base
17024 // pointer. We must have a base and an offset. Do not handle stores to undef
17025 // base pointers.
17026 BaseIndexOffset BasePtr = BaseIndexOffset::match(St, DAG);
17027 if (!BasePtr.getBase().getNode() || BasePtr.getBase().isUndef())
17028 return;
17029
17030 SDValue Val = peekThroughBitcasts(St->getValue());
17031 StoreSource StoreSrc = getStoreSource(Val);
17032 assert(StoreSrc != StoreSource::Unknown && "Expected known source for store")(static_cast <bool> (StoreSrc != StoreSource::Unknown &&
"Expected known source for store") ? void (0) : __assert_fail
("StoreSrc != StoreSource::Unknown && \"Expected known source for store\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 17032, __extension__ __PRETTY_FUNCTION__))
;
17033
17034 // Match on loadbaseptr if relevant.
17035 EVT MemVT = St->getMemoryVT();
17036 BaseIndexOffset LBasePtr;
17037 EVT LoadVT;
17038 if (StoreSrc == StoreSource::Load) {
17039 auto *Ld = cast<LoadSDNode>(Val);
17040 LBasePtr = BaseIndexOffset::match(Ld, DAG);
17041 LoadVT = Ld->getMemoryVT();
17042 // Load and store should be the same type.
17043 if (MemVT != LoadVT)
17044 return;
17045 // Loads must only have one use.
17046 if (!Ld->hasNUsesOfValue(1, 0))
17047 return;
17048 // The memory operands must not be volatile/indexed/atomic.
17049 // TODO: May be able to relax for unordered atomics (see D66309)
17050 if (!Ld->isSimple() || Ld->isIndexed())
17051 return;
17052 }
17053 auto CandidateMatch = [&](StoreSDNode *Other, BaseIndexOffset &Ptr,
17054 int64_t &Offset) -> bool {
17055 // The memory operands must not be volatile/indexed/atomic.
17056 // TODO: May be able to relax for unordered atomics (see D66309)
17057 if (!Other->isSimple() || Other->isIndexed())
17058 return false;
17059 // Don't mix temporal stores with non-temporal stores.
17060 if (St->isNonTemporal() != Other->isNonTemporal())
17061 return false;
17062 SDValue OtherBC = peekThroughBitcasts(Other->getValue());
17063 // Allow merging constants of different types as integers.
17064 bool NoTypeMatch = (MemVT.isInteger()) ? !MemVT.bitsEq(Other->getMemoryVT())
17065 : Other->getMemoryVT() != MemVT;
17066 switch (StoreSrc) {
17067 case StoreSource::Load: {
17068 if (NoTypeMatch)
17069 return false;
17070 // The Load's Base Ptr must also match.
17071 auto *OtherLd = dyn_cast<LoadSDNode>(OtherBC);
17072 if (!OtherLd)
17073 return false;
17074 BaseIndexOffset LPtr = BaseIndexOffset::match(OtherLd, DAG);
17075 if (LoadVT != OtherLd->getMemoryVT())
17076 return false;
17077 // Loads must only have one use.
17078 if (!OtherLd->hasNUsesOfValue(1, 0))
17079 return false;
17080 // The memory operands must not be volatile/indexed/atomic.
17081 // TODO: May be able to relax for unordered atomics (see D66309)
17082 if (!OtherLd->isSimple() || OtherLd->isIndexed())
17083 return false;
17084 // Don't mix temporal loads with non-temporal loads.
17085 if (cast<LoadSDNode>(Val)->isNonTemporal() != OtherLd->isNonTemporal())
17086 return false;
17087 if (!(LBasePtr.equalBaseIndex(LPtr, DAG)))
17088 return false;
17089 break;
17090 }
17091 case StoreSource::Constant:
17092 if (NoTypeMatch)
17093 return false;
17094 if (!isIntOrFPConstant(OtherBC))
17095 return false;
17096 break;
17097 case StoreSource::Extract:
17098 // Do not merge truncated stores here.
17099 if (Other->isTruncatingStore())
17100 return false;
17101 if (!MemVT.bitsEq(OtherBC.getValueType()))
17102 return false;
17103 if (OtherBC.getOpcode() != ISD::EXTRACT_VECTOR_ELT &&
17104 OtherBC.getOpcode() != ISD::EXTRACT_SUBVECTOR)
17105 return false;
17106 break;
17107 default:
17108 llvm_unreachable("Unhandled store source for merging")::llvm::llvm_unreachable_internal("Unhandled store source for merging"
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 17108)
;
17109 }
17110 Ptr = BaseIndexOffset::match(Other, DAG);
17111 return (BasePtr.equalBaseIndex(Ptr, DAG, Offset));
17112 };
17113
17114 // Check if the pair of StoreNode and the RootNode already bail out many
17115 // times which is over the limit in dependence check.
17116 auto OverLimitInDependenceCheck = [&](SDNode *StoreNode,
17117 SDNode *RootNode) -> bool {
17118 auto RootCount = StoreRootCountMap.find(StoreNode);
17119 return RootCount != StoreRootCountMap.end() &&
17120 RootCount->second.first == RootNode &&
17121 RootCount->second.second > StoreMergeDependenceLimit;
17122 };
17123
17124 auto TryToAddCandidate = [&](SDNode::use_iterator UseIter) {
17125 // This must be a chain use.
17126 if (UseIter.getOperandNo() != 0)
17127 return;
17128 if (auto *OtherStore = dyn_cast<StoreSDNode>(*UseIter)) {
17129 BaseIndexOffset Ptr;
17130 int64_t PtrDiff;
17131 if (CandidateMatch(OtherStore, Ptr, PtrDiff) &&
17132 !OverLimitInDependenceCheck(OtherStore, RootNode))
17133 StoreNodes.push_back(MemOpLink(OtherStore, PtrDiff));
17134 }
17135 };
17136
17137 // We looking for a root node which is an ancestor to all mergable
17138 // stores. We search up through a load, to our root and then down
17139 // through all children. For instance we will find Store{1,2,3} if
17140 // St is Store1, Store2. or Store3 where the root is not a load
17141 // which always true for nonvolatile ops. TODO: Expand
17142 // the search to find all valid candidates through multiple layers of loads.
17143 //
17144 // Root
17145 // |-------|-------|
17146 // Load Load Store3
17147 // | |
17148 // Store1 Store2
17149 //
17150 // FIXME: We should be able to climb and
17151 // descend TokenFactors to find candidates as well.
17152
17153 RootNode = St->getChain().getNode();
17154
17155 unsigned NumNodesExplored = 0;
17156 const unsigned MaxSearchNodes = 1024;
17157 if (auto *Ldn = dyn_cast<LoadSDNode>(RootNode)) {
17158 RootNode = Ldn->getChain().getNode();
17159 for (auto I = RootNode->use_begin(), E = RootNode->use_end();
17160 I != E && NumNodesExplored < MaxSearchNodes; ++I, ++NumNodesExplored) {
17161 if (I.getOperandNo() == 0 && isa<LoadSDNode>(*I)) { // walk down chain
17162 for (auto I2 = (*I)->use_begin(), E2 = (*I)->use_end(); I2 != E2; ++I2)
17163 TryToAddCandidate(I2);
17164 }
17165 }
17166 } else {
17167 for (auto I = RootNode->use_begin(), E = RootNode->use_end();
17168 I != E && NumNodesExplored < MaxSearchNodes; ++I, ++NumNodesExplored)
17169 TryToAddCandidate(I);
17170 }
17171}
17172
17173// We need to check that merging these stores does not cause a loop in
17174// the DAG. Any store candidate may depend on another candidate
17175// indirectly through its operand (we already consider dependencies
17176// through the chain). Check in parallel by searching up from
17177// non-chain operands of candidates.
17178bool DAGCombiner::checkMergeStoreCandidatesForDependencies(
17179 SmallVectorImpl<MemOpLink> &StoreNodes, unsigned NumStores,
17180 SDNode *RootNode) {
17181 // FIXME: We should be able to truncate a full search of
17182 // predecessors by doing a BFS and keeping tabs the originating
17183 // stores from which worklist nodes come from in a similar way to
17184 // TokenFactor simplfication.
17185
17186 SmallPtrSet<const SDNode *, 32> Visited;
17187 SmallVector<const SDNode *, 8> Worklist;
17188
17189 // RootNode is a predecessor to all candidates so we need not search
17190 // past it. Add RootNode (peeking through TokenFactors). Do not count
17191 // these towards size check.
17192
17193 Worklist.push_back(RootNode);
17194 while (!Worklist.empty()) {
17195 auto N = Worklist.pop_back_val();
17196 if (!Visited.insert(N).second)
17197 continue; // Already present in Visited.
17198 if (N->getOpcode() == ISD::TokenFactor) {
17199 for (SDValue Op : N->ops())
17200 Worklist.push_back(Op.getNode());
17201 }
17202 }
17203
17204 // Don't count pruning nodes towards max.
17205 unsigned int Max = 1024 + Visited.size();
17206 // Search Ops of store candidates.
17207 for (unsigned i = 0; i < NumStores; ++i) {
17208 SDNode *N = StoreNodes[i].MemNode;
17209 // Of the 4 Store Operands:
17210 // * Chain (Op 0) -> We have already considered these
17211 // in candidate selection and can be
17212 // safely ignored
17213 // * Value (Op 1) -> Cycles may happen (e.g. through load chains)
17214 // * Address (Op 2) -> Merged addresses may only vary by a fixed constant,
17215 // but aren't necessarily fromt the same base node, so
17216 // cycles possible (e.g. via indexed store).
17217 // * (Op 3) -> Represents the pre or post-indexing offset (or undef for
17218 // non-indexed stores). Not constant on all targets (e.g. ARM)
17219 // and so can participate in a cycle.
17220 for (unsigned j = 1; j < N->getNumOperands(); ++j)
17221 Worklist.push_back(N->getOperand(j).getNode());
17222 }
17223 // Search through DAG. We can stop early if we find a store node.
17224 for (unsigned i = 0; i < NumStores; ++i)
17225 if (SDNode::hasPredecessorHelper(StoreNodes[i].MemNode, Visited, Worklist,
17226 Max)) {
17227 // If the searching bail out, record the StoreNode and RootNode in the
17228 // StoreRootCountMap. If we have seen the pair many times over a limit,
17229 // we won't add the StoreNode into StoreNodes set again.
17230 if (Visited.size() >= Max) {
17231 auto &RootCount = StoreRootCountMap[StoreNodes[i].MemNode];
17232 if (RootCount.first == RootNode)
17233 RootCount.second++;
17234 else
17235 RootCount = {RootNode, 1};
17236 }
17237 return false;
17238 }
17239 return true;
17240}
17241
17242unsigned
17243DAGCombiner::getConsecutiveStores(SmallVectorImpl<MemOpLink> &StoreNodes,
17244 int64_t ElementSizeBytes) const {
17245 while (true) {
17246 // Find a store past the width of the first store.
17247 size_t StartIdx = 0;
17248 while ((StartIdx + 1 < StoreNodes.size()) &&
17249 StoreNodes[StartIdx].OffsetFromBase + ElementSizeBytes !=
17250 StoreNodes[StartIdx + 1].OffsetFromBase)
17251 ++StartIdx;
17252
17253 // Bail if we don't have enough candidates to merge.
17254 if (StartIdx + 1 >= StoreNodes.size())
17255 return 0;
17256
17257 // Trim stores that overlapped with the first store.
17258 if (StartIdx)
17259 StoreNodes.erase(StoreNodes.begin(), StoreNodes.begin() + StartIdx);
17260
17261 // Scan the memory operations on the chain and find the first
17262 // non-consecutive store memory address.
17263 unsigned NumConsecutiveStores = 1;
17264 int64_t StartAddress = StoreNodes[0].OffsetFromBase;
17265 // Check that the addresses are consecutive starting from the second
17266 // element in the list of stores.
17267 for (unsigned i = 1, e = StoreNodes.size(); i < e; ++i) {
17268 int64_t CurrAddress = StoreNodes[i].OffsetFromBase;
17269 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
17270 break;
17271 NumConsecutiveStores = i + 1;
17272 }
17273 if (NumConsecutiveStores > 1)
17274 return NumConsecutiveStores;
17275
17276 // There are no consecutive stores at the start of the list.
17277 // Remove the first store and try again.
17278 StoreNodes.erase(StoreNodes.begin(), StoreNodes.begin() + 1);
17279 }
17280}
17281
17282bool DAGCombiner::tryStoreMergeOfConstants(
17283 SmallVectorImpl<MemOpLink> &StoreNodes, unsigned NumConsecutiveStores,
17284 EVT MemVT, SDNode *RootNode, bool AllowVectors) {
17285 LLVMContext &Context = *DAG.getContext();
17286 const DataLayout &DL = DAG.getDataLayout();
17287 int64_t ElementSizeBytes = MemVT.getStoreSize();
17288 unsigned NumMemElts = MemVT.isVector() ? MemVT.getVectorNumElements() : 1;
17289 bool MadeChange = false;
17290
17291 // Store the constants into memory as one consecutive store.
17292 while (NumConsecutiveStores >= 2) {
17293 LSBaseSDNode *FirstInChain = StoreNodes[0].MemNode;
17294 unsigned FirstStoreAS = FirstInChain->getAddressSpace();
17295 unsigned FirstStoreAlign = FirstInChain->getAlignment();
17296 unsigned LastLegalType = 1;
17297 unsigned LastLegalVectorType = 1;
17298 bool LastIntegerTrunc = false;
17299 bool NonZero = false;
17300 unsigned FirstZeroAfterNonZero = NumConsecutiveStores;
17301 for (unsigned i = 0; i < NumConsecutiveStores; ++i) {
17302 StoreSDNode *ST = cast<StoreSDNode>(StoreNodes[i].MemNode);
17303 SDValue StoredVal = ST->getValue();
17304 bool IsElementZero = false;
17305 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(StoredVal))
17306 IsElementZero = C->isNullValue();
17307 else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(StoredVal))
17308 IsElementZero = C->getConstantFPValue()->isNullValue();
17309 if (IsElementZero) {
17310 if (NonZero && FirstZeroAfterNonZero == NumConsecutiveStores)
17311 FirstZeroAfterNonZero = i;
17312 }
17313 NonZero |= !IsElementZero;
17314
17315 // Find a legal type for the constant store.
17316 unsigned SizeInBits = (i + 1) * ElementSizeBytes * 8;
17317 EVT StoreTy = EVT::getIntegerVT(Context, SizeInBits);
17318 bool IsFast = false;
17319
17320 // Break early when size is too large to be legal.
17321 if (StoreTy.getSizeInBits() > MaximumLegalStoreInBits)
17322 break;
17323
17324 if (TLI.isTypeLegal(StoreTy) &&
17325 TLI.canMergeStoresTo(FirstStoreAS, StoreTy, DAG) &&
17326 TLI.allowsMemoryAccess(Context, DL, StoreTy,
17327 *FirstInChain->getMemOperand(), &IsFast) &&
17328 IsFast) {
17329 LastIntegerTrunc = false;
17330 LastLegalType = i + 1;
17331 // Or check whether a truncstore is legal.
17332 } else if (TLI.getTypeAction(Context, StoreTy) ==
17333 TargetLowering::TypePromoteInteger) {
17334 EVT LegalizedStoredValTy =
17335 TLI.getTypeToTransformTo(Context, StoredVal.getValueType());
17336 if (TLI.isTruncStoreLegal(LegalizedStoredValTy, StoreTy) &&
17337 TLI.canMergeStoresTo(FirstStoreAS, LegalizedStoredValTy, DAG) &&
17338 TLI.allowsMemoryAccess(Context, DL, StoreTy,
17339 *FirstInChain->getMemOperand(), &IsFast) &&
17340 IsFast) {
17341 LastIntegerTrunc = true;
17342 LastLegalType = i + 1;
17343 }
17344 }
17345
17346 // We only use vectors if the constant is known to be zero or the
17347 // target allows it and the function is not marked with the
17348 // noimplicitfloat attribute.
17349 if ((!NonZero ||
17350 TLI.storeOfVectorConstantIsCheap(MemVT, i + 1, FirstStoreAS)) &&
17351 AllowVectors) {
17352 // Find a legal type for the vector store.
17353 unsigned Elts = (i + 1) * NumMemElts;
17354 EVT Ty = EVT::getVectorVT(Context, MemVT.getScalarType(), Elts);
17355 if (TLI.isTypeLegal(Ty) && TLI.isTypeLegal(MemVT) &&
17356 TLI.canMergeStoresTo(FirstStoreAS, Ty, DAG) &&
17357 TLI.allowsMemoryAccess(Context, DL, Ty,
17358 *FirstInChain->getMemOperand(), &IsFast) &&
17359 IsFast)
17360 LastLegalVectorType = i + 1;
17361 }
17362 }
17363
17364 bool UseVector = (LastLegalVectorType > LastLegalType) && AllowVectors;
17365 unsigned NumElem = (UseVector) ? LastLegalVectorType : LastLegalType;
17366 bool UseTrunc = LastIntegerTrunc && !UseVector;
17367
17368 // Check if we found a legal integer type that creates a meaningful
17369 // merge.
17370 if (NumElem < 2) {
17371 // We know that candidate stores are in order and of correct
17372 // shape. While there is no mergeable sequence from the
17373 // beginning one may start later in the sequence. The only
17374 // reason a merge of size N could have failed where another of
17375 // the same size would not have, is if the alignment has
17376 // improved or we've dropped a non-zero value. Drop as many
17377 // candidates as we can here.
17378 unsigned NumSkip = 1;
17379 while ((NumSkip < NumConsecutiveStores) &&
17380 (NumSkip < FirstZeroAfterNonZero) &&
17381 (StoreNodes[NumSkip].MemNode->getAlignment() <= FirstStoreAlign))
17382 NumSkip++;
17383
17384 StoreNodes.erase(StoreNodes.begin(), StoreNodes.begin() + NumSkip);
17385 NumConsecutiveStores -= NumSkip;
17386 continue;
17387 }
17388
17389 // Check that we can merge these candidates without causing a cycle.
17390 if (!checkMergeStoreCandidatesForDependencies(StoreNodes, NumElem,
17391 RootNode)) {
17392 StoreNodes.erase(StoreNodes.begin(), StoreNodes.begin() + NumElem);
17393 NumConsecutiveStores -= NumElem;
17394 continue;
17395 }
17396
17397 MadeChange |= mergeStoresOfConstantsOrVecElts(StoreNodes, MemVT, NumElem,
17398 /*IsConstantSrc*/ true,
17399 UseVector, UseTrunc);
17400
17401 // Remove merged stores for next iteration.
17402 StoreNodes.erase(StoreNodes.begin(), StoreNodes.begin() + NumElem);
17403 NumConsecutiveStores -= NumElem;
17404 }
17405 return MadeChange;
17406}
17407
17408bool DAGCombiner::tryStoreMergeOfExtracts(
17409 SmallVectorImpl<MemOpLink> &StoreNodes, unsigned NumConsecutiveStores,
17410 EVT MemVT, SDNode *RootNode) {
17411 LLVMContext &Context = *DAG.getContext();
17412 const DataLayout &DL = DAG.getDataLayout();
17413 unsigned NumMemElts = MemVT.isVector() ? MemVT.getVectorNumElements() : 1;
17414 bool MadeChange = false;
17415
17416 // Loop on Consecutive Stores on success.
17417 while (NumConsecutiveStores >= 2) {
17418 LSBaseSDNode *FirstInChain = StoreNodes[0].MemNode;
17419 unsigned FirstStoreAS = FirstInChain->getAddressSpace();
17420 unsigned FirstStoreAlign = FirstInChain->getAlignment();
17421 unsigned NumStoresToMerge = 1;
17422 for (unsigned i = 0; i < NumConsecutiveStores; ++i) {
17423 // Find a legal type for the vector store.
17424 unsigned Elts = (i + 1) * NumMemElts;
17425 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(), Elts);
17426 bool IsFast = false;
17427
17428 // Break early when size is too large to be legal.
17429 if (Ty.getSizeInBits() > MaximumLegalStoreInBits)
17430 break;
17431
17432 if (TLI.isTypeLegal(Ty) && TLI.canMergeStoresTo(FirstStoreAS, Ty, DAG) &&
17433 TLI.allowsMemoryAccess(Context, DL, Ty,
17434 *FirstInChain->getMemOperand(), &IsFast) &&
17435 IsFast)
17436 NumStoresToMerge = i + 1;
17437 }
17438
17439 // Check if we found a legal integer type creating a meaningful
17440 // merge.
17441 if (NumStoresToMerge < 2) {
17442 // We know that candidate stores are in order and of correct
17443 // shape. While there is no mergeable sequence from the
17444 // beginning one may start later in the sequence. The only
17445 // reason a merge of size N could have failed where another of
17446 // the same size would not have, is if the alignment has
17447 // improved. Drop as many candidates as we can here.
17448 unsigned NumSkip = 1;
17449 while ((NumSkip < NumConsecutiveStores) &&
17450 (StoreNodes[NumSkip].MemNode->getAlignment() <= FirstStoreAlign))
17451 NumSkip++;
17452
17453 StoreNodes.erase(StoreNodes.begin(), StoreNodes.begin() + NumSkip);
17454 NumConsecutiveStores -= NumSkip;
17455 continue;
17456 }
17457
17458 // Check that we can merge these candidates without causing a cycle.
17459 if (!checkMergeStoreCandidatesForDependencies(StoreNodes, NumStoresToMerge,
17460 RootNode)) {
17461 StoreNodes.erase(StoreNodes.begin(),
17462 StoreNodes.begin() + NumStoresToMerge);
17463 NumConsecutiveStores -= NumStoresToMerge;
17464 continue;
17465 }
17466
17467 MadeChange |= mergeStoresOfConstantsOrVecElts(
17468 StoreNodes, MemVT, NumStoresToMerge, /*IsConstantSrc*/ false,
17469 /*UseVector*/ true, /*UseTrunc*/ false);
17470
17471 StoreNodes.erase(StoreNodes.begin(), StoreNodes.begin() + NumStoresToMerge);
17472 NumConsecutiveStores -= NumStoresToMerge;
17473 }
17474 return MadeChange;
17475}
17476
17477bool DAGCombiner::tryStoreMergeOfLoads(SmallVectorImpl<MemOpLink> &StoreNodes,
17478 unsigned NumConsecutiveStores, EVT MemVT,
17479 SDNode *RootNode, bool AllowVectors,
17480 bool IsNonTemporalStore,
17481 bool IsNonTemporalLoad) {
17482 LLVMContext &Context = *DAG.getContext();
17483 const DataLayout &DL = DAG.getDataLayout();
17484 int64_t ElementSizeBytes = MemVT.getStoreSize();
17485 unsigned NumMemElts = MemVT.isVector() ? MemVT.getVectorNumElements() : 1;
17486 bool MadeChange = false;
17487
17488 // Look for load nodes which are used by the stored values.
17489 SmallVector<MemOpLink, 8> LoadNodes;
17490
17491 // Find acceptable loads. Loads need to have the same chain (token factor),
17492 // must not be zext, volatile, indexed, and they must be consecutive.
17493 BaseIndexOffset LdBasePtr;
17494
17495 for (unsigned i = 0; i < NumConsecutiveStores; ++i) {
17496 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
17497 SDValue Val = peekThroughBitcasts(St->getValue());
17498 LoadSDNode *Ld = cast<LoadSDNode>(Val);
17499
17500 BaseIndexOffset LdPtr = BaseIndexOffset::match(Ld, DAG);
17501 // If this is not the first ptr that we check.
17502 int64_t LdOffset = 0;
17503 if (LdBasePtr.getBase().getNode()) {
17504 // The base ptr must be the same.
17505 if (!LdBasePtr.equalBaseIndex(LdPtr, DAG, LdOffset))
17506 break;
17507 } else {
17508 // Check that all other base pointers are the same as this one.
17509 LdBasePtr = LdPtr;
17510 }
17511
17512 // We found a potential memory operand to merge.
17513 LoadNodes.push_back(MemOpLink(Ld, LdOffset));
17514 }
17515
17516 while (NumConsecutiveStores >= 2 && LoadNodes.size() >= 2) {
17517 Align RequiredAlignment;
17518 bool NeedRotate = false;
17519 if (LoadNodes.size() == 2) {
17520 // If we have load/store pair instructions and we only have two values,
17521 // don't bother merging.
17522 if (TLI.hasPairedLoad(MemVT, RequiredAlignment) &&
17523 StoreNodes[0].MemNode->getAlign() >= RequiredAlignment) {
17524 StoreNodes.erase(StoreNodes.begin(), StoreNodes.begin() + 2);
17525 LoadNodes.erase(LoadNodes.begin(), LoadNodes.begin() + 2);
17526 break;
17527 }
17528 // If the loads are reversed, see if we can rotate the halves into place.
17529 int64_t Offset0 = LoadNodes[0].OffsetFromBase;
17530 int64_t Offset1 = LoadNodes[1].OffsetFromBase;
17531 EVT PairVT = EVT::getIntegerVT(Context, ElementSizeBytes * 8 * 2);
17532 if (Offset0 - Offset1 == ElementSizeBytes &&
17533 (hasOperation(ISD::ROTL, PairVT) ||
17534 hasOperation(ISD::ROTR, PairVT))) {
17535 std::swap(LoadNodes[0], LoadNodes[1]);
17536 NeedRotate = true;
17537 }
17538 }
17539 LSBaseSDNode *FirstInChain = StoreNodes[0].MemNode;
17540 unsigned FirstStoreAS = FirstInChain->getAddressSpace();
17541 Align FirstStoreAlign = FirstInChain->getAlign();
17542 LoadSDNode *FirstLoad = cast<LoadSDNode>(LoadNodes[0].MemNode);
17543
17544 // Scan the memory operations on the chain and find the first
17545 // non-consecutive load memory address. These variables hold the index in
17546 // the store node array.
17547
17548 unsigned LastConsecutiveLoad = 1;
17549
17550 // This variable refers to the size and not index in the array.
17551 unsigned LastLegalVectorType = 1;
17552 unsigned LastLegalIntegerType = 1;
17553 bool isDereferenceable = true;
17554 bool DoIntegerTruncate = false;
17555 int64_t StartAddress = LoadNodes[0].OffsetFromBase;
17556 SDValue LoadChain = FirstLoad->getChain();
17557 for (unsigned i = 1; i < LoadNodes.size(); ++i) {
17558 // All loads must share the same chain.
17559 if (LoadNodes[i].MemNode->getChain() != LoadChain)
17560 break;
17561
17562 int64_t CurrAddress = LoadNodes[i].OffsetFromBase;
17563 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
17564 break;
17565 LastConsecutiveLoad = i;
17566
17567 if (isDereferenceable && !LoadNodes[i].MemNode->isDereferenceable())
17568 isDereferenceable = false;
17569
17570 // Find a legal type for the vector store.
17571 unsigned Elts = (i + 1) * NumMemElts;
17572 EVT StoreTy = EVT::getVectorVT(Context, MemVT.getScalarType(), Elts);
17573
17574 // Break early when size is too large to be legal.
17575 if (StoreTy.getSizeInBits() > MaximumLegalStoreInBits)
17576 break;
17577
17578 bool IsFastSt = false;
17579 bool IsFastLd = false;
17580 if (TLI.isTypeLegal(StoreTy) &&
17581 TLI.canMergeStoresTo(FirstStoreAS, StoreTy, DAG) &&
17582 TLI.allowsMemoryAccess(Context, DL, StoreTy,
17583 *FirstInChain->getMemOperand(), &IsFastSt) &&
17584 IsFastSt &&
17585 TLI.allowsMemoryAccess(Context, DL, StoreTy,
17586 *FirstLoad->getMemOperand(), &IsFastLd) &&
17587 IsFastLd) {
17588 LastLegalVectorType = i + 1;
17589 }
17590
17591 // Find a legal type for the integer store.
17592 unsigned SizeInBits = (i + 1) * ElementSizeBytes * 8;
17593 StoreTy = EVT::getIntegerVT(Context, SizeInBits);
17594 if (TLI.isTypeLegal(StoreTy) &&
17595 TLI.canMergeStoresTo(FirstStoreAS, StoreTy, DAG) &&
17596 TLI.allowsMemoryAccess(Context, DL, StoreTy,
17597 *FirstInChain->getMemOperand(), &IsFastSt) &&
17598 IsFastSt &&
17599 TLI.allowsMemoryAccess(Context, DL, StoreTy,
17600 *FirstLoad->getMemOperand(), &IsFastLd) &&
17601 IsFastLd) {
17602 LastLegalIntegerType = i + 1;
17603 DoIntegerTruncate = false;
17604 // Or check whether a truncstore and extload is legal.
17605 } else if (TLI.getTypeAction(Context, StoreTy) ==
17606 TargetLowering::TypePromoteInteger) {
17607 EVT LegalizedStoredValTy = TLI.getTypeToTransformTo(Context, StoreTy);
17608 if (TLI.isTruncStoreLegal(LegalizedStoredValTy, StoreTy) &&
17609 TLI.canMergeStoresTo(FirstStoreAS, LegalizedStoredValTy, DAG) &&
17610 TLI.isLoadExtLegal(ISD::ZEXTLOAD, LegalizedStoredValTy, StoreTy) &&
17611 TLI.isLoadExtLegal(ISD::SEXTLOAD, LegalizedStoredValTy, StoreTy) &&
17612 TLI.isLoadExtLegal(ISD::EXTLOAD, LegalizedStoredValTy, StoreTy) &&
17613 TLI.allowsMemoryAccess(Context, DL, StoreTy,
17614 *FirstInChain->getMemOperand(), &IsFastSt) &&
17615 IsFastSt &&
17616 TLI.allowsMemoryAccess(Context, DL, StoreTy,
17617 *FirstLoad->getMemOperand(), &IsFastLd) &&
17618 IsFastLd) {
17619 LastLegalIntegerType = i + 1;
17620 DoIntegerTruncate = true;
17621 }
17622 }
17623 }
17624
17625 // Only use vector types if the vector type is larger than the integer
17626 // type. If they are the same, use integers.
17627 bool UseVectorTy =
17628 LastLegalVectorType > LastLegalIntegerType && AllowVectors;
17629 unsigned LastLegalType =
17630 std::max(LastLegalVectorType, LastLegalIntegerType);
17631
17632 // We add +1 here because the LastXXX variables refer to location while
17633 // the NumElem refers to array/index size.
17634 unsigned NumElem = std::min(NumConsecutiveStores, LastConsecutiveLoad + 1);
17635 NumElem = std::min(LastLegalType, NumElem);
17636 Align FirstLoadAlign = FirstLoad->getAlign();
17637
17638 if (NumElem < 2) {
17639 // We know that candidate stores are in order and of correct
17640 // shape. While there is no mergeable sequence from the
17641 // beginning one may start later in the sequence. The only
17642 // reason a merge of size N could have failed where another of
17643 // the same size would not have is if the alignment or either
17644 // the load or store has improved. Drop as many candidates as we
17645 // can here.
17646 unsigned NumSkip = 1;
17647 while ((NumSkip < LoadNodes.size()) &&
17648 (LoadNodes[NumSkip].MemNode->getAlign() <= FirstLoadAlign) &&
17649 (StoreNodes[NumSkip].MemNode->getAlign() <= FirstStoreAlign))
17650 NumSkip++;
17651 StoreNodes.erase(StoreNodes.begin(), StoreNodes.begin() + NumSkip);
17652 LoadNodes.erase(LoadNodes.begin(), LoadNodes.begin() + NumSkip);
17653 NumConsecutiveStores -= NumSkip;
17654 continue;
17655 }
17656
17657 // Check that we can merge these candidates without causing a cycle.
17658 if (!checkMergeStoreCandidatesForDependencies(StoreNodes, NumElem,
17659 RootNode)) {
17660 StoreNodes.erase(StoreNodes.begin(), StoreNodes.begin() + NumElem);
17661 LoadNodes.erase(LoadNodes.begin(), LoadNodes.begin() + NumElem);
17662 NumConsecutiveStores -= NumElem;
17663 continue;
17664 }
17665
17666 // Find if it is better to use vectors or integers to load and store
17667 // to memory.
17668 EVT JointMemOpVT;
17669 if (UseVectorTy) {
17670 // Find a legal type for the vector store.
17671 unsigned Elts = NumElem * NumMemElts;
17672 JointMemOpVT = EVT::getVectorVT(Context, MemVT.getScalarType(), Elts);
17673 } else {
17674 unsigned SizeInBits = NumElem * ElementSizeBytes * 8;
17675 JointMemOpVT = EVT::getIntegerVT(Context, SizeInBits);
17676 }
17677
17678 SDLoc LoadDL(LoadNodes[0].MemNode);
17679 SDLoc StoreDL(StoreNodes[0].MemNode);
17680
17681 // The merged loads are required to have the same incoming chain, so
17682 // using the first's chain is acceptable.
17683
17684 SDValue NewStoreChain = getMergeStoreChains(StoreNodes, NumElem);
17685 AddToWorklist(NewStoreChain.getNode());
17686
17687 MachineMemOperand::Flags LdMMOFlags =
17688 isDereferenceable ? MachineMemOperand::MODereferenceable
17689 : MachineMemOperand::MONone;
17690 if (IsNonTemporalLoad)
17691 LdMMOFlags |= MachineMemOperand::MONonTemporal;
17692
17693 MachineMemOperand::Flags StMMOFlags = IsNonTemporalStore
17694 ? MachineMemOperand::MONonTemporal
17695 : MachineMemOperand::MONone;
17696
17697 SDValue NewLoad, NewStore;
17698 if (UseVectorTy || !DoIntegerTruncate) {
17699 NewLoad = DAG.getLoad(
17700 JointMemOpVT, LoadDL, FirstLoad->getChain(), FirstLoad->getBasePtr(),
17701 FirstLoad->getPointerInfo(), FirstLoadAlign, LdMMOFlags);
17702 SDValue StoreOp = NewLoad;
17703 if (NeedRotate) {
17704 unsigned LoadWidth = ElementSizeBytes * 8 * 2;
17705 assert(JointMemOpVT == EVT::getIntegerVT(Context, LoadWidth) &&(static_cast <bool> (JointMemOpVT == EVT::getIntegerVT(
Context, LoadWidth) && "Unexpected type for rotate-able load pair"
) ? void (0) : __assert_fail ("JointMemOpVT == EVT::getIntegerVT(Context, LoadWidth) && \"Unexpected type for rotate-able load pair\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 17706, __extension__ __PRETTY_FUNCTION__))
17706 "Unexpected type for rotate-able load pair")(static_cast <bool> (JointMemOpVT == EVT::getIntegerVT(
Context, LoadWidth) && "Unexpected type for rotate-able load pair"
) ? void (0) : __assert_fail ("JointMemOpVT == EVT::getIntegerVT(Context, LoadWidth) && \"Unexpected type for rotate-able load pair\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 17706, __extension__ __PRETTY_FUNCTION__))
;
17707 SDValue RotAmt =
17708 DAG.getShiftAmountConstant(LoadWidth / 2, JointMemOpVT, LoadDL);
17709 // Target can convert to the identical ROTR if it does not have ROTL.
17710 StoreOp = DAG.getNode(ISD::ROTL, LoadDL, JointMemOpVT, NewLoad, RotAmt);
17711 }
17712 NewStore = DAG.getStore(
17713 NewStoreChain, StoreDL, StoreOp, FirstInChain->getBasePtr(),
17714 FirstInChain->getPointerInfo(), FirstStoreAlign, StMMOFlags);
17715 } else { // This must be the truncstore/extload case
17716 EVT ExtendedTy =
17717 TLI.getTypeToTransformTo(*DAG.getContext(), JointMemOpVT);
17718 NewLoad = DAG.getExtLoad(ISD::EXTLOAD, LoadDL, ExtendedTy,
17719 FirstLoad->getChain(), FirstLoad->getBasePtr(),
17720 FirstLoad->getPointerInfo(), JointMemOpVT,
17721 FirstLoadAlign, LdMMOFlags);
17722 NewStore = DAG.getTruncStore(
17723 NewStoreChain, StoreDL, NewLoad, FirstInChain->getBasePtr(),
17724 FirstInChain->getPointerInfo(), JointMemOpVT,
17725 FirstInChain->getAlign(), FirstInChain->getMemOperand()->getFlags());
17726 }
17727
17728 // Transfer chain users from old loads to the new load.
17729 for (unsigned i = 0; i < NumElem; ++i) {
17730 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[i].MemNode);
17731 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1),
17732 SDValue(NewLoad.getNode(), 1));
17733 }
17734
17735 // Replace all stores with the new store. Recursively remove corresponding
17736 // values if they are no longer used.
17737 for (unsigned i = 0; i < NumElem; ++i) {
17738 SDValue Val = StoreNodes[i].MemNode->getOperand(1);
17739 CombineTo(StoreNodes[i].MemNode, NewStore);
17740 if (Val.getNode()->use_empty())
17741 recursivelyDeleteUnusedNodes(Val.getNode());
17742 }
17743
17744 MadeChange = true;
17745 StoreNodes.erase(StoreNodes.begin(), StoreNodes.begin() + NumElem);
17746 LoadNodes.erase(LoadNodes.begin(), LoadNodes.begin() + NumElem);
17747 NumConsecutiveStores -= NumElem;
17748 }
17749 return MadeChange;
17750}
17751
17752bool DAGCombiner::mergeConsecutiveStores(StoreSDNode *St) {
17753 if (OptLevel == CodeGenOpt::None || !EnableStoreMerging)
17754 return false;
17755
17756 // TODO: Extend this function to merge stores of scalable vectors.
17757 // (i.e. two <vscale x 8 x i8> stores can be merged to one <vscale x 16 x i8>
17758 // store since we know <vscale x 16 x i8> is exactly twice as large as
17759 // <vscale x 8 x i8>). Until then, bail out for scalable vectors.
17760 EVT MemVT = St->getMemoryVT();
17761 if (MemVT.isScalableVector())
17762 return false;
17763 if (!MemVT.isSimple() || MemVT.getSizeInBits() * 2 > MaximumLegalStoreInBits)
17764 return false;
17765
17766 // This function cannot currently deal with non-byte-sized memory sizes.
17767 int64_t ElementSizeBytes = MemVT.getStoreSize();
17768 if (ElementSizeBytes * 8 != (int64_t)MemVT.getSizeInBits())
17769 return false;
17770
17771 // Do not bother looking at stored values that are not constants, loads, or
17772 // extracted vector elements.
17773 SDValue StoredVal = peekThroughBitcasts(St->getValue());
17774 const StoreSource StoreSrc = getStoreSource(StoredVal);
17775 if (StoreSrc == StoreSource::Unknown)
17776 return false;
17777
17778 SmallVector<MemOpLink, 8> StoreNodes;
17779 SDNode *RootNode;
17780 // Find potential store merge candidates by searching through chain sub-DAG
17781 getStoreMergeCandidates(St, StoreNodes, RootNode);
17782
17783 // Check if there is anything to merge.
17784 if (StoreNodes.size() < 2)
17785 return false;
17786
17787 // Sort the memory operands according to their distance from the
17788 // base pointer.
17789 llvm::sort(StoreNodes, [](MemOpLink LHS, MemOpLink RHS) {
17790 return LHS.OffsetFromBase < RHS.OffsetFromBase;
17791 });
17792
17793 bool AllowVectors = !DAG.getMachineFunction().getFunction().hasFnAttribute(
17794 Attribute::NoImplicitFloat);
17795 bool IsNonTemporalStore = St->isNonTemporal();
17796 bool IsNonTemporalLoad = StoreSrc == StoreSource::Load &&
17797 cast<LoadSDNode>(StoredVal)->isNonTemporal();
17798
17799 // Store Merge attempts to merge the lowest stores. This generally
17800 // works out as if successful, as the remaining stores are checked
17801 // after the first collection of stores is merged. However, in the
17802 // case that a non-mergeable store is found first, e.g., {p[-2],
17803 // p[0], p[1], p[2], p[3]}, we would fail and miss the subsequent
17804 // mergeable cases. To prevent this, we prune such stores from the
17805 // front of StoreNodes here.
17806 bool MadeChange = false;
17807 while (StoreNodes.size() > 1) {
17808 unsigned NumConsecutiveStores =
17809 getConsecutiveStores(StoreNodes, ElementSizeBytes);
17810 // There are no more stores in the list to examine.
17811 if (NumConsecutiveStores == 0)
17812 return MadeChange;
17813
17814 // We have at least 2 consecutive stores. Try to merge them.
17815 assert(NumConsecutiveStores >= 2 && "Expected at least 2 stores")(static_cast <bool> (NumConsecutiveStores >= 2 &&
"Expected at least 2 stores") ? void (0) : __assert_fail ("NumConsecutiveStores >= 2 && \"Expected at least 2 stores\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 17815, __extension__ __PRETTY_FUNCTION__))
;
17816 switch (StoreSrc) {
17817 case StoreSource::Constant:
17818 MadeChange |= tryStoreMergeOfConstants(StoreNodes, NumConsecutiveStores,
17819 MemVT, RootNode, AllowVectors);
17820 break;
17821
17822 case StoreSource::Extract:
17823 MadeChange |= tryStoreMergeOfExtracts(StoreNodes, NumConsecutiveStores,
17824 MemVT, RootNode);
17825 break;
17826
17827 case StoreSource::Load:
17828 MadeChange |= tryStoreMergeOfLoads(StoreNodes, NumConsecutiveStores,
17829 MemVT, RootNode, AllowVectors,
17830 IsNonTemporalStore, IsNonTemporalLoad);
17831 break;
17832
17833 default:
17834 llvm_unreachable("Unhandled store source type")::llvm::llvm_unreachable_internal("Unhandled store source type"
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 17834)
;
17835 }
17836 }
17837 return MadeChange;
17838}
17839
17840SDValue DAGCombiner::replaceStoreChain(StoreSDNode *ST, SDValue BetterChain) {
17841 SDLoc SL(ST);
17842 SDValue ReplStore;
17843
17844 // Replace the chain to avoid dependency.
17845 if (ST->isTruncatingStore()) {
17846 ReplStore = DAG.getTruncStore(BetterChain, SL, ST->getValue(),
17847 ST->getBasePtr(), ST->getMemoryVT(),
17848 ST->getMemOperand());
17849 } else {
17850 ReplStore = DAG.getStore(BetterChain, SL, ST->getValue(), ST->getBasePtr(),
17851 ST->getMemOperand());
17852 }
17853
17854 // Create token to keep both nodes around.
17855 SDValue Token = DAG.getNode(ISD::TokenFactor, SL,
17856 MVT::Other, ST->getChain(), ReplStore);
17857
17858 // Make sure the new and old chains are cleaned up.
17859 AddToWorklist(Token.getNode());
17860
17861 // Don't add users to work list.
17862 return CombineTo(ST, Token, false);
17863}
17864
17865SDValue DAGCombiner::replaceStoreOfFPConstant(StoreSDNode *ST) {
17866 SDValue Value = ST->getValue();
17867 if (Value.getOpcode() == ISD::TargetConstantFP)
17868 return SDValue();
17869
17870 if (!ISD::isNormalStore(ST))
17871 return SDValue();
17872
17873 SDLoc DL(ST);
17874
17875 SDValue Chain = ST->getChain();
17876 SDValue Ptr = ST->getBasePtr();
17877
17878 const ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Value);
17879
17880 // NOTE: If the original store is volatile, this transform must not increase
17881 // the number of stores. For example, on x86-32 an f64 can be stored in one
17882 // processor operation but an i64 (which is not legal) requires two. So the
17883 // transform should not be done in this case.
17884
17885 SDValue Tmp;
17886 switch (CFP->getSimpleValueType(0).SimpleTy) {
17887 default:
17888 llvm_unreachable("Unknown FP type")::llvm::llvm_unreachable_internal("Unknown FP type", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 17888)
;
17889 case MVT::f16: // We don't do this for these yet.
17890 case MVT::f80:
17891 case MVT::f128:
17892 case MVT::ppcf128:
17893 return SDValue();
17894 case MVT::f32:
17895 if ((isTypeLegal(MVT::i32) && !LegalOperations && ST->isSimple()) ||
17896 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
17897 ;
17898 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
17899 bitcastToAPInt().getZExtValue(), SDLoc(CFP),
17900 MVT::i32);
17901 return DAG.getStore(Chain, DL, Tmp, Ptr, ST->getMemOperand());
17902 }
17903
17904 return SDValue();
17905 case MVT::f64:
17906 if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations &&
17907 ST->isSimple()) ||
17908 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
17909 ;
17910 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
17911 getZExtValue(), SDLoc(CFP), MVT::i64);
17912 return DAG.getStore(Chain, DL, Tmp,
17913 Ptr, ST->getMemOperand());
17914 }
17915
17916 if (ST->isSimple() &&
17917 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
17918 // Many FP stores are not made apparent until after legalize, e.g. for
17919 // argument passing. Since this is so common, custom legalize the
17920 // 64-bit integer store into two 32-bit stores.
17921 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
17922 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, SDLoc(CFP), MVT::i32);
17923 SDValue Hi = DAG.getConstant(Val >> 32, SDLoc(CFP), MVT::i32);
17924 if (DAG.getDataLayout().isBigEndian())
17925 std::swap(Lo, Hi);
17926
17927 MachineMemOperand::Flags MMOFlags = ST->getMemOperand()->getFlags();
17928 AAMDNodes AAInfo = ST->getAAInfo();
17929
17930 SDValue St0 = DAG.getStore(Chain, DL, Lo, Ptr, ST->getPointerInfo(),
17931 ST->getOriginalAlign(), MMOFlags, AAInfo);
17932 Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::Fixed(4), DL);
17933 SDValue St1 = DAG.getStore(Chain, DL, Hi, Ptr,
17934 ST->getPointerInfo().getWithOffset(4),
17935 ST->getOriginalAlign(), MMOFlags, AAInfo);
17936 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
17937 St0, St1);
17938 }
17939
17940 return SDValue();
17941 }
17942}
17943
17944SDValue DAGCombiner::visitSTORE(SDNode *N) {
17945 StoreSDNode *ST = cast<StoreSDNode>(N);
17946 SDValue Chain = ST->getChain();
17947 SDValue Value = ST->getValue();
17948 SDValue Ptr = ST->getBasePtr();
17949
17950 // If this is a store of a bit convert, store the input value if the
17951 // resultant store does not need a higher alignment than the original.
17952 if (Value.getOpcode() == ISD::BITCAST && !ST->isTruncatingStore() &&
17953 ST->isUnindexed()) {
17954 EVT SVT = Value.getOperand(0).getValueType();
17955 // If the store is volatile, we only want to change the store type if the
17956 // resulting store is legal. Otherwise we might increase the number of
17957 // memory accesses. We don't care if the original type was legal or not
17958 // as we assume software couldn't rely on the number of accesses of an
17959 // illegal type.
17960 // TODO: May be able to relax for unordered atomics (see D66309)
17961 if (((!LegalOperations && ST->isSimple()) ||
17962 TLI.isOperationLegal(ISD::STORE, SVT)) &&
17963 TLI.isStoreBitCastBeneficial(Value.getValueType(), SVT,
17964 DAG, *ST->getMemOperand())) {
17965 return DAG.getStore(Chain, SDLoc(N), Value.getOperand(0), Ptr,
17966 ST->getMemOperand());
17967 }
17968 }
17969
17970 // Turn 'store undef, Ptr' -> nothing.
17971 if (Value.isUndef() && ST->isUnindexed())
17972 return Chain;
17973
17974 // Try to infer better alignment information than the store already has.
17975 if (OptLevel != CodeGenOpt::None && ST->isUnindexed() && !ST->isAtomic()) {
17976 if (MaybeAlign Alignment = DAG.InferPtrAlign(Ptr)) {
17977 if (*Alignment > ST->getAlign() &&
17978 isAligned(*Alignment, ST->getSrcValueOffset())) {
17979 SDValue NewStore =
17980 DAG.getTruncStore(Chain, SDLoc(N), Value, Ptr, ST->getPointerInfo(),
17981 ST->getMemoryVT(), *Alignment,
17982 ST->getMemOperand()->getFlags(), ST->getAAInfo());
17983 // NewStore will always be N as we are only refining the alignment
17984 assert(NewStore.getNode() == N)(static_cast <bool> (NewStore.getNode() == N) ? void (0
) : __assert_fail ("NewStore.getNode() == N", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 17984, __extension__ __PRETTY_FUNCTION__))
;
17985 (void)NewStore;
17986 }
17987 }
17988 }
17989
17990 // Try transforming a pair floating point load / store ops to integer
17991 // load / store ops.
17992 if (SDValue NewST = TransformFPLoadStorePair(N))
17993 return NewST;
17994
17995 // Try transforming several stores into STORE (BSWAP).
17996 if (SDValue Store = mergeTruncStores(ST))
17997 return Store;
17998
17999 if (ST->isUnindexed()) {
18000 // Walk up chain skipping non-aliasing memory nodes, on this store and any
18001 // adjacent stores.
18002 if (findBetterNeighborChains(ST)) {
18003 // replaceStoreChain uses CombineTo, which handled all of the worklist
18004 // manipulation. Return the original node to not do anything else.
18005 return SDValue(ST, 0);
18006 }
18007 Chain = ST->getChain();
18008 }
18009
18010 // FIXME: is there such a thing as a truncating indexed store?
18011 if (ST->isTruncatingStore() && ST->isUnindexed() &&
18012 Value.getValueType().isInteger() &&
18013 (!isa<ConstantSDNode>(Value) ||
18014 !cast<ConstantSDNode>(Value)->isOpaque())) {
18015 APInt TruncDemandedBits =
18016 APInt::getLowBitsSet(Value.getScalarValueSizeInBits(),
18017 ST->getMemoryVT().getScalarSizeInBits());
18018
18019 // See if we can simplify the input to this truncstore with knowledge that
18020 // only the low bits are being used. For example:
18021 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
18022 AddToWorklist(Value.getNode());
18023 if (SDValue Shorter = DAG.GetDemandedBits(Value, TruncDemandedBits))
18024 return DAG.getTruncStore(Chain, SDLoc(N), Shorter, Ptr, ST->getMemoryVT(),
18025 ST->getMemOperand());
18026
18027 // Otherwise, see if we can simplify the operation with
18028 // SimplifyDemandedBits, which only works if the value has a single use.
18029 if (SimplifyDemandedBits(Value, TruncDemandedBits)) {
18030 // Re-visit the store if anything changed and the store hasn't been merged
18031 // with another node (N is deleted) SimplifyDemandedBits will add Value's
18032 // node back to the worklist if necessary, but we also need to re-visit
18033 // the Store node itself.
18034 if (N->getOpcode() != ISD::DELETED_NODE)
18035 AddToWorklist(N);
18036 return SDValue(N, 0);
18037 }
18038 }
18039
18040 // If this is a load followed by a store to the same location, then the store
18041 // is dead/noop.
18042 // TODO: Can relax for unordered atomics (see D66309)
18043 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
18044 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
18045 ST->isUnindexed() && ST->isSimple() &&
18046 Ld->getAddressSpace() == ST->getAddressSpace() &&
18047 // There can't be any side effects between the load and store, such as
18048 // a call or store.
18049 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
18050 // The store is dead, remove it.
18051 return Chain;
18052 }
18053 }
18054
18055 // TODO: Can relax for unordered atomics (see D66309)
18056 if (StoreSDNode *ST1 = dyn_cast<StoreSDNode>(Chain)) {
18057 if (ST->isUnindexed() && ST->isSimple() &&
18058 ST1->isUnindexed() && ST1->isSimple()) {
18059 if (ST1->getBasePtr() == Ptr && ST1->getValue() == Value &&
18060 ST->getMemoryVT() == ST1->getMemoryVT() &&
18061 ST->getAddressSpace() == ST1->getAddressSpace()) {
18062 // If this is a store followed by a store with the same value to the
18063 // same location, then the store is dead/noop.
18064 return Chain;
18065 }
18066
18067 if (OptLevel != CodeGenOpt::None && ST1->hasOneUse() &&
18068 !ST1->getBasePtr().isUndef() &&
18069 // BaseIndexOffset and the code below requires knowing the size
18070 // of a vector, so bail out if MemoryVT is scalable.
18071 !ST->getMemoryVT().isScalableVector() &&
18072 !ST1->getMemoryVT().isScalableVector() &&
18073 ST->getAddressSpace() == ST1->getAddressSpace()) {
18074 const BaseIndexOffset STBase = BaseIndexOffset::match(ST, DAG);
18075 const BaseIndexOffset ChainBase = BaseIndexOffset::match(ST1, DAG);
18076 unsigned STBitSize = ST->getMemoryVT().getFixedSizeInBits();
18077 unsigned ChainBitSize = ST1->getMemoryVT().getFixedSizeInBits();
18078 // If this is a store who's preceding store to a subset of the current
18079 // location and no one other node is chained to that store we can
18080 // effectively drop the store. Do not remove stores to undef as they may
18081 // be used as data sinks.
18082 if (STBase.contains(DAG, STBitSize, ChainBase, ChainBitSize)) {
18083 CombineTo(ST1, ST1->getChain());
18084 return SDValue();
18085 }
18086 }
18087 }
18088 }
18089
18090 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
18091 // truncating store. We can do this even if this is already a truncstore.
18092 if ((Value.getOpcode() == ISD::FP_ROUND ||
18093 Value.getOpcode() == ISD::TRUNCATE) &&
18094 Value.getNode()->hasOneUse() && ST->isUnindexed() &&
18095 TLI.canCombineTruncStore(Value.getOperand(0).getValueType(),
18096 ST->getMemoryVT(), LegalOperations)) {
18097 return DAG.getTruncStore(Chain, SDLoc(N), Value.getOperand(0),
18098 Ptr, ST->getMemoryVT(), ST->getMemOperand());
18099 }
18100
18101 // Always perform this optimization before types are legal. If the target
18102 // prefers, also try this after legalization to catch stores that were created
18103 // by intrinsics or other nodes.
18104 if (!LegalTypes || (TLI.mergeStoresAfterLegalization(ST->getMemoryVT()))) {
18105 while (true) {
18106 // There can be multiple store sequences on the same chain.
18107 // Keep trying to merge store sequences until we are unable to do so
18108 // or until we merge the last store on the chain.
18109 bool Changed = mergeConsecutiveStores(ST);
18110 if (!Changed) break;
18111 // Return N as merge only uses CombineTo and no worklist clean
18112 // up is necessary.
18113 if (N->getOpcode() == ISD::DELETED_NODE || !isa<StoreSDNode>(N))
18114 return SDValue(N, 0);
18115 }
18116 }
18117
18118 // Try transforming N to an indexed store.
18119 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
18120 return SDValue(N, 0);
18121
18122 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
18123 //
18124 // Make sure to do this only after attempting to merge stores in order to
18125 // avoid changing the types of some subset of stores due to visit order,
18126 // preventing their merging.
18127 if (isa<ConstantFPSDNode>(ST->getValue())) {
18128 if (SDValue NewSt = replaceStoreOfFPConstant(ST))
18129 return NewSt;
18130 }
18131
18132 if (SDValue NewSt = splitMergedValStore(ST))
18133 return NewSt;
18134
18135 return ReduceLoadOpStoreWidth(N);
18136}
18137
18138SDValue DAGCombiner::visitLIFETIME_END(SDNode *N) {
18139 const auto *LifetimeEnd = cast<LifetimeSDNode>(N);
18140 if (!LifetimeEnd->hasOffset())
18141 return SDValue();
18142
18143 const BaseIndexOffset LifetimeEndBase(N->getOperand(1), SDValue(),
18144 LifetimeEnd->getOffset(), false);
18145
18146 // We walk up the chains to find stores.
18147 SmallVector<SDValue, 8> Chains = {N->getOperand(0)};
18148 while (!Chains.empty()) {
18149 SDValue Chain = Chains.pop_back_val();
18150 if (!Chain.hasOneUse())
18151 continue;
18152 switch (Chain.getOpcode()) {
18153 case ISD::TokenFactor:
18154 for (unsigned Nops = Chain.getNumOperands(); Nops;)
18155 Chains.push_back(Chain.getOperand(--Nops));
18156 break;
18157 case ISD::LIFETIME_START:
18158 case ISD::LIFETIME_END:
18159 // We can forward past any lifetime start/end that can be proven not to
18160 // alias the node.
18161 if (!isAlias(Chain.getNode(), N))
18162 Chains.push_back(Chain.getOperand(0));
18163 break;
18164 case ISD::STORE: {
18165 StoreSDNode *ST = dyn_cast<StoreSDNode>(Chain);
18166 // TODO: Can relax for unordered atomics (see D66309)
18167 if (!ST->isSimple() || ST->isIndexed())
18168 continue;
18169 const TypeSize StoreSize = ST->getMemoryVT().getStoreSize();
18170 // The bounds of a scalable store are not known until runtime, so this
18171 // store cannot be elided.
18172 if (StoreSize.isScalable())
18173 continue;
18174 const BaseIndexOffset StoreBase = BaseIndexOffset::match(ST, DAG);
18175 // If we store purely within object bounds just before its lifetime ends,
18176 // we can remove the store.
18177 if (LifetimeEndBase.contains(DAG, LifetimeEnd->getSize() * 8, StoreBase,
18178 StoreSize.getFixedSize() * 8)) {
18179 LLVM_DEBUG(dbgs() << "\nRemoving store:"; StoreBase.dump();do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nRemoving store:"; StoreBase
.dump(); dbgs() << "\nwithin LIFETIME_END of : "; LifetimeEndBase
.dump(); dbgs() << "\n"; } } while (false)
18180 dbgs() << "\nwithin LIFETIME_END of : ";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nRemoving store:"; StoreBase
.dump(); dbgs() << "\nwithin LIFETIME_END of : "; LifetimeEndBase
.dump(); dbgs() << "\n"; } } while (false)
18181 LifetimeEndBase.dump(); dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("dagcombine")) { dbgs() << "\nRemoving store:"; StoreBase
.dump(); dbgs() << "\nwithin LIFETIME_END of : "; LifetimeEndBase
.dump(); dbgs() << "\n"; } } while (false)
;
18182 CombineTo(ST, ST->getChain());
18183 return SDValue(N, 0);
18184 }
18185 }
18186 }
18187 }
18188 return SDValue();
18189}
18190
18191/// For the instruction sequence of store below, F and I values
18192/// are bundled together as an i64 value before being stored into memory.
18193/// Sometimes it is more efficent to generate separate stores for F and I,
18194/// which can remove the bitwise instructions or sink them to colder places.
18195///
18196/// (store (or (zext (bitcast F to i32) to i64),
18197/// (shl (zext I to i64), 32)), addr) -->
18198/// (store F, addr) and (store I, addr+4)
18199///
18200/// Similarly, splitting for other merged store can also be beneficial, like:
18201/// For pair of {i32, i32}, i64 store --> two i32 stores.
18202/// For pair of {i32, i16}, i64 store --> two i32 stores.
18203/// For pair of {i16, i16}, i32 store --> two i16 stores.
18204/// For pair of {i16, i8}, i32 store --> two i16 stores.
18205/// For pair of {i8, i8}, i16 store --> two i8 stores.
18206///
18207/// We allow each target to determine specifically which kind of splitting is
18208/// supported.
18209///
18210/// The store patterns are commonly seen from the simple code snippet below
18211/// if only std::make_pair(...) is sroa transformed before inlined into hoo.
18212/// void goo(const std::pair<int, float> &);
18213/// hoo() {
18214/// ...
18215/// goo(std::make_pair(tmp, ftmp));
18216/// ...
18217/// }
18218///
18219SDValue DAGCombiner::splitMergedValStore(StoreSDNode *ST) {
18220 if (OptLevel == CodeGenOpt::None)
18221 return SDValue();
18222
18223 // Can't change the number of memory accesses for a volatile store or break
18224 // atomicity for an atomic one.
18225 if (!ST->isSimple())
18226 return SDValue();
18227
18228 SDValue Val = ST->getValue();
18229 SDLoc DL(ST);
18230
18231 // Match OR operand.
18232 if (!Val.getValueType().isScalarInteger() || Val.getOpcode() != ISD::OR)
18233 return SDValue();
18234
18235 // Match SHL operand and get Lower and Higher parts of Val.
18236 SDValue Op1 = Val.getOperand(0);
18237 SDValue Op2 = Val.getOperand(1);
18238 SDValue Lo, Hi;
18239 if (Op1.getOpcode() != ISD::SHL) {
18240 std::swap(Op1, Op2);
18241 if (Op1.getOpcode() != ISD::SHL)
18242 return SDValue();
18243 }
18244 Lo = Op2;
18245 Hi = Op1.getOperand(0);
18246 if (!Op1.hasOneUse())
18247 return SDValue();
18248
18249 // Match shift amount to HalfValBitSize.
18250 unsigned HalfValBitSize = Val.getValueSizeInBits() / 2;
18251 ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op1.getOperand(1));
18252 if (!ShAmt || ShAmt->getAPIntValue() != HalfValBitSize)
18253 return SDValue();
18254
18255 // Lo and Hi are zero-extended from int with size less equal than 32
18256 // to i64.
18257 if (Lo.getOpcode() != ISD::ZERO_EXTEND || !Lo.hasOneUse() ||
18258 !Lo.getOperand(0).getValueType().isScalarInteger() ||
18259 Lo.getOperand(0).getValueSizeInBits() > HalfValBitSize ||
18260 Hi.getOpcode() != ISD::ZERO_EXTEND || !Hi.hasOneUse() ||
18261 !Hi.getOperand(0).getValueType().isScalarInteger() ||
18262 Hi.getOperand(0).getValueSizeInBits() > HalfValBitSize)
18263 return SDValue();
18264
18265 // Use the EVT of low and high parts before bitcast as the input
18266 // of target query.
18267 EVT LowTy = (Lo.getOperand(0).getOpcode() == ISD::BITCAST)
18268 ? Lo.getOperand(0).getValueType()
18269 : Lo.getValueType();
18270 EVT HighTy = (Hi.getOperand(0).getOpcode() == ISD::BITCAST)
18271 ? Hi.getOperand(0).getValueType()
18272 : Hi.getValueType();
18273 if (!TLI.isMultiStoresCheaperThanBitsMerge(LowTy, HighTy))
18274 return SDValue();
18275
18276 // Start to split store.
18277 MachineMemOperand::Flags MMOFlags = ST->getMemOperand()->getFlags();
18278 AAMDNodes AAInfo = ST->getAAInfo();
18279
18280 // Change the sizes of Lo and Hi's value types to HalfValBitSize.
18281 EVT VT = EVT::getIntegerVT(*DAG.getContext(), HalfValBitSize);
18282 Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Lo.getOperand(0));
18283 Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Hi.getOperand(0));
18284
18285 SDValue Chain = ST->getChain();
18286 SDValue Ptr = ST->getBasePtr();
18287 // Lower value store.
18288 SDValue St0 = DAG.getStore(Chain, DL, Lo, Ptr, ST->getPointerInfo(),
18289 ST->getOriginalAlign(), MMOFlags, AAInfo);
18290 Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::Fixed(HalfValBitSize / 8), DL);
18291 // Higher value store.
18292 SDValue St1 = DAG.getStore(
18293 St0, DL, Hi, Ptr, ST->getPointerInfo().getWithOffset(HalfValBitSize / 8),
18294 ST->getOriginalAlign(), MMOFlags, AAInfo);
18295 return St1;
18296}
18297
18298/// Convert a disguised subvector insertion into a shuffle:
18299SDValue DAGCombiner::combineInsertEltToShuffle(SDNode *N, unsigned InsIndex) {
18300 assert(N->getOpcode() == ISD::INSERT_VECTOR_ELT &&(static_cast <bool> (N->getOpcode() == ISD::INSERT_VECTOR_ELT
&& "Expected extract_vector_elt") ? void (0) : __assert_fail
("N->getOpcode() == ISD::INSERT_VECTOR_ELT && \"Expected extract_vector_elt\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 18301, __extension__ __PRETTY_FUNCTION__))
18301 "Expected extract_vector_elt")(static_cast <bool> (N->getOpcode() == ISD::INSERT_VECTOR_ELT
&& "Expected extract_vector_elt") ? void (0) : __assert_fail
("N->getOpcode() == ISD::INSERT_VECTOR_ELT && \"Expected extract_vector_elt\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 18301, __extension__ __PRETTY_FUNCTION__))
;
18302 SDValue InsertVal = N->getOperand(1);
18303 SDValue Vec = N->getOperand(0);
18304
18305 // (insert_vector_elt (vector_shuffle X, Y), (extract_vector_elt X, N),
18306 // InsIndex)
18307 // --> (vector_shuffle X, Y) and variations where shuffle operands may be
18308 // CONCAT_VECTORS.
18309 if (Vec.getOpcode() == ISD::VECTOR_SHUFFLE && Vec.hasOneUse() &&
18310 InsertVal.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
18311 isa<ConstantSDNode>(InsertVal.getOperand(1))) {
18312 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Vec.getNode());
18313 ArrayRef<int> Mask = SVN->getMask();
18314
18315 SDValue X = Vec.getOperand(0);
18316 SDValue Y = Vec.getOperand(1);
18317
18318 // Vec's operand 0 is using indices from 0 to N-1 and
18319 // operand 1 from N to 2N - 1, where N is the number of
18320 // elements in the vectors.
18321 SDValue InsertVal0 = InsertVal.getOperand(0);
18322 int ElementOffset = -1;
18323
18324 // We explore the inputs of the shuffle in order to see if we find the
18325 // source of the extract_vector_elt. If so, we can use it to modify the
18326 // shuffle rather than perform an insert_vector_elt.
18327 SmallVector<std::pair<int, SDValue>, 8> ArgWorkList;
18328 ArgWorkList.emplace_back(Mask.size(), Y);
18329 ArgWorkList.emplace_back(0, X);
18330
18331 while (!ArgWorkList.empty()) {
18332 int ArgOffset;
18333 SDValue ArgVal;
18334 std::tie(ArgOffset, ArgVal) = ArgWorkList.pop_back_val();
18335
18336 if (ArgVal == InsertVal0) {
18337 ElementOffset = ArgOffset;
18338 break;
18339 }
18340
18341 // Peek through concat_vector.
18342 if (ArgVal.getOpcode() == ISD::CONCAT_VECTORS) {
18343 int CurrentArgOffset =
18344 ArgOffset + ArgVal.getValueType().getVectorNumElements();
18345 int Step = ArgVal.getOperand(0).getValueType().getVectorNumElements();
18346 for (SDValue Op : reverse(ArgVal->ops())) {
18347 CurrentArgOffset -= Step;
18348 ArgWorkList.emplace_back(CurrentArgOffset, Op);
18349 }
18350
18351 // Make sure we went through all the elements and did not screw up index
18352 // computation.
18353 assert(CurrentArgOffset == ArgOffset)(static_cast <bool> (CurrentArgOffset == ArgOffset) ? void
(0) : __assert_fail ("CurrentArgOffset == ArgOffset", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 18353, __extension__ __PRETTY_FUNCTION__))
;
18354 }
18355 }
18356
18357 if (ElementOffset != -1) {
18358 SmallVector<int, 16> NewMask(Mask.begin(), Mask.end());
18359
18360 auto *ExtrIndex = cast<ConstantSDNode>(InsertVal.getOperand(1));
18361 NewMask[InsIndex] = ElementOffset + ExtrIndex->getZExtValue();
18362 assert(NewMask[InsIndex] <(static_cast <bool> (NewMask[InsIndex] < (int)(2 * Vec
.getValueType().getVectorNumElements()) && NewMask[InsIndex
] >= 0 && "NewMask[InsIndex] is out of bound") ? void
(0) : __assert_fail ("NewMask[InsIndex] < (int)(2 * Vec.getValueType().getVectorNumElements()) && NewMask[InsIndex] >= 0 && \"NewMask[InsIndex] is out of bound\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 18364, __extension__ __PRETTY_FUNCTION__))
18363 (int)(2 * Vec.getValueType().getVectorNumElements()) &&(static_cast <bool> (NewMask[InsIndex] < (int)(2 * Vec
.getValueType().getVectorNumElements()) && NewMask[InsIndex
] >= 0 && "NewMask[InsIndex] is out of bound") ? void
(0) : __assert_fail ("NewMask[InsIndex] < (int)(2 * Vec.getValueType().getVectorNumElements()) && NewMask[InsIndex] >= 0 && \"NewMask[InsIndex] is out of bound\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 18364, __extension__ __PRETTY_FUNCTION__))
18364 NewMask[InsIndex] >= 0 && "NewMask[InsIndex] is out of bound")(static_cast <bool> (NewMask[InsIndex] < (int)(2 * Vec
.getValueType().getVectorNumElements()) && NewMask[InsIndex
] >= 0 && "NewMask[InsIndex] is out of bound") ? void
(0) : __assert_fail ("NewMask[InsIndex] < (int)(2 * Vec.getValueType().getVectorNumElements()) && NewMask[InsIndex] >= 0 && \"NewMask[InsIndex] is out of bound\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 18364, __extension__ __PRETTY_FUNCTION__))
;
18365
18366 SDValue LegalShuffle =
18367 TLI.buildLegalVectorShuffle(Vec.getValueType(), SDLoc(N), X,
18368 Y, NewMask, DAG);
18369 if (LegalShuffle)
18370 return LegalShuffle;
18371 }
18372 }
18373
18374 // insert_vector_elt V, (bitcast X from vector type), IdxC -->
18375 // bitcast(shuffle (bitcast V), (extended X), Mask)
18376 // Note: We do not use an insert_subvector node because that requires a
18377 // legal subvector type.
18378 if (InsertVal.getOpcode() != ISD::BITCAST || !InsertVal.hasOneUse() ||
18379 !InsertVal.getOperand(0).getValueType().isVector())
18380 return SDValue();
18381
18382 SDValue SubVec = InsertVal.getOperand(0);
18383 SDValue DestVec = N->getOperand(0);
18384 EVT SubVecVT = SubVec.getValueType();
18385 EVT VT = DestVec.getValueType();
18386 unsigned NumSrcElts = SubVecVT.getVectorNumElements();
18387 // If the source only has a single vector element, the cost of creating adding
18388 // it to a vector is likely to exceed the cost of a insert_vector_elt.
18389 if (NumSrcElts == 1)
18390 return SDValue();
18391 unsigned ExtendRatio = VT.getSizeInBits() / SubVecVT.getSizeInBits();
18392 unsigned NumMaskVals = ExtendRatio * NumSrcElts;
18393
18394 // Step 1: Create a shuffle mask that implements this insert operation. The
18395 // vector that we are inserting into will be operand 0 of the shuffle, so
18396 // those elements are just 'i'. The inserted subvector is in the first
18397 // positions of operand 1 of the shuffle. Example:
18398 // insert v4i32 V, (v2i16 X), 2 --> shuffle v8i16 V', X', {0,1,2,3,8,9,6,7}
18399 SmallVector<int, 16> Mask(NumMaskVals);
18400 for (unsigned i = 0; i != NumMaskVals; ++i) {
18401 if (i / NumSrcElts == InsIndex)
18402 Mask[i] = (i % NumSrcElts) + NumMaskVals;
18403 else
18404 Mask[i] = i;
18405 }
18406
18407 // Bail out if the target can not handle the shuffle we want to create.
18408 EVT SubVecEltVT = SubVecVT.getVectorElementType();
18409 EVT ShufVT = EVT::getVectorVT(*DAG.getContext(), SubVecEltVT, NumMaskVals);
18410 if (!TLI.isShuffleMaskLegal(Mask, ShufVT))
18411 return SDValue();
18412
18413 // Step 2: Create a wide vector from the inserted source vector by appending
18414 // undefined elements. This is the same size as our destination vector.
18415 SDLoc DL(N);
18416 SmallVector<SDValue, 8> ConcatOps(ExtendRatio, DAG.getUNDEF(SubVecVT));
18417 ConcatOps[0] = SubVec;
18418 SDValue PaddedSubV = DAG.getNode(ISD::CONCAT_VECTORS, DL, ShufVT, ConcatOps);
18419
18420 // Step 3: Shuffle in the padded subvector.
18421 SDValue DestVecBC = DAG.getBitcast(ShufVT, DestVec);
18422 SDValue Shuf = DAG.getVectorShuffle(ShufVT, DL, DestVecBC, PaddedSubV, Mask);
18423 AddToWorklist(PaddedSubV.getNode());
18424 AddToWorklist(DestVecBC.getNode());
18425 AddToWorklist(Shuf.getNode());
18426 return DAG.getBitcast(VT, Shuf);
18427}
18428
18429SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
18430 SDValue InVec = N->getOperand(0);
18431 SDValue InVal = N->getOperand(1);
18432 SDValue EltNo = N->getOperand(2);
18433 SDLoc DL(N);
18434
18435 EVT VT = InVec.getValueType();
18436 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
18437
18438 // Insert into out-of-bounds element is undefined.
18439 if (IndexC && VT.isFixedLengthVector() &&
18440 IndexC->getZExtValue() >= VT.getVectorNumElements())
18441 return DAG.getUNDEF(VT);
18442
18443 // Remove redundant insertions:
18444 // (insert_vector_elt x (extract_vector_elt x idx) idx) -> x
18445 if (InVal.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
18446 InVec == InVal.getOperand(0) && EltNo == InVal.getOperand(1))
18447 return InVec;
18448
18449 if (!IndexC) {
18450 // If this is variable insert to undef vector, it might be better to splat:
18451 // inselt undef, InVal, EltNo --> build_vector < InVal, InVal, ... >
18452 if (InVec.isUndef() && TLI.shouldSplatInsEltVarIndex(VT)) {
18453 if (VT.isScalableVector())
18454 return DAG.getSplatVector(VT, DL, InVal);
18455 else {
18456 SmallVector<SDValue, 8> Ops(VT.getVectorNumElements(), InVal);
18457 return DAG.getBuildVector(VT, DL, Ops);
18458 }
18459 }
18460 return SDValue();
18461 }
18462
18463 if (VT.isScalableVector())
18464 return SDValue();
18465
18466 unsigned NumElts = VT.getVectorNumElements();
18467
18468 // We must know which element is being inserted for folds below here.
18469 unsigned Elt = IndexC->getZExtValue();
18470 if (SDValue Shuf = combineInsertEltToShuffle(N, Elt))
18471 return Shuf;
18472
18473 // Canonicalize insert_vector_elt dag nodes.
18474 // Example:
18475 // (insert_vector_elt (insert_vector_elt A, Idx0), Idx1)
18476 // -> (insert_vector_elt (insert_vector_elt A, Idx1), Idx0)
18477 //
18478 // Do this only if the child insert_vector node has one use; also
18479 // do this only if indices are both constants and Idx1 < Idx0.
18480 if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT && InVec.hasOneUse()
18481 && isa<ConstantSDNode>(InVec.getOperand(2))) {
18482 unsigned OtherElt = InVec.getConstantOperandVal(2);
18483 if (Elt < OtherElt) {
18484 // Swap nodes.
18485 SDValue NewOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT,
18486 InVec.getOperand(0), InVal, EltNo);
18487 AddToWorklist(NewOp.getNode());
18488 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(InVec.getNode()),
18489 VT, NewOp, InVec.getOperand(1), InVec.getOperand(2));
18490 }
18491 }
18492
18493 // If we can't generate a legal BUILD_VECTOR, exit
18494 if (LegalOperations && !TLI.isOperationLegal(ISD::BUILD_VECTOR, VT))
18495 return SDValue();
18496
18497 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
18498 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
18499 // vector elements.
18500 SmallVector<SDValue, 8> Ops;
18501 // Do not combine these two vectors if the output vector will not replace
18502 // the input vector.
18503 if (InVec.getOpcode() == ISD::BUILD_VECTOR && InVec.hasOneUse()) {
18504 Ops.append(InVec.getNode()->op_begin(),
18505 InVec.getNode()->op_end());
18506 } else if (InVec.isUndef()) {
18507 Ops.append(NumElts, DAG.getUNDEF(InVal.getValueType()));
18508 } else {
18509 return SDValue();
18510 }
18511 assert(Ops.size() == NumElts && "Unexpected vector size")(static_cast <bool> (Ops.size() == NumElts && "Unexpected vector size"
) ? void (0) : __assert_fail ("Ops.size() == NumElts && \"Unexpected vector size\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 18511, __extension__ __PRETTY_FUNCTION__))
;
18512
18513 // Insert the element
18514 if (Elt < Ops.size()) {
18515 // All the operands of BUILD_VECTOR must have the same type;
18516 // we enforce that here.
18517 EVT OpVT = Ops[0].getValueType();
18518 Ops[Elt] = OpVT.isInteger() ? DAG.getAnyExtOrTrunc(InVal, DL, OpVT) : InVal;
18519 }
18520
18521 // Return the new vector
18522 return DAG.getBuildVector(VT, DL, Ops);
18523}
18524
18525SDValue DAGCombiner::scalarizeExtractedVectorLoad(SDNode *EVE, EVT InVecVT,
18526 SDValue EltNo,
18527 LoadSDNode *OriginalLoad) {
18528 assert(OriginalLoad->isSimple())(static_cast <bool> (OriginalLoad->isSimple()) ? void
(0) : __assert_fail ("OriginalLoad->isSimple()", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 18528, __extension__ __PRETTY_FUNCTION__))
;
18529
18530 EVT ResultVT = EVE->getValueType(0);
18531 EVT VecEltVT = InVecVT.getVectorElementType();
18532
18533 // If the vector element type is not a multiple of a byte then we are unable
18534 // to correctly compute an address to load only the extracted element as a
18535 // scalar.
18536 if (!VecEltVT.isByteSized())
18537 return SDValue();
18538
18539 Align Alignment = OriginalLoad->getAlign();
18540 Align NewAlign = DAG.getDataLayout().getABITypeAlign(
18541 VecEltVT.getTypeForEVT(*DAG.getContext()));
18542
18543 if (NewAlign > Alignment ||
18544 !TLI.isOperationLegalOrCustom(ISD::LOAD, VecEltVT))
18545 return SDValue();
18546
18547 ISD::LoadExtType ExtTy = ResultVT.bitsGT(VecEltVT) ?
18548 ISD::NON_EXTLOAD : ISD::EXTLOAD;
18549 if (!TLI.shouldReduceLoadWidth(OriginalLoad, ExtTy, VecEltVT))
18550 return SDValue();
18551
18552 Alignment = NewAlign;
18553
18554 MachinePointerInfo MPI;
18555 SDLoc DL(EVE);
18556 if (auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo)) {
18557 int Elt = ConstEltNo->getZExtValue();
18558 unsigned PtrOff = VecEltVT.getSizeInBits() * Elt / 8;
18559 MPI = OriginalLoad->getPointerInfo().getWithOffset(PtrOff);
18560 } else {
18561 // Discard the pointer info except the address space because the memory
18562 // operand can't represent this new access since the offset is variable.
18563 MPI = MachinePointerInfo(OriginalLoad->getPointerInfo().getAddrSpace());
18564 }
18565 SDValue NewPtr = TLI.getVectorElementPointer(DAG, OriginalLoad->getBasePtr(),
18566 InVecVT, EltNo);
18567
18568 // The replacement we need to do here is a little tricky: we need to
18569 // replace an extractelement of a load with a load.
18570 // Use ReplaceAllUsesOfValuesWith to do the replacement.
18571 // Note that this replacement assumes that the extractvalue is the only
18572 // use of the load; that's okay because we don't want to perform this
18573 // transformation in other cases anyway.
18574 SDValue Load;
18575 SDValue Chain;
18576 if (ResultVT.bitsGT(VecEltVT)) {
18577 // If the result type of vextract is wider than the load, then issue an
18578 // extending load instead.
18579 ISD::LoadExtType ExtType = TLI.isLoadExtLegal(ISD::ZEXTLOAD, ResultVT,
18580 VecEltVT)
18581 ? ISD::ZEXTLOAD
18582 : ISD::EXTLOAD;
18583 Load = DAG.getExtLoad(ExtType, SDLoc(EVE), ResultVT,
18584 OriginalLoad->getChain(), NewPtr, MPI, VecEltVT,
18585 Alignment, OriginalLoad->getMemOperand()->getFlags(),
18586 OriginalLoad->getAAInfo());
18587 Chain = Load.getValue(1);
18588 } else {
18589 Load = DAG.getLoad(
18590 VecEltVT, SDLoc(EVE), OriginalLoad->getChain(), NewPtr, MPI, Alignment,
18591 OriginalLoad->getMemOperand()->getFlags(), OriginalLoad->getAAInfo());
18592 Chain = Load.getValue(1);
18593 if (ResultVT.bitsLT(VecEltVT))
18594 Load = DAG.getNode(ISD::TRUNCATE, SDLoc(EVE), ResultVT, Load);
18595 else
18596 Load = DAG.getBitcast(ResultVT, Load);
18597 }
18598 WorklistRemover DeadNodes(*this);
18599 SDValue From[] = { SDValue(EVE, 0), SDValue(OriginalLoad, 1) };
18600 SDValue To[] = { Load, Chain };
18601 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
18602 // Make sure to revisit this node to clean it up; it will usually be dead.
18603 AddToWorklist(EVE);
18604 // Since we're explicitly calling ReplaceAllUses, add the new node to the
18605 // worklist explicitly as well.
18606 AddToWorklistWithUsers(Load.getNode());
18607 ++OpsNarrowed;
18608 return SDValue(EVE, 0);
18609}
18610
18611/// Transform a vector binary operation into a scalar binary operation by moving
18612/// the math/logic after an extract element of a vector.
18613static SDValue scalarizeExtractedBinop(SDNode *ExtElt, SelectionDAG &DAG,
18614 bool LegalOperations) {
18615 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
18616 SDValue Vec = ExtElt->getOperand(0);
18617 SDValue Index = ExtElt->getOperand(1);
18618 auto *IndexC = dyn_cast<ConstantSDNode>(Index);
18619 if (!IndexC || !TLI.isBinOp(Vec.getOpcode()) || !Vec.hasOneUse() ||
18620 Vec.getNode()->getNumValues() != 1)
18621 return SDValue();
18622
18623 // Targets may want to avoid this to prevent an expensive register transfer.
18624 if (!TLI.shouldScalarizeBinop(Vec))
18625 return SDValue();
18626
18627 // Extracting an element of a vector constant is constant-folded, so this
18628 // transform is just replacing a vector op with a scalar op while moving the
18629 // extract.
18630 SDValue Op0 = Vec.getOperand(0);
18631 SDValue Op1 = Vec.getOperand(1);
18632 if (isAnyConstantBuildVector(Op0, true) ||
18633 isAnyConstantBuildVector(Op1, true)) {
18634 // extractelt (binop X, C), IndexC --> binop (extractelt X, IndexC), C'
18635 // extractelt (binop C, X), IndexC --> binop C', (extractelt X, IndexC)
18636 SDLoc DL(ExtElt);
18637 EVT VT = ExtElt->getValueType(0);
18638 SDValue Ext0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, Op0, Index);
18639 SDValue Ext1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, Op1, Index);
18640 return DAG.getNode(Vec.getOpcode(), DL, VT, Ext0, Ext1);
18641 }
18642
18643 return SDValue();
18644}
18645
18646SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
18647 SDValue VecOp = N->getOperand(0);
18648 SDValue Index = N->getOperand(1);
18649 EVT ScalarVT = N->getValueType(0);
18650 EVT VecVT = VecOp.getValueType();
18651 if (VecOp.isUndef())
18652 return DAG.getUNDEF(ScalarVT);
18653
18654 // extract_vector_elt (insert_vector_elt vec, val, idx), idx) -> val
18655 //
18656 // This only really matters if the index is non-constant since other combines
18657 // on the constant elements already work.
18658 SDLoc DL(N);
18659 if (VecOp.getOpcode() == ISD::INSERT_VECTOR_ELT &&
18660 Index == VecOp.getOperand(2)) {
18661 SDValue Elt = VecOp.getOperand(1);
18662 return VecVT.isInteger() ? DAG.getAnyExtOrTrunc(Elt, DL, ScalarVT) : Elt;
18663 }
18664
18665 // (vextract (scalar_to_vector val, 0) -> val
18666 if (VecOp.getOpcode() == ISD::SCALAR_TO_VECTOR) {
18667 // Only 0'th element of SCALAR_TO_VECTOR is defined.
18668 if (DAG.isKnownNeverZero(Index))
18669 return DAG.getUNDEF(ScalarVT);
18670
18671 // Check if the result type doesn't match the inserted element type. A
18672 // SCALAR_TO_VECTOR may truncate the inserted element and the
18673 // EXTRACT_VECTOR_ELT may widen the extracted vector.
18674 SDValue InOp = VecOp.getOperand(0);
18675 if (InOp.getValueType() != ScalarVT) {
18676 assert(InOp.getValueType().isInteger() && ScalarVT.isInteger())(static_cast <bool> (InOp.getValueType().isInteger() &&
ScalarVT.isInteger()) ? void (0) : __assert_fail ("InOp.getValueType().isInteger() && ScalarVT.isInteger()"
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 18676, __extension__ __PRETTY_FUNCTION__))
;
18677 return DAG.getSExtOrTrunc(InOp, DL, ScalarVT);
18678 }
18679 return InOp;
18680 }
18681
18682 // extract_vector_elt of out-of-bounds element -> UNDEF
18683 auto *IndexC = dyn_cast<ConstantSDNode>(Index);
18684 if (IndexC && VecVT.isFixedLengthVector() &&
18685 IndexC->getAPIntValue().uge(VecVT.getVectorNumElements()))
18686 return DAG.getUNDEF(ScalarVT);
18687
18688 // extract_vector_elt (build_vector x, y), 1 -> y
18689 if (((IndexC && VecOp.getOpcode() == ISD::BUILD_VECTOR) ||
18690 VecOp.getOpcode() == ISD::SPLAT_VECTOR) &&
18691 TLI.isTypeLegal(VecVT) &&
18692 (VecOp.hasOneUse() || TLI.aggressivelyPreferBuildVectorSources(VecVT))) {
18693 assert((VecOp.getOpcode() != ISD::BUILD_VECTOR ||(static_cast <bool> ((VecOp.getOpcode() != ISD::BUILD_VECTOR
|| VecVT.isFixedLengthVector()) && "BUILD_VECTOR used for scalable vectors"
) ? void (0) : __assert_fail ("(VecOp.getOpcode() != ISD::BUILD_VECTOR || VecVT.isFixedLengthVector()) && \"BUILD_VECTOR used for scalable vectors\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 18695, __extension__ __PRETTY_FUNCTION__))
18694 VecVT.isFixedLengthVector()) &&(static_cast <bool> ((VecOp.getOpcode() != ISD::BUILD_VECTOR
|| VecVT.isFixedLengthVector()) && "BUILD_VECTOR used for scalable vectors"
) ? void (0) : __assert_fail ("(VecOp.getOpcode() != ISD::BUILD_VECTOR || VecVT.isFixedLengthVector()) && \"BUILD_VECTOR used for scalable vectors\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 18695, __extension__ __PRETTY_FUNCTION__))
18695 "BUILD_VECTOR used for scalable vectors")(static_cast <bool> ((VecOp.getOpcode() != ISD::BUILD_VECTOR
|| VecVT.isFixedLengthVector()) && "BUILD_VECTOR used for scalable vectors"
) ? void (0) : __assert_fail ("(VecOp.getOpcode() != ISD::BUILD_VECTOR || VecVT.isFixedLengthVector()) && \"BUILD_VECTOR used for scalable vectors\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 18695, __extension__ __PRETTY_FUNCTION__))
;
18696 unsigned IndexVal =
18697 VecOp.getOpcode() == ISD::BUILD_VECTOR ? IndexC->getZExtValue() : 0;
18698 SDValue Elt = VecOp.getOperand(IndexVal);
18699 EVT InEltVT = Elt.getValueType();
18700
18701 // Sometimes build_vector's scalar input types do not match result type.
18702 if (ScalarVT == InEltVT)
18703 return Elt;
18704
18705 // TODO: It may be useful to truncate if free if the build_vector implicitly
18706 // converts.
18707 }
18708
18709 if (VecVT.isScalableVector())
18710 return SDValue();
18711
18712 // All the code from this point onwards assumes fixed width vectors, but it's
18713 // possible that some of the combinations could be made to work for scalable
18714 // vectors too.
18715 unsigned NumElts = VecVT.getVectorNumElements();
18716 unsigned VecEltBitWidth = VecVT.getScalarSizeInBits();
18717
18718 // TODO: These transforms should not require the 'hasOneUse' restriction, but
18719 // there are regressions on multiple targets without it. We can end up with a
18720 // mess of scalar and vector code if we reduce only part of the DAG to scalar.
18721 if (IndexC && VecOp.getOpcode() == ISD::BITCAST && VecVT.isInteger() &&
18722 VecOp.hasOneUse()) {
18723 // The vector index of the LSBs of the source depend on the endian-ness.
18724 bool IsLE = DAG.getDataLayout().isLittleEndian();
18725 unsigned ExtractIndex = IndexC->getZExtValue();
18726 // extract_elt (v2i32 (bitcast i64:x)), BCTruncElt -> i32 (trunc i64:x)
18727 unsigned BCTruncElt = IsLE ? 0 : NumElts - 1;
18728 SDValue BCSrc = VecOp.getOperand(0);
18729 if (ExtractIndex == BCTruncElt && BCSrc.getValueType().isScalarInteger())
18730 return DAG.getNode(ISD::TRUNCATE, DL, ScalarVT, BCSrc);
18731
18732 if (LegalTypes && BCSrc.getValueType().isInteger() &&
18733 BCSrc.getOpcode() == ISD::SCALAR_TO_VECTOR) {
18734 // ext_elt (bitcast (scalar_to_vec i64 X to v2i64) to v4i32), TruncElt -->
18735 // trunc i64 X to i32
18736 SDValue X = BCSrc.getOperand(0);
18737 assert(X.getValueType().isScalarInteger() && ScalarVT.isScalarInteger() &&(static_cast <bool> (X.getValueType().isScalarInteger()
&& ScalarVT.isScalarInteger() && "Extract element and scalar to vector can't change element type "
"from FP to integer.") ? void (0) : __assert_fail ("X.getValueType().isScalarInteger() && ScalarVT.isScalarInteger() && \"Extract element and scalar to vector can't change element type \" \"from FP to integer.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 18739, __extension__ __PRETTY_FUNCTION__))
18738 "Extract element and scalar to vector can't change element type "(static_cast <bool> (X.getValueType().isScalarInteger()
&& ScalarVT.isScalarInteger() && "Extract element and scalar to vector can't change element type "
"from FP to integer.") ? void (0) : __assert_fail ("X.getValueType().isScalarInteger() && ScalarVT.isScalarInteger() && \"Extract element and scalar to vector can't change element type \" \"from FP to integer.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 18739, __extension__ __PRETTY_FUNCTION__))
18739 "from FP to integer.")(static_cast <bool> (X.getValueType().isScalarInteger()
&& ScalarVT.isScalarInteger() && "Extract element and scalar to vector can't change element type "
"from FP to integer.") ? void (0) : __assert_fail ("X.getValueType().isScalarInteger() && ScalarVT.isScalarInteger() && \"Extract element and scalar to vector can't change element type \" \"from FP to integer.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 18739, __extension__ __PRETTY_FUNCTION__))
;
18740 unsigned XBitWidth = X.getValueSizeInBits();
18741 BCTruncElt = IsLE ? 0 : XBitWidth / VecEltBitWidth - 1;
18742
18743 // An extract element return value type can be wider than its vector
18744 // operand element type. In that case, the high bits are undefined, so
18745 // it's possible that we may need to extend rather than truncate.
18746 if (ExtractIndex == BCTruncElt && XBitWidth > VecEltBitWidth) {
18747 assert(XBitWidth % VecEltBitWidth == 0 &&(static_cast <bool> (XBitWidth % VecEltBitWidth == 0 &&
"Scalar bitwidth must be a multiple of vector element bitwidth"
) ? void (0) : __assert_fail ("XBitWidth % VecEltBitWidth == 0 && \"Scalar bitwidth must be a multiple of vector element bitwidth\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 18748, __extension__ __PRETTY_FUNCTION__))
18748 "Scalar bitwidth must be a multiple of vector element bitwidth")(static_cast <bool> (XBitWidth % VecEltBitWidth == 0 &&
"Scalar bitwidth must be a multiple of vector element bitwidth"
) ? void (0) : __assert_fail ("XBitWidth % VecEltBitWidth == 0 && \"Scalar bitwidth must be a multiple of vector element bitwidth\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 18748, __extension__ __PRETTY_FUNCTION__))
;
18749 return DAG.getAnyExtOrTrunc(X, DL, ScalarVT);
18750 }
18751 }
18752 }
18753
18754 if (SDValue BO = scalarizeExtractedBinop(N, DAG, LegalOperations))
18755 return BO;
18756
18757 // Transform: (EXTRACT_VECTOR_ELT( VECTOR_SHUFFLE )) -> EXTRACT_VECTOR_ELT.
18758 // We only perform this optimization before the op legalization phase because
18759 // we may introduce new vector instructions which are not backed by TD
18760 // patterns. For example on AVX, extracting elements from a wide vector
18761 // without using extract_subvector. However, if we can find an underlying
18762 // scalar value, then we can always use that.
18763 if (IndexC && VecOp.getOpcode() == ISD::VECTOR_SHUFFLE) {
18764 auto *Shuf = cast<ShuffleVectorSDNode>(VecOp);
18765 // Find the new index to extract from.
18766 int OrigElt = Shuf->getMaskElt(IndexC->getZExtValue());
18767
18768 // Extracting an undef index is undef.
18769 if (OrigElt == -1)
18770 return DAG.getUNDEF(ScalarVT);
18771
18772 // Select the right vector half to extract from.
18773 SDValue SVInVec;
18774 if (OrigElt < (int)NumElts) {
18775 SVInVec = VecOp.getOperand(0);
18776 } else {
18777 SVInVec = VecOp.getOperand(1);
18778 OrigElt -= NumElts;
18779 }
18780
18781 if (SVInVec.getOpcode() == ISD::BUILD_VECTOR) {
18782 SDValue InOp = SVInVec.getOperand(OrigElt);
18783 if (InOp.getValueType() != ScalarVT) {
18784 assert(InOp.getValueType().isInteger() && ScalarVT.isInteger())(static_cast <bool> (InOp.getValueType().isInteger() &&
ScalarVT.isInteger()) ? void (0) : __assert_fail ("InOp.getValueType().isInteger() && ScalarVT.isInteger()"
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 18784, __extension__ __PRETTY_FUNCTION__))
;
18785 InOp = DAG.getSExtOrTrunc(InOp, DL, ScalarVT);
18786 }
18787
18788 return InOp;
18789 }
18790
18791 // FIXME: We should handle recursing on other vector shuffles and
18792 // scalar_to_vector here as well.
18793
18794 if (!LegalOperations ||
18795 // FIXME: Should really be just isOperationLegalOrCustom.
18796 TLI.isOperationLegal(ISD::EXTRACT_VECTOR_ELT, VecVT) ||
18797 TLI.isOperationExpand(ISD::VECTOR_SHUFFLE, VecVT)) {
18798 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ScalarVT, SVInVec,
18799 DAG.getVectorIdxConstant(OrigElt, DL));
18800 }
18801 }
18802
18803 // If only EXTRACT_VECTOR_ELT nodes use the source vector we can
18804 // simplify it based on the (valid) extraction indices.
18805 if (llvm::all_of(VecOp->uses(), [&](SDNode *Use) {
18806 return Use->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
18807 Use->getOperand(0) == VecOp &&
18808 isa<ConstantSDNode>(Use->getOperand(1));
18809 })) {
18810 APInt DemandedElts = APInt::getNullValue(NumElts);
18811 for (SDNode *Use : VecOp->uses()) {
18812 auto *CstElt = cast<ConstantSDNode>(Use->getOperand(1));
18813 if (CstElt->getAPIntValue().ult(NumElts))
18814 DemandedElts.setBit(CstElt->getZExtValue());
18815 }
18816 if (SimplifyDemandedVectorElts(VecOp, DemandedElts, true)) {
18817 // We simplified the vector operand of this extract element. If this
18818 // extract is not dead, visit it again so it is folded properly.
18819 if (N->getOpcode() != ISD::DELETED_NODE)
18820 AddToWorklist(N);
18821 return SDValue(N, 0);
18822 }
18823 APInt DemandedBits = APInt::getAllOnesValue(VecEltBitWidth);
18824 if (SimplifyDemandedBits(VecOp, DemandedBits, DemandedElts, true)) {
18825 // We simplified the vector operand of this extract element. If this
18826 // extract is not dead, visit it again so it is folded properly.
18827 if (N->getOpcode() != ISD::DELETED_NODE)
18828 AddToWorklist(N);
18829 return SDValue(N, 0);
18830 }
18831 }
18832
18833 // Everything under here is trying to match an extract of a loaded value.
18834 // If the result of load has to be truncated, then it's not necessarily
18835 // profitable.
18836 bool BCNumEltsChanged = false;
18837 EVT ExtVT = VecVT.getVectorElementType();
18838 EVT LVT = ExtVT;
18839 if (ScalarVT.bitsLT(LVT) && !TLI.isTruncateFree(LVT, ScalarVT))
18840 return SDValue();
18841
18842 if (VecOp.getOpcode() == ISD::BITCAST) {
18843 // Don't duplicate a load with other uses.
18844 if (!VecOp.hasOneUse())
18845 return SDValue();
18846
18847 EVT BCVT = VecOp.getOperand(0).getValueType();
18848 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
18849 return SDValue();
18850 if (NumElts != BCVT.getVectorNumElements())
18851 BCNumEltsChanged = true;
18852 VecOp = VecOp.getOperand(0);
18853 ExtVT = BCVT.getVectorElementType();
18854 }
18855
18856 // extract (vector load $addr), i --> load $addr + i * size
18857 if (!LegalOperations && !IndexC && VecOp.hasOneUse() &&
18858 ISD::isNormalLoad(VecOp.getNode()) &&
18859 !Index->hasPredecessor(VecOp.getNode())) {
18860 auto *VecLoad = dyn_cast<LoadSDNode>(VecOp);
18861 if (VecLoad && VecLoad->isSimple())
18862 return scalarizeExtractedVectorLoad(N, VecVT, Index, VecLoad);
18863 }
18864
18865 // Perform only after legalization to ensure build_vector / vector_shuffle
18866 // optimizations have already been done.
18867 if (!LegalOperations || !IndexC)
18868 return SDValue();
18869
18870 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
18871 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
18872 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
18873 int Elt = IndexC->getZExtValue();
18874 LoadSDNode *LN0 = nullptr;
18875 if (ISD::isNormalLoad(VecOp.getNode())) {
18876 LN0 = cast<LoadSDNode>(VecOp);
18877 } else if (VecOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
18878 VecOp.getOperand(0).getValueType() == ExtVT &&
18879 ISD::isNormalLoad(VecOp.getOperand(0).getNode())) {
18880 // Don't duplicate a load with other uses.
18881 if (!VecOp.hasOneUse())
18882 return SDValue();
18883
18884 LN0 = cast<LoadSDNode>(VecOp.getOperand(0));
18885 }
18886 if (auto *Shuf = dyn_cast<ShuffleVectorSDNode>(VecOp)) {
18887 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
18888 // =>
18889 // (load $addr+1*size)
18890
18891 // Don't duplicate a load with other uses.
18892 if (!VecOp.hasOneUse())
18893 return SDValue();
18894
18895 // If the bit convert changed the number of elements, it is unsafe
18896 // to examine the mask.
18897 if (BCNumEltsChanged)
18898 return SDValue();
18899
18900 // Select the input vector, guarding against out of range extract vector.
18901 int Idx = (Elt > (int)NumElts) ? -1 : Shuf->getMaskElt(Elt);
18902 VecOp = (Idx < (int)NumElts) ? VecOp.getOperand(0) : VecOp.getOperand(1);
18903
18904 if (VecOp.getOpcode() == ISD::BITCAST) {
18905 // Don't duplicate a load with other uses.
18906 if (!VecOp.hasOneUse())
18907 return SDValue();
18908
18909 VecOp = VecOp.getOperand(0);
18910 }
18911 if (ISD::isNormalLoad(VecOp.getNode())) {
18912 LN0 = cast<LoadSDNode>(VecOp);
18913 Elt = (Idx < (int)NumElts) ? Idx : Idx - (int)NumElts;
18914 Index = DAG.getConstant(Elt, DL, Index.getValueType());
18915 }
18916 } else if (VecOp.getOpcode() == ISD::CONCAT_VECTORS && !BCNumEltsChanged &&
18917 VecVT.getVectorElementType() == ScalarVT &&
18918 (!LegalTypes ||
18919 TLI.isTypeLegal(
18920 VecOp.getOperand(0).getValueType().getVectorElementType()))) {
18921 // extract_vector_elt (concat_vectors v2i16:a, v2i16:b), 0
18922 // -> extract_vector_elt a, 0
18923 // extract_vector_elt (concat_vectors v2i16:a, v2i16:b), 1
18924 // -> extract_vector_elt a, 1
18925 // extract_vector_elt (concat_vectors v2i16:a, v2i16:b), 2
18926 // -> extract_vector_elt b, 0
18927 // extract_vector_elt (concat_vectors v2i16:a, v2i16:b), 3
18928 // -> extract_vector_elt b, 1
18929 SDLoc SL(N);
18930 EVT ConcatVT = VecOp.getOperand(0).getValueType();
18931 unsigned ConcatNumElts = ConcatVT.getVectorNumElements();
18932 SDValue NewIdx = DAG.getConstant(Elt % ConcatNumElts, SL,
18933 Index.getValueType());
18934
18935 SDValue ConcatOp = VecOp.getOperand(Elt / ConcatNumElts);
18936 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL,
18937 ConcatVT.getVectorElementType(),
18938 ConcatOp, NewIdx);
18939 return DAG.getNode(ISD::BITCAST, SL, ScalarVT, Elt);
18940 }
18941
18942 // Make sure we found a non-volatile load and the extractelement is
18943 // the only use.
18944 if (!LN0 || !LN0->hasNUsesOfValue(1,0) || !LN0->isSimple())
18945 return SDValue();
18946
18947 // If Idx was -1 above, Elt is going to be -1, so just return undef.
18948 if (Elt == -1)
18949 return DAG.getUNDEF(LVT);
18950
18951 return scalarizeExtractedVectorLoad(N, VecVT, Index, LN0);
18952}
18953
18954// Simplify (build_vec (ext )) to (bitcast (build_vec ))
18955SDValue DAGCombiner::reduceBuildVecExtToExtBuildVec(SDNode *N) {
18956 // We perform this optimization post type-legalization because
18957 // the type-legalizer often scalarizes integer-promoted vectors.
18958 // Performing this optimization before may create bit-casts which
18959 // will be type-legalized to complex code sequences.
18960 // We perform this optimization only before the operation legalizer because we
18961 // may introduce illegal operations.
18962 if (Level != AfterLegalizeVectorOps && Level != AfterLegalizeTypes)
18963 return SDValue();
18964
18965 unsigned NumInScalars = N->getNumOperands();
18966 SDLoc DL(N);
18967 EVT VT = N->getValueType(0);
18968
18969 // Check to see if this is a BUILD_VECTOR of a bunch of values
18970 // which come from any_extend or zero_extend nodes. If so, we can create
18971 // a new BUILD_VECTOR using bit-casts which may enable other BUILD_VECTOR
18972 // optimizations. We do not handle sign-extend because we can't fill the sign
18973 // using shuffles.
18974 EVT SourceType = MVT::Other;
18975 bool AllAnyExt = true;
18976
18977 for (unsigned i = 0; i != NumInScalars; ++i) {
18978 SDValue In = N->getOperand(i);
18979 // Ignore undef inputs.
18980 if (In.isUndef()) continue;
18981
18982 bool AnyExt = In.getOpcode() == ISD::ANY_EXTEND;
18983 bool ZeroExt = In.getOpcode() == ISD::ZERO_EXTEND;
18984
18985 // Abort if the element is not an extension.
18986 if (!ZeroExt && !AnyExt) {
18987 SourceType = MVT::Other;
18988 break;
18989 }
18990
18991 // The input is a ZeroExt or AnyExt. Check the original type.
18992 EVT InTy = In.getOperand(0).getValueType();
18993
18994 // Check that all of the widened source types are the same.
18995 if (SourceType == MVT::Other)
18996 // First time.
18997 SourceType = InTy;
18998 else if (InTy != SourceType) {
18999 // Multiple income types. Abort.
19000 SourceType = MVT::Other;
19001 break;
19002 }
19003
19004 // Check if all of the extends are ANY_EXTENDs.
19005 AllAnyExt &= AnyExt;
19006 }
19007
19008 // In order to have valid types, all of the inputs must be extended from the
19009 // same source type and all of the inputs must be any or zero extend.
19010 // Scalar sizes must be a power of two.
19011 EVT OutScalarTy = VT.getScalarType();
19012 bool ValidTypes = SourceType != MVT::Other &&
19013 isPowerOf2_32(OutScalarTy.getSizeInBits()) &&
19014 isPowerOf2_32(SourceType.getSizeInBits());
19015
19016 // Create a new simpler BUILD_VECTOR sequence which other optimizations can
19017 // turn into a single shuffle instruction.
19018 if (!ValidTypes)
19019 return SDValue();
19020
19021 // If we already have a splat buildvector, then don't fold it if it means
19022 // introducing zeros.
19023 if (!AllAnyExt && DAG.isSplatValue(SDValue(N, 0), /*AllowUndefs*/ true))
19024 return SDValue();
19025
19026 bool isLE = DAG.getDataLayout().isLittleEndian();
19027 unsigned ElemRatio = OutScalarTy.getSizeInBits()/SourceType.getSizeInBits();
19028 assert(ElemRatio > 1 && "Invalid element size ratio")(static_cast <bool> (ElemRatio > 1 && "Invalid element size ratio"
) ? void (0) : __assert_fail ("ElemRatio > 1 && \"Invalid element size ratio\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 19028, __extension__ __PRETTY_FUNCTION__))
;
19029 SDValue Filler = AllAnyExt ? DAG.getUNDEF(SourceType):
19030 DAG.getConstant(0, DL, SourceType);
19031
19032 unsigned NewBVElems = ElemRatio * VT.getVectorNumElements();
19033 SmallVector<SDValue, 8> Ops(NewBVElems, Filler);
19034
19035 // Populate the new build_vector
19036 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
19037 SDValue Cast = N->getOperand(i);
19038 assert((Cast.getOpcode() == ISD::ANY_EXTEND ||(static_cast <bool> ((Cast.getOpcode() == ISD::ANY_EXTEND
|| Cast.getOpcode() == ISD::ZERO_EXTEND || Cast.isUndef()) &&
"Invalid cast opcode") ? void (0) : __assert_fail ("(Cast.getOpcode() == ISD::ANY_EXTEND || Cast.getOpcode() == ISD::ZERO_EXTEND || Cast.isUndef()) && \"Invalid cast opcode\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 19040, __extension__ __PRETTY_FUNCTION__))
19039 Cast.getOpcode() == ISD::ZERO_EXTEND ||(static_cast <bool> ((Cast.getOpcode() == ISD::ANY_EXTEND
|| Cast.getOpcode() == ISD::ZERO_EXTEND || Cast.isUndef()) &&
"Invalid cast opcode") ? void (0) : __assert_fail ("(Cast.getOpcode() == ISD::ANY_EXTEND || Cast.getOpcode() == ISD::ZERO_EXTEND || Cast.isUndef()) && \"Invalid cast opcode\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 19040, __extension__ __PRETTY_FUNCTION__))
19040 Cast.isUndef()) && "Invalid cast opcode")(static_cast <bool> ((Cast.getOpcode() == ISD::ANY_EXTEND
|| Cast.getOpcode() == ISD::ZERO_EXTEND || Cast.isUndef()) &&
"Invalid cast opcode") ? void (0) : __assert_fail ("(Cast.getOpcode() == ISD::ANY_EXTEND || Cast.getOpcode() == ISD::ZERO_EXTEND || Cast.isUndef()) && \"Invalid cast opcode\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 19040, __extension__ __PRETTY_FUNCTION__))
;
19041 SDValue In;
19042 if (Cast.isUndef())
19043 In = DAG.getUNDEF(SourceType);
19044 else
19045 In = Cast->getOperand(0);
19046 unsigned Index = isLE ? (i * ElemRatio) :
19047 (i * ElemRatio + (ElemRatio - 1));
19048
19049 assert(Index < Ops.size() && "Invalid index")(static_cast <bool> (Index < Ops.size() && "Invalid index"
) ? void (0) : __assert_fail ("Index < Ops.size() && \"Invalid index\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 19049, __extension__ __PRETTY_FUNCTION__))
;
19050 Ops[Index] = In;
19051 }
19052
19053 // The type of the new BUILD_VECTOR node.
19054 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SourceType, NewBVElems);
19055 assert(VecVT.getSizeInBits() == VT.getSizeInBits() &&(static_cast <bool> (VecVT.getSizeInBits() == VT.getSizeInBits
() && "Invalid vector size") ? void (0) : __assert_fail
("VecVT.getSizeInBits() == VT.getSizeInBits() && \"Invalid vector size\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 19056, __extension__ __PRETTY_FUNCTION__))
19056 "Invalid vector size")(static_cast <bool> (VecVT.getSizeInBits() == VT.getSizeInBits
() && "Invalid vector size") ? void (0) : __assert_fail
("VecVT.getSizeInBits() == VT.getSizeInBits() && \"Invalid vector size\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 19056, __extension__ __PRETTY_FUNCTION__))
;
19057 // Check if the new vector type is legal.
19058 if (!isTypeLegal(VecVT) ||
19059 (!TLI.isOperationLegal(ISD::BUILD_VECTOR, VecVT) &&
19060 TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)))
19061 return SDValue();
19062
19063 // Make the new BUILD_VECTOR.
19064 SDValue BV = DAG.getBuildVector(VecVT, DL, Ops);
19065
19066 // The new BUILD_VECTOR node has the potential to be further optimized.
19067 AddToWorklist(BV.getNode());
19068 // Bitcast to the desired type.
19069 return DAG.getBitcast(VT, BV);
19070}
19071
19072// Simplify (build_vec (trunc $1)
19073// (trunc (srl $1 half-width))
19074// (trunc (srl $1 (2 * half-width))) …)
19075// to (bitcast $1)
19076SDValue DAGCombiner::reduceBuildVecTruncToBitCast(SDNode *N) {
19077 assert(N->getOpcode() == ISD::BUILD_VECTOR && "Expected build vector")(static_cast <bool> (N->getOpcode() == ISD::BUILD_VECTOR
&& "Expected build vector") ? void (0) : __assert_fail
("N->getOpcode() == ISD::BUILD_VECTOR && \"Expected build vector\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 19077, __extension__ __PRETTY_FUNCTION__))
;
19078
19079 // Only for little endian
19080 if (!DAG.getDataLayout().isLittleEndian())
19081 return SDValue();
19082
19083 SDLoc DL(N);
19084 EVT VT = N->getValueType(0);
19085 EVT OutScalarTy = VT.getScalarType();
19086 uint64_t ScalarTypeBitsize = OutScalarTy.getSizeInBits();
19087
19088 // Only for power of two types to be sure that bitcast works well
19089 if (!isPowerOf2_64(ScalarTypeBitsize))
19090 return SDValue();
19091
19092 unsigned NumInScalars = N->getNumOperands();
19093
19094 // Look through bitcasts
19095 auto PeekThroughBitcast = [](SDValue Op) {
19096 if (Op.getOpcode() == ISD::BITCAST)
19097 return Op.getOperand(0);
19098 return Op;
19099 };
19100
19101 // The source value where all the parts are extracted.
19102 SDValue Src;
19103 for (unsigned i = 0; i != NumInScalars; ++i) {
19104 SDValue In = PeekThroughBitcast(N->getOperand(i));
19105 // Ignore undef inputs.
19106 if (In.isUndef()) continue;
19107
19108 if (In.getOpcode() != ISD::TRUNCATE)
19109 return SDValue();
19110
19111 In = PeekThroughBitcast(In.getOperand(0));
19112
19113 if (In.getOpcode() != ISD::SRL) {
19114 // For now only build_vec without shuffling, handle shifts here in the
19115 // future.
19116 if (i != 0)
19117 return SDValue();
19118
19119 Src = In;
19120 } else {
19121 // In is SRL
19122 SDValue part = PeekThroughBitcast(In.getOperand(0));
19123
19124 if (!Src) {
19125 Src = part;
19126 } else if (Src != part) {
19127 // Vector parts do not stem from the same variable
19128 return SDValue();
19129 }
19130
19131 SDValue ShiftAmtVal = In.getOperand(1);
19132 if (!isa<ConstantSDNode>(ShiftAmtVal))
19133 return SDValue();
19134
19135 uint64_t ShiftAmt = In.getNode()->getConstantOperandVal(1);
19136
19137 // The extracted value is not extracted at the right position
19138 if (ShiftAmt != i * ScalarTypeBitsize)
19139 return SDValue();
19140 }
19141 }
19142
19143 // Only cast if the size is the same
19144 if (Src.getValueType().getSizeInBits() != VT.getSizeInBits())
19145 return SDValue();
19146
19147 return DAG.getBitcast(VT, Src);
19148}
19149
19150SDValue DAGCombiner::createBuildVecShuffle(const SDLoc &DL, SDNode *N,
19151 ArrayRef<int> VectorMask,
19152 SDValue VecIn1, SDValue VecIn2,
19153 unsigned LeftIdx, bool DidSplitVec) {
19154 SDValue ZeroIdx = DAG.getVectorIdxConstant(0, DL);
19155
19156 EVT VT = N->getValueType(0);
19157 EVT InVT1 = VecIn1.getValueType();
19158 EVT InVT2 = VecIn2.getNode() ? VecIn2.getValueType() : InVT1;
19159
19160 unsigned NumElems = VT.getVectorNumElements();
19161 unsigned ShuffleNumElems = NumElems;
19162
19163 // If we artificially split a vector in two already, then the offsets in the
19164 // operands will all be based off of VecIn1, even those in VecIn2.
19165 unsigned Vec2Offset = DidSplitVec ? 0 : InVT1.getVectorNumElements();
19166
19167 uint64_t VTSize = VT.getFixedSizeInBits();
19168 uint64_t InVT1Size = InVT1.getFixedSizeInBits();
19169 uint64_t InVT2Size = InVT2.getFixedSizeInBits();
19170
19171 assert(InVT2Size <= InVT1Size &&(static_cast <bool> (InVT2Size <= InVT1Size &&
"Inputs must be sorted to be in non-increasing vector size order."
) ? void (0) : __assert_fail ("InVT2Size <= InVT1Size && \"Inputs must be sorted to be in non-increasing vector size order.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 19172, __extension__ __PRETTY_FUNCTION__))
19172 "Inputs must be sorted to be in non-increasing vector size order.")(static_cast <bool> (InVT2Size <= InVT1Size &&
"Inputs must be sorted to be in non-increasing vector size order."
) ? void (0) : __assert_fail ("InVT2Size <= InVT1Size && \"Inputs must be sorted to be in non-increasing vector size order.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 19172, __extension__ __PRETTY_FUNCTION__))
;
19173
19174 // We can't generate a shuffle node with mismatched input and output types.
19175 // Try to make the types match the type of the output.
19176 if (InVT1 != VT || InVT2 != VT) {
19177 if ((VTSize % InVT1Size == 0) && InVT1 == InVT2) {
19178 // If the output vector length is a multiple of both input lengths,
19179 // we can concatenate them and pad the rest with undefs.
19180 unsigned NumConcats = VTSize / InVT1Size;
19181 assert(NumConcats >= 2 && "Concat needs at least two inputs!")(static_cast <bool> (NumConcats >= 2 && "Concat needs at least two inputs!"
) ? void (0) : __assert_fail ("NumConcats >= 2 && \"Concat needs at least two inputs!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 19181, __extension__ __PRETTY_FUNCTION__))
;
19182 SmallVector<SDValue, 2> ConcatOps(NumConcats, DAG.getUNDEF(InVT1));
19183 ConcatOps[0] = VecIn1;
19184 ConcatOps[1] = VecIn2 ? VecIn2 : DAG.getUNDEF(InVT1);
19185 VecIn1 = DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, ConcatOps);
19186 VecIn2 = SDValue();
19187 } else if (InVT1Size == VTSize * 2) {
19188 if (!TLI.isExtractSubvectorCheap(VT, InVT1, NumElems))
19189 return SDValue();
19190
19191 if (!VecIn2.getNode()) {
19192 // If we only have one input vector, and it's twice the size of the
19193 // output, split it in two.
19194 VecIn2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, VecIn1,
19195 DAG.getVectorIdxConstant(NumElems, DL));
19196 VecIn1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, VecIn1, ZeroIdx);
19197 // Since we now have shorter input vectors, adjust the offset of the
19198 // second vector's start.
19199 Vec2Offset = NumElems;
19200 } else {
19201 assert(InVT2Size <= InVT1Size &&(static_cast <bool> (InVT2Size <= InVT1Size &&
"Second input is not going to be larger than the first one."
) ? void (0) : __assert_fail ("InVT2Size <= InVT1Size && \"Second input is not going to be larger than the first one.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 19202, __extension__ __PRETTY_FUNCTION__))
19202 "Second input is not going to be larger than the first one.")(static_cast <bool> (InVT2Size <= InVT1Size &&
"Second input is not going to be larger than the first one."
) ? void (0) : __assert_fail ("InVT2Size <= InVT1Size && \"Second input is not going to be larger than the first one.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 19202, __extension__ __PRETTY_FUNCTION__))
;
19203
19204 // VecIn1 is wider than the output, and we have another, possibly
19205 // smaller input. Pad the smaller input with undefs, shuffle at the
19206 // input vector width, and extract the output.
19207 // The shuffle type is different than VT, so check legality again.
19208 if (LegalOperations &&
19209 !TLI.isOperationLegal(ISD::VECTOR_SHUFFLE, InVT1))
19210 return SDValue();
19211
19212 // Legalizing INSERT_SUBVECTOR is tricky - you basically have to
19213 // lower it back into a BUILD_VECTOR. So if the inserted type is
19214 // illegal, don't even try.
19215 if (InVT1 != InVT2) {
19216 if (!TLI.isTypeLegal(InVT2))
19217 return SDValue();
19218 VecIn2 = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, InVT1,
19219 DAG.getUNDEF(InVT1), VecIn2, ZeroIdx);
19220 }
19221 ShuffleNumElems = NumElems * 2;
19222 }
19223 } else if (InVT2Size * 2 == VTSize && InVT1Size == VTSize) {
19224 SmallVector<SDValue, 2> ConcatOps(2, DAG.getUNDEF(InVT2));
19225 ConcatOps[0] = VecIn2;
19226 VecIn2 = DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, ConcatOps);
19227 } else {
19228 // TODO: Support cases where the length mismatch isn't exactly by a
19229 // factor of 2.
19230 // TODO: Move this check upwards, so that if we have bad type
19231 // mismatches, we don't create any DAG nodes.
19232 return SDValue();
19233 }
19234 }
19235
19236 // Initialize mask to undef.
19237 SmallVector<int, 8> Mask(ShuffleNumElems, -1);
19238
19239 // Only need to run up to the number of elements actually used, not the
19240 // total number of elements in the shuffle - if we are shuffling a wider
19241 // vector, the high lanes should be set to undef.
19242 for (unsigned i = 0; i != NumElems; ++i) {
19243 if (VectorMask[i] <= 0)
19244 continue;
19245
19246 unsigned ExtIndex = N->getOperand(i).getConstantOperandVal(1);
19247 if (VectorMask[i] == (int)LeftIdx) {
19248 Mask[i] = ExtIndex;
19249 } else if (VectorMask[i] == (int)LeftIdx + 1) {
19250 Mask[i] = Vec2Offset + ExtIndex;
19251 }
19252 }
19253
19254 // The type the input vectors may have changed above.
19255 InVT1 = VecIn1.getValueType();
19256
19257 // If we already have a VecIn2, it should have the same type as VecIn1.
19258 // If we don't, get an undef/zero vector of the appropriate type.
19259 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(InVT1);
19260 assert(InVT1 == VecIn2.getValueType() && "Unexpected second input type.")(static_cast <bool> (InVT1 == VecIn2.getValueType() &&
"Unexpected second input type.") ? void (0) : __assert_fail (
"InVT1 == VecIn2.getValueType() && \"Unexpected second input type.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 19260, __extension__ __PRETTY_FUNCTION__))
;
19261
19262 SDValue Shuffle = DAG.getVectorShuffle(InVT1, DL, VecIn1, VecIn2, Mask);
19263 if (ShuffleNumElems > NumElems)
19264 Shuffle = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Shuffle, ZeroIdx);
19265
19266 return Shuffle;
19267}
19268
19269static SDValue reduceBuildVecToShuffleWithZero(SDNode *BV, SelectionDAG &DAG) {
19270 assert(BV->getOpcode() == ISD::BUILD_VECTOR && "Expected build vector")(static_cast <bool> (BV->getOpcode() == ISD::BUILD_VECTOR
&& "Expected build vector") ? void (0) : __assert_fail
("BV->getOpcode() == ISD::BUILD_VECTOR && \"Expected build vector\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 19270, __extension__ __PRETTY_FUNCTION__))
;
19271
19272 // First, determine where the build vector is not undef.
19273 // TODO: We could extend this to handle zero elements as well as undefs.
19274 int NumBVOps = BV->getNumOperands();
19275 int ZextElt = -1;
19276 for (int i = 0; i != NumBVOps; ++i) {
19277 SDValue Op = BV->getOperand(i);
19278 if (Op.isUndef())
19279 continue;
19280 if (ZextElt == -1)
19281 ZextElt = i;
19282 else
19283 return SDValue();
19284 }
19285 // Bail out if there's no non-undef element.
19286 if (ZextElt == -1)
19287 return SDValue();
19288
19289 // The build vector contains some number of undef elements and exactly
19290 // one other element. That other element must be a zero-extended scalar
19291 // extracted from a vector at a constant index to turn this into a shuffle.
19292 // Also, require that the build vector does not implicitly truncate/extend
19293 // its elements.
19294 // TODO: This could be enhanced to allow ANY_EXTEND as well as ZERO_EXTEND.
19295 EVT VT = BV->getValueType(0);
19296 SDValue Zext = BV->getOperand(ZextElt);
19297 if (Zext.getOpcode() != ISD::ZERO_EXTEND || !Zext.hasOneUse() ||
19298 Zext.getOperand(0).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
19299 !isa<ConstantSDNode>(Zext.getOperand(0).getOperand(1)) ||
19300 Zext.getValueSizeInBits() != VT.getScalarSizeInBits())
19301 return SDValue();
19302
19303 // The zero-extend must be a multiple of the source size, and we must be
19304 // building a vector of the same size as the source of the extract element.
19305 SDValue Extract = Zext.getOperand(0);
19306 unsigned DestSize = Zext.getValueSizeInBits();
19307 unsigned SrcSize = Extract.getValueSizeInBits();
19308 if (DestSize % SrcSize != 0 ||
19309 Extract.getOperand(0).getValueSizeInBits() != VT.getSizeInBits())
19310 return SDValue();
19311
19312 // Create a shuffle mask that will combine the extracted element with zeros
19313 // and undefs.
19314 int ZextRatio = DestSize / SrcSize;
19315 int NumMaskElts = NumBVOps * ZextRatio;
19316 SmallVector<int, 32> ShufMask(NumMaskElts, -1);
19317 for (int i = 0; i != NumMaskElts; ++i) {
19318 if (i / ZextRatio == ZextElt) {
19319 // The low bits of the (potentially translated) extracted element map to
19320 // the source vector. The high bits map to zero. We will use a zero vector
19321 // as the 2nd source operand of the shuffle, so use the 1st element of
19322 // that vector (mask value is number-of-elements) for the high bits.
19323 if (i % ZextRatio == 0)
19324 ShufMask[i] = Extract.getConstantOperandVal(1);
19325 else
19326 ShufMask[i] = NumMaskElts;
19327 }
19328
19329 // Undef elements of the build vector remain undef because we initialize
19330 // the shuffle mask with -1.
19331 }
19332
19333 // buildvec undef, ..., (zext (extractelt V, IndexC)), undef... -->
19334 // bitcast (shuffle V, ZeroVec, VectorMask)
19335 SDLoc DL(BV);
19336 EVT VecVT = Extract.getOperand(0).getValueType();
19337 SDValue ZeroVec = DAG.getConstant(0, DL, VecVT);
19338 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
19339 SDValue Shuf = TLI.buildLegalVectorShuffle(VecVT, DL, Extract.getOperand(0),
19340 ZeroVec, ShufMask, DAG);
19341 if (!Shuf)
19342 return SDValue();
19343 return DAG.getBitcast(VT, Shuf);
19344}
19345
19346// FIXME: promote to STLExtras.
19347template <typename R, typename T>
19348static auto getFirstIndexOf(R &&Range, const T &Val) {
19349 auto I = find(Range, Val);
19350 if (I == Range.end())
19351 return static_cast<decltype(std::distance(Range.begin(), I))>(-1);
19352 return std::distance(Range.begin(), I);
19353}
19354
19355// Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
19356// operations. If the types of the vectors we're extracting from allow it,
19357// turn this into a vector_shuffle node.
19358SDValue DAGCombiner::reduceBuildVecToShuffle(SDNode *N) {
19359 SDLoc DL(N);
19360 EVT VT = N->getValueType(0);
19361
19362 // Only type-legal BUILD_VECTOR nodes are converted to shuffle nodes.
19363 if (!isTypeLegal(VT))
19364 return SDValue();
19365
19366 if (SDValue V = reduceBuildVecToShuffleWithZero(N, DAG))
19367 return V;
19368
19369 // May only combine to shuffle after legalize if shuffle is legal.
19370 if (LegalOperations && !TLI.isOperationLegal(ISD::VECTOR_SHUFFLE, VT))
19371 return SDValue();
19372
19373 bool UsesZeroVector = false;
19374 unsigned NumElems = N->getNumOperands();
19375
19376 // Record, for each element of the newly built vector, which input vector
19377 // that element comes from. -1 stands for undef, 0 for the zero vector,
19378 // and positive values for the input vectors.
19379 // VectorMask maps each element to its vector number, and VecIn maps vector
19380 // numbers to their initial SDValues.
19381
19382 SmallVector<int, 8> VectorMask(NumElems, -1);
19383 SmallVector<SDValue, 8> VecIn;
19384 VecIn.push_back(SDValue());
19385
19386 for (unsigned i = 0; i != NumElems; ++i) {
19387 SDValue Op = N->getOperand(i);
19388
19389 if (Op.isUndef())
19390 continue;
19391
19392 // See if we can use a blend with a zero vector.
19393 // TODO: Should we generalize this to a blend with an arbitrary constant
19394 // vector?
19395 if (isNullConstant(Op) || isNullFPConstant(Op)) {
19396 UsesZeroVector = true;
19397 VectorMask[i] = 0;
19398 continue;
19399 }
19400
19401 // Not an undef or zero. If the input is something other than an
19402 // EXTRACT_VECTOR_ELT with an in-range constant index, bail out.
19403 if (Op.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
19404 !isa<ConstantSDNode>(Op.getOperand(1)))
19405 return SDValue();
19406 SDValue ExtractedFromVec = Op.getOperand(0);
19407
19408 if (ExtractedFromVec.getValueType().isScalableVector())
19409 return SDValue();
19410
19411 const APInt &ExtractIdx = Op.getConstantOperandAPInt(1);
19412 if (ExtractIdx.uge(ExtractedFromVec.getValueType().getVectorNumElements()))
19413 return SDValue();
19414
19415 // All inputs must have the same element type as the output.
19416 if (VT.getVectorElementType() !=
19417 ExtractedFromVec.getValueType().getVectorElementType())
19418 return SDValue();
19419
19420 // Have we seen this input vector before?
19421 // The vectors are expected to be tiny (usually 1 or 2 elements), so using
19422 // a map back from SDValues to numbers isn't worth it.
19423 int Idx = getFirstIndexOf(VecIn, ExtractedFromVec);
19424 if (Idx == -1) { // A new source vector?
19425 Idx = VecIn.size();
19426 VecIn.push_back(ExtractedFromVec);
19427 }
19428
19429 VectorMask[i] = Idx;
19430 }
19431
19432 // If we didn't find at least one input vector, bail out.
19433 if (VecIn.size() < 2)
19434 return SDValue();
19435
19436 // If all the Operands of BUILD_VECTOR extract from same
19437 // vector, then split the vector efficiently based on the maximum
19438 // vector access index and adjust the VectorMask and
19439 // VecIn accordingly.
19440 bool DidSplitVec = false;
19441 if (VecIn.size() == 2) {
19442 unsigned MaxIndex = 0;
19443 unsigned NearestPow2 = 0;
19444 SDValue Vec = VecIn.back();
19445 EVT InVT = Vec.getValueType();
19446 SmallVector<unsigned, 8> IndexVec(NumElems, 0);
19447
19448 for (unsigned i = 0; i < NumElems; i++) {
19449 if (VectorMask[i] <= 0)
19450 continue;
19451 unsigned Index = N->getOperand(i).getConstantOperandVal(1);
19452 IndexVec[i] = Index;
19453 MaxIndex = std::max(MaxIndex, Index);
19454 }
19455
19456 NearestPow2 = PowerOf2Ceil(MaxIndex);
19457 if (InVT.isSimple() && NearestPow2 > 2 && MaxIndex < NearestPow2 &&
19458 NumElems * 2 < NearestPow2) {
19459 unsigned SplitSize = NearestPow2 / 2;
19460 EVT SplitVT = EVT::getVectorVT(*DAG.getContext(),
19461 InVT.getVectorElementType(), SplitSize);
19462 if (TLI.isTypeLegal(SplitVT) &&
19463 SplitSize + SplitVT.getVectorNumElements() <=
19464 InVT.getVectorNumElements()) {
19465 SDValue VecIn2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SplitVT, Vec,
19466 DAG.getVectorIdxConstant(SplitSize, DL));
19467 SDValue VecIn1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SplitVT, Vec,
19468 DAG.getVectorIdxConstant(0, DL));
19469 VecIn.pop_back();
19470 VecIn.push_back(VecIn1);
19471 VecIn.push_back(VecIn2);
19472 DidSplitVec = true;
19473
19474 for (unsigned i = 0; i < NumElems; i++) {
19475 if (VectorMask[i] <= 0)
19476 continue;
19477 VectorMask[i] = (IndexVec[i] < SplitSize) ? 1 : 2;
19478 }
19479 }
19480 }
19481 }
19482
19483 // Sort input vectors by decreasing vector element count,
19484 // while preserving the relative order of equally-sized vectors.
19485 // Note that we keep the first "implicit zero vector as-is.
19486 SmallVector<SDValue, 8> SortedVecIn(VecIn);
19487 llvm::stable_sort(MutableArrayRef<SDValue>(SortedVecIn).drop_front(),
19488 [](const SDValue &a, const SDValue &b) {
19489 return a.getValueType().getVectorNumElements() >
19490 b.getValueType().getVectorNumElements();
19491 });
19492
19493 // We now also need to rebuild the VectorMask, because it referenced element
19494 // order in VecIn, and we just sorted them.
19495 for (int &SourceVectorIndex : VectorMask) {
19496 if (SourceVectorIndex <= 0)
19497 continue;
19498 unsigned Idx = getFirstIndexOf(SortedVecIn, VecIn[SourceVectorIndex]);
19499 assert(Idx > 0 && Idx < SortedVecIn.size() &&(static_cast <bool> (Idx > 0 && Idx < SortedVecIn
.size() && VecIn[SourceVectorIndex] == SortedVecIn[Idx
] && "Remapping failure") ? void (0) : __assert_fail (
"Idx > 0 && Idx < SortedVecIn.size() && VecIn[SourceVectorIndex] == SortedVecIn[Idx] && \"Remapping failure\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 19500, __extension__ __PRETTY_FUNCTION__))
19500 VecIn[SourceVectorIndex] == SortedVecIn[Idx] && "Remapping failure")(static_cast <bool> (Idx > 0 && Idx < SortedVecIn
.size() && VecIn[SourceVectorIndex] == SortedVecIn[Idx
] && "Remapping failure") ? void (0) : __assert_fail (
"Idx > 0 && Idx < SortedVecIn.size() && VecIn[SourceVectorIndex] == SortedVecIn[Idx] && \"Remapping failure\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 19500, __extension__ __PRETTY_FUNCTION__))
;
19501 SourceVectorIndex = Idx;
19502 }
19503
19504 VecIn = std::move(SortedVecIn);
19505
19506 // TODO: Should this fire if some of the input vectors has illegal type (like
19507 // it does now), or should we let legalization run its course first?
19508
19509 // Shuffle phase:
19510 // Take pairs of vectors, and shuffle them so that the result has elements
19511 // from these vectors in the correct places.
19512 // For example, given:
19513 // t10: i32 = extract_vector_elt t1, Constant:i64<0>
19514 // t11: i32 = extract_vector_elt t2, Constant:i64<0>
19515 // t12: i32 = extract_vector_elt t3, Constant:i64<0>
19516 // t13: i32 = extract_vector_elt t1, Constant:i64<1>
19517 // t14: v4i32 = BUILD_VECTOR t10, t11, t12, t13
19518 // We will generate:
19519 // t20: v4i32 = vector_shuffle<0,4,u,1> t1, t2
19520 // t21: v4i32 = vector_shuffle<u,u,0,u> t3, undef
19521 SmallVector<SDValue, 4> Shuffles;
19522 for (unsigned In = 0, Len = (VecIn.size() / 2); In < Len; ++In) {
19523 unsigned LeftIdx = 2 * In + 1;
19524 SDValue VecLeft = VecIn[LeftIdx];
19525 SDValue VecRight =
19526 (LeftIdx + 1) < VecIn.size() ? VecIn[LeftIdx + 1] : SDValue();
19527
19528 if (SDValue Shuffle = createBuildVecShuffle(DL, N, VectorMask, VecLeft,
19529 VecRight, LeftIdx, DidSplitVec))
19530 Shuffles.push_back(Shuffle);
19531 else
19532 return SDValue();
19533 }
19534
19535 // If we need the zero vector as an "ingredient" in the blend tree, add it
19536 // to the list of shuffles.
19537 if (UsesZeroVector)
19538 Shuffles.push_back(VT.isInteger() ? DAG.getConstant(0, DL, VT)
19539 : DAG.getConstantFP(0.0, DL, VT));
19540
19541 // If we only have one shuffle, we're done.
19542 if (Shuffles.size() == 1)
19543 return Shuffles[0];
19544
19545 // Update the vector mask to point to the post-shuffle vectors.
19546 for (int &Vec : VectorMask)
19547 if (Vec == 0)
19548 Vec = Shuffles.size() - 1;
19549 else
19550 Vec = (Vec - 1) / 2;
19551
19552 // More than one shuffle. Generate a binary tree of blends, e.g. if from
19553 // the previous step we got the set of shuffles t10, t11, t12, t13, we will
19554 // generate:
19555 // t10: v8i32 = vector_shuffle<0,8,u,u,u,u,u,u> t1, t2
19556 // t11: v8i32 = vector_shuffle<u,u,0,8,u,u,u,u> t3, t4
19557 // t12: v8i32 = vector_shuffle<u,u,u,u,0,8,u,u> t5, t6
19558 // t13: v8i32 = vector_shuffle<u,u,u,u,u,u,0,8> t7, t8
19559 // t20: v8i32 = vector_shuffle<0,1,10,11,u,u,u,u> t10, t11
19560 // t21: v8i32 = vector_shuffle<u,u,u,u,4,5,14,15> t12, t13
19561 // t30: v8i32 = vector_shuffle<0,1,2,3,12,13,14,15> t20, t21
19562
19563 // Make sure the initial size of the shuffle list is even.
19564 if (Shuffles.size() % 2)
19565 Shuffles.push_back(DAG.getUNDEF(VT));
19566
19567 for (unsigned CurSize = Shuffles.size(); CurSize > 1; CurSize /= 2) {
19568 if (CurSize % 2) {
19569 Shuffles[CurSize] = DAG.getUNDEF(VT);
19570 CurSize++;
19571 }
19572 for (unsigned In = 0, Len = CurSize / 2; In < Len; ++In) {
19573 int Left = 2 * In;
19574 int Right = 2 * In + 1;
19575 SmallVector<int, 8> Mask(NumElems, -1);
19576 for (unsigned i = 0; i != NumElems; ++i) {
19577 if (VectorMask[i] == Left) {
19578 Mask[i] = i;
19579 VectorMask[i] = In;
19580 } else if (VectorMask[i] == Right) {
19581 Mask[i] = i + NumElems;
19582 VectorMask[i] = In;
19583 }
19584 }
19585
19586 Shuffles[In] =
19587 DAG.getVectorShuffle(VT, DL, Shuffles[Left], Shuffles[Right], Mask);
19588 }
19589 }
19590 return Shuffles[0];
19591}
19592
19593// Try to turn a build vector of zero extends of extract vector elts into a
19594// a vector zero extend and possibly an extract subvector.
19595// TODO: Support sign extend?
19596// TODO: Allow undef elements?
19597SDValue DAGCombiner::convertBuildVecZextToZext(SDNode *N) {
19598 if (LegalOperations)
19599 return SDValue();
19600
19601 EVT VT = N->getValueType(0);
19602
19603 bool FoundZeroExtend = false;
19604 SDValue Op0 = N->getOperand(0);
19605 auto checkElem = [&](SDValue Op) -> int64_t {
19606 unsigned Opc = Op.getOpcode();
19607 FoundZeroExtend |= (Opc == ISD::ZERO_EXTEND);
19608 if ((Opc == ISD::ZERO_EXTEND || Opc == ISD::ANY_EXTEND) &&
19609 Op.getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
19610 Op0.getOperand(0).getOperand(0) == Op.getOperand(0).getOperand(0))
19611 if (auto *C = dyn_cast<ConstantSDNode>(Op.getOperand(0).getOperand(1)))
19612 return C->getZExtValue();
19613 return -1;
19614 };
19615
19616 // Make sure the first element matches
19617 // (zext (extract_vector_elt X, C))
19618 int64_t Offset = checkElem(Op0);
19619 if (Offset < 0)
19620 return SDValue();
19621
19622 unsigned NumElems = N->getNumOperands();
19623 SDValue In = Op0.getOperand(0).getOperand(0);
19624 EVT InSVT = In.getValueType().getScalarType();
19625 EVT InVT = EVT::getVectorVT(*DAG.getContext(), InSVT, NumElems);
19626
19627 // Don't create an illegal input type after type legalization.
19628 if (LegalTypes && !TLI.isTypeLegal(InVT))
19629 return SDValue();
19630
19631 // Ensure all the elements come from the same vector and are adjacent.
19632 for (unsigned i = 1; i != NumElems; ++i) {
19633 if ((Offset + i) != checkElem(N->getOperand(i)))
19634 return SDValue();
19635 }
19636
19637 SDLoc DL(N);
19638 In = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, InVT, In,
19639 Op0.getOperand(0).getOperand(1));
19640 return DAG.getNode(FoundZeroExtend ? ISD::ZERO_EXTEND : ISD::ANY_EXTEND, DL,
19641 VT, In);
19642}
19643
19644SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
19645 EVT VT = N->getValueType(0);
19646
19647 // A vector built entirely of undefs is undef.
19648 if (ISD::allOperandsUndef(N))
19649 return DAG.getUNDEF(VT);
19650
19651 // If this is a splat of a bitcast from another vector, change to a
19652 // concat_vector.
19653 // For example:
19654 // (build_vector (i64 (bitcast (v2i32 X))), (i64 (bitcast (v2i32 X)))) ->
19655 // (v2i64 (bitcast (concat_vectors (v2i32 X), (v2i32 X))))
19656 //
19657 // If X is a build_vector itself, the concat can become a larger build_vector.
19658 // TODO: Maybe this is useful for non-splat too?
19659 if (!LegalOperations) {
19660 if (SDValue Splat = cast<BuildVectorSDNode>(N)->getSplatValue()) {
19661 Splat = peekThroughBitcasts(Splat);
19662 EVT SrcVT = Splat.getValueType();
19663 if (SrcVT.isVector()) {
19664 unsigned NumElts = N->getNumOperands() * SrcVT.getVectorNumElements();
19665 EVT NewVT = EVT::getVectorVT(*DAG.getContext(),
19666 SrcVT.getVectorElementType(), NumElts);
19667 if (!LegalTypes || TLI.isTypeLegal(NewVT)) {
19668 SmallVector<SDValue, 8> Ops(N->getNumOperands(), Splat);
19669 SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N),
19670 NewVT, Ops);
19671 return DAG.getBitcast(VT, Concat);
19672 }
19673 }
19674 }
19675 }
19676
19677 // Check if we can express BUILD VECTOR via subvector extract.
19678 if (!LegalTypes && (N->getNumOperands() > 1)) {
19679 SDValue Op0 = N->getOperand(0);
19680 auto checkElem = [&](SDValue Op) -> uint64_t {
19681 if ((Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT) &&
19682 (Op0.getOperand(0) == Op.getOperand(0)))
19683 if (auto CNode = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
19684 return CNode->getZExtValue();
19685 return -1;
19686 };
19687
19688 int Offset = checkElem(Op0);
19689 for (unsigned i = 0; i < N->getNumOperands(); ++i) {
19690 if (Offset + i != checkElem(N->getOperand(i))) {
19691 Offset = -1;
19692 break;
19693 }
19694 }
19695
19696 if ((Offset == 0) &&
19697 (Op0.getOperand(0).getValueType() == N->getValueType(0)))
19698 return Op0.getOperand(0);
19699 if ((Offset != -1) &&
19700 ((Offset % N->getValueType(0).getVectorNumElements()) ==
19701 0)) // IDX must be multiple of output size.
19702 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(N), N->getValueType(0),
19703 Op0.getOperand(0), Op0.getOperand(1));
19704 }
19705
19706 if (SDValue V = convertBuildVecZextToZext(N))
19707 return V;
19708
19709 if (SDValue V = reduceBuildVecExtToExtBuildVec(N))
19710 return V;
19711
19712 if (SDValue V = reduceBuildVecTruncToBitCast(N))
19713 return V;
19714
19715 if (SDValue V = reduceBuildVecToShuffle(N))
19716 return V;
19717
19718 // A splat of a single element is a SPLAT_VECTOR if supported on the target.
19719 // Do this late as some of the above may replace the splat.
19720 if (TLI.getOperationAction(ISD::SPLAT_VECTOR, VT) != TargetLowering::Expand)
19721 if (SDValue V = cast<BuildVectorSDNode>(N)->getSplatValue()) {
19722 assert(!V.isUndef() && "Splat of undef should have been handled earlier")(static_cast <bool> (!V.isUndef() && "Splat of undef should have been handled earlier"
) ? void (0) : __assert_fail ("!V.isUndef() && \"Splat of undef should have been handled earlier\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 19722, __extension__ __PRETTY_FUNCTION__))
;
19723 return DAG.getNode(ISD::SPLAT_VECTOR, SDLoc(N), VT, V);
19724 }
19725
19726 return SDValue();
19727}
19728
19729static SDValue combineConcatVectorOfScalars(SDNode *N, SelectionDAG &DAG) {
19730 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
19731 EVT OpVT = N->getOperand(0).getValueType();
19732
19733 // If the operands are legal vectors, leave them alone.
19734 if (TLI.isTypeLegal(OpVT))
19735 return SDValue();
19736
19737 SDLoc DL(N);
19738 EVT VT = N->getValueType(0);
19739 SmallVector<SDValue, 8> Ops;
19740
19741 EVT SVT = EVT::getIntegerVT(*DAG.getContext(), OpVT.getSizeInBits());
19742 SDValue ScalarUndef = DAG.getNode(ISD::UNDEF, DL, SVT);
19743
19744 // Keep track of what we encounter.
19745 bool AnyInteger = false;
19746 bool AnyFP = false;
19747 for (const SDValue &Op : N->ops()) {
19748 if (ISD::BITCAST == Op.getOpcode() &&
19749 !Op.getOperand(0).getValueType().isVector())
19750 Ops.push_back(Op.getOperand(0));
19751 else if (ISD::UNDEF == Op.getOpcode())
19752 Ops.push_back(ScalarUndef);
19753 else
19754 return SDValue();
19755
19756 // Note whether we encounter an integer or floating point scalar.
19757 // If it's neither, bail out, it could be something weird like x86mmx.
19758 EVT LastOpVT = Ops.back().getValueType();
19759 if (LastOpVT.isFloatingPoint())
19760 AnyFP = true;
19761 else if (LastOpVT.isInteger())
19762 AnyInteger = true;
19763 else
19764 return SDValue();
19765 }
19766
19767 // If any of the operands is a floating point scalar bitcast to a vector,
19768 // use floating point types throughout, and bitcast everything.
19769 // Replace UNDEFs by another scalar UNDEF node, of the final desired type.
19770 if (AnyFP) {
19771 SVT = EVT::getFloatingPointVT(OpVT.getSizeInBits());
19772 ScalarUndef = DAG.getNode(ISD::UNDEF, DL, SVT);
19773 if (AnyInteger) {
19774 for (SDValue &Op : Ops) {
19775 if (Op.getValueType() == SVT)
19776 continue;
19777 if (Op.isUndef())
19778 Op = ScalarUndef;
19779 else
19780 Op = DAG.getBitcast(SVT, Op);
19781 }
19782 }
19783 }
19784
19785 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SVT,
19786 VT.getSizeInBits() / SVT.getSizeInBits());
19787 return DAG.getBitcast(VT, DAG.getBuildVector(VecVT, DL, Ops));
19788}
19789
19790// Check to see if this is a CONCAT_VECTORS of a bunch of EXTRACT_SUBVECTOR
19791// operations. If so, and if the EXTRACT_SUBVECTOR vector inputs come from at
19792// most two distinct vectors the same size as the result, attempt to turn this
19793// into a legal shuffle.
19794static SDValue combineConcatVectorOfExtracts(SDNode *N, SelectionDAG &DAG) {
19795 EVT VT = N->getValueType(0);
19796 EVT OpVT = N->getOperand(0).getValueType();
19797
19798 // We currently can't generate an appropriate shuffle for a scalable vector.
19799 if (VT.isScalableVector())
19800 return SDValue();
19801
19802 int NumElts = VT.getVectorNumElements();
19803 int NumOpElts = OpVT.getVectorNumElements();
19804
19805 SDValue SV0 = DAG.getUNDEF(VT), SV1 = DAG.getUNDEF(VT);
19806 SmallVector<int, 8> Mask;
19807
19808 for (SDValue Op : N->ops()) {
19809 Op = peekThroughBitcasts(Op);
19810
19811 // UNDEF nodes convert to UNDEF shuffle mask values.
19812 if (Op.isUndef()) {
19813 Mask.append((unsigned)NumOpElts, -1);
19814 continue;
19815 }
19816
19817 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR)
19818 return SDValue();
19819
19820 // What vector are we extracting the subvector from and at what index?
19821 SDValue ExtVec = Op.getOperand(0);
19822 int ExtIdx = Op.getConstantOperandVal(1);
19823
19824 // We want the EVT of the original extraction to correctly scale the
19825 // extraction index.
19826 EVT ExtVT = ExtVec.getValueType();
19827 ExtVec = peekThroughBitcasts(ExtVec);
19828
19829 // UNDEF nodes convert to UNDEF shuffle mask values.
19830 if (ExtVec.isUndef()) {
19831 Mask.append((unsigned)NumOpElts, -1);
19832 continue;
19833 }
19834
19835 // Ensure that we are extracting a subvector from a vector the same
19836 // size as the result.
19837 if (ExtVT.getSizeInBits() != VT.getSizeInBits())
19838 return SDValue();
19839
19840 // Scale the subvector index to account for any bitcast.
19841 int NumExtElts = ExtVT.getVectorNumElements();
19842 if (0 == (NumExtElts % NumElts))
19843 ExtIdx /= (NumExtElts / NumElts);
19844 else if (0 == (NumElts % NumExtElts))
19845 ExtIdx *= (NumElts / NumExtElts);
19846 else
19847 return SDValue();
19848
19849 // At most we can reference 2 inputs in the final shuffle.
19850 if (SV0.isUndef() || SV0 == ExtVec) {
19851 SV0 = ExtVec;
19852 for (int i = 0; i != NumOpElts; ++i)
19853 Mask.push_back(i + ExtIdx);
19854 } else if (SV1.isUndef() || SV1 == ExtVec) {
19855 SV1 = ExtVec;
19856 for (int i = 0; i != NumOpElts; ++i)
19857 Mask.push_back(i + ExtIdx + NumElts);
19858 } else {
19859 return SDValue();
19860 }
19861 }
19862
19863 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
19864 return TLI.buildLegalVectorShuffle(VT, SDLoc(N), DAG.getBitcast(VT, SV0),
19865 DAG.getBitcast(VT, SV1), Mask, DAG);
19866}
19867
19868static SDValue combineConcatVectorOfCasts(SDNode *N, SelectionDAG &DAG) {
19869 unsigned CastOpcode = N->getOperand(0).getOpcode();
19870 switch (CastOpcode) {
19871 case ISD::SINT_TO_FP:
19872 case ISD::UINT_TO_FP:
19873 case ISD::FP_TO_SINT:
19874 case ISD::FP_TO_UINT:
19875 // TODO: Allow more opcodes?
19876 // case ISD::BITCAST:
19877 // case ISD::TRUNCATE:
19878 // case ISD::ZERO_EXTEND:
19879 // case ISD::SIGN_EXTEND:
19880 // case ISD::FP_EXTEND:
19881 break;
19882 default:
19883 return SDValue();
19884 }
19885
19886 EVT SrcVT = N->getOperand(0).getOperand(0).getValueType();
19887 if (!SrcVT.isVector())
19888 return SDValue();
19889
19890 // All operands of the concat must be the same kind of cast from the same
19891 // source type.
19892 SmallVector<SDValue, 4> SrcOps;
19893 for (SDValue Op : N->ops()) {
19894 if (Op.getOpcode() != CastOpcode || !Op.hasOneUse() ||
19895 Op.getOperand(0).getValueType() != SrcVT)
19896 return SDValue();
19897 SrcOps.push_back(Op.getOperand(0));
19898 }
19899
19900 // The wider cast must be supported by the target. This is unusual because
19901 // the operation support type parameter depends on the opcode. In addition,
19902 // check the other type in the cast to make sure this is really legal.
19903 EVT VT = N->getValueType(0);
19904 EVT SrcEltVT = SrcVT.getVectorElementType();
19905 ElementCount NumElts = SrcVT.getVectorElementCount() * N->getNumOperands();
19906 EVT ConcatSrcVT = EVT::getVectorVT(*DAG.getContext(), SrcEltVT, NumElts);
19907 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
19908 switch (CastOpcode) {
19909 case ISD::SINT_TO_FP:
19910 case ISD::UINT_TO_FP:
19911 if (!TLI.isOperationLegalOrCustom(CastOpcode, ConcatSrcVT) ||
19912 !TLI.isTypeLegal(VT))
19913 return SDValue();
19914 break;
19915 case ISD::FP_TO_SINT:
19916 case ISD::FP_TO_UINT:
19917 if (!TLI.isOperationLegalOrCustom(CastOpcode, VT) ||
19918 !TLI.isTypeLegal(ConcatSrcVT))
19919 return SDValue();
19920 break;
19921 default:
19922 llvm_unreachable("Unexpected cast opcode")::llvm::llvm_unreachable_internal("Unexpected cast opcode", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 19922)
;
19923 }
19924
19925 // concat (cast X), (cast Y)... -> cast (concat X, Y...)
19926 SDLoc DL(N);
19927 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, DL, ConcatSrcVT, SrcOps);
19928 return DAG.getNode(CastOpcode, DL, VT, NewConcat);
19929}
19930
19931SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
19932 // If we only have one input vector, we don't need to do any concatenation.
19933 if (N->getNumOperands() == 1)
19934 return N->getOperand(0);
19935
19936 // Check if all of the operands are undefs.
19937 EVT VT = N->getValueType(0);
19938 if (ISD::allOperandsUndef(N))
19939 return DAG.getUNDEF(VT);
19940
19941 // Optimize concat_vectors where all but the first of the vectors are undef.
19942 if (all_of(drop_begin(N->ops()),
19943 [](const SDValue &Op) { return Op.isUndef(); })) {
19944 SDValue In = N->getOperand(0);
19945 assert(In.getValueType().isVector() && "Must concat vectors")(static_cast <bool> (In.getValueType().isVector() &&
"Must concat vectors") ? void (0) : __assert_fail ("In.getValueType().isVector() && \"Must concat vectors\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 19945, __extension__ __PRETTY_FUNCTION__))
;
19946
19947 // If the input is a concat_vectors, just make a larger concat by padding
19948 // with smaller undefs.
19949 if (In.getOpcode() == ISD::CONCAT_VECTORS && In.hasOneUse()) {
19950 unsigned NumOps = N->getNumOperands() * In.getNumOperands();
19951 SmallVector<SDValue, 4> Ops(In->op_begin(), In->op_end());
19952 Ops.resize(NumOps, DAG.getUNDEF(Ops[0].getValueType()));
19953 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, Ops);
19954 }
19955
19956 SDValue Scalar = peekThroughOneUseBitcasts(In);
19957
19958 // concat_vectors(scalar_to_vector(scalar), undef) ->
19959 // scalar_to_vector(scalar)
19960 if (!LegalOperations && Scalar.getOpcode() == ISD::SCALAR_TO_VECTOR &&
19961 Scalar.hasOneUse()) {
19962 EVT SVT = Scalar.getValueType().getVectorElementType();
19963 if (SVT == Scalar.getOperand(0).getValueType())
19964 Scalar = Scalar.getOperand(0);
19965 }
19966
19967 // concat_vectors(scalar, undef) -> scalar_to_vector(scalar)
19968 if (!Scalar.getValueType().isVector()) {
19969 // If the bitcast type isn't legal, it might be a trunc of a legal type;
19970 // look through the trunc so we can still do the transform:
19971 // concat_vectors(trunc(scalar), undef) -> scalar_to_vector(scalar)
19972 if (Scalar->getOpcode() == ISD::TRUNCATE &&
19973 !TLI.isTypeLegal(Scalar.getValueType()) &&
19974 TLI.isTypeLegal(Scalar->getOperand(0).getValueType()))
19975 Scalar = Scalar->getOperand(0);
19976
19977 EVT SclTy = Scalar.getValueType();
19978
19979 if (!SclTy.isFloatingPoint() && !SclTy.isInteger())
19980 return SDValue();
19981
19982 // Bail out if the vector size is not a multiple of the scalar size.
19983 if (VT.getSizeInBits() % SclTy.getSizeInBits())
19984 return SDValue();
19985
19986 unsigned VNTNumElms = VT.getSizeInBits() / SclTy.getSizeInBits();
19987 if (VNTNumElms < 2)
19988 return SDValue();
19989
19990 EVT NVT = EVT::getVectorVT(*DAG.getContext(), SclTy, VNTNumElms);
19991 if (!TLI.isTypeLegal(NVT) || !TLI.isTypeLegal(Scalar.getValueType()))
19992 return SDValue();
19993
19994 SDValue Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), NVT, Scalar);
19995 return DAG.getBitcast(VT, Res);
19996 }
19997 }
19998
19999 // Fold any combination of BUILD_VECTOR or UNDEF nodes into one BUILD_VECTOR.
20000 // We have already tested above for an UNDEF only concatenation.
20001 // fold (concat_vectors (BUILD_VECTOR A, B, ...), (BUILD_VECTOR C, D, ...))
20002 // -> (BUILD_VECTOR A, B, ..., C, D, ...)
20003 auto IsBuildVectorOrUndef = [](const SDValue &Op) {
20004 return ISD::UNDEF == Op.getOpcode() || ISD::BUILD_VECTOR == Op.getOpcode();
20005 };
20006 if (llvm::all_of(N->ops(), IsBuildVectorOrUndef)) {
20007 SmallVector<SDValue, 8> Opnds;
20008 EVT SVT = VT.getScalarType();
20009
20010 EVT MinVT = SVT;
20011 if (!SVT.isFloatingPoint()) {
20012 // If BUILD_VECTOR are from built from integer, they may have different
20013 // operand types. Get the smallest type and truncate all operands to it.
20014 bool FoundMinVT = false;
20015 for (const SDValue &Op : N->ops())
20016 if (ISD::BUILD_VECTOR == Op.getOpcode()) {
20017 EVT OpSVT = Op.getOperand(0).getValueType();
20018 MinVT = (!FoundMinVT || OpSVT.bitsLE(MinVT)) ? OpSVT : MinVT;
20019 FoundMinVT = true;
20020 }
20021 assert(FoundMinVT && "Concat vector type mismatch")(static_cast <bool> (FoundMinVT && "Concat vector type mismatch"
) ? void (0) : __assert_fail ("FoundMinVT && \"Concat vector type mismatch\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 20021, __extension__ __PRETTY_FUNCTION__))
;
20022 }
20023
20024 for (const SDValue &Op : N->ops()) {
20025 EVT OpVT = Op.getValueType();
20026 unsigned NumElts = OpVT.getVectorNumElements();
20027
20028 if (ISD::UNDEF == Op.getOpcode())
20029 Opnds.append(NumElts, DAG.getUNDEF(MinVT));
20030
20031 if (ISD::BUILD_VECTOR == Op.getOpcode()) {
20032 if (SVT.isFloatingPoint()) {
20033 assert(SVT == OpVT.getScalarType() && "Concat vector type mismatch")(static_cast <bool> (SVT == OpVT.getScalarType() &&
"Concat vector type mismatch") ? void (0) : __assert_fail ("SVT == OpVT.getScalarType() && \"Concat vector type mismatch\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 20033, __extension__ __PRETTY_FUNCTION__))
;
20034 Opnds.append(Op->op_begin(), Op->op_begin() + NumElts);
20035 } else {
20036 for (unsigned i = 0; i != NumElts; ++i)
20037 Opnds.push_back(
20038 DAG.getNode(ISD::TRUNCATE, SDLoc(N), MinVT, Op.getOperand(i)));
20039 }
20040 }
20041 }
20042
20043 assert(VT.getVectorNumElements() == Opnds.size() &&(static_cast <bool> (VT.getVectorNumElements() == Opnds
.size() && "Concat vector type mismatch") ? void (0) :
__assert_fail ("VT.getVectorNumElements() == Opnds.size() && \"Concat vector type mismatch\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 20044, __extension__ __PRETTY_FUNCTION__))
20044 "Concat vector type mismatch")(static_cast <bool> (VT.getVectorNumElements() == Opnds
.size() && "Concat vector type mismatch") ? void (0) :
__assert_fail ("VT.getVectorNumElements() == Opnds.size() && \"Concat vector type mismatch\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 20044, __extension__ __PRETTY_FUNCTION__))
;
20045 return DAG.getBuildVector(VT, SDLoc(N), Opnds);
20046 }
20047
20048 // Fold CONCAT_VECTORS of only bitcast scalars (or undef) to BUILD_VECTOR.
20049 if (SDValue V = combineConcatVectorOfScalars(N, DAG))
20050 return V;
20051
20052 // Fold CONCAT_VECTORS of EXTRACT_SUBVECTOR (or undef) to VECTOR_SHUFFLE.
20053 if (Level < AfterLegalizeVectorOps && TLI.isTypeLegal(VT))
20054 if (SDValue V = combineConcatVectorOfExtracts(N, DAG))
20055 return V;
20056
20057 if (SDValue V = combineConcatVectorOfCasts(N, DAG))
20058 return V;
20059
20060 // Type legalization of vectors and DAG canonicalization of SHUFFLE_VECTOR
20061 // nodes often generate nop CONCAT_VECTOR nodes. Scan the CONCAT_VECTOR
20062 // operands and look for a CONCAT operations that place the incoming vectors
20063 // at the exact same location.
20064 //
20065 // For scalable vectors, EXTRACT_SUBVECTOR indexes are implicitly scaled.
20066 SDValue SingleSource = SDValue();
20067 unsigned PartNumElem =
20068 N->getOperand(0).getValueType().getVectorMinNumElements();
20069
20070 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
20071 SDValue Op = N->getOperand(i);
20072
20073 if (Op.isUndef())
20074 continue;
20075
20076 // Check if this is the identity extract:
20077 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR)
20078 return SDValue();
20079
20080 // Find the single incoming vector for the extract_subvector.
20081 if (SingleSource.getNode()) {
20082 if (Op.getOperand(0) != SingleSource)
20083 return SDValue();
20084 } else {
20085 SingleSource = Op.getOperand(0);
20086
20087 // Check the source type is the same as the type of the result.
20088 // If not, this concat may extend the vector, so we can not
20089 // optimize it away.
20090 if (SingleSource.getValueType() != N->getValueType(0))
20091 return SDValue();
20092 }
20093
20094 // Check that we are reading from the identity index.
20095 unsigned IdentityIndex = i * PartNumElem;
20096 if (Op.getConstantOperandAPInt(1) != IdentityIndex)
20097 return SDValue();
20098 }
20099
20100 if (SingleSource.getNode())
20101 return SingleSource;
20102
20103 return SDValue();
20104}
20105
20106// Helper that peeks through INSERT_SUBVECTOR/CONCAT_VECTORS to find
20107// if the subvector can be sourced for free.
20108static SDValue getSubVectorSrc(SDValue V, SDValue Index, EVT SubVT) {
20109 if (V.getOpcode() == ISD::INSERT_SUBVECTOR &&
20110 V.getOperand(1).getValueType() == SubVT && V.getOperand(2) == Index) {
20111 return V.getOperand(1);
20112 }
20113 auto *IndexC = dyn_cast<ConstantSDNode>(Index);
20114 if (IndexC && V.getOpcode() == ISD::CONCAT_VECTORS &&
20115 V.getOperand(0).getValueType() == SubVT &&
20116 (IndexC->getZExtValue() % SubVT.getVectorMinNumElements()) == 0) {
20117 uint64_t SubIdx = IndexC->getZExtValue() / SubVT.getVectorMinNumElements();
20118 return V.getOperand(SubIdx);
20119 }
20120 return SDValue();
20121}
20122
20123static SDValue narrowInsertExtractVectorBinOp(SDNode *Extract,
20124 SelectionDAG &DAG,
20125 bool LegalOperations) {
20126 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
20127 SDValue BinOp = Extract->getOperand(0);
20128 unsigned BinOpcode = BinOp.getOpcode();
20129 if (!TLI.isBinOp(BinOpcode) || BinOp.getNode()->getNumValues() != 1)
20130 return SDValue();
20131
20132 EVT VecVT = BinOp.getValueType();
20133 SDValue Bop0 = BinOp.getOperand(0), Bop1 = BinOp.getOperand(1);
20134 if (VecVT != Bop0.getValueType() || VecVT != Bop1.getValueType())
20135 return SDValue();
20136
20137 SDValue Index = Extract->getOperand(1);
20138 EVT SubVT = Extract->getValueType(0);
20139 if (!TLI.isOperationLegalOrCustom(BinOpcode, SubVT, LegalOperations))
20140 return SDValue();
20141
20142 SDValue Sub0 = getSubVectorSrc(Bop0, Index, SubVT);
20143 SDValue Sub1 = getSubVectorSrc(Bop1, Index, SubVT);
20144
20145 // TODO: We could handle the case where only 1 operand is being inserted by
20146 // creating an extract of the other operand, but that requires checking
20147 // number of uses and/or costs.
20148 if (!Sub0 || !Sub1)
20149 return SDValue();
20150
20151 // We are inserting both operands of the wide binop only to extract back
20152 // to the narrow vector size. Eliminate all of the insert/extract:
20153 // ext (binop (ins ?, X, Index), (ins ?, Y, Index)), Index --> binop X, Y
20154 return DAG.getNode(BinOpcode, SDLoc(Extract), SubVT, Sub0, Sub1,
20155 BinOp->getFlags());
20156}
20157
20158/// If we are extracting a subvector produced by a wide binary operator try
20159/// to use a narrow binary operator and/or avoid concatenation and extraction.
20160static SDValue narrowExtractedVectorBinOp(SDNode *Extract, SelectionDAG &DAG,
20161 bool LegalOperations) {
20162 // TODO: Refactor with the caller (visitEXTRACT_SUBVECTOR), so we can share
20163 // some of these bailouts with other transforms.
20164
20165 if (SDValue V = narrowInsertExtractVectorBinOp(Extract, DAG, LegalOperations))
20166 return V;
20167
20168 // The extract index must be a constant, so we can map it to a concat operand.
20169 auto *ExtractIndexC = dyn_cast<ConstantSDNode>(Extract->getOperand(1));
20170 if (!ExtractIndexC)
20171 return SDValue();
20172
20173 // We are looking for an optionally bitcasted wide vector binary operator
20174 // feeding an extract subvector.
20175 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
20176 SDValue BinOp = peekThroughBitcasts(Extract->getOperand(0));
20177 unsigned BOpcode = BinOp.getOpcode();
20178 if (!TLI.isBinOp(BOpcode) || BinOp.getNode()->getNumValues() != 1)
20179 return SDValue();
20180
20181 // Exclude the fake form of fneg (fsub -0.0, x) because that is likely to be
20182 // reduced to the unary fneg when it is visited, and we probably want to deal
20183 // with fneg in a target-specific way.
20184 if (BOpcode == ISD::FSUB) {
20185 auto *C = isConstOrConstSplatFP(BinOp.getOperand(0), /*AllowUndefs*/ true);
20186 if (C && C->getValueAPF().isNegZero())
20187 return SDValue();
20188 }
20189
20190 // The binop must be a vector type, so we can extract some fraction of it.
20191 EVT WideBVT = BinOp.getValueType();
20192 // The optimisations below currently assume we are dealing with fixed length
20193 // vectors. It is possible to add support for scalable vectors, but at the
20194 // moment we've done no analysis to prove whether they are profitable or not.
20195 if (!WideBVT.isFixedLengthVector())
20196 return SDValue();
20197
20198 EVT VT = Extract->getValueType(0);
20199 unsigned ExtractIndex = ExtractIndexC->getZExtValue();
20200 assert(ExtractIndex % VT.getVectorNumElements() == 0 &&(static_cast <bool> (ExtractIndex % VT.getVectorNumElements
() == 0 && "Extract index is not a multiple of the vector length."
) ? void (0) : __assert_fail ("ExtractIndex % VT.getVectorNumElements() == 0 && \"Extract index is not a multiple of the vector length.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 20201, __extension__ __PRETTY_FUNCTION__))
20201 "Extract index is not a multiple of the vector length.")(static_cast <bool> (ExtractIndex % VT.getVectorNumElements
() == 0 && "Extract index is not a multiple of the vector length."
) ? void (0) : __assert_fail ("ExtractIndex % VT.getVectorNumElements() == 0 && \"Extract index is not a multiple of the vector length.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 20201, __extension__ __PRETTY_FUNCTION__))
;
20202
20203 // Bail out if this is not a proper multiple width extraction.
20204 unsigned WideWidth = WideBVT.getSizeInBits();
20205 unsigned NarrowWidth = VT.getSizeInBits();
20206 if (WideWidth % NarrowWidth != 0)
20207 return SDValue();
20208
20209 // Bail out if we are extracting a fraction of a single operation. This can
20210 // occur because we potentially looked through a bitcast of the binop.
20211 unsigned NarrowingRatio = WideWidth / NarrowWidth;
20212 unsigned WideNumElts = WideBVT.getVectorNumElements();
20213 if (WideNumElts % NarrowingRatio != 0)
20214 return SDValue();
20215
20216 // Bail out if the target does not support a narrower version of the binop.
20217 EVT NarrowBVT = EVT::getVectorVT(*DAG.getContext(), WideBVT.getScalarType(),
20218 WideNumElts / NarrowingRatio);
20219 if (!TLI.isOperationLegalOrCustomOrPromote(BOpcode, NarrowBVT))
20220 return SDValue();
20221
20222 // If extraction is cheap, we don't need to look at the binop operands
20223 // for concat ops. The narrow binop alone makes this transform profitable.
20224 // We can't just reuse the original extract index operand because we may have
20225 // bitcasted.
20226 unsigned ConcatOpNum = ExtractIndex / VT.getVectorNumElements();
20227 unsigned ExtBOIdx = ConcatOpNum * NarrowBVT.getVectorNumElements();
20228 if (TLI.isExtractSubvectorCheap(NarrowBVT, WideBVT, ExtBOIdx) &&
20229 BinOp.hasOneUse() && Extract->getOperand(0)->hasOneUse()) {
20230 // extract (binop B0, B1), N --> binop (extract B0, N), (extract B1, N)
20231 SDLoc DL(Extract);
20232 SDValue NewExtIndex = DAG.getVectorIdxConstant(ExtBOIdx, DL);
20233 SDValue X = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, NarrowBVT,
20234 BinOp.getOperand(0), NewExtIndex);
20235 SDValue Y = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, NarrowBVT,
20236 BinOp.getOperand(1), NewExtIndex);
20237 SDValue NarrowBinOp = DAG.getNode(BOpcode, DL, NarrowBVT, X, Y,
20238 BinOp.getNode()->getFlags());
20239 return DAG.getBitcast(VT, NarrowBinOp);
20240 }
20241
20242 // Only handle the case where we are doubling and then halving. A larger ratio
20243 // may require more than two narrow binops to replace the wide binop.
20244 if (NarrowingRatio != 2)
20245 return SDValue();
20246
20247 // TODO: The motivating case for this transform is an x86 AVX1 target. That
20248 // target has temptingly almost legal versions of bitwise logic ops in 256-bit
20249 // flavors, but no other 256-bit integer support. This could be extended to
20250 // handle any binop, but that may require fixing/adding other folds to avoid
20251 // codegen regressions.
20252 if (BOpcode != ISD::AND && BOpcode != ISD::OR && BOpcode != ISD::XOR)
20253 return SDValue();
20254
20255 // We need at least one concatenation operation of a binop operand to make
20256 // this transform worthwhile. The concat must double the input vector sizes.
20257 auto GetSubVector = [ConcatOpNum](SDValue V) -> SDValue {
20258 if (V.getOpcode() == ISD::CONCAT_VECTORS && V.getNumOperands() == 2)
20259 return V.getOperand(ConcatOpNum);
20260 return SDValue();
20261 };
20262 SDValue SubVecL = GetSubVector(peekThroughBitcasts(BinOp.getOperand(0)));
20263 SDValue SubVecR = GetSubVector(peekThroughBitcasts(BinOp.getOperand(1)));
20264
20265 if (SubVecL || SubVecR) {
20266 // If a binop operand was not the result of a concat, we must extract a
20267 // half-sized operand for our new narrow binop:
20268 // extract (binop (concat X1, X2), (concat Y1, Y2)), N --> binop XN, YN
20269 // extract (binop (concat X1, X2), Y), N --> binop XN, (extract Y, IndexC)
20270 // extract (binop X, (concat Y1, Y2)), N --> binop (extract X, IndexC), YN
20271 SDLoc DL(Extract);
20272 SDValue IndexC = DAG.getVectorIdxConstant(ExtBOIdx, DL);
20273 SDValue X = SubVecL ? DAG.getBitcast(NarrowBVT, SubVecL)
20274 : DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, NarrowBVT,
20275 BinOp.getOperand(0), IndexC);
20276
20277 SDValue Y = SubVecR ? DAG.getBitcast(NarrowBVT, SubVecR)
20278 : DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, NarrowBVT,
20279 BinOp.getOperand(1), IndexC);
20280
20281 SDValue NarrowBinOp = DAG.getNode(BOpcode, DL, NarrowBVT, X, Y);
20282 return DAG.getBitcast(VT, NarrowBinOp);
20283 }
20284
20285 return SDValue();
20286}
20287
20288/// If we are extracting a subvector from a wide vector load, convert to a
20289/// narrow load to eliminate the extraction:
20290/// (extract_subvector (load wide vector)) --> (load narrow vector)
20291static SDValue narrowExtractedVectorLoad(SDNode *Extract, SelectionDAG &DAG) {
20292 // TODO: Add support for big-endian. The offset calculation must be adjusted.
20293 if (DAG.getDataLayout().isBigEndian())
20294 return SDValue();
20295
20296 auto *Ld = dyn_cast<LoadSDNode>(Extract->getOperand(0));
20297 auto *ExtIdx = dyn_cast<ConstantSDNode>(Extract->getOperand(1));
20298 if (!Ld || Ld->getExtensionType() || !Ld->isSimple() ||
20299 !ExtIdx)
20300 return SDValue();
20301
20302 // Allow targets to opt-out.
20303 EVT VT = Extract->getValueType(0);
20304
20305 // We can only create byte sized loads.
20306 if (!VT.isByteSized())
20307 return SDValue();
20308
20309 unsigned Index = ExtIdx->getZExtValue();
20310 unsigned NumElts = VT.getVectorMinNumElements();
20311
20312 // The definition of EXTRACT_SUBVECTOR states that the index must be a
20313 // multiple of the minimum number of elements in the result type.
20314 assert(Index % NumElts == 0 && "The extract subvector index is not a "(static_cast <bool> (Index % NumElts == 0 && "The extract subvector index is not a "
"multiple of the result's element count") ? void (0) : __assert_fail
("Index % NumElts == 0 && \"The extract subvector index is not a \" \"multiple of the result's element count\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 20315, __extension__ __PRETTY_FUNCTION__))
20315 "multiple of the result's element count")(static_cast <bool> (Index % NumElts == 0 && "The extract subvector index is not a "
"multiple of the result's element count") ? void (0) : __assert_fail
("Index % NumElts == 0 && \"The extract subvector index is not a \" \"multiple of the result's element count\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 20315, __extension__ __PRETTY_FUNCTION__))
;
20316
20317 // It's fine to use TypeSize here as we know the offset will not be negative.
20318 TypeSize Offset = VT.getStoreSize() * (Index / NumElts);
20319
20320 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
20321 if (!TLI.shouldReduceLoadWidth(Ld, Ld->getExtensionType(), VT))
20322 return SDValue();
20323
20324 // The narrow load will be offset from the base address of the old load if
20325 // we are extracting from something besides index 0 (little-endian).
20326 SDLoc DL(Extract);
20327
20328 // TODO: Use "BaseIndexOffset" to make this more effective.
20329 SDValue NewAddr = DAG.getMemBasePlusOffset(Ld->getBasePtr(), Offset, DL);
20330
20331 uint64_t StoreSize = MemoryLocation::getSizeOrUnknown(VT.getStoreSize());
20332 MachineFunction &MF = DAG.getMachineFunction();
20333 MachineMemOperand *MMO;
20334 if (Offset.isScalable()) {
20335 MachinePointerInfo MPI =
20336 MachinePointerInfo(Ld->getPointerInfo().getAddrSpace());
20337 MMO = MF.getMachineMemOperand(Ld->getMemOperand(), MPI, StoreSize);
20338 } else
20339 MMO = MF.getMachineMemOperand(Ld->getMemOperand(), Offset.getFixedSize(),
20340 StoreSize);
20341
20342 SDValue NewLd = DAG.getLoad(VT, DL, Ld->getChain(), NewAddr, MMO);
20343 DAG.makeEquivalentMemoryOrdering(Ld, NewLd);
20344 return NewLd;
20345}
20346
20347SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode *N) {
20348 EVT NVT = N->getValueType(0);
20349 SDValue V = N->getOperand(0);
20350 uint64_t ExtIdx = N->getConstantOperandVal(1);
20351
20352 // Extract from UNDEF is UNDEF.
20353 if (V.isUndef())
20354 return DAG.getUNDEF(NVT);
20355
20356 if (TLI.isOperationLegalOrCustomOrPromote(ISD::LOAD, NVT))
20357 if (SDValue NarrowLoad = narrowExtractedVectorLoad(N, DAG))
20358 return NarrowLoad;
20359
20360 // Combine an extract of an extract into a single extract_subvector.
20361 // ext (ext X, C), 0 --> ext X, C
20362 if (ExtIdx == 0 && V.getOpcode() == ISD::EXTRACT_SUBVECTOR && V.hasOneUse()) {
20363 if (TLI.isExtractSubvectorCheap(NVT, V.getOperand(0).getValueType(),
20364 V.getConstantOperandVal(1)) &&
20365 TLI.isOperationLegalOrCustom(ISD::EXTRACT_SUBVECTOR, NVT)) {
20366 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(N), NVT, V.getOperand(0),
20367 V.getOperand(1));
20368 }
20369 }
20370
20371 // Try to move vector bitcast after extract_subv by scaling extraction index:
20372 // extract_subv (bitcast X), Index --> bitcast (extract_subv X, Index')
20373 if (V.getOpcode() == ISD::BITCAST &&
20374 V.getOperand(0).getValueType().isVector() &&
20375 (!LegalOperations || TLI.isOperationLegal(ISD::BITCAST, NVT))) {
20376 SDValue SrcOp = V.getOperand(0);
20377 EVT SrcVT = SrcOp.getValueType();
20378 unsigned SrcNumElts = SrcVT.getVectorMinNumElements();
20379 unsigned DestNumElts = V.getValueType().getVectorMinNumElements();
20380 if ((SrcNumElts % DestNumElts) == 0) {
20381 unsigned SrcDestRatio = SrcNumElts / DestNumElts;
20382 ElementCount NewExtEC = NVT.getVectorElementCount() * SrcDestRatio;
20383 EVT NewExtVT = EVT::getVectorVT(*DAG.getContext(), SrcVT.getScalarType(),
20384 NewExtEC);
20385 if (TLI.isOperationLegalOrCustom(ISD::EXTRACT_SUBVECTOR, NewExtVT)) {
20386 SDLoc DL(N);
20387 SDValue NewIndex = DAG.getVectorIdxConstant(ExtIdx * SrcDestRatio, DL);
20388 SDValue NewExtract = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, NewExtVT,
20389 V.getOperand(0), NewIndex);
20390 return DAG.getBitcast(NVT, NewExtract);
20391 }
20392 }
20393 if ((DestNumElts % SrcNumElts) == 0) {
20394 unsigned DestSrcRatio = DestNumElts / SrcNumElts;
20395 if (NVT.getVectorElementCount().isKnownMultipleOf(DestSrcRatio)) {
20396 ElementCount NewExtEC =
20397 NVT.getVectorElementCount().divideCoefficientBy(DestSrcRatio);
20398 EVT ScalarVT = SrcVT.getScalarType();
20399 if ((ExtIdx % DestSrcRatio) == 0) {
20400 SDLoc DL(N);
20401 unsigned IndexValScaled = ExtIdx / DestSrcRatio;
20402 EVT NewExtVT =
20403 EVT::getVectorVT(*DAG.getContext(), ScalarVT, NewExtEC);
20404 if (TLI.isOperationLegalOrCustom(ISD::EXTRACT_SUBVECTOR, NewExtVT)) {
20405 SDValue NewIndex = DAG.getVectorIdxConstant(IndexValScaled, DL);
20406 SDValue NewExtract =
20407 DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, NewExtVT,
20408 V.getOperand(0), NewIndex);
20409 return DAG.getBitcast(NVT, NewExtract);
20410 }
20411 if (NewExtEC.isScalar() &&
20412 TLI.isOperationLegalOrCustom(ISD::EXTRACT_VECTOR_ELT, ScalarVT)) {
20413 SDValue NewIndex = DAG.getVectorIdxConstant(IndexValScaled, DL);
20414 SDValue NewExtract =
20415 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ScalarVT,
20416 V.getOperand(0), NewIndex);
20417 return DAG.getBitcast(NVT, NewExtract);
20418 }
20419 }
20420 }
20421 }
20422 }
20423
20424 if (V.getOpcode() == ISD::CONCAT_VECTORS) {
20425 unsigned ExtNumElts = NVT.getVectorMinNumElements();
20426 EVT ConcatSrcVT = V.getOperand(0).getValueType();
20427 assert(ConcatSrcVT.getVectorElementType() == NVT.getVectorElementType() &&(static_cast <bool> (ConcatSrcVT.getVectorElementType()
== NVT.getVectorElementType() && "Concat and extract subvector do not change element type"
) ? void (0) : __assert_fail ("ConcatSrcVT.getVectorElementType() == NVT.getVectorElementType() && \"Concat and extract subvector do not change element type\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 20428, __extension__ __PRETTY_FUNCTION__))
20428 "Concat and extract subvector do not change element type")(static_cast <bool> (ConcatSrcVT.getVectorElementType()
== NVT.getVectorElementType() && "Concat and extract subvector do not change element type"
) ? void (0) : __assert_fail ("ConcatSrcVT.getVectorElementType() == NVT.getVectorElementType() && \"Concat and extract subvector do not change element type\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 20428, __extension__ __PRETTY_FUNCTION__))
;
20429 assert((ExtIdx % ExtNumElts) == 0 &&(static_cast <bool> ((ExtIdx % ExtNumElts) == 0 &&
"Extract index is not a multiple of the input vector length."
) ? void (0) : __assert_fail ("(ExtIdx % ExtNumElts) == 0 && \"Extract index is not a multiple of the input vector length.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 20430, __extension__ __PRETTY_FUNCTION__))
20430 "Extract index is not a multiple of the input vector length.")(static_cast <bool> ((ExtIdx % ExtNumElts) == 0 &&
"Extract index is not a multiple of the input vector length."
) ? void (0) : __assert_fail ("(ExtIdx % ExtNumElts) == 0 && \"Extract index is not a multiple of the input vector length.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 20430, __extension__ __PRETTY_FUNCTION__))
;
20431
20432 unsigned ConcatSrcNumElts = ConcatSrcVT.getVectorMinNumElements();
20433 unsigned ConcatOpIdx = ExtIdx / ConcatSrcNumElts;
20434
20435 // If the concatenated source types match this extract, it's a direct
20436 // simplification:
20437 // extract_subvec (concat V1, V2, ...), i --> Vi
20438 if (ConcatSrcNumElts == ExtNumElts)
20439 return V.getOperand(ConcatOpIdx);
20440
20441 // If the concatenated source vectors are a multiple length of this extract,
20442 // then extract a fraction of one of those source vectors directly from a
20443 // concat operand. Example:
20444 // v2i8 extract_subvec (v16i8 concat (v8i8 X), (v8i8 Y), 14 -->
20445 // v2i8 extract_subvec v8i8 Y, 6
20446 if (NVT.isFixedLengthVector() && ConcatSrcNumElts % ExtNumElts == 0) {
20447 SDLoc DL(N);
20448 unsigned NewExtIdx = ExtIdx - ConcatOpIdx * ConcatSrcNumElts;
20449 assert(NewExtIdx + ExtNumElts <= ConcatSrcNumElts &&(static_cast <bool> (NewExtIdx + ExtNumElts <= ConcatSrcNumElts
&& "Trying to extract from >1 concat operand?") ?
void (0) : __assert_fail ("NewExtIdx + ExtNumElts <= ConcatSrcNumElts && \"Trying to extract from >1 concat operand?\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 20450, __extension__ __PRETTY_FUNCTION__))
20450 "Trying to extract from >1 concat operand?")(static_cast <bool> (NewExtIdx + ExtNumElts <= ConcatSrcNumElts
&& "Trying to extract from >1 concat operand?") ?
void (0) : __assert_fail ("NewExtIdx + ExtNumElts <= ConcatSrcNumElts && \"Trying to extract from >1 concat operand?\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 20450, __extension__ __PRETTY_FUNCTION__))
;
20451 assert(NewExtIdx % ExtNumElts == 0 &&(static_cast <bool> (NewExtIdx % ExtNumElts == 0 &&
"Extract index is not a multiple of the input vector length."
) ? void (0) : __assert_fail ("NewExtIdx % ExtNumElts == 0 && \"Extract index is not a multiple of the input vector length.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 20452, __extension__ __PRETTY_FUNCTION__))
20452 "Extract index is not a multiple of the input vector length.")(static_cast <bool> (NewExtIdx % ExtNumElts == 0 &&
"Extract index is not a multiple of the input vector length."
) ? void (0) : __assert_fail ("NewExtIdx % ExtNumElts == 0 && \"Extract index is not a multiple of the input vector length.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 20452, __extension__ __PRETTY_FUNCTION__))
;
20453 SDValue NewIndexC = DAG.getVectorIdxConstant(NewExtIdx, DL);
20454 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, NVT,
20455 V.getOperand(ConcatOpIdx), NewIndexC);
20456 }
20457 }
20458
20459 V = peekThroughBitcasts(V);
20460
20461 // If the input is a build vector. Try to make a smaller build vector.
20462 if (V.getOpcode() == ISD::BUILD_VECTOR) {
20463 EVT InVT = V.getValueType();
20464 unsigned ExtractSize = NVT.getSizeInBits();
20465 unsigned EltSize = InVT.getScalarSizeInBits();
20466 // Only do this if we won't split any elements.
20467 if (ExtractSize % EltSize == 0) {
20468 unsigned NumElems = ExtractSize / EltSize;
20469 EVT EltVT = InVT.getVectorElementType();
20470 EVT ExtractVT =
20471 NumElems == 1 ? EltVT
20472 : EVT::getVectorVT(*DAG.getContext(), EltVT, NumElems);
20473 if ((Level < AfterLegalizeDAG ||
20474 (NumElems == 1 ||
20475 TLI.isOperationLegal(ISD::BUILD_VECTOR, ExtractVT))) &&
20476 (!LegalTypes || TLI.isTypeLegal(ExtractVT))) {
20477 unsigned IdxVal = (ExtIdx * NVT.getScalarSizeInBits()) / EltSize;
20478
20479 if (NumElems == 1) {
20480 SDValue Src = V->getOperand(IdxVal);
20481 if (EltVT != Src.getValueType())
20482 Src = DAG.getNode(ISD::TRUNCATE, SDLoc(N), InVT, Src);
20483 return DAG.getBitcast(NVT, Src);
20484 }
20485
20486 // Extract the pieces from the original build_vector.
20487 SDValue BuildVec = DAG.getBuildVector(ExtractVT, SDLoc(N),
20488 V->ops().slice(IdxVal, NumElems));
20489 return DAG.getBitcast(NVT, BuildVec);
20490 }
20491 }
20492 }
20493
20494 if (V.getOpcode() == ISD::INSERT_SUBVECTOR) {
20495 // Handle only simple case where vector being inserted and vector
20496 // being extracted are of same size.
20497 EVT SmallVT = V.getOperand(1).getValueType();
20498 if (!NVT.bitsEq(SmallVT))
20499 return SDValue();
20500
20501 // Combine:
20502 // (extract_subvec (insert_subvec V1, V2, InsIdx), ExtIdx)
20503 // Into:
20504 // indices are equal or bit offsets are equal => V1
20505 // otherwise => (extract_subvec V1, ExtIdx)
20506 uint64_t InsIdx = V.getConstantOperandVal(2);
20507 if (InsIdx * SmallVT.getScalarSizeInBits() ==
20508 ExtIdx * NVT.getScalarSizeInBits())
20509 return DAG.getBitcast(NVT, V.getOperand(1));
20510 return DAG.getNode(
20511 ISD::EXTRACT_SUBVECTOR, SDLoc(N), NVT,
20512 DAG.getBitcast(N->getOperand(0).getValueType(), V.getOperand(0)),
20513 N->getOperand(1));
20514 }
20515
20516 if (SDValue NarrowBOp = narrowExtractedVectorBinOp(N, DAG, LegalOperations))
20517 return NarrowBOp;
20518
20519 if (SimplifyDemandedVectorElts(SDValue(N, 0)))
20520 return SDValue(N, 0);
20521
20522 return SDValue();
20523}
20524
20525/// Try to convert a wide shuffle of concatenated vectors into 2 narrow shuffles
20526/// followed by concatenation. Narrow vector ops may have better performance
20527/// than wide ops, and this can unlock further narrowing of other vector ops.
20528/// Targets can invert this transform later if it is not profitable.
20529static SDValue foldShuffleOfConcatUndefs(ShuffleVectorSDNode *Shuf,
20530 SelectionDAG &DAG) {
20531 SDValue N0 = Shuf->getOperand(0), N1 = Shuf->getOperand(1);
20532 if (N0.getOpcode() != ISD::CONCAT_VECTORS || N0.getNumOperands() != 2 ||
20533 N1.getOpcode() != ISD::CONCAT_VECTORS || N1.getNumOperands() != 2 ||
20534 !N0.getOperand(1).isUndef() || !N1.getOperand(1).isUndef())
20535 return SDValue();
20536
20537 // Split the wide shuffle mask into halves. Any mask element that is accessing
20538 // operand 1 is offset down to account for narrowing of the vectors.
20539 ArrayRef<int> Mask = Shuf->getMask();
20540 EVT VT = Shuf->getValueType(0);
20541 unsigned NumElts = VT.getVectorNumElements();
20542 unsigned HalfNumElts = NumElts / 2;
20543 SmallVector<int, 16> Mask0(HalfNumElts, -1);
20544 SmallVector<int, 16> Mask1(HalfNumElts, -1);
20545 for (unsigned i = 0; i != NumElts; ++i) {
20546 if (Mask[i] == -1)
20547 continue;
20548 // If we reference the upper (undef) subvector then the element is undef.
20549 if ((Mask[i] % NumElts) >= HalfNumElts)
20550 continue;
20551 int M = Mask[i] < (int)NumElts ? Mask[i] : Mask[i] - (int)HalfNumElts;
20552 if (i < HalfNumElts)
20553 Mask0[i] = M;
20554 else
20555 Mask1[i - HalfNumElts] = M;
20556 }
20557
20558 // Ask the target if this is a valid transform.
20559 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
20560 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
20561 HalfNumElts);
20562 if (!TLI.isShuffleMaskLegal(Mask0, HalfVT) ||
20563 !TLI.isShuffleMaskLegal(Mask1, HalfVT))
20564 return SDValue();
20565
20566 // shuffle (concat X, undef), (concat Y, undef), Mask -->
20567 // concat (shuffle X, Y, Mask0), (shuffle X, Y, Mask1)
20568 SDValue X = N0.getOperand(0), Y = N1.getOperand(0);
20569 SDLoc DL(Shuf);
20570 SDValue Shuf0 = DAG.getVectorShuffle(HalfVT, DL, X, Y, Mask0);
20571 SDValue Shuf1 = DAG.getVectorShuffle(HalfVT, DL, X, Y, Mask1);
20572 return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, Shuf0, Shuf1);
20573}
20574
20575// Tries to turn a shuffle of two CONCAT_VECTORS into a single concat,
20576// or turn a shuffle of a single concat into simpler shuffle then concat.
20577static SDValue partitionShuffleOfConcats(SDNode *N, SelectionDAG &DAG) {
20578 EVT VT = N->getValueType(0);
20579 unsigned NumElts = VT.getVectorNumElements();
20580
20581 SDValue N0 = N->getOperand(0);
20582 SDValue N1 = N->getOperand(1);
20583 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
20584 ArrayRef<int> Mask = SVN->getMask();
20585
20586 SmallVector<SDValue, 4> Ops;
20587 EVT ConcatVT = N0.getOperand(0).getValueType();
20588 unsigned NumElemsPerConcat = ConcatVT.getVectorNumElements();
20589 unsigned NumConcats = NumElts / NumElemsPerConcat;
20590
20591 auto IsUndefMaskElt = [](int i) { return i == -1; };
20592
20593 // Special case: shuffle(concat(A,B)) can be more efficiently represented
20594 // as concat(shuffle(A,B),UNDEF) if the shuffle doesn't set any of the high
20595 // half vector elements.
20596 if (NumElemsPerConcat * 2 == NumElts && N1.isUndef() &&
20597 llvm::all_of(Mask.slice(NumElemsPerConcat, NumElemsPerConcat),
20598 IsUndefMaskElt)) {
20599 N0 = DAG.getVectorShuffle(ConcatVT, SDLoc(N), N0.getOperand(0),
20600 N0.getOperand(1),
20601 Mask.slice(0, NumElemsPerConcat));
20602 N1 = DAG.getUNDEF(ConcatVT);
20603 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, N0, N1);
20604 }
20605
20606 // Look at every vector that's inserted. We're looking for exact
20607 // subvector-sized copies from a concatenated vector
20608 for (unsigned I = 0; I != NumConcats; ++I) {
20609 unsigned Begin = I * NumElemsPerConcat;
20610 ArrayRef<int> SubMask = Mask.slice(Begin, NumElemsPerConcat);
20611
20612 // Make sure we're dealing with a copy.
20613 if (llvm::all_of(SubMask, IsUndefMaskElt)) {
20614 Ops.push_back(DAG.getUNDEF(ConcatVT));
20615 continue;
20616 }
20617
20618 int OpIdx = -1;
20619 for (int i = 0; i != (int)NumElemsPerConcat; ++i) {
20620 if (IsUndefMaskElt(SubMask[i]))
20621 continue;
20622 if ((SubMask[i] % (int)NumElemsPerConcat) != i)
20623 return SDValue();
20624 int EltOpIdx = SubMask[i] / NumElemsPerConcat;
20625 if (0 <= OpIdx && EltOpIdx != OpIdx)
20626 return SDValue();
20627 OpIdx = EltOpIdx;
20628 }
20629 assert(0 <= OpIdx && "Unknown concat_vectors op")(static_cast <bool> (0 <= OpIdx && "Unknown concat_vectors op"
) ? void (0) : __assert_fail ("0 <= OpIdx && \"Unknown concat_vectors op\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 20629, __extension__ __PRETTY_FUNCTION__))
;
20630
20631 if (OpIdx < (int)N0.getNumOperands())
20632 Ops.push_back(N0.getOperand(OpIdx));
20633 else
20634 Ops.push_back(N1.getOperand(OpIdx - N0.getNumOperands()));
20635 }
20636
20637 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, Ops);
20638}
20639
20640// Attempt to combine a shuffle of 2 inputs of 'scalar sources' -
20641// BUILD_VECTOR or SCALAR_TO_VECTOR into a single BUILD_VECTOR.
20642//
20643// SHUFFLE(BUILD_VECTOR(), BUILD_VECTOR()) -> BUILD_VECTOR() is always
20644// a simplification in some sense, but it isn't appropriate in general: some
20645// BUILD_VECTORs are substantially cheaper than others. The general case
20646// of a BUILD_VECTOR requires inserting each element individually (or
20647// performing the equivalent in a temporary stack variable). A BUILD_VECTOR of
20648// all constants is a single constant pool load. A BUILD_VECTOR where each
20649// element is identical is a splat. A BUILD_VECTOR where most of the operands
20650// are undef lowers to a small number of element insertions.
20651//
20652// To deal with this, we currently use a bunch of mostly arbitrary heuristics.
20653// We don't fold shuffles where one side is a non-zero constant, and we don't
20654// fold shuffles if the resulting (non-splat) BUILD_VECTOR would have duplicate
20655// non-constant operands. This seems to work out reasonably well in practice.
20656static SDValue combineShuffleOfScalars(ShuffleVectorSDNode *SVN,
20657 SelectionDAG &DAG,
20658 const TargetLowering &TLI) {
20659 EVT VT = SVN->getValueType(0);
20660 unsigned NumElts = VT.getVectorNumElements();
20661 SDValue N0 = SVN->getOperand(0);
20662 SDValue N1 = SVN->getOperand(1);
20663
20664 if (!N0->hasOneUse())
20665 return SDValue();
20666
20667 // If only one of N1,N2 is constant, bail out if it is not ALL_ZEROS as
20668 // discussed above.
20669 if (!N1.isUndef()) {
20670 if (!N1->hasOneUse())
20671 return SDValue();
20672
20673 bool N0AnyConst = isAnyConstantBuildVector(N0);
20674 bool N1AnyConst = isAnyConstantBuildVector(N1);
20675 if (N0AnyConst && !N1AnyConst && !ISD::isBuildVectorAllZeros(N0.getNode()))
20676 return SDValue();
20677 if (!N0AnyConst && N1AnyConst && !ISD::isBuildVectorAllZeros(N1.getNode()))
20678 return SDValue();
20679 }
20680
20681 // If both inputs are splats of the same value then we can safely merge this
20682 // to a single BUILD_VECTOR with undef elements based on the shuffle mask.
20683 bool IsSplat = false;
20684 auto *BV0 = dyn_cast<BuildVectorSDNode>(N0);
20685 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
20686 if (BV0 && BV1)
20687 if (SDValue Splat0 = BV0->getSplatValue())
20688 IsSplat = (Splat0 == BV1->getSplatValue());
20689
20690 SmallVector<SDValue, 8> Ops;
20691 SmallSet<SDValue, 16> DuplicateOps;
20692 for (int M : SVN->getMask()) {
20693 SDValue Op = DAG.getUNDEF(VT.getScalarType());
20694 if (M >= 0) {
20695 int Idx = M < (int)NumElts ? M : M - NumElts;
20696 SDValue &S = (M < (int)NumElts ? N0 : N1);
20697 if (S.getOpcode() == ISD::BUILD_VECTOR) {
20698 Op = S.getOperand(Idx);
20699 } else if (S.getOpcode() == ISD::SCALAR_TO_VECTOR) {
20700 SDValue Op0 = S.getOperand(0);
20701 Op = Idx == 0 ? Op0 : DAG.getUNDEF(Op0.getValueType());
20702 } else {
20703 // Operand can't be combined - bail out.
20704 return SDValue();
20705 }
20706 }
20707
20708 // Don't duplicate a non-constant BUILD_VECTOR operand unless we're
20709 // generating a splat; semantically, this is fine, but it's likely to
20710 // generate low-quality code if the target can't reconstruct an appropriate
20711 // shuffle.
20712 if (!Op.isUndef() && !isIntOrFPConstant(Op))
20713 if (!IsSplat && !DuplicateOps.insert(Op).second)
20714 return SDValue();
20715
20716 Ops.push_back(Op);
20717 }
20718
20719 // BUILD_VECTOR requires all inputs to be of the same type, find the
20720 // maximum type and extend them all.
20721 EVT SVT = VT.getScalarType();
20722 if (SVT.isInteger())
20723 for (SDValue &Op : Ops)
20724 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
20725 if (SVT != VT.getScalarType())
20726 for (SDValue &Op : Ops)
20727 Op = TLI.isZExtFree(Op.getValueType(), SVT)
20728 ? DAG.getZExtOrTrunc(Op, SDLoc(SVN), SVT)
20729 : DAG.getSExtOrTrunc(Op, SDLoc(SVN), SVT);
20730 return DAG.getBuildVector(VT, SDLoc(SVN), Ops);
20731}
20732
20733// Match shuffles that can be converted to any_vector_extend_in_reg.
20734// This is often generated during legalization.
20735// e.g. v4i32 <0,u,1,u> -> (v2i64 any_vector_extend_in_reg(v4i32 src))
20736// TODO Add support for ZERO_EXTEND_VECTOR_INREG when we have a test case.
20737static SDValue combineShuffleToVectorExtend(ShuffleVectorSDNode *SVN,
20738 SelectionDAG &DAG,
20739 const TargetLowering &TLI,
20740 bool LegalOperations) {
20741 EVT VT = SVN->getValueType(0);
20742 bool IsBigEndian = DAG.getDataLayout().isBigEndian();
20743
20744 // TODO Add support for big-endian when we have a test case.
20745 if (!VT.isInteger() || IsBigEndian)
20746 return SDValue();
20747
20748 unsigned NumElts = VT.getVectorNumElements();
20749 unsigned EltSizeInBits = VT.getScalarSizeInBits();
20750 ArrayRef<int> Mask = SVN->getMask();
20751 SDValue N0 = SVN->getOperand(0);
20752
20753 // shuffle<0,-1,1,-1> == (v2i64 anyextend_vector_inreg(v4i32))
20754 auto isAnyExtend = [&Mask, &NumElts](unsigned Scale) {
20755 for (unsigned i = 0; i != NumElts; ++i) {
20756 if (Mask[i] < 0)
20757 continue;
20758 if ((i % Scale) == 0 && Mask[i] == (int)(i / Scale))
20759 continue;
20760 return false;
20761 }
20762 return true;
20763 };
20764
20765 // Attempt to match a '*_extend_vector_inreg' shuffle, we just search for
20766 // power-of-2 extensions as they are the most likely.
20767 for (unsigned Scale = 2; Scale < NumElts; Scale *= 2) {
20768 // Check for non power of 2 vector sizes
20769 if (NumElts % Scale != 0)
20770 continue;
20771 if (!isAnyExtend(Scale))
20772 continue;
20773
20774 EVT OutSVT = EVT::getIntegerVT(*DAG.getContext(), EltSizeInBits * Scale);
20775 EVT OutVT = EVT::getVectorVT(*DAG.getContext(), OutSVT, NumElts / Scale);
20776 // Never create an illegal type. Only create unsupported operations if we
20777 // are pre-legalization.
20778 if (TLI.isTypeLegal(OutVT))
20779 if (!LegalOperations ||
20780 TLI.isOperationLegalOrCustom(ISD::ANY_EXTEND_VECTOR_INREG, OutVT))
20781 return DAG.getBitcast(VT,
20782 DAG.getNode(ISD::ANY_EXTEND_VECTOR_INREG,
20783 SDLoc(SVN), OutVT, N0));
20784 }
20785
20786 return SDValue();
20787}
20788
20789// Detect 'truncate_vector_inreg' style shuffles that pack the lower parts of
20790// each source element of a large type into the lowest elements of a smaller
20791// destination type. This is often generated during legalization.
20792// If the source node itself was a '*_extend_vector_inreg' node then we should
20793// then be able to remove it.
20794static SDValue combineTruncationShuffle(ShuffleVectorSDNode *SVN,
20795 SelectionDAG &DAG) {
20796 EVT VT = SVN->getValueType(0);
20797 bool IsBigEndian = DAG.getDataLayout().isBigEndian();
20798
20799 // TODO Add support for big-endian when we have a test case.
20800 if (!VT.isInteger() || IsBigEndian)
20801 return SDValue();
20802
20803 SDValue N0 = peekThroughBitcasts(SVN->getOperand(0));
20804
20805 unsigned Opcode = N0.getOpcode();
20806 if (Opcode != ISD::ANY_EXTEND_VECTOR_INREG &&
20807 Opcode != ISD::SIGN_EXTEND_VECTOR_INREG &&
20808 Opcode != ISD::ZERO_EXTEND_VECTOR_INREG)
20809 return SDValue();
20810
20811 SDValue N00 = N0.getOperand(0);
20812 ArrayRef<int> Mask = SVN->getMask();
20813 unsigned NumElts = VT.getVectorNumElements();
20814 unsigned EltSizeInBits = VT.getScalarSizeInBits();
20815 unsigned ExtSrcSizeInBits = N00.getScalarValueSizeInBits();
20816 unsigned ExtDstSizeInBits = N0.getScalarValueSizeInBits();
20817
20818 if (ExtDstSizeInBits % ExtSrcSizeInBits != 0)
20819 return SDValue();
20820 unsigned ExtScale = ExtDstSizeInBits / ExtSrcSizeInBits;
20821
20822 // (v4i32 truncate_vector_inreg(v2i64)) == shuffle<0,2-1,-1>
20823 // (v8i16 truncate_vector_inreg(v4i32)) == shuffle<0,2,4,6,-1,-1,-1,-1>
20824 // (v8i16 truncate_vector_inreg(v2i64)) == shuffle<0,4,-1,-1,-1,-1,-1,-1>
20825 auto isTruncate = [&Mask, &NumElts](unsigned Scale) {
20826 for (unsigned i = 0; i != NumElts; ++i) {
20827 if (Mask[i] < 0)
20828 continue;
20829 if ((i * Scale) < NumElts && Mask[i] == (int)(i * Scale))
20830 continue;
20831 return false;
20832 }
20833 return true;
20834 };
20835
20836 // At the moment we just handle the case where we've truncated back to the
20837 // same size as before the extension.
20838 // TODO: handle more extension/truncation cases as cases arise.
20839 if (EltSizeInBits != ExtSrcSizeInBits)
20840 return SDValue();
20841
20842 // We can remove *extend_vector_inreg only if the truncation happens at
20843 // the same scale as the extension.
20844 if (isTruncate(ExtScale))
20845 return DAG.getBitcast(VT, N00);
20846
20847 return SDValue();
20848}
20849
20850// Combine shuffles of splat-shuffles of the form:
20851// shuffle (shuffle V, undef, splat-mask), undef, M
20852// If splat-mask contains undef elements, we need to be careful about
20853// introducing undef's in the folded mask which are not the result of composing
20854// the masks of the shuffles.
20855static SDValue combineShuffleOfSplatVal(ShuffleVectorSDNode *Shuf,
20856 SelectionDAG &DAG) {
20857 if (!Shuf->getOperand(1).isUndef())
20858 return SDValue();
20859 auto *Splat = dyn_cast<ShuffleVectorSDNode>(Shuf->getOperand(0));
20860 if (!Splat || !Splat->isSplat())
20861 return SDValue();
20862
20863 ArrayRef<int> ShufMask = Shuf->getMask();
20864 ArrayRef<int> SplatMask = Splat->getMask();
20865 assert(ShufMask.size() == SplatMask.size() && "Mask length mismatch")(static_cast <bool> (ShufMask.size() == SplatMask.size(
) && "Mask length mismatch") ? void (0) : __assert_fail
("ShufMask.size() == SplatMask.size() && \"Mask length mismatch\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 20865, __extension__ __PRETTY_FUNCTION__))
;
20866
20867 // Prefer simplifying to the splat-shuffle, if possible. This is legal if
20868 // every undef mask element in the splat-shuffle has a corresponding undef
20869 // element in the user-shuffle's mask or if the composition of mask elements
20870 // would result in undef.
20871 // Examples for (shuffle (shuffle v, undef, SplatMask), undef, UserMask):
20872 // * UserMask=[0,2,u,u], SplatMask=[2,u,2,u] -> [2,2,u,u]
20873 // In this case it is not legal to simplify to the splat-shuffle because we
20874 // may be exposing the users of the shuffle an undef element at index 1
20875 // which was not there before the combine.
20876 // * UserMask=[0,u,2,u], SplatMask=[2,u,2,u] -> [2,u,2,u]
20877 // In this case the composition of masks yields SplatMask, so it's ok to
20878 // simplify to the splat-shuffle.
20879 // * UserMask=[3,u,2,u], SplatMask=[2,u,2,u] -> [u,u,2,u]
20880 // In this case the composed mask includes all undef elements of SplatMask
20881 // and in addition sets element zero to undef. It is safe to simplify to
20882 // the splat-shuffle.
20883 auto CanSimplifyToExistingSplat = [](ArrayRef<int> UserMask,
20884 ArrayRef<int> SplatMask) {
20885 for (unsigned i = 0, e = UserMask.size(); i != e; ++i)
20886 if (UserMask[i] != -1 && SplatMask[i] == -1 &&
20887 SplatMask[UserMask[i]] != -1)
20888 return false;
20889 return true;
20890 };
20891 if (CanSimplifyToExistingSplat(ShufMask, SplatMask))
20892 return Shuf->getOperand(0);
20893
20894 // Create a new shuffle with a mask that is composed of the two shuffles'
20895 // masks.
20896 SmallVector<int, 32> NewMask;
20897 for (int Idx : ShufMask)
20898 NewMask.push_back(Idx == -1 ? -1 : SplatMask[Idx]);
20899
20900 return DAG.getVectorShuffle(Splat->getValueType(0), SDLoc(Splat),
20901 Splat->getOperand(0), Splat->getOperand(1),
20902 NewMask);
20903}
20904
20905/// Combine shuffle of shuffle of the form:
20906/// shuf (shuf X, undef, InnerMask), undef, OuterMask --> splat X
20907static SDValue formSplatFromShuffles(ShuffleVectorSDNode *OuterShuf,
20908 SelectionDAG &DAG) {
20909 if (!OuterShuf->getOperand(1).isUndef())
20910 return SDValue();
20911 auto *InnerShuf = dyn_cast<ShuffleVectorSDNode>(OuterShuf->getOperand(0));
20912 if (!InnerShuf || !InnerShuf->getOperand(1).isUndef())
20913 return SDValue();
20914
20915 ArrayRef<int> OuterMask = OuterShuf->getMask();
20916 ArrayRef<int> InnerMask = InnerShuf->getMask();
20917 unsigned NumElts = OuterMask.size();
20918 assert(NumElts == InnerMask.size() && "Mask length mismatch")(static_cast <bool> (NumElts == InnerMask.size() &&
"Mask length mismatch") ? void (0) : __assert_fail ("NumElts == InnerMask.size() && \"Mask length mismatch\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 20918, __extension__ __PRETTY_FUNCTION__))
;
20919 SmallVector<int, 32> CombinedMask(NumElts, -1);
20920 int SplatIndex = -1;
20921 for (unsigned i = 0; i != NumElts; ++i) {
20922 // Undef lanes remain undef.
20923 int OuterMaskElt = OuterMask[i];
20924 if (OuterMaskElt == -1)
20925 continue;
20926
20927 // Peek through the shuffle masks to get the underlying source element.
20928 int InnerMaskElt = InnerMask[OuterMaskElt];
20929 if (InnerMaskElt == -1)
20930 continue;
20931
20932 // Initialize the splatted element.
20933 if (SplatIndex == -1)
20934 SplatIndex = InnerMaskElt;
20935
20936 // Non-matching index - this is not a splat.
20937 if (SplatIndex != InnerMaskElt)
20938 return SDValue();
20939
20940 CombinedMask[i] = InnerMaskElt;
20941 }
20942 assert((all_of(CombinedMask, [](int M) { return M == -1; }) ||(static_cast <bool> ((all_of(CombinedMask, [](int M) { return
M == -1; }) || getSplatIndex(CombinedMask) != -1) &&
"Expected a splat mask") ? void (0) : __assert_fail ("(all_of(CombinedMask, [](int M) { return M == -1; }) || getSplatIndex(CombinedMask) != -1) && \"Expected a splat mask\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 20944, __extension__ __PRETTY_FUNCTION__))
20943 getSplatIndex(CombinedMask) != -1) &&(static_cast <bool> ((all_of(CombinedMask, [](int M) { return
M == -1; }) || getSplatIndex(CombinedMask) != -1) &&
"Expected a splat mask") ? void (0) : __assert_fail ("(all_of(CombinedMask, [](int M) { return M == -1; }) || getSplatIndex(CombinedMask) != -1) && \"Expected a splat mask\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 20944, __extension__ __PRETTY_FUNCTION__))
20944 "Expected a splat mask")(static_cast <bool> ((all_of(CombinedMask, [](int M) { return
M == -1; }) || getSplatIndex(CombinedMask) != -1) &&
"Expected a splat mask") ? void (0) : __assert_fail ("(all_of(CombinedMask, [](int M) { return M == -1; }) || getSplatIndex(CombinedMask) != -1) && \"Expected a splat mask\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 20944, __extension__ __PRETTY_FUNCTION__))
;
20945
20946 // TODO: The transform may be a win even if the mask is not legal.
20947 EVT VT = OuterShuf->getValueType(0);
20948 assert(VT == InnerShuf->getValueType(0) && "Expected matching shuffle types")(static_cast <bool> (VT == InnerShuf->getValueType(0
) && "Expected matching shuffle types") ? void (0) : __assert_fail
("VT == InnerShuf->getValueType(0) && \"Expected matching shuffle types\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 20948, __extension__ __PRETTY_FUNCTION__))
;
20949 if (!DAG.getTargetLoweringInfo().isShuffleMaskLegal(CombinedMask, VT))
20950 return SDValue();
20951
20952 return DAG.getVectorShuffle(VT, SDLoc(OuterShuf), InnerShuf->getOperand(0),
20953 InnerShuf->getOperand(1), CombinedMask);
20954}
20955
20956/// If the shuffle mask is taking exactly one element from the first vector
20957/// operand and passing through all other elements from the second vector
20958/// operand, return the index of the mask element that is choosing an element
20959/// from the first operand. Otherwise, return -1.
20960static int getShuffleMaskIndexOfOneElementFromOp0IntoOp1(ArrayRef<int> Mask) {
20961 int MaskSize = Mask.size();
20962 int EltFromOp0 = -1;
20963 // TODO: This does not match if there are undef elements in the shuffle mask.
20964 // Should we ignore undefs in the shuffle mask instead? The trade-off is
20965 // removing an instruction (a shuffle), but losing the knowledge that some
20966 // vector lanes are not needed.
20967 for (int i = 0; i != MaskSize; ++i) {
20968 if (Mask[i] >= 0 && Mask[i] < MaskSize) {
20969 // We're looking for a shuffle of exactly one element from operand 0.
20970 if (EltFromOp0 != -1)
20971 return -1;
20972 EltFromOp0 = i;
20973 } else if (Mask[i] != i + MaskSize) {
20974 // Nothing from operand 1 can change lanes.
20975 return -1;
20976 }
20977 }
20978 return EltFromOp0;
20979}
20980
20981/// If a shuffle inserts exactly one element from a source vector operand into
20982/// another vector operand and we can access the specified element as a scalar,
20983/// then we can eliminate the shuffle.
20984static SDValue replaceShuffleOfInsert(ShuffleVectorSDNode *Shuf,
20985 SelectionDAG &DAG) {
20986 // First, check if we are taking one element of a vector and shuffling that
20987 // element into another vector.
20988 ArrayRef<int> Mask = Shuf->getMask();
20989 SmallVector<int, 16> CommutedMask(Mask.begin(), Mask.end());
20990 SDValue Op0 = Shuf->getOperand(0);
20991 SDValue Op1 = Shuf->getOperand(1);
20992 int ShufOp0Index = getShuffleMaskIndexOfOneElementFromOp0IntoOp1(Mask);
20993 if (ShufOp0Index == -1) {
20994 // Commute mask and check again.
20995 ShuffleVectorSDNode::commuteMask(CommutedMask);
20996 ShufOp0Index = getShuffleMaskIndexOfOneElementFromOp0IntoOp1(CommutedMask);
20997 if (ShufOp0Index == -1)
20998 return SDValue();
20999 // Commute operands to match the commuted shuffle mask.
21000 std::swap(Op0, Op1);
21001 Mask = CommutedMask;
21002 }
21003
21004 // The shuffle inserts exactly one element from operand 0 into operand 1.
21005 // Now see if we can access that element as a scalar via a real insert element
21006 // instruction.
21007 // TODO: We can try harder to locate the element as a scalar. Examples: it
21008 // could be an operand of SCALAR_TO_VECTOR, BUILD_VECTOR, or a constant.
21009 assert(Mask[ShufOp0Index] >= 0 && Mask[ShufOp0Index] < (int)Mask.size() &&(static_cast <bool> (Mask[ShufOp0Index] >= 0 &&
Mask[ShufOp0Index] < (int)Mask.size() && "Shuffle mask value must be from operand 0"
) ? void (0) : __assert_fail ("Mask[ShufOp0Index] >= 0 && Mask[ShufOp0Index] < (int)Mask.size() && \"Shuffle mask value must be from operand 0\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 21010, __extension__ __PRETTY_FUNCTION__))
21010 "Shuffle mask value must be from operand 0")(static_cast <bool> (Mask[ShufOp0Index] >= 0 &&
Mask[ShufOp0Index] < (int)Mask.size() && "Shuffle mask value must be from operand 0"
) ? void (0) : __assert_fail ("Mask[ShufOp0Index] >= 0 && Mask[ShufOp0Index] < (int)Mask.size() && \"Shuffle mask value must be from operand 0\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 21010, __extension__ __PRETTY_FUNCTION__))
;
21011 if (Op0.getOpcode() != ISD::INSERT_VECTOR_ELT)
21012 return SDValue();
21013
21014 auto *InsIndexC = dyn_cast<ConstantSDNode>(Op0.getOperand(2));
21015 if (!InsIndexC || InsIndexC->getSExtValue() != Mask[ShufOp0Index])
21016 return SDValue();
21017
21018 // There's an existing insertelement with constant insertion index, so we
21019 // don't need to check the legality/profitability of a replacement operation
21020 // that differs at most in the constant value. The target should be able to
21021 // lower any of those in a similar way. If not, legalization will expand this
21022 // to a scalar-to-vector plus shuffle.
21023 //
21024 // Note that the shuffle may move the scalar from the position that the insert
21025 // element used. Therefore, our new insert element occurs at the shuffle's
21026 // mask index value, not the insert's index value.
21027 // shuffle (insertelt v1, x, C), v2, mask --> insertelt v2, x, C'
21028 SDValue NewInsIndex = DAG.getVectorIdxConstant(ShufOp0Index, SDLoc(Shuf));
21029 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(Shuf), Op0.getValueType(),
21030 Op1, Op0.getOperand(1), NewInsIndex);
21031}
21032
21033/// If we have a unary shuffle of a shuffle, see if it can be folded away
21034/// completely. This has the potential to lose undef knowledge because the first
21035/// shuffle may not have an undef mask element where the second one does. So
21036/// only call this after doing simplifications based on demanded elements.
21037static SDValue simplifyShuffleOfShuffle(ShuffleVectorSDNode *Shuf) {
21038 // shuf (shuf0 X, Y, Mask0), undef, Mask
21039 auto *Shuf0 = dyn_cast<ShuffleVectorSDNode>(Shuf->getOperand(0));
21040 if (!Shuf0 || !Shuf->getOperand(1).isUndef())
21041 return SDValue();
21042
21043 ArrayRef<int> Mask = Shuf->getMask();
21044 ArrayRef<int> Mask0 = Shuf0->getMask();
21045 for (int i = 0, e = (int)Mask.size(); i != e; ++i) {
21046 // Ignore undef elements.
21047 if (Mask[i] == -1)
21048 continue;
21049 assert(Mask[i] >= 0 && Mask[i] < e && "Unexpected shuffle mask value")(static_cast <bool> (Mask[i] >= 0 && Mask[i]
< e && "Unexpected shuffle mask value") ? void (0
) : __assert_fail ("Mask[i] >= 0 && Mask[i] < e && \"Unexpected shuffle mask value\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 21049, __extension__ __PRETTY_FUNCTION__))
;
21050
21051 // Is the element of the shuffle operand chosen by this shuffle the same as
21052 // the element chosen by the shuffle operand itself?
21053 if (Mask0[Mask[i]] != Mask0[i])
21054 return SDValue();
21055 }
21056 // Every element of this shuffle is identical to the result of the previous
21057 // shuffle, so we can replace this value.
21058 return Shuf->getOperand(0);
21059}
21060
21061SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
21062 EVT VT = N->getValueType(0);
21063 unsigned NumElts = VT.getVectorNumElements();
21064
21065 SDValue N0 = N->getOperand(0);
21066 SDValue N1 = N->getOperand(1);
21067
21068 assert(N0.getValueType() == VT && "Vector shuffle must be normalized in DAG")(static_cast <bool> (N0.getValueType() == VT &&
"Vector shuffle must be normalized in DAG") ? void (0) : __assert_fail
("N0.getValueType() == VT && \"Vector shuffle must be normalized in DAG\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 21068, __extension__ __PRETTY_FUNCTION__))
;
21069
21070 // Canonicalize shuffle undef, undef -> undef
21071 if (N0.isUndef() && N1.isUndef())
21072 return DAG.getUNDEF(VT);
21073
21074 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
21075
21076 // Canonicalize shuffle v, v -> v, undef
21077 if (N0 == N1) {
21078 SmallVector<int, 8> NewMask;
21079 for (unsigned i = 0; i != NumElts; ++i) {
21080 int Idx = SVN->getMaskElt(i);
21081 if (Idx >= (int)NumElts) Idx -= NumElts;
21082 NewMask.push_back(Idx);
21083 }
21084 return DAG.getVectorShuffle(VT, SDLoc(N), N0, DAG.getUNDEF(VT), NewMask);
21085 }
21086
21087 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
21088 if (N0.isUndef())
21089 return DAG.getCommutedVectorShuffle(*SVN);
21090
21091 // Remove references to rhs if it is undef
21092 if (N1.isUndef()) {
21093 bool Changed = false;
21094 SmallVector<int, 8> NewMask;
21095 for (unsigned i = 0; i != NumElts; ++i) {
21096 int Idx = SVN->getMaskElt(i);
21097 if (Idx >= (int)NumElts) {
21098 Idx = -1;
21099 Changed = true;
21100 }
21101 NewMask.push_back(Idx);
21102 }
21103 if (Changed)
21104 return DAG.getVectorShuffle(VT, SDLoc(N), N0, N1, NewMask);
21105 }
21106
21107 if (SDValue InsElt = replaceShuffleOfInsert(SVN, DAG))
21108 return InsElt;
21109
21110 // A shuffle of a single vector that is a splatted value can always be folded.
21111 if (SDValue V = combineShuffleOfSplatVal(SVN, DAG))
21112 return V;
21113
21114 if (SDValue V = formSplatFromShuffles(SVN, DAG))
21115 return V;
21116
21117 // If it is a splat, check if the argument vector is another splat or a
21118 // build_vector.
21119 if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) {
21120 int SplatIndex = SVN->getSplatIndex();
21121 if (N0.hasOneUse() && TLI.isExtractVecEltCheap(VT, SplatIndex) &&
21122 TLI.isBinOp(N0.getOpcode()) && N0.getNode()->getNumValues() == 1) {
21123 // splat (vector_bo L, R), Index -->
21124 // splat (scalar_bo (extelt L, Index), (extelt R, Index))
21125 SDValue L = N0.getOperand(0), R = N0.getOperand(1);
21126 SDLoc DL(N);
21127 EVT EltVT = VT.getScalarType();
21128 SDValue Index = DAG.getVectorIdxConstant(SplatIndex, DL);
21129 SDValue ExtL = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, L, Index);
21130 SDValue ExtR = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, R, Index);
21131 SDValue NewBO = DAG.getNode(N0.getOpcode(), DL, EltVT, ExtL, ExtR,
21132 N0.getNode()->getFlags());
21133 SDValue Insert = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, NewBO);
21134 SmallVector<int, 16> ZeroMask(VT.getVectorNumElements(), 0);
21135 return DAG.getVectorShuffle(VT, DL, Insert, DAG.getUNDEF(VT), ZeroMask);
21136 }
21137
21138 // If this is a bit convert that changes the element type of the vector but
21139 // not the number of vector elements, look through it. Be careful not to
21140 // look though conversions that change things like v4f32 to v2f64.
21141 SDNode *V = N0.getNode();
21142 if (V->getOpcode() == ISD::BITCAST) {
21143 SDValue ConvInput = V->getOperand(0);
21144 if (ConvInput.getValueType().isVector() &&
21145 ConvInput.getValueType().getVectorNumElements() == NumElts)
21146 V = ConvInput.getNode();
21147 }
21148
21149 if (V->getOpcode() == ISD::BUILD_VECTOR) {
21150 assert(V->getNumOperands() == NumElts &&(static_cast <bool> (V->getNumOperands() == NumElts &&
"BUILD_VECTOR has wrong number of operands") ? void (0) : __assert_fail
("V->getNumOperands() == NumElts && \"BUILD_VECTOR has wrong number of operands\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 21151, __extension__ __PRETTY_FUNCTION__))
21151 "BUILD_VECTOR has wrong number of operands")(static_cast <bool> (V->getNumOperands() == NumElts &&
"BUILD_VECTOR has wrong number of operands") ? void (0) : __assert_fail
("V->getNumOperands() == NumElts && \"BUILD_VECTOR has wrong number of operands\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 21151, __extension__ __PRETTY_FUNCTION__))
;
21152 SDValue Base;
21153 bool AllSame = true;
21154 for (unsigned i = 0; i != NumElts; ++i) {
21155 if (!V->getOperand(i).isUndef()) {
21156 Base = V->getOperand(i);
21157 break;
21158 }
21159 }
21160 // Splat of <u, u, u, u>, return <u, u, u, u>
21161 if (!Base.getNode())
21162 return N0;
21163 for (unsigned i = 0; i != NumElts; ++i) {
21164 if (V->getOperand(i) != Base) {
21165 AllSame = false;
21166 break;
21167 }
21168 }
21169 // Splat of <x, x, x, x>, return <x, x, x, x>
21170 if (AllSame)
21171 return N0;
21172
21173 // Canonicalize any other splat as a build_vector.
21174 SDValue Splatted = V->getOperand(SplatIndex);
21175 SmallVector<SDValue, 8> Ops(NumElts, Splatted);
21176 SDValue NewBV = DAG.getBuildVector(V->getValueType(0), SDLoc(N), Ops);
21177
21178 // We may have jumped through bitcasts, so the type of the
21179 // BUILD_VECTOR may not match the type of the shuffle.
21180 if (V->getValueType(0) != VT)
21181 NewBV = DAG.getBitcast(VT, NewBV);
21182 return NewBV;
21183 }
21184 }
21185
21186 // Simplify source operands based on shuffle mask.
21187 if (SimplifyDemandedVectorElts(SDValue(N, 0)))
21188 return SDValue(N, 0);
21189
21190 // This is intentionally placed after demanded elements simplification because
21191 // it could eliminate knowledge of undef elements created by this shuffle.
21192 if (SDValue ShufOp = simplifyShuffleOfShuffle(SVN))
21193 return ShufOp;
21194
21195 // Match shuffles that can be converted to any_vector_extend_in_reg.
21196 if (SDValue V = combineShuffleToVectorExtend(SVN, DAG, TLI, LegalOperations))
21197 return V;
21198
21199 // Combine "truncate_vector_in_reg" style shuffles.
21200 if (SDValue V = combineTruncationShuffle(SVN, DAG))
21201 return V;
21202
21203 if (N0.getOpcode() == ISD::CONCAT_VECTORS &&
21204 Level < AfterLegalizeVectorOps &&
21205 (N1.isUndef() ||
21206 (N1.getOpcode() == ISD::CONCAT_VECTORS &&
21207 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()))) {
21208 if (SDValue V = partitionShuffleOfConcats(N, DAG))
21209 return V;
21210 }
21211
21212 // A shuffle of a concat of the same narrow vector can be reduced to use
21213 // only low-half elements of a concat with undef:
21214 // shuf (concat X, X), undef, Mask --> shuf (concat X, undef), undef, Mask'
21215 if (N0.getOpcode() == ISD::CONCAT_VECTORS && N1.isUndef() &&
21216 N0.getNumOperands() == 2 &&
21217 N0.getOperand(0) == N0.getOperand(1)) {
21218 int HalfNumElts = (int)NumElts / 2;
21219 SmallVector<int, 8> NewMask;
21220 for (unsigned i = 0; i != NumElts; ++i) {
21221 int Idx = SVN->getMaskElt(i);
21222 if (Idx >= HalfNumElts) {
21223 assert(Idx < (int)NumElts && "Shuffle mask chooses undef op")(static_cast <bool> (Idx < (int)NumElts && "Shuffle mask chooses undef op"
) ? void (0) : __assert_fail ("Idx < (int)NumElts && \"Shuffle mask chooses undef op\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 21223, __extension__ __PRETTY_FUNCTION__))
;
21224 Idx -= HalfNumElts;
21225 }
21226 NewMask.push_back(Idx);
21227 }
21228 if (TLI.isShuffleMaskLegal(NewMask, VT)) {
21229 SDValue UndefVec = DAG.getUNDEF(N0.getOperand(0).getValueType());
21230 SDValue NewCat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
21231 N0.getOperand(0), UndefVec);
21232 return DAG.getVectorShuffle(VT, SDLoc(N), NewCat, N1, NewMask);
21233 }
21234 }
21235
21236 // Attempt to combine a shuffle of 2 inputs of 'scalar sources' -
21237 // BUILD_VECTOR or SCALAR_TO_VECTOR into a single BUILD_VECTOR.
21238 if (Level < AfterLegalizeDAG && TLI.isTypeLegal(VT))
21239 if (SDValue Res = combineShuffleOfScalars(SVN, DAG, TLI))
21240 return Res;
21241
21242 // If this shuffle only has a single input that is a bitcasted shuffle,
21243 // attempt to merge the 2 shuffles and suitably bitcast the inputs/output
21244 // back to their original types.
21245 if (N0.getOpcode() == ISD::BITCAST && N0.hasOneUse() &&
21246 N1.isUndef() && Level < AfterLegalizeVectorOps &&
21247 TLI.isTypeLegal(VT)) {
21248
21249 SDValue BC0 = peekThroughOneUseBitcasts(N0);
21250 if (BC0.getOpcode() == ISD::VECTOR_SHUFFLE && BC0.hasOneUse()) {
21251 EVT SVT = VT.getScalarType();
21252 EVT InnerVT = BC0->getValueType(0);
21253 EVT InnerSVT = InnerVT.getScalarType();
21254
21255 // Determine which shuffle works with the smaller scalar type.
21256 EVT ScaleVT = SVT.bitsLT(InnerSVT) ? VT : InnerVT;
21257 EVT ScaleSVT = ScaleVT.getScalarType();
21258
21259 if (TLI.isTypeLegal(ScaleVT) &&
21260 0 == (InnerSVT.getSizeInBits() % ScaleSVT.getSizeInBits()) &&
21261 0 == (SVT.getSizeInBits() % ScaleSVT.getSizeInBits())) {
21262 int InnerScale = InnerSVT.getSizeInBits() / ScaleSVT.getSizeInBits();
21263 int OuterScale = SVT.getSizeInBits() / ScaleSVT.getSizeInBits();
21264
21265 // Scale the shuffle masks to the smaller scalar type.
21266 ShuffleVectorSDNode *InnerSVN = cast<ShuffleVectorSDNode>(BC0);
21267 SmallVector<int, 8> InnerMask;
21268 SmallVector<int, 8> OuterMask;
21269 narrowShuffleMaskElts(InnerScale, InnerSVN->getMask(), InnerMask);
21270 narrowShuffleMaskElts(OuterScale, SVN->getMask(), OuterMask);
21271
21272 // Merge the shuffle masks.
21273 SmallVector<int, 8> NewMask;
21274 for (int M : OuterMask)
21275 NewMask.push_back(M < 0 ? -1 : InnerMask[M]);
21276
21277 // Test for shuffle mask legality over both commutations.
21278 SDValue SV0 = BC0->getOperand(0);
21279 SDValue SV1 = BC0->getOperand(1);
21280 bool LegalMask = TLI.isShuffleMaskLegal(NewMask, ScaleVT);
21281 if (!LegalMask) {
21282 std::swap(SV0, SV1);
21283 ShuffleVectorSDNode::commuteMask(NewMask);
21284 LegalMask = TLI.isShuffleMaskLegal(NewMask, ScaleVT);
21285 }
21286
21287 if (LegalMask) {
21288 SV0 = DAG.getBitcast(ScaleVT, SV0);
21289 SV1 = DAG.getBitcast(ScaleVT, SV1);
21290 return DAG.getBitcast(
21291 VT, DAG.getVectorShuffle(ScaleVT, SDLoc(N), SV0, SV1, NewMask));
21292 }
21293 }
21294 }
21295 }
21296
21297 // Compute the combined shuffle mask for a shuffle with SV0 as the first
21298 // operand, and SV1 as the second operand.
21299 // i.e. Merge SVN(OtherSVN, N1) -> shuffle(SV0, SV1, Mask) iff Commute = false
21300 // Merge SVN(N1, OtherSVN) -> shuffle(SV0, SV1, Mask') iff Commute = true
21301 auto MergeInnerShuffle =
21302 [NumElts, &VT](bool Commute, ShuffleVectorSDNode *SVN,
21303 ShuffleVectorSDNode *OtherSVN, SDValue N1,
21304 const TargetLowering &TLI, SDValue &SV0, SDValue &SV1,
21305 SmallVectorImpl<int> &Mask) -> bool {
21306 // Don't try to fold splats; they're likely to simplify somehow, or they
21307 // might be free.
21308 if (OtherSVN->isSplat())
21309 return false;
21310
21311 SV0 = SV1 = SDValue();
21312 Mask.clear();
21313
21314 for (unsigned i = 0; i != NumElts; ++i) {
21315 int Idx = SVN->getMaskElt(i);
21316 if (Idx < 0) {
21317 // Propagate Undef.
21318 Mask.push_back(Idx);
21319 continue;
21320 }
21321
21322 if (Commute)
21323 Idx = (Idx < (int)NumElts) ? (Idx + NumElts) : (Idx - NumElts);
21324
21325 SDValue CurrentVec;
21326 if (Idx < (int)NumElts) {
21327 // This shuffle index refers to the inner shuffle N0. Lookup the inner
21328 // shuffle mask to identify which vector is actually referenced.
21329 Idx = OtherSVN->getMaskElt(Idx);
21330 if (Idx < 0) {
21331 // Propagate Undef.
21332 Mask.push_back(Idx);
21333 continue;
21334 }
21335 CurrentVec = (Idx < (int)NumElts) ? OtherSVN->getOperand(0)
21336 : OtherSVN->getOperand(1);
21337 } else {
21338 // This shuffle index references an element within N1.
21339 CurrentVec = N1;
21340 }
21341
21342 // Simple case where 'CurrentVec' is UNDEF.
21343 if (CurrentVec.isUndef()) {
21344 Mask.push_back(-1);
21345 continue;
21346 }
21347
21348 // Canonicalize the shuffle index. We don't know yet if CurrentVec
21349 // will be the first or second operand of the combined shuffle.
21350 Idx = Idx % NumElts;
21351 if (!SV0.getNode() || SV0 == CurrentVec) {
21352 // Ok. CurrentVec is the left hand side.
21353 // Update the mask accordingly.
21354 SV0 = CurrentVec;
21355 Mask.push_back(Idx);
21356 continue;
21357 }
21358 if (!SV1.getNode() || SV1 == CurrentVec) {
21359 // Ok. CurrentVec is the right hand side.
21360 // Update the mask accordingly.
21361 SV1 = CurrentVec;
21362 Mask.push_back(Idx + NumElts);
21363 continue;
21364 }
21365
21366 // Last chance - see if the vector is another shuffle and if it
21367 // uses one of the existing candidate shuffle ops.
21368 if (auto *CurrentSVN = dyn_cast<ShuffleVectorSDNode>(CurrentVec)) {
21369 int InnerIdx = CurrentSVN->getMaskElt(Idx);
21370 if (InnerIdx < 0) {
21371 Mask.push_back(-1);
21372 continue;
21373 }
21374 SDValue InnerVec = (InnerIdx < (int)NumElts)
21375 ? CurrentSVN->getOperand(0)
21376 : CurrentSVN->getOperand(1);
21377 if (InnerVec.isUndef()) {
21378 Mask.push_back(-1);
21379 continue;
21380 }
21381 InnerIdx %= NumElts;
21382 if (InnerVec == SV0) {
21383 Mask.push_back(InnerIdx);
21384 continue;
21385 }
21386 if (InnerVec == SV1) {
21387 Mask.push_back(InnerIdx + NumElts);
21388 continue;
21389 }
21390 }
21391
21392 // Bail out if we cannot convert the shuffle pair into a single shuffle.
21393 return false;
21394 }
21395
21396 if (llvm::all_of(Mask, [](int M) { return M < 0; }))
21397 return true;
21398
21399 // Avoid introducing shuffles with illegal mask.
21400 // shuffle(shuffle(A, B, M0), C, M1) -> shuffle(A, B, M2)
21401 // shuffle(shuffle(A, B, M0), C, M1) -> shuffle(A, C, M2)
21402 // shuffle(shuffle(A, B, M0), C, M1) -> shuffle(B, C, M2)
21403 // shuffle(shuffle(A, B, M0), C, M1) -> shuffle(B, A, M2)
21404 // shuffle(shuffle(A, B, M0), C, M1) -> shuffle(C, A, M2)
21405 // shuffle(shuffle(A, B, M0), C, M1) -> shuffle(C, B, M2)
21406 if (TLI.isShuffleMaskLegal(Mask, VT))
21407 return true;
21408
21409 std::swap(SV0, SV1);
21410 ShuffleVectorSDNode::commuteMask(Mask);
21411 return TLI.isShuffleMaskLegal(Mask, VT);
21412 };
21413
21414 if (Level < AfterLegalizeDAG && TLI.isTypeLegal(VT)) {
21415 // Canonicalize shuffles according to rules:
21416 // shuffle(A, shuffle(A, B)) -> shuffle(shuffle(A,B), A)
21417 // shuffle(B, shuffle(A, B)) -> shuffle(shuffle(A,B), B)
21418 // shuffle(B, shuffle(A, Undef)) -> shuffle(shuffle(A, Undef), B)
21419 if (N1.getOpcode() == ISD::VECTOR_SHUFFLE &&
21420 N0.getOpcode() != ISD::VECTOR_SHUFFLE) {
21421 // The incoming shuffle must be of the same type as the result of the
21422 // current shuffle.
21423 assert(N1->getOperand(0).getValueType() == VT &&(static_cast <bool> (N1->getOperand(0).getValueType(
) == VT && "Shuffle types don't match") ? void (0) : __assert_fail
("N1->getOperand(0).getValueType() == VT && \"Shuffle types don't match\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 21424, __extension__ __PRETTY_FUNCTION__))
21424 "Shuffle types don't match")(static_cast <bool> (N1->getOperand(0).getValueType(
) == VT && "Shuffle types don't match") ? void (0) : __assert_fail
("N1->getOperand(0).getValueType() == VT && \"Shuffle types don't match\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 21424, __extension__ __PRETTY_FUNCTION__))
;
21425
21426 SDValue SV0 = N1->getOperand(0);
21427 SDValue SV1 = N1->getOperand(1);
21428 bool HasSameOp0 = N0 == SV0;
21429 bool IsSV1Undef = SV1.isUndef();
21430 if (HasSameOp0 || IsSV1Undef || N0 == SV1)
21431 // Commute the operands of this shuffle so merging below will trigger.
21432 return DAG.getCommutedVectorShuffle(*SVN);
21433 }
21434
21435 // Canonicalize splat shuffles to the RHS to improve merging below.
21436 // shuffle(splat(A,u), shuffle(C,D)) -> shuffle'(shuffle(C,D), splat(A,u))
21437 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE &&
21438 N1.getOpcode() == ISD::VECTOR_SHUFFLE &&
21439 cast<ShuffleVectorSDNode>(N0)->isSplat() &&
21440 !cast<ShuffleVectorSDNode>(N1)->isSplat()) {
21441 return DAG.getCommutedVectorShuffle(*SVN);
21442 }
21443
21444 // Try to fold according to rules:
21445 // shuffle(shuffle(A, B, M0), C, M1) -> shuffle(A, B, M2)
21446 // shuffle(shuffle(A, B, M0), C, M1) -> shuffle(A, C, M2)
21447 // shuffle(shuffle(A, B, M0), C, M1) -> shuffle(B, C, M2)
21448 // Don't try to fold shuffles with illegal type.
21449 // Only fold if this shuffle is the only user of the other shuffle.
21450 // Try matching shuffle(C,shuffle(A,B)) commutted patterns as well.
21451 for (int i = 0; i != 2; ++i) {
21452 if (N->getOperand(i).getOpcode() == ISD::VECTOR_SHUFFLE &&
21453 N->isOnlyUserOf(N->getOperand(i).getNode())) {
21454 // The incoming shuffle must be of the same type as the result of the
21455 // current shuffle.
21456 auto *OtherSV = cast<ShuffleVectorSDNode>(N->getOperand(i));
21457 assert(OtherSV->getOperand(0).getValueType() == VT &&(static_cast <bool> (OtherSV->getOperand(0).getValueType
() == VT && "Shuffle types don't match") ? void (0) :
__assert_fail ("OtherSV->getOperand(0).getValueType() == VT && \"Shuffle types don't match\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 21458, __extension__ __PRETTY_FUNCTION__))
21458 "Shuffle types don't match")(static_cast <bool> (OtherSV->getOperand(0).getValueType
() == VT && "Shuffle types don't match") ? void (0) :
__assert_fail ("OtherSV->getOperand(0).getValueType() == VT && \"Shuffle types don't match\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 21458, __extension__ __PRETTY_FUNCTION__))
;
21459
21460 SDValue SV0, SV1;
21461 SmallVector<int, 4> Mask;
21462 if (MergeInnerShuffle(i != 0, SVN, OtherSV, N->getOperand(1 - i), TLI,
21463 SV0, SV1, Mask)) {
21464 // Check if all indices in Mask are Undef. In case, propagate Undef.
21465 if (llvm::all_of(Mask, [](int M) { return M < 0; }))
21466 return DAG.getUNDEF(VT);
21467
21468 return DAG.getVectorShuffle(VT, SDLoc(N),
21469 SV0 ? SV0 : DAG.getUNDEF(VT),
21470 SV1 ? SV1 : DAG.getUNDEF(VT), Mask);
21471 }
21472 }
21473 }
21474
21475 // Merge shuffles through binops if we are able to merge it with at least
21476 // one other shuffles.
21477 // shuffle(bop(shuffle(x,y),shuffle(z,w)),undef)
21478 // shuffle(bop(shuffle(x,y),shuffle(z,w)),bop(shuffle(a,b),shuffle(c,d)))
21479 unsigned SrcOpcode = N0.getOpcode();
21480 if (TLI.isBinOp(SrcOpcode) && N->isOnlyUserOf(N0.getNode()) &&
21481 (N1.isUndef() ||
21482 (SrcOpcode == N1.getOpcode() && N->isOnlyUserOf(N1.getNode())))) {
21483 // Get binop source ops, or just pass on the undef.
21484 SDValue Op00 = N0.getOperand(0);
21485 SDValue Op01 = N0.getOperand(1);
21486 SDValue Op10 = N1.isUndef() ? N1 : N1.getOperand(0);
21487 SDValue Op11 = N1.isUndef() ? N1 : N1.getOperand(1);
21488 // TODO: We might be able to relax the VT check but we don't currently
21489 // have any isBinOp() that has different result/ops VTs so play safe until
21490 // we have test coverage.
21491 if (Op00.getValueType() == VT && Op10.getValueType() == VT &&
21492 Op01.getValueType() == VT && Op11.getValueType() == VT &&
21493 (Op00.getOpcode() == ISD::VECTOR_SHUFFLE ||
21494 Op10.getOpcode() == ISD::VECTOR_SHUFFLE ||
21495 Op01.getOpcode() == ISD::VECTOR_SHUFFLE ||
21496 Op11.getOpcode() == ISD::VECTOR_SHUFFLE)) {
21497 auto CanMergeInnerShuffle = [&](SDValue &SV0, SDValue &SV1,
21498 SmallVectorImpl<int> &Mask, bool LeftOp,
21499 bool Commute) {
21500 SDValue InnerN = Commute ? N1 : N0;
21501 SDValue Op0 = LeftOp ? Op00 : Op01;
21502 SDValue Op1 = LeftOp ? Op10 : Op11;
21503 if (Commute)
21504 std::swap(Op0, Op1);
21505 // Only accept the merged shuffle if we don't introduce undef elements,
21506 // or the inner shuffle already contained undef elements.
21507 auto *SVN0 = dyn_cast<ShuffleVectorSDNode>(Op0);
21508 return SVN0 && InnerN->isOnlyUserOf(SVN0) &&
21509 MergeInnerShuffle(Commute, SVN, SVN0, Op1, TLI, SV0, SV1,
21510 Mask) &&
21511 (llvm::any_of(SVN0->getMask(), [](int M) { return M < 0; }) ||
21512 llvm::none_of(Mask, [](int M) { return M < 0; }));
21513 };
21514
21515 // Ensure we don't increase the number of shuffles - we must merge a
21516 // shuffle from at least one of the LHS and RHS ops.
21517 bool MergedLeft = false;
21518 SDValue LeftSV0, LeftSV1;
21519 SmallVector<int, 4> LeftMask;
21520 if (CanMergeInnerShuffle(LeftSV0, LeftSV1, LeftMask, true, false) ||
21521 CanMergeInnerShuffle(LeftSV0, LeftSV1, LeftMask, true, true)) {
21522 MergedLeft = true;
21523 } else {
21524 LeftMask.assign(SVN->getMask().begin(), SVN->getMask().end());
21525 LeftSV0 = Op00, LeftSV1 = Op10;
21526 }
21527
21528 bool MergedRight = false;
21529 SDValue RightSV0, RightSV1;
21530 SmallVector<int, 4> RightMask;
21531 if (CanMergeInnerShuffle(RightSV0, RightSV1, RightMask, false, false) ||
21532 CanMergeInnerShuffle(RightSV0, RightSV1, RightMask, false, true)) {
21533 MergedRight = true;
21534 } else {
21535 RightMask.assign(SVN->getMask().begin(), SVN->getMask().end());
21536 RightSV0 = Op01, RightSV1 = Op11;
21537 }
21538
21539 if (MergedLeft || MergedRight) {
21540 SDLoc DL(N);
21541 SDValue LHS = DAG.getVectorShuffle(
21542 VT, DL, LeftSV0 ? LeftSV0 : DAG.getUNDEF(VT),
21543 LeftSV1 ? LeftSV1 : DAG.getUNDEF(VT), LeftMask);
21544 SDValue RHS = DAG.getVectorShuffle(
21545 VT, DL, RightSV0 ? RightSV0 : DAG.getUNDEF(VT),
21546 RightSV1 ? RightSV1 : DAG.getUNDEF(VT), RightMask);
21547 return DAG.getNode(SrcOpcode, DL, VT, LHS, RHS);
21548 }
21549 }
21550 }
21551 }
21552
21553 if (SDValue V = foldShuffleOfConcatUndefs(SVN, DAG))
21554 return V;
21555
21556 return SDValue();
21557}
21558
21559SDValue DAGCombiner::visitSCALAR_TO_VECTOR(SDNode *N) {
21560 SDValue InVal = N->getOperand(0);
21561 EVT VT = N->getValueType(0);
21562
21563 // Replace a SCALAR_TO_VECTOR(EXTRACT_VECTOR_ELT(V,C0)) pattern
21564 // with a VECTOR_SHUFFLE and possible truncate.
21565 if (InVal.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
21566 VT.isFixedLengthVector() &&
21567 InVal->getOperand(0).getValueType().isFixedLengthVector()) {
21568 SDValue InVec = InVal->getOperand(0);
21569 SDValue EltNo = InVal->getOperand(1);
21570 auto InVecT = InVec.getValueType();
21571 if (ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(EltNo)) {
21572 SmallVector<int, 8> NewMask(InVecT.getVectorNumElements(), -1);
21573 int Elt = C0->getZExtValue();
21574 NewMask[0] = Elt;
21575 // If we have an implict truncate do truncate here as long as it's legal.
21576 // if it's not legal, this should
21577 if (VT.getScalarType() != InVal.getValueType() &&
21578 InVal.getValueType().isScalarInteger() &&
21579 isTypeLegal(VT.getScalarType())) {
21580 SDValue Val =
21581 DAG.getNode(ISD::TRUNCATE, SDLoc(InVal), VT.getScalarType(), InVal);
21582 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), VT, Val);
21583 }
21584 if (VT.getScalarType() == InVecT.getScalarType() &&
21585 VT.getVectorNumElements() <= InVecT.getVectorNumElements()) {
21586 SDValue LegalShuffle =
21587 TLI.buildLegalVectorShuffle(InVecT, SDLoc(N), InVec,
21588 DAG.getUNDEF(InVecT), NewMask, DAG);
21589 if (LegalShuffle) {
21590 // If the initial vector is the correct size this shuffle is a
21591 // valid result.
21592 if (VT == InVecT)
21593 return LegalShuffle;
21594 // If not we must truncate the vector.
21595 if (VT.getVectorNumElements() != InVecT.getVectorNumElements()) {
21596 SDValue ZeroIdx = DAG.getVectorIdxConstant(0, SDLoc(N));
21597 EVT SubVT = EVT::getVectorVT(*DAG.getContext(),
21598 InVecT.getVectorElementType(),
21599 VT.getVectorNumElements());
21600 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(N), SubVT,
21601 LegalShuffle, ZeroIdx);
21602 }
21603 }
21604 }
21605 }
21606 }
21607
21608 return SDValue();
21609}
21610
21611SDValue DAGCombiner::visitINSERT_SUBVECTOR(SDNode *N) {
21612 EVT VT = N->getValueType(0);
21613 SDValue N0 = N->getOperand(0);
21614 SDValue N1 = N->getOperand(1);
21615 SDValue N2 = N->getOperand(2);
21616 uint64_t InsIdx = N->getConstantOperandVal(2);
21617
21618 // If inserting an UNDEF, just return the original vector.
21619 if (N1.isUndef())
21620 return N0;
21621
21622 // If this is an insert of an extracted vector into an undef vector, we can
21623 // just use the input to the extract.
21624 if (N0.isUndef() && N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
21625 N1.getOperand(1) == N2 && N1.getOperand(0).getValueType() == VT)
21626 return N1.getOperand(0);
21627
21628 // If we are inserting a bitcast value into an undef, with the same
21629 // number of elements, just use the bitcast input of the extract.
21630 // i.e. INSERT_SUBVECTOR UNDEF (BITCAST N1) N2 ->
21631 // BITCAST (INSERT_SUBVECTOR UNDEF N1 N2)
21632 if (N0.isUndef() && N1.getOpcode() == ISD::BITCAST &&
21633 N1.getOperand(0).getOpcode() == ISD::EXTRACT_SUBVECTOR &&
21634 N1.getOperand(0).getOperand(1) == N2 &&
21635 N1.getOperand(0).getOperand(0).getValueType().getVectorElementCount() ==
21636 VT.getVectorElementCount() &&
21637 N1.getOperand(0).getOperand(0).getValueType().getSizeInBits() ==
21638 VT.getSizeInBits()) {
21639 return DAG.getBitcast(VT, N1.getOperand(0).getOperand(0));
21640 }
21641
21642 // If both N1 and N2 are bitcast values on which insert_subvector
21643 // would makes sense, pull the bitcast through.
21644 // i.e. INSERT_SUBVECTOR (BITCAST N0) (BITCAST N1) N2 ->
21645 // BITCAST (INSERT_SUBVECTOR N0 N1 N2)
21646 if (N0.getOpcode() == ISD::BITCAST && N1.getOpcode() == ISD::BITCAST) {
21647 SDValue CN0 = N0.getOperand(0);
21648 SDValue CN1 = N1.getOperand(0);
21649 EVT CN0VT = CN0.getValueType();
21650 EVT CN1VT = CN1.getValueType();
21651 if (CN0VT.isVector() && CN1VT.isVector() &&
21652 CN0VT.getVectorElementType() == CN1VT.getVectorElementType() &&
21653 CN0VT.getVectorElementCount() == VT.getVectorElementCount()) {
21654 SDValue NewINSERT = DAG.getNode(ISD::INSERT_SUBVECTOR, SDLoc(N),
21655 CN0.getValueType(), CN0, CN1, N2);
21656 return DAG.getBitcast(VT, NewINSERT);
21657 }
21658 }
21659
21660 // Combine INSERT_SUBVECTORs where we are inserting to the same index.
21661 // INSERT_SUBVECTOR( INSERT_SUBVECTOR( Vec, SubOld, Idx ), SubNew, Idx )
21662 // --> INSERT_SUBVECTOR( Vec, SubNew, Idx )
21663 if (N0.getOpcode() == ISD::INSERT_SUBVECTOR &&
21664 N0.getOperand(1).getValueType() == N1.getValueType() &&
21665 N0.getOperand(2) == N2)
21666 return DAG.getNode(ISD::INSERT_SUBVECTOR, SDLoc(N), VT, N0.getOperand(0),
21667 N1, N2);
21668
21669 // Eliminate an intermediate insert into an undef vector:
21670 // insert_subvector undef, (insert_subvector undef, X, 0), N2 -->
21671 // insert_subvector undef, X, N2
21672 if (N0.isUndef() && N1.getOpcode() == ISD::INSERT_SUBVECTOR &&
21673 N1.getOperand(0).isUndef() && isNullConstant(N1.getOperand(2)))
21674 return DAG.getNode(ISD::INSERT_SUBVECTOR, SDLoc(N), VT, N0,
21675 N1.getOperand(1), N2);
21676
21677 // Push subvector bitcasts to the output, adjusting the index as we go.
21678 // insert_subvector(bitcast(v), bitcast(s), c1)
21679 // -> bitcast(insert_subvector(v, s, c2))
21680 if ((N0.isUndef() || N0.getOpcode() == ISD::BITCAST) &&
21681 N1.getOpcode() == ISD::BITCAST) {
21682 SDValue N0Src = peekThroughBitcasts(N0);
21683 SDValue N1Src = peekThroughBitcasts(N1);
21684 EVT N0SrcSVT = N0Src.getValueType().getScalarType();
21685 EVT N1SrcSVT = N1Src.getValueType().getScalarType();
21686 if ((N0.isUndef() || N0SrcSVT == N1SrcSVT) &&
21687 N0Src.getValueType().isVector() && N1Src.getValueType().isVector()) {
21688 EVT NewVT;
21689 SDLoc DL(N);
21690 SDValue NewIdx;
21691 LLVMContext &Ctx = *DAG.getContext();
21692 ElementCount NumElts = VT.getVectorElementCount();
21693 unsigned EltSizeInBits = VT.getScalarSizeInBits();
21694 if ((EltSizeInBits % N1SrcSVT.getSizeInBits()) == 0) {
21695 unsigned Scale = EltSizeInBits / N1SrcSVT.getSizeInBits();
21696 NewVT = EVT::getVectorVT(Ctx, N1SrcSVT, NumElts * Scale);
21697 NewIdx = DAG.getVectorIdxConstant(InsIdx * Scale, DL);
21698 } else if ((N1SrcSVT.getSizeInBits() % EltSizeInBits) == 0) {
21699 unsigned Scale = N1SrcSVT.getSizeInBits() / EltSizeInBits;
21700 if (NumElts.isKnownMultipleOf(Scale) && (InsIdx % Scale) == 0) {
21701 NewVT = EVT::getVectorVT(Ctx, N1SrcSVT,
21702 NumElts.divideCoefficientBy(Scale));
21703 NewIdx = DAG.getVectorIdxConstant(InsIdx / Scale, DL);
21704 }
21705 }
21706 if (NewIdx && hasOperation(ISD::INSERT_SUBVECTOR, NewVT)) {
21707 SDValue Res = DAG.getBitcast(NewVT, N0Src);
21708 Res = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, NewVT, Res, N1Src, NewIdx);
21709 return DAG.getBitcast(VT, Res);
21710 }
21711 }
21712 }
21713
21714 // Canonicalize insert_subvector dag nodes.
21715 // Example:
21716 // (insert_subvector (insert_subvector A, Idx0), Idx1)
21717 // -> (insert_subvector (insert_subvector A, Idx1), Idx0)
21718 if (N0.getOpcode() == ISD::INSERT_SUBVECTOR && N0.hasOneUse() &&
21719 N1.getValueType() == N0.getOperand(1).getValueType()) {
21720 unsigned OtherIdx = N0.getConstantOperandVal(2);
21721 if (InsIdx < OtherIdx) {
21722 // Swap nodes.
21723 SDValue NewOp = DAG.getNode(ISD::INSERT_SUBVECTOR, SDLoc(N), VT,
21724 N0.getOperand(0), N1, N2);
21725 AddToWorklist(NewOp.getNode());
21726 return DAG.getNode(ISD::INSERT_SUBVECTOR, SDLoc(N0.getNode()),
21727 VT, NewOp, N0.getOperand(1), N0.getOperand(2));
21728 }
21729 }
21730
21731 // If the input vector is a concatenation, and the insert replaces
21732 // one of the pieces, we can optimize into a single concat_vectors.
21733 if (N0.getOpcode() == ISD::CONCAT_VECTORS && N0.hasOneUse() &&
21734 N0.getOperand(0).getValueType() == N1.getValueType() &&
21735 N0.getOperand(0).getValueType().isScalableVector() ==
21736 N1.getValueType().isScalableVector()) {
21737 unsigned Factor = N1.getValueType().getVectorMinNumElements();
21738 SmallVector<SDValue, 8> Ops(N0->op_begin(), N0->op_end());
21739 Ops[InsIdx / Factor] = N1;
21740 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, Ops);
21741 }
21742
21743 // Simplify source operands based on insertion.
21744 if (SimplifyDemandedVectorElts(SDValue(N, 0)))
21745 return SDValue(N, 0);
21746
21747 return SDValue();
21748}
21749
21750SDValue DAGCombiner::visitFP_TO_FP16(SDNode *N) {
21751 SDValue N0 = N->getOperand(0);
21752
21753 // fold (fp_to_fp16 (fp16_to_fp op)) -> op
21754 if (N0->getOpcode() == ISD::FP16_TO_FP)
21755 return N0->getOperand(0);
21756
21757 return SDValue();
21758}
21759
21760SDValue DAGCombiner::visitFP16_TO_FP(SDNode *N) {
21761 SDValue N0 = N->getOperand(0);
21762
21763 // fold fp16_to_fp(op & 0xffff) -> fp16_to_fp(op)
21764 if (!TLI.shouldKeepZExtForFP16Conv() && N0->getOpcode() == ISD::AND) {
21765 ConstantSDNode *AndConst = getAsNonOpaqueConstant(N0.getOperand(1));
21766 if (AndConst && AndConst->getAPIntValue() == 0xffff) {
21767 return DAG.getNode(ISD::FP16_TO_FP, SDLoc(N), N->getValueType(0),
21768 N0.getOperand(0));
21769 }
21770 }
21771
21772 return SDValue();
21773}
21774
21775SDValue DAGCombiner::visitVECREDUCE(SDNode *N) {
21776 SDValue N0 = N->getOperand(0);
21777 EVT VT = N0.getValueType();
21778 unsigned Opcode = N->getOpcode();
21779
21780 // VECREDUCE over 1-element vector is just an extract.
21781 if (VT.getVectorElementCount().isScalar()) {
21782 SDLoc dl(N);
21783 SDValue Res =
21784 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT.getVectorElementType(), N0,
21785 DAG.getVectorIdxConstant(0, dl));
21786 if (Res.getValueType() != N->getValueType(0))
21787 Res = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Res);
21788 return Res;
21789 }
21790
21791 // On an boolean vector an and/or reduction is the same as a umin/umax
21792 // reduction. Convert them if the latter is legal while the former isn't.
21793 if (Opcode == ISD::VECREDUCE_AND || Opcode == ISD::VECREDUCE_OR) {
21794 unsigned NewOpcode = Opcode == ISD::VECREDUCE_AND
21795 ? ISD::VECREDUCE_UMIN : ISD::VECREDUCE_UMAX;
21796 if (!TLI.isOperationLegalOrCustom(Opcode, VT) &&
21797 TLI.isOperationLegalOrCustom(NewOpcode, VT) &&
21798 DAG.ComputeNumSignBits(N0) == VT.getScalarSizeInBits())
21799 return DAG.getNode(NewOpcode, SDLoc(N), N->getValueType(0), N0);
21800 }
21801
21802 return SDValue();
21803}
21804
21805/// Returns a vector_shuffle if it able to transform an AND to a vector_shuffle
21806/// with the destination vector and a zero vector.
21807/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
21808/// vector_shuffle V, Zero, <0, 4, 2, 4>
21809SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
21810 assert(N->getOpcode() == ISD::AND && "Unexpected opcode!")(static_cast <bool> (N->getOpcode() == ISD::AND &&
"Unexpected opcode!") ? void (0) : __assert_fail ("N->getOpcode() == ISD::AND && \"Unexpected opcode!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 21810, __extension__ __PRETTY_FUNCTION__))
;
21811
21812 EVT VT = N->getValueType(0);
21813 SDValue LHS = N->getOperand(0);
21814 SDValue RHS = peekThroughBitcasts(N->getOperand(1));
21815 SDLoc DL(N);
21816
21817 // Make sure we're not running after operation legalization where it
21818 // may have custom lowered the vector shuffles.
21819 if (LegalOperations)
21820 return SDValue();
21821
21822 if (RHS.getOpcode() != ISD::BUILD_VECTOR)
21823 return SDValue();
21824
21825 EVT RVT = RHS.getValueType();
21826 unsigned NumElts = RHS.getNumOperands();
21827
21828 // Attempt to create a valid clear mask, splitting the mask into
21829 // sub elements and checking to see if each is
21830 // all zeros or all ones - suitable for shuffle masking.
21831 auto BuildClearMask = [&](int Split) {
21832 int NumSubElts = NumElts * Split;
21833 int NumSubBits = RVT.getScalarSizeInBits() / Split;
21834
21835 SmallVector<int, 8> Indices;
21836 for (int i = 0; i != NumSubElts; ++i) {
21837 int EltIdx = i / Split;
21838 int SubIdx = i % Split;
21839 SDValue Elt = RHS.getOperand(EltIdx);
21840 // X & undef --> 0 (not undef). So this lane must be converted to choose
21841 // from the zero constant vector (same as if the element had all 0-bits).
21842 if (Elt.isUndef()) {
21843 Indices.push_back(i + NumSubElts);
21844 continue;
21845 }
21846
21847 APInt Bits;
21848 if (isa<ConstantSDNode>(Elt))
21849 Bits = cast<ConstantSDNode>(Elt)->getAPIntValue();
21850 else if (isa<ConstantFPSDNode>(Elt))
21851 Bits = cast<ConstantFPSDNode>(Elt)->getValueAPF().bitcastToAPInt();
21852 else
21853 return SDValue();
21854
21855 // Extract the sub element from the constant bit mask.
21856 if (DAG.getDataLayout().isBigEndian())
21857 Bits = Bits.extractBits(NumSubBits, (Split - SubIdx - 1) * NumSubBits);
21858 else
21859 Bits = Bits.extractBits(NumSubBits, SubIdx * NumSubBits);
21860
21861 if (Bits.isAllOnesValue())
21862 Indices.push_back(i);
21863 else if (Bits == 0)
21864 Indices.push_back(i + NumSubElts);
21865 else
21866 return SDValue();
21867 }
21868
21869 // Let's see if the target supports this vector_shuffle.
21870 EVT ClearSVT = EVT::getIntegerVT(*DAG.getContext(), NumSubBits);
21871 EVT ClearVT = EVT::getVectorVT(*DAG.getContext(), ClearSVT, NumSubElts);
21872 if (!TLI.isVectorClearMaskLegal(Indices, ClearVT))
21873 return SDValue();
21874
21875 SDValue Zero = DAG.getConstant(0, DL, ClearVT);
21876 return DAG.getBitcast(VT, DAG.getVectorShuffle(ClearVT, DL,
21877 DAG.getBitcast(ClearVT, LHS),
21878 Zero, Indices));
21879 };
21880
21881 // Determine maximum split level (byte level masking).
21882 int MaxSplit = 1;
21883 if (RVT.getScalarSizeInBits() % 8 == 0)
21884 MaxSplit = RVT.getScalarSizeInBits() / 8;
21885
21886 for (int Split = 1; Split <= MaxSplit; ++Split)
21887 if (RVT.getScalarSizeInBits() % Split == 0)
21888 if (SDValue S = BuildClearMask(Split))
21889 return S;
21890
21891 return SDValue();
21892}
21893
21894/// If a vector binop is performed on splat values, it may be profitable to
21895/// extract, scalarize, and insert/splat.
21896static SDValue scalarizeBinOpOfSplats(SDNode *N, SelectionDAG &DAG) {
21897 SDValue N0 = N->getOperand(0);
21898 SDValue N1 = N->getOperand(1);
21899 unsigned Opcode = N->getOpcode();
21900 EVT VT = N->getValueType(0);
21901 EVT EltVT = VT.getVectorElementType();
21902 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
21903
21904 // TODO: Remove/replace the extract cost check? If the elements are available
21905 // as scalars, then there may be no extract cost. Should we ask if
21906 // inserting a scalar back into a vector is cheap instead?
21907 int Index0, Index1;
21908 SDValue Src0 = DAG.getSplatSourceVector(N0, Index0);
21909 SDValue Src1 = DAG.getSplatSourceVector(N1, Index1);
21910 if (!Src0 || !Src1 || Index0 != Index1 ||
21911 Src0.getValueType().getVectorElementType() != EltVT ||
21912 Src1.getValueType().getVectorElementType() != EltVT ||
21913 !TLI.isExtractVecEltCheap(VT, Index0) ||
21914 !TLI.isOperationLegalOrCustom(Opcode, EltVT))
21915 return SDValue();
21916
21917 SDLoc DL(N);
21918 SDValue IndexC = DAG.getVectorIdxConstant(Index0, DL);
21919 SDValue X = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Src0, IndexC);
21920 SDValue Y = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Src1, IndexC);
21921 SDValue ScalarBO = DAG.getNode(Opcode, DL, EltVT, X, Y, N->getFlags());
21922
21923 // If all lanes but 1 are undefined, no need to splat the scalar result.
21924 // TODO: Keep track of undefs and use that info in the general case.
21925 if (N0.getOpcode() == ISD::BUILD_VECTOR && N0.getOpcode() == N1.getOpcode() &&
21926 count_if(N0->ops(), [](SDValue V) { return !V.isUndef(); }) == 1 &&
21927 count_if(N1->ops(), [](SDValue V) { return !V.isUndef(); }) == 1) {
21928 // bo (build_vec ..undef, X, undef...), (build_vec ..undef, Y, undef...) -->
21929 // build_vec ..undef, (bo X, Y), undef...
21930 SmallVector<SDValue, 8> Ops(VT.getVectorNumElements(), DAG.getUNDEF(EltVT));
21931 Ops[Index0] = ScalarBO;
21932 return DAG.getBuildVector(VT, DL, Ops);
21933 }
21934
21935 // bo (splat X, Index), (splat Y, Index) --> splat (bo X, Y), Index
21936 SmallVector<SDValue, 8> Ops(VT.getVectorNumElements(), ScalarBO);
21937 return DAG.getBuildVector(VT, DL, Ops);
21938}
21939
21940/// Visit a binary vector operation, like ADD.
21941SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
21942 assert(N->getValueType(0).isVector() &&(static_cast <bool> (N->getValueType(0).isVector() &&
"SimplifyVBinOp only works on vectors!") ? void (0) : __assert_fail
("N->getValueType(0).isVector() && \"SimplifyVBinOp only works on vectors!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 21943, __extension__ __PRETTY_FUNCTION__))
21943 "SimplifyVBinOp only works on vectors!")(static_cast <bool> (N->getValueType(0).isVector() &&
"SimplifyVBinOp only works on vectors!") ? void (0) : __assert_fail
("N->getValueType(0).isVector() && \"SimplifyVBinOp only works on vectors!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 21943, __extension__ __PRETTY_FUNCTION__))
;
21944
21945 SDValue LHS = N->getOperand(0);
21946 SDValue RHS = N->getOperand(1);
21947 SDValue Ops[] = {LHS, RHS};
21948 EVT VT = N->getValueType(0);
21949 unsigned Opcode = N->getOpcode();
21950 SDNodeFlags Flags = N->getFlags();
21951
21952 // See if we can constant fold the vector operation.
21953 if (SDValue Fold = DAG.FoldConstantVectorArithmetic(
21954 Opcode, SDLoc(LHS), LHS.getValueType(), Ops, N->getFlags()))
21955 return Fold;
21956
21957 // Move unary shuffles with identical masks after a vector binop:
21958 // VBinOp (shuffle A, Undef, Mask), (shuffle B, Undef, Mask))
21959 // --> shuffle (VBinOp A, B), Undef, Mask
21960 // This does not require type legality checks because we are creating the
21961 // same types of operations that are in the original sequence. We do have to
21962 // restrict ops like integer div that have immediate UB (eg, div-by-zero)
21963 // though. This code is adapted from the identical transform in instcombine.
21964 if (Opcode != ISD::UDIV && Opcode != ISD::SDIV &&
21965 Opcode != ISD::UREM && Opcode != ISD::SREM &&
21966 Opcode != ISD::UDIVREM && Opcode != ISD::SDIVREM) {
21967 auto *Shuf0 = dyn_cast<ShuffleVectorSDNode>(LHS);
21968 auto *Shuf1 = dyn_cast<ShuffleVectorSDNode>(RHS);
21969 if (Shuf0 && Shuf1 && Shuf0->getMask().equals(Shuf1->getMask()) &&
21970 LHS.getOperand(1).isUndef() && RHS.getOperand(1).isUndef() &&
21971 (LHS.hasOneUse() || RHS.hasOneUse() || LHS == RHS)) {
21972 SDLoc DL(N);
21973 SDValue NewBinOp = DAG.getNode(Opcode, DL, VT, LHS.getOperand(0),
21974 RHS.getOperand(0), Flags);
21975 SDValue UndefV = LHS.getOperand(1);
21976 return DAG.getVectorShuffle(VT, DL, NewBinOp, UndefV, Shuf0->getMask());
21977 }
21978
21979 // Try to sink a splat shuffle after a binop with a uniform constant.
21980 // This is limited to cases where neither the shuffle nor the constant have
21981 // undefined elements because that could be poison-unsafe or inhibit
21982 // demanded elements analysis. It is further limited to not change a splat
21983 // of an inserted scalar because that may be optimized better by
21984 // load-folding or other target-specific behaviors.
21985 if (isConstOrConstSplat(RHS) && Shuf0 && is_splat(Shuf0->getMask()) &&
21986 Shuf0->hasOneUse() && Shuf0->getOperand(1).isUndef() &&
21987 Shuf0->getOperand(0).getOpcode() != ISD::INSERT_VECTOR_ELT) {
21988 // binop (splat X), (splat C) --> splat (binop X, C)
21989 SDLoc DL(N);
21990 SDValue X = Shuf0->getOperand(0);
21991 SDValue NewBinOp = DAG.getNode(Opcode, DL, VT, X, RHS, Flags);
21992 return DAG.getVectorShuffle(VT, DL, NewBinOp, DAG.getUNDEF(VT),
21993 Shuf0->getMask());
21994 }
21995 if (isConstOrConstSplat(LHS) && Shuf1 && is_splat(Shuf1->getMask()) &&
21996 Shuf1->hasOneUse() && Shuf1->getOperand(1).isUndef() &&
21997 Shuf1->getOperand(0).getOpcode() != ISD::INSERT_VECTOR_ELT) {
21998 // binop (splat C), (splat X) --> splat (binop C, X)
21999 SDLoc DL(N);
22000 SDValue X = Shuf1->getOperand(0);
22001 SDValue NewBinOp = DAG.getNode(Opcode, DL, VT, LHS, X, Flags);
22002 return DAG.getVectorShuffle(VT, DL, NewBinOp, DAG.getUNDEF(VT),
22003 Shuf1->getMask());
22004 }
22005 }
22006
22007 // The following pattern is likely to emerge with vector reduction ops. Moving
22008 // the binary operation ahead of insertion may allow using a narrower vector
22009 // instruction that has better performance than the wide version of the op:
22010 // VBinOp (ins undef, X, Z), (ins undef, Y, Z) --> ins VecC, (VBinOp X, Y), Z
22011 if (LHS.getOpcode() == ISD::INSERT_SUBVECTOR && LHS.getOperand(0).isUndef() &&
22012 RHS.getOpcode() == ISD::INSERT_SUBVECTOR && RHS.getOperand(0).isUndef() &&
22013 LHS.getOperand(2) == RHS.getOperand(2) &&
22014 (LHS.hasOneUse() || RHS.hasOneUse())) {
22015 SDValue X = LHS.getOperand(1);
22016 SDValue Y = RHS.getOperand(1);
22017 SDValue Z = LHS.getOperand(2);
22018 EVT NarrowVT = X.getValueType();
22019 if (NarrowVT == Y.getValueType() &&
22020 TLI.isOperationLegalOrCustomOrPromote(Opcode, NarrowVT,
22021 LegalOperations)) {
22022 // (binop undef, undef) may not return undef, so compute that result.
22023 SDLoc DL(N);
22024 SDValue VecC =
22025 DAG.getNode(Opcode, DL, VT, DAG.getUNDEF(VT), DAG.getUNDEF(VT));
22026 SDValue NarrowBO = DAG.getNode(Opcode, DL, NarrowVT, X, Y);
22027 return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VT, VecC, NarrowBO, Z);
22028 }
22029 }
22030
22031 // Make sure all but the first op are undef or constant.
22032 auto ConcatWithConstantOrUndef = [](SDValue Concat) {
22033 return Concat.getOpcode() == ISD::CONCAT_VECTORS &&
22034 all_of(drop_begin(Concat->ops()), [](const SDValue &Op) {
22035 return Op.isUndef() ||
22036 ISD::isBuildVectorOfConstantSDNodes(Op.getNode());
22037 });
22038 };
22039
22040 // The following pattern is likely to emerge with vector reduction ops. Moving
22041 // the binary operation ahead of the concat may allow using a narrower vector
22042 // instruction that has better performance than the wide version of the op:
22043 // VBinOp (concat X, undef/constant), (concat Y, undef/constant) -->
22044 // concat (VBinOp X, Y), VecC
22045 if (ConcatWithConstantOrUndef(LHS) && ConcatWithConstantOrUndef(RHS) &&
22046 (LHS.hasOneUse() || RHS.hasOneUse())) {
22047 EVT NarrowVT = LHS.getOperand(0).getValueType();
22048 if (NarrowVT == RHS.getOperand(0).getValueType() &&
22049 TLI.isOperationLegalOrCustomOrPromote(Opcode, NarrowVT)) {
22050 SDLoc DL(N);
22051 unsigned NumOperands = LHS.getNumOperands();
22052 SmallVector<SDValue, 4> ConcatOps;
22053 for (unsigned i = 0; i != NumOperands; ++i) {
22054 // This constant fold for operands 1 and up.
22055 ConcatOps.push_back(DAG.getNode(Opcode, DL, NarrowVT, LHS.getOperand(i),
22056 RHS.getOperand(i)));
22057 }
22058
22059 return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, ConcatOps);
22060 }
22061 }
22062
22063 if (SDValue V = scalarizeBinOpOfSplats(N, DAG))
22064 return V;
22065
22066 return SDValue();
22067}
22068
22069SDValue DAGCombiner::SimplifySelect(const SDLoc &DL, SDValue N0, SDValue N1,
22070 SDValue N2) {
22071 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!")(static_cast <bool> (N0.getOpcode() ==ISD::SETCC &&
"First argument must be a SetCC node!") ? void (0) : __assert_fail
("N0.getOpcode() ==ISD::SETCC && \"First argument must be a SetCC node!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 22071, __extension__ __PRETTY_FUNCTION__))
;
22072
22073 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
22074 cast<CondCodeSDNode>(N0.getOperand(2))->get());
22075
22076 // If we got a simplified select_cc node back from SimplifySelectCC, then
22077 // break it down into a new SETCC node, and a new SELECT node, and then return
22078 // the SELECT node, since we were called with a SELECT node.
22079 if (SCC.getNode()) {
22080 // Check to see if we got a select_cc back (to turn into setcc/select).
22081 // Otherwise, just return whatever node we got back, like fabs.
22082 if (SCC.getOpcode() == ISD::SELECT_CC) {
22083 const SDNodeFlags Flags = N0.getNode()->getFlags();
22084 SDValue SETCC = DAG.getNode(ISD::SETCC, SDLoc(N0),
22085 N0.getValueType(),
22086 SCC.getOperand(0), SCC.getOperand(1),
22087 SCC.getOperand(4), Flags);
22088 AddToWorklist(SETCC.getNode());
22089 SDValue SelectNode = DAG.getSelect(SDLoc(SCC), SCC.getValueType(), SETCC,
22090 SCC.getOperand(2), SCC.getOperand(3));
22091 SelectNode->setFlags(Flags);
22092 return SelectNode;
22093 }
22094
22095 return SCC;
22096 }
22097 return SDValue();
22098}
22099
22100/// Given a SELECT or a SELECT_CC node, where LHS and RHS are the two values
22101/// being selected between, see if we can simplify the select. Callers of this
22102/// should assume that TheSelect is deleted if this returns true. As such, they
22103/// should return the appropriate thing (e.g. the node) back to the top-level of
22104/// the DAG combiner loop to avoid it being looked at.
22105bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
22106 SDValue RHS) {
22107 // fold (select (setcc x, [+-]0.0, *lt), NaN, (fsqrt x))
22108 // The select + setcc is redundant, because fsqrt returns NaN for X < 0.
22109 if (const ConstantFPSDNode *NaN = isConstOrConstSplatFP(LHS)) {
22110 if (NaN->isNaN() && RHS.getOpcode() == ISD::FSQRT) {
22111 // We have: (select (setcc ?, ?, ?), NaN, (fsqrt ?))
22112 SDValue Sqrt = RHS;
22113 ISD::CondCode CC;
22114 SDValue CmpLHS;
22115 const ConstantFPSDNode *Zero = nullptr;
22116
22117 if (TheSelect->getOpcode() == ISD::SELECT_CC) {
22118 CC = cast<CondCodeSDNode>(TheSelect->getOperand(4))->get();
22119 CmpLHS = TheSelect->getOperand(0);
22120 Zero = isConstOrConstSplatFP(TheSelect->getOperand(1));
22121 } else {
22122 // SELECT or VSELECT
22123 SDValue Cmp = TheSelect->getOperand(0);
22124 if (Cmp.getOpcode() == ISD::SETCC) {
22125 CC = cast<CondCodeSDNode>(Cmp.getOperand(2))->get();
22126 CmpLHS = Cmp.getOperand(0);
22127 Zero = isConstOrConstSplatFP(Cmp.getOperand(1));
22128 }
22129 }
22130 if (Zero && Zero->isZero() &&
22131 Sqrt.getOperand(0) == CmpLHS && (CC == ISD::SETOLT ||
22132 CC == ISD::SETULT || CC == ISD::SETLT)) {
22133 // We have: (select (setcc x, [+-]0.0, *lt), NaN, (fsqrt x))
22134 CombineTo(TheSelect, Sqrt);
22135 return true;
22136 }
22137 }
22138 }
22139 // Cannot simplify select with vector condition
22140 if (TheSelect->getOperand(0).getValueType().isVector()) return false;
22141
22142 // If this is a select from two identical things, try to pull the operation
22143 // through the select.
22144 if (LHS.getOpcode() != RHS.getOpcode() ||
22145 !LHS.hasOneUse() || !RHS.hasOneUse())
22146 return false;
22147
22148 // If this is a load and the token chain is identical, replace the select
22149 // of two loads with a load through a select of the address to load from.
22150 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
22151 // constants have been dropped into the constant pool.
22152 if (LHS.getOpcode() == ISD::LOAD) {
22153 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
22154 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
22155
22156 // Token chains must be identical.
22157 if (LHS.getOperand(0) != RHS.getOperand(0) ||
22158 // Do not let this transformation reduce the number of volatile loads.
22159 // Be conservative for atomics for the moment
22160 // TODO: This does appear to be legal for unordered atomics (see D66309)
22161 !LLD->isSimple() || !RLD->isSimple() ||
22162 // FIXME: If either is a pre/post inc/dec load,
22163 // we'd need to split out the address adjustment.
22164 LLD->isIndexed() || RLD->isIndexed() ||
22165 // If this is an EXTLOAD, the VT's must match.
22166 LLD->getMemoryVT() != RLD->getMemoryVT() ||
22167 // If this is an EXTLOAD, the kind of extension must match.
22168 (LLD->getExtensionType() != RLD->getExtensionType() &&
22169 // The only exception is if one of the extensions is anyext.
22170 LLD->getExtensionType() != ISD::EXTLOAD &&
22171 RLD->getExtensionType() != ISD::EXTLOAD) ||
22172 // FIXME: this discards src value information. This is
22173 // over-conservative. It would be beneficial to be able to remember
22174 // both potential memory locations. Since we are discarding
22175 // src value info, don't do the transformation if the memory
22176 // locations are not in the default address space.
22177 LLD->getPointerInfo().getAddrSpace() != 0 ||
22178 RLD->getPointerInfo().getAddrSpace() != 0 ||
22179 // We can't produce a CMOV of a TargetFrameIndex since we won't
22180 // generate the address generation required.
22181 LLD->getBasePtr().getOpcode() == ISD::TargetFrameIndex ||
22182 RLD->getBasePtr().getOpcode() == ISD::TargetFrameIndex ||
22183 !TLI.isOperationLegalOrCustom(TheSelect->getOpcode(),
22184 LLD->getBasePtr().getValueType()))
22185 return false;
22186
22187 // The loads must not depend on one another.
22188 if (LLD->isPredecessorOf(RLD) || RLD->isPredecessorOf(LLD))
22189 return false;
22190
22191 // Check that the select condition doesn't reach either load. If so,
22192 // folding this will induce a cycle into the DAG. If not, this is safe to
22193 // xform, so create a select of the addresses.
22194
22195 SmallPtrSet<const SDNode *, 32> Visited;
22196 SmallVector<const SDNode *, 16> Worklist;
22197
22198 // Always fail if LLD and RLD are not independent. TheSelect is a
22199 // predecessor to all Nodes in question so we need not search past it.
22200
22201 Visited.insert(TheSelect);
22202 Worklist.push_back(LLD);
22203 Worklist.push_back(RLD);
22204
22205 if (SDNode::hasPredecessorHelper(LLD, Visited, Worklist) ||
22206 SDNode::hasPredecessorHelper(RLD, Visited, Worklist))
22207 return false;
22208
22209 SDValue Addr;
22210 if (TheSelect->getOpcode() == ISD::SELECT) {
22211 // We cannot do this optimization if any pair of {RLD, LLD} is a
22212 // predecessor to {RLD, LLD, CondNode}. As we've already compared the
22213 // Loads, we only need to check if CondNode is a successor to one of the
22214 // loads. We can further avoid this if there's no use of their chain
22215 // value.
22216 SDNode *CondNode = TheSelect->getOperand(0).getNode();
22217 Worklist.push_back(CondNode);
22218
22219 if ((LLD->hasAnyUseOfValue(1) &&
22220 SDNode::hasPredecessorHelper(LLD, Visited, Worklist)) ||
22221 (RLD->hasAnyUseOfValue(1) &&
22222 SDNode::hasPredecessorHelper(RLD, Visited, Worklist)))
22223 return false;
22224
22225 Addr = DAG.getSelect(SDLoc(TheSelect),
22226 LLD->getBasePtr().getValueType(),
22227 TheSelect->getOperand(0), LLD->getBasePtr(),
22228 RLD->getBasePtr());
22229 } else { // Otherwise SELECT_CC
22230 // We cannot do this optimization if any pair of {RLD, LLD} is a
22231 // predecessor to {RLD, LLD, CondLHS, CondRHS}. As we've already compared
22232 // the Loads, we only need to check if CondLHS/CondRHS is a successor to
22233 // one of the loads. We can further avoid this if there's no use of their
22234 // chain value.
22235
22236 SDNode *CondLHS = TheSelect->getOperand(0).getNode();
22237 SDNode *CondRHS = TheSelect->getOperand(1).getNode();
22238 Worklist.push_back(CondLHS);
22239 Worklist.push_back(CondRHS);
22240
22241 if ((LLD->hasAnyUseOfValue(1) &&
22242 SDNode::hasPredecessorHelper(LLD, Visited, Worklist)) ||
22243 (RLD->hasAnyUseOfValue(1) &&
22244 SDNode::hasPredecessorHelper(RLD, Visited, Worklist)))
22245 return false;
22246
22247 Addr = DAG.getNode(ISD::SELECT_CC, SDLoc(TheSelect),
22248 LLD->getBasePtr().getValueType(),
22249 TheSelect->getOperand(0),
22250 TheSelect->getOperand(1),
22251 LLD->getBasePtr(), RLD->getBasePtr(),
22252 TheSelect->getOperand(4));
22253 }
22254
22255 SDValue Load;
22256 // It is safe to replace the two loads if they have different alignments,
22257 // but the new load must be the minimum (most restrictive) alignment of the
22258 // inputs.
22259 Align Alignment = std::min(LLD->getAlign(), RLD->getAlign());
22260 MachineMemOperand::Flags MMOFlags = LLD->getMemOperand()->getFlags();
22261 if (!RLD->isInvariant())
22262 MMOFlags &= ~MachineMemOperand::MOInvariant;
22263 if (!RLD->isDereferenceable())
22264 MMOFlags &= ~MachineMemOperand::MODereferenceable;
22265 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
22266 // FIXME: Discards pointer and AA info.
22267 Load = DAG.getLoad(TheSelect->getValueType(0), SDLoc(TheSelect),
22268 LLD->getChain(), Addr, MachinePointerInfo(), Alignment,
22269 MMOFlags);
22270 } else {
22271 // FIXME: Discards pointer and AA info.
22272 Load = DAG.getExtLoad(
22273 LLD->getExtensionType() == ISD::EXTLOAD ? RLD->getExtensionType()
22274 : LLD->getExtensionType(),
22275 SDLoc(TheSelect), TheSelect->getValueType(0), LLD->getChain(), Addr,
22276 MachinePointerInfo(), LLD->getMemoryVT(), Alignment, MMOFlags);
22277 }
22278
22279 // Users of the select now use the result of the load.
22280 CombineTo(TheSelect, Load);
22281
22282 // Users of the old loads now use the new load's chain. We know the
22283 // old-load value is dead now.
22284 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
22285 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
22286 return true;
22287 }
22288
22289 return false;
22290}
22291
22292/// Try to fold an expression of the form (N0 cond N1) ? N2 : N3 to a shift and
22293/// bitwise 'and'.
22294SDValue DAGCombiner::foldSelectCCToShiftAnd(const SDLoc &DL, SDValue N0,
22295 SDValue N1, SDValue N2, SDValue N3,
22296 ISD::CondCode CC) {
22297 // If this is a select where the false operand is zero and the compare is a
22298 // check of the sign bit, see if we can perform the "gzip trick":
22299 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
22300 // select_cc setgt X, 0, A, 0 -> and (not (sra X, size(X)-1)), A
22301 EVT XType = N0.getValueType();
22302 EVT AType = N2.getValueType();
22303 if (!isNullConstant(N3) || !XType.bitsGE(AType))
22304 return SDValue();
22305
22306 // If the comparison is testing for a positive value, we have to invert
22307 // the sign bit mask, so only do that transform if the target has a bitwise
22308 // 'and not' instruction (the invert is free).
22309 if (CC == ISD::SETGT && TLI.hasAndNot(N2)) {
22310 // (X > -1) ? A : 0
22311 // (X > 0) ? X : 0 <-- This is canonical signed max.
22312 if (!(isAllOnesConstant(N1) || (isNullConstant(N1) && N0 == N2)))
22313 return SDValue();
22314 } else if (CC == ISD::SETLT) {
22315 // (X < 0) ? A : 0
22316 // (X < 1) ? X : 0 <-- This is un-canonicalized signed min.
22317 if (!(isNullConstant(N1) || (isOneConstant(N1) && N0 == N2)))
22318 return SDValue();
22319 } else {
22320 return SDValue();
22321 }
22322
22323 // and (sra X, size(X)-1), A -> "and (srl X, C2), A" iff A is a single-bit
22324 // constant.
22325 EVT ShiftAmtTy = getShiftAmountTy(N0.getValueType());
22326 auto *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
22327 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue() - 1)) == 0)) {
22328 unsigned ShCt = XType.getSizeInBits() - N2C->getAPIntValue().logBase2() - 1;
22329 if (!TLI.shouldAvoidTransformToShift(XType, ShCt)) {
22330 SDValue ShiftAmt = DAG.getConstant(ShCt, DL, ShiftAmtTy);
22331 SDValue Shift = DAG.getNode(ISD::SRL, DL, XType, N0, ShiftAmt);
22332 AddToWorklist(Shift.getNode());
22333
22334 if (XType.bitsGT(AType)) {
22335 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
22336 AddToWorklist(Shift.getNode());
22337 }
22338
22339 if (CC == ISD::SETGT)
22340 Shift = DAG.getNOT(DL, Shift, AType);
22341
22342 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
22343 }
22344 }
22345
22346 unsigned ShCt = XType.getSizeInBits() - 1;
22347 if (TLI.shouldAvoidTransformToShift(XType, ShCt))
22348 return SDValue();
22349
22350 SDValue ShiftAmt = DAG.getConstant(ShCt, DL, ShiftAmtTy);
22351 SDValue Shift = DAG.getNode(ISD::SRA, DL, XType, N0, ShiftAmt);
22352 AddToWorklist(Shift.getNode());
22353
22354 if (XType.bitsGT(AType)) {
22355 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
22356 AddToWorklist(Shift.getNode());
22357 }
22358
22359 if (CC == ISD::SETGT)
22360 Shift = DAG.getNOT(DL, Shift, AType);
22361
22362 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
22363}
22364
22365// Fold select(cc, binop(), binop()) -> binop(select(), select()) etc.
22366SDValue DAGCombiner::foldSelectOfBinops(SDNode *N) {
22367 SDValue N0 = N->getOperand(0);
22368 SDValue N1 = N->getOperand(1);
22369 SDValue N2 = N->getOperand(2);
22370 EVT VT = N->getValueType(0);
22371 SDLoc DL(N);
22372
22373 unsigned BinOpc = N1.getOpcode();
22374 if (!TLI.isBinOp(BinOpc) || (N2.getOpcode() != BinOpc))
22375 return SDValue();
22376
22377 if (!N->isOnlyUserOf(N0.getNode()) || !N->isOnlyUserOf(N1.getNode()))
22378 return SDValue();
22379
22380 // Fold select(cond, binop(x, y), binop(z, y))
22381 // --> binop(select(cond, x, z), y)
22382 if (N1.getOperand(1) == N2.getOperand(1)) {
22383 SDValue NewSel =
22384 DAG.getSelect(DL, VT, N0, N1.getOperand(0), N2.getOperand(0));
22385 SDValue NewBinOp = DAG.getNode(BinOpc, DL, VT, NewSel, N1.getOperand(1));
22386 NewBinOp->setFlags(N1->getFlags());
22387 NewBinOp->intersectFlagsWith(N2->getFlags());
22388 return NewBinOp;
22389 }
22390
22391 // Fold select(cond, binop(x, y), binop(x, z))
22392 // --> binop(x, select(cond, y, z))
22393 // Second op VT might be different (e.g. shift amount type)
22394 if (N1.getOperand(0) == N2.getOperand(0) &&
22395 VT == N1.getOperand(1).getValueType() &&
22396 VT == N2.getOperand(1).getValueType()) {
22397 SDValue NewSel =
22398 DAG.getSelect(DL, VT, N0, N1.getOperand(1), N2.getOperand(1));
22399 SDValue NewBinOp = DAG.getNode(BinOpc, DL, VT, N1.getOperand(0), NewSel);
22400 NewBinOp->setFlags(N1->getFlags());
22401 NewBinOp->intersectFlagsWith(N2->getFlags());
22402 return NewBinOp;
22403 }
22404
22405 // TODO: Handle isCommutativeBinOp patterns as well?
22406 return SDValue();
22407}
22408
22409// Transform (fneg/fabs (bitconvert x)) to avoid loading constant pool values.
22410SDValue DAGCombiner::foldSignChangeInBitcast(SDNode *N) {
22411 SDValue N0 = N->getOperand(0);
22412 EVT VT = N->getValueType(0);
22413 bool IsFabs = N->getOpcode() == ISD::FABS;
22414 bool IsFree = IsFabs ? TLI.isFAbsFree(VT) : TLI.isFNegFree(VT);
22415
22416 if (IsFree || N0.getOpcode() != ISD::BITCAST || !N0.hasOneUse())
22417 return SDValue();
22418
22419 SDValue Int = N0.getOperand(0);
22420 EVT IntVT = Int.getValueType();
22421
22422 // The operand to cast should be integer.
22423 if (!IntVT.isInteger() || IntVT.isVector())
22424 return SDValue();
22425
22426 // (fneg (bitconvert x)) -> (bitconvert (xor x sign))
22427 // (fabs (bitconvert x)) -> (bitconvert (and x ~sign))
22428 APInt SignMask;
22429 if (N0.getValueType().isVector()) {
22430 // For vector, create a sign mask (0x80...) or its inverse (for fabs,
22431 // 0x7f...) per element and splat it.
22432 SignMask = APInt::getSignMask(N0.getScalarValueSizeInBits());
22433 if (IsFabs)
22434 SignMask = ~SignMask;
22435 SignMask = APInt::getSplat(IntVT.getSizeInBits(), SignMask);
22436 } else {
22437 // For scalar, just use the sign mask (0x80... or the inverse, 0x7f...)
22438 SignMask = APInt::getSignMask(IntVT.getSizeInBits());
22439 if (IsFabs)
22440 SignMask = ~SignMask;
22441 }
22442 SDLoc DL(N0);
22443 Int = DAG.getNode(IsFabs ? ISD::AND : ISD::XOR, DL, IntVT, Int,
22444 DAG.getConstant(SignMask, DL, IntVT));
22445 AddToWorklist(Int.getNode());
22446 return DAG.getBitcast(VT, Int);
22447}
22448
22449/// Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
22450/// where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
22451/// in it. This may be a win when the constant is not otherwise available
22452/// because it replaces two constant pool loads with one.
22453SDValue DAGCombiner::convertSelectOfFPConstantsToLoadOffset(
22454 const SDLoc &DL, SDValue N0, SDValue N1, SDValue N2, SDValue N3,
22455 ISD::CondCode CC) {
22456 if (!TLI.reduceSelectOfFPConstantLoads(N0.getValueType()))
22457 return SDValue();
22458
22459 // If we are before legalize types, we want the other legalization to happen
22460 // first (for example, to avoid messing with soft float).
22461 auto *TV = dyn_cast<ConstantFPSDNode>(N2);
22462 auto *FV = dyn_cast<ConstantFPSDNode>(N3);
22463 EVT VT = N2.getValueType();
22464 if (!TV || !FV || !TLI.isTypeLegal(VT))
22465 return SDValue();
22466
22467 // If a constant can be materialized without loads, this does not make sense.
22468 if (TLI.getOperationAction(ISD::ConstantFP, VT) == TargetLowering::Legal ||
22469 TLI.isFPImmLegal(TV->getValueAPF(), TV->getValueType(0), ForCodeSize) ||
22470 TLI.isFPImmLegal(FV->getValueAPF(), FV->getValueType(0), ForCodeSize))
22471 return SDValue();
22472
22473 // If both constants have multiple uses, then we won't need to do an extra
22474 // load. The values are likely around in registers for other users.
22475 if (!TV->hasOneUse() && !FV->hasOneUse())
22476 return SDValue();
22477
22478 Constant *Elts[] = { const_cast<ConstantFP*>(FV->getConstantFPValue()),
22479 const_cast<ConstantFP*>(TV->getConstantFPValue()) };
22480 Type *FPTy = Elts[0]->getType();
22481 const DataLayout &TD = DAG.getDataLayout();
22482
22483 // Create a ConstantArray of the two constants.
22484 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts);
22485 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(DAG.getDataLayout()),
22486 TD.getPrefTypeAlign(FPTy));
22487 Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign();
22488
22489 // Get offsets to the 0 and 1 elements of the array, so we can select between
22490 // them.
22491 SDValue Zero = DAG.getIntPtrConstant(0, DL);
22492 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
22493 SDValue One = DAG.getIntPtrConstant(EltSize, SDLoc(FV));
22494 SDValue Cond =
22495 DAG.getSetCC(DL, getSetCCResultType(N0.getValueType()), N0, N1, CC);
22496 AddToWorklist(Cond.getNode());
22497 SDValue CstOffset = DAG.getSelect(DL, Zero.getValueType(), Cond, One, Zero);
22498 AddToWorklist(CstOffset.getNode());
22499 CPIdx = DAG.getNode(ISD::ADD, DL, CPIdx.getValueType(), CPIdx, CstOffset);
22500 AddToWorklist(CPIdx.getNode());
22501 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
22502 MachinePointerInfo::getConstantPool(
22503 DAG.getMachineFunction()), Alignment);
22504}
22505
22506/// Simplify an expression of the form (N0 cond N1) ? N2 : N3
22507/// where 'cond' is the comparison specified by CC.
22508SDValue DAGCombiner::SimplifySelectCC(const SDLoc &DL, SDValue N0, SDValue N1,
22509 SDValue N2, SDValue N3, ISD::CondCode CC,
22510 bool NotExtCompare) {
22511 // (x ? y : y) -> y.
22512 if (N2 == N3) return N2;
22513
22514 EVT CmpOpVT = N0.getValueType();
22515 EVT CmpResVT = getSetCCResultType(CmpOpVT);
22516 EVT VT = N2.getValueType();
22517 auto *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
22518 auto *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
22519 auto *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
22520
22521 // Determine if the condition we're dealing with is constant.
22522 if (SDValue SCC = DAG.FoldSetCC(CmpResVT, N0, N1, CC, DL)) {
22523 AddToWorklist(SCC.getNode());
22524 if (auto *SCCC = dyn_cast<ConstantSDNode>(SCC)) {
22525 // fold select_cc true, x, y -> x
22526 // fold select_cc false, x, y -> y
22527 return !(SCCC->isNullValue()) ? N2 : N3;
22528 }
22529 }
22530
22531 if (SDValue V =
22532 convertSelectOfFPConstantsToLoadOffset(DL, N0, N1, N2, N3, CC))
22533 return V;
22534
22535 if (SDValue V = foldSelectCCToShiftAnd(DL, N0, N1, N2, N3, CC))
22536 return V;
22537
22538 // fold (select_cc seteq (and x, y), 0, 0, A) -> (and (shr (shl x)) A)
22539 // where y is has a single bit set.
22540 // A plaintext description would be, we can turn the SELECT_CC into an AND
22541 // when the condition can be materialized as an all-ones register. Any
22542 // single bit-test can be materialized as an all-ones register with
22543 // shift-left and shift-right-arith.
22544 if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND &&
22545 N0->getValueType(0) == VT && isNullConstant(N1) && isNullConstant(N2)) {
22546 SDValue AndLHS = N0->getOperand(0);
22547 auto *ConstAndRHS = dyn_cast<ConstantSDNode>(N0->getOperand(1));
22548 if (ConstAndRHS && ConstAndRHS->getAPIntValue().countPopulation() == 1) {
22549 // Shift the tested bit over the sign bit.
22550 const APInt &AndMask = ConstAndRHS->getAPIntValue();
22551 unsigned ShCt = AndMask.getBitWidth() - 1;
22552 if (!TLI.shouldAvoidTransformToShift(VT, ShCt)) {
22553 SDValue ShlAmt =
22554 DAG.getConstant(AndMask.countLeadingZeros(), SDLoc(AndLHS),
22555 getShiftAmountTy(AndLHS.getValueType()));
22556 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(N0), VT, AndLHS, ShlAmt);
22557
22558 // Now arithmetic right shift it all the way over, so the result is
22559 // either all-ones, or zero.
22560 SDValue ShrAmt =
22561 DAG.getConstant(ShCt, SDLoc(Shl),
22562 getShiftAmountTy(Shl.getValueType()));
22563 SDValue Shr = DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl, ShrAmt);
22564
22565 return DAG.getNode(ISD::AND, DL, VT, Shr, N3);
22566 }
22567 }
22568 }
22569
22570 // fold select C, 16, 0 -> shl C, 4
22571 bool Fold = N2C && isNullConstant(N3) && N2C->getAPIntValue().isPowerOf2();
22572 bool Swap = N3C && isNullConstant(N2) && N3C->getAPIntValue().isPowerOf2();
22573
22574 if ((Fold || Swap) &&
22575 TLI.getBooleanContents(CmpOpVT) ==
22576 TargetLowering::ZeroOrOneBooleanContent &&
22577 (!LegalOperations || TLI.isOperationLegal(ISD::SETCC, CmpOpVT))) {
22578
22579 if (Swap) {
22580 CC = ISD::getSetCCInverse(CC, CmpOpVT);
22581 std::swap(N2C, N3C);
22582 }
22583
22584 // If the caller doesn't want us to simplify this into a zext of a compare,
22585 // don't do it.
22586 if (NotExtCompare && N2C->isOne())
22587 return SDValue();
22588
22589 SDValue Temp, SCC;
22590 // zext (setcc n0, n1)
22591 if (LegalTypes) {
22592 SCC = DAG.getSetCC(DL, CmpResVT, N0, N1, CC);
22593 if (VT.bitsLT(SCC.getValueType()))
22594 Temp = DAG.getZeroExtendInReg(SCC, SDLoc(N2), VT);
22595 else
22596 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2), VT, SCC);
22597 } else {
22598 SCC = DAG.getSetCC(SDLoc(N0), MVT::i1, N0, N1, CC);
22599 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2), VT, SCC);
22600 }
22601
22602 AddToWorklist(SCC.getNode());
22603 AddToWorklist(Temp.getNode());
22604
22605 if (N2C->isOne())
22606 return Temp;
22607
22608 unsigned ShCt = N2C->getAPIntValue().logBase2();
22609 if (TLI.shouldAvoidTransformToShift(VT, ShCt))
22610 return SDValue();
22611
22612 // shl setcc result by log2 n2c
22613 return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp,
22614 DAG.getConstant(ShCt, SDLoc(Temp),
22615 getShiftAmountTy(Temp.getValueType())));
22616 }
22617
22618 // select_cc seteq X, 0, sizeof(X), ctlz(X) -> ctlz(X)
22619 // select_cc seteq X, 0, sizeof(X), ctlz_zero_undef(X) -> ctlz(X)
22620 // select_cc seteq X, 0, sizeof(X), cttz(X) -> cttz(X)
22621 // select_cc seteq X, 0, sizeof(X), cttz_zero_undef(X) -> cttz(X)
22622 // select_cc setne X, 0, ctlz(X), sizeof(X) -> ctlz(X)
22623 // select_cc setne X, 0, ctlz_zero_undef(X), sizeof(X) -> ctlz(X)
22624 // select_cc setne X, 0, cttz(X), sizeof(X) -> cttz(X)
22625 // select_cc setne X, 0, cttz_zero_undef(X), sizeof(X) -> cttz(X)
22626 if (N1C && N1C->isNullValue() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
22627 SDValue ValueOnZero = N2;
22628 SDValue Count = N3;
22629 // If the condition is NE instead of E, swap the operands.
22630 if (CC == ISD::SETNE)
22631 std::swap(ValueOnZero, Count);
22632 // Check if the value on zero is a constant equal to the bits in the type.
22633 if (auto *ValueOnZeroC = dyn_cast<ConstantSDNode>(ValueOnZero)) {
22634 if (ValueOnZeroC->getAPIntValue() == VT.getSizeInBits()) {
22635 // If the other operand is cttz/cttz_zero_undef of N0, and cttz is
22636 // legal, combine to just cttz.
22637 if ((Count.getOpcode() == ISD::CTTZ ||
22638 Count.getOpcode() == ISD::CTTZ_ZERO_UNDEF) &&
22639 N0 == Count.getOperand(0) &&
22640 (!LegalOperations || TLI.isOperationLegal(ISD::CTTZ, VT)))
22641 return DAG.getNode(ISD::CTTZ, DL, VT, N0);
22642 // If the other operand is ctlz/ctlz_zero_undef of N0, and ctlz is
22643 // legal, combine to just ctlz.
22644 if ((Count.getOpcode() == ISD::CTLZ ||
22645 Count.getOpcode() == ISD::CTLZ_ZERO_UNDEF) &&
22646 N0 == Count.getOperand(0) &&
22647 (!LegalOperations || TLI.isOperationLegal(ISD::CTLZ, VT)))
22648 return DAG.getNode(ISD::CTLZ, DL, VT, N0);
22649 }
22650 }
22651 }
22652
22653 return SDValue();
22654}
22655
22656/// This is a stub for TargetLowering::SimplifySetCC.
22657SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0, SDValue N1,
22658 ISD::CondCode Cond, const SDLoc &DL,
22659 bool foldBooleans) {
22660 TargetLowering::DAGCombinerInfo
22661 DagCombineInfo(DAG, Level, false, this);
22662 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
22663}
22664
22665/// Given an ISD::SDIV node expressing a divide by constant, return
22666/// a DAG expression to select that will generate the same value by multiplying
22667/// by a magic number.
22668/// Ref: "Hacker's Delight" or "The PowerPC Compiler Writer's Guide".
22669SDValue DAGCombiner::BuildSDIV(SDNode *N) {
22670 // when optimising for minimum size, we don't want to expand a div to a mul
22671 // and a shift.
22672 if (DAG.getMachineFunction().getFunction().hasMinSize())
22673 return SDValue();
22674
22675 SmallVector<SDNode *, 8> Built;
22676 if (SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, Built)) {
22677 for (SDNode *N : Built)
22678 AddToWorklist(N);
22679 return S;
22680 }
22681
22682 return SDValue();
22683}
22684
22685/// Given an ISD::SDIV node expressing a divide by constant power of 2, return a
22686/// DAG expression that will generate the same value by right shifting.
22687SDValue DAGCombiner::BuildSDIVPow2(SDNode *N) {
22688 ConstantSDNode *C = isConstOrConstSplat(N->getOperand(1));
22689 if (!C)
22690 return SDValue();
22691
22692 // Avoid division by zero.
22693 if (C->isNullValue())
22694 return SDValue();
22695
22696 SmallVector<SDNode *, 8> Built;
22697 if (SDValue S = TLI.BuildSDIVPow2(N, C->getAPIntValue(), DAG, Built)) {
22698 for (SDNode *N : Built)
22699 AddToWorklist(N);
22700 return S;
22701 }
22702
22703 return SDValue();
22704}
22705
22706/// Given an ISD::UDIV node expressing a divide by constant, return a DAG
22707/// expression that will generate the same value by multiplying by a magic
22708/// number.
22709/// Ref: "Hacker's Delight" or "The PowerPC Compiler Writer's Guide".
22710SDValue DAGCombiner::BuildUDIV(SDNode *N) {
22711 // when optimising for minimum size, we don't want to expand a div to a mul
22712 // and a shift.
22713 if (DAG.getMachineFunction().getFunction().hasMinSize())
22714 return SDValue();
22715
22716 SmallVector<SDNode *, 8> Built;
22717 if (SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, Built)) {
22718 for (SDNode *N : Built)
22719 AddToWorklist(N);
22720 return S;
22721 }
22722
22723 return SDValue();
22724}
22725
22726/// Determines the LogBase2 value for a non-null input value using the
22727/// transform: LogBase2(V) = (EltBits - 1) - ctlz(V).
22728SDValue DAGCombiner::BuildLogBase2(SDValue V, const SDLoc &DL) {
22729 EVT VT = V.getValueType();
22730 SDValue Ctlz = DAG.getNode(ISD::CTLZ, DL, VT, V);
22731 SDValue Base = DAG.getConstant(VT.getScalarSizeInBits() - 1, DL, VT);
22732 SDValue LogBase2 = DAG.getNode(ISD::SUB, DL, VT, Base, Ctlz);
22733 return LogBase2;
22734}
22735
22736/// Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
22737/// For the reciprocal, we need to find the zero of the function:
22738/// F(X) = A X - 1 [which has a zero at X = 1/A]
22739/// =>
22740/// X_{i+1} = X_i (2 - A X_i) = X_i + X_i (1 - A X_i) [this second form
22741/// does not require additional intermediate precision]
22742/// For the last iteration, put numerator N into it to gain more precision:
22743/// Result = N X_i + X_i (N - N A X_i)
22744SDValue DAGCombiner::BuildDivEstimate(SDValue N, SDValue Op,
22745 SDNodeFlags Flags) {
22746 if (LegalDAG)
22747 return SDValue();
22748
22749 // TODO: Handle half and/or extended types?
22750 EVT VT = Op.getValueType();
22751 if (VT.getScalarType() != MVT::f32 && VT.getScalarType() != MVT::f64)
22752 return SDValue();
22753
22754 // If estimates are explicitly disabled for this function, we're done.
22755 MachineFunction &MF = DAG.getMachineFunction();
22756 int Enabled = TLI.getRecipEstimateDivEnabled(VT, MF);
22757 if (Enabled == TLI.ReciprocalEstimate::Disabled)
22758 return SDValue();
22759
22760 // Estimates may be explicitly enabled for this type with a custom number of
22761 // refinement steps.
22762 int Iterations = TLI.getDivRefinementSteps(VT, MF);
22763 if (SDValue Est = TLI.getRecipEstimate(Op, DAG, Enabled, Iterations)) {
22764 AddToWorklist(Est.getNode());
22765
22766 SDLoc DL(Op);
22767 if (Iterations) {
22768 SDValue FPOne = DAG.getConstantFP(1.0, DL, VT);
22769
22770 // Newton iterations: Est = Est + Est (N - Arg * Est)
22771 // If this is the last iteration, also multiply by the numerator.
22772 for (int i = 0; i < Iterations; ++i) {
22773 SDValue MulEst = Est;
22774
22775 if (i == Iterations - 1) {
22776 MulEst = DAG.getNode(ISD::FMUL, DL, VT, N, Est, Flags);
22777 AddToWorklist(MulEst.getNode());
22778 }
22779
22780 SDValue NewEst = DAG.getNode(ISD::FMUL, DL, VT, Op, MulEst, Flags);
22781 AddToWorklist(NewEst.getNode());
22782
22783 NewEst = DAG.getNode(ISD::FSUB, DL, VT,
22784 (i == Iterations - 1 ? N : FPOne), NewEst, Flags);
22785 AddToWorklist(NewEst.getNode());
22786
22787 NewEst = DAG.getNode(ISD::FMUL, DL, VT, Est, NewEst, Flags);
22788 AddToWorklist(NewEst.getNode());
22789
22790 Est = DAG.getNode(ISD::FADD, DL, VT, MulEst, NewEst, Flags);
22791 AddToWorklist(Est.getNode());
22792 }
22793 } else {
22794 // If no iterations are available, multiply with N.
22795 Est = DAG.getNode(ISD::FMUL, DL, VT, Est, N, Flags);
22796 AddToWorklist(Est.getNode());
22797 }
22798
22799 return Est;
22800 }
22801
22802 return SDValue();
22803}
22804
22805/// Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
22806/// For the reciprocal sqrt, we need to find the zero of the function:
22807/// F(X) = 1/X^2 - A [which has a zero at X = 1/sqrt(A)]
22808/// =>
22809/// X_{i+1} = X_i (1.5 - A X_i^2 / 2)
22810/// As a result, we precompute A/2 prior to the iteration loop.
22811SDValue DAGCombiner::buildSqrtNROneConst(SDValue Arg, SDValue Est,
22812 unsigned Iterations,
22813 SDNodeFlags Flags, bool Reciprocal) {
22814 EVT VT = Arg.getValueType();
22815 SDLoc DL(Arg);
22816 SDValue ThreeHalves = DAG.getConstantFP(1.5, DL, VT);
22817
22818 // We now need 0.5 * Arg which we can write as (1.5 * Arg - Arg) so that
22819 // this entire sequence requires only one FP constant.
22820 SDValue HalfArg = DAG.getNode(ISD::FMUL, DL, VT, ThreeHalves, Arg, Flags);
22821 HalfArg = DAG.getNode(ISD::FSUB, DL, VT, HalfArg, Arg, Flags);
22822
22823 // Newton iterations: Est = Est * (1.5 - HalfArg * Est * Est)
22824 for (unsigned i = 0; i < Iterations; ++i) {
22825 SDValue NewEst = DAG.getNode(ISD::FMUL, DL, VT, Est, Est, Flags);
22826 NewEst = DAG.getNode(ISD::FMUL, DL, VT, HalfArg, NewEst, Flags);
22827 NewEst = DAG.getNode(ISD::FSUB, DL, VT, ThreeHalves, NewEst, Flags);
22828 Est = DAG.getNode(ISD::FMUL, DL, VT, Est, NewEst, Flags);
22829 }
22830
22831 // If non-reciprocal square root is requested, multiply the result by Arg.
22832 if (!Reciprocal)
22833 Est = DAG.getNode(ISD::FMUL, DL, VT, Est, Arg, Flags);
22834
22835 return Est;
22836}
22837
22838/// Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
22839/// For the reciprocal sqrt, we need to find the zero of the function:
22840/// F(X) = 1/X^2 - A [which has a zero at X = 1/sqrt(A)]
22841/// =>
22842/// X_{i+1} = (-0.5 * X_i) * (A * X_i * X_i + (-3.0))
22843SDValue DAGCombiner::buildSqrtNRTwoConst(SDValue Arg, SDValue Est,
22844 unsigned Iterations,
22845 SDNodeFlags Flags, bool Reciprocal) {
22846 EVT VT = Arg.getValueType();
22847 SDLoc DL(Arg);
22848 SDValue MinusThree = DAG.getConstantFP(-3.0, DL, VT);
22849 SDValue MinusHalf = DAG.getConstantFP(-0.5, DL, VT);
22850
22851 // This routine must enter the loop below to work correctly
22852 // when (Reciprocal == false).
22853 assert(Iterations > 0)(static_cast <bool> (Iterations > 0) ? void (0) : __assert_fail
("Iterations > 0", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp"
, 22853, __extension__ __PRETTY_FUNCTION__))
;
22854
22855 // Newton iterations for reciprocal square root:
22856 // E = (E * -0.5) * ((A * E) * E + -3.0)
22857 for (unsigned i = 0; i < Iterations; ++i) {
22858 SDValue AE = DAG.getNode(ISD::FMUL, DL, VT, Arg, Est, Flags);
22859 SDValue AEE = DAG.getNode(ISD::FMUL, DL, VT, AE, Est, Flags);
22860 SDValue RHS = DAG.getNode(ISD::FADD, DL, VT, AEE, MinusThree, Flags);
22861
22862 // When calculating a square root at the last iteration build:
22863 // S = ((A * E) * -0.5) * ((A * E) * E + -3.0)
22864 // (notice a common subexpression)
22865 SDValue LHS;
22866 if (Reciprocal || (i + 1) < Iterations) {
22867 // RSQRT: LHS = (E * -0.5)
22868 LHS = DAG.getNode(ISD::FMUL, DL, VT, Est, MinusHalf, Flags);
22869 } else {
22870 // SQRT: LHS = (A * E) * -0.5
22871 LHS = DAG.getNode(ISD::FMUL, DL, VT, AE, MinusHalf, Flags);
22872 }
22873
22874 Est = DAG.getNode(ISD::FMUL, DL, VT, LHS, RHS, Flags);
22875 }
22876
22877 return Est;
22878}
22879
22880/// Build code to calculate either rsqrt(Op) or sqrt(Op). In the latter case
22881/// Op*rsqrt(Op) is actually computed, so additional postprocessing is needed if
22882/// Op can be zero.
22883SDValue DAGCombiner::buildSqrtEstimateImpl(SDValue Op, SDNodeFlags Flags,
22884 bool Reciprocal) {
22885 if (LegalDAG)
22886 return SDValue();
22887
22888 // TODO: Handle half and/or extended types?
22889 EVT VT = Op.getValueType();
22890 if (VT.getScalarType() != MVT::f32 && VT.getScalarType() != MVT::f64)
22891 return SDValue();
22892
22893 // If estimates are explicitly disabled for this function, we're done.
22894 MachineFunction &MF = DAG.getMachineFunction();
22895 int Enabled = TLI.getRecipEstimateSqrtEnabled(VT, MF);
22896 if (Enabled == TLI.ReciprocalEstimate::Disabled)
22897 return SDValue();
22898
22899 // Estimates may be explicitly enabled for this type with a custom number of
22900 // refinement steps.
22901 int Iterations = TLI.getSqrtRefinementSteps(VT, MF);
22902
22903 bool UseOneConstNR = false;
22904 if (SDValue Est =
22905 TLI.getSqrtEstimate(Op, DAG, Enabled, Iterations, UseOneConstNR,
22906 Reciprocal)) {
22907 AddToWorklist(Est.getNode());
22908
22909 if (Iterations)
22910 Est = UseOneConstNR
22911 ? buildSqrtNROneConst(Op, Est, Iterations, Flags, Reciprocal)
22912 : buildSqrtNRTwoConst(Op, Est, Iterations, Flags, Reciprocal);
22913 if (!Reciprocal) {
22914 SDLoc DL(Op);
22915 // Try the target specific test first.
22916 SDValue Test = TLI.getSqrtInputTest(Op, DAG, DAG.getDenormalMode(VT));
22917
22918 // The estimate is now completely wrong if the input was exactly 0.0 or
22919 // possibly a denormal. Force the answer to 0.0 or value provided by
22920 // target for those cases.
22921 Est = DAG.getNode(
22922 Test.getValueType().isVector() ? ISD::VSELECT : ISD::SELECT, DL, VT,
22923 Test, TLI.getSqrtResultForDenormInput(Op, DAG), Est);
22924 }
22925 return Est;
22926 }
22927
22928 return SDValue();
22929}
22930
22931SDValue DAGCombiner::buildRsqrtEstimate(SDValue Op, SDNodeFlags Flags) {
22932 return buildSqrtEstimateImpl(Op, Flags, true);
22933}
22934
22935SDValue DAGCombiner::buildSqrtEstimate(SDValue Op, SDNodeFlags Flags) {
22936 return buildSqrtEstimateImpl(Op, Flags, false);
22937}
22938
22939/// Return true if there is any possibility that the two addresses overlap.
22940bool DAGCombiner::isAlias(SDNode *Op0, SDNode *Op1) const {
22941
22942 struct MemUseCharacteristics {
22943 bool IsVolatile;
22944 bool IsAtomic;
22945 SDValue BasePtr;
22946 int64_t Offset;
22947 Optional<int64_t> NumBytes;
22948 MachineMemOperand *MMO;
22949 };
22950
22951 auto getCharacteristics = [](SDNode *N) -> MemUseCharacteristics {
22952 if (const auto *LSN = dyn_cast<LSBaseSDNode>(N)) {
22953 int64_t Offset = 0;
22954 if (auto *C = dyn_cast<ConstantSDNode>(LSN->getOffset()))
22955 Offset = (LSN->getAddressingMode() == ISD::PRE_INC)
22956 ? C->getSExtValue()
22957 : (LSN->getAddressingMode() == ISD::PRE_DEC)
22958 ? -1 * C->getSExtValue()
22959 : 0;
22960 uint64_t Size =
22961 MemoryLocation::getSizeOrUnknown(LSN->getMemoryVT().getStoreSize());
22962 return {LSN->isVolatile(), LSN->isAtomic(), LSN->getBasePtr(),
22963 Offset /*base offset*/,
22964 Optional<int64_t>(Size),
22965 LSN->getMemOperand()};
22966 }
22967 if (const auto *LN = cast<LifetimeSDNode>(N))
22968 return {false /*isVolatile*/, /*isAtomic*/ false, LN->getOperand(1),
22969 (LN->hasOffset()) ? LN->getOffset() : 0,
22970 (LN->hasOffset()) ? Optional<int64_t>(LN->getSize())
22971 : Optional<int64_t>(),
22972 (MachineMemOperand *)nullptr};
22973 // Default.
22974 return {false /*isvolatile*/, /*isAtomic*/ false, SDValue(),
22975 (int64_t)0 /*offset*/,
22976 Optional<int64_t>() /*size*/, (MachineMemOperand *)nullptr};
22977 };
22978
22979 MemUseCharacteristics MUC0 = getCharacteristics(Op0),
22980 MUC1 = getCharacteristics(Op1);
22981
22982 // If they are to the same address, then they must be aliases.
22983 if (MUC0.BasePtr.getNode() && MUC0.BasePtr == MUC1.BasePtr &&
22984 MUC0.Offset == MUC1.Offset)
22985 return true;
22986
22987 // If they are both volatile then they cannot be reordered.
22988 if (MUC0.IsVolatile && MUC1.IsVolatile)
22989 return true;
22990
22991 // Be conservative about atomics for the moment
22992 // TODO: This is way overconservative for unordered atomics (see D66309)
22993 if (MUC0.IsAtomic && MUC1.IsAtomic)
22994 return true;
22995
22996 if (MUC0.MMO && MUC1.MMO) {
22997 if ((MUC0.MMO->isInvariant() && MUC1.MMO->isStore()) ||
22998 (MUC1.MMO->isInvariant() && MUC0.MMO->isStore()))
22999 return false;
23000 }
23001
23002 // Try to prove that there is aliasing, or that there is no aliasing. Either
23003 // way, we can return now. If nothing can be proved, proceed with more tests.
23004 bool IsAlias;
23005 if (BaseIndexOffset::computeAliasing(Op0, MUC0.NumBytes, Op1, MUC1.NumBytes,
23006 DAG, IsAlias))
23007 return IsAlias;
23008
23009 // The following all rely on MMO0 and MMO1 being valid. Fail conservatively if
23010 // either are not known.
23011 if (!MUC0.MMO || !MUC1.MMO)
23012 return true;
23013
23014 // If one operation reads from invariant memory, and the other may store, they
23015 // cannot alias. These should really be checking the equivalent of mayWrite,
23016 // but it only matters for memory nodes other than load /store.
23017 if ((MUC0.MMO->isInvariant() && MUC1.MMO->isStore()) ||
23018 (MUC1.MMO->isInvariant() && MUC0.MMO->isStore()))
23019 return false;
23020
23021 // If we know required SrcValue1 and SrcValue2 have relatively large
23022 // alignment compared to the size and offset of the access, we may be able
23023 // to prove they do not alias. This check is conservative for now to catch
23024 // cases created by splitting vector types, it only works when the offsets are
23025 // multiples of the size of the data.
23026 int64_t SrcValOffset0 = MUC0.MMO->getOffset();
23027 int64_t SrcValOffset1 = MUC1.MMO->getOffset();
23028 Align OrigAlignment0 = MUC0.MMO->getBaseAlign();
23029 Align OrigAlignment1 = MUC1.MMO->getBaseAlign();
23030 auto &Size0 = MUC0.NumBytes;
23031 auto &Size1 = MUC1.NumBytes;
23032 if (OrigAlignment0 == OrigAlignment1 && SrcValOffset0 != SrcValOffset1 &&
23033 Size0.hasValue() && Size1.hasValue() && *Size0 == *Size1 &&
23034 OrigAlignment0 > *Size0 && SrcValOffset0 % *Size0 == 0 &&
23035 SrcValOffset1 % *Size1 == 0) {
23036 int64_t OffAlign0 = SrcValOffset0 % OrigAlignment0.value();
23037 int64_t OffAlign1 = SrcValOffset1 % OrigAlignment1.value();
23038
23039 // There is no overlap between these relatively aligned accesses of
23040 // similar size. Return no alias.
23041 if ((OffAlign0 + *Size0) <= OffAlign1 || (OffAlign1 + *Size1) <= OffAlign0)
23042 return false;
23043 }
23044
23045 bool UseAA = CombinerGlobalAA.getNumOccurrences() > 0
23046 ? CombinerGlobalAA
23047 : DAG.getSubtarget().useAA();
23048#ifndef NDEBUG
23049 if (CombinerAAOnlyFunc.getNumOccurrences() &&
23050 CombinerAAOnlyFunc != DAG.getMachineFunction().getName())
23051 UseAA = false;
23052#endif
23053
23054 if (UseAA && AA && MUC0.MMO->getValue() && MUC1.MMO->getValue() &&
23055 Size0.hasValue() && Size1.hasValue()) {
23056 // Use alias analysis information.
23057 int64_t MinOffset = std::min(SrcValOffset0, SrcValOffset1);
23058 int64_t Overlap0 = *Size0 + SrcValOffset0 - MinOffset;
23059 int64_t Overlap1 = *Size1 + SrcValOffset1 - MinOffset;
23060 if (AA->isNoAlias(
23061 MemoryLocation(MUC0.MMO->getValue(), Overlap0,
23062 UseTBAA ? MUC0.MMO->getAAInfo() : AAMDNodes()),
23063 MemoryLocation(MUC1.MMO->getValue(), Overlap1,
23064 UseTBAA ? MUC1.MMO->getAAInfo() : AAMDNodes())))
23065 return false;
23066 }
23067
23068 // Otherwise we have to assume they alias.
23069 return true;
23070}
23071
23072/// Walk up chain skipping non-aliasing memory nodes,
23073/// looking for aliasing nodes and adding them to the Aliases vector.
23074void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
23075 SmallVectorImpl<SDValue> &Aliases) {
23076 SmallVector<SDValue, 8> Chains; // List of chains to visit.
23077 SmallPtrSet<SDNode *, 16> Visited; // Visited node set.
23078
23079 // Get alias information for node.
23080 // TODO: relax aliasing for unordered atomics (see D66309)
23081 const bool IsLoad = isa<LoadSDNode>(N) && cast<LoadSDNode>(N)->isSimple();
23082
23083 // Starting off.
23084 Chains.push_back(OriginalChain);
23085 unsigned Depth = 0;
23086
23087 // Attempt to improve chain by a single step
23088 std::function<bool(SDValue &)> ImproveChain = [&](SDValue &C) -> bool {
23089 switch (C.getOpcode()) {
23090 case ISD::EntryToken:
23091 // No need to mark EntryToken.
23092 C = SDValue();
23093 return true;
23094 case ISD::LOAD:
23095 case ISD::STORE: {
23096 // Get alias information for C.
23097 // TODO: Relax aliasing for unordered atomics (see D66309)
23098 bool IsOpLoad = isa<LoadSDNode>(C.getNode()) &&
23099 cast<LSBaseSDNode>(C.getNode())->isSimple();
23100 if ((IsLoad && IsOpLoad) || !isAlias(N, C.getNode())) {
23101 // Look further up the chain.
23102 C = C.getOperand(0);
23103 return true;
23104 }
23105 // Alias, so stop here.
23106 return false;
23107 }
23108
23109 case ISD::CopyFromReg:
23110 // Always forward past past CopyFromReg.
23111 C = C.getOperand(0);
23112 return true;
23113
23114 case ISD::LIFETIME_START:
23115 case ISD::LIFETIME_END: {
23116 // We can forward past any lifetime start/end that can be proven not to
23117 // alias the memory access.
23118 if (!isAlias(N, C.getNode())) {
23119 // Look further up the chain.
23120 C = C.getOperand(0);
23121 return true;
23122 }
23123 return false;
23124 }
23125 default:
23126 return false;
23127 }
23128 };
23129
23130 // Look at each chain and determine if it is an alias. If so, add it to the
23131 // aliases list. If not, then continue up the chain looking for the next
23132 // candidate.
23133 while (!Chains.empty()) {
23134 SDValue Chain = Chains.pop_back_val();
23135
23136 // Don't bother if we've seen Chain before.
23137 if (!Visited.insert(Chain.getNode()).second)
23138 continue;
23139
23140 // For TokenFactor nodes, look at each operand and only continue up the
23141 // chain until we reach the depth limit.
23142 //
23143 // FIXME: The depth check could be made to return the last non-aliasing
23144 // chain we found before we hit a tokenfactor rather than the original
23145 // chain.
23146 if (Depth > TLI.getGatherAllAliasesMaxDepth()) {
23147 Aliases.clear();
23148 Aliases.push_back(OriginalChain);
23149 return;
23150 }
23151
23152 if (Chain.getOpcode() == ISD::TokenFactor) {
23153 // We have to check each of the operands of the token factor for "small"
23154 // token factors, so we queue them up. Adding the operands to the queue
23155 // (stack) in reverse order maintains the original order and increases the
23156 // likelihood that getNode will find a matching token factor (CSE.)
23157 if (Chain.getNumOperands() > 16) {
23158 Aliases.push_back(Chain);
23159 continue;
23160 }
23161 for (unsigned n = Chain.getNumOperands(); n;)
23162 Chains.push_back(Chain.getOperand(--n));
23163 ++Depth;
23164 continue;
23165 }
23166 // Everything else
23167 if (ImproveChain(Chain)) {
23168 // Updated Chain Found, Consider new chain if one exists.
23169 if (Chain.getNode())
23170 Chains.push_back(Chain);
23171 ++Depth;
23172 continue;
23173 }
23174 // No Improved Chain Possible, treat as Alias.
23175 Aliases.push_back(Chain);
23176 }
23177}
23178
23179/// Walk up chain skipping non-aliasing memory nodes, looking for a better chain
23180/// (aliasing node.)
23181SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
23182 if (OptLevel == CodeGenOpt::None)
23183 return OldChain;
23184
23185 // Ops for replacing token factor.
23186 SmallVector<SDValue, 8> Aliases;
23187
23188 // Accumulate all the aliases to this node.
23189 GatherAllAliases(N, OldChain, Aliases);
23190
23191 // If no operands then chain to entry token.
23192 if (Aliases.size() == 0)
23193 return DAG.getEntryNode();
23194
23195 // If a single operand then chain to it. We don't need to revisit it.
23196 if (Aliases.size() == 1)
23197 return Aliases[0];
23198
23199 // Construct a custom tailored token factor.
23200 return DAG.getTokenFactor(SDLoc(N), Aliases);
23201}
23202
23203namespace {
23204// TODO: Replace with with std::monostate when we move to C++17.
23205struct UnitT { } Unit;
23206bool operator==(const UnitT &, const UnitT &) { return true; }
23207bool operator!=(const UnitT &, const UnitT &) { return false; }
23208} // namespace
23209
23210// This function tries to collect a bunch of potentially interesting
23211// nodes to improve the chains of, all at once. This might seem
23212// redundant, as this function gets called when visiting every store
23213// node, so why not let the work be done on each store as it's visited?
23214//
23215// I believe this is mainly important because mergeConsecutiveStores
23216// is unable to deal with merging stores of different sizes, so unless
23217// we improve the chains of all the potential candidates up-front
23218// before running mergeConsecutiveStores, it might only see some of
23219// the nodes that will eventually be candidates, and then not be able
23220// to go from a partially-merged state to the desired final
23221// fully-merged state.
23222
23223bool DAGCombiner::parallelizeChainedStores(StoreSDNode *St) {
23224 SmallVector<StoreSDNode *, 8> ChainedStores;
23225 StoreSDNode *STChain = St;
23226 // Intervals records which offsets from BaseIndex have been covered. In
23227 // the common case, every store writes to the immediately previous address
23228 // space and thus merged with the previous interval at insertion time.
23229
23230 using IMap =
23231 llvm::IntervalMap<int64_t, UnitT, 8, IntervalMapHalfOpenInfo<int64_t>>;
23232 IMap::Allocator A;
23233 IMap Intervals(A);
23234
23235 // This holds the base pointer, index, and the offset in bytes from the base
23236 // pointer.
23237 const BaseIndexOffset BasePtr = BaseIndexOffset::match(St, DAG);
23238
23239 // We must have a base and an offset.
23240 if (!BasePtr.getBase().getNode())
23241 return false;
23242
23243 // Do not handle stores to undef base pointers.
23244 if (BasePtr.getBase().isUndef())
23245 return false;
23246
23247 // Do not handle stores to opaque types
23248 if (St->getMemoryVT().isZeroSized())
23249 return false;
23250
23251 // BaseIndexOffset assumes that offsets are fixed-size, which
23252 // is not valid for scalable vectors where the offsets are
23253 // scaled by `vscale`, so bail out early.
23254 if (St->getMemoryVT().isScalableVector())
23255 return false;
23256
23257 // Add ST's interval.
23258 Intervals.insert(0, (St->getMemoryVT().getSizeInBits() + 7) / 8, Unit);
23259
23260 while (StoreSDNode *Chain = dyn_cast<StoreSDNode>(STChain->getChain())) {
23261 if (Chain->getMemoryVT().isScalableVector())
23262 return false;
23263
23264 // If the chain has more than one use, then we can't reorder the mem ops.
23265 if (!SDValue(Chain, 0)->hasOneUse())
23266 break;
23267 // TODO: Relax for unordered atomics (see D66309)
23268 if (!Chain->isSimple() || Chain->isIndexed())
23269 break;
23270
23271 // Find the base pointer and offset for this memory node.
23272 const BaseIndexOffset Ptr = BaseIndexOffset::match(Chain, DAG);
23273 // Check that the base pointer is the same as the original one.
23274 int64_t Offset;
23275 if (!BasePtr.equalBaseIndex(Ptr, DAG, Offset))
23276 break;
23277 int64_t Length = (Chain->getMemoryVT().getSizeInBits() + 7) / 8;
23278 // Make sure we don't overlap with other intervals by checking the ones to
23279 // the left or right before inserting.
23280 auto I = Intervals.find(Offset);
23281 // If there's a next interval, we should end before it.
23282 if (I != Intervals.end() && I.start() < (Offset + Length))
23283 break;
23284 // If there's a previous interval, we should start after it.
23285 if (I != Intervals.begin() && (--I).stop() <= Offset)
23286 break;
23287 Intervals.insert(Offset, Offset + Length, Unit);
23288
23289 ChainedStores.push_back(Chain);
23290 STChain = Chain;
23291 }
23292
23293 // If we didn't find a chained store, exit.
23294 if (ChainedStores.size() == 0)
23295 return false;
23296
23297 // Improve all chained stores (St and ChainedStores members) starting from
23298 // where the store chain ended and return single TokenFactor.
23299 SDValue NewChain = STChain->getChain();
23300 SmallVector<SDValue, 8> TFOps;
23301 for (unsigned I = ChainedStores.size(); I;) {
23302 StoreSDNode *S = ChainedStores[--I];
23303 SDValue BetterChain = FindBetterChain(S, NewChain);
23304 S = cast<StoreSDNode>(DAG.UpdateNodeOperands(
23305 S, BetterChain, S->getOperand(1), S->getOperand(2), S->getOperand(3)));
23306 TFOps.push_back(SDValue(S, 0));
23307 ChainedStores[I] = S;
23308 }
23309
23310 // Improve St's chain. Use a new node to avoid creating a loop from CombineTo.
23311 SDValue BetterChain = FindBetterChain(St, NewChain);
23312 SDValue NewST;
23313 if (St->isTruncatingStore())
23314 NewST = DAG.getTruncStore(BetterChain, SDLoc(St), St->getValue(),
23315 St->getBasePtr(), St->getMemoryVT(),
23316 St->getMemOperand());
23317 else
23318 NewST = DAG.getStore(BetterChain, SDLoc(St), St->getValue(),
23319 St->getBasePtr(), St->getMemOperand());
23320
23321 TFOps.push_back(NewST);
23322
23323 // If we improved every element of TFOps, then we've lost the dependence on
23324 // NewChain to successors of St and we need to add it back to TFOps. Do so at
23325 // the beginning to keep relative order consistent with FindBetterChains.
23326 auto hasImprovedChain = [&](SDValue ST) -> bool {
23327 return ST->getOperand(0) != NewChain;
23328 };
23329 bool AddNewChain = llvm::all_of(TFOps, hasImprovedChain);
23330 if (AddNewChain)
23331 TFOps.insert(TFOps.begin(), NewChain);
23332
23333 SDValue TF = DAG.getTokenFactor(SDLoc(STChain), TFOps);
23334 CombineTo(St, TF);
23335
23336 // Add TF and its operands to the worklist.
23337 AddToWorklist(TF.getNode());
23338 for (const SDValue &Op : TF->ops())
23339 AddToWorklist(Op.getNode());
23340 AddToWorklist(STChain);
23341 return true;
23342}
23343
23344bool DAGCombiner::findBetterNeighborChains(StoreSDNode *St) {
23345 if (OptLevel == CodeGenOpt::None)
23346 return false;
23347
23348 const BaseIndexOffset BasePtr = BaseIndexOffset::match(St, DAG);
23349
23350 // We must have a base and an offset.
23351 if (!BasePtr.getBase().getNode())
23352 return false;
23353
23354 // Do not handle stores to undef base pointers.
23355 if (BasePtr.getBase().isUndef())
23356 return false;
23357
23358 // Directly improve a chain of disjoint stores starting at St.
23359 if (parallelizeChainedStores(St))
23360 return true;
23361
23362 // Improve St's Chain..
23363 SDValue BetterChain = FindBetterChain(St, St->getChain());
23364 if (St->getChain() != BetterChain) {
23365 replaceStoreChain(St, BetterChain);
23366 return true;
23367 }
23368 return false;
23369}
23370
23371/// This is the entry point for the file.
23372void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis *AA,
23373 CodeGenOpt::Level OptLevel) {
23374 /// This is the main entry point to this class.
23375 DAGCombiner(*this, AA, OptLevel).Run(Level);
23376}

/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h

1//===-- llvm/ADT/APInt.h - For Arbitrary Precision Integer -----*- C++ -*--===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file implements a class to represent arbitrary precision
11/// integral constant values and operations on them.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_ADT_APINT_H
16#define LLVM_ADT_APINT_H
17
18#include "llvm/Support/Compiler.h"
19#include "llvm/Support/MathExtras.h"
20#include <cassert>
21#include <climits>
22#include <cstring>
23#include <utility>
24
25namespace llvm {
26class FoldingSetNodeID;
27class StringRef;
28class hash_code;
29class raw_ostream;
30
31template <typename T> class SmallVectorImpl;
32template <typename T> class ArrayRef;
33template <typename T> class Optional;
34template <typename T> struct DenseMapInfo;
35
36class APInt;
37
38inline APInt operator-(APInt);
39
40//===----------------------------------------------------------------------===//
41// APInt Class
42//===----------------------------------------------------------------------===//
43
44/// Class for arbitrary precision integers.
45///
46/// APInt is a functional replacement for common case unsigned integer type like
47/// "unsigned", "unsigned long" or "uint64_t", but also allows non-byte-width
48/// integer sizes and large integer value types such as 3-bits, 15-bits, or more
49/// than 64-bits of precision. APInt provides a variety of arithmetic operators
50/// and methods to manipulate integer values of any bit-width. It supports both
51/// the typical integer arithmetic and comparison operations as well as bitwise
52/// manipulation.
53///
54/// The class has several invariants worth noting:
55/// * All bit, byte, and word positions are zero-based.
56/// * Once the bit width is set, it doesn't change except by the Truncate,
57/// SignExtend, or ZeroExtend operations.
58/// * All binary operators must be on APInt instances of the same bit width.
59/// Attempting to use these operators on instances with different bit
60/// widths will yield an assertion.
61/// * The value is stored canonically as an unsigned value. For operations
62/// where it makes a difference, there are both signed and unsigned variants
63/// of the operation. For example, sdiv and udiv. However, because the bit
64/// widths must be the same, operations such as Mul and Add produce the same
65/// results regardless of whether the values are interpreted as signed or
66/// not.
67/// * In general, the class tries to follow the style of computation that LLVM
68/// uses in its IR. This simplifies its use for LLVM.
69///
70class LLVM_NODISCARD[[clang::warn_unused_result]] APInt {
71public:
72 typedef uint64_t WordType;
73
74 /// This enum is used to hold the constants we needed for APInt.
75 enum : unsigned {
76 /// Byte size of a word.
77 APINT_WORD_SIZE = sizeof(WordType),
78 /// Bits in a word.
79 APINT_BITS_PER_WORD = APINT_WORD_SIZE * CHAR_BIT8
80 };
81
82 enum class Rounding {
83 DOWN,
84 TOWARD_ZERO,
85 UP,
86 };
87
88 static constexpr WordType WORDTYPE_MAX = ~WordType(0);
89
90private:
91 /// This union is used to store the integer value. When the
92 /// integer bit-width <= 64, it uses VAL, otherwise it uses pVal.
93 union {
94 uint64_t VAL; ///< Used to store the <= 64 bits integer value.
95 uint64_t *pVal; ///< Used to store the >64 bits integer value.
96 } U;
97
98 unsigned BitWidth; ///< The number of bits in this APInt.
99
100 friend struct DenseMapInfo<APInt>;
101
102 friend class APSInt;
103
104 /// Fast internal constructor
105 ///
106 /// This constructor is used only internally for speed of construction of
107 /// temporaries. It is unsafe for general use so it is not public.
108 APInt(uint64_t *val, unsigned bits) : BitWidth(bits) {
109 U.pVal = val;
110 }
111
112 /// Determine if this APInt just has one word to store value.
113 ///
114 /// \returns true if the number of bits <= 64, false otherwise.
115 bool isSingleWord() const { return BitWidth <= APINT_BITS_PER_WORD; }
116
117 /// Determine which word a bit is in.
118 ///
119 /// \returns the word position for the specified bit position.
120 static unsigned whichWord(unsigned bitPosition) {
121 return bitPosition / APINT_BITS_PER_WORD;
122 }
123
124 /// Determine which bit in a word a bit is in.
125 ///
126 /// \returns the bit position in a word for the specified bit position
127 /// in the APInt.
128 static unsigned whichBit(unsigned bitPosition) {
129 return bitPosition % APINT_BITS_PER_WORD;
130 }
131
132 /// Get a single bit mask.
133 ///
134 /// \returns a uint64_t with only bit at "whichBit(bitPosition)" set
135 /// This method generates and returns a uint64_t (word) mask for a single
136 /// bit at a specific bit position. This is used to mask the bit in the
137 /// corresponding word.
138 static uint64_t maskBit(unsigned bitPosition) {
139 return 1ULL << whichBit(bitPosition);
140 }
141
142 /// Clear unused high order bits
143 ///
144 /// This method is used internally to clear the top "N" bits in the high order
145 /// word that are not used by the APInt. This is needed after the most
146 /// significant word is assigned a value to ensure that those bits are
147 /// zero'd out.
148 APInt &clearUnusedBits() {
149 // Compute how many bits are used in the final word
150 unsigned WordBits = ((BitWidth-1) % APINT_BITS_PER_WORD) + 1;
151
152 // Mask out the high bits.
153 uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - WordBits);
154 if (isSingleWord())
155 U.VAL &= mask;
156 else
157 U.pVal[getNumWords() - 1] &= mask;
158 return *this;
159 }
160
161 /// Get the word corresponding to a bit position
162 /// \returns the corresponding word for the specified bit position.
163 uint64_t getWord(unsigned bitPosition) const {
164 return isSingleWord() ? U.VAL : U.pVal[whichWord(bitPosition)];
165 }
166
167 /// Utility method to change the bit width of this APInt to new bit width,
168 /// allocating and/or deallocating as necessary. There is no guarantee on the
169 /// value of any bits upon return. Caller should populate the bits after.
170 void reallocate(unsigned NewBitWidth);
171
172 /// Convert a char array into an APInt
173 ///
174 /// \param radix 2, 8, 10, 16, or 36
175 /// Converts a string into a number. The string must be non-empty
176 /// and well-formed as a number of the given base. The bit-width
177 /// must be sufficient to hold the result.
178 ///
179 /// This is used by the constructors that take string arguments.
180 ///
181 /// StringRef::getAsInteger is superficially similar but (1) does
182 /// not assume that the string is well-formed and (2) grows the
183 /// result to hold the input.
184 void fromString(unsigned numBits, StringRef str, uint8_t radix);
185
186 /// An internal division function for dividing APInts.
187 ///
188 /// This is used by the toString method to divide by the radix. It simply
189 /// provides a more convenient form of divide for internal use since KnuthDiv
190 /// has specific constraints on its inputs. If those constraints are not met
191 /// then it provides a simpler form of divide.
192 static void divide(const WordType *LHS, unsigned lhsWords,
193 const WordType *RHS, unsigned rhsWords, WordType *Quotient,
194 WordType *Remainder);
195
196 /// out-of-line slow case for inline constructor
197 void initSlowCase(uint64_t val, bool isSigned);
198
199 /// shared code between two array constructors
200 void initFromArray(ArrayRef<uint64_t> array);
201
202 /// out-of-line slow case for inline copy constructor
203 void initSlowCase(const APInt &that);
204
205 /// out-of-line slow case for shl
206 void shlSlowCase(unsigned ShiftAmt);
207
208 /// out-of-line slow case for lshr.
209 void lshrSlowCase(unsigned ShiftAmt);
210
211 /// out-of-line slow case for ashr.
212 void ashrSlowCase(unsigned ShiftAmt);
213
214 /// out-of-line slow case for operator=
215 void AssignSlowCase(const APInt &RHS);
216
217 /// out-of-line slow case for operator==
218 bool EqualSlowCase(const APInt &RHS) const LLVM_READONLY__attribute__((__pure__));
219
220 /// out-of-line slow case for countLeadingZeros
221 unsigned countLeadingZerosSlowCase() const LLVM_READONLY__attribute__((__pure__));
222
223 /// out-of-line slow case for countLeadingOnes.
224 unsigned countLeadingOnesSlowCase() const LLVM_READONLY__attribute__((__pure__));
225
226 /// out-of-line slow case for countTrailingZeros.
227 unsigned countTrailingZerosSlowCase() const LLVM_READONLY__attribute__((__pure__));
228
229 /// out-of-line slow case for countTrailingOnes
230 unsigned countTrailingOnesSlowCase() const LLVM_READONLY__attribute__((__pure__));
231
232 /// out-of-line slow case for countPopulation
233 unsigned countPopulationSlowCase() const LLVM_READONLY__attribute__((__pure__));
234
235 /// out-of-line slow case for intersects.
236 bool intersectsSlowCase(const APInt &RHS) const LLVM_READONLY__attribute__((__pure__));
237
238 /// out-of-line slow case for isSubsetOf.
239 bool isSubsetOfSlowCase(const APInt &RHS) const LLVM_READONLY__attribute__((__pure__));
240
241 /// out-of-line slow case for setBits.
242 void setBitsSlowCase(unsigned loBit, unsigned hiBit);
243
244 /// out-of-line slow case for flipAllBits.
245 void flipAllBitsSlowCase();
246
247 /// out-of-line slow case for operator&=.
248 void AndAssignSlowCase(const APInt& RHS);
249
250 /// out-of-line slow case for operator|=.
251 void OrAssignSlowCase(const APInt& RHS);
252
253 /// out-of-line slow case for operator^=.
254 void XorAssignSlowCase(const APInt& RHS);
255
256 /// Unsigned comparison. Returns -1, 0, or 1 if this APInt is less than, equal
257 /// to, or greater than RHS.
258 int compare(const APInt &RHS) const LLVM_READONLY__attribute__((__pure__));
259
260 /// Signed comparison. Returns -1, 0, or 1 if this APInt is less than, equal
261 /// to, or greater than RHS.
262 int compareSigned(const APInt &RHS) const LLVM_READONLY__attribute__((__pure__));
263
264public:
265 /// \name Constructors
266 /// @{
267
268 /// Create a new APInt of numBits width, initialized as val.
269 ///
270 /// If isSigned is true then val is treated as if it were a signed value
271 /// (i.e. as an int64_t) and the appropriate sign extension to the bit width
272 /// will be done. Otherwise, no sign extension occurs (high order bits beyond
273 /// the range of val are zero filled).
274 ///
275 /// \param numBits the bit width of the constructed APInt
276 /// \param val the initial value of the APInt
277 /// \param isSigned how to treat signedness of val
278 APInt(unsigned numBits, uint64_t val, bool isSigned = false)
279 : BitWidth(numBits) {
280 assert(BitWidth && "bitwidth too small")(static_cast <bool> (BitWidth && "bitwidth too small"
) ? void (0) : __assert_fail ("BitWidth && \"bitwidth too small\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 280, __extension__ __PRETTY_FUNCTION__))
;
281 if (isSingleWord()) {
282 U.VAL = val;
283 clearUnusedBits();
284 } else {
285 initSlowCase(val, isSigned);
286 }
287 }
288
289 /// Construct an APInt of numBits width, initialized as bigVal[].
290 ///
291 /// Note that bigVal.size() can be smaller or larger than the corresponding
292 /// bit width but any extraneous bits will be dropped.
293 ///
294 /// \param numBits the bit width of the constructed APInt
295 /// \param bigVal a sequence of words to form the initial value of the APInt
296 APInt(unsigned numBits, ArrayRef<uint64_t> bigVal);
297
298 /// Equivalent to APInt(numBits, ArrayRef<uint64_t>(bigVal, numWords)), but
299 /// deprecated because this constructor is prone to ambiguity with the
300 /// APInt(unsigned, uint64_t, bool) constructor.
301 ///
302 /// If this overload is ever deleted, care should be taken to prevent calls
303 /// from being incorrectly captured by the APInt(unsigned, uint64_t, bool)
304 /// constructor.
305 APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[]);
306
307 /// Construct an APInt from a string representation.
308 ///
309 /// This constructor interprets the string \p str in the given radix. The
310 /// interpretation stops when the first character that is not suitable for the
311 /// radix is encountered, or the end of the string. Acceptable radix values
312 /// are 2, 8, 10, 16, and 36. It is an error for the value implied by the
313 /// string to require more bits than numBits.
314 ///
315 /// \param numBits the bit width of the constructed APInt
316 /// \param str the string to be interpreted
317 /// \param radix the radix to use for the conversion
318 APInt(unsigned numBits, StringRef str, uint8_t radix);
319
320 /// Simply makes *this a copy of that.
321 /// Copy Constructor.
322 APInt(const APInt &that) : BitWidth(that.BitWidth) {
323 if (isSingleWord())
324 U.VAL = that.U.VAL;
325 else
326 initSlowCase(that);
327 }
328
329 /// Move Constructor.
330 APInt(APInt &&that) : BitWidth(that.BitWidth) {
331 memcpy(&U, &that.U, sizeof(U));
332 that.BitWidth = 0;
333 }
334
335 /// Destructor.
336 ~APInt() {
337 if (needsCleanup())
338 delete[] U.pVal;
339 }
340
341 /// Default constructor that creates an uninteresting APInt
342 /// representing a 1-bit zero value.
343 ///
344 /// This is useful for object deserialization (pair this with the static
345 /// method Read).
346 explicit APInt() : BitWidth(1) { U.VAL = 0; }
347
348 /// Returns whether this instance allocated memory.
349 bool needsCleanup() const { return !isSingleWord(); }
350
351 /// Used to insert APInt objects, or objects that contain APInt objects, into
352 /// FoldingSets.
353 void Profile(FoldingSetNodeID &id) const;
354
355 /// @}
356 /// \name Value Tests
357 /// @{
358
359 /// Determine sign of this APInt.
360 ///
361 /// This tests the high bit of this APInt to determine if it is set.
362 ///
363 /// \returns true if this APInt is negative, false otherwise
364 bool isNegative() const { return (*this)[BitWidth - 1]; }
365
366 /// Determine if this APInt Value is non-negative (>= 0)
367 ///
368 /// This tests the high bit of the APInt to determine if it is unset.
369 bool isNonNegative() const { return !isNegative(); }
370
371 /// Determine if sign bit of this APInt is set.
372 ///
373 /// This tests the high bit of this APInt to determine if it is set.
374 ///
375 /// \returns true if this APInt has its sign bit set, false otherwise.
376 bool isSignBitSet() const { return (*this)[BitWidth-1]; }
377
378 /// Determine if sign bit of this APInt is clear.
379 ///
380 /// This tests the high bit of this APInt to determine if it is clear.
381 ///
382 /// \returns true if this APInt has its sign bit clear, false otherwise.
383 bool isSignBitClear() const { return !isSignBitSet(); }
384
385 /// Determine if this APInt Value is positive.
386 ///
387 /// This tests if the value of this APInt is positive (> 0). Note
388 /// that 0 is not a positive value.
389 ///
390 /// \returns true if this APInt is positive.
391 bool isStrictlyPositive() const { return isNonNegative() && !isNullValue(); }
392
393 /// Determine if this APInt Value is non-positive (<= 0).
394 ///
395 /// \returns true if this APInt is non-positive.
396 bool isNonPositive() const { return !isStrictlyPositive(); }
397
398 /// Determine if all bits are set
399 ///
400 /// This checks to see if the value has all bits of the APInt are set or not.
401 bool isAllOnesValue() const {
402 if (isSingleWord())
403 return U.VAL == WORDTYPE_MAX >> (APINT_BITS_PER_WORD - BitWidth);
404 return countTrailingOnesSlowCase() == BitWidth;
405 }
406
407 /// Determine if all bits are clear
408 ///
409 /// This checks to see if the value has all bits of the APInt are clear or
410 /// not.
411 bool isNullValue() const { return !*this; }
412
413 /// Determine if this is a value of 1.
414 ///
415 /// This checks to see if the value of this APInt is one.
416 bool isOneValue() const {
417 if (isSingleWord())
418 return U.VAL == 1;
419 return countLeadingZerosSlowCase() == BitWidth - 1;
420 }
421
422 /// Determine if this is the largest unsigned value.
423 ///
424 /// This checks to see if the value of this APInt is the maximum unsigned
425 /// value for the APInt's bit width.
426 bool isMaxValue() const { return isAllOnesValue(); }
427
428 /// Determine if this is the largest signed value.
429 ///
430 /// This checks to see if the value of this APInt is the maximum signed
431 /// value for the APInt's bit width.
432 bool isMaxSignedValue() const {
433 if (isSingleWord())
434 return U.VAL == ((WordType(1) << (BitWidth - 1)) - 1);
435 return !isNegative() && countTrailingOnesSlowCase() == BitWidth - 1;
436 }
437
438 /// Determine if this is the smallest unsigned value.
439 ///
440 /// This checks to see if the value of this APInt is the minimum unsigned
441 /// value for the APInt's bit width.
442 bool isMinValue() const { return isNullValue(); }
443
444 /// Determine if this is the smallest signed value.
445 ///
446 /// This checks to see if the value of this APInt is the minimum signed
447 /// value for the APInt's bit width.
448 bool isMinSignedValue() const {
449 if (isSingleWord())
450 return U.VAL == (WordType(1) << (BitWidth - 1));
451 return isNegative() && countTrailingZerosSlowCase() == BitWidth - 1;
452 }
453
454 /// Check if this APInt has an N-bits unsigned integer value.
455 bool isIntN(unsigned N) const {
456 assert(N && "N == 0 ???")(static_cast <bool> (N && "N == 0 ???") ? void (
0) : __assert_fail ("N && \"N == 0 ???\"", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 456, __extension__ __PRETTY_FUNCTION__))
;
457 return getActiveBits() <= N;
458 }
459
460 /// Check if this APInt has an N-bits signed integer value.
461 bool isSignedIntN(unsigned N) const {
462 assert(N && "N == 0 ???")(static_cast <bool> (N && "N == 0 ???") ? void (
0) : __assert_fail ("N && \"N == 0 ???\"", "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 462, __extension__ __PRETTY_FUNCTION__))
;
463 return getMinSignedBits() <= N;
464 }
465
466 /// Check if this APInt's value is a power of two greater than zero.
467 ///
468 /// \returns true if the argument APInt value is a power of two > 0.
469 bool isPowerOf2() const {
470 if (isSingleWord())
471 return isPowerOf2_64(U.VAL);
472 return countPopulationSlowCase() == 1;
473 }
474
475 /// Check if the APInt's value is returned by getSignMask.
476 ///
477 /// \returns true if this is the value returned by getSignMask.
478 bool isSignMask() const { return isMinSignedValue(); }
479
480 /// Convert APInt to a boolean value.
481 ///
482 /// This converts the APInt to a boolean value as a test against zero.
483 bool getBoolValue() const { return !!*this; }
484
485 /// If this value is smaller than the specified limit, return it, otherwise
486 /// return the limit value. This causes the value to saturate to the limit.
487 uint64_t getLimitedValue(uint64_t Limit = UINT64_MAX(18446744073709551615UL)) const {
488 return ugt(Limit) ? Limit : getZExtValue();
489 }
490
491 /// Check if the APInt consists of a repeated bit pattern.
492 ///
493 /// e.g. 0x01010101 satisfies isSplat(8).
494 /// \param SplatSizeInBits The size of the pattern in bits. Must divide bit
495 /// width without remainder.
496 bool isSplat(unsigned SplatSizeInBits) const;
497
498 /// \returns true if this APInt value is a sequence of \param numBits ones
499 /// starting at the least significant bit with the remainder zero.
500 bool isMask(unsigned numBits) const {
501 assert(numBits != 0 && "numBits must be non-zero")(static_cast <bool> (numBits != 0 && "numBits must be non-zero"
) ? void (0) : __assert_fail ("numBits != 0 && \"numBits must be non-zero\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 501, __extension__ __PRETTY_FUNCTION__))
;
502 assert(numBits <= BitWidth && "numBits out of range")(static_cast <bool> (numBits <= BitWidth && "numBits out of range"
) ? void (0) : __assert_fail ("numBits <= BitWidth && \"numBits out of range\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 502, __extension__ __PRETTY_FUNCTION__))
;
503 if (isSingleWord())
504 return U.VAL == (WORDTYPE_MAX >> (APINT_BITS_PER_WORD - numBits));
505 unsigned Ones = countTrailingOnesSlowCase();
506 return (numBits == Ones) &&
507 ((Ones + countLeadingZerosSlowCase()) == BitWidth);
508 }
509
510 /// \returns true if this APInt is a non-empty sequence of ones starting at
511 /// the least significant bit with the remainder zero.
512 /// Ex. isMask(0x0000FFFFU) == true.
513 bool isMask() const {
514 if (isSingleWord())
515 return isMask_64(U.VAL);
516 unsigned Ones = countTrailingOnesSlowCase();
517 return (Ones > 0) && ((Ones + countLeadingZerosSlowCase()) == BitWidth);
518 }
519
520 /// Return true if this APInt value contains a sequence of ones with
521 /// the remainder zero.
522 bool isShiftedMask() const {
523 if (isSingleWord())
524 return isShiftedMask_64(U.VAL);
525 unsigned Ones = countPopulationSlowCase();
526 unsigned LeadZ = countLeadingZerosSlowCase();
527 return (Ones + LeadZ + countTrailingZeros()) == BitWidth;
528 }
529
530 /// @}
531 /// \name Value Generators
532 /// @{
533
534 /// Gets maximum unsigned value of APInt for specific bit width.
535 static APInt getMaxValue(unsigned numBits) {
536 return getAllOnesValue(numBits);
537 }
538
539 /// Gets maximum signed value of APInt for a specific bit width.
540 static APInt getSignedMaxValue(unsigned numBits) {
541 APInt API = getAllOnesValue(numBits);
542 API.clearBit(numBits - 1);
543 return API;
544 }
545
546 /// Gets minimum unsigned value of APInt for a specific bit width.
547 static APInt getMinValue(unsigned numBits) { return APInt(numBits, 0); }
548
549 /// Gets minimum signed value of APInt for a specific bit width.
550 static APInt getSignedMinValue(unsigned numBits) {
551 APInt API(numBits, 0);
552 API.setBit(numBits - 1);
553 return API;
554 }
555
556 /// Get the SignMask for a specific bit width.
557 ///
558 /// This is just a wrapper function of getSignedMinValue(), and it helps code
559 /// readability when we want to get a SignMask.
560 static APInt getSignMask(unsigned BitWidth) {
561 return getSignedMinValue(BitWidth);
562 }
563
564 /// Get the all-ones value.
565 ///
566 /// \returns the all-ones value for an APInt of the specified bit-width.
567 static APInt getAllOnesValue(unsigned numBits) {
568 return APInt(numBits, WORDTYPE_MAX, true);
569 }
570
571 /// Get the '0' value.
572 ///
573 /// \returns the '0' value for an APInt of the specified bit-width.
574 static APInt getNullValue(unsigned numBits) { return APInt(numBits, 0); }
575
576 /// Compute an APInt containing numBits highbits from this APInt.
577 ///
578 /// Get an APInt with the same BitWidth as this APInt, just zero mask
579 /// the low bits and right shift to the least significant bit.
580 ///
581 /// \returns the high "numBits" bits of this APInt.
582 APInt getHiBits(unsigned numBits) const;
583
584 /// Compute an APInt containing numBits lowbits from this APInt.
585 ///
586 /// Get an APInt with the same BitWidth as this APInt, just zero mask
587 /// the high bits.
588 ///
589 /// \returns the low "numBits" bits of this APInt.
590 APInt getLoBits(unsigned numBits) const;
591
592 /// Return an APInt with exactly one bit set in the result.
593 static APInt getOneBitSet(unsigned numBits, unsigned BitNo) {
594 APInt Res(numBits, 0);
595 Res.setBit(BitNo);
596 return Res;
597 }
598
599 /// Get a value with a block of bits set.
600 ///
601 /// Constructs an APInt value that has a contiguous range of bits set. The
602 /// bits from loBit (inclusive) to hiBit (exclusive) will be set. All other
603 /// bits will be zero. For example, with parameters(32, 0, 16) you would get
604 /// 0x0000FFFF. Please call getBitsSetWithWrap if \p loBit may be greater than
605 /// \p hiBit.
606 ///
607 /// \param numBits the intended bit width of the result
608 /// \param loBit the index of the lowest bit set.
609 /// \param hiBit the index of the highest bit set.
610 ///
611 /// \returns An APInt value with the requested bits set.
612 static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit) {
613 assert(loBit <= hiBit && "loBit greater than hiBit")(static_cast <bool> (loBit <= hiBit && "loBit greater than hiBit"
) ? void (0) : __assert_fail ("loBit <= hiBit && \"loBit greater than hiBit\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 613, __extension__ __PRETTY_FUNCTION__))
;
614 APInt Res(numBits, 0);
615 Res.setBits(loBit, hiBit);
616 return Res;
617 }
618
619 /// Wrap version of getBitsSet.
620 /// If \p hiBit is bigger than \p loBit, this is same with getBitsSet.
621 /// If \p hiBit is not bigger than \p loBit, the set bits "wrap". For example,
622 /// with parameters (32, 28, 4), you would get 0xF000000F.
623 /// If \p hiBit is equal to \p loBit, you would get a result with all bits
624 /// set.
625 static APInt getBitsSetWithWrap(unsigned numBits, unsigned loBit,
626 unsigned hiBit) {
627 APInt Res(numBits, 0);
628 Res.setBitsWithWrap(loBit, hiBit);
629 return Res;
630 }
631
632 /// Get a value with upper bits starting at loBit set.
633 ///
634 /// Constructs an APInt value that has a contiguous range of bits set. The
635 /// bits from loBit (inclusive) to numBits (exclusive) will be set. All other
636 /// bits will be zero. For example, with parameters(32, 12) you would get
637 /// 0xFFFFF000.
638 ///
639 /// \param numBits the intended bit width of the result
640 /// \param loBit the index of the lowest bit to set.
641 ///
642 /// \returns An APInt value with the requested bits set.
643 static APInt getBitsSetFrom(unsigned numBits, unsigned loBit) {
644 APInt Res(numBits, 0);
645 Res.setBitsFrom(loBit);
646 return Res;
647 }
648
649 /// Get a value with high bits set
650 ///
651 /// Constructs an APInt value that has the top hiBitsSet bits set.
652 ///
653 /// \param numBits the bitwidth of the result
654 /// \param hiBitsSet the number of high-order bits set in the result.
655 static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet) {
656 APInt Res(numBits, 0);
657 Res.setHighBits(hiBitsSet);
658 return Res;
659 }
660
661 /// Get a value with low bits set
662 ///
663 /// Constructs an APInt value that has the bottom loBitsSet bits set.
664 ///
665 /// \param numBits the bitwidth of the result
666 /// \param loBitsSet the number of low-order bits set in the result.
667 static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet) {
668 APInt Res(numBits, 0);
669 Res.setLowBits(loBitsSet);
670 return Res;
671 }
672
673 /// Return a value containing V broadcasted over NewLen bits.
674 static APInt getSplat(unsigned NewLen, const APInt &V);
675
676 /// Determine if two APInts have the same value, after zero-extending
677 /// one of them (if needed!) to ensure that the bit-widths match.
678 static bool isSameValue(const APInt &I1, const APInt &I2) {
679 if (I1.getBitWidth() == I2.getBitWidth())
680 return I1 == I2;
681
682 if (I1.getBitWidth() > I2.getBitWidth())
683 return I1 == I2.zext(I1.getBitWidth());
684
685 return I1.zext(I2.getBitWidth()) == I2;
686 }
687
688 /// Overload to compute a hash_code for an APInt value.
689 friend hash_code hash_value(const APInt &Arg);
690
691 /// This function returns a pointer to the internal storage of the APInt.
692 /// This is useful for writing out the APInt in binary form without any
693 /// conversions.
694 const uint64_t *getRawData() const {
695 if (isSingleWord())
696 return &U.VAL;
697 return &U.pVal[0];
698 }
699
700 /// @}
701 /// \name Unary Operators
702 /// @{
703
704 /// Postfix increment operator.
705 ///
706 /// Increments *this by 1.
707 ///
708 /// \returns a new APInt value representing the original value of *this.
709 const APInt operator++(int) {
710 APInt API(*this);
711 ++(*this);
712 return API;
713 }
714
715 /// Prefix increment operator.
716 ///
717 /// \returns *this incremented by one
718 APInt &operator++();
719
720 /// Postfix decrement operator.
721 ///
722 /// Decrements *this by 1.
723 ///
724 /// \returns a new APInt value representing the original value of *this.
725 const APInt operator--(int) {
726 APInt API(*this);
727 --(*this);
728 return API;
729 }
730
731 /// Prefix decrement operator.
732 ///
733 /// \returns *this decremented by one.
734 APInt &operator--();
735
736 /// Logical negation operator.
737 ///
738 /// Performs logical negation operation on this APInt.
739 ///
740 /// \returns true if *this is zero, false otherwise.
741 bool operator!() const {
742 if (isSingleWord())
743 return U.VAL == 0;
744 return countLeadingZerosSlowCase() == BitWidth;
745 }
746
747 /// @}
748 /// \name Assignment Operators
749 /// @{
750
751 /// Copy assignment operator.
752 ///
753 /// \returns *this after assignment of RHS.
754 APInt &operator=(const APInt &RHS) {
755 // If the bitwidths are the same, we can avoid mucking with memory
756 if (isSingleWord() && RHS.isSingleWord()) {
757 U.VAL = RHS.U.VAL;
758 BitWidth = RHS.BitWidth;
759 return clearUnusedBits();
760 }
761
762 AssignSlowCase(RHS);
763 return *this;
764 }
765
766 /// Move assignment operator.
767 APInt &operator=(APInt &&that) {
768#ifdef EXPENSIVE_CHECKS
769 // Some std::shuffle implementations still do self-assignment.
770 if (this == &that)
771 return *this;
772#endif
773 assert(this != &that && "Self-move not supported")(static_cast <bool> (this != &that && "Self-move not supported"
) ? void (0) : __assert_fail ("this != &that && \"Self-move not supported\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 773, __extension__ __PRETTY_FUNCTION__))
;
774 if (!isSingleWord())
775 delete[] U.pVal;
776
777 // Use memcpy so that type based alias analysis sees both VAL and pVal
778 // as modified.
779 memcpy(&U, &that.U, sizeof(U));
780
781 BitWidth = that.BitWidth;
782 that.BitWidth = 0;
783
784 return *this;
785 }
786
787 /// Assignment operator.
788 ///
789 /// The RHS value is assigned to *this. If the significant bits in RHS exceed
790 /// the bit width, the excess bits are truncated. If the bit width is larger
791 /// than 64, the value is zero filled in the unspecified high order bits.
792 ///
793 /// \returns *this after assignment of RHS value.
794 APInt &operator=(uint64_t RHS) {
795 if (isSingleWord()) {
796 U.VAL = RHS;
797 return clearUnusedBits();
798 }
799 U.pVal[0] = RHS;
800 memset(U.pVal + 1, 0, (getNumWords() - 1) * APINT_WORD_SIZE);
801 return *this;
802 }
803
804 /// Bitwise AND assignment operator.
805 ///
806 /// Performs a bitwise AND operation on this APInt and RHS. The result is
807 /// assigned to *this.
808 ///
809 /// \returns *this after ANDing with RHS.
810 APInt &operator&=(const APInt &RHS) {
811 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same")(static_cast <bool> (BitWidth == RHS.BitWidth &&
"Bit widths must be the same") ? void (0) : __assert_fail ("BitWidth == RHS.BitWidth && \"Bit widths must be the same\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 811, __extension__ __PRETTY_FUNCTION__))
;
812 if (isSingleWord())
813 U.VAL &= RHS.U.VAL;
814 else
815 AndAssignSlowCase(RHS);
816 return *this;
817 }
818
819 /// Bitwise AND assignment operator.
820 ///
821 /// Performs a bitwise AND operation on this APInt and RHS. RHS is
822 /// logically zero-extended or truncated to match the bit-width of
823 /// the LHS.
824 APInt &operator&=(uint64_t RHS) {
825 if (isSingleWord()) {
826 U.VAL &= RHS;
827 return *this;
828 }
829 U.pVal[0] &= RHS;
830 memset(U.pVal+1, 0, (getNumWords() - 1) * APINT_WORD_SIZE);
831 return *this;
832 }
833
834 /// Bitwise OR assignment operator.
835 ///
836 /// Performs a bitwise OR operation on this APInt and RHS. The result is
837 /// assigned *this;
838 ///
839 /// \returns *this after ORing with RHS.
840 APInt &operator|=(const APInt &RHS) {
841 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same")(static_cast <bool> (BitWidth == RHS.BitWidth &&
"Bit widths must be the same") ? void (0) : __assert_fail ("BitWidth == RHS.BitWidth && \"Bit widths must be the same\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 841, __extension__ __PRETTY_FUNCTION__))
;
842 if (isSingleWord())
843 U.VAL |= RHS.U.VAL;
844 else
845 OrAssignSlowCase(RHS);
846 return *this;
847 }
848
849 /// Bitwise OR assignment operator.
850 ///
851 /// Performs a bitwise OR operation on this APInt and RHS. RHS is
852 /// logically zero-extended or truncated to match the bit-width of
853 /// the LHS.
854 APInt &operator|=(uint64_t RHS) {
855 if (isSingleWord()) {
856 U.VAL |= RHS;
857 return clearUnusedBits();
858 }
859 U.pVal[0] |= RHS;
860 return *this;
861 }
862
863 /// Bitwise XOR assignment operator.
864 ///
865 /// Performs a bitwise XOR operation on this APInt and RHS. The result is
866 /// assigned to *this.
867 ///
868 /// \returns *this after XORing with RHS.
869 APInt &operator^=(const APInt &RHS) {
870 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same")(static_cast <bool> (BitWidth == RHS.BitWidth &&
"Bit widths must be the same") ? void (0) : __assert_fail ("BitWidth == RHS.BitWidth && \"Bit widths must be the same\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 870, __extension__ __PRETTY_FUNCTION__))
;
871 if (isSingleWord())
872 U.VAL ^= RHS.U.VAL;
873 else
874 XorAssignSlowCase(RHS);
875 return *this;
876 }
877
878 /// Bitwise XOR assignment operator.
879 ///
880 /// Performs a bitwise XOR operation on this APInt and RHS. RHS is
881 /// logically zero-extended or truncated to match the bit-width of
882 /// the LHS.
883 APInt &operator^=(uint64_t RHS) {
884 if (isSingleWord()) {
885 U.VAL ^= RHS;
886 return clearUnusedBits();
887 }
888 U.pVal[0] ^= RHS;
889 return *this;
890 }
891
892 /// Multiplication assignment operator.
893 ///
894 /// Multiplies this APInt by RHS and assigns the result to *this.
895 ///
896 /// \returns *this
897 APInt &operator*=(const APInt &RHS);
898 APInt &operator*=(uint64_t RHS);
899
900 /// Addition assignment operator.
901 ///
902 /// Adds RHS to *this and assigns the result to *this.
903 ///
904 /// \returns *this
905 APInt &operator+=(const APInt &RHS);
906 APInt &operator+=(uint64_t RHS);
907
908 /// Subtraction assignment operator.
909 ///
910 /// Subtracts RHS from *this and assigns the result to *this.
911 ///
912 /// \returns *this
913 APInt &operator-=(const APInt &RHS);
914 APInt &operator-=(uint64_t RHS);
915
916 /// Left-shift assignment function.
917 ///
918 /// Shifts *this left by shiftAmt and assigns the result to *this.
919 ///
920 /// \returns *this after shifting left by ShiftAmt
921 APInt &operator<<=(unsigned ShiftAmt) {
922 assert(ShiftAmt <= BitWidth && "Invalid shift amount")(static_cast <bool> (ShiftAmt <= BitWidth &&
"Invalid shift amount") ? void (0) : __assert_fail ("ShiftAmt <= BitWidth && \"Invalid shift amount\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 922, __extension__ __PRETTY_FUNCTION__))
;
923 if (isSingleWord()) {
924 if (ShiftAmt == BitWidth)
925 U.VAL = 0;
926 else
927 U.VAL <<= ShiftAmt;
928 return clearUnusedBits();
929 }
930 shlSlowCase(ShiftAmt);
931 return *this;
932 }
933
934 /// Left-shift assignment function.
935 ///
936 /// Shifts *this left by shiftAmt and assigns the result to *this.
937 ///
938 /// \returns *this after shifting left by ShiftAmt
939 APInt &operator<<=(const APInt &ShiftAmt);
940
941 /// @}
942 /// \name Binary Operators
943 /// @{
944
945 /// Multiplication operator.
946 ///
947 /// Multiplies this APInt by RHS and returns the result.
948 APInt operator*(const APInt &RHS) const;
949
950 /// Left logical shift operator.
951 ///
952 /// Shifts this APInt left by \p Bits and returns the result.
953 APInt operator<<(unsigned Bits) const { return shl(Bits); }
954
955 /// Left logical shift operator.
956 ///
957 /// Shifts this APInt left by \p Bits and returns the result.
958 APInt operator<<(const APInt &Bits) const { return shl(Bits); }
959
960 /// Arithmetic right-shift function.
961 ///
962 /// Arithmetic right-shift this APInt by shiftAmt.
963 APInt ashr(unsigned ShiftAmt) const {
964 APInt R(*this);
965 R.ashrInPlace(ShiftAmt);
966 return R;
967 }
968
969 /// Arithmetic right-shift this APInt by ShiftAmt in place.
970 void ashrInPlace(unsigned ShiftAmt) {
971 assert(ShiftAmt <= BitWidth && "Invalid shift amount")(static_cast <bool> (ShiftAmt <= BitWidth &&
"Invalid shift amount") ? void (0) : __assert_fail ("ShiftAmt <= BitWidth && \"Invalid shift amount\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 971, __extension__ __PRETTY_FUNCTION__))
;
972 if (isSingleWord()) {
973 int64_t SExtVAL = SignExtend64(U.VAL, BitWidth);
974 if (ShiftAmt == BitWidth)
975 U.VAL = SExtVAL >> (APINT_BITS_PER_WORD - 1); // Fill with sign bit.
976 else
977 U.VAL = SExtVAL >> ShiftAmt;
978 clearUnusedBits();
979 return;
980 }
981 ashrSlowCase(ShiftAmt);
982 }
983
984 /// Logical right-shift function.
985 ///
986 /// Logical right-shift this APInt by shiftAmt.
987 APInt lshr(unsigned shiftAmt) const {
988 APInt R(*this);
989 R.lshrInPlace(shiftAmt);
37
Passing the value 64 via 1st parameter 'ShiftAmt'
38
Calling 'APInt::lshrInPlace'
990 return R;
991 }
992
993 /// Logical right-shift this APInt by ShiftAmt in place.
994 void lshrInPlace(unsigned ShiftAmt) {
995 assert(ShiftAmt <= BitWidth && "Invalid shift amount")(static_cast <bool> (ShiftAmt <= BitWidth &&
"Invalid shift amount") ? void (0) : __assert_fail ("ShiftAmt <= BitWidth && \"Invalid shift amount\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 995, __extension__ __PRETTY_FUNCTION__))
;
39
Assuming 'ShiftAmt' is <= field 'BitWidth'
40
'?' condition is true
996 if (isSingleWord()) {
41
Assuming the condition is true
42
Taking true branch
997 if (ShiftAmt == BitWidth)
43
Assuming 'ShiftAmt' is not equal to field 'BitWidth'
44
Taking false branch
998 U.VAL = 0;
999 else
1000 U.VAL >>= ShiftAmt;
45
Assigned value is garbage or undefined
1001 return;
1002 }
1003 lshrSlowCase(ShiftAmt);
1004 }
1005
1006 /// Left-shift function.
1007 ///
1008 /// Left-shift this APInt by shiftAmt.
1009 APInt shl(unsigned shiftAmt) const {
1010 APInt R(*this);
1011 R <<= shiftAmt;
1012 return R;
1013 }
1014
1015 /// Rotate left by rotateAmt.
1016 APInt rotl(unsigned rotateAmt) const;
1017
1018 /// Rotate right by rotateAmt.
1019 APInt rotr(unsigned rotateAmt) const;
1020
1021 /// Arithmetic right-shift function.
1022 ///
1023 /// Arithmetic right-shift this APInt by shiftAmt.
1024 APInt ashr(const APInt &ShiftAmt) const {
1025 APInt R(*this);
1026 R.ashrInPlace(ShiftAmt);
1027 return R;
1028 }
1029
1030 /// Arithmetic right-shift this APInt by shiftAmt in place.
1031 void ashrInPlace(const APInt &shiftAmt);
1032
1033 /// Logical right-shift function.
1034 ///
1035 /// Logical right-shift this APInt by shiftAmt.
1036 APInt lshr(const APInt &ShiftAmt) const {
1037 APInt R(*this);
1038 R.lshrInPlace(ShiftAmt);
1039 return R;
1040 }
1041
1042 /// Logical right-shift this APInt by ShiftAmt in place.
1043 void lshrInPlace(const APInt &ShiftAmt);
1044
1045 /// Left-shift function.
1046 ///
1047 /// Left-shift this APInt by shiftAmt.
1048 APInt shl(const APInt &ShiftAmt) const {
1049 APInt R(*this);
1050 R <<= ShiftAmt;
1051 return R;
1052 }
1053
1054 /// Rotate left by rotateAmt.
1055 APInt rotl(const APInt &rotateAmt) const;
1056
1057 /// Rotate right by rotateAmt.
1058 APInt rotr(const APInt &rotateAmt) const;
1059
1060 /// Unsigned division operation.
1061 ///
1062 /// Perform an unsigned divide operation on this APInt by RHS. Both this and
1063 /// RHS are treated as unsigned quantities for purposes of this division.
1064 ///
1065 /// \returns a new APInt value containing the division result, rounded towards
1066 /// zero.
1067 APInt udiv(const APInt &RHS) const;
1068 APInt udiv(uint64_t RHS) const;
1069
1070 /// Signed division function for APInt.
1071 ///
1072 /// Signed divide this APInt by APInt RHS.
1073 ///
1074 /// The result is rounded towards zero.
1075 APInt sdiv(const APInt &RHS) const;
1076 APInt sdiv(int64_t RHS) const;
1077
1078 /// Unsigned remainder operation.
1079 ///
1080 /// Perform an unsigned remainder operation on this APInt with RHS being the
1081 /// divisor. Both this and RHS are treated as unsigned quantities for purposes
1082 /// of this operation. Note that this is a true remainder operation and not a
1083 /// modulo operation because the sign follows the sign of the dividend which
1084 /// is *this.
1085 ///
1086 /// \returns a new APInt value containing the remainder result
1087 APInt urem(const APInt &RHS) const;
1088 uint64_t urem(uint64_t RHS) const;
1089
1090 /// Function for signed remainder operation.
1091 ///
1092 /// Signed remainder operation on APInt.
1093 APInt srem(const APInt &RHS) const;
1094 int64_t srem(int64_t RHS) const;
1095
1096 /// Dual division/remainder interface.
1097 ///
1098 /// Sometimes it is convenient to divide two APInt values and obtain both the
1099 /// quotient and remainder. This function does both operations in the same
1100 /// computation making it a little more efficient. The pair of input arguments
1101 /// may overlap with the pair of output arguments. It is safe to call
1102 /// udivrem(X, Y, X, Y), for example.
1103 static void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient,
1104 APInt &Remainder);
1105 static void udivrem(const APInt &LHS, uint64_t RHS, APInt &Quotient,
1106 uint64_t &Remainder);
1107
1108 static void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient,
1109 APInt &Remainder);
1110 static void sdivrem(const APInt &LHS, int64_t RHS, APInt &Quotient,
1111 int64_t &Remainder);
1112
1113 // Operations that return overflow indicators.
1114 APInt sadd_ov(const APInt &RHS, bool &Overflow) const;
1115 APInt uadd_ov(const APInt &RHS, bool &Overflow) const;
1116 APInt ssub_ov(const APInt &RHS, bool &Overflow) const;
1117 APInt usub_ov(const APInt &RHS, bool &Overflow) const;
1118 APInt sdiv_ov(const APInt &RHS, bool &Overflow) const;
1119 APInt smul_ov(const APInt &RHS, bool &Overflow) const;
1120 APInt umul_ov(const APInt &RHS, bool &Overflow) const;
1121 APInt sshl_ov(const APInt &Amt, bool &Overflow) const;
1122 APInt ushl_ov(const APInt &Amt, bool &Overflow) const;
1123
1124 // Operations that saturate
1125 APInt sadd_sat(const APInt &RHS) const;
1126 APInt uadd_sat(const APInt &RHS) const;
1127 APInt ssub_sat(const APInt &RHS) const;
1128 APInt usub_sat(const APInt &RHS) const;
1129 APInt smul_sat(const APInt &RHS) const;
1130 APInt umul_sat(const APInt &RHS) const;
1131 APInt sshl_sat(const APInt &RHS) const;
1132 APInt ushl_sat(const APInt &RHS) const;
1133
1134 /// Array-indexing support.
1135 ///
1136 /// \returns the bit value at bitPosition
1137 bool operator[](unsigned bitPosition) const {
1138 assert(bitPosition < getBitWidth() && "Bit position out of bounds!")(static_cast <bool> (bitPosition < getBitWidth() &&
"Bit position out of bounds!") ? void (0) : __assert_fail ("bitPosition < getBitWidth() && \"Bit position out of bounds!\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 1138, __extension__ __PRETTY_FUNCTION__))
;
1139 return (maskBit(bitPosition) & getWord(bitPosition)) != 0;
1140 }
1141
1142 /// @}
1143 /// \name Comparison Operators
1144 /// @{
1145
1146 /// Equality operator.
1147 ///
1148 /// Compares this APInt with RHS for the validity of the equality
1149 /// relationship.
1150 bool operator==(const APInt &RHS) const {
1151 assert(BitWidth == RHS.BitWidth && "Comparison requires equal bit widths")(static_cast <bool> (BitWidth == RHS.BitWidth &&
"Comparison requires equal bit widths") ? void (0) : __assert_fail
("BitWidth == RHS.BitWidth && \"Comparison requires equal bit widths\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 1151, __extension__ __PRETTY_FUNCTION__))
;
1152 if (isSingleWord())
1153 return U.VAL == RHS.U.VAL;
1154 return EqualSlowCase(RHS);
1155 }
1156
1157 /// Equality operator.
1158 ///
1159 /// Compares this APInt with a uint64_t for the validity of the equality
1160 /// relationship.
1161 ///
1162 /// \returns true if *this == Val
1163 bool operator==(uint64_t Val) const {
1164 return (isSingleWord() || getActiveBits() <= 64) && getZExtValue() == Val;
1165 }
1166
1167 /// Equality comparison.
1168 ///
1169 /// Compares this APInt with RHS for the validity of the equality
1170 /// relationship.
1171 ///
1172 /// \returns true if *this == Val
1173 bool eq(const APInt &RHS) const { return (*this) == RHS; }
1174
1175 /// Inequality operator.
1176 ///
1177 /// Compares this APInt with RHS for the validity of the inequality
1178 /// relationship.
1179 ///
1180 /// \returns true if *this != Val
1181 bool operator!=(const APInt &RHS) const { return !((*this) == RHS); }
1182
1183 /// Inequality operator.
1184 ///
1185 /// Compares this APInt with a uint64_t for the validity of the inequality
1186 /// relationship.
1187 ///
1188 /// \returns true if *this != Val
1189 bool operator!=(uint64_t Val) const { return !((*this) == Val); }
1190
1191 /// Inequality comparison
1192 ///
1193 /// Compares this APInt with RHS for the validity of the inequality
1194 /// relationship.
1195 ///
1196 /// \returns true if *this != Val
1197 bool ne(const APInt &RHS) const { return !((*this) == RHS); }
1198
1199 /// Unsigned less than comparison
1200 ///
1201 /// Regards both *this and RHS as unsigned quantities and compares them for
1202 /// the validity of the less-than relationship.
1203 ///
1204 /// \returns true if *this < RHS when both are considered unsigned.
1205 bool ult(const APInt &RHS) const { return compare(RHS) < 0; }
1206
1207 /// Unsigned less than comparison
1208 ///
1209 /// Regards both *this as an unsigned quantity and compares it with RHS for
1210 /// the validity of the less-than relationship.
1211 ///
1212 /// \returns true if *this < RHS when considered unsigned.
1213 bool ult(uint64_t RHS) const {
1214 // Only need to check active bits if not a single word.
1215 return (isSingleWord() || getActiveBits() <= 64) && getZExtValue() < RHS;
1216 }
1217
1218 /// Signed less than comparison
1219 ///
1220 /// Regards both *this and RHS as signed quantities and compares them for
1221 /// validity of the less-than relationship.
1222 ///
1223 /// \returns true if *this < RHS when both are considered signed.
1224 bool slt(const APInt &RHS) const { return compareSigned(RHS) < 0; }
1225
1226 /// Signed less than comparison
1227 ///
1228 /// Regards both *this as a signed quantity and compares it with RHS for
1229 /// the validity of the less-than relationship.
1230 ///
1231 /// \returns true if *this < RHS when considered signed.
1232 bool slt(int64_t RHS) const {
1233 return (!isSingleWord() && getMinSignedBits() > 64) ? isNegative()
1234 : getSExtValue() < RHS;
1235 }
1236
1237 /// Unsigned less or equal comparison
1238 ///
1239 /// Regards both *this and RHS as unsigned quantities and compares them for
1240 /// validity of the less-or-equal relationship.
1241 ///
1242 /// \returns true if *this <= RHS when both are considered unsigned.
1243 bool ule(const APInt &RHS) const { return compare(RHS) <= 0; }
1244
1245 /// Unsigned less or equal comparison
1246 ///
1247 /// Regards both *this as an unsigned quantity and compares it with RHS for
1248 /// the validity of the less-or-equal relationship.
1249 ///
1250 /// \returns true if *this <= RHS when considered unsigned.
1251 bool ule(uint64_t RHS) const { return !ugt(RHS); }
1252
1253 /// Signed less or equal comparison
1254 ///
1255 /// Regards both *this and RHS as signed quantities and compares them for
1256 /// validity of the less-or-equal relationship.
1257 ///
1258 /// \returns true if *this <= RHS when both are considered signed.
1259 bool sle(const APInt &RHS) const { return compareSigned(RHS) <= 0; }
1260
1261 /// Signed less or equal comparison
1262 ///
1263 /// Regards both *this as a signed quantity and compares it with RHS for the
1264 /// validity of the less-or-equal relationship.
1265 ///
1266 /// \returns true if *this <= RHS when considered signed.
1267 bool sle(uint64_t RHS) const { return !sgt(RHS); }
1268
1269 /// Unsigned greater than comparison
1270 ///
1271 /// Regards both *this and RHS as unsigned quantities and compares them for
1272 /// the validity of the greater-than relationship.
1273 ///
1274 /// \returns true if *this > RHS when both are considered unsigned.
1275 bool ugt(const APInt &RHS) const { return !ule(RHS); }
1276
1277 /// Unsigned greater than comparison
1278 ///
1279 /// Regards both *this as an unsigned quantity and compares it with RHS for
1280 /// the validity of the greater-than relationship.
1281 ///
1282 /// \returns true if *this > RHS when considered unsigned.
1283 bool ugt(uint64_t RHS) const {
1284 // Only need to check active bits if not a single word.
1285 return (!isSingleWord() && getActiveBits() > 64) || getZExtValue() > RHS;
1286 }
1287
1288 /// Signed greater than comparison
1289 ///
1290 /// Regards both *this and RHS as signed quantities and compares them for the
1291 /// validity of the greater-than relationship.
1292 ///
1293 /// \returns true if *this > RHS when both are considered signed.
1294 bool sgt(const APInt &RHS) const { return !sle(RHS); }
1295
1296 /// Signed greater than comparison
1297 ///
1298 /// Regards both *this as a signed quantity and compares it with RHS for
1299 /// the validity of the greater-than relationship.
1300 ///
1301 /// \returns true if *this > RHS when considered signed.
1302 bool sgt(int64_t RHS) const {
1303 return (!isSingleWord() && getMinSignedBits() > 64) ? !isNegative()
1304 : getSExtValue() > RHS;
1305 }
1306
1307 /// Unsigned greater or equal comparison
1308 ///
1309 /// Regards both *this and RHS as unsigned quantities and compares them for
1310 /// validity of the greater-or-equal relationship.
1311 ///
1312 /// \returns true if *this >= RHS when both are considered unsigned.
1313 bool uge(const APInt &RHS) const { return !ult(RHS); }
1314
1315 /// Unsigned greater or equal comparison
1316 ///
1317 /// Regards both *this as an unsigned quantity and compares it with RHS for
1318 /// the validity of the greater-or-equal relationship.
1319 ///
1320 /// \returns true if *this >= RHS when considered unsigned.
1321 bool uge(uint64_t RHS) const { return !ult(RHS); }
1322
1323 /// Signed greater or equal comparison
1324 ///
1325 /// Regards both *this and RHS as signed quantities and compares them for
1326 /// validity of the greater-or-equal relationship.
1327 ///
1328 /// \returns true if *this >= RHS when both are considered signed.
1329 bool sge(const APInt &RHS) const { return !slt(RHS); }
1330
1331 /// Signed greater or equal comparison
1332 ///
1333 /// Regards both *this as a signed quantity and compares it with RHS for
1334 /// the validity of the greater-or-equal relationship.
1335 ///
1336 /// \returns true if *this >= RHS when considered signed.
1337 bool sge(int64_t RHS) const { return !slt(RHS); }
1338
1339 /// This operation tests if there are any pairs of corresponding bits
1340 /// between this APInt and RHS that are both set.
1341 bool intersects(const APInt &RHS) const {
1342 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same")(static_cast <bool> (BitWidth == RHS.BitWidth &&
"Bit widths must be the same") ? void (0) : __assert_fail ("BitWidth == RHS.BitWidth && \"Bit widths must be the same\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 1342, __extension__ __PRETTY_FUNCTION__))
;
1343 if (isSingleWord())
1344 return (U.VAL & RHS.U.VAL) != 0;
1345 return intersectsSlowCase(RHS);
1346 }
1347
1348 /// This operation checks that all bits set in this APInt are also set in RHS.
1349 bool isSubsetOf(const APInt &RHS) const {
1350 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same")(static_cast <bool> (BitWidth == RHS.BitWidth &&
"Bit widths must be the same") ? void (0) : __assert_fail ("BitWidth == RHS.BitWidth && \"Bit widths must be the same\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 1350, __extension__ __PRETTY_FUNCTION__))
;
1351 if (isSingleWord())
1352 return (U.VAL & ~RHS.U.VAL) == 0;
1353 return isSubsetOfSlowCase(RHS);
1354 }
1355
1356 /// @}
1357 /// \name Resizing Operators
1358 /// @{
1359
1360 /// Truncate to new width.
1361 ///
1362 /// Truncate the APInt to a specified width. It is an error to specify a width
1363 /// that is greater than or equal to the current width.
1364 APInt trunc(unsigned width) const;
1365
1366 /// Truncate to new width with unsigned saturation.
1367 ///
1368 /// If the APInt, treated as unsigned integer, can be losslessly truncated to
1369 /// the new bitwidth, then return truncated APInt. Else, return max value.
1370 APInt truncUSat(unsigned width) const;
1371
1372 /// Truncate to new width with signed saturation.
1373 ///
1374 /// If this APInt, treated as signed integer, can be losslessly truncated to
1375 /// the new bitwidth, then return truncated APInt. Else, return either
1376 /// signed min value if the APInt was negative, or signed max value.
1377 APInt truncSSat(unsigned width) const;
1378
1379 /// Sign extend to a new width.
1380 ///
1381 /// This operation sign extends the APInt to a new width. If the high order
1382 /// bit is set, the fill on the left will be done with 1 bits, otherwise zero.
1383 /// It is an error to specify a width that is less than or equal to the
1384 /// current width.
1385 APInt sext(unsigned width) const;
1386
1387 /// Zero extend to a new width.
1388 ///
1389 /// This operation zero extends the APInt to a new width. The high order bits
1390 /// are filled with 0 bits. It is an error to specify a width that is less
1391 /// than or equal to the current width.
1392 APInt zext(unsigned width) const;
1393
1394 /// Sign extend or truncate to width
1395 ///
1396 /// Make this APInt have the bit width given by \p width. The value is sign
1397 /// extended, truncated, or left alone to make it that width.
1398 APInt sextOrTrunc(unsigned width) const;
1399
1400 /// Zero extend or truncate to width
1401 ///
1402 /// Make this APInt have the bit width given by \p width. The value is zero
1403 /// extended, truncated, or left alone to make it that width.
1404 APInt zextOrTrunc(unsigned width) const;
1405
1406 /// Truncate to width
1407 ///
1408 /// Make this APInt have the bit width given by \p width. The value is
1409 /// truncated or left alone to make it that width.
1410 APInt truncOrSelf(unsigned width) const;
1411
1412 /// Sign extend or truncate to width
1413 ///
1414 /// Make this APInt have the bit width given by \p width. The value is sign
1415 /// extended, or left alone to make it that width.
1416 APInt sextOrSelf(unsigned width) const;
1417
1418 /// Zero extend or truncate to width
1419 ///
1420 /// Make this APInt have the bit width given by \p width. The value is zero
1421 /// extended, or left alone to make it that width.
1422 APInt zextOrSelf(unsigned width) const;
1423
1424 /// @}
1425 /// \name Bit Manipulation Operators
1426 /// @{
1427
1428 /// Set every bit to 1.
1429 void setAllBits() {
1430 if (isSingleWord())
1431 U.VAL = WORDTYPE_MAX;
1432 else
1433 // Set all the bits in all the words.
1434 memset(U.pVal, -1, getNumWords() * APINT_WORD_SIZE);
1435 // Clear the unused ones
1436 clearUnusedBits();
1437 }
1438
1439 /// Set a given bit to 1.
1440 ///
1441 /// Set the given bit to 1 whose position is given as "bitPosition".
1442 void setBit(unsigned BitPosition) {
1443 assert(BitPosition < BitWidth && "BitPosition out of range")(static_cast <bool> (BitPosition < BitWidth &&
"BitPosition out of range") ? void (0) : __assert_fail ("BitPosition < BitWidth && \"BitPosition out of range\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 1443, __extension__ __PRETTY_FUNCTION__))
;
1444 WordType Mask = maskBit(BitPosition);
1445 if (isSingleWord())
1446 U.VAL |= Mask;
1447 else
1448 U.pVal[whichWord(BitPosition)] |= Mask;
1449 }
1450
1451 /// Set the sign bit to 1.
1452 void setSignBit() {
1453 setBit(BitWidth - 1);
1454 }
1455
1456 /// Set a given bit to a given value.
1457 void setBitVal(unsigned BitPosition, bool BitValue) {
1458 if (BitValue)
1459 setBit(BitPosition);
1460 else
1461 clearBit(BitPosition);
1462 }
1463
1464 /// Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
1465 /// This function handles "wrap" case when \p loBit >= \p hiBit, and calls
1466 /// setBits when \p loBit < \p hiBit.
1467 /// For \p loBit == \p hiBit wrap case, set every bit to 1.
1468 void setBitsWithWrap(unsigned loBit, unsigned hiBit) {
1469 assert(hiBit <= BitWidth && "hiBit out of range")(static_cast <bool> (hiBit <= BitWidth && "hiBit out of range"
) ? void (0) : __assert_fail ("hiBit <= BitWidth && \"hiBit out of range\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 1469, __extension__ __PRETTY_FUNCTION__))
;
1470 assert(loBit <= BitWidth && "loBit out of range")(static_cast <bool> (loBit <= BitWidth && "loBit out of range"
) ? void (0) : __assert_fail ("loBit <= BitWidth && \"loBit out of range\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 1470, __extension__ __PRETTY_FUNCTION__))
;
1471 if (loBit < hiBit) {
1472 setBits(loBit, hiBit);
1473 return;
1474 }
1475 setLowBits(hiBit);
1476 setHighBits(BitWidth - loBit);
1477 }
1478
1479 /// Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
1480 /// This function handles case when \p loBit <= \p hiBit.
1481 void setBits(unsigned loBit, unsigned hiBit) {
1482 assert(hiBit <= BitWidth && "hiBit out of range")(static_cast <bool> (hiBit <= BitWidth && "hiBit out of range"
) ? void (0) : __assert_fail ("hiBit <= BitWidth && \"hiBit out of range\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 1482, __extension__ __PRETTY_FUNCTION__))
;
1483 assert(loBit <= BitWidth && "loBit out of range")(static_cast <bool> (loBit <= BitWidth && "loBit out of range"
) ? void (0) : __assert_fail ("loBit <= BitWidth && \"loBit out of range\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 1483, __extension__ __PRETTY_FUNCTION__))
;
1484 assert(loBit <= hiBit && "loBit greater than hiBit")(static_cast <bool> (loBit <= hiBit && "loBit greater than hiBit"
) ? void (0) : __assert_fail ("loBit <= hiBit && \"loBit greater than hiBit\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 1484, __extension__ __PRETTY_FUNCTION__))
;
1485 if (loBit == hiBit)
1486 return;
1487 if (loBit < APINT_BITS_PER_WORD && hiBit <= APINT_BITS_PER_WORD) {
1488 uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - (hiBit - loBit));
1489 mask <<= loBit;
1490 if (isSingleWord())
1491 U.VAL |= mask;
1492 else
1493 U.pVal[0] |= mask;
1494 } else {
1495 setBitsSlowCase(loBit, hiBit);
1496 }
1497 }
1498
1499 /// Set the top bits starting from loBit.
1500 void setBitsFrom(unsigned loBit) {
1501 return setBits(loBit, BitWidth);
1502 }
1503
1504 /// Set the bottom loBits bits.
1505 void setLowBits(unsigned loBits) {
1506 return setBits(0, loBits);
1507 }
1508
1509 /// Set the top hiBits bits.
1510 void setHighBits(unsigned hiBits) {
1511 return setBits(BitWidth - hiBits, BitWidth);
1512 }
1513
1514 /// Set every bit to 0.
1515 void clearAllBits() {
1516 if (isSingleWord())
1517 U.VAL = 0;
1518 else
1519 memset(U.pVal, 0, getNumWords() * APINT_WORD_SIZE);
1520 }
1521
1522 /// Set a given bit to 0.
1523 ///
1524 /// Set the given bit to 0 whose position is given as "bitPosition".
1525 void clearBit(unsigned BitPosition) {
1526 assert(BitPosition < BitWidth && "BitPosition out of range")(static_cast <bool> (BitPosition < BitWidth &&
"BitPosition out of range") ? void (0) : __assert_fail ("BitPosition < BitWidth && \"BitPosition out of range\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 1526, __extension__ __PRETTY_FUNCTION__))
;
1527 WordType Mask = ~maskBit(BitPosition);
1528 if (isSingleWord())
1529 U.VAL &= Mask;
1530 else
1531 U.pVal[whichWord(BitPosition)] &= Mask;
1532 }
1533
1534 /// Set bottom loBits bits to 0.
1535 void clearLowBits(unsigned loBits) {
1536 assert(loBits <= BitWidth && "More bits than bitwidth")(static_cast <bool> (loBits <= BitWidth && "More bits than bitwidth"
) ? void (0) : __assert_fail ("loBits <= BitWidth && \"More bits than bitwidth\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 1536, __extension__ __PRETTY_FUNCTION__))
;
1537 APInt Keep = getHighBitsSet(BitWidth, BitWidth - loBits);
1538 *this &= Keep;
1539 }
1540
1541 /// Set the sign bit to 0.
1542 void clearSignBit() {
1543 clearBit(BitWidth - 1);
1544 }
1545
1546 /// Toggle every bit to its opposite value.
1547 void flipAllBits() {
1548 if (isSingleWord()) {
1549 U.VAL ^= WORDTYPE_MAX;
1550 clearUnusedBits();
1551 } else {
1552 flipAllBitsSlowCase();
1553 }
1554 }
1555
1556 /// Toggles a given bit to its opposite value.
1557 ///
1558 /// Toggle a given bit to its opposite value whose position is given
1559 /// as "bitPosition".
1560 void flipBit(unsigned bitPosition);
1561
1562 /// Negate this APInt in place.
1563 void negate() {
1564 flipAllBits();
1565 ++(*this);
1566 }
1567
1568 /// Insert the bits from a smaller APInt starting at bitPosition.
1569 void insertBits(const APInt &SubBits, unsigned bitPosition);
1570 void insertBits(uint64_t SubBits, unsigned bitPosition, unsigned numBits);
1571
1572 /// Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
1573 APInt extractBits(unsigned numBits, unsigned bitPosition) const;
1574 uint64_t extractBitsAsZExtValue(unsigned numBits, unsigned bitPosition) const;
1575
1576 /// @}
1577 /// \name Value Characterization Functions
1578 /// @{
1579
1580 /// Return the number of bits in the APInt.
1581 unsigned getBitWidth() const { return BitWidth; }
1582
1583 /// Get the number of words.
1584 ///
1585 /// Here one word's bitwidth equals to that of uint64_t.
1586 ///
1587 /// \returns the number of words to hold the integer value of this APInt.
1588 unsigned getNumWords() const { return getNumWords(BitWidth); }
1589
1590 /// Get the number of words.
1591 ///
1592 /// *NOTE* Here one word's bitwidth equals to that of uint64_t.
1593 ///
1594 /// \returns the number of words to hold the integer value with a given bit
1595 /// width.
1596 static unsigned getNumWords(unsigned BitWidth) {
1597 return ((uint64_t)BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
1598 }
1599
1600 /// Compute the number of active bits in the value
1601 ///
1602 /// This function returns the number of active bits which is defined as the
1603 /// bit width minus the number of leading zeros. This is used in several
1604 /// computations to see how "wide" the value is.
1605 unsigned getActiveBits() const { return BitWidth - countLeadingZeros(); }
1606
1607 /// Compute the number of active words in the value of this APInt.
1608 ///
1609 /// This is used in conjunction with getActiveData to extract the raw value of
1610 /// the APInt.
1611 unsigned getActiveWords() const {
1612 unsigned numActiveBits = getActiveBits();
1613 return numActiveBits ? whichWord(numActiveBits - 1) + 1 : 1;
1614 }
1615
1616 /// Get the minimum bit size for this signed APInt
1617 ///
1618 /// Computes the minimum bit width for this APInt while considering it to be a
1619 /// signed (and probably negative) value. If the value is not negative, this
1620 /// function returns the same value as getActiveBits()+1. Otherwise, it
1621 /// returns the smallest bit width that will retain the negative value. For
1622 /// example, -1 can be written as 0b1 or 0xFFFFFFFFFF. 0b1 is shorter and so
1623 /// for -1, this function will always return 1.
1624 unsigned getMinSignedBits() const { return BitWidth - getNumSignBits() + 1; }
1625
1626 /// Get zero extended value
1627 ///
1628 /// This method attempts to return the value of this APInt as a zero extended
1629 /// uint64_t. The bitwidth must be <= 64 or the value must fit within a
1630 /// uint64_t. Otherwise an assertion will result.
1631 uint64_t getZExtValue() const {
1632 if (isSingleWord())
1633 return U.VAL;
1634 assert(getActiveBits() <= 64 && "Too many bits for uint64_t")(static_cast <bool> (getActiveBits() <= 64 &&
"Too many bits for uint64_t") ? void (0) : __assert_fail ("getActiveBits() <= 64 && \"Too many bits for uint64_t\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 1634, __extension__ __PRETTY_FUNCTION__))
;
1635 return U.pVal[0];
1636 }
1637
1638 /// Get sign extended value
1639 ///
1640 /// This method attempts to return the value of this APInt as a sign extended
1641 /// int64_t. The bit width must be <= 64 or the value must fit within an
1642 /// int64_t. Otherwise an assertion will result.
1643 int64_t getSExtValue() const {
1644 if (isSingleWord())
1645 return SignExtend64(U.VAL, BitWidth);
1646 assert(getMinSignedBits() <= 64 && "Too many bits for int64_t")(static_cast <bool> (getMinSignedBits() <= 64 &&
"Too many bits for int64_t") ? void (0) : __assert_fail ("getMinSignedBits() <= 64 && \"Too many bits for int64_t\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/ADT/APInt.h"
, 1646, __extension__ __PRETTY_FUNCTION__))
;
1647 return int64_t(U.pVal[0]);
1648 }
1649
1650 /// Get bits required for string value.
1651 ///
1652 /// This method determines how many bits are required to hold the APInt
1653 /// equivalent of the string given by \p str.
1654 static unsigned getBitsNeeded(StringRef str, uint8_t radix);
1655
1656 /// The APInt version of the countLeadingZeros functions in
1657 /// MathExtras.h.
1658 ///
1659 /// It counts the number of zeros from the most significant bit to the first
1660 /// one bit.
1661 ///
1662 /// \returns BitWidth if the value is zero, otherwise returns the number of
1663 /// zeros from the most significant bit to the first one bits.
1664 unsigned countLeadingZeros() const {
1665 if (isSingleWord()) {
1666 unsigned unusedBits = APINT_BITS_PER_WORD - BitWidth;
1667 return llvm::countLeadingZeros(U.VAL) - unusedBits;
1668 }
1669 return countLeadingZerosSlowCase();
1670 }
1671
1672 /// Count the number of leading one bits.
1673 ///
1674 /// This function is an APInt version of the countLeadingOnes
1675 /// functions in MathExtras.h. It counts the number of ones from the most
1676 /// significant bit to the first zero bit.
1677 ///
1678 /// \returns 0 if the high order bit is not set, otherwise returns the number
1679 /// of 1 bits from the most significant to the least
1680 unsigned countLeadingOnes() const {
1681 if (isSingleWord())
1682 return llvm::countLeadingOnes(U.VAL << (APINT_BITS_PER_WORD - BitWidth));
1683 return countLeadingOnesSlowCase();
1684 }
1685
1686 /// Computes the number of leading bits of this APInt that are equal to its
1687 /// sign bit.
1688 unsigned getNumSignBits() const {
1689 return isNegative() ? countLeadingOnes() : countLeadingZeros();
1690 }
1691
1692 /// Count the number of trailing zero bits.
1693 ///
1694 /// This function is an APInt version of the countTrailingZeros
1695 /// functions in MathExtras.h. It counts the number of zeros from the least
1696 /// significant bit to the first set bit.
1697 ///
1698 /// \returns BitWidth if the value is zero, otherwise returns the number of
1699 /// zeros from the least significant bit to the first one bit.
1700 unsigned countTrailingZeros() const {
1701 if (isSingleWord()) {
20
Assuming the condition is true
21
Taking true branch
1702 unsigned TrailingZeros = llvm::countTrailingZeros(U.VAL);
22
Calling 'countTrailingZeros<unsigned long>'
29
Returning from 'countTrailingZeros<unsigned long>'
30
'TrailingZeros' initialized to 64
1703 return (TrailingZeros > BitWidth ? BitWidth : TrailingZeros);
31
Assuming 'TrailingZeros' is <= field 'BitWidth'
32
'?' condition is false
33
Returning the value 64
1704 }
1705 return countTrailingZerosSlowCase();
1706 }
1707
1708 /// Count the number of trailing one bits.
1709 ///
1710 /// This function is an APInt version of the countTrailingOnes
1711 /// functions in MathExtras.h. It counts the number of ones from the least
1712 /// significant bit to the first zero bit.
1713 ///
1714 /// \returns BitWidth if the value is all ones, otherwise returns the number
1715 /// of ones from the least significant bit to the first zero bit.
1716 unsigned countTrailingOnes() const {
1717 if (isSingleWord())
1718 return llvm::countTrailingOnes(U.VAL);
1719 return countTrailingOnesSlowCase();
1720 }
1721
1722 /// Count the number of bits set.
1723 ///
1724 /// This function is an APInt version of the countPopulation functions
1725 /// in MathExtras.h. It counts the number of 1 bits in the APInt value.
1726 ///
1727 /// \returns 0 if the value is zero, otherwise returns the number of set bits.
1728 unsigned countPopulation() const {
1729 if (isSingleWord())
1730 return llvm::countPopulation(U.VAL);
1731 return countPopulationSlowCase();
1732 }
1733
1734 /// @}
1735 /// \name Conversion Functions
1736 /// @{
1737 void print(raw_ostream &OS, bool isSigned) const;
1738
1739 /// Converts an APInt to a string and append it to Str. Str is commonly a
1740 /// SmallString.
1741 void toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed,
1742 bool formatAsCLiteral = false) const;
1743
1744 /// Considers the APInt to be unsigned and converts it into a string in the
1745 /// radix given. The radix can be 2, 8, 10 16, or 36.
1746 void toStringUnsigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
1747 toString(Str, Radix, false, false);
1748 }
1749
1750 /// Considers the APInt to be signed and converts it into a string in the
1751 /// radix given. The radix can be 2, 8, 10, 16, or 36.
1752 void toStringSigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
1753 toString(Str, Radix, true, false);
1754 }
1755
1756 /// \returns a byte-swapped representation of this APInt Value.
1757 APInt byteSwap() const;
1758
1759 /// \returns the value with the bit representation reversed of this APInt
1760 /// Value.
1761 APInt reverseBits() const;
1762
1763 /// Converts this APInt to a double value.
1764 double roundToDouble(bool isSigned) const;
1765
1766 /// Converts this unsigned APInt to a double value.
1767 double roundToDouble() const { return roundToDouble(false); }
1768
1769 /// Converts this signed APInt to a double value.
1770 double signedRoundToDouble() const { return roundToDouble(true); }
1771
1772 /// Converts APInt bits to a double
1773 ///
1774 /// The conversion does not do a translation from integer to double, it just
1775 /// re-interprets the bits as a double. Note that it is valid to do this on
1776 /// any bit width. Exactly 64 bits will be translated.
1777 double bitsToDouble() const {
1778 return BitsToDouble(getWord(0));
1779 }
1780
1781 /// Converts APInt bits to a float
1782 ///
1783 /// The conversion does not do a translation from integer to float, it just
1784 /// re-interprets the bits as a float. Note that it is valid to do this on
1785 /// any bit width. Exactly 32 bits will be translated.
1786 float bitsToFloat() const {
1787 return BitsToFloat(static_cast<uint32_t>(getWord(0)));
1788 }
1789
1790 /// Converts a double to APInt bits.
1791 ///
1792 /// The conversion does not do a translation from double to integer, it just
1793 /// re-interprets the bits of the double.
1794 static APInt doubleToBits(double V) {
1795 return APInt(sizeof(double) * CHAR_BIT8, DoubleToBits(V));
1796 }
1797
1798 /// Converts a float to APInt bits.
1799 ///
1800 /// The conversion does not do a translation from float to integer, it just
1801 /// re-interprets the bits of the float.
1802 static APInt floatToBits(float V) {
1803 return APInt(sizeof(float) * CHAR_BIT8, FloatToBits(V));
1804 }
1805
1806 /// @}
1807 /// \name Mathematics Operations
1808 /// @{
1809
1810 /// \returns the floor log base 2 of this APInt.
1811 unsigned logBase2() const { return getActiveBits() - 1; }
1812
1813 /// \returns the ceil log base 2 of this APInt.
1814 unsigned ceilLogBase2() const {
1815 APInt temp(*this);
1816 --temp;
1817 return temp.getActiveBits();
1818 }
1819
1820 /// \returns the nearest log base 2 of this APInt. Ties round up.
1821 ///
1822 /// NOTE: When we have a BitWidth of 1, we define:
1823 ///
1824 /// log2(0) = UINT32_MAX
1825 /// log2(1) = 0
1826 ///
1827 /// to get around any mathematical concerns resulting from
1828 /// referencing 2 in a space where 2 does no exist.
1829 unsigned nearestLogBase2() const {
1830 // Special case when we have a bitwidth of 1. If VAL is 1, then we
1831 // get 0. If VAL is 0, we get WORDTYPE_MAX which gets truncated to
1832 // UINT32_MAX.
1833 if (BitWidth == 1)
1834 return U.VAL - 1;
1835
1836 // Handle the zero case.
1837 if (isNullValue())
1838 return UINT32_MAX(4294967295U);
1839
1840 // The non-zero case is handled by computing:
1841 //
1842 // nearestLogBase2(x) = logBase2(x) + x[logBase2(x)-1].
1843 //
1844 // where x[i] is referring to the value of the ith bit of x.
1845 unsigned lg = logBase2();
1846 return lg + unsigned((*this)[lg - 1]);
1847 }
1848
1849 /// \returns the log base 2 of this APInt if its an exact power of two, -1
1850 /// otherwise
1851 int32_t exactLogBase2() const {
1852 if (!isPowerOf2())
1853 return -1;
1854 return logBase2();
1855 }
1856
1857 /// Compute the square root
1858 APInt sqrt() const;
1859
1860 /// Get the absolute value;
1861 ///
1862 /// If *this is < 0 then return -(*this), otherwise *this;
1863 APInt abs() const {
1864 if (isNegative())
1865 return -(*this);
1866 return *this;
1867 }
1868
1869 /// \returns the multiplicative inverse for a given modulo.
1870 APInt multiplicativeInverse(const APInt &modulo) const;
1871
1872 /// @}
1873 /// \name Support for division by constant
1874 /// @{
1875
1876 /// Calculate the magic number for signed division by a constant.
1877 struct ms;
1878 ms magic() const;
1879
1880 /// Calculate the magic number for unsigned division by a constant.
1881 struct mu;
1882 mu magicu(unsigned LeadingZeros = 0) const;
1883
1884 /// @}
1885 /// \name Building-block Operations for APInt and APFloat
1886 /// @{
1887
1888 // These building block operations operate on a representation of arbitrary
1889 // precision, two's-complement, bignum integer values. They should be
1890 // sufficient to implement APInt and APFloat bignum requirements. Inputs are
1891 // generally a pointer to the base of an array of integer parts, representing
1892 // an unsigned bignum, and a count of how many parts there are.
1893
1894 /// Sets the least significant part of a bignum to the input value, and zeroes
1895 /// out higher parts.
1896 static void tcSet(WordType *, WordType, unsigned);
1897
1898 /// Assign one bignum to another.
1899 static void tcAssign(WordType *, const WordType *, unsigned);
1900
1901 /// Returns true if a bignum is zero, false otherwise.
1902 static bool tcIsZero(const WordType *, unsigned);
1903
1904 /// Extract the given bit of a bignum; returns 0 or 1. Zero-based.
1905 static int tcExtractBit(const WordType *, unsigned bit);
1906
1907 /// Copy the bit vector of width srcBITS from SRC, starting at bit srcLSB, to
1908 /// DST, of dstCOUNT parts, such that the bit srcLSB becomes the least
1909 /// significant bit of DST. All high bits above srcBITS in DST are
1910 /// zero-filled.
1911 static void tcExtract(WordType *, unsigned dstCount,
1912 const WordType *, unsigned srcBits,
1913 unsigned srcLSB);
1914
1915 /// Set the given bit of a bignum. Zero-based.
1916 static void tcSetBit(WordType *, unsigned bit);
1917
1918 /// Clear the given bit of a bignum. Zero-based.
1919 static void tcClearBit(WordType *, unsigned bit);
1920
1921 /// Returns the bit number of the least or most significant set bit of a
1922 /// number. If the input number has no bits set -1U is returned.
1923 static unsigned tcLSB(const WordType *, unsigned n);
1924 static unsigned tcMSB(const WordType *parts, unsigned n);
1925
1926 /// Negate a bignum in-place.
1927 static void tcNegate(WordType *, unsigned);
1928
1929 /// DST += RHS + CARRY where CARRY is zero or one. Returns the carry flag.
1930 static WordType tcAdd(WordType *, const WordType *,
1931 WordType carry, unsigned);
1932 /// DST += RHS. Returns the carry flag.
1933 static WordType tcAddPart(WordType *, WordType, unsigned);
1934
1935 /// DST -= RHS + CARRY where CARRY is zero or one. Returns the carry flag.
1936 static WordType tcSubtract(WordType *, const WordType *,
1937 WordType carry, unsigned);
1938 /// DST -= RHS. Returns the carry flag.
1939 static WordType tcSubtractPart(WordType *, WordType, unsigned);
1940
1941 /// DST += SRC * MULTIPLIER + PART if add is true
1942 /// DST = SRC * MULTIPLIER + PART if add is false
1943 ///
1944 /// Requires 0 <= DSTPARTS <= SRCPARTS + 1. If DST overlaps SRC they must
1945 /// start at the same point, i.e. DST == SRC.
1946 ///
1947 /// If DSTPARTS == SRC_PARTS + 1 no overflow occurs and zero is returned.
1948 /// Otherwise DST is filled with the least significant DSTPARTS parts of the
1949 /// result, and if all of the omitted higher parts were zero return zero,
1950 /// otherwise overflow occurred and return one.
1951 static int tcMultiplyPart(WordType *dst, const WordType *src,
1952 WordType multiplier, WordType carry,
1953 unsigned srcParts, unsigned dstParts,
1954 bool add);
1955
1956 /// DST = LHS * RHS, where DST has the same width as the operands and is
1957 /// filled with the least significant parts of the result. Returns one if
1958 /// overflow occurred, otherwise zero. DST must be disjoint from both
1959 /// operands.
1960 static int tcMultiply(WordType *, const WordType *, const WordType *,
1961 unsigned);
1962
1963 /// DST = LHS * RHS, where DST has width the sum of the widths of the
1964 /// operands. No overflow occurs. DST must be disjoint from both operands.
1965 static void tcFullMultiply(WordType *, const WordType *,
1966 const WordType *, unsigned, unsigned);
1967
1968 /// If RHS is zero LHS and REMAINDER are left unchanged, return one.
1969 /// Otherwise set LHS to LHS / RHS with the fractional part discarded, set
1970 /// REMAINDER to the remainder, return zero. i.e.
1971 ///
1972 /// OLD_LHS = RHS * LHS + REMAINDER
1973 ///
1974 /// SCRATCH is a bignum of the same size as the operands and result for use by
1975 /// the routine; its contents need not be initialized and are destroyed. LHS,
1976 /// REMAINDER and SCRATCH must be distinct.
1977 static int tcDivide(WordType *lhs, const WordType *rhs,
1978 WordType *remainder, WordType *scratch,
1979 unsigned parts);
1980
1981 /// Shift a bignum left Count bits. Shifted in bits are zero. There are no
1982 /// restrictions on Count.
1983 static void tcShiftLeft(WordType *, unsigned Words, unsigned Count);
1984
1985 /// Shift a bignum right Count bits. Shifted in bits are zero. There are no
1986 /// restrictions on Count.
1987 static void tcShiftRight(WordType *, unsigned Words, unsigned Count);
1988
1989 /// The obvious AND, OR and XOR and complement operations.
1990 static void tcAnd(WordType *, const WordType *, unsigned);
1991 static void tcOr(WordType *, const WordType *, unsigned);
1992 static void tcXor(WordType *, const WordType *, unsigned);
1993 static void tcComplement(WordType *, unsigned);
1994
1995 /// Comparison (unsigned) of two bignums.
1996 static int tcCompare(const WordType *, const WordType *, unsigned);
1997
1998 /// Increment a bignum in-place. Return the carry flag.
1999 static WordType tcIncrement(WordType *dst, unsigned parts) {
2000 return tcAddPart(dst, 1, parts);
2001 }
2002
2003 /// Decrement a bignum in-place. Return the borrow flag.
2004 static WordType tcDecrement(WordType *dst, unsigned parts) {
2005 return tcSubtractPart(dst, 1, parts);
2006 }
2007
2008 /// Set the least significant BITS and clear the rest.
2009 static void tcSetLeastSignificantBits(WordType *, unsigned, unsigned bits);
2010
2011 /// debug method
2012 void dump() const;
2013
2014 /// @}
2015};
2016
2017/// Magic data for optimising signed division by a constant.
2018struct APInt::ms {
2019 APInt m; ///< magic number
2020 unsigned s; ///< shift amount
2021};
2022
2023/// Magic data for optimising unsigned division by a constant.
2024struct APInt::mu {
2025 APInt m; ///< magic number
2026 bool a; ///< add indicator
2027 unsigned s; ///< shift amount
2028};
2029
2030inline bool operator==(uint64_t V1, const APInt &V2) { return V2 == V1; }
2031
2032inline bool operator!=(uint64_t V1, const APInt &V2) { return V2 != V1; }
2033
2034/// Unary bitwise complement operator.
2035///
2036/// \returns an APInt that is the bitwise complement of \p v.
2037inline APInt operator~(APInt v) {
2038 v.flipAllBits();
2039 return v;
2040}
2041
2042inline APInt operator&(APInt a, const APInt &b) {
2043 a &= b;
2044 return a;
2045}
2046
2047inline APInt operator&(const APInt &a, APInt &&b) {
2048 b &= a;
2049 return std::move(b);
2050}
2051
2052inline APInt operator&(APInt a, uint64_t RHS) {
2053 a &= RHS;
2054 return a;
2055}
2056
2057inline APInt operator&(uint64_t LHS, APInt b) {
2058 b &= LHS;
2059 return b;
2060}
2061
2062inline APInt operator|(APInt a, const APInt &b) {
2063 a |= b;
2064 return a;
2065}
2066
2067inline APInt operator|(const APInt &a, APInt &&b) {
2068 b |= a;
2069 return std::move(b);
2070}
2071
2072inline APInt operator|(APInt a, uint64_t RHS) {
2073 a |= RHS;
2074 return a;
2075}
2076
2077inline APInt operator|(uint64_t LHS, APInt b) {
2078 b |= LHS;
2079 return b;
2080}
2081
2082inline APInt operator^(APInt a, const APInt &b) {
2083 a ^= b;
2084 return a;
2085}
2086
2087inline APInt operator^(const APInt &a, APInt &&b) {
2088 b ^= a;
2089 return std::move(b);
2090}
2091
2092inline APInt operator^(APInt a, uint64_t RHS) {
2093 a ^= RHS;
2094 return a;
2095}
2096
2097inline APInt operator^(uint64_t LHS, APInt b) {
2098 b ^= LHS;
2099 return b;
2100}
2101
2102inline raw_ostream &operator<<(raw_ostream &OS, const APInt &I) {
2103 I.print(OS, true);
2104 return OS;
2105}
2106
2107inline APInt operator-(APInt v) {
2108 v.negate();
2109 return v;
2110}
2111
2112inline APInt operator+(APInt a, const APInt &b) {
2113 a += b;
2114 return a;
2115}
2116
2117inline APInt operator+(const APInt &a, APInt &&b) {
2118 b += a;
2119 return std::move(b);
2120}
2121
2122inline APInt operator+(APInt a, uint64_t RHS) {
2123 a += RHS;
2124 return a;
2125}
2126
2127inline APInt operator+(uint64_t LHS, APInt b) {
2128 b += LHS;
2129 return b;
2130}
2131
2132inline APInt operator-(APInt a, const APInt &b) {
2133 a -= b;
2134 return a;
2135}
2136
2137inline APInt operator-(const APInt &a, APInt &&b) {
2138 b.negate();
2139 b += a;
2140 return std::move(b);
2141}
2142
2143inline APInt operator-(APInt a, uint64_t RHS) {
2144 a -= RHS;
2145 return a;
2146}
2147
2148inline APInt operator-(uint64_t LHS, APInt b) {
2149 b.negate();
2150 b += LHS;
2151 return b;
2152}
2153
2154inline APInt operator*(APInt a, uint64_t RHS) {
2155 a *= RHS;
2156 return a;
2157}
2158
2159inline APInt operator*(uint64_t LHS, APInt b) {
2160 b *= LHS;
2161 return b;
2162}
2163
2164
2165namespace APIntOps {
2166
2167/// Determine the smaller of two APInts considered to be signed.
2168inline const APInt &smin(const APInt &A, const APInt &B) {
2169 return A.slt(B) ? A : B;
2170}
2171
2172/// Determine the larger of two APInts considered to be signed.
2173inline const APInt &smax(const APInt &A, const APInt &B) {
2174 return A.sgt(B) ? A : B;
2175}
2176
2177/// Determine the smaller of two APInts considered to be unsigned.
2178inline const APInt &umin(const APInt &A, const APInt &B) {
2179 return A.ult(B) ? A : B;
2180}
2181
2182/// Determine the larger of two APInts considered to be unsigned.
2183inline const APInt &umax(const APInt &A, const APInt &B) {
2184 return A.ugt(B) ? A : B;
2185}
2186
2187/// Compute GCD of two unsigned APInt values.
2188///
2189/// This function returns the greatest common divisor of the two APInt values
2190/// using Stein's algorithm.
2191///
2192/// \returns the greatest common divisor of A and B.
2193APInt GreatestCommonDivisor(APInt A, APInt B);
2194
2195/// Converts the given APInt to a double value.
2196///
2197/// Treats the APInt as an unsigned value for conversion purposes.
2198inline double RoundAPIntToDouble(const APInt &APIVal) {
2199 return APIVal.roundToDouble();
2200}
2201
2202/// Converts the given APInt to a double value.
2203///
2204/// Treats the APInt as a signed value for conversion purposes.
2205inline double RoundSignedAPIntToDouble(const APInt &APIVal) {
2206 return APIVal.signedRoundToDouble();
2207}
2208
2209/// Converts the given APInt to a float value.
2210inline float RoundAPIntToFloat(const APInt &APIVal) {
2211 return float(RoundAPIntToDouble(APIVal));
2212}
2213
2214/// Converts the given APInt to a float value.
2215///
2216/// Treats the APInt as a signed value for conversion purposes.
2217inline float RoundSignedAPIntToFloat(const APInt &APIVal) {
2218 return float(APIVal.signedRoundToDouble());
2219}
2220
2221/// Converts the given double value into a APInt.
2222///
2223/// This function convert a double value to an APInt value.
2224APInt RoundDoubleToAPInt(double Double, unsigned width);
2225
2226/// Converts a float value into a APInt.
2227///
2228/// Converts a float value into an APInt value.
2229inline APInt RoundFloatToAPInt(float Float, unsigned width) {
2230 return RoundDoubleToAPInt(double(Float), width);
2231}
2232
2233/// Return A unsign-divided by B, rounded by the given rounding mode.
2234APInt RoundingUDiv(const APInt &A, const APInt &B, APInt::Rounding RM);
2235
2236/// Return A sign-divided by B, rounded by the given rounding mode.
2237APInt RoundingSDiv(const APInt &A, const APInt &B, APInt::Rounding RM);
2238
2239/// Let q(n) = An^2 + Bn + C, and BW = bit width of the value range
2240/// (e.g. 32 for i32).
2241/// This function finds the smallest number n, such that
2242/// (a) n >= 0 and q(n) = 0, or
2243/// (b) n >= 1 and q(n-1) and q(n), when evaluated in the set of all
2244/// integers, belong to two different intervals [Rk, Rk+R),
2245/// where R = 2^BW, and k is an integer.
2246/// The idea here is to find when q(n) "overflows" 2^BW, while at the
2247/// same time "allowing" subtraction. In unsigned modulo arithmetic a
2248/// subtraction (treated as addition of negated numbers) would always
2249/// count as an overflow, but here we want to allow values to decrease
2250/// and increase as long as they are within the same interval.
2251/// Specifically, adding of two negative numbers should not cause an
2252/// overflow (as long as the magnitude does not exceed the bit width).
2253/// On the other hand, given a positive number, adding a negative
2254/// number to it can give a negative result, which would cause the
2255/// value to go from [-2^BW, 0) to [0, 2^BW). In that sense, zero is
2256/// treated as a special case of an overflow.
2257///
2258/// This function returns None if after finding k that minimizes the
2259/// positive solution to q(n) = kR, both solutions are contained between
2260/// two consecutive integers.
2261///
2262/// There are cases where q(n) > T, and q(n+1) < T (assuming evaluation
2263/// in arithmetic modulo 2^BW, and treating the values as signed) by the
2264/// virtue of *signed* overflow. This function will *not* find such an n,
2265/// however it may find a value of n satisfying the inequalities due to
2266/// an *unsigned* overflow (if the values are treated as unsigned).
2267/// To find a solution for a signed overflow, treat it as a problem of
2268/// finding an unsigned overflow with a range with of BW-1.
2269///
2270/// The returned value may have a different bit width from the input
2271/// coefficients.
2272Optional<APInt> SolveQuadraticEquationWrap(APInt A, APInt B, APInt C,
2273 unsigned RangeWidth);
2274
2275/// Compare two values, and if they are different, return the position of the
2276/// most significant bit that is different in the values.
2277Optional<unsigned> GetMostSignificantDifferentBit(const APInt &A,
2278 const APInt &B);
2279
2280} // End of APIntOps namespace
2281
2282// See friend declaration above. This additional declaration is required in
2283// order to compile LLVM with IBM xlC compiler.
2284hash_code hash_value(const APInt &Arg);
2285
2286/// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
2287/// with the integer held in IntVal.
2288void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst, unsigned StoreBytes);
2289
2290/// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
2291/// from Src into IntVal, which is assumed to be wide enough and to hold zero.
2292void LoadIntFromMemory(APInt &IntVal, const uint8_t *Src, unsigned LoadBytes);
2293
2294/// Provide DenseMapInfo for APInt.
2295template <> struct DenseMapInfo<APInt> {
2296 static inline APInt getEmptyKey() {
2297 APInt V(nullptr, 0);
2298 V.U.VAL = 0;
2299 return V;
2300 }
2301
2302 static inline APInt getTombstoneKey() {
2303 APInt V(nullptr, 0);
2304 V.U.VAL = 1;
2305 return V;
2306 }
2307
2308 static unsigned getHashValue(const APInt &Key);
2309
2310 static bool isEqual(const APInt &LHS, const APInt &RHS) {
2311 return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS;
2312 }
2313};
2314
2315} // namespace llvm
2316
2317#endif

/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/Support/MathExtras.h

1//===-- llvm/Support/MathExtras.h - Useful math functions -------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains some functions that are useful for math stuff.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_SUPPORT_MATHEXTRAS_H
14#define LLVM_SUPPORT_MATHEXTRAS_H
15
16#include "llvm/Support/Compiler.h"
17#include <cassert>
18#include <climits>
19#include <cmath>
20#include <cstdint>
21#include <cstring>
22#include <limits>
23#include <type_traits>
24
25#ifdef __ANDROID_NDK__
26#include <android/api-level.h>
27#endif
28
29#ifdef _MSC_VER
30// Declare these intrinsics manually rather including intrin.h. It's very
31// expensive, and MathExtras.h is popular.
32// #include <intrin.h>
33extern "C" {
34unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask);
35unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask);
36unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask);
37unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask);
38}
39#endif
40
41namespace llvm {
42
43/// The behavior an operation has on an input of 0.
44enum ZeroBehavior {
45 /// The returned value is undefined.
46 ZB_Undefined,
47 /// The returned value is numeric_limits<T>::max()
48 ZB_Max,
49 /// The returned value is numeric_limits<T>::digits
50 ZB_Width
51};
52
53/// Mathematical constants.
54namespace numbers {
55// TODO: Track C++20 std::numbers.
56// TODO: Favor using the hexadecimal FP constants (requires C++17).
57constexpr double e = 2.7182818284590452354, // (0x1.5bf0a8b145749P+1) https://oeis.org/A001113
58 egamma = .57721566490153286061, // (0x1.2788cfc6fb619P-1) https://oeis.org/A001620
59 ln2 = .69314718055994530942, // (0x1.62e42fefa39efP-1) https://oeis.org/A002162
60 ln10 = 2.3025850929940456840, // (0x1.24bb1bbb55516P+1) https://oeis.org/A002392
61 log2e = 1.4426950408889634074, // (0x1.71547652b82feP+0)
62 log10e = .43429448190325182765, // (0x1.bcb7b1526e50eP-2)
63 pi = 3.1415926535897932385, // (0x1.921fb54442d18P+1) https://oeis.org/A000796
64 inv_pi = .31830988618379067154, // (0x1.45f306bc9c883P-2) https://oeis.org/A049541
65 sqrtpi = 1.7724538509055160273, // (0x1.c5bf891b4ef6bP+0) https://oeis.org/A002161
66 inv_sqrtpi = .56418958354775628695, // (0x1.20dd750429b6dP-1) https://oeis.org/A087197
67 sqrt2 = 1.4142135623730950488, // (0x1.6a09e667f3bcdP+0) https://oeis.org/A00219
68 inv_sqrt2 = .70710678118654752440, // (0x1.6a09e667f3bcdP-1)
69 sqrt3 = 1.7320508075688772935, // (0x1.bb67ae8584caaP+0) https://oeis.org/A002194
70 inv_sqrt3 = .57735026918962576451, // (0x1.279a74590331cP-1)
71 phi = 1.6180339887498948482; // (0x1.9e3779b97f4a8P+0) https://oeis.org/A001622
72constexpr float ef = 2.71828183F, // (0x1.5bf0a8P+1) https://oeis.org/A001113
73 egammaf = .577215665F, // (0x1.2788d0P-1) https://oeis.org/A001620
74 ln2f = .693147181F, // (0x1.62e430P-1) https://oeis.org/A002162
75 ln10f = 2.30258509F, // (0x1.26bb1cP+1) https://oeis.org/A002392
76 log2ef = 1.44269504F, // (0x1.715476P+0)
77 log10ef = .434294482F, // (0x1.bcb7b2P-2)
78 pif = 3.14159265F, // (0x1.921fb6P+1) https://oeis.org/A000796
79 inv_pif = .318309886F, // (0x1.45f306P-2) https://oeis.org/A049541
80 sqrtpif = 1.77245385F, // (0x1.c5bf8aP+0) https://oeis.org/A002161
81 inv_sqrtpif = .564189584F, // (0x1.20dd76P-1) https://oeis.org/A087197
82 sqrt2f = 1.41421356F, // (0x1.6a09e6P+0) https://oeis.org/A002193
83 inv_sqrt2f = .707106781F, // (0x1.6a09e6P-1)
84 sqrt3f = 1.73205081F, // (0x1.bb67aeP+0) https://oeis.org/A002194
85 inv_sqrt3f = .577350269F, // (0x1.279a74P-1)
86 phif = 1.61803399F; // (0x1.9e377aP+0) https://oeis.org/A001622
87} // namespace numbers
88
89namespace detail {
90template <typename T, std::size_t SizeOfT> struct TrailingZerosCounter {
91 static unsigned count(T Val, ZeroBehavior) {
92 if (!Val)
93 return std::numeric_limits<T>::digits;
94 if (Val & 0x1)
95 return 0;
96
97 // Bisection method.
98 unsigned ZeroBits = 0;
99 T Shift = std::numeric_limits<T>::digits >> 1;
100 T Mask = std::numeric_limits<T>::max() >> Shift;
101 while (Shift) {
102 if ((Val & Mask) == 0) {
103 Val >>= Shift;
104 ZeroBits |= Shift;
105 }
106 Shift >>= 1;
107 Mask >>= Shift;
108 }
109 return ZeroBits;
110 }
111};
112
113#if defined(__GNUC__4) || defined(_MSC_VER)
114template <typename T> struct TrailingZerosCounter<T, 4> {
115 static unsigned count(T Val, ZeroBehavior ZB) {
116 if (ZB != ZB_Undefined && Val == 0)
117 return 32;
118
119#if __has_builtin(__builtin_ctz)1 || defined(__GNUC__4)
120 return __builtin_ctz(Val);
121#elif defined(_MSC_VER)
122 unsigned long Index;
123 _BitScanForward(&Index, Val);
124 return Index;
125#endif
126 }
127};
128
129#if !defined(_MSC_VER) || defined(_M_X64)
130template <typename T> struct TrailingZerosCounter<T, 8> {
131 static unsigned count(T Val, ZeroBehavior ZB) {
132 if (ZB
23.1
'ZB' is not equal to ZB_Undefined
23.1
'ZB' is not equal to ZB_Undefined
23.1
'ZB' is not equal to ZB_Undefined
!= ZB_Undefined && Val == 0)
24
Assuming 'Val' is equal to 0
25
Taking true branch
133 return 64;
26
Returning the value 64
134
135#if __has_builtin(__builtin_ctzll)1 || defined(__GNUC__4)
136 return __builtin_ctzll(Val);
137#elif defined(_MSC_VER)
138 unsigned long Index;
139 _BitScanForward64(&Index, Val);
140 return Index;
141#endif
142 }
143};
144#endif
145#endif
146} // namespace detail
147
148/// Count number of 0's from the least significant bit to the most
149/// stopping at the first 1.
150///
151/// Only unsigned integral types are allowed.
152///
153/// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are
154/// valid arguments.
155template <typename T>
156unsigned countTrailingZeros(T Val, ZeroBehavior ZB = ZB_Width) {
157 static_assert(std::numeric_limits<T>::is_integer &&
158 !std::numeric_limits<T>::is_signed,
159 "Only unsigned integral types are allowed.");
160 return llvm::detail::TrailingZerosCounter<T, sizeof(T)>::count(Val, ZB);
23
Calling 'TrailingZerosCounter::count'
27
Returning from 'TrailingZerosCounter::count'
28
Returning the value 64
161}
162
163namespace detail {
164template <typename T, std::size_t SizeOfT> struct LeadingZerosCounter {
165 static unsigned count(T Val, ZeroBehavior) {
166 if (!Val)
167 return std::numeric_limits<T>::digits;
168
169 // Bisection method.
170 unsigned ZeroBits = 0;
171 for (T Shift = std::numeric_limits<T>::digits >> 1; Shift; Shift >>= 1) {
172 T Tmp = Val >> Shift;
173 if (Tmp)
174 Val = Tmp;
175 else
176 ZeroBits |= Shift;
177 }
178 return ZeroBits;
179 }
180};
181
182#if defined(__GNUC__4) || defined(_MSC_VER)
183template <typename T> struct LeadingZerosCounter<T, 4> {
184 static unsigned count(T Val, ZeroBehavior ZB) {
185 if (ZB != ZB_Undefined && Val == 0)
186 return 32;
187
188#if __has_builtin(__builtin_clz)1 || defined(__GNUC__4)
189 return __builtin_clz(Val);
190#elif defined(_MSC_VER)
191 unsigned long Index;
192 _BitScanReverse(&Index, Val);
193 return Index ^ 31;
194#endif
195 }
196};
197
198#if !defined(_MSC_VER) || defined(_M_X64)
199template <typename T> struct LeadingZerosCounter<T, 8> {
200 static unsigned count(T Val, ZeroBehavior ZB) {
201 if (ZB != ZB_Undefined && Val == 0)
202 return 64;
203
204#if __has_builtin(__builtin_clzll)1 || defined(__GNUC__4)
205 return __builtin_clzll(Val);
206#elif defined(_MSC_VER)
207 unsigned long Index;
208 _BitScanReverse64(&Index, Val);
209 return Index ^ 63;
210#endif
211 }
212};
213#endif
214#endif
215} // namespace detail
216
217/// Count number of 0's from the most significant bit to the least
218/// stopping at the first 1.
219///
220/// Only unsigned integral types are allowed.
221///
222/// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are
223/// valid arguments.
224template <typename T>
225unsigned countLeadingZeros(T Val, ZeroBehavior ZB = ZB_Width) {
226 static_assert(std::numeric_limits<T>::is_integer &&
227 !std::numeric_limits<T>::is_signed,
228 "Only unsigned integral types are allowed.");
229 return llvm::detail::LeadingZerosCounter<T, sizeof(T)>::count(Val, ZB);
230}
231
232/// Get the index of the first set bit starting from the least
233/// significant bit.
234///
235/// Only unsigned integral types are allowed.
236///
237/// \param ZB the behavior on an input of 0. Only ZB_Max and ZB_Undefined are
238/// valid arguments.
239template <typename T> T findFirstSet(T Val, ZeroBehavior ZB = ZB_Max) {
240 if (ZB == ZB_Max && Val == 0)
241 return std::numeric_limits<T>::max();
242
243 return countTrailingZeros(Val, ZB_Undefined);
244}
245
246/// Create a bitmask with the N right-most bits set to 1, and all other
247/// bits set to 0. Only unsigned types are allowed.
248template <typename T> T maskTrailingOnes(unsigned N) {
249 static_assert(std::is_unsigned<T>::value, "Invalid type!");
250 const unsigned Bits = CHAR_BIT8 * sizeof(T);
251 assert(N <= Bits && "Invalid bit index")(static_cast <bool> (N <= Bits && "Invalid bit index"
) ? void (0) : __assert_fail ("N <= Bits && \"Invalid bit index\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/Support/MathExtras.h"
, 251, __extension__ __PRETTY_FUNCTION__))
;
252 return N == 0 ? 0 : (T(-1) >> (Bits - N));
253}
254
255/// Create a bitmask with the N left-most bits set to 1, and all other
256/// bits set to 0. Only unsigned types are allowed.
257template <typename T> T maskLeadingOnes(unsigned N) {
258 return ~maskTrailingOnes<T>(CHAR_BIT8 * sizeof(T) - N);
259}
260
261/// Create a bitmask with the N right-most bits set to 0, and all other
262/// bits set to 1. Only unsigned types are allowed.
263template <typename T> T maskTrailingZeros(unsigned N) {
264 return maskLeadingOnes<T>(CHAR_BIT8 * sizeof(T) - N);
265}
266
267/// Create a bitmask with the N left-most bits set to 0, and all other
268/// bits set to 1. Only unsigned types are allowed.
269template <typename T> T maskLeadingZeros(unsigned N) {
270 return maskTrailingOnes<T>(CHAR_BIT8 * sizeof(T) - N);
271}
272
273/// Get the index of the last set bit starting from the least
274/// significant bit.
275///
276/// Only unsigned integral types are allowed.
277///
278/// \param ZB the behavior on an input of 0. Only ZB_Max and ZB_Undefined are
279/// valid arguments.
280template <typename T> T findLastSet(T Val, ZeroBehavior ZB = ZB_Max) {
281 if (ZB == ZB_Max && Val == 0)
282 return std::numeric_limits<T>::max();
283
284 // Use ^ instead of - because both gcc and llvm can remove the associated ^
285 // in the __builtin_clz intrinsic on x86.
286 return countLeadingZeros(Val, ZB_Undefined) ^
287 (std::numeric_limits<T>::digits - 1);
288}
289
290/// Macro compressed bit reversal table for 256 bits.
291///
292/// http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
293static const unsigned char BitReverseTable256[256] = {
294#define R2(n) n, n + 2 * 64, n + 1 * 64, n + 3 * 64
295#define R4(n) R2(n), R2(n + 2 * 16), R2(n + 1 * 16), R2(n + 3 * 16)
296#define R6(n) R4(n), R4(n + 2 * 4), R4(n + 1 * 4), R4(n + 3 * 4)
297 R6(0), R6(2), R6(1), R6(3)
298#undef R2
299#undef R4
300#undef R6
301};
302
303/// Reverse the bits in \p Val.
304template <typename T>
305T reverseBits(T Val) {
306 unsigned char in[sizeof(Val)];
307 unsigned char out[sizeof(Val)];
308 std::memcpy(in, &Val, sizeof(Val));
309 for (unsigned i = 0; i < sizeof(Val); ++i)
310 out[(sizeof(Val) - i) - 1] = BitReverseTable256[in[i]];
311 std::memcpy(&Val, out, sizeof(Val));
312 return Val;
313}
314
315#if __has_builtin(__builtin_bitreverse8)1
316template<>
317inline uint8_t reverseBits<uint8_t>(uint8_t Val) {
318 return __builtin_bitreverse8(Val);
319}
320#endif
321
322#if __has_builtin(__builtin_bitreverse16)1
323template<>
324inline uint16_t reverseBits<uint16_t>(uint16_t Val) {
325 return __builtin_bitreverse16(Val);
326}
327#endif
328
329#if __has_builtin(__builtin_bitreverse32)1
330template<>
331inline uint32_t reverseBits<uint32_t>(uint32_t Val) {
332 return __builtin_bitreverse32(Val);
333}
334#endif
335
336#if __has_builtin(__builtin_bitreverse64)1
337template<>
338inline uint64_t reverseBits<uint64_t>(uint64_t Val) {
339 return __builtin_bitreverse64(Val);
340}
341#endif
342
343// NOTE: The following support functions use the _32/_64 extensions instead of
344// type overloading so that signed and unsigned integers can be used without
345// ambiguity.
346
347/// Return the high 32 bits of a 64 bit value.
348constexpr inline uint32_t Hi_32(uint64_t Value) {
349 return static_cast<uint32_t>(Value >> 32);
350}
351
352/// Return the low 32 bits of a 64 bit value.
353constexpr inline uint32_t Lo_32(uint64_t Value) {
354 return static_cast<uint32_t>(Value);
355}
356
357/// Make a 64-bit integer from a high / low pair of 32-bit integers.
358constexpr inline uint64_t Make_64(uint32_t High, uint32_t Low) {
359 return ((uint64_t)High << 32) | (uint64_t)Low;
360}
361
362/// Checks if an integer fits into the given bit width.
363template <unsigned N> constexpr inline bool isInt(int64_t x) {
364 return N >= 64 || (-(INT64_C(1)1L<<(N-1)) <= x && x < (INT64_C(1)1L<<(N-1)));
365}
366// Template specializations to get better code for common cases.
367template <> constexpr inline bool isInt<8>(int64_t x) {
368 return static_cast<int8_t>(x) == x;
369}
370template <> constexpr inline bool isInt<16>(int64_t x) {
371 return static_cast<int16_t>(x) == x;
372}
373template <> constexpr inline bool isInt<32>(int64_t x) {
374 return static_cast<int32_t>(x) == x;
375}
376
377/// Checks if a signed integer is an N bit number shifted left by S.
378template <unsigned N, unsigned S>
379constexpr inline bool isShiftedInt(int64_t x) {
380 static_assert(
381 N > 0, "isShiftedInt<0> doesn't make sense (refers to a 0-bit number.");
382 static_assert(N + S <= 64, "isShiftedInt<N, S> with N + S > 64 is too wide.");
383 return isInt<N + S>(x) && (x % (UINT64_C(1)1UL << S) == 0);
384}
385
386/// Checks if an unsigned integer fits into the given bit width.
387///
388/// This is written as two functions rather than as simply
389///
390/// return N >= 64 || X < (UINT64_C(1) << N);
391///
392/// to keep MSVC from (incorrectly) warning on isUInt<64> that we're shifting
393/// left too many places.
394template <unsigned N>
395constexpr inline std::enable_if_t<(N < 64), bool> isUInt(uint64_t X) {
396 static_assert(N > 0, "isUInt<0> doesn't make sense");
397 return X < (UINT64_C(1)1UL << (N));
398}
399template <unsigned N>
400constexpr inline std::enable_if_t<N >= 64, bool> isUInt(uint64_t) {
401 return true;
402}
403
404// Template specializations to get better code for common cases.
405template <> constexpr inline bool isUInt<8>(uint64_t x) {
406 return static_cast<uint8_t>(x) == x;
407}
408template <> constexpr inline bool isUInt<16>(uint64_t x) {
409 return static_cast<uint16_t>(x) == x;
410}
411template <> constexpr inline bool isUInt<32>(uint64_t x) {
412 return static_cast<uint32_t>(x) == x;
413}
414
415/// Checks if a unsigned integer is an N bit number shifted left by S.
416template <unsigned N, unsigned S>
417constexpr inline bool isShiftedUInt(uint64_t x) {
418 static_assert(
419 N > 0, "isShiftedUInt<0> doesn't make sense (refers to a 0-bit number)");
420 static_assert(N + S <= 64,
421 "isShiftedUInt<N, S> with N + S > 64 is too wide.");
422 // Per the two static_asserts above, S must be strictly less than 64. So
423 // 1 << S is not undefined behavior.
424 return isUInt<N + S>(x) && (x % (UINT64_C(1)1UL << S) == 0);
425}
426
427/// Gets the maximum value for a N-bit unsigned integer.
428inline uint64_t maxUIntN(uint64_t N) {
429 assert(N > 0 && N <= 64 && "integer width out of range")(static_cast <bool> (N > 0 && N <= 64 &&
"integer width out of range") ? void (0) : __assert_fail ("N > 0 && N <= 64 && \"integer width out of range\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/Support/MathExtras.h"
, 429, __extension__ __PRETTY_FUNCTION__))
;
430
431 // uint64_t(1) << 64 is undefined behavior, so we can't do
432 // (uint64_t(1) << N) - 1
433 // without checking first that N != 64. But this works and doesn't have a
434 // branch.
435 return UINT64_MAX(18446744073709551615UL) >> (64 - N);
436}
437
438/// Gets the minimum value for a N-bit signed integer.
439inline int64_t minIntN(int64_t N) {
440 assert(N > 0 && N <= 64 && "integer width out of range")(static_cast <bool> (N > 0 && N <= 64 &&
"integer width out of range") ? void (0) : __assert_fail ("N > 0 && N <= 64 && \"integer width out of range\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/Support/MathExtras.h"
, 440, __extension__ __PRETTY_FUNCTION__))
;
441
442 return UINT64_C(1)1UL + ~(UINT64_C(1)1UL << (N - 1));
443}
444
445/// Gets the maximum value for a N-bit signed integer.
446inline int64_t maxIntN(int64_t N) {
447 assert(N > 0 && N <= 64 && "integer width out of range")(static_cast <bool> (N > 0 && N <= 64 &&
"integer width out of range") ? void (0) : __assert_fail ("N > 0 && N <= 64 && \"integer width out of range\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/Support/MathExtras.h"
, 447, __extension__ __PRETTY_FUNCTION__))
;
448
449 // This relies on two's complement wraparound when N == 64, so we convert to
450 // int64_t only at the very end to avoid UB.
451 return (UINT64_C(1)1UL << (N - 1)) - 1;
452}
453
454/// Checks if an unsigned integer fits into the given (dynamic) bit width.
455inline bool isUIntN(unsigned N, uint64_t x) {
456 return N >= 64 || x <= maxUIntN(N);
457}
458
459/// Checks if an signed integer fits into the given (dynamic) bit width.
460inline bool isIntN(unsigned N, int64_t x) {
461 return N >= 64 || (minIntN(N) <= x && x <= maxIntN(N));
462}
463
464/// Return true if the argument is a non-empty sequence of ones starting at the
465/// least significant bit with the remainder zero (32 bit version).
466/// Ex. isMask_32(0x0000FFFFU) == true.
467constexpr inline bool isMask_32(uint32_t Value) {
468 return Value && ((Value + 1) & Value) == 0;
469}
470
471/// Return true if the argument is a non-empty sequence of ones starting at the
472/// least significant bit with the remainder zero (64 bit version).
473constexpr inline bool isMask_64(uint64_t Value) {
474 return Value && ((Value + 1) & Value) == 0;
475}
476
477/// Return true if the argument contains a non-empty sequence of ones with the
478/// remainder zero (32 bit version.) Ex. isShiftedMask_32(0x0000FF00U) == true.
479constexpr inline bool isShiftedMask_32(uint32_t Value) {
480 return Value && isMask_32((Value - 1) | Value);
481}
482
483/// Return true if the argument contains a non-empty sequence of ones with the
484/// remainder zero (64 bit version.)
485constexpr inline bool isShiftedMask_64(uint64_t Value) {
486 return Value && isMask_64((Value - 1) | Value);
487}
488
489/// Return true if the argument is a power of two > 0.
490/// Ex. isPowerOf2_32(0x00100000U) == true (32 bit edition.)
491constexpr inline bool isPowerOf2_32(uint32_t Value) {
492 return Value && !(Value & (Value - 1));
493}
494
495/// Return true if the argument is a power of two > 0 (64 bit edition.)
496constexpr inline bool isPowerOf2_64(uint64_t Value) {
497 return Value && !(Value & (Value - 1));
498}
499
500/// Count the number of ones from the most significant bit to the first
501/// zero bit.
502///
503/// Ex. countLeadingOnes(0xFF0FFF00) == 8.
504/// Only unsigned integral types are allowed.
505///
506/// \param ZB the behavior on an input of all ones. Only ZB_Width and
507/// ZB_Undefined are valid arguments.
508template <typename T>
509unsigned countLeadingOnes(T Value, ZeroBehavior ZB = ZB_Width) {
510 static_assert(std::numeric_limits<T>::is_integer &&
511 !std::numeric_limits<T>::is_signed,
512 "Only unsigned integral types are allowed.");
513 return countLeadingZeros<T>(~Value, ZB);
514}
515
516/// Count the number of ones from the least significant bit to the first
517/// zero bit.
518///
519/// Ex. countTrailingOnes(0x00FF00FF) == 8.
520/// Only unsigned integral types are allowed.
521///
522/// \param ZB the behavior on an input of all ones. Only ZB_Width and
523/// ZB_Undefined are valid arguments.
524template <typename T>
525unsigned countTrailingOnes(T Value, ZeroBehavior ZB = ZB_Width) {
526 static_assert(std::numeric_limits<T>::is_integer &&
527 !std::numeric_limits<T>::is_signed,
528 "Only unsigned integral types are allowed.");
529 return countTrailingZeros<T>(~Value, ZB);
530}
531
532namespace detail {
533template <typename T, std::size_t SizeOfT> struct PopulationCounter {
534 static unsigned count(T Value) {
535 // Generic version, forward to 32 bits.
536 static_assert(SizeOfT <= 4, "Not implemented!");
537#if defined(__GNUC__4)
538 return __builtin_popcount(Value);
539#else
540 uint32_t v = Value;
541 v = v - ((v >> 1) & 0x55555555);
542 v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
543 return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
544#endif
545 }
546};
547
548template <typename T> struct PopulationCounter<T, 8> {
549 static unsigned count(T Value) {
550#if defined(__GNUC__4)
551 return __builtin_popcountll(Value);
552#else
553 uint64_t v = Value;
554 v = v - ((v >> 1) & 0x5555555555555555ULL);
555 v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
556 v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
557 return unsigned((uint64_t)(v * 0x0101010101010101ULL) >> 56);
558#endif
559 }
560};
561} // namespace detail
562
563/// Count the number of set bits in a value.
564/// Ex. countPopulation(0xF000F000) = 8
565/// Returns 0 if the word is zero.
566template <typename T>
567inline unsigned countPopulation(T Value) {
568 static_assert(std::numeric_limits<T>::is_integer &&
569 !std::numeric_limits<T>::is_signed,
570 "Only unsigned integral types are allowed.");
571 return detail::PopulationCounter<T, sizeof(T)>::count(Value);
572}
573
574/// Compile time Log2.
575/// Valid only for positive powers of two.
576template <size_t kValue> constexpr inline size_t CTLog2() {
577 static_assert(kValue > 0 && llvm::isPowerOf2_64(kValue),
578 "Value is not a valid power of 2");
579 return 1 + CTLog2<kValue / 2>();
580}
581
582template <> constexpr inline size_t CTLog2<1>() { return 0; }
583
584/// Return the log base 2 of the specified value.
585inline double Log2(double Value) {
586#if defined(__ANDROID_API__) && __ANDROID_API__ < 18
587 return __builtin_log(Value) / __builtin_log(2.0);
588#else
589 return log2(Value);
590#endif
591}
592
593/// Return the floor log base 2 of the specified value, -1 if the value is zero.
594/// (32 bit edition.)
595/// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2
596inline unsigned Log2_32(uint32_t Value) {
597 return 31 - countLeadingZeros(Value);
598}
599
600/// Return the floor log base 2 of the specified value, -1 if the value is zero.
601/// (64 bit edition.)
602inline unsigned Log2_64(uint64_t Value) {
603 return 63 - countLeadingZeros(Value);
604}
605
606/// Return the ceil log base 2 of the specified value, 32 if the value is zero.
607/// (32 bit edition).
608/// Ex. Log2_32_Ceil(32) == 5, Log2_32_Ceil(1) == 0, Log2_32_Ceil(6) == 3
609inline unsigned Log2_32_Ceil(uint32_t Value) {
610 return 32 - countLeadingZeros(Value - 1);
611}
612
613/// Return the ceil log base 2 of the specified value, 64 if the value is zero.
614/// (64 bit edition.)
615inline unsigned Log2_64_Ceil(uint64_t Value) {
616 return 64 - countLeadingZeros(Value - 1);
617}
618
619/// Return the greatest common divisor of the values using Euclid's algorithm.
620template <typename T>
621inline T greatestCommonDivisor(T A, T B) {
622 while (B) {
623 T Tmp = B;
624 B = A % B;
625 A = Tmp;
626 }
627 return A;
628}
629
630inline uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B) {
631 return greatestCommonDivisor<uint64_t>(A, B);
632}
633
634/// This function takes a 64-bit integer and returns the bit equivalent double.
635inline double BitsToDouble(uint64_t Bits) {
636 double D;
637 static_assert(sizeof(uint64_t) == sizeof(double), "Unexpected type sizes");
638 memcpy(&D, &Bits, sizeof(Bits));
639 return D;
640}
641
642/// This function takes a 32-bit integer and returns the bit equivalent float.
643inline float BitsToFloat(uint32_t Bits) {
644 float F;
645 static_assert(sizeof(uint32_t) == sizeof(float), "Unexpected type sizes");
646 memcpy(&F, &Bits, sizeof(Bits));
647 return F;
648}
649
650/// This function takes a double and returns the bit equivalent 64-bit integer.
651/// Note that copying doubles around changes the bits of NaNs on some hosts,
652/// notably x86, so this routine cannot be used if these bits are needed.
653inline uint64_t DoubleToBits(double Double) {
654 uint64_t Bits;
655 static_assert(sizeof(uint64_t) == sizeof(double), "Unexpected type sizes");
656 memcpy(&Bits, &Double, sizeof(Double));
657 return Bits;
658}
659
660/// This function takes a float and returns the bit equivalent 32-bit integer.
661/// Note that copying floats around changes the bits of NaNs on some hosts,
662/// notably x86, so this routine cannot be used if these bits are needed.
663inline uint32_t FloatToBits(float Float) {
664 uint32_t Bits;
665 static_assert(sizeof(uint32_t) == sizeof(float), "Unexpected type sizes");
666 memcpy(&Bits, &Float, sizeof(Float));
667 return Bits;
668}
669
670/// A and B are either alignments or offsets. Return the minimum alignment that
671/// may be assumed after adding the two together.
672constexpr inline uint64_t MinAlign(uint64_t A, uint64_t B) {
673 // The largest power of 2 that divides both A and B.
674 //
675 // Replace "-Value" by "1+~Value" in the following commented code to avoid
676 // MSVC warning C4146
677 // return (A | B) & -(A | B);
678 return (A | B) & (1 + ~(A | B));
679}
680
681/// Returns the next power of two (in 64-bits) that is strictly greater than A.
682/// Returns zero on overflow.
683inline uint64_t NextPowerOf2(uint64_t A) {
684 A |= (A >> 1);
685 A |= (A >> 2);
686 A |= (A >> 4);
687 A |= (A >> 8);
688 A |= (A >> 16);
689 A |= (A >> 32);
690 return A + 1;
691}
692
693/// Returns the power of two which is less than or equal to the given value.
694/// Essentially, it is a floor operation across the domain of powers of two.
695inline uint64_t PowerOf2Floor(uint64_t A) {
696 if (!A) return 0;
697 return 1ull << (63 - countLeadingZeros(A, ZB_Undefined));
698}
699
700/// Returns the power of two which is greater than or equal to the given value.
701/// Essentially, it is a ceil operation across the domain of powers of two.
702inline uint64_t PowerOf2Ceil(uint64_t A) {
703 if (!A)
704 return 0;
705 return NextPowerOf2(A - 1);
706}
707
708/// Returns the next integer (mod 2**64) that is greater than or equal to
709/// \p Value and is a multiple of \p Align. \p Align must be non-zero.
710///
711/// If non-zero \p Skew is specified, the return value will be a minimal
712/// integer that is greater than or equal to \p Value and equal to
713/// \p Align * N + \p Skew for some integer N. If \p Skew is larger than
714/// \p Align, its value is adjusted to '\p Skew mod \p Align'.
715///
716/// Examples:
717/// \code
718/// alignTo(5, 8) = 8
719/// alignTo(17, 8) = 24
720/// alignTo(~0LL, 8) = 0
721/// alignTo(321, 255) = 510
722///
723/// alignTo(5, 8, 7) = 7
724/// alignTo(17, 8, 1) = 17
725/// alignTo(~0LL, 8, 3) = 3
726/// alignTo(321, 255, 42) = 552
727/// \endcode
728inline uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew = 0) {
729 assert(Align != 0u && "Align can't be 0.")(static_cast <bool> (Align != 0u && "Align can't be 0."
) ? void (0) : __assert_fail ("Align != 0u && \"Align can't be 0.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/Support/MathExtras.h"
, 729, __extension__ __PRETTY_FUNCTION__))
;
730 Skew %= Align;
731 return (Value + Align - 1 - Skew) / Align * Align + Skew;
732}
733
734/// Returns the next integer (mod 2**64) that is greater than or equal to
735/// \p Value and is a multiple of \c Align. \c Align must be non-zero.
736template <uint64_t Align> constexpr inline uint64_t alignTo(uint64_t Value) {
737 static_assert(Align != 0u, "Align must be non-zero");
738 return (Value + Align - 1) / Align * Align;
739}
740
741/// Returns the integer ceil(Numerator / Denominator).
742inline uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator) {
743 return alignTo(Numerator, Denominator) / Denominator;
744}
745
746/// Returns the integer nearest(Numerator / Denominator).
747inline uint64_t divideNearest(uint64_t Numerator, uint64_t Denominator) {
748 return (Numerator + (Denominator / 2)) / Denominator;
749}
750
751/// Returns the largest uint64_t less than or equal to \p Value and is
752/// \p Skew mod \p Align. \p Align must be non-zero
753inline uint64_t alignDown(uint64_t Value, uint64_t Align, uint64_t Skew = 0) {
754 assert(Align != 0u && "Align can't be 0.")(static_cast <bool> (Align != 0u && "Align can't be 0."
) ? void (0) : __assert_fail ("Align != 0u && \"Align can't be 0.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/Support/MathExtras.h"
, 754, __extension__ __PRETTY_FUNCTION__))
;
755 Skew %= Align;
756 return (Value - Skew) / Align * Align + Skew;
757}
758
759/// Sign-extend the number in the bottom B bits of X to a 32-bit integer.
760/// Requires 0 < B <= 32.
761template <unsigned B> constexpr inline int32_t SignExtend32(uint32_t X) {
762 static_assert(B > 0, "Bit width can't be 0.");
763 static_assert(B <= 32, "Bit width out of range.");
764 return int32_t(X << (32 - B)) >> (32 - B);
765}
766
767/// Sign-extend the number in the bottom B bits of X to a 32-bit integer.
768/// Requires 0 < B <= 32.
769inline int32_t SignExtend32(uint32_t X, unsigned B) {
770 assert(B > 0 && "Bit width can't be 0.")(static_cast <bool> (B > 0 && "Bit width can't be 0."
) ? void (0) : __assert_fail ("B > 0 && \"Bit width can't be 0.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/Support/MathExtras.h"
, 770, __extension__ __PRETTY_FUNCTION__))
;
771 assert(B <= 32 && "Bit width out of range.")(static_cast <bool> (B <= 32 && "Bit width out of range."
) ? void (0) : __assert_fail ("B <= 32 && \"Bit width out of range.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/Support/MathExtras.h"
, 771, __extension__ __PRETTY_FUNCTION__))
;
772 return int32_t(X << (32 - B)) >> (32 - B);
773}
774
775/// Sign-extend the number in the bottom B bits of X to a 64-bit integer.
776/// Requires 0 < B <= 64.
777template <unsigned B> constexpr inline int64_t SignExtend64(uint64_t x) {
778 static_assert(B > 0, "Bit width can't be 0.");
779 static_assert(B <= 64, "Bit width out of range.");
780 return int64_t(x << (64 - B)) >> (64 - B);
781}
782
783/// Sign-extend the number in the bottom B bits of X to a 64-bit integer.
784/// Requires 0 < B <= 64.
785inline int64_t SignExtend64(uint64_t X, unsigned B) {
786 assert(B > 0 && "Bit width can't be 0.")(static_cast <bool> (B > 0 && "Bit width can't be 0."
) ? void (0) : __assert_fail ("B > 0 && \"Bit width can't be 0.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/Support/MathExtras.h"
, 786, __extension__ __PRETTY_FUNCTION__))
;
787 assert(B <= 64 && "Bit width out of range.")(static_cast <bool> (B <= 64 && "Bit width out of range."
) ? void (0) : __assert_fail ("B <= 64 && \"Bit width out of range.\""
, "/build/llvm-toolchain-snapshot-13~++20210726100616+dead50d4427c/llvm/include/llvm/Support/MathExtras.h"
, 787, __extension__ __PRETTY_FUNCTION__))
;
788 return int64_t(X << (64 - B)) >> (64 - B);
789}
790
791/// Subtract two unsigned integers, X and Y, of type T and return the absolute
792/// value of the result.
793template <typename T>
794std::enable_if_t<std::is_unsigned<T>::value, T> AbsoluteDifference(T X, T Y) {
795 return X > Y ? (X - Y) : (Y - X);
796}
797
798/// Add two unsigned integers, X and Y, of type T. Clamp the result to the
799/// maximum representable value of T on overflow. ResultOverflowed indicates if
800/// the result is larger than the maximum representable value of type T.
801template <typename T>
802std::enable_if_t<std::is_unsigned<T>::value, T>
803SaturatingAdd(T X, T Y, bool *ResultOverflowed = nullptr) {
804 bool Dummy;
805 bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
806 // Hacker's Delight, p. 29
807 T Z = X + Y;
808 Overflowed = (Z < X || Z < Y);
809 if (Overflowed)
810 return std::numeric_limits<T>::max();
811 else
812 return Z;
813}
814
815/// Multiply two unsigned integers, X and Y, of type T. Clamp the result to the
816/// maximum representable value of T on overflow. ResultOverflowed indicates if
817/// the result is larger than the maximum representable value of type T.
818template <typename T>
819std::enable_if_t<std::is_unsigned<T>::value, T>
820SaturatingMultiply(T X, T Y, bool *ResultOverflowed = nullptr) {
821 bool Dummy;
822 bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
823
824 // Hacker's Delight, p. 30 has a different algorithm, but we don't use that
825 // because it fails for uint16_t (where multiplication can have undefined
826 // behavior due to promotion to int), and requires a division in addition
827 // to the multiplication.
828
829 Overflowed = false;
830
831 // Log2(Z) would be either Log2Z or Log2Z + 1.
832 // Special case: if X or Y is 0, Log2_64 gives -1, and Log2Z
833 // will necessarily be less than Log2Max as desired.
834 int Log2Z = Log2_64(X) + Log2_64(Y);
835 const T Max = std::numeric_limits<T>::max();
836 int Log2Max = Log2_64(Max);
837 if (Log2Z < Log2Max) {
838 return X * Y;
839 }
840 if (Log2Z > Log2Max) {
841 Overflowed = true;
842 return Max;
843 }
844
845 // We're going to use the top bit, and maybe overflow one
846 // bit past it. Multiply all but the bottom bit then add
847 // that on at the end.
848 T Z = (X >> 1) * Y;
849 if (Z & ~(Max >> 1)) {
850 Overflowed = true;
851 return Max;
852 }
853 Z <<= 1;
854 if (X & 1)
855 return SaturatingAdd(Z, Y, ResultOverflowed);
856
857 return Z;
858}
859
860/// Multiply two unsigned integers, X and Y, and add the unsigned integer, A to
861/// the product. Clamp the result to the maximum representable value of T on
862/// overflow. ResultOverflowed indicates if the result is larger than the
863/// maximum representable value of type T.
864template <typename T>
865std::enable_if_t<std::is_unsigned<T>::value, T>
866SaturatingMultiplyAdd(T X, T Y, T A, bool *ResultOverflowed = nullptr) {
867 bool Dummy;
868 bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
869
870 T Product = SaturatingMultiply(X, Y, &Overflowed);
871 if (Overflowed)
872 return Product;
873
874 return SaturatingAdd(A, Product, &Overflowed);
875}
876
877/// Use this rather than HUGE_VALF; the latter causes warnings on MSVC.
878extern const float huge_valf;
879
880
881/// Add two signed integers, computing the two's complement truncated result,
882/// returning true if overflow occured.
883template <typename T>
884std::enable_if_t<std::is_signed<T>::value, T> AddOverflow(T X, T Y, T &Result) {
885#if __has_builtin(__builtin_add_overflow)1
886 return __builtin_add_overflow(X, Y, &Result);
887#else
888 // Perform the unsigned addition.
889 using U = std::make_unsigned_t<T>;
890 const U UX = static_cast<U>(X);
891 const U UY = static_cast<U>(Y);
892 const U UResult = UX + UY;
893
894 // Convert to signed.
895 Result = static_cast<T>(UResult);
896
897 // Adding two positive numbers should result in a positive number.
898 if (X > 0 && Y > 0)
899 return Result <= 0;
900 // Adding two negatives should result in a negative number.
901 if (X < 0 && Y < 0)
902 return Result >= 0;
903 return false;
904#endif
905}
906
907/// Subtract two signed integers, computing the two's complement truncated
908/// result, returning true if an overflow ocurred.
909template <typename T>
910std::enable_if_t<std::is_signed<T>::value, T> SubOverflow(T X, T Y, T &Result) {
911#if __has_builtin(__builtin_sub_overflow)1
912 return __builtin_sub_overflow(X, Y, &Result);
913#else
914 // Perform the unsigned addition.
915 using U = std::make_unsigned_t<T>;
916 const U UX = static_cast<U>(X);
917 const U UY = static_cast<U>(Y);
918 const U UResult = UX - UY;
919
920 // Convert to signed.
921 Result = static_cast<T>(UResult);
922
923 // Subtracting a positive number from a negative results in a negative number.
924 if (X <= 0 && Y > 0)
925 return Result >= 0;
926 // Subtracting a negative number from a positive results in a positive number.
927 if (X >= 0 && Y < 0)
928 return Result <= 0;
929 return false;
930#endif
931}
932
933/// Multiply two signed integers, computing the two's complement truncated
934/// result, returning true if an overflow ocurred.
935template <typename T>
936std::enable_if_t<std::is_signed<T>::value, T> MulOverflow(T X, T Y, T &Result) {
937 // Perform the unsigned multiplication on absolute values.
938 using U = std::make_unsigned_t<T>;
939 const U UX = X < 0 ? (0 - static_cast<U>(X)) : static_cast<U>(X);
940 const U UY = Y < 0 ? (0 - static_cast<U>(Y)) : static_cast<U>(Y);
941 const U UResult = UX * UY;
942
943 // Convert to signed.
944 const bool IsNegative = (X < 0) ^ (Y < 0);
945 Result = IsNegative ? (0 - UResult) : UResult;
946
947 // If any of the args was 0, result is 0 and no overflow occurs.
948 if (UX == 0 || UY == 0)
949 return false;
950
951 // UX and UY are in [1, 2^n], where n is the number of digits.
952 // Check how the max allowed absolute value (2^n for negative, 2^(n-1) for
953 // positive) divided by an argument compares to the other.
954 if (IsNegative)
955 return UX > (static_cast<U>(std::numeric_limits<T>::max()) + U(1)) / UY;
956 else
957 return UX > (static_cast<U>(std::numeric_limits<T>::max())) / UY;
958}
959
960} // End llvm namespace
961
962#endif