1 //===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run 11 // both before and after the DAG is legalized. 12 // 13 // This pass is not a substitute for the LLVM IR instcombine pass. This pass is 14 // primarily intended to handle simplification opportunities that are implicit 15 // in the LLVM IR and exposed by the various codegen lowering phases. 16 // 17 //===----------------------------------------------------------------------===// 18 19 #include "llvm/ADT/SetVector.h" 20 #include "llvm/ADT/SmallBitVector.h" 21 #include "llvm/ADT/SmallPtrSet.h" 22 #include "llvm/ADT/SmallSet.h" 23 #include "llvm/ADT/Statistic.h" 24 #include "llvm/Analysis/AliasAnalysis.h" 25 #include "llvm/CodeGen/MachineFrameInfo.h" 26 #include "llvm/CodeGen/MachineFunction.h" 27 #include "llvm/CodeGen/SelectionDAG.h" 28 #include "llvm/CodeGen/SelectionDAGTargetInfo.h" 29 #include "llvm/IR/DataLayout.h" 30 #include "llvm/IR/DerivedTypes.h" 31 #include "llvm/IR/Function.h" 32 #include "llvm/IR/LLVMContext.h" 33 #include "llvm/Support/CommandLine.h" 34 #include "llvm/Support/Debug.h" 35 #include "llvm/Support/ErrorHandling.h" 36 #include "llvm/Support/MathExtras.h" 37 #include "llvm/Support/raw_ostream.h" 38 #include "llvm/Target/TargetLowering.h" 39 #include "llvm/Target/TargetOptions.h" 40 #include "llvm/Target/TargetRegisterInfo.h" 41 #include "llvm/Target/TargetSubtargetInfo.h" 42 #include <algorithm> 43 using namespace llvm; 44 45 #define DEBUG_TYPE "dagcombine" 46 47 STATISTIC(NodesCombined , "Number of dag nodes combined"); 48 STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created"); 49 STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created"); 50 STATISTIC(OpsNarrowed , "Number of load/op/store narrowed"); 51 STATISTIC(LdStFP2Int , "Number of fp load/store pairs transformed to int"); 52 STATISTIC(SlicedLoads, "Number of load sliced"); 53 54 namespace { 55 static cl::opt<bool> 56 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden, 57 cl::desc("Enable DAG combiner's use of IR alias analysis")); 58 59 static cl::opt<bool> 60 UseTBAA("combiner-use-tbaa", cl::Hidden, cl::init(true), 61 cl::desc("Enable DAG combiner's use of TBAA")); 62 63 #ifndef NDEBUG 64 static cl::opt<std::string> 65 CombinerAAOnlyFunc("combiner-aa-only-func", cl::Hidden, 66 cl::desc("Only use DAG-combiner alias analysis in this" 67 " function")); 68 #endif 69 70 /// Hidden option to stress test load slicing, i.e., when this option 71 /// is enabled, load slicing bypasses most of its profitability guards. 72 static cl::opt<bool> 73 StressLoadSlicing("combiner-stress-load-slicing", cl::Hidden, 74 cl::desc("Bypass the profitability model of load " 75 "slicing"), 76 cl::init(false)); 77 78 static cl::opt<bool> 79 MaySplitLoadIndex("combiner-split-load-index", cl::Hidden, cl::init(true), 80 cl::desc("DAG combiner may split indexing from loads")); 81 82 //------------------------------ DAGCombiner ---------------------------------// 83 84 class DAGCombiner { 85 SelectionDAG &DAG; 86 const TargetLowering &TLI; 87 CombineLevel Level; 88 CodeGenOpt::Level OptLevel; 89 bool LegalOperations; 90 bool LegalTypes; 91 bool ForCodeSize; 92 93 /// \brief Worklist of all of the nodes that need to be simplified. 94 /// 95 /// This must behave as a stack -- new nodes to process are pushed onto the 96 /// back and when processing we pop off of the back. 97 /// 98 /// The worklist will not contain duplicates but may contain null entries 99 /// due to nodes being deleted from the underlying DAG. 100 SmallVector<SDNode *, 64> Worklist; 101 102 /// \brief Mapping from an SDNode to its position on the worklist. 103 /// 104 /// This is used to find and remove nodes from the worklist (by nulling 105 /// them) when they are deleted from the underlying DAG. It relies on 106 /// stable indices of nodes within the worklist. 107 DenseMap<SDNode *, unsigned> WorklistMap; 108 109 /// \brief Set of nodes which have been combined (at least once). 110 /// 111 /// This is used to allow us to reliably add any operands of a DAG node 112 /// which have not yet been combined to the worklist. 113 SmallPtrSet<SDNode *, 32> CombinedNodes; 114 115 // AA - Used for DAG load/store alias analysis. 116 AliasAnalysis &AA; 117 118 /// When an instruction is simplified, add all users of the instruction to 119 /// the work lists because they might get more simplified now. 120 void AddUsersToWorklist(SDNode *N) { 121 for (SDNode *Node : N->uses()) 122 AddToWorklist(Node); 123 } 124 125 /// Call the node-specific routine that folds each particular type of node. 126 SDValue visit(SDNode *N); 127 128 public: 129 /// Add to the worklist making sure its instance is at the back (next to be 130 /// processed.) 131 void AddToWorklist(SDNode *N) { 132 assert(N->getOpcode() != ISD::DELETED_NODE && 133 "Deleted Node added to Worklist"); 134 135 // Skip handle nodes as they can't usefully be combined and confuse the 136 // zero-use deletion strategy. 137 if (N->getOpcode() == ISD::HANDLENODE) 138 return; 139 140 if (WorklistMap.insert(std::make_pair(N, Worklist.size())).second) 141 Worklist.push_back(N); 142 } 143 144 /// Remove all instances of N from the worklist. 145 void removeFromWorklist(SDNode *N) { 146 CombinedNodes.erase(N); 147 148 auto It = WorklistMap.find(N); 149 if (It == WorklistMap.end()) 150 return; // Not in the worklist. 151 152 // Null out the entry rather than erasing it to avoid a linear operation. 153 Worklist[It->second] = nullptr; 154 WorklistMap.erase(It); 155 } 156 157 void deleteAndRecombine(SDNode *N); 158 bool recursivelyDeleteUnusedNodes(SDNode *N); 159 160 /// Replaces all uses of the results of one DAG node with new values. 161 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo, 162 bool AddTo = true); 163 164 /// Replaces all uses of the results of one DAG node with new values. 165 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) { 166 return CombineTo(N, &Res, 1, AddTo); 167 } 168 169 /// Replaces all uses of the results of one DAG node with new values. 170 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1, 171 bool AddTo = true) { 172 SDValue To[] = { Res0, Res1 }; 173 return CombineTo(N, To, 2, AddTo); 174 } 175 176 void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO); 177 178 private: 179 unsigned MaximumLegalStoreInBits; 180 181 /// Check the specified integer node value to see if it can be simplified or 182 /// if things it uses can be simplified by bit propagation. 183 /// If so, return true. 184 bool SimplifyDemandedBits(SDValue Op) { 185 unsigned BitWidth = Op.getScalarValueSizeInBits(); 186 APInt Demanded = APInt::getAllOnesValue(BitWidth); 187 return SimplifyDemandedBits(Op, Demanded); 188 } 189 190 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded); 191 192 bool CombineToPreIndexedLoadStore(SDNode *N); 193 bool CombineToPostIndexedLoadStore(SDNode *N); 194 SDValue SplitIndexingFromLoad(LoadSDNode *LD); 195 bool SliceUpLoad(SDNode *N); 196 197 /// \brief Replace an ISD::EXTRACT_VECTOR_ELT of a load with a narrowed 198 /// load. 199 /// 200 /// \param EVE ISD::EXTRACT_VECTOR_ELT to be replaced. 201 /// \param InVecVT type of the input vector to EVE with bitcasts resolved. 202 /// \param EltNo index of the vector element to load. 203 /// \param OriginalLoad load that EVE came from to be replaced. 204 /// \returns EVE on success SDValue() on failure. 205 SDValue ReplaceExtractVectorEltOfLoadWithNarrowedLoad( 206 SDNode *EVE, EVT InVecVT, SDValue EltNo, LoadSDNode *OriginalLoad); 207 void ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad); 208 SDValue PromoteOperand(SDValue Op, EVT PVT, bool &Replace); 209 SDValue SExtPromoteOperand(SDValue Op, EVT PVT); 210 SDValue ZExtPromoteOperand(SDValue Op, EVT PVT); 211 SDValue PromoteIntBinOp(SDValue Op); 212 SDValue PromoteIntShiftOp(SDValue Op); 213 SDValue PromoteExtend(SDValue Op); 214 bool PromoteLoad(SDValue Op); 215 216 void ExtendSetCCUses(const SmallVectorImpl<SDNode *> &SetCCs, SDValue Trunc, 217 SDValue ExtLoad, const SDLoc &DL, 218 ISD::NodeType ExtType); 219 220 /// Call the node-specific routine that knows how to fold each 221 /// particular type of node. If that doesn't do anything, try the 222 /// target-specific DAG combines. 223 SDValue combine(SDNode *N); 224 225 // Visitation implementation - Implement dag node combining for different 226 // node types. The semantics are as follows: 227 // Return Value: 228 // SDValue.getNode() == 0 - No change was made 229 // SDValue.getNode() == N - N was replaced, is dead and has been handled. 230 // otherwise - N should be replaced by the returned Operand. 231 // 232 SDValue visitTokenFactor(SDNode *N); 233 SDValue visitMERGE_VALUES(SDNode *N); 234 SDValue visitADD(SDNode *N); 235 SDValue visitADDLike(SDValue N0, SDValue N1, SDNode *LocReference); 236 SDValue visitSUB(SDNode *N); 237 SDValue visitADDC(SDNode *N); 238 SDValue visitUADDO(SDNode *N); 239 SDValue visitSUBC(SDNode *N); 240 SDValue visitUSUBO(SDNode *N); 241 SDValue visitADDE(SDNode *N); 242 SDValue visitSUBE(SDNode *N); 243 SDValue visitMUL(SDNode *N); 244 SDValue useDivRem(SDNode *N); 245 SDValue visitSDIV(SDNode *N); 246 SDValue visitUDIV(SDNode *N); 247 SDValue visitREM(SDNode *N); 248 SDValue visitMULHU(SDNode *N); 249 SDValue visitMULHS(SDNode *N); 250 SDValue visitSMUL_LOHI(SDNode *N); 251 SDValue visitUMUL_LOHI(SDNode *N); 252 SDValue visitSMULO(SDNode *N); 253 SDValue visitUMULO(SDNode *N); 254 SDValue visitIMINMAX(SDNode *N); 255 SDValue visitAND(SDNode *N); 256 SDValue visitANDLike(SDValue N0, SDValue N1, SDNode *LocReference); 257 SDValue visitOR(SDNode *N); 258 SDValue visitORLike(SDValue N0, SDValue N1, SDNode *LocReference); 259 SDValue visitXOR(SDNode *N); 260 SDValue SimplifyVBinOp(SDNode *N); 261 SDValue visitSHL(SDNode *N); 262 SDValue visitSRA(SDNode *N); 263 SDValue visitSRL(SDNode *N); 264 SDValue visitRotate(SDNode *N); 265 SDValue visitABS(SDNode *N); 266 SDValue visitBSWAP(SDNode *N); 267 SDValue visitBITREVERSE(SDNode *N); 268 SDValue visitCTLZ(SDNode *N); 269 SDValue visitCTLZ_ZERO_UNDEF(SDNode *N); 270 SDValue visitCTTZ(SDNode *N); 271 SDValue visitCTTZ_ZERO_UNDEF(SDNode *N); 272 SDValue visitCTPOP(SDNode *N); 273 SDValue visitSELECT(SDNode *N); 274 SDValue visitVSELECT(SDNode *N); 275 SDValue visitSELECT_CC(SDNode *N); 276 SDValue visitSETCC(SDNode *N); 277 SDValue visitSETCCE(SDNode *N); 278 SDValue visitSIGN_EXTEND(SDNode *N); 279 SDValue visitZERO_EXTEND(SDNode *N); 280 SDValue visitANY_EXTEND(SDNode *N); 281 SDValue visitAssertZext(SDNode *N); 282 SDValue visitSIGN_EXTEND_INREG(SDNode *N); 283 SDValue visitSIGN_EXTEND_VECTOR_INREG(SDNode *N); 284 SDValue visitZERO_EXTEND_VECTOR_INREG(SDNode *N); 285 SDValue visitTRUNCATE(SDNode *N); 286 SDValue visitBITCAST(SDNode *N); 287 SDValue visitBUILD_PAIR(SDNode *N); 288 SDValue visitFADD(SDNode *N); 289 SDValue visitFSUB(SDNode *N); 290 SDValue visitFMUL(SDNode *N); 291 SDValue visitFMA(SDNode *N); 292 SDValue visitFDIV(SDNode *N); 293 SDValue visitFREM(SDNode *N); 294 SDValue visitFSQRT(SDNode *N); 295 SDValue visitFCOPYSIGN(SDNode *N); 296 SDValue visitSINT_TO_FP(SDNode *N); 297 SDValue visitUINT_TO_FP(SDNode *N); 298 SDValue visitFP_TO_SINT(SDNode *N); 299 SDValue visitFP_TO_UINT(SDNode *N); 300 SDValue visitFP_ROUND(SDNode *N); 301 SDValue visitFP_ROUND_INREG(SDNode *N); 302 SDValue visitFP_EXTEND(SDNode *N); 303 SDValue visitFNEG(SDNode *N); 304 SDValue visitFABS(SDNode *N); 305 SDValue visitFCEIL(SDNode *N); 306 SDValue visitFTRUNC(SDNode *N); 307 SDValue visitFFLOOR(SDNode *N); 308 SDValue visitFMINNUM(SDNode *N); 309 SDValue visitFMAXNUM(SDNode *N); 310 SDValue visitBRCOND(SDNode *N); 311 SDValue visitBR_CC(SDNode *N); 312 SDValue visitLOAD(SDNode *N); 313 314 SDValue replaceStoreChain(StoreSDNode *ST, SDValue BetterChain); 315 SDValue replaceStoreOfFPConstant(StoreSDNode *ST); 316 317 SDValue visitSTORE(SDNode *N); 318 SDValue visitINSERT_VECTOR_ELT(SDNode *N); 319 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N); 320 SDValue visitBUILD_VECTOR(SDNode *N); 321 SDValue visitCONCAT_VECTORS(SDNode *N); 322 SDValue visitEXTRACT_SUBVECTOR(SDNode *N); 323 SDValue visitVECTOR_SHUFFLE(SDNode *N); 324 SDValue visitSCALAR_TO_VECTOR(SDNode *N); 325 SDValue visitINSERT_SUBVECTOR(SDNode *N); 326 SDValue visitMLOAD(SDNode *N); 327 SDValue visitMSTORE(SDNode *N); 328 SDValue visitMGATHER(SDNode *N); 329 SDValue visitMSCATTER(SDNode *N); 330 SDValue visitFP_TO_FP16(SDNode *N); 331 SDValue visitFP16_TO_FP(SDNode *N); 332 333 SDValue visitFADDForFMACombine(SDNode *N); 334 SDValue visitFSUBForFMACombine(SDNode *N); 335 SDValue visitFMULForFMADistributiveCombine(SDNode *N); 336 337 SDValue XformToShuffleWithZero(SDNode *N); 338 SDValue ReassociateOps(unsigned Opc, const SDLoc &DL, SDValue LHS, 339 SDValue RHS); 340 341 SDValue visitShiftByConstant(SDNode *N, ConstantSDNode *Amt); 342 343 SDValue foldSelectOfConstants(SDNode *N); 344 SDValue foldBinOpIntoSelect(SDNode *BO); 345 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS); 346 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N); 347 SDValue SimplifySelect(const SDLoc &DL, SDValue N0, SDValue N1, SDValue N2); 348 SDValue SimplifySelectCC(const SDLoc &DL, SDValue N0, SDValue N1, 349 SDValue N2, SDValue N3, ISD::CondCode CC, 350 bool NotExtCompare = false); 351 SDValue foldSelectCCToShiftAnd(const SDLoc &DL, SDValue N0, SDValue N1, 352 SDValue N2, SDValue N3, ISD::CondCode CC); 353 SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond, 354 const SDLoc &DL, bool foldBooleans = true); 355 356 bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS, 357 SDValue &CC) const; 358 bool isOneUseSetCC(SDValue N) const; 359 360 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp, 361 unsigned HiOp); 362 SDValue CombineConsecutiveLoads(SDNode *N, EVT VT); 363 SDValue CombineExtLoad(SDNode *N); 364 SDValue combineRepeatedFPDivisors(SDNode *N); 365 SDValue ConstantFoldBITCASTofBUILD_VECTOR(SDNode *, EVT); 366 SDValue BuildSDIV(SDNode *N); 367 SDValue BuildSDIVPow2(SDNode *N); 368 SDValue BuildUDIV(SDNode *N); 369 SDValue BuildLogBase2(SDValue Op, const SDLoc &DL); 370 SDValue BuildReciprocalEstimate(SDValue Op, SDNodeFlags *Flags); 371 SDValue buildRsqrtEstimate(SDValue Op, SDNodeFlags *Flags); 372 SDValue buildSqrtEstimate(SDValue Op, SDNodeFlags *Flags); 373 SDValue buildSqrtEstimateImpl(SDValue Op, SDNodeFlags *Flags, bool Recip); 374 SDValue buildSqrtNROneConst(SDValue Op, SDValue Est, unsigned Iterations, 375 SDNodeFlags *Flags, bool Reciprocal); 376 SDValue buildSqrtNRTwoConst(SDValue Op, SDValue Est, unsigned Iterations, 377 SDNodeFlags *Flags, bool Reciprocal); 378 SDValue MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1, 379 bool DemandHighBits = true); 380 SDValue MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1); 381 SDNode *MatchRotatePosNeg(SDValue Shifted, SDValue Pos, SDValue Neg, 382 SDValue InnerPos, SDValue InnerNeg, 383 unsigned PosOpcode, unsigned NegOpcode, 384 const SDLoc &DL); 385 SDNode *MatchRotate(SDValue LHS, SDValue RHS, const SDLoc &DL); 386 SDValue MatchLoadCombine(SDNode *N); 387 SDValue ReduceLoadWidth(SDNode *N); 388 SDValue ReduceLoadOpStoreWidth(SDNode *N); 389 SDValue splitMergedValStore(StoreSDNode *ST); 390 SDValue TransformFPLoadStorePair(SDNode *N); 391 SDValue reduceBuildVecExtToExtBuildVec(SDNode *N); 392 SDValue reduceBuildVecConvertToConvertBuildVec(SDNode *N); 393 SDValue reduceBuildVecToShuffle(SDNode *N); 394 SDValue createBuildVecShuffle(const SDLoc &DL, SDNode *N, 395 ArrayRef<int> VectorMask, SDValue VecIn1, 396 SDValue VecIn2, unsigned LeftIdx); 397 398 SDValue GetDemandedBits(SDValue V, const APInt &Mask); 399 400 /// Walk up chain skipping non-aliasing memory nodes, 401 /// looking for aliasing nodes and adding them to the Aliases vector. 402 void GatherAllAliases(SDNode *N, SDValue OriginalChain, 403 SmallVectorImpl<SDValue> &Aliases); 404 405 /// Return true if there is any possibility that the two addresses overlap. 406 bool isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1) const; 407 408 /// Walk up chain skipping non-aliasing memory nodes, looking for a better 409 /// chain (aliasing node.) 410 SDValue FindBetterChain(SDNode *N, SDValue Chain); 411 412 /// Try to replace a store and any possibly adjacent stores on 413 /// consecutive chains with better chains. Return true only if St is 414 /// replaced. 415 /// 416 /// Notice that other chains may still be replaced even if the function 417 /// returns false. 418 bool findBetterNeighborChains(StoreSDNode *St); 419 420 /// Match "(X shl/srl V1) & V2" where V2 may not be present. 421 bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask); 422 423 /// Holds a pointer to an LSBaseSDNode as well as information on where it 424 /// is located in a sequence of memory operations connected by a chain. 425 struct MemOpLink { 426 MemOpLink(LSBaseSDNode *N, int64_t Offset) 427 : MemNode(N), OffsetFromBase(Offset) {} 428 // Ptr to the mem node. 429 LSBaseSDNode *MemNode; 430 // Offset from the base ptr. 431 int64_t OffsetFromBase; 432 }; 433 434 /// This is a helper function for visitMUL to check the profitability 435 /// of folding (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2). 436 /// MulNode is the original multiply, AddNode is (add x, c1), 437 /// and ConstNode is c2. 438 bool isMulAddWithConstProfitable(SDNode *MulNode, 439 SDValue &AddNode, 440 SDValue &ConstNode); 441 442 443 /// This is a helper function for visitAND and visitZERO_EXTEND. Returns 444 /// true if the (and (load x) c) pattern matches an extload. ExtVT returns 445 /// the type of the loaded value to be extended. LoadedVT returns the type 446 /// of the original loaded value. NarrowLoad returns whether the load would 447 /// need to be narrowed in order to match. 448 bool isAndLoadExtLoad(ConstantSDNode *AndC, LoadSDNode *LoadN, 449 EVT LoadResultTy, EVT &ExtVT, EVT &LoadedVT, 450 bool &NarrowLoad); 451 452 /// This is a helper function for MergeConsecutiveStores. When the source 453 /// elements of the consecutive stores are all constants or all extracted 454 /// vector elements, try to merge them into one larger store. 455 /// \return True if a merged store was created. 456 bool MergeStoresOfConstantsOrVecElts(SmallVectorImpl<MemOpLink> &StoreNodes, 457 EVT MemVT, unsigned NumStores, 458 bool IsConstantSrc, bool UseVector); 459 460 /// This is a helper function for MergeConsecutiveStores. 461 /// Stores that may be merged are placed in StoreNodes. 462 void getStoreMergeCandidates(StoreSDNode *St, 463 SmallVectorImpl<MemOpLink> &StoreNodes); 464 465 /// Helper function for MergeConsecutiveStores. Checks if 466 /// Candidate stores have indirect dependency through their 467 /// operands. \return True if safe to merge 468 bool checkMergeStoreCandidatesForDependencies( 469 SmallVectorImpl<MemOpLink> &StoreNodes); 470 471 /// Merge consecutive store operations into a wide store. 472 /// This optimization uses wide integers or vectors when possible. 473 /// \return number of stores that were merged into a merged store (the 474 /// affected nodes are stored as a prefix in \p StoreNodes). 475 bool MergeConsecutiveStores(StoreSDNode *N); 476 477 /// \brief Try to transform a truncation where C is a constant: 478 /// (trunc (and X, C)) -> (and (trunc X), (trunc C)) 479 /// 480 /// \p N needs to be a truncation and its first operand an AND. Other 481 /// requirements are checked by the function (e.g. that trunc is 482 /// single-use) and if missed an empty SDValue is returned. 483 SDValue distributeTruncateThroughAnd(SDNode *N); 484 485 public: 486 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL) 487 : DAG(D), TLI(D.getTargetLoweringInfo()), Level(BeforeLegalizeTypes), 488 OptLevel(OL), LegalOperations(false), LegalTypes(false), AA(A) { 489 ForCodeSize = DAG.getMachineFunction().getFunction()->optForSize(); 490 491 MaximumLegalStoreInBits = 0; 492 for (MVT VT : MVT::all_valuetypes()) 493 if (EVT(VT).isSimple() && VT != MVT::Other && 494 TLI.isTypeLegal(EVT(VT)) && 495 VT.getSizeInBits() >= MaximumLegalStoreInBits) 496 MaximumLegalStoreInBits = VT.getSizeInBits(); 497 } 498 499 /// Runs the dag combiner on all nodes in the work list 500 void Run(CombineLevel AtLevel); 501 502 SelectionDAG &getDAG() const { return DAG; } 503 504 /// Returns a type large enough to hold any valid shift amount - before type 505 /// legalization these can be huge. 506 EVT getShiftAmountTy(EVT LHSTy) { 507 assert(LHSTy.isInteger() && "Shift amount is not an integer type!"); 508 if (LHSTy.isVector()) 509 return LHSTy; 510 auto &DL = DAG.getDataLayout(); 511 return LegalTypes ? TLI.getScalarShiftAmountTy(DL, LHSTy) 512 : TLI.getPointerTy(DL); 513 } 514 515 /// This method returns true if we are running before type legalization or 516 /// if the specified VT is legal. 517 bool isTypeLegal(const EVT &VT) { 518 if (!LegalTypes) return true; 519 return TLI.isTypeLegal(VT); 520 } 521 522 /// Convenience wrapper around TargetLowering::getSetCCResultType 523 EVT getSetCCResultType(EVT VT) const { 524 return TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT); 525 } 526 }; 527 } 528 529 530 namespace { 531 /// This class is a DAGUpdateListener that removes any deleted 532 /// nodes from the worklist. 533 class WorklistRemover : public SelectionDAG::DAGUpdateListener { 534 DAGCombiner &DC; 535 public: 536 explicit WorklistRemover(DAGCombiner &dc) 537 : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {} 538 539 void NodeDeleted(SDNode *N, SDNode *E) override { 540 DC.removeFromWorklist(N); 541 } 542 }; 543 } 544 545 //===----------------------------------------------------------------------===// 546 // TargetLowering::DAGCombinerInfo implementation 547 //===----------------------------------------------------------------------===// 548 549 void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) { 550 ((DAGCombiner*)DC)->AddToWorklist(N); 551 } 552 553 SDValue TargetLowering::DAGCombinerInfo:: 554 CombineTo(SDNode *N, ArrayRef<SDValue> To, bool AddTo) { 555 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo); 556 } 557 558 SDValue TargetLowering::DAGCombinerInfo:: 559 CombineTo(SDNode *N, SDValue Res, bool AddTo) { 560 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo); 561 } 562 563 564 SDValue TargetLowering::DAGCombinerInfo:: 565 CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) { 566 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo); 567 } 568 569 void TargetLowering::DAGCombinerInfo:: 570 CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) { 571 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO); 572 } 573 574 //===----------------------------------------------------------------------===// 575 // Helper Functions 576 //===----------------------------------------------------------------------===// 577 578 void DAGCombiner::deleteAndRecombine(SDNode *N) { 579 removeFromWorklist(N); 580 581 // If the operands of this node are only used by the node, they will now be 582 // dead. Make sure to re-visit them and recursively delete dead nodes. 583 for (const SDValue &Op : N->ops()) 584 // For an operand generating multiple values, one of the values may 585 // become dead allowing further simplification (e.g. split index 586 // arithmetic from an indexed load). 587 if (Op->hasOneUse() || Op->getNumValues() > 1) 588 AddToWorklist(Op.getNode()); 589 590 DAG.DeleteNode(N); 591 } 592 593 /// Return 1 if we can compute the negated form of the specified expression for 594 /// the same cost as the expression itself, or 2 if we can compute the negated 595 /// form more cheaply than the expression itself. 596 static char isNegatibleForFree(SDValue Op, bool LegalOperations, 597 const TargetLowering &TLI, 598 const TargetOptions *Options, 599 unsigned Depth = 0) { 600 // fneg is removable even if it has multiple uses. 601 if (Op.getOpcode() == ISD::FNEG) return 2; 602 603 // Don't allow anything with multiple uses. 604 if (!Op.hasOneUse()) return 0; 605 606 // Don't recurse exponentially. 607 if (Depth > 6) return 0; 608 609 switch (Op.getOpcode()) { 610 default: return false; 611 case ISD::ConstantFP: { 612 if (!LegalOperations) 613 return 1; 614 615 // Don't invert constant FP values after legalization unless the target says 616 // the negated constant is legal. 617 EVT VT = Op.getValueType(); 618 return TLI.isOperationLegal(ISD::ConstantFP, VT) || 619 TLI.isFPImmLegal(neg(cast<ConstantFPSDNode>(Op)->getValueAPF()), VT); 620 } 621 case ISD::FADD: 622 // FIXME: determine better conditions for this xform. 623 if (!Options->UnsafeFPMath) return 0; 624 625 // After operation legalization, it might not be legal to create new FSUBs. 626 if (LegalOperations && 627 !TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType())) 628 return 0; 629 630 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B) 631 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, 632 Options, Depth + 1)) 633 return V; 634 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A) 635 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options, 636 Depth + 1); 637 case ISD::FSUB: 638 // We can't turn -(A-B) into B-A when we honor signed zeros. 639 if (!Options->NoSignedZerosFPMath && 640 !Op.getNode()->getFlags()->hasNoSignedZeros()) 641 return 0; 642 643 // fold (fneg (fsub A, B)) -> (fsub B, A) 644 return 1; 645 646 case ISD::FMUL: 647 case ISD::FDIV: 648 if (Options->HonorSignDependentRoundingFPMath()) return 0; 649 650 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y)) 651 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, 652 Options, Depth + 1)) 653 return V; 654 655 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options, 656 Depth + 1); 657 658 case ISD::FP_EXTEND: 659 case ISD::FP_ROUND: 660 case ISD::FSIN: 661 return isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, Options, 662 Depth + 1); 663 } 664 } 665 666 /// If isNegatibleForFree returns true, return the newly negated expression. 667 static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG, 668 bool LegalOperations, unsigned Depth = 0) { 669 const TargetOptions &Options = DAG.getTarget().Options; 670 // fneg is removable even if it has multiple uses. 671 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0); 672 673 // Don't allow anything with multiple uses. 674 assert(Op.hasOneUse() && "Unknown reuse!"); 675 676 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree"); 677 678 const SDNodeFlags *Flags = Op.getNode()->getFlags(); 679 680 switch (Op.getOpcode()) { 681 default: llvm_unreachable("Unknown code"); 682 case ISD::ConstantFP: { 683 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF(); 684 V.changeSign(); 685 return DAG.getConstantFP(V, SDLoc(Op), Op.getValueType()); 686 } 687 case ISD::FADD: 688 // FIXME: determine better conditions for this xform. 689 assert(Options.UnsafeFPMath); 690 691 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B) 692 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, 693 DAG.getTargetLoweringInfo(), &Options, Depth+1)) 694 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(), 695 GetNegatedExpression(Op.getOperand(0), DAG, 696 LegalOperations, Depth+1), 697 Op.getOperand(1), Flags); 698 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A) 699 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(), 700 GetNegatedExpression(Op.getOperand(1), DAG, 701 LegalOperations, Depth+1), 702 Op.getOperand(0), Flags); 703 case ISD::FSUB: 704 // fold (fneg (fsub 0, B)) -> B 705 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0))) 706 if (N0CFP->isZero()) 707 return Op.getOperand(1); 708 709 // fold (fneg (fsub A, B)) -> (fsub B, A) 710 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(), 711 Op.getOperand(1), Op.getOperand(0), Flags); 712 713 case ISD::FMUL: 714 case ISD::FDIV: 715 assert(!Options.HonorSignDependentRoundingFPMath()); 716 717 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) 718 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, 719 DAG.getTargetLoweringInfo(), &Options, Depth+1)) 720 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(), 721 GetNegatedExpression(Op.getOperand(0), DAG, 722 LegalOperations, Depth+1), 723 Op.getOperand(1), Flags); 724 725 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y)) 726 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(), 727 Op.getOperand(0), 728 GetNegatedExpression(Op.getOperand(1), DAG, 729 LegalOperations, Depth+1), Flags); 730 731 case ISD::FP_EXTEND: 732 case ISD::FSIN: 733 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(), 734 GetNegatedExpression(Op.getOperand(0), DAG, 735 LegalOperations, Depth+1)); 736 case ISD::FP_ROUND: 737 return DAG.getNode(ISD::FP_ROUND, SDLoc(Op), Op.getValueType(), 738 GetNegatedExpression(Op.getOperand(0), DAG, 739 LegalOperations, Depth+1), 740 Op.getOperand(1)); 741 } 742 } 743 744 // APInts must be the same size for most operations, this helper 745 // function zero extends the shorter of the pair so that they match. 746 // We provide an Offset so that we can create bitwidths that won't overflow. 747 static void zeroExtendToMatch(APInt &LHS, APInt &RHS, unsigned Offset = 0) { 748 unsigned Bits = Offset + std::max(LHS.getBitWidth(), RHS.getBitWidth()); 749 LHS = LHS.zextOrSelf(Bits); 750 RHS = RHS.zextOrSelf(Bits); 751 } 752 753 // Return true if this node is a setcc, or is a select_cc 754 // that selects between the target values used for true and false, making it 755 // equivalent to a setcc. Also, set the incoming LHS, RHS, and CC references to 756 // the appropriate nodes based on the type of node we are checking. This 757 // simplifies life a bit for the callers. 758 bool DAGCombiner::isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS, 759 SDValue &CC) const { 760 if (N.getOpcode() == ISD::SETCC) { 761 LHS = N.getOperand(0); 762 RHS = N.getOperand(1); 763 CC = N.getOperand(2); 764 return true; 765 } 766 767 if (N.getOpcode() != ISD::SELECT_CC || 768 !TLI.isConstTrueVal(N.getOperand(2).getNode()) || 769 !TLI.isConstFalseVal(N.getOperand(3).getNode())) 770 return false; 771 772 if (TLI.getBooleanContents(N.getValueType()) == 773 TargetLowering::UndefinedBooleanContent) 774 return false; 775 776 LHS = N.getOperand(0); 777 RHS = N.getOperand(1); 778 CC = N.getOperand(4); 779 return true; 780 } 781 782 /// Return true if this is a SetCC-equivalent operation with only one use. 783 /// If this is true, it allows the users to invert the operation for free when 784 /// it is profitable to do so. 785 bool DAGCombiner::isOneUseSetCC(SDValue N) const { 786 SDValue N0, N1, N2; 787 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse()) 788 return true; 789 return false; 790 } 791 792 // \brief Returns the SDNode if it is a constant float BuildVector 793 // or constant float. 794 static SDNode *isConstantFPBuildVectorOrConstantFP(SDValue N) { 795 if (isa<ConstantFPSDNode>(N)) 796 return N.getNode(); 797 if (ISD::isBuildVectorOfConstantFPSDNodes(N.getNode())) 798 return N.getNode(); 799 return nullptr; 800 } 801 802 // Determines if it is a constant integer or a build vector of constant 803 // integers (and undefs). 804 // Do not permit build vector implicit truncation. 805 static bool isConstantOrConstantVector(SDValue N, bool NoOpaques = false) { 806 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N)) 807 return !(Const->isOpaque() && NoOpaques); 808 if (N.getOpcode() != ISD::BUILD_VECTOR) 809 return false; 810 unsigned BitWidth = N.getScalarValueSizeInBits(); 811 for (const SDValue &Op : N->op_values()) { 812 if (Op.isUndef()) 813 continue; 814 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(Op); 815 if (!Const || Const->getAPIntValue().getBitWidth() != BitWidth || 816 (Const->isOpaque() && NoOpaques)) 817 return false; 818 } 819 return true; 820 } 821 822 // Determines if it is a constant null integer or a splatted vector of a 823 // constant null integer (with no undefs). 824 // Build vector implicit truncation is not an issue for null values. 825 static bool isNullConstantOrNullSplatConstant(SDValue N) { 826 if (ConstantSDNode *Splat = isConstOrConstSplat(N)) 827 return Splat->isNullValue(); 828 return false; 829 } 830 831 // Determines if it is a constant integer of one or a splatted vector of a 832 // constant integer of one (with no undefs). 833 // Do not permit build vector implicit truncation. 834 static bool isOneConstantOrOneSplatConstant(SDValue N) { 835 unsigned BitWidth = N.getScalarValueSizeInBits(); 836 if (ConstantSDNode *Splat = isConstOrConstSplat(N)) 837 return Splat->isOne() && Splat->getAPIntValue().getBitWidth() == BitWidth; 838 return false; 839 } 840 841 // Determines if it is a constant integer of all ones or a splatted vector of a 842 // constant integer of all ones (with no undefs). 843 // Do not permit build vector implicit truncation. 844 static bool isAllOnesConstantOrAllOnesSplatConstant(SDValue N) { 845 unsigned BitWidth = N.getScalarValueSizeInBits(); 846 if (ConstantSDNode *Splat = isConstOrConstSplat(N)) 847 return Splat->isAllOnesValue() && 848 Splat->getAPIntValue().getBitWidth() == BitWidth; 849 return false; 850 } 851 852 // Determines if a BUILD_VECTOR is composed of all-constants possibly mixed with 853 // undef's. 854 static bool isAnyConstantBuildVector(const SDNode *N) { 855 return ISD::isBuildVectorOfConstantSDNodes(N) || 856 ISD::isBuildVectorOfConstantFPSDNodes(N); 857 } 858 859 SDValue DAGCombiner::ReassociateOps(unsigned Opc, const SDLoc &DL, SDValue N0, 860 SDValue N1) { 861 EVT VT = N0.getValueType(); 862 if (N0.getOpcode() == Opc) { 863 if (SDNode *L = DAG.isConstantIntBuildVectorOrConstantInt(N0.getOperand(1))) { 864 if (SDNode *R = DAG.isConstantIntBuildVectorOrConstantInt(N1)) { 865 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2)) 866 if (SDValue OpNode = DAG.FoldConstantArithmetic(Opc, DL, VT, L, R)) 867 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode); 868 return SDValue(); 869 } 870 if (N0.hasOneUse()) { 871 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one 872 // use 873 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT, N0.getOperand(0), N1); 874 if (!OpNode.getNode()) 875 return SDValue(); 876 AddToWorklist(OpNode.getNode()); 877 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1)); 878 } 879 } 880 } 881 882 if (N1.getOpcode() == Opc) { 883 if (SDNode *R = DAG.isConstantIntBuildVectorOrConstantInt(N1.getOperand(1))) { 884 if (SDNode *L = DAG.isConstantIntBuildVectorOrConstantInt(N0)) { 885 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2)) 886 if (SDValue OpNode = DAG.FoldConstantArithmetic(Opc, DL, VT, R, L)) 887 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode); 888 return SDValue(); 889 } 890 if (N1.hasOneUse()) { 891 // reassoc. (op x, (op y, c1)) -> (op (op x, y), c1) iff x+c1 has one 892 // use 893 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT, N0, N1.getOperand(0)); 894 if (!OpNode.getNode()) 895 return SDValue(); 896 AddToWorklist(OpNode.getNode()); 897 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1)); 898 } 899 } 900 } 901 902 return SDValue(); 903 } 904 905 SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo, 906 bool AddTo) { 907 assert(N->getNumValues() == NumTo && "Broken CombineTo call!"); 908 ++NodesCombined; 909 DEBUG(dbgs() << "\nReplacing.1 "; 910 N->dump(&DAG); 911 dbgs() << "\nWith: "; 912 To[0].getNode()->dump(&DAG); 913 dbgs() << " and " << NumTo-1 << " other values\n"); 914 for (unsigned i = 0, e = NumTo; i != e; ++i) 915 assert((!To[i].getNode() || 916 N->getValueType(i) == To[i].getValueType()) && 917 "Cannot combine value to value of different type!"); 918 919 WorklistRemover DeadNodes(*this); 920 DAG.ReplaceAllUsesWith(N, To); 921 if (AddTo) { 922 // Push the new nodes and any users onto the worklist 923 for (unsigned i = 0, e = NumTo; i != e; ++i) { 924 if (To[i].getNode()) { 925 AddToWorklist(To[i].getNode()); 926 AddUsersToWorklist(To[i].getNode()); 927 } 928 } 929 } 930 931 // Finally, if the node is now dead, remove it from the graph. The node 932 // may not be dead if the replacement process recursively simplified to 933 // something else needing this node. 934 if (N->use_empty()) 935 deleteAndRecombine(N); 936 return SDValue(N, 0); 937 } 938 939 void DAGCombiner:: 940 CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) { 941 // Replace all uses. If any nodes become isomorphic to other nodes and 942 // are deleted, make sure to remove them from our worklist. 943 WorklistRemover DeadNodes(*this); 944 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New); 945 946 // Push the new node and any (possibly new) users onto the worklist. 947 AddToWorklist(TLO.New.getNode()); 948 AddUsersToWorklist(TLO.New.getNode()); 949 950 // Finally, if the node is now dead, remove it from the graph. The node 951 // may not be dead if the replacement process recursively simplified to 952 // something else needing this node. 953 if (TLO.Old.getNode()->use_empty()) 954 deleteAndRecombine(TLO.Old.getNode()); 955 } 956 957 /// Check the specified integer node value to see if it can be simplified or if 958 /// things it uses can be simplified by bit propagation. If so, return true. 959 bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) { 960 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations); 961 APInt KnownZero, KnownOne; 962 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO)) 963 return false; 964 965 // Revisit the node. 966 AddToWorklist(Op.getNode()); 967 968 // Replace the old value with the new one. 969 ++NodesCombined; 970 DEBUG(dbgs() << "\nReplacing.2 "; 971 TLO.Old.getNode()->dump(&DAG); 972 dbgs() << "\nWith: "; 973 TLO.New.getNode()->dump(&DAG); 974 dbgs() << '\n'); 975 976 CommitTargetLoweringOpt(TLO); 977 return true; 978 } 979 980 void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) { 981 SDLoc DL(Load); 982 EVT VT = Load->getValueType(0); 983 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, VT, SDValue(ExtLoad, 0)); 984 985 DEBUG(dbgs() << "\nReplacing.9 "; 986 Load->dump(&DAG); 987 dbgs() << "\nWith: "; 988 Trunc.getNode()->dump(&DAG); 989 dbgs() << '\n'); 990 WorklistRemover DeadNodes(*this); 991 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc); 992 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1)); 993 deleteAndRecombine(Load); 994 AddToWorklist(Trunc.getNode()); 995 } 996 997 SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) { 998 Replace = false; 999 SDLoc DL(Op); 1000 if (ISD::isUNINDEXEDLoad(Op.getNode())) { 1001 LoadSDNode *LD = cast<LoadSDNode>(Op); 1002 EVT MemVT = LD->getMemoryVT(); 1003 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD) 1004 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, PVT, MemVT) ? ISD::ZEXTLOAD 1005 : ISD::EXTLOAD) 1006 : LD->getExtensionType(); 1007 Replace = true; 1008 return DAG.getExtLoad(ExtType, DL, PVT, 1009 LD->getChain(), LD->getBasePtr(), 1010 MemVT, LD->getMemOperand()); 1011 } 1012 1013 unsigned Opc = Op.getOpcode(); 1014 switch (Opc) { 1015 default: break; 1016 case ISD::AssertSext: 1017 return DAG.getNode(ISD::AssertSext, DL, PVT, 1018 SExtPromoteOperand(Op.getOperand(0), PVT), 1019 Op.getOperand(1)); 1020 case ISD::AssertZext: 1021 return DAG.getNode(ISD::AssertZext, DL, PVT, 1022 ZExtPromoteOperand(Op.getOperand(0), PVT), 1023 Op.getOperand(1)); 1024 case ISD::Constant: { 1025 unsigned ExtOpc = 1026 Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 1027 return DAG.getNode(ExtOpc, DL, PVT, Op); 1028 } 1029 } 1030 1031 if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT)) 1032 return SDValue(); 1033 return DAG.getNode(ISD::ANY_EXTEND, DL, PVT, Op); 1034 } 1035 1036 SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) { 1037 if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT)) 1038 return SDValue(); 1039 EVT OldVT = Op.getValueType(); 1040 SDLoc DL(Op); 1041 bool Replace = false; 1042 SDValue NewOp = PromoteOperand(Op, PVT, Replace); 1043 if (!NewOp.getNode()) 1044 return SDValue(); 1045 AddToWorklist(NewOp.getNode()); 1046 1047 if (Replace) 1048 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode()); 1049 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, NewOp.getValueType(), NewOp, 1050 DAG.getValueType(OldVT)); 1051 } 1052 1053 SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) { 1054 EVT OldVT = Op.getValueType(); 1055 SDLoc DL(Op); 1056 bool Replace = false; 1057 SDValue NewOp = PromoteOperand(Op, PVT, Replace); 1058 if (!NewOp.getNode()) 1059 return SDValue(); 1060 AddToWorklist(NewOp.getNode()); 1061 1062 if (Replace) 1063 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode()); 1064 return DAG.getZeroExtendInReg(NewOp, DL, OldVT); 1065 } 1066 1067 /// Promote the specified integer binary operation if the target indicates it is 1068 /// beneficial. e.g. On x86, it's usually better to promote i16 operations to 1069 /// i32 since i16 instructions are longer. 1070 SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) { 1071 if (!LegalOperations) 1072 return SDValue(); 1073 1074 EVT VT = Op.getValueType(); 1075 if (VT.isVector() || !VT.isInteger()) 1076 return SDValue(); 1077 1078 // If operation type is 'undesirable', e.g. i16 on x86, consider 1079 // promoting it. 1080 unsigned Opc = Op.getOpcode(); 1081 if (TLI.isTypeDesirableForOp(Opc, VT)) 1082 return SDValue(); 1083 1084 EVT PVT = VT; 1085 // Consult target whether it is a good idea to promote this operation and 1086 // what's the right type to promote it to. 1087 if (TLI.IsDesirableToPromoteOp(Op, PVT)) { 1088 assert(PVT != VT && "Don't know what type to promote to!"); 1089 1090 bool Replace0 = false; 1091 SDValue N0 = Op.getOperand(0); 1092 SDValue NN0 = PromoteOperand(N0, PVT, Replace0); 1093 if (!NN0.getNode()) 1094 return SDValue(); 1095 1096 bool Replace1 = false; 1097 SDValue N1 = Op.getOperand(1); 1098 SDValue NN1; 1099 if (N0 == N1) 1100 NN1 = NN0; 1101 else { 1102 NN1 = PromoteOperand(N1, PVT, Replace1); 1103 if (!NN1.getNode()) 1104 return SDValue(); 1105 } 1106 1107 AddToWorklist(NN0.getNode()); 1108 if (NN1.getNode()) 1109 AddToWorklist(NN1.getNode()); 1110 1111 if (Replace0) 1112 ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode()); 1113 if (Replace1) 1114 ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode()); 1115 1116 DEBUG(dbgs() << "\nPromoting "; 1117 Op.getNode()->dump(&DAG)); 1118 SDLoc DL(Op); 1119 return DAG.getNode(ISD::TRUNCATE, DL, VT, 1120 DAG.getNode(Opc, DL, PVT, NN0, NN1)); 1121 } 1122 return SDValue(); 1123 } 1124 1125 /// Promote the specified integer shift operation if the target indicates it is 1126 /// beneficial. e.g. On x86, it's usually better to promote i16 operations to 1127 /// i32 since i16 instructions are longer. 1128 SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) { 1129 if (!LegalOperations) 1130 return SDValue(); 1131 1132 EVT VT = Op.getValueType(); 1133 if (VT.isVector() || !VT.isInteger()) 1134 return SDValue(); 1135 1136 // If operation type is 'undesirable', e.g. i16 on x86, consider 1137 // promoting it. 1138 unsigned Opc = Op.getOpcode(); 1139 if (TLI.isTypeDesirableForOp(Opc, VT)) 1140 return SDValue(); 1141 1142 EVT PVT = VT; 1143 // Consult target whether it is a good idea to promote this operation and 1144 // what's the right type to promote it to. 1145 if (TLI.IsDesirableToPromoteOp(Op, PVT)) { 1146 assert(PVT != VT && "Don't know what type to promote to!"); 1147 1148 bool Replace = false; 1149 SDValue N0 = Op.getOperand(0); 1150 if (Opc == ISD::SRA) 1151 N0 = SExtPromoteOperand(Op.getOperand(0), PVT); 1152 else if (Opc == ISD::SRL) 1153 N0 = ZExtPromoteOperand(Op.getOperand(0), PVT); 1154 else 1155 N0 = PromoteOperand(N0, PVT, Replace); 1156 if (!N0.getNode()) 1157 return SDValue(); 1158 1159 AddToWorklist(N0.getNode()); 1160 if (Replace) 1161 ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode()); 1162 1163 DEBUG(dbgs() << "\nPromoting "; 1164 Op.getNode()->dump(&DAG)); 1165 SDLoc DL(Op); 1166 return DAG.getNode(ISD::TRUNCATE, DL, VT, 1167 DAG.getNode(Opc, DL, PVT, N0, Op.getOperand(1))); 1168 } 1169 return SDValue(); 1170 } 1171 1172 SDValue DAGCombiner::PromoteExtend(SDValue Op) { 1173 if (!LegalOperations) 1174 return SDValue(); 1175 1176 EVT VT = Op.getValueType(); 1177 if (VT.isVector() || !VT.isInteger()) 1178 return SDValue(); 1179 1180 // If operation type is 'undesirable', e.g. i16 on x86, consider 1181 // promoting it. 1182 unsigned Opc = Op.getOpcode(); 1183 if (TLI.isTypeDesirableForOp(Opc, VT)) 1184 return SDValue(); 1185 1186 EVT PVT = VT; 1187 // Consult target whether it is a good idea to promote this operation and 1188 // what's the right type to promote it to. 1189 if (TLI.IsDesirableToPromoteOp(Op, PVT)) { 1190 assert(PVT != VT && "Don't know what type to promote to!"); 1191 // fold (aext (aext x)) -> (aext x) 1192 // fold (aext (zext x)) -> (zext x) 1193 // fold (aext (sext x)) -> (sext x) 1194 DEBUG(dbgs() << "\nPromoting "; 1195 Op.getNode()->dump(&DAG)); 1196 return DAG.getNode(Op.getOpcode(), SDLoc(Op), VT, Op.getOperand(0)); 1197 } 1198 return SDValue(); 1199 } 1200 1201 bool DAGCombiner::PromoteLoad(SDValue Op) { 1202 if (!LegalOperations) 1203 return false; 1204 1205 if (!ISD::isUNINDEXEDLoad(Op.getNode())) 1206 return false; 1207 1208 EVT VT = Op.getValueType(); 1209 if (VT.isVector() || !VT.isInteger()) 1210 return false; 1211 1212 // If operation type is 'undesirable', e.g. i16 on x86, consider 1213 // promoting it. 1214 unsigned Opc = Op.getOpcode(); 1215 if (TLI.isTypeDesirableForOp(Opc, VT)) 1216 return false; 1217 1218 EVT PVT = VT; 1219 // Consult target whether it is a good idea to promote this operation and 1220 // what's the right type to promote it to. 1221 if (TLI.IsDesirableToPromoteOp(Op, PVT)) { 1222 assert(PVT != VT && "Don't know what type to promote to!"); 1223 1224 SDLoc DL(Op); 1225 SDNode *N = Op.getNode(); 1226 LoadSDNode *LD = cast<LoadSDNode>(N); 1227 EVT MemVT = LD->getMemoryVT(); 1228 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD) 1229 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, PVT, MemVT) ? ISD::ZEXTLOAD 1230 : ISD::EXTLOAD) 1231 : LD->getExtensionType(); 1232 SDValue NewLD = DAG.getExtLoad(ExtType, DL, PVT, 1233 LD->getChain(), LD->getBasePtr(), 1234 MemVT, LD->getMemOperand()); 1235 SDValue Result = DAG.getNode(ISD::TRUNCATE, DL, VT, NewLD); 1236 1237 DEBUG(dbgs() << "\nPromoting "; 1238 N->dump(&DAG); 1239 dbgs() << "\nTo: "; 1240 Result.getNode()->dump(&DAG); 1241 dbgs() << '\n'); 1242 WorklistRemover DeadNodes(*this); 1243 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result); 1244 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1)); 1245 deleteAndRecombine(N); 1246 AddToWorklist(Result.getNode()); 1247 return true; 1248 } 1249 return false; 1250 } 1251 1252 /// \brief Recursively delete a node which has no uses and any operands for 1253 /// which it is the only use. 1254 /// 1255 /// Note that this both deletes the nodes and removes them from the worklist. 1256 /// It also adds any nodes who have had a user deleted to the worklist as they 1257 /// may now have only one use and subject to other combines. 1258 bool DAGCombiner::recursivelyDeleteUnusedNodes(SDNode *N) { 1259 if (!N->use_empty()) 1260 return false; 1261 1262 SmallSetVector<SDNode *, 16> Nodes; 1263 Nodes.insert(N); 1264 do { 1265 N = Nodes.pop_back_val(); 1266 if (!N) 1267 continue; 1268 1269 if (N->use_empty()) { 1270 for (const SDValue &ChildN : N->op_values()) 1271 Nodes.insert(ChildN.getNode()); 1272 1273 removeFromWorklist(N); 1274 DAG.DeleteNode(N); 1275 } else { 1276 AddToWorklist(N); 1277 } 1278 } while (!Nodes.empty()); 1279 return true; 1280 } 1281 1282 //===----------------------------------------------------------------------===// 1283 // Main DAG Combiner implementation 1284 //===----------------------------------------------------------------------===// 1285 1286 void DAGCombiner::Run(CombineLevel AtLevel) { 1287 // set the instance variables, so that the various visit routines may use it. 1288 Level = AtLevel; 1289 LegalOperations = Level >= AfterLegalizeVectorOps; 1290 LegalTypes = Level >= AfterLegalizeTypes; 1291 1292 // Add all the dag nodes to the worklist. 1293 for (SDNode &Node : DAG.allnodes()) 1294 AddToWorklist(&Node); 1295 1296 // Create a dummy node (which is not added to allnodes), that adds a reference 1297 // to the root node, preventing it from being deleted, and tracking any 1298 // changes of the root. 1299 HandleSDNode Dummy(DAG.getRoot()); 1300 1301 // While the worklist isn't empty, find a node and try to combine it. 1302 while (!WorklistMap.empty()) { 1303 SDNode *N; 1304 // The Worklist holds the SDNodes in order, but it may contain null entries. 1305 do { 1306 N = Worklist.pop_back_val(); 1307 } while (!N); 1308 1309 bool GoodWorklistEntry = WorklistMap.erase(N); 1310 (void)GoodWorklistEntry; 1311 assert(GoodWorklistEntry && 1312 "Found a worklist entry without a corresponding map entry!"); 1313 1314 // If N has no uses, it is dead. Make sure to revisit all N's operands once 1315 // N is deleted from the DAG, since they too may now be dead or may have a 1316 // reduced number of uses, allowing other xforms. 1317 if (recursivelyDeleteUnusedNodes(N)) 1318 continue; 1319 1320 WorklistRemover DeadNodes(*this); 1321 1322 // If this combine is running after legalizing the DAG, re-legalize any 1323 // nodes pulled off the worklist. 1324 if (Level == AfterLegalizeDAG) { 1325 SmallSetVector<SDNode *, 16> UpdatedNodes; 1326 bool NIsValid = DAG.LegalizeOp(N, UpdatedNodes); 1327 1328 for (SDNode *LN : UpdatedNodes) { 1329 AddToWorklist(LN); 1330 AddUsersToWorklist(LN); 1331 } 1332 if (!NIsValid) 1333 continue; 1334 } 1335 1336 DEBUG(dbgs() << "\nCombining: "; N->dump(&DAG)); 1337 1338 // Add any operands of the new node which have not yet been combined to the 1339 // worklist as well. Because the worklist uniques things already, this 1340 // won't repeatedly process the same operand. 1341 CombinedNodes.insert(N); 1342 for (const SDValue &ChildN : N->op_values()) 1343 if (!CombinedNodes.count(ChildN.getNode())) 1344 AddToWorklist(ChildN.getNode()); 1345 1346 SDValue RV = combine(N); 1347 1348 if (!RV.getNode()) 1349 continue; 1350 1351 ++NodesCombined; 1352 1353 // If we get back the same node we passed in, rather than a new node or 1354 // zero, we know that the node must have defined multiple values and 1355 // CombineTo was used. Since CombineTo takes care of the worklist 1356 // mechanics for us, we have no work to do in this case. 1357 if (RV.getNode() == N) 1358 continue; 1359 1360 assert(N->getOpcode() != ISD::DELETED_NODE && 1361 RV.getOpcode() != ISD::DELETED_NODE && 1362 "Node was deleted but visit returned new node!"); 1363 1364 DEBUG(dbgs() << " ... into: "; 1365 RV.getNode()->dump(&DAG)); 1366 1367 if (N->getNumValues() == RV.getNode()->getNumValues()) 1368 DAG.ReplaceAllUsesWith(N, RV.getNode()); 1369 else { 1370 assert(N->getValueType(0) == RV.getValueType() && 1371 N->getNumValues() == 1 && "Type mismatch"); 1372 DAG.ReplaceAllUsesWith(N, &RV); 1373 } 1374 1375 // Push the new node and any users onto the worklist 1376 AddToWorklist(RV.getNode()); 1377 AddUsersToWorklist(RV.getNode()); 1378 1379 // Finally, if the node is now dead, remove it from the graph. The node 1380 // may not be dead if the replacement process recursively simplified to 1381 // something else needing this node. This will also take care of adding any 1382 // operands which have lost a user to the worklist. 1383 recursivelyDeleteUnusedNodes(N); 1384 } 1385 1386 // If the root changed (e.g. it was a dead load, update the root). 1387 DAG.setRoot(Dummy.getValue()); 1388 DAG.RemoveDeadNodes(); 1389 } 1390 1391 SDValue DAGCombiner::visit(SDNode *N) { 1392 switch (N->getOpcode()) { 1393 default: break; 1394 case ISD::TokenFactor: return visitTokenFactor(N); 1395 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N); 1396 case ISD::ADD: return visitADD(N); 1397 case ISD::SUB: return visitSUB(N); 1398 case ISD::ADDC: return visitADDC(N); 1399 case ISD::UADDO: return visitUADDO(N); 1400 case ISD::SUBC: return visitSUBC(N); 1401 case ISD::USUBO: return visitUSUBO(N); 1402 case ISD::ADDE: return visitADDE(N); 1403 case ISD::SUBE: return visitSUBE(N); 1404 case ISD::MUL: return visitMUL(N); 1405 case ISD::SDIV: return visitSDIV(N); 1406 case ISD::UDIV: return visitUDIV(N); 1407 case ISD::SREM: 1408 case ISD::UREM: return visitREM(N); 1409 case ISD::MULHU: return visitMULHU(N); 1410 case ISD::MULHS: return visitMULHS(N); 1411 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N); 1412 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N); 1413 case ISD::SMULO: return visitSMULO(N); 1414 case ISD::UMULO: return visitUMULO(N); 1415 case ISD::SMIN: 1416 case ISD::SMAX: 1417 case ISD::UMIN: 1418 case ISD::UMAX: return visitIMINMAX(N); 1419 case ISD::AND: return visitAND(N); 1420 case ISD::OR: return visitOR(N); 1421 case ISD::XOR: return visitXOR(N); 1422 case ISD::SHL: return visitSHL(N); 1423 case ISD::SRA: return visitSRA(N); 1424 case ISD::SRL: return visitSRL(N); 1425 case ISD::ROTR: 1426 case ISD::ROTL: return visitRotate(N); 1427 case ISD::ABS: return visitABS(N); 1428 case ISD::BSWAP: return visitBSWAP(N); 1429 case ISD::BITREVERSE: return visitBITREVERSE(N); 1430 case ISD::CTLZ: return visitCTLZ(N); 1431 case ISD::CTLZ_ZERO_UNDEF: return visitCTLZ_ZERO_UNDEF(N); 1432 case ISD::CTTZ: return visitCTTZ(N); 1433 case ISD::CTTZ_ZERO_UNDEF: return visitCTTZ_ZERO_UNDEF(N); 1434 case ISD::CTPOP: return visitCTPOP(N); 1435 case ISD::SELECT: return visitSELECT(N); 1436 case ISD::VSELECT: return visitVSELECT(N); 1437 case ISD::SELECT_CC: return visitSELECT_CC(N); 1438 case ISD::SETCC: return visitSETCC(N); 1439 case ISD::SETCCE: return visitSETCCE(N); 1440 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N); 1441 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N); 1442 case ISD::ANY_EXTEND: return visitANY_EXTEND(N); 1443 case ISD::AssertZext: return visitAssertZext(N); 1444 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N); 1445 case ISD::SIGN_EXTEND_VECTOR_INREG: return visitSIGN_EXTEND_VECTOR_INREG(N); 1446 case ISD::ZERO_EXTEND_VECTOR_INREG: return visitZERO_EXTEND_VECTOR_INREG(N); 1447 case ISD::TRUNCATE: return visitTRUNCATE(N); 1448 case ISD::BITCAST: return visitBITCAST(N); 1449 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N); 1450 case ISD::FADD: return visitFADD(N); 1451 case ISD::FSUB: return visitFSUB(N); 1452 case ISD::FMUL: return visitFMUL(N); 1453 case ISD::FMA: return visitFMA(N); 1454 case ISD::FDIV: return visitFDIV(N); 1455 case ISD::FREM: return visitFREM(N); 1456 case ISD::FSQRT: return visitFSQRT(N); 1457 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N); 1458 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N); 1459 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N); 1460 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N); 1461 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N); 1462 case ISD::FP_ROUND: return visitFP_ROUND(N); 1463 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N); 1464 case ISD::FP_EXTEND: return visitFP_EXTEND(N); 1465 case ISD::FNEG: return visitFNEG(N); 1466 case ISD::FABS: return visitFABS(N); 1467 case ISD::FFLOOR: return visitFFLOOR(N); 1468 case ISD::FMINNUM: return visitFMINNUM(N); 1469 case ISD::FMAXNUM: return visitFMAXNUM(N); 1470 case ISD::FCEIL: return visitFCEIL(N); 1471 case ISD::FTRUNC: return visitFTRUNC(N); 1472 case ISD::BRCOND: return visitBRCOND(N); 1473 case ISD::BR_CC: return visitBR_CC(N); 1474 case ISD::LOAD: return visitLOAD(N); 1475 case ISD::STORE: return visitSTORE(N); 1476 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N); 1477 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N); 1478 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N); 1479 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N); 1480 case ISD::EXTRACT_SUBVECTOR: return visitEXTRACT_SUBVECTOR(N); 1481 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N); 1482 case ISD::SCALAR_TO_VECTOR: return visitSCALAR_TO_VECTOR(N); 1483 case ISD::INSERT_SUBVECTOR: return visitINSERT_SUBVECTOR(N); 1484 case ISD::MGATHER: return visitMGATHER(N); 1485 case ISD::MLOAD: return visitMLOAD(N); 1486 case ISD::MSCATTER: return visitMSCATTER(N); 1487 case ISD::MSTORE: return visitMSTORE(N); 1488 case ISD::FP_TO_FP16: return visitFP_TO_FP16(N); 1489 case ISD::FP16_TO_FP: return visitFP16_TO_FP(N); 1490 } 1491 return SDValue(); 1492 } 1493 1494 SDValue DAGCombiner::combine(SDNode *N) { 1495 SDValue RV = visit(N); 1496 1497 // If nothing happened, try a target-specific DAG combine. 1498 if (!RV.getNode()) { 1499 assert(N->getOpcode() != ISD::DELETED_NODE && 1500 "Node was deleted but visit returned NULL!"); 1501 1502 if (N->getOpcode() >= ISD::BUILTIN_OP_END || 1503 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) { 1504 1505 // Expose the DAG combiner to the target combiner impls. 1506 TargetLowering::DAGCombinerInfo 1507 DagCombineInfo(DAG, Level, false, this); 1508 1509 RV = TLI.PerformDAGCombine(N, DagCombineInfo); 1510 } 1511 } 1512 1513 // If nothing happened still, try promoting the operation. 1514 if (!RV.getNode()) { 1515 switch (N->getOpcode()) { 1516 default: break; 1517 case ISD::ADD: 1518 case ISD::SUB: 1519 case ISD::MUL: 1520 case ISD::AND: 1521 case ISD::OR: 1522 case ISD::XOR: 1523 RV = PromoteIntBinOp(SDValue(N, 0)); 1524 break; 1525 case ISD::SHL: 1526 case ISD::SRA: 1527 case ISD::SRL: 1528 RV = PromoteIntShiftOp(SDValue(N, 0)); 1529 break; 1530 case ISD::SIGN_EXTEND: 1531 case ISD::ZERO_EXTEND: 1532 case ISD::ANY_EXTEND: 1533 RV = PromoteExtend(SDValue(N, 0)); 1534 break; 1535 case ISD::LOAD: 1536 if (PromoteLoad(SDValue(N, 0))) 1537 RV = SDValue(N, 0); 1538 break; 1539 } 1540 } 1541 1542 // If N is a commutative binary node, try commuting it to enable more 1543 // sdisel CSE. 1544 if (!RV.getNode() && SelectionDAG::isCommutativeBinOp(N->getOpcode()) && 1545 N->getNumValues() == 1) { 1546 SDValue N0 = N->getOperand(0); 1547 SDValue N1 = N->getOperand(1); 1548 1549 // Constant operands are canonicalized to RHS. 1550 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) { 1551 SDValue Ops[] = {N1, N0}; 1552 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(), Ops, 1553 N->getFlags()); 1554 if (CSENode) 1555 return SDValue(CSENode, 0); 1556 } 1557 } 1558 1559 return RV; 1560 } 1561 1562 /// Given a node, return its input chain if it has one, otherwise return a null 1563 /// sd operand. 1564 static SDValue getInputChainForNode(SDNode *N) { 1565 if (unsigned NumOps = N->getNumOperands()) { 1566 if (N->getOperand(0).getValueType() == MVT::Other) 1567 return N->getOperand(0); 1568 if (N->getOperand(NumOps-1).getValueType() == MVT::Other) 1569 return N->getOperand(NumOps-1); 1570 for (unsigned i = 1; i < NumOps-1; ++i) 1571 if (N->getOperand(i).getValueType() == MVT::Other) 1572 return N->getOperand(i); 1573 } 1574 return SDValue(); 1575 } 1576 1577 SDValue DAGCombiner::visitTokenFactor(SDNode *N) { 1578 // If N has two operands, where one has an input chain equal to the other, 1579 // the 'other' chain is redundant. 1580 if (N->getNumOperands() == 2) { 1581 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1)) 1582 return N->getOperand(0); 1583 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0)) 1584 return N->getOperand(1); 1585 } 1586 1587 SmallVector<SDNode *, 8> TFs; // List of token factors to visit. 1588 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor. 1589 SmallPtrSet<SDNode*, 16> SeenOps; 1590 bool Changed = false; // If we should replace this token factor. 1591 1592 // Start out with this token factor. 1593 TFs.push_back(N); 1594 1595 // Iterate through token factors. The TFs grows when new token factors are 1596 // encountered. 1597 for (unsigned i = 0; i < TFs.size(); ++i) { 1598 SDNode *TF = TFs[i]; 1599 1600 // Check each of the operands. 1601 for (const SDValue &Op : TF->op_values()) { 1602 1603 switch (Op.getOpcode()) { 1604 case ISD::EntryToken: 1605 // Entry tokens don't need to be added to the list. They are 1606 // redundant. 1607 Changed = true; 1608 break; 1609 1610 case ISD::TokenFactor: 1611 if (Op.hasOneUse() && !is_contained(TFs, Op.getNode())) { 1612 // Queue up for processing. 1613 TFs.push_back(Op.getNode()); 1614 // Clean up in case the token factor is removed. 1615 AddToWorklist(Op.getNode()); 1616 Changed = true; 1617 break; 1618 } 1619 LLVM_FALLTHROUGH; 1620 1621 default: 1622 // Only add if it isn't already in the list. 1623 if (SeenOps.insert(Op.getNode()).second) 1624 Ops.push_back(Op); 1625 else 1626 Changed = true; 1627 break; 1628 } 1629 } 1630 } 1631 1632 // Remove Nodes that are chained to another node in the list. Do so 1633 // by walking up chains breath-first stopping when we've seen 1634 // another operand. In general we must climb to the EntryNode, but we can exit 1635 // early if we find all remaining work is associated with just one operand as 1636 // no further pruning is possible. 1637 1638 // List of nodes to search through and original Ops from which they originate. 1639 SmallVector<std::pair<SDNode *, unsigned>, 8> Worklist; 1640 SmallVector<unsigned, 8> OpWorkCount; // Count of work for each Op. 1641 SmallPtrSet<SDNode *, 16> SeenChains; 1642 bool DidPruneOps = false; 1643 1644 unsigned NumLeftToConsider = 0; 1645 for (const SDValue &Op : Ops) { 1646 Worklist.push_back(std::make_pair(Op.getNode(), NumLeftToConsider++)); 1647 OpWorkCount.push_back(1); 1648 } 1649 1650 auto AddToWorklist = [&](unsigned CurIdx, SDNode *Op, unsigned OpNumber) { 1651 // If this is an Op, we can remove the op from the list. Remark any 1652 // search associated with it as from the current OpNumber. 1653 if (SeenOps.count(Op) != 0) { 1654 Changed = true; 1655 DidPruneOps = true; 1656 unsigned OrigOpNumber = 0; 1657 while (Ops[OrigOpNumber].getNode() != Op && OrigOpNumber < Ops.size()) 1658 OrigOpNumber++; 1659 assert((OrigOpNumber != Ops.size()) && 1660 "expected to find TokenFactor Operand"); 1661 // Re-mark worklist from OrigOpNumber to OpNumber 1662 for (unsigned i = CurIdx + 1; i < Worklist.size(); ++i) { 1663 if (Worklist[i].second == OrigOpNumber) { 1664 Worklist[i].second = OpNumber; 1665 } 1666 } 1667 OpWorkCount[OpNumber] += OpWorkCount[OrigOpNumber]; 1668 OpWorkCount[OrigOpNumber] = 0; 1669 NumLeftToConsider--; 1670 } 1671 // Add if it's a new chain 1672 if (SeenChains.insert(Op).second) { 1673 OpWorkCount[OpNumber]++; 1674 Worklist.push_back(std::make_pair(Op, OpNumber)); 1675 } 1676 }; 1677 1678 for (unsigned i = 0; i < Worklist.size() && i < 1024; ++i) { 1679 // We need at least be consider at least 2 Ops to prune. 1680 if (NumLeftToConsider <= 1) 1681 break; 1682 auto CurNode = Worklist[i].first; 1683 auto CurOpNumber = Worklist[i].second; 1684 assert((OpWorkCount[CurOpNumber] > 0) && 1685 "Node should not appear in worklist"); 1686 switch (CurNode->getOpcode()) { 1687 case ISD::EntryToken: 1688 // Hitting EntryToken is the only way for the search to terminate without 1689 // hitting 1690 // another operand's search. Prevent us from marking this operand 1691 // considered. 1692 NumLeftToConsider++; 1693 break; 1694 case ISD::TokenFactor: 1695 for (const SDValue &Op : CurNode->op_values()) 1696 AddToWorklist(i, Op.getNode(), CurOpNumber); 1697 break; 1698 case ISD::CopyFromReg: 1699 case ISD::CopyToReg: 1700 AddToWorklist(i, CurNode->getOperand(0).getNode(), CurOpNumber); 1701 break; 1702 default: 1703 if (auto *MemNode = dyn_cast<MemSDNode>(CurNode)) 1704 AddToWorklist(i, MemNode->getChain().getNode(), CurOpNumber); 1705 break; 1706 } 1707 OpWorkCount[CurOpNumber]--; 1708 if (OpWorkCount[CurOpNumber] == 0) 1709 NumLeftToConsider--; 1710 } 1711 1712 SDValue Result; 1713 1714 // If we've changed things around then replace token factor. 1715 if (Changed) { 1716 if (Ops.empty()) { 1717 // The entry token is the only possible outcome. 1718 Result = DAG.getEntryNode(); 1719 } else { 1720 if (DidPruneOps) { 1721 SmallVector<SDValue, 8> PrunedOps; 1722 // 1723 for (const SDValue &Op : Ops) { 1724 if (SeenChains.count(Op.getNode()) == 0) 1725 PrunedOps.push_back(Op); 1726 } 1727 Result = DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other, PrunedOps); 1728 } else { 1729 Result = DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other, Ops); 1730 } 1731 } 1732 1733 // Add users to worklist, since we may introduce a lot of new 1734 // chained token factors while removing memory deps. 1735 return CombineTo(N, Result, true /*add to worklist*/); 1736 } 1737 1738 return Result; 1739 } 1740 1741 /// MERGE_VALUES can always be eliminated. 1742 SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) { 1743 WorklistRemover DeadNodes(*this); 1744 // Replacing results may cause a different MERGE_VALUES to suddenly 1745 // be CSE'd with N, and carry its uses with it. Iterate until no 1746 // uses remain, to ensure that the node can be safely deleted. 1747 // First add the users of this node to the work list so that they 1748 // can be tried again once they have new operands. 1749 AddUsersToWorklist(N); 1750 do { 1751 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 1752 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i)); 1753 } while (!N->use_empty()); 1754 deleteAndRecombine(N); 1755 return SDValue(N, 0); // Return N so it doesn't get rechecked! 1756 } 1757 1758 /// If \p N is a ConstantSDNode with isOpaque() == false return it casted to a 1759 /// ConstantSDNode pointer else nullptr. 1760 static ConstantSDNode *getAsNonOpaqueConstant(SDValue N) { 1761 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N); 1762 return Const != nullptr && !Const->isOpaque() ? Const : nullptr; 1763 } 1764 1765 SDValue DAGCombiner::foldBinOpIntoSelect(SDNode *BO) { 1766 auto BinOpcode = BO->getOpcode(); 1767 assert((BinOpcode == ISD::ADD || BinOpcode == ISD::SUB || 1768 BinOpcode == ISD::MUL || BinOpcode == ISD::SDIV || 1769 BinOpcode == ISD::UDIV || BinOpcode == ISD::SREM || 1770 BinOpcode == ISD::UREM || BinOpcode == ISD::AND || 1771 BinOpcode == ISD::OR || BinOpcode == ISD::XOR || 1772 BinOpcode == ISD::SHL || BinOpcode == ISD::SRL || 1773 BinOpcode == ISD::SRA || BinOpcode == ISD::FADD || 1774 BinOpcode == ISD::FSUB || BinOpcode == ISD::FMUL || 1775 BinOpcode == ISD::FDIV || BinOpcode == ISD::FREM) && 1776 "Unexpected binary operator"); 1777 1778 // Bail out if any constants are opaque because we can't constant fold those. 1779 SDValue C1 = BO->getOperand(1); 1780 if (!isConstantOrConstantVector(C1, true) && 1781 !isConstantFPBuildVectorOrConstantFP(C1)) 1782 return SDValue(); 1783 1784 // Don't do this unless the old select is going away. We want to eliminate the 1785 // binary operator, not replace a binop with a select. 1786 // TODO: Handle ISD::SELECT_CC. 1787 SDValue Sel = BO->getOperand(0); 1788 if (Sel.getOpcode() != ISD::SELECT || !Sel.hasOneUse()) 1789 return SDValue(); 1790 1791 SDValue CT = Sel.getOperand(1); 1792 if (!isConstantOrConstantVector(CT, true) && 1793 !isConstantFPBuildVectorOrConstantFP(CT)) 1794 return SDValue(); 1795 1796 SDValue CF = Sel.getOperand(2); 1797 if (!isConstantOrConstantVector(CF, true) && 1798 !isConstantFPBuildVectorOrConstantFP(CF)) 1799 return SDValue(); 1800 1801 // We have a select-of-constants followed by a binary operator with a 1802 // constant. Eliminate the binop by pulling the constant math into the select. 1803 // Example: add (select Cond, CT, CF), C1 --> select Cond, CT + C1, CF + C1 1804 EVT VT = Sel.getValueType(); 1805 SDLoc DL(Sel); 1806 SDValue NewCT = DAG.getNode(BinOpcode, DL, VT, CT, C1); 1807 assert((NewCT.isUndef() || isConstantOrConstantVector(NewCT) || 1808 isConstantFPBuildVectorOrConstantFP(NewCT)) && 1809 "Failed to constant fold a binop with constant operands"); 1810 1811 SDValue NewCF = DAG.getNode(BinOpcode, DL, VT, CF, C1); 1812 assert((NewCF.isUndef() || isConstantOrConstantVector(NewCF) || 1813 isConstantFPBuildVectorOrConstantFP(NewCF)) && 1814 "Failed to constant fold a binop with constant operands"); 1815 1816 return DAG.getSelect(DL, VT, Sel.getOperand(0), NewCT, NewCF); 1817 } 1818 1819 SDValue DAGCombiner::visitADD(SDNode *N) { 1820 SDValue N0 = N->getOperand(0); 1821 SDValue N1 = N->getOperand(1); 1822 EVT VT = N0.getValueType(); 1823 SDLoc DL(N); 1824 1825 // fold vector ops 1826 if (VT.isVector()) { 1827 if (SDValue FoldedVOp = SimplifyVBinOp(N)) 1828 return FoldedVOp; 1829 1830 // fold (add x, 0) -> x, vector edition 1831 if (ISD::isBuildVectorAllZeros(N1.getNode())) 1832 return N0; 1833 if (ISD::isBuildVectorAllZeros(N0.getNode())) 1834 return N1; 1835 } 1836 1837 // fold (add x, undef) -> undef 1838 if (N0.isUndef()) 1839 return N0; 1840 1841 if (N1.isUndef()) 1842 return N1; 1843 1844 if (DAG.isConstantIntBuildVectorOrConstantInt(N0)) { 1845 // canonicalize constant to RHS 1846 if (!DAG.isConstantIntBuildVectorOrConstantInt(N1)) 1847 return DAG.getNode(ISD::ADD, DL, VT, N1, N0); 1848 // fold (add c1, c2) -> c1+c2 1849 return DAG.FoldConstantArithmetic(ISD::ADD, DL, VT, N0.getNode(), 1850 N1.getNode()); 1851 } 1852 1853 // fold (add x, 0) -> x 1854 if (isNullConstant(N1)) 1855 return N0; 1856 1857 // fold ((c1-A)+c2) -> (c1+c2)-A 1858 if (isConstantOrConstantVector(N1, /* NoOpaque */ true)) { 1859 if (N0.getOpcode() == ISD::SUB) 1860 if (isConstantOrConstantVector(N0.getOperand(0), /* NoOpaque */ true)) { 1861 return DAG.getNode(ISD::SUB, DL, VT, 1862 DAG.getNode(ISD::ADD, DL, VT, N1, N0.getOperand(0)), 1863 N0.getOperand(1)); 1864 } 1865 } 1866 1867 if (SDValue NewSel = foldBinOpIntoSelect(N)) 1868 return NewSel; 1869 1870 // reassociate add 1871 if (SDValue RADD = ReassociateOps(ISD::ADD, DL, N0, N1)) 1872 return RADD; 1873 1874 // fold ((0-A) + B) -> B-A 1875 if (N0.getOpcode() == ISD::SUB && 1876 isNullConstantOrNullSplatConstant(N0.getOperand(0))) 1877 return DAG.getNode(ISD::SUB, DL, VT, N1, N0.getOperand(1)); 1878 1879 // fold (A + (0-B)) -> A-B 1880 if (N1.getOpcode() == ISD::SUB && 1881 isNullConstantOrNullSplatConstant(N1.getOperand(0))) 1882 return DAG.getNode(ISD::SUB, DL, VT, N0, N1.getOperand(1)); 1883 1884 // fold (A+(B-A)) -> B 1885 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1)) 1886 return N1.getOperand(0); 1887 1888 // fold ((B-A)+A) -> B 1889 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1)) 1890 return N0.getOperand(0); 1891 1892 // fold (A+(B-(A+C))) to (B-C) 1893 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD && 1894 N0 == N1.getOperand(1).getOperand(0)) 1895 return DAG.getNode(ISD::SUB, DL, VT, N1.getOperand(0), 1896 N1.getOperand(1).getOperand(1)); 1897 1898 // fold (A+(B-(C+A))) to (B-C) 1899 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD && 1900 N0 == N1.getOperand(1).getOperand(1)) 1901 return DAG.getNode(ISD::SUB, DL, VT, N1.getOperand(0), 1902 N1.getOperand(1).getOperand(0)); 1903 1904 // fold (A+((B-A)+or-C)) to (B+or-C) 1905 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) && 1906 N1.getOperand(0).getOpcode() == ISD::SUB && 1907 N0 == N1.getOperand(0).getOperand(1)) 1908 return DAG.getNode(N1.getOpcode(), DL, VT, N1.getOperand(0).getOperand(0), 1909 N1.getOperand(1)); 1910 1911 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant 1912 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) { 1913 SDValue N00 = N0.getOperand(0); 1914 SDValue N01 = N0.getOperand(1); 1915 SDValue N10 = N1.getOperand(0); 1916 SDValue N11 = N1.getOperand(1); 1917 1918 if (isConstantOrConstantVector(N00) || isConstantOrConstantVector(N10)) 1919 return DAG.getNode(ISD::SUB, DL, VT, 1920 DAG.getNode(ISD::ADD, SDLoc(N0), VT, N00, N10), 1921 DAG.getNode(ISD::ADD, SDLoc(N1), VT, N01, N11)); 1922 } 1923 1924 if (SimplifyDemandedBits(SDValue(N, 0))) 1925 return SDValue(N, 0); 1926 1927 // fold (a+b) -> (a|b) iff a and b share no bits. 1928 if ((!LegalOperations || TLI.isOperationLegal(ISD::OR, VT)) && 1929 VT.isInteger() && DAG.haveNoCommonBitsSet(N0, N1)) 1930 return DAG.getNode(ISD::OR, DL, VT, N0, N1); 1931 1932 if (SDValue Combined = visitADDLike(N0, N1, N)) 1933 return Combined; 1934 1935 if (SDValue Combined = visitADDLike(N1, N0, N)) 1936 return Combined; 1937 1938 return SDValue(); 1939 } 1940 1941 SDValue DAGCombiner::visitADDLike(SDValue N0, SDValue N1, SDNode *LocReference) { 1942 EVT VT = N0.getValueType(); 1943 SDLoc DL(LocReference); 1944 1945 // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n)) 1946 if (N1.getOpcode() == ISD::SHL && N1.getOperand(0).getOpcode() == ISD::SUB && 1947 isNullConstantOrNullSplatConstant(N1.getOperand(0).getOperand(0))) 1948 return DAG.getNode(ISD::SUB, DL, VT, N0, 1949 DAG.getNode(ISD::SHL, DL, VT, 1950 N1.getOperand(0).getOperand(1), 1951 N1.getOperand(1))); 1952 1953 if (N1.getOpcode() == ISD::AND) { 1954 SDValue AndOp0 = N1.getOperand(0); 1955 unsigned NumSignBits = DAG.ComputeNumSignBits(AndOp0); 1956 unsigned DestBits = VT.getScalarSizeInBits(); 1957 1958 // (add z, (and (sbbl x, x), 1)) -> (sub z, (sbbl x, x)) 1959 // and similar xforms where the inner op is either ~0 or 0. 1960 if (NumSignBits == DestBits && 1961 isOneConstantOrOneSplatConstant(N1->getOperand(1))) 1962 return DAG.getNode(ISD::SUB, DL, VT, N0, AndOp0); 1963 } 1964 1965 // add (sext i1), X -> sub X, (zext i1) 1966 if (N0.getOpcode() == ISD::SIGN_EXTEND && 1967 N0.getOperand(0).getValueType() == MVT::i1 && 1968 !TLI.isOperationLegal(ISD::SIGN_EXTEND, MVT::i1)) { 1969 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)); 1970 return DAG.getNode(ISD::SUB, DL, VT, N1, ZExt); 1971 } 1972 1973 // add X, (sextinreg Y i1) -> sub X, (and Y 1) 1974 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) { 1975 VTSDNode *TN = cast<VTSDNode>(N1.getOperand(1)); 1976 if (TN->getVT() == MVT::i1) { 1977 SDValue ZExt = DAG.getNode(ISD::AND, DL, VT, N1.getOperand(0), 1978 DAG.getConstant(1, DL, VT)); 1979 return DAG.getNode(ISD::SUB, DL, VT, N0, ZExt); 1980 } 1981 } 1982 1983 return SDValue(); 1984 } 1985 1986 SDValue DAGCombiner::visitADDC(SDNode *N) { 1987 SDValue N0 = N->getOperand(0); 1988 SDValue N1 = N->getOperand(1); 1989 EVT VT = N0.getValueType(); 1990 SDLoc DL(N); 1991 1992 // If the flag result is dead, turn this into an ADD. 1993 if (!N->hasAnyUseOfValue(1)) 1994 return CombineTo(N, DAG.getNode(ISD::ADD, DL, VT, N0, N1), 1995 DAG.getNode(ISD::CARRY_FALSE, DL, MVT::Glue)); 1996 1997 // canonicalize constant to RHS. 1998 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); 1999 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); 2000 if (N0C && !N1C) 2001 return DAG.getNode(ISD::ADDC, DL, N->getVTList(), N1, N0); 2002 2003 // fold (addc x, 0) -> x + no carry out 2004 if (isNullConstant(N1)) 2005 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, 2006 DL, MVT::Glue)); 2007 2008 // If it cannot overflow, transform into an add. 2009 if (DAG.computeOverflowKind(N0, N1) == SelectionDAG::OFK_Never) 2010 return CombineTo(N, DAG.getNode(ISD::ADD, DL, VT, N0, N1), 2011 DAG.getNode(ISD::CARRY_FALSE, DL, MVT::Glue)); 2012 2013 return SDValue(); 2014 } 2015 2016 SDValue DAGCombiner::visitUADDO(SDNode *N) { 2017 SDValue N0 = N->getOperand(0); 2018 SDValue N1 = N->getOperand(1); 2019 EVT VT = N0.getValueType(); 2020 if (VT.isVector()) 2021 return SDValue(); 2022 2023 EVT CarryVT = N->getValueType(1); 2024 SDLoc DL(N); 2025 2026 // If the flag result is dead, turn this into an ADD. 2027 if (!N->hasAnyUseOfValue(1)) 2028 return CombineTo(N, DAG.getNode(ISD::ADD, DL, VT, N0, N1), 2029 DAG.getUNDEF(CarryVT)); 2030 2031 // canonicalize constant to RHS. 2032 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); 2033 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); 2034 if (N0C && !N1C) 2035 return DAG.getNode(ISD::UADDO, DL, N->getVTList(), N1, N0); 2036 2037 // fold (uaddo x, 0) -> x + no carry out 2038 if (isNullConstant(N1)) 2039 return CombineTo(N, N0, DAG.getConstant(0, DL, CarryVT)); 2040 2041 // If it cannot overflow, transform into an add. 2042 if (DAG.computeOverflowKind(N0, N1) == SelectionDAG::OFK_Never) 2043 return CombineTo(N, DAG.getNode(ISD::ADD, DL, VT, N0, N1), 2044 DAG.getConstant(0, DL, CarryVT)); 2045 2046 return SDValue(); 2047 } 2048 2049 SDValue DAGCombiner::visitADDE(SDNode *N) { 2050 SDValue N0 = N->getOperand(0); 2051 SDValue N1 = N->getOperand(1); 2052 SDValue CarryIn = N->getOperand(2); 2053 2054 // canonicalize constant to RHS 2055 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); 2056 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); 2057 if (N0C && !N1C) 2058 return DAG.getNode(ISD::ADDE, SDLoc(N), N->getVTList(), 2059 N1, N0, CarryIn); 2060 2061 // fold (adde x, y, false) -> (addc x, y) 2062 if (CarryIn.getOpcode() == ISD::CARRY_FALSE) 2063 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N0, N1); 2064 2065 return SDValue(); 2066 } 2067 2068 // Since it may not be valid to emit a fold to zero for vector initializers 2069 // check if we can before folding. 2070 static SDValue tryFoldToZero(const SDLoc &DL, const TargetLowering &TLI, EVT VT, 2071 SelectionDAG &DAG, bool LegalOperations, 2072 bool LegalTypes) { 2073 if (!VT.isVector()) 2074 return DAG.getConstant(0, DL, VT); 2075 if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) 2076 return DAG.getConstant(0, DL, VT); 2077 return SDValue(); 2078 } 2079 2080 SDValue DAGCombiner::visitSUB(SDNode *N) { 2081 SDValue N0 = N->getOperand(0); 2082 SDValue N1 = N->getOperand(1); 2083 EVT VT = N0.getValueType(); 2084 SDLoc DL(N); 2085 2086 // fold vector ops 2087 if (VT.isVector()) { 2088 if (SDValue FoldedVOp = SimplifyVBinOp(N)) 2089 return FoldedVOp; 2090 2091 // fold (sub x, 0) -> x, vector edition 2092 if (ISD::isBuildVectorAllZeros(N1.getNode())) 2093 return N0; 2094 } 2095 2096 // fold (sub x, x) -> 0 2097 // FIXME: Refactor this and xor and other similar operations together. 2098 if (N0 == N1) 2099 return tryFoldToZero(DL, TLI, VT, DAG, LegalOperations, LegalTypes); 2100 if (DAG.isConstantIntBuildVectorOrConstantInt(N0) && 2101 DAG.isConstantIntBuildVectorOrConstantInt(N1)) { 2102 // fold (sub c1, c2) -> c1-c2 2103 return DAG.FoldConstantArithmetic(ISD::SUB, DL, VT, N0.getNode(), 2104 N1.getNode()); 2105 } 2106 2107 if (SDValue NewSel = foldBinOpIntoSelect(N)) 2108 return NewSel; 2109 2110 ConstantSDNode *N1C = getAsNonOpaqueConstant(N1); 2111 2112 // fold (sub x, c) -> (add x, -c) 2113 if (N1C) { 2114 return DAG.getNode(ISD::ADD, DL, VT, N0, 2115 DAG.getConstant(-N1C->getAPIntValue(), DL, VT)); 2116 } 2117 2118 if (isNullConstantOrNullSplatConstant(N0)) { 2119 unsigned BitWidth = VT.getScalarSizeInBits(); 2120 // Right-shifting everything out but the sign bit followed by negation is 2121 // the same as flipping arithmetic/logical shift type without the negation: 2122 // -(X >>u 31) -> (X >>s 31) 2123 // -(X >>s 31) -> (X >>u 31) 2124 if (N1->getOpcode() == ISD::SRA || N1->getOpcode() == ISD::SRL) { 2125 ConstantSDNode *ShiftAmt = isConstOrConstSplat(N1.getOperand(1)); 2126 if (ShiftAmt && ShiftAmt->getZExtValue() == BitWidth - 1) { 2127 auto NewSh = N1->getOpcode() == ISD::SRA ? ISD::SRL : ISD::SRA; 2128 if (!LegalOperations || TLI.isOperationLegal(NewSh, VT)) 2129 return DAG.getNode(NewSh, DL, VT, N1.getOperand(0), N1.getOperand(1)); 2130 } 2131 } 2132 2133 // 0 - X --> 0 if the sub is NUW. 2134 if (N->getFlags()->hasNoUnsignedWrap()) 2135 return N0; 2136 2137 if (DAG.MaskedValueIsZero(N1, ~APInt::getSignBit(BitWidth))) { 2138 // N1 is either 0 or the minimum signed value. If the sub is NSW, then 2139 // N1 must be 0 because negating the minimum signed value is undefined. 2140 if (N->getFlags()->hasNoSignedWrap()) 2141 return N0; 2142 2143 // 0 - X --> X if X is 0 or the minimum signed value. 2144 return N1; 2145 } 2146 } 2147 2148 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) 2149 if (isAllOnesConstantOrAllOnesSplatConstant(N0)) 2150 return DAG.getNode(ISD::XOR, DL, VT, N1, N0); 2151 2152 // fold A-(A-B) -> B 2153 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0)) 2154 return N1.getOperand(1); 2155 2156 // fold (A+B)-A -> B 2157 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1) 2158 return N0.getOperand(1); 2159 2160 // fold (A+B)-B -> A 2161 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1) 2162 return N0.getOperand(0); 2163 2164 // fold C2-(A+C1) -> (C2-C1)-A 2165 if (N1.getOpcode() == ISD::ADD) { 2166 SDValue N11 = N1.getOperand(1); 2167 if (isConstantOrConstantVector(N0, /* NoOpaques */ true) && 2168 isConstantOrConstantVector(N11, /* NoOpaques */ true)) { 2169 SDValue NewC = DAG.getNode(ISD::SUB, DL, VT, N0, N11); 2170 return DAG.getNode(ISD::SUB, DL, VT, NewC, N1.getOperand(0)); 2171 } 2172 } 2173 2174 // fold ((A+(B+or-C))-B) -> A+or-C 2175 if (N0.getOpcode() == ISD::ADD && 2176 (N0.getOperand(1).getOpcode() == ISD::SUB || 2177 N0.getOperand(1).getOpcode() == ISD::ADD) && 2178 N0.getOperand(1).getOperand(0) == N1) 2179 return DAG.getNode(N0.getOperand(1).getOpcode(), DL, VT, N0.getOperand(0), 2180 N0.getOperand(1).getOperand(1)); 2181 2182 // fold ((A+(C+B))-B) -> A+C 2183 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1).getOpcode() == ISD::ADD && 2184 N0.getOperand(1).getOperand(1) == N1) 2185 return DAG.getNode(ISD::ADD, DL, VT, N0.getOperand(0), 2186 N0.getOperand(1).getOperand(0)); 2187 2188 // fold ((A-(B-C))-C) -> A-B 2189 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1).getOpcode() == ISD::SUB && 2190 N0.getOperand(1).getOperand(1) == N1) 2191 return DAG.getNode(ISD::SUB, DL, VT, N0.getOperand(0), 2192 N0.getOperand(1).getOperand(0)); 2193 2194 // If either operand of a sub is undef, the result is undef 2195 if (N0.isUndef()) 2196 return N0; 2197 if (N1.isUndef()) 2198 return N1; 2199 2200 // If the relocation model supports it, consider symbol offsets. 2201 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0)) 2202 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) { 2203 // fold (sub Sym, c) -> Sym-c 2204 if (N1C && GA->getOpcode() == ISD::GlobalAddress) 2205 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT, 2206 GA->getOffset() - 2207 (uint64_t)N1C->getSExtValue()); 2208 // fold (sub Sym+c1, Sym+c2) -> c1-c2 2209 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1)) 2210 if (GA->getGlobal() == GB->getGlobal()) 2211 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(), 2212 DL, VT); 2213 } 2214 2215 // sub X, (sextinreg Y i1) -> add X, (and Y 1) 2216 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) { 2217 VTSDNode *TN = cast<VTSDNode>(N1.getOperand(1)); 2218 if (TN->getVT() == MVT::i1) { 2219 SDValue ZExt = DAG.getNode(ISD::AND, DL, VT, N1.getOperand(0), 2220 DAG.getConstant(1, DL, VT)); 2221 return DAG.getNode(ISD::ADD, DL, VT, N0, ZExt); 2222 } 2223 } 2224 2225 return SDValue(); 2226 } 2227 2228 SDValue DAGCombiner::visitSUBC(SDNode *N) { 2229 SDValue N0 = N->getOperand(0); 2230 SDValue N1 = N->getOperand(1); 2231 EVT VT = N0.getValueType(); 2232 SDLoc DL(N); 2233 2234 // If the flag result is dead, turn this into an SUB. 2235 if (!N->hasAnyUseOfValue(1)) 2236 return CombineTo(N, DAG.getNode(ISD::SUB, DL, VT, N0, N1), 2237 DAG.getNode(ISD::CARRY_FALSE, DL, MVT::Glue)); 2238 2239 // fold (subc x, x) -> 0 + no borrow 2240 if (N0 == N1) 2241 return CombineTo(N, DAG.getConstant(0, DL, VT), 2242 DAG.getNode(ISD::CARRY_FALSE, DL, MVT::Glue)); 2243 2244 // fold (subc x, 0) -> x + no borrow 2245 if (isNullConstant(N1)) 2246 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, DL, MVT::Glue)); 2247 2248 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) + no borrow 2249 if (isAllOnesConstant(N0)) 2250 return CombineTo(N, DAG.getNode(ISD::XOR, DL, VT, N1, N0), 2251 DAG.getNode(ISD::CARRY_FALSE, DL, MVT::Glue)); 2252 2253 return SDValue(); 2254 } 2255 2256 SDValue DAGCombiner::visitUSUBO(SDNode *N) { 2257 SDValue N0 = N->getOperand(0); 2258 SDValue N1 = N->getOperand(1); 2259 EVT VT = N0.getValueType(); 2260 if (VT.isVector()) 2261 return SDValue(); 2262 2263 EVT CarryVT = N->getValueType(1); 2264 SDLoc DL(N); 2265 2266 // If the flag result is dead, turn this into an SUB. 2267 if (!N->hasAnyUseOfValue(1)) 2268 return CombineTo(N, DAG.getNode(ISD::SUB, DL, VT, N0, N1), 2269 DAG.getUNDEF(CarryVT)); 2270 2271 // fold (usubo x, x) -> 0 + no borrow 2272 if (N0 == N1) 2273 return CombineTo(N, DAG.getConstant(0, DL, VT), 2274 DAG.getConstant(0, DL, CarryVT)); 2275 2276 // fold (usubo x, 0) -> x + no borrow 2277 if (isNullConstant(N1)) 2278 return CombineTo(N, N0, DAG.getConstant(0, DL, CarryVT)); 2279 2280 // Canonicalize (usubo -1, x) -> ~x, i.e. (xor x, -1) + no borrow 2281 if (isAllOnesConstant(N0)) 2282 return CombineTo(N, DAG.getNode(ISD::XOR, DL, VT, N1, N0), 2283 DAG.getConstant(0, DL, CarryVT)); 2284 2285 return SDValue(); 2286 } 2287 2288 SDValue DAGCombiner::visitSUBE(SDNode *N) { 2289 SDValue N0 = N->getOperand(0); 2290 SDValue N1 = N->getOperand(1); 2291 SDValue CarryIn = N->getOperand(2); 2292 2293 // fold (sube x, y, false) -> (subc x, y) 2294 if (CarryIn.getOpcode() == ISD::CARRY_FALSE) 2295 return DAG.getNode(ISD::SUBC, SDLoc(N), N->getVTList(), N0, N1); 2296 2297 return SDValue(); 2298 } 2299 2300 SDValue DAGCombiner::visitMUL(SDNode *N) { 2301 SDValue N0 = N->getOperand(0); 2302 SDValue N1 = N->getOperand(1); 2303 EVT VT = N0.getValueType(); 2304 2305 // fold (mul x, undef) -> 0 2306 if (N0.isUndef() || N1.isUndef()) 2307 return DAG.getConstant(0, SDLoc(N), VT); 2308 2309 bool N0IsConst = false; 2310 bool N1IsConst = false; 2311 bool N1IsOpaqueConst = false; 2312 bool N0IsOpaqueConst = false; 2313 APInt ConstValue0, ConstValue1; 2314 // fold vector ops 2315 if (VT.isVector()) { 2316 if (SDValue FoldedVOp = SimplifyVBinOp(N)) 2317 return FoldedVOp; 2318 2319 N0IsConst = ISD::isConstantSplatVector(N0.getNode(), ConstValue0); 2320 N1IsConst = ISD::isConstantSplatVector(N1.getNode(), ConstValue1); 2321 } else { 2322 N0IsConst = isa<ConstantSDNode>(N0); 2323 if (N0IsConst) { 2324 ConstValue0 = cast<ConstantSDNode>(N0)->getAPIntValue(); 2325 N0IsOpaqueConst = cast<ConstantSDNode>(N0)->isOpaque(); 2326 } 2327 N1IsConst = isa<ConstantSDNode>(N1); 2328 if (N1IsConst) { 2329 ConstValue1 = cast<ConstantSDNode>(N1)->getAPIntValue(); 2330 N1IsOpaqueConst = cast<ConstantSDNode>(N1)->isOpaque(); 2331 } 2332 } 2333 2334 // fold (mul c1, c2) -> c1*c2 2335 if (N0IsConst && N1IsConst && !N0IsOpaqueConst && !N1IsOpaqueConst) 2336 return DAG.FoldConstantArithmetic(ISD::MUL, SDLoc(N), VT, 2337 N0.getNode(), N1.getNode()); 2338 2339 // canonicalize constant to RHS (vector doesn't have to splat) 2340 if (DAG.isConstantIntBuildVectorOrConstantInt(N0) && 2341 !DAG.isConstantIntBuildVectorOrConstantInt(N1)) 2342 return DAG.getNode(ISD::MUL, SDLoc(N), VT, N1, N0); 2343 // fold (mul x, 0) -> 0 2344 if (N1IsConst && ConstValue1 == 0) 2345 return N1; 2346 // We require a splat of the entire scalar bit width for non-contiguous 2347 // bit patterns. 2348 bool IsFullSplat = 2349 ConstValue1.getBitWidth() == VT.getScalarSizeInBits(); 2350 // fold (mul x, 1) -> x 2351 if (N1IsConst && ConstValue1 == 1 && IsFullSplat) 2352 return N0; 2353 2354 if (SDValue NewSel = foldBinOpIntoSelect(N)) 2355 return NewSel; 2356 2357 // fold (mul x, -1) -> 0-x 2358 if (N1IsConst && ConstValue1.isAllOnesValue()) { 2359 SDLoc DL(N); 2360 return DAG.getNode(ISD::SUB, DL, VT, 2361 DAG.getConstant(0, DL, VT), N0); 2362 } 2363 // fold (mul x, (1 << c)) -> x << c 2364 if (N1IsConst && !N1IsOpaqueConst && ConstValue1.isPowerOf2() && 2365 IsFullSplat) { 2366 SDLoc DL(N); 2367 return DAG.getNode(ISD::SHL, DL, VT, N0, 2368 DAG.getConstant(ConstValue1.logBase2(), DL, 2369 getShiftAmountTy(N0.getValueType()))); 2370 } 2371 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c 2372 if (N1IsConst && !N1IsOpaqueConst && (-ConstValue1).isPowerOf2() && 2373 IsFullSplat) { 2374 unsigned Log2Val = (-ConstValue1).logBase2(); 2375 SDLoc DL(N); 2376 // FIXME: If the input is something that is easily negated (e.g. a 2377 // single-use add), we should put the negate there. 2378 return DAG.getNode(ISD::SUB, DL, VT, 2379 DAG.getConstant(0, DL, VT), 2380 DAG.getNode(ISD::SHL, DL, VT, N0, 2381 DAG.getConstant(Log2Val, DL, 2382 getShiftAmountTy(N0.getValueType())))); 2383 } 2384 2385 // (mul (shl X, c1), c2) -> (mul X, c2 << c1) 2386 if (N0.getOpcode() == ISD::SHL && 2387 isConstantOrConstantVector(N1, /* NoOpaques */ true) && 2388 isConstantOrConstantVector(N0.getOperand(1), /* NoOpaques */ true)) { 2389 SDValue C3 = DAG.getNode(ISD::SHL, SDLoc(N), VT, N1, N0.getOperand(1)); 2390 if (isConstantOrConstantVector(C3)) 2391 return DAG.getNode(ISD::MUL, SDLoc(N), VT, N0.getOperand(0), C3); 2392 } 2393 2394 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one 2395 // use. 2396 { 2397 SDValue Sh(nullptr, 0), Y(nullptr, 0); 2398 2399 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)). 2400 if (N0.getOpcode() == ISD::SHL && 2401 isConstantOrConstantVector(N0.getOperand(1)) && 2402 N0.getNode()->hasOneUse()) { 2403 Sh = N0; Y = N1; 2404 } else if (N1.getOpcode() == ISD::SHL && 2405 isConstantOrConstantVector(N1.getOperand(1)) && 2406 N1.getNode()->hasOneUse()) { 2407 Sh = N1; Y = N0; 2408 } 2409 2410 if (Sh.getNode()) { 2411 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT, Sh.getOperand(0), Y); 2412 return DAG.getNode(ISD::SHL, SDLoc(N), VT, Mul, Sh.getOperand(1)); 2413 } 2414 } 2415 2416 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2) 2417 if (DAG.isConstantIntBuildVectorOrConstantInt(N1) && 2418 N0.getOpcode() == ISD::ADD && 2419 DAG.isConstantIntBuildVectorOrConstantInt(N0.getOperand(1)) && 2420 isMulAddWithConstProfitable(N, N0, N1)) 2421 return DAG.getNode(ISD::ADD, SDLoc(N), VT, 2422 DAG.getNode(ISD::MUL, SDLoc(N0), VT, 2423 N0.getOperand(0), N1), 2424 DAG.getNode(ISD::MUL, SDLoc(N1), VT, 2425 N0.getOperand(1), N1)); 2426 2427 // reassociate mul 2428 if (SDValue RMUL = ReassociateOps(ISD::MUL, SDLoc(N), N0, N1)) 2429 return RMUL; 2430 2431 return SDValue(); 2432 } 2433 2434 /// Return true if divmod libcall is available. 2435 static bool isDivRemLibcallAvailable(SDNode *Node, bool isSigned, 2436 const TargetLowering &TLI) { 2437 RTLIB::Libcall LC; 2438 EVT NodeType = Node->getValueType(0); 2439 if (!NodeType.isSimple()) 2440 return false; 2441 switch (NodeType.getSimpleVT().SimpleTy) { 2442 default: return false; // No libcall for vector types. 2443 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break; 2444 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break; 2445 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break; 2446 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break; 2447 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break; 2448 } 2449 2450 return TLI.getLibcallName(LC) != nullptr; 2451 } 2452 2453 /// Issue divrem if both quotient and remainder are needed. 2454 SDValue DAGCombiner::useDivRem(SDNode *Node) { 2455 if (Node->use_empty()) 2456 return SDValue(); // This is a dead node, leave it alone. 2457 2458 unsigned Opcode = Node->getOpcode(); 2459 bool isSigned = (Opcode == ISD::SDIV) || (Opcode == ISD::SREM); 2460 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM; 2461 2462 // DivMod lib calls can still work on non-legal types if using lib-calls. 2463 EVT VT = Node->getValueType(0); 2464 if (VT.isVector() || !VT.isInteger()) 2465 return SDValue(); 2466 2467 if (!TLI.isTypeLegal(VT) && !TLI.isOperationCustom(DivRemOpc, VT)) 2468 return SDValue(); 2469 2470 // If DIVREM is going to get expanded into a libcall, 2471 // but there is no libcall available, then don't combine. 2472 if (!TLI.isOperationLegalOrCustom(DivRemOpc, VT) && 2473 !isDivRemLibcallAvailable(Node, isSigned, TLI)) 2474 return SDValue(); 2475 2476 // If div is legal, it's better to do the normal expansion 2477 unsigned OtherOpcode = 0; 2478 if ((Opcode == ISD::SDIV) || (Opcode == ISD::UDIV)) { 2479 OtherOpcode = isSigned ? ISD::SREM : ISD::UREM; 2480 if (TLI.isOperationLegalOrCustom(Opcode, VT)) 2481 return SDValue(); 2482 } else { 2483 OtherOpcode = isSigned ? ISD::SDIV : ISD::UDIV; 2484 if (TLI.isOperationLegalOrCustom(OtherOpcode, VT)) 2485 return SDValue(); 2486 } 2487 2488 SDValue Op0 = Node->getOperand(0); 2489 SDValue Op1 = Node->getOperand(1); 2490 SDValue combined; 2491 for (SDNode::use_iterator UI = Op0.getNode()->use_begin(), 2492 UE = Op0.getNode()->use_end(); UI != UE;) { 2493 SDNode *User = *UI++; 2494 if (User == Node || User->use_empty()) 2495 continue; 2496 // Convert the other matching node(s), too; 2497 // otherwise, the DIVREM may get target-legalized into something 2498 // target-specific that we won't be able to recognize. 2499 unsigned UserOpc = User->getOpcode(); 2500 if ((UserOpc == Opcode || UserOpc == OtherOpcode || UserOpc == DivRemOpc) && 2501 User->getOperand(0) == Op0 && 2502 User->getOperand(1) == Op1) { 2503 if (!combined) { 2504 if (UserOpc == OtherOpcode) { 2505 SDVTList VTs = DAG.getVTList(VT, VT); 2506 combined = DAG.getNode(DivRemOpc, SDLoc(Node), VTs, Op0, Op1); 2507 } else if (UserOpc == DivRemOpc) { 2508 combined = SDValue(User, 0); 2509 } else { 2510 assert(UserOpc == Opcode); 2511 continue; 2512 } 2513 } 2514 if (UserOpc == ISD::SDIV || UserOpc == ISD::UDIV) 2515 CombineTo(User, combined); 2516 else if (UserOpc == ISD::SREM || UserOpc == ISD::UREM) 2517 CombineTo(User, combined.getValue(1)); 2518 } 2519 } 2520 return combined; 2521 } 2522 2523 static SDValue simplifyDivRem(SDNode *N, SelectionDAG &DAG) { 2524 SDValue N0 = N->getOperand(0); 2525 SDValue N1 = N->getOperand(1); 2526 EVT VT = N->getValueType(0); 2527 SDLoc DL(N); 2528 2529 if (DAG.isUndef(N->getOpcode(), {N0, N1})) 2530 return DAG.getUNDEF(VT); 2531 2532 // undef / X -> 0 2533 // undef % X -> 0 2534 if (N0.isUndef()) 2535 return DAG.getConstant(0, DL, VT); 2536 2537 return SDValue(); 2538 } 2539 2540 SDValue DAGCombiner::visitSDIV(SDNode *N) { 2541 SDValue N0 = N->getOperand(0); 2542 SDValue N1 = N->getOperand(1); 2543 EVT VT = N->getValueType(0); 2544 2545 // fold vector ops 2546 if (VT.isVector()) 2547 if (SDValue FoldedVOp = SimplifyVBinOp(N)) 2548 return FoldedVOp; 2549 2550 SDLoc DL(N); 2551 2552 // fold (sdiv c1, c2) -> c1/c2 2553 ConstantSDNode *N0C = isConstOrConstSplat(N0); 2554 ConstantSDNode *N1C = isConstOrConstSplat(N1); 2555 if (N0C && N1C && !N0C->isOpaque() && !N1C->isOpaque()) 2556 return DAG.FoldConstantArithmetic(ISD::SDIV, DL, VT, N0C, N1C); 2557 // fold (sdiv X, 1) -> X 2558 if (N1C && N1C->isOne()) 2559 return N0; 2560 // fold (sdiv X, -1) -> 0-X 2561 if (N1C && N1C->isAllOnesValue()) 2562 return DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), N0); 2563 2564 if (SDValue V = simplifyDivRem(N, DAG)) 2565 return V; 2566 2567 if (SDValue NewSel = foldBinOpIntoSelect(N)) 2568 return NewSel; 2569 2570 // If we know the sign bits of both operands are zero, strength reduce to a 2571 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2 2572 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0)) 2573 return DAG.getNode(ISD::UDIV, DL, N1.getValueType(), N0, N1); 2574 2575 // fold (sdiv X, pow2) -> simple ops after legalize 2576 // FIXME: We check for the exact bit here because the generic lowering gives 2577 // better results in that case. The target-specific lowering should learn how 2578 // to handle exact sdivs efficiently. 2579 if (N1C && !N1C->isNullValue() && !N1C->isOpaque() && 2580 !cast<BinaryWithFlagsSDNode>(N)->Flags.hasExact() && 2581 (N1C->getAPIntValue().isPowerOf2() || 2582 (-N1C->getAPIntValue()).isPowerOf2())) { 2583 // Target-specific implementation of sdiv x, pow2. 2584 if (SDValue Res = BuildSDIVPow2(N)) 2585 return Res; 2586 2587 unsigned lg2 = N1C->getAPIntValue().countTrailingZeros(); 2588 2589 // Splat the sign bit into the register 2590 SDValue SGN = 2591 DAG.getNode(ISD::SRA, DL, VT, N0, 2592 DAG.getConstant(VT.getScalarSizeInBits() - 1, DL, 2593 getShiftAmountTy(N0.getValueType()))); 2594 AddToWorklist(SGN.getNode()); 2595 2596 // Add (N0 < 0) ? abs2 - 1 : 0; 2597 SDValue SRL = 2598 DAG.getNode(ISD::SRL, DL, VT, SGN, 2599 DAG.getConstant(VT.getScalarSizeInBits() - lg2, DL, 2600 getShiftAmountTy(SGN.getValueType()))); 2601 SDValue ADD = DAG.getNode(ISD::ADD, DL, VT, N0, SRL); 2602 AddToWorklist(SRL.getNode()); 2603 AddToWorklist(ADD.getNode()); // Divide by pow2 2604 SDValue SRA = DAG.getNode(ISD::SRA, DL, VT, ADD, 2605 DAG.getConstant(lg2, DL, 2606 getShiftAmountTy(ADD.getValueType()))); 2607 2608 // If we're dividing by a positive value, we're done. Otherwise, we must 2609 // negate the result. 2610 if (N1C->getAPIntValue().isNonNegative()) 2611 return SRA; 2612 2613 AddToWorklist(SRA.getNode()); 2614 return DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), SRA); 2615 } 2616 2617 // If integer divide is expensive and we satisfy the requirements, emit an 2618 // alternate sequence. Targets may check function attributes for size/speed 2619 // trade-offs. 2620 AttributeList Attr = DAG.getMachineFunction().getFunction()->getAttributes(); 2621 if (N1C && !TLI.isIntDivCheap(N->getValueType(0), Attr)) 2622 if (SDValue Op = BuildSDIV(N)) 2623 return Op; 2624 2625 // sdiv, srem -> sdivrem 2626 // If the divisor is constant, then return DIVREM only if isIntDivCheap() is 2627 // true. Otherwise, we break the simplification logic in visitREM(). 2628 if (!N1C || TLI.isIntDivCheap(N->getValueType(0), Attr)) 2629 if (SDValue DivRem = useDivRem(N)) 2630 return DivRem; 2631 2632 return SDValue(); 2633 } 2634 2635 SDValue DAGCombiner::visitUDIV(SDNode *N) { 2636 SDValue N0 = N->getOperand(0); 2637 SDValue N1 = N->getOperand(1); 2638 EVT VT = N->getValueType(0); 2639 2640 // fold vector ops 2641 if (VT.isVector()) 2642 if (SDValue FoldedVOp = SimplifyVBinOp(N)) 2643 return FoldedVOp; 2644 2645 SDLoc DL(N); 2646 2647 // fold (udiv c1, c2) -> c1/c2 2648 ConstantSDNode *N0C = isConstOrConstSplat(N0); 2649 ConstantSDNode *N1C = isConstOrConstSplat(N1); 2650 if (N0C && N1C) 2651 if (SDValue Folded = DAG.FoldConstantArithmetic(ISD::UDIV, DL, VT, 2652 N0C, N1C)) 2653 return Folded; 2654 2655 if (SDValue V = simplifyDivRem(N, DAG)) 2656 return V; 2657 2658 if (SDValue NewSel = foldBinOpIntoSelect(N)) 2659 return NewSel; 2660 2661 // fold (udiv x, (1 << c)) -> x >>u c 2662 if (isConstantOrConstantVector(N1, /*NoOpaques*/ true) && 2663 DAG.isKnownToBeAPowerOfTwo(N1)) { 2664 SDValue LogBase2 = BuildLogBase2(N1, DL); 2665 AddToWorklist(LogBase2.getNode()); 2666 2667 EVT ShiftVT = getShiftAmountTy(N0.getValueType()); 2668 SDValue Trunc = DAG.getZExtOrTrunc(LogBase2, DL, ShiftVT); 2669 AddToWorklist(Trunc.getNode()); 2670 return DAG.getNode(ISD::SRL, DL, VT, N0, Trunc); 2671 } 2672 2673 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2 2674 if (N1.getOpcode() == ISD::SHL) { 2675 SDValue N10 = N1.getOperand(0); 2676 if (isConstantOrConstantVector(N10, /*NoOpaques*/ true) && 2677 DAG.isKnownToBeAPowerOfTwo(N10)) { 2678 SDValue LogBase2 = BuildLogBase2(N10, DL); 2679 AddToWorklist(LogBase2.getNode()); 2680 2681 EVT ADDVT = N1.getOperand(1).getValueType(); 2682 SDValue Trunc = DAG.getZExtOrTrunc(LogBase2, DL, ADDVT); 2683 AddToWorklist(Trunc.getNode()); 2684 SDValue Add = DAG.getNode(ISD::ADD, DL, ADDVT, N1.getOperand(1), Trunc); 2685 AddToWorklist(Add.getNode()); 2686 return DAG.getNode(ISD::SRL, DL, VT, N0, Add); 2687 } 2688 } 2689 2690 // fold (udiv x, c) -> alternate 2691 AttributeList Attr = DAG.getMachineFunction().getFunction()->getAttributes(); 2692 if (N1C && !TLI.isIntDivCheap(N->getValueType(0), Attr)) 2693 if (SDValue Op = BuildUDIV(N)) 2694 return Op; 2695 2696 // sdiv, srem -> sdivrem 2697 // If the divisor is constant, then return DIVREM only if isIntDivCheap() is 2698 // true. Otherwise, we break the simplification logic in visitREM(). 2699 if (!N1C || TLI.isIntDivCheap(N->getValueType(0), Attr)) 2700 if (SDValue DivRem = useDivRem(N)) 2701 return DivRem; 2702 2703 return SDValue(); 2704 } 2705 2706 // handles ISD::SREM and ISD::UREM 2707 SDValue DAGCombiner::visitREM(SDNode *N) { 2708 unsigned Opcode = N->getOpcode(); 2709 SDValue N0 = N->getOperand(0); 2710 SDValue N1 = N->getOperand(1); 2711 EVT VT = N->getValueType(0); 2712 bool isSigned = (Opcode == ISD::SREM); 2713 SDLoc DL(N); 2714 2715 // fold (rem c1, c2) -> c1%c2 2716 ConstantSDNode *N0C = isConstOrConstSplat(N0); 2717 ConstantSDNode *N1C = isConstOrConstSplat(N1); 2718 if (N0C && N1C) 2719 if (SDValue Folded = DAG.FoldConstantArithmetic(Opcode, DL, VT, N0C, N1C)) 2720 return Folded; 2721 2722 if (SDValue V = simplifyDivRem(N, DAG)) 2723 return V; 2724 2725 if (SDValue NewSel = foldBinOpIntoSelect(N)) 2726 return NewSel; 2727 2728 if (isSigned) { 2729 // If we know the sign bits of both operands are zero, strength reduce to a 2730 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15 2731 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0)) 2732 return DAG.getNode(ISD::UREM, DL, VT, N0, N1); 2733 } else { 2734 SDValue NegOne = DAG.getAllOnesConstant(DL, VT); 2735 if (DAG.isKnownToBeAPowerOfTwo(N1)) { 2736 // fold (urem x, pow2) -> (and x, pow2-1) 2737 SDValue Add = DAG.getNode(ISD::ADD, DL, VT, N1, NegOne); 2738 AddToWorklist(Add.getNode()); 2739 return DAG.getNode(ISD::AND, DL, VT, N0, Add); 2740 } 2741 if (N1.getOpcode() == ISD::SHL && 2742 DAG.isKnownToBeAPowerOfTwo(N1.getOperand(0))) { 2743 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1)) 2744 SDValue Add = DAG.getNode(ISD::ADD, DL, VT, N1, NegOne); 2745 AddToWorklist(Add.getNode()); 2746 return DAG.getNode(ISD::AND, DL, VT, N0, Add); 2747 } 2748 } 2749 2750 AttributeList Attr = DAG.getMachineFunction().getFunction()->getAttributes(); 2751 2752 // If X/C can be simplified by the division-by-constant logic, lower 2753 // X%C to the equivalent of X-X/C*C. 2754 // To avoid mangling nodes, this simplification requires that the combine() 2755 // call for the speculative DIV must not cause a DIVREM conversion. We guard 2756 // against this by skipping the simplification if isIntDivCheap(). When 2757 // div is not cheap, combine will not return a DIVREM. Regardless, 2758 // checking cheapness here makes sense since the simplification results in 2759 // fatter code. 2760 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap(VT, Attr)) { 2761 unsigned DivOpcode = isSigned ? ISD::SDIV : ISD::UDIV; 2762 SDValue Div = DAG.getNode(DivOpcode, DL, VT, N0, N1); 2763 AddToWorklist(Div.getNode()); 2764 SDValue OptimizedDiv = combine(Div.getNode()); 2765 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) { 2766 assert((OptimizedDiv.getOpcode() != ISD::UDIVREM) && 2767 (OptimizedDiv.getOpcode() != ISD::SDIVREM)); 2768 SDValue Mul = DAG.getNode(ISD::MUL, DL, VT, OptimizedDiv, N1); 2769 SDValue Sub = DAG.getNode(ISD::SUB, DL, VT, N0, Mul); 2770 AddToWorklist(Mul.getNode()); 2771 return Sub; 2772 } 2773 } 2774 2775 // sdiv, srem -> sdivrem 2776 if (SDValue DivRem = useDivRem(N)) 2777 return DivRem.getValue(1); 2778 2779 return SDValue(); 2780 } 2781 2782 SDValue DAGCombiner::visitMULHS(SDNode *N) { 2783 SDValue N0 = N->getOperand(0); 2784 SDValue N1 = N->getOperand(1); 2785 EVT VT = N->getValueType(0); 2786 SDLoc DL(N); 2787 2788 // fold (mulhs x, 0) -> 0 2789 if (isNullConstant(N1)) 2790 return N1; 2791 // fold (mulhs x, 1) -> (sra x, size(x)-1) 2792 if (isOneConstant(N1)) { 2793 SDLoc DL(N); 2794 return DAG.getNode(ISD::SRA, DL, N0.getValueType(), N0, 2795 DAG.getConstant(N0.getValueSizeInBits() - 1, DL, 2796 getShiftAmountTy(N0.getValueType()))); 2797 } 2798 // fold (mulhs x, undef) -> 0 2799 if (N0.isUndef() || N1.isUndef()) 2800 return DAG.getConstant(0, SDLoc(N), VT); 2801 2802 // If the type twice as wide is legal, transform the mulhs to a wider multiply 2803 // plus a shift. 2804 if (VT.isSimple() && !VT.isVector()) { 2805 MVT Simple = VT.getSimpleVT(); 2806 unsigned SimpleSize = Simple.getSizeInBits(); 2807 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2); 2808 if (TLI.isOperationLegal(ISD::MUL, NewVT)) { 2809 N0 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N0); 2810 N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1); 2811 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1); 2812 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1, 2813 DAG.getConstant(SimpleSize, DL, 2814 getShiftAmountTy(N1.getValueType()))); 2815 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1); 2816 } 2817 } 2818 2819 return SDValue(); 2820 } 2821 2822 SDValue DAGCombiner::visitMULHU(SDNode *N) { 2823 SDValue N0 = N->getOperand(0); 2824 SDValue N1 = N->getOperand(1); 2825 EVT VT = N->getValueType(0); 2826 SDLoc DL(N); 2827 2828 // fold (mulhu x, 0) -> 0 2829 if (isNullConstant(N1)) 2830 return N1; 2831 // fold (mulhu x, 1) -> 0 2832 if (isOneConstant(N1)) 2833 return DAG.getConstant(0, DL, N0.getValueType()); 2834 // fold (mulhu x, undef) -> 0 2835 if (N0.isUndef() || N1.isUndef()) 2836 return DAG.getConstant(0, DL, VT); 2837 2838 // If the type twice as wide is legal, transform the mulhu to a wider multiply 2839 // plus a shift. 2840 if (VT.isSimple() && !VT.isVector()) { 2841 MVT Simple = VT.getSimpleVT(); 2842 unsigned SimpleSize = Simple.getSizeInBits(); 2843 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2); 2844 if (TLI.isOperationLegal(ISD::MUL, NewVT)) { 2845 N0 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N0); 2846 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1); 2847 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1); 2848 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1, 2849 DAG.getConstant(SimpleSize, DL, 2850 getShiftAmountTy(N1.getValueType()))); 2851 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1); 2852 } 2853 } 2854 2855 return SDValue(); 2856 } 2857 2858 /// Perform optimizations common to nodes that compute two values. LoOp and HiOp 2859 /// give the opcodes for the two computations that are being performed. Return 2860 /// true if a simplification was made. 2861 SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp, 2862 unsigned HiOp) { 2863 // If the high half is not needed, just compute the low half. 2864 bool HiExists = N->hasAnyUseOfValue(1); 2865 if (!HiExists && 2866 (!LegalOperations || 2867 TLI.isOperationLegalOrCustom(LoOp, N->getValueType(0)))) { 2868 SDValue Res = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0), N->ops()); 2869 return CombineTo(N, Res, Res); 2870 } 2871 2872 // If the low half is not needed, just compute the high half. 2873 bool LoExists = N->hasAnyUseOfValue(0); 2874 if (!LoExists && 2875 (!LegalOperations || 2876 TLI.isOperationLegal(HiOp, N->getValueType(1)))) { 2877 SDValue Res = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1), N->ops()); 2878 return CombineTo(N, Res, Res); 2879 } 2880 2881 // If both halves are used, return as it is. 2882 if (LoExists && HiExists) 2883 return SDValue(); 2884 2885 // If the two computed results can be simplified separately, separate them. 2886 if (LoExists) { 2887 SDValue Lo = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0), N->ops()); 2888 AddToWorklist(Lo.getNode()); 2889 SDValue LoOpt = combine(Lo.getNode()); 2890 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() && 2891 (!LegalOperations || 2892 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType()))) 2893 return CombineTo(N, LoOpt, LoOpt); 2894 } 2895 2896 if (HiExists) { 2897 SDValue Hi = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1), N->ops()); 2898 AddToWorklist(Hi.getNode()); 2899 SDValue HiOpt = combine(Hi.getNode()); 2900 if (HiOpt.getNode() && HiOpt != Hi && 2901 (!LegalOperations || 2902 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType()))) 2903 return CombineTo(N, HiOpt, HiOpt); 2904 } 2905 2906 return SDValue(); 2907 } 2908 2909 SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) { 2910 if (SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS)) 2911 return Res; 2912 2913 EVT VT = N->getValueType(0); 2914 SDLoc DL(N); 2915 2916 // If the type is twice as wide is legal, transform the mulhu to a wider 2917 // multiply plus a shift. 2918 if (VT.isSimple() && !VT.isVector()) { 2919 MVT Simple = VT.getSimpleVT(); 2920 unsigned SimpleSize = Simple.getSizeInBits(); 2921 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2); 2922 if (TLI.isOperationLegal(ISD::MUL, NewVT)) { 2923 SDValue Lo = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(0)); 2924 SDValue Hi = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(1)); 2925 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi); 2926 // Compute the high part as N1. 2927 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo, 2928 DAG.getConstant(SimpleSize, DL, 2929 getShiftAmountTy(Lo.getValueType()))); 2930 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi); 2931 // Compute the low part as N0. 2932 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo); 2933 return CombineTo(N, Lo, Hi); 2934 } 2935 } 2936 2937 return SDValue(); 2938 } 2939 2940 SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) { 2941 if (SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU)) 2942 return Res; 2943 2944 EVT VT = N->getValueType(0); 2945 SDLoc DL(N); 2946 2947 // If the type is twice as wide is legal, transform the mulhu to a wider 2948 // multiply plus a shift. 2949 if (VT.isSimple() && !VT.isVector()) { 2950 MVT Simple = VT.getSimpleVT(); 2951 unsigned SimpleSize = Simple.getSizeInBits(); 2952 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2); 2953 if (TLI.isOperationLegal(ISD::MUL, NewVT)) { 2954 SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(0)); 2955 SDValue Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(1)); 2956 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi); 2957 // Compute the high part as N1. 2958 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo, 2959 DAG.getConstant(SimpleSize, DL, 2960 getShiftAmountTy(Lo.getValueType()))); 2961 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi); 2962 // Compute the low part as N0. 2963 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo); 2964 return CombineTo(N, Lo, Hi); 2965 } 2966 } 2967 2968 return SDValue(); 2969 } 2970 2971 SDValue DAGCombiner::visitSMULO(SDNode *N) { 2972 // (smulo x, 2) -> (saddo x, x) 2973 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1))) 2974 if (C2->getAPIntValue() == 2) 2975 return DAG.getNode(ISD::SADDO, SDLoc(N), N->getVTList(), 2976 N->getOperand(0), N->getOperand(0)); 2977 2978 return SDValue(); 2979 } 2980 2981 SDValue DAGCombiner::visitUMULO(SDNode *N) { 2982 // (umulo x, 2) -> (uaddo x, x) 2983 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1))) 2984 if (C2->getAPIntValue() == 2) 2985 return DAG.getNode(ISD::UADDO, SDLoc(N), N->getVTList(), 2986 N->getOperand(0), N->getOperand(0)); 2987 2988 return SDValue(); 2989 } 2990 2991 SDValue DAGCombiner::visitIMINMAX(SDNode *N) { 2992 SDValue N0 = N->getOperand(0); 2993 SDValue N1 = N->getOperand(1); 2994 EVT VT = N0.getValueType(); 2995 2996 // fold vector ops 2997 if (VT.isVector()) 2998 if (SDValue FoldedVOp = SimplifyVBinOp(N)) 2999 return FoldedVOp; 3000 3001 // fold (add c1, c2) -> c1+c2 3002 ConstantSDNode *N0C = getAsNonOpaqueConstant(N0); 3003 ConstantSDNode *N1C = getAsNonOpaqueConstant(N1); 3004 if (N0C && N1C) 3005 return DAG.FoldConstantArithmetic(N->getOpcode(), SDLoc(N), VT, N0C, N1C); 3006 3007 // canonicalize constant to RHS 3008 if (DAG.isConstantIntBuildVectorOrConstantInt(N0) && 3009 !DAG.isConstantIntBuildVectorOrConstantInt(N1)) 3010 return DAG.getNode(N->getOpcode(), SDLoc(N), VT, N1, N0); 3011 3012 return SDValue(); 3013 } 3014 3015 /// If this is a binary operator with two operands of the same opcode, try to 3016 /// simplify it. 3017 SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) { 3018 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1); 3019 EVT VT = N0.getValueType(); 3020 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!"); 3021 3022 // Bail early if none of these transforms apply. 3023 if (N0.getNumOperands() == 0) return SDValue(); 3024 3025 // For each of OP in AND/OR/XOR: 3026 // fold (OP (zext x), (zext y)) -> (zext (OP x, y)) 3027 // fold (OP (sext x), (sext y)) -> (sext (OP x, y)) 3028 // fold (OP (aext x), (aext y)) -> (aext (OP x, y)) 3029 // fold (OP (bswap x), (bswap y)) -> (bswap (OP x, y)) 3030 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) (if trunc isn't free) 3031 // 3032 // do not sink logical op inside of a vector extend, since it may combine 3033 // into a vsetcc. 3034 EVT Op0VT = N0.getOperand(0).getValueType(); 3035 if ((N0.getOpcode() == ISD::ZERO_EXTEND || 3036 N0.getOpcode() == ISD::SIGN_EXTEND || 3037 N0.getOpcode() == ISD::BSWAP || 3038 // Avoid infinite looping with PromoteIntBinOp. 3039 (N0.getOpcode() == ISD::ANY_EXTEND && 3040 (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) || 3041 (N0.getOpcode() == ISD::TRUNCATE && 3042 (!TLI.isZExtFree(VT, Op0VT) || 3043 !TLI.isTruncateFree(Op0VT, VT)) && 3044 TLI.isTypeLegal(Op0VT))) && 3045 !VT.isVector() && 3046 Op0VT == N1.getOperand(0).getValueType() && 3047 (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) { 3048 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0), 3049 N0.getOperand(0).getValueType(), 3050 N0.getOperand(0), N1.getOperand(0)); 3051 AddToWorklist(ORNode.getNode()); 3052 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, ORNode); 3053 } 3054 3055 // For each of OP in SHL/SRL/SRA/AND... 3056 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z) 3057 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z) 3058 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z) 3059 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL || 3060 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) && 3061 N0.getOperand(1) == N1.getOperand(1)) { 3062 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0), 3063 N0.getOperand(0).getValueType(), 3064 N0.getOperand(0), N1.getOperand(0)); 3065 AddToWorklist(ORNode.getNode()); 3066 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, 3067 ORNode, N0.getOperand(1)); 3068 } 3069 3070 // Simplify xor/and/or (bitcast(A), bitcast(B)) -> bitcast(op (A,B)) 3071 // Only perform this optimization up until type legalization, before 3072 // LegalizeVectorOprs. LegalizeVectorOprs promotes vector operations by 3073 // adding bitcasts. For example (xor v4i32) is promoted to (v2i64), and 3074 // we don't want to undo this promotion. 3075 // We also handle SCALAR_TO_VECTOR because xor/or/and operations are cheaper 3076 // on scalars. 3077 if ((N0.getOpcode() == ISD::BITCAST || 3078 N0.getOpcode() == ISD::SCALAR_TO_VECTOR) && 3079 Level <= AfterLegalizeTypes) { 3080 SDValue In0 = N0.getOperand(0); 3081 SDValue In1 = N1.getOperand(0); 3082 EVT In0Ty = In0.getValueType(); 3083 EVT In1Ty = In1.getValueType(); 3084 SDLoc DL(N); 3085 // If both incoming values are integers, and the original types are the 3086 // same. 3087 if (In0Ty.isInteger() && In1Ty.isInteger() && In0Ty == In1Ty) { 3088 SDValue Op = DAG.getNode(N->getOpcode(), DL, In0Ty, In0, In1); 3089 SDValue BC = DAG.getNode(N0.getOpcode(), DL, VT, Op); 3090 AddToWorklist(Op.getNode()); 3091 return BC; 3092 } 3093 } 3094 3095 // Xor/and/or are indifferent to the swizzle operation (shuffle of one value). 3096 // Simplify xor/and/or (shuff(A), shuff(B)) -> shuff(op (A,B)) 3097 // If both shuffles use the same mask, and both shuffle within a single 3098 // vector, then it is worthwhile to move the swizzle after the operation. 3099 // The type-legalizer generates this pattern when loading illegal 3100 // vector types from memory. In many cases this allows additional shuffle 3101 // optimizations. 3102 // There are other cases where moving the shuffle after the xor/and/or 3103 // is profitable even if shuffles don't perform a swizzle. 3104 // If both shuffles use the same mask, and both shuffles have the same first 3105 // or second operand, then it might still be profitable to move the shuffle 3106 // after the xor/and/or operation. 3107 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG) { 3108 ShuffleVectorSDNode *SVN0 = cast<ShuffleVectorSDNode>(N0); 3109 ShuffleVectorSDNode *SVN1 = cast<ShuffleVectorSDNode>(N1); 3110 3111 assert(N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType() && 3112 "Inputs to shuffles are not the same type"); 3113 3114 // Check that both shuffles use the same mask. The masks are known to be of 3115 // the same length because the result vector type is the same. 3116 // Check also that shuffles have only one use to avoid introducing extra 3117 // instructions. 3118 if (SVN0->hasOneUse() && SVN1->hasOneUse() && 3119 SVN0->getMask().equals(SVN1->getMask())) { 3120 SDValue ShOp = N0->getOperand(1); 3121 3122 // Don't try to fold this node if it requires introducing a 3123 // build vector of all zeros that might be illegal at this stage. 3124 if (N->getOpcode() == ISD::XOR && !ShOp.isUndef()) { 3125 if (!LegalTypes) 3126 ShOp = DAG.getConstant(0, SDLoc(N), VT); 3127 else 3128 ShOp = SDValue(); 3129 } 3130 3131 // (AND (shuf (A, C), shuf (B, C)) -> shuf (AND (A, B), C) 3132 // (OR (shuf (A, C), shuf (B, C)) -> shuf (OR (A, B), C) 3133 // (XOR (shuf (A, C), shuf (B, C)) -> shuf (XOR (A, B), V_0) 3134 if (N0.getOperand(1) == N1.getOperand(1) && ShOp.getNode()) { 3135 SDValue NewNode = DAG.getNode(N->getOpcode(), SDLoc(N), VT, 3136 N0->getOperand(0), N1->getOperand(0)); 3137 AddToWorklist(NewNode.getNode()); 3138 return DAG.getVectorShuffle(VT, SDLoc(N), NewNode, ShOp, 3139 SVN0->getMask()); 3140 } 3141 3142 // Don't try to fold this node if it requires introducing a 3143 // build vector of all zeros that might be illegal at this stage. 3144 ShOp = N0->getOperand(0); 3145 if (N->getOpcode() == ISD::XOR && !ShOp.isUndef()) { 3146 if (!LegalTypes) 3147 ShOp = DAG.getConstant(0, SDLoc(N), VT); 3148 else 3149 ShOp = SDValue(); 3150 } 3151 3152 // (AND (shuf (C, A), shuf (C, B)) -> shuf (C, AND (A, B)) 3153 // (OR (shuf (C, A), shuf (C, B)) -> shuf (C, OR (A, B)) 3154 // (XOR (shuf (C, A), shuf (C, B)) -> shuf (V_0, XOR (A, B)) 3155 if (N0->getOperand(0) == N1->getOperand(0) && ShOp.getNode()) { 3156 SDValue NewNode = DAG.getNode(N->getOpcode(), SDLoc(N), VT, 3157 N0->getOperand(1), N1->getOperand(1)); 3158 AddToWorklist(NewNode.getNode()); 3159 return DAG.getVectorShuffle(VT, SDLoc(N), ShOp, NewNode, 3160 SVN0->getMask()); 3161 } 3162 } 3163 } 3164 3165 return SDValue(); 3166 } 3167 3168 /// This contains all DAGCombine rules which reduce two values combined by 3169 /// an And operation to a single value. This makes them reusable in the context 3170 /// of visitSELECT(). Rules involving constants are not included as 3171 /// visitSELECT() already handles those cases. 3172 SDValue DAGCombiner::visitANDLike(SDValue N0, SDValue N1, 3173 SDNode *LocReference) { 3174 EVT VT = N1.getValueType(); 3175 3176 // fold (and x, undef) -> 0 3177 if (N0.isUndef() || N1.isUndef()) 3178 return DAG.getConstant(0, SDLoc(LocReference), VT); 3179 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y)) 3180 SDValue LL, LR, RL, RR, CC0, CC1; 3181 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){ 3182 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get(); 3183 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get(); 3184 3185 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 && 3186 LL.getValueType().isInteger()) { 3187 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0) 3188 if (isNullConstant(LR) && Op1 == ISD::SETEQ) { 3189 EVT CCVT = getSetCCResultType(LR.getValueType()); 3190 if (VT == CCVT || (!LegalOperations && VT == MVT::i1)) { 3191 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0), 3192 LR.getValueType(), LL, RL); 3193 AddToWorklist(ORNode.getNode()); 3194 return DAG.getSetCC(SDLoc(LocReference), VT, ORNode, LR, Op1); 3195 } 3196 } 3197 if (isAllOnesConstant(LR)) { 3198 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1) 3199 if (Op1 == ISD::SETEQ) { 3200 EVT CCVT = getSetCCResultType(LR.getValueType()); 3201 if (VT == CCVT || (!LegalOperations && VT == MVT::i1)) { 3202 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(N0), 3203 LR.getValueType(), LL, RL); 3204 AddToWorklist(ANDNode.getNode()); 3205 return DAG.getSetCC(SDLoc(LocReference), VT, ANDNode, LR, Op1); 3206 } 3207 } 3208 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1) 3209 if (Op1 == ISD::SETGT) { 3210 EVT CCVT = getSetCCResultType(LR.getValueType()); 3211 if (VT == CCVT || (!LegalOperations && VT == MVT::i1)) { 3212 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0), 3213 LR.getValueType(), LL, RL); 3214 AddToWorklist(ORNode.getNode()); 3215 return DAG.getSetCC(SDLoc(LocReference), VT, ORNode, LR, Op1); 3216 } 3217 } 3218 } 3219 } 3220 // Simplify (and (setne X, 0), (setne X, -1)) -> (setuge (add X, 1), 2) 3221 if (LL == RL && isa<ConstantSDNode>(LR) && isa<ConstantSDNode>(RR) && 3222 Op0 == Op1 && LL.getValueType().isInteger() && 3223 Op0 == ISD::SETNE && ((isNullConstant(LR) && isAllOnesConstant(RR)) || 3224 (isAllOnesConstant(LR) && isNullConstant(RR)))) { 3225 EVT CCVT = getSetCCResultType(LL.getValueType()); 3226 if (VT == CCVT || (!LegalOperations && VT == MVT::i1)) { 3227 SDLoc DL(N0); 3228 SDValue ADDNode = DAG.getNode(ISD::ADD, DL, LL.getValueType(), 3229 LL, DAG.getConstant(1, DL, 3230 LL.getValueType())); 3231 AddToWorklist(ADDNode.getNode()); 3232 return DAG.getSetCC(SDLoc(LocReference), VT, ADDNode, 3233 DAG.getConstant(2, DL, LL.getValueType()), 3234 ISD::SETUGE); 3235 } 3236 } 3237 // canonicalize equivalent to ll == rl 3238 if (LL == RR && LR == RL) { 3239 Op1 = ISD::getSetCCSwappedOperands(Op1); 3240 std::swap(RL, RR); 3241 } 3242 if (LL == RL && LR == RR) { 3243 bool isInteger = LL.getValueType().isInteger(); 3244 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger); 3245 if (Result != ISD::SETCC_INVALID && 3246 (!LegalOperations || 3247 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) && 3248 TLI.isOperationLegal(ISD::SETCC, LL.getValueType())))) { 3249 EVT CCVT = getSetCCResultType(LL.getValueType()); 3250 if (N0.getValueType() == CCVT || 3251 (!LegalOperations && N0.getValueType() == MVT::i1)) 3252 return DAG.getSetCC(SDLoc(LocReference), N0.getValueType(), 3253 LL, LR, Result); 3254 } 3255 } 3256 } 3257 3258 if (N0.getOpcode() == ISD::ADD && N1.getOpcode() == ISD::SRL && 3259 VT.getSizeInBits() <= 64) { 3260 if (ConstantSDNode *ADDI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { 3261 APInt ADDC = ADDI->getAPIntValue(); 3262 if (!TLI.isLegalAddImmediate(ADDC.getSExtValue())) { 3263 // Look for (and (add x, c1), (lshr y, c2)). If C1 wasn't a legal 3264 // immediate for an add, but it is legal if its top c2 bits are set, 3265 // transform the ADD so the immediate doesn't need to be materialized 3266 // in a register. 3267 if (ConstantSDNode *SRLI = dyn_cast<ConstantSDNode>(N1.getOperand(1))) { 3268 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(), 3269 SRLI->getZExtValue()); 3270 if (DAG.MaskedValueIsZero(N0.getOperand(1), Mask)) { 3271 ADDC |= Mask; 3272 if (TLI.isLegalAddImmediate(ADDC.getSExtValue())) { 3273 SDLoc DL(N0); 3274 SDValue NewAdd = 3275 DAG.getNode(ISD::ADD, DL, VT, 3276 N0.getOperand(0), DAG.getConstant(ADDC, DL, VT)); 3277 CombineTo(N0.getNode(), NewAdd); 3278 // Return N so it doesn't get rechecked! 3279 return SDValue(LocReference, 0); 3280 } 3281 } 3282 } 3283 } 3284 } 3285 } 3286 3287 // Reduce bit extract of low half of an integer to the narrower type. 3288 // (and (srl i64:x, K), KMask) -> 3289 // (i64 zero_extend (and (srl (i32 (trunc i64:x)), K)), KMask) 3290 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) { 3291 if (ConstantSDNode *CAnd = dyn_cast<ConstantSDNode>(N1)) { 3292 if (ConstantSDNode *CShift = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { 3293 unsigned Size = VT.getSizeInBits(); 3294 const APInt &AndMask = CAnd->getAPIntValue(); 3295 unsigned ShiftBits = CShift->getZExtValue(); 3296 3297 // Bail out, this node will probably disappear anyway. 3298 if (ShiftBits == 0) 3299 return SDValue(); 3300 3301 unsigned MaskBits = AndMask.countTrailingOnes(); 3302 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), Size / 2); 3303 3304 if (APIntOps::isMask(AndMask) && 3305 // Required bits must not span the two halves of the integer and 3306 // must fit in the half size type. 3307 (ShiftBits + MaskBits <= Size / 2) && 3308 TLI.isNarrowingProfitable(VT, HalfVT) && 3309 TLI.isTypeDesirableForOp(ISD::AND, HalfVT) && 3310 TLI.isTypeDesirableForOp(ISD::SRL, HalfVT) && 3311 TLI.isTruncateFree(VT, HalfVT) && 3312 TLI.isZExtFree(HalfVT, VT)) { 3313 // The isNarrowingProfitable is to avoid regressions on PPC and 3314 // AArch64 which match a few 64-bit bit insert / bit extract patterns 3315 // on downstream users of this. Those patterns could probably be 3316 // extended to handle extensions mixed in. 3317 3318 SDValue SL(N0); 3319 assert(MaskBits <= Size); 3320 3321 // Extracting the highest bit of the low half. 3322 EVT ShiftVT = TLI.getShiftAmountTy(HalfVT, DAG.getDataLayout()); 3323 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SL, HalfVT, 3324 N0.getOperand(0)); 3325 3326 SDValue NewMask = DAG.getConstant(AndMask.trunc(Size / 2), SL, HalfVT); 3327 SDValue ShiftK = DAG.getConstant(ShiftBits, SL, ShiftVT); 3328 SDValue Shift = DAG.getNode(ISD::SRL, SL, HalfVT, Trunc, ShiftK); 3329 SDValue And = DAG.getNode(ISD::AND, SL, HalfVT, Shift, NewMask); 3330 return DAG.getNode(ISD::ZERO_EXTEND, SL, VT, And); 3331 } 3332 } 3333 } 3334 } 3335 3336 return SDValue(); 3337 } 3338 3339 bool DAGCombiner::isAndLoadExtLoad(ConstantSDNode *AndC, LoadSDNode *LoadN, 3340 EVT LoadResultTy, EVT &ExtVT, EVT &LoadedVT, 3341 bool &NarrowLoad) { 3342 uint32_t ActiveBits = AndC->getAPIntValue().getActiveBits(); 3343 3344 if (ActiveBits == 0 || !APIntOps::isMask(ActiveBits, AndC->getAPIntValue())) 3345 return false; 3346 3347 ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits); 3348 LoadedVT = LoadN->getMemoryVT(); 3349 3350 if (ExtVT == LoadedVT && 3351 (!LegalOperations || 3352 TLI.isLoadExtLegal(ISD::ZEXTLOAD, LoadResultTy, ExtVT))) { 3353 // ZEXTLOAD will match without needing to change the size of the value being 3354 // loaded. 3355 NarrowLoad = false; 3356 return true; 3357 } 3358 3359 // Do not change the width of a volatile load. 3360 if (LoadN->isVolatile()) 3361 return false; 3362 3363 // Do not generate loads of non-round integer types since these can 3364 // be expensive (and would be wrong if the type is not byte sized). 3365 if (!LoadedVT.bitsGT(ExtVT) || !ExtVT.isRound()) 3366 return false; 3367 3368 if (LegalOperations && 3369 !TLI.isLoadExtLegal(ISD::ZEXTLOAD, LoadResultTy, ExtVT)) 3370 return false; 3371 3372 if (!TLI.shouldReduceLoadWidth(LoadN, ISD::ZEXTLOAD, ExtVT)) 3373 return false; 3374 3375 NarrowLoad = true; 3376 return true; 3377 } 3378 3379 SDValue DAGCombiner::visitAND(SDNode *N) { 3380 SDValue N0 = N->getOperand(0); 3381 SDValue N1 = N->getOperand(1); 3382 EVT VT = N1.getValueType(); 3383 3384 // x & x --> x 3385 if (N0 == N1) 3386 return N0; 3387 3388 // fold vector ops 3389 if (VT.isVector()) { 3390 if (SDValue FoldedVOp = SimplifyVBinOp(N)) 3391 return FoldedVOp; 3392 3393 // fold (and x, 0) -> 0, vector edition 3394 if (ISD::isBuildVectorAllZeros(N0.getNode())) 3395 // do not return N0, because undef node may exist in N0 3396 return DAG.getConstant(APInt::getNullValue(N0.getScalarValueSizeInBits()), 3397 SDLoc(N), N0.getValueType()); 3398 if (ISD::isBuildVectorAllZeros(N1.getNode())) 3399 // do not return N1, because undef node may exist in N1 3400 return DAG.getConstant(APInt::getNullValue(N1.getScalarValueSizeInBits()), 3401 SDLoc(N), N1.getValueType()); 3402 3403 // fold (and x, -1) -> x, vector edition 3404 if (ISD::isBuildVectorAllOnes(N0.getNode())) 3405 return N1; 3406 if (ISD::isBuildVectorAllOnes(N1.getNode())) 3407 return N0; 3408 } 3409 3410 // fold (and c1, c2) -> c1&c2 3411 ConstantSDNode *N0C = getAsNonOpaqueConstant(N0); 3412 ConstantSDNode *N1C = isConstOrConstSplat(N1); 3413 if (N0C && N1C && !N1C->isOpaque()) 3414 return DAG.FoldConstantArithmetic(ISD::AND, SDLoc(N), VT, N0C, N1C); 3415 // canonicalize constant to RHS 3416 if (DAG.isConstantIntBuildVectorOrConstantInt(N0) && 3417 !DAG.isConstantIntBuildVectorOrConstantInt(N1)) 3418 return DAG.getNode(ISD::AND, SDLoc(N), VT, N1, N0); 3419 // fold (and x, -1) -> x 3420 if (isAllOnesConstant(N1)) 3421 return N0; 3422 // if (and x, c) is known to be zero, return 0 3423 unsigned BitWidth = VT.getScalarSizeInBits(); 3424 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0), 3425 APInt::getAllOnesValue(BitWidth))) 3426 return DAG.getConstant(0, SDLoc(N), VT); 3427 3428 if (SDValue NewSel = foldBinOpIntoSelect(N)) 3429 return NewSel; 3430 3431 // reassociate and 3432 if (SDValue RAND = ReassociateOps(ISD::AND, SDLoc(N), N0, N1)) 3433 return RAND; 3434 // fold (and (or x, C), D) -> D if (C & D) == D 3435 if (N1C && N0.getOpcode() == ISD::OR) 3436 if (ConstantSDNode *ORI = isConstOrConstSplat(N0.getOperand(1))) 3437 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue()) 3438 return N1; 3439 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits. 3440 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) { 3441 SDValue N0Op0 = N0.getOperand(0); 3442 APInt Mask = ~N1C->getAPIntValue(); 3443 Mask = Mask.trunc(N0Op0.getScalarValueSizeInBits()); 3444 if (DAG.MaskedValueIsZero(N0Op0, Mask)) { 3445 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), 3446 N0.getValueType(), N0Op0); 3447 3448 // Replace uses of the AND with uses of the Zero extend node. 3449 CombineTo(N, Zext); 3450 3451 // We actually want to replace all uses of the any_extend with the 3452 // zero_extend, to avoid duplicating things. This will later cause this 3453 // AND to be folded. 3454 CombineTo(N0.getNode(), Zext); 3455 return SDValue(N, 0); // Return N so it doesn't get rechecked! 3456 } 3457 } 3458 // similarly fold (and (X (load ([non_ext|any_ext|zero_ext] V))), c) -> 3459 // (X (load ([non_ext|zero_ext] V))) if 'and' only clears top bits which must 3460 // already be zero by virtue of the width of the base type of the load. 3461 // 3462 // the 'X' node here can either be nothing or an extract_vector_elt to catch 3463 // more cases. 3464 if ((N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT && 3465 N0.getValueSizeInBits() == N0.getOperand(0).getScalarValueSizeInBits() && 3466 N0.getOperand(0).getOpcode() == ISD::LOAD && 3467 N0.getOperand(0).getResNo() == 0) || 3468 (N0.getOpcode() == ISD::LOAD && N0.getResNo() == 0)) { 3469 LoadSDNode *Load = cast<LoadSDNode>( (N0.getOpcode() == ISD::LOAD) ? 3470 N0 : N0.getOperand(0) ); 3471 3472 // Get the constant (if applicable) the zero'th operand is being ANDed with. 3473 // This can be a pure constant or a vector splat, in which case we treat the 3474 // vector as a scalar and use the splat value. 3475 APInt Constant = APInt::getNullValue(1); 3476 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) { 3477 Constant = C->getAPIntValue(); 3478 } else if (BuildVectorSDNode *Vector = dyn_cast<BuildVectorSDNode>(N1)) { 3479 APInt SplatValue, SplatUndef; 3480 unsigned SplatBitSize; 3481 bool HasAnyUndefs; 3482 bool IsSplat = Vector->isConstantSplat(SplatValue, SplatUndef, 3483 SplatBitSize, HasAnyUndefs); 3484 if (IsSplat) { 3485 // Undef bits can contribute to a possible optimisation if set, so 3486 // set them. 3487 SplatValue |= SplatUndef; 3488 3489 // The splat value may be something like "0x00FFFFFF", which means 0 for 3490 // the first vector value and FF for the rest, repeating. We need a mask 3491 // that will apply equally to all members of the vector, so AND all the 3492 // lanes of the constant together. 3493 EVT VT = Vector->getValueType(0); 3494 unsigned BitWidth = VT.getScalarSizeInBits(); 3495 3496 // If the splat value has been compressed to a bitlength lower 3497 // than the size of the vector lane, we need to re-expand it to 3498 // the lane size. 3499 if (BitWidth > SplatBitSize) 3500 for (SplatValue = SplatValue.zextOrTrunc(BitWidth); 3501 SplatBitSize < BitWidth; 3502 SplatBitSize = SplatBitSize * 2) 3503 SplatValue |= SplatValue.shl(SplatBitSize); 3504 3505 // Make sure that variable 'Constant' is only set if 'SplatBitSize' is a 3506 // multiple of 'BitWidth'. Otherwise, we could propagate a wrong value. 3507 if (SplatBitSize % BitWidth == 0) { 3508 Constant = APInt::getAllOnesValue(BitWidth); 3509 for (unsigned i = 0, n = SplatBitSize/BitWidth; i < n; ++i) 3510 Constant &= SplatValue.lshr(i*BitWidth).zextOrTrunc(BitWidth); 3511 } 3512 } 3513 } 3514 3515 // If we want to change an EXTLOAD to a ZEXTLOAD, ensure a ZEXTLOAD is 3516 // actually legal and isn't going to get expanded, else this is a false 3517 // optimisation. 3518 bool CanZextLoadProfitably = TLI.isLoadExtLegal(ISD::ZEXTLOAD, 3519 Load->getValueType(0), 3520 Load->getMemoryVT()); 3521 3522 // Resize the constant to the same size as the original memory access before 3523 // extension. If it is still the AllOnesValue then this AND is completely 3524 // unneeded. 3525 Constant = Constant.zextOrTrunc(Load->getMemoryVT().getScalarSizeInBits()); 3526 3527 bool B; 3528 switch (Load->getExtensionType()) { 3529 default: B = false; break; 3530 case ISD::EXTLOAD: B = CanZextLoadProfitably; break; 3531 case ISD::ZEXTLOAD: 3532 case ISD::NON_EXTLOAD: B = true; break; 3533 } 3534 3535 if (B && Constant.isAllOnesValue()) { 3536 // If the load type was an EXTLOAD, convert to ZEXTLOAD in order to 3537 // preserve semantics once we get rid of the AND. 3538 SDValue NewLoad(Load, 0); 3539 if (Load->getExtensionType() == ISD::EXTLOAD) { 3540 NewLoad = DAG.getLoad(Load->getAddressingMode(), ISD::ZEXTLOAD, 3541 Load->getValueType(0), SDLoc(Load), 3542 Load->getChain(), Load->getBasePtr(), 3543 Load->getOffset(), Load->getMemoryVT(), 3544 Load->getMemOperand()); 3545 // Replace uses of the EXTLOAD with the new ZEXTLOAD. 3546 if (Load->getNumValues() == 3) { 3547 // PRE/POST_INC loads have 3 values. 3548 SDValue To[] = { NewLoad.getValue(0), NewLoad.getValue(1), 3549 NewLoad.getValue(2) }; 3550 CombineTo(Load, To, 3, true); 3551 } else { 3552 CombineTo(Load, NewLoad.getValue(0), NewLoad.getValue(1)); 3553 } 3554 } 3555 3556 // Fold the AND away, taking care not to fold to the old load node if we 3557 // replaced it. 3558 CombineTo(N, (N0.getNode() == Load) ? NewLoad : N0); 3559 3560 return SDValue(N, 0); // Return N so it doesn't get rechecked! 3561 } 3562 } 3563 3564 // fold (and (load x), 255) -> (zextload x, i8) 3565 // fold (and (extload x, i16), 255) -> (zextload x, i8) 3566 // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8) 3567 if (!VT.isVector() && N1C && (N0.getOpcode() == ISD::LOAD || 3568 (N0.getOpcode() == ISD::ANY_EXTEND && 3569 N0.getOperand(0).getOpcode() == ISD::LOAD))) { 3570 bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND; 3571 LoadSDNode *LN0 = HasAnyExt 3572 ? cast<LoadSDNode>(N0.getOperand(0)) 3573 : cast<LoadSDNode>(N0); 3574 if (LN0->getExtensionType() != ISD::SEXTLOAD && 3575 LN0->isUnindexed() && N0.hasOneUse() && SDValue(LN0, 0).hasOneUse()) { 3576 auto NarrowLoad = false; 3577 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT; 3578 EVT ExtVT, LoadedVT; 3579 if (isAndLoadExtLoad(N1C, LN0, LoadResultTy, ExtVT, LoadedVT, 3580 NarrowLoad)) { 3581 if (!NarrowLoad) { 3582 SDValue NewLoad = 3583 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy, 3584 LN0->getChain(), LN0->getBasePtr(), ExtVT, 3585 LN0->getMemOperand()); 3586 AddToWorklist(N); 3587 CombineTo(LN0, NewLoad, NewLoad.getValue(1)); 3588 return SDValue(N, 0); // Return N so it doesn't get rechecked! 3589 } else { 3590 EVT PtrType = LN0->getOperand(1).getValueType(); 3591 3592 unsigned Alignment = LN0->getAlignment(); 3593 SDValue NewPtr = LN0->getBasePtr(); 3594 3595 // For big endian targets, we need to add an offset to the pointer 3596 // to load the correct bytes. For little endian systems, we merely 3597 // need to read fewer bytes from the same pointer. 3598 if (DAG.getDataLayout().isBigEndian()) { 3599 unsigned LVTStoreBytes = LoadedVT.getStoreSize(); 3600 unsigned EVTStoreBytes = ExtVT.getStoreSize(); 3601 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes; 3602 SDLoc DL(LN0); 3603 NewPtr = DAG.getNode(ISD::ADD, DL, PtrType, 3604 NewPtr, DAG.getConstant(PtrOff, DL, PtrType)); 3605 Alignment = MinAlign(Alignment, PtrOff); 3606 } 3607 3608 AddToWorklist(NewPtr.getNode()); 3609 3610 SDValue Load = DAG.getExtLoad( 3611 ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy, LN0->getChain(), NewPtr, 3612 LN0->getPointerInfo(), ExtVT, Alignment, 3613 LN0->getMemOperand()->getFlags(), LN0->getAAInfo()); 3614 AddToWorklist(N); 3615 CombineTo(LN0, Load, Load.getValue(1)); 3616 return SDValue(N, 0); // Return N so it doesn't get rechecked! 3617 } 3618 } 3619 } 3620 } 3621 3622 if (SDValue Combined = visitANDLike(N0, N1, N)) 3623 return Combined; 3624 3625 // Simplify: (and (op x...), (op y...)) -> (op (and x, y)) 3626 if (N0.getOpcode() == N1.getOpcode()) 3627 if (SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N)) 3628 return Tmp; 3629 3630 // Masking the negated extension of a boolean is just the zero-extended 3631 // boolean: 3632 // and (sub 0, zext(bool X)), 1 --> zext(bool X) 3633 // and (sub 0, sext(bool X)), 1 --> zext(bool X) 3634 // 3635 // Note: the SimplifyDemandedBits fold below can make an information-losing 3636 // transform, and then we have no way to find this better fold. 3637 if (N1C && N1C->isOne() && N0.getOpcode() == ISD::SUB) { 3638 ConstantSDNode *SubLHS = isConstOrConstSplat(N0.getOperand(0)); 3639 SDValue SubRHS = N0.getOperand(1); 3640 if (SubLHS && SubLHS->isNullValue()) { 3641 if (SubRHS.getOpcode() == ISD::ZERO_EXTEND && 3642 SubRHS.getOperand(0).getScalarValueSizeInBits() == 1) 3643 return SubRHS; 3644 if (SubRHS.getOpcode() == ISD::SIGN_EXTEND && 3645 SubRHS.getOperand(0).getScalarValueSizeInBits() == 1) 3646 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, SubRHS.getOperand(0)); 3647 } 3648 } 3649 3650 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1) 3651 // fold (and (sra)) -> (and (srl)) when possible. 3652 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0))) 3653 return SDValue(N, 0); 3654 3655 // fold (zext_inreg (extload x)) -> (zextload x) 3656 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) { 3657 LoadSDNode *LN0 = cast<LoadSDNode>(N0); 3658 EVT MemVT = LN0->getMemoryVT(); 3659 // If we zero all the possible extended bits, then we can turn this into 3660 // a zextload if we are running before legalize or the operation is legal. 3661 unsigned BitWidth = N1.getScalarValueSizeInBits(); 3662 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth, 3663 BitWidth - MemVT.getScalarSizeInBits())) && 3664 ((!LegalOperations && !LN0->isVolatile()) || 3665 TLI.isLoadExtLegal(ISD::ZEXTLOAD, VT, MemVT))) { 3666 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT, 3667 LN0->getChain(), LN0->getBasePtr(), 3668 MemVT, LN0->getMemOperand()); 3669 AddToWorklist(N); 3670 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1)); 3671 return SDValue(N, 0); // Return N so it doesn't get rechecked! 3672 } 3673 } 3674 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use 3675 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) && 3676 N0.hasOneUse()) { 3677 LoadSDNode *LN0 = cast<LoadSDNode>(N0); 3678 EVT MemVT = LN0->getMemoryVT(); 3679 // If we zero all the possible extended bits, then we can turn this into 3680 // a zextload if we are running before legalize or the operation is legal. 3681 unsigned BitWidth = N1.getScalarValueSizeInBits(); 3682 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth, 3683 BitWidth - MemVT.getScalarSizeInBits())) && 3684 ((!LegalOperations && !LN0->isVolatile()) || 3685 TLI.isLoadExtLegal(ISD::ZEXTLOAD, VT, MemVT))) { 3686 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT, 3687 LN0->getChain(), LN0->getBasePtr(), 3688 MemVT, LN0->getMemOperand()); 3689 AddToWorklist(N); 3690 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1)); 3691 return SDValue(N, 0); // Return N so it doesn't get rechecked! 3692 } 3693 } 3694 // fold (and (or (srl N, 8), (shl N, 8)), 0xffff) -> (srl (bswap N), const) 3695 if (N1C && N1C->getAPIntValue() == 0xffff && N0.getOpcode() == ISD::OR) { 3696 if (SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0), 3697 N0.getOperand(1), false)) 3698 return BSwap; 3699 } 3700 3701 return SDValue(); 3702 } 3703 3704 /// Match (a >> 8) | (a << 8) as (bswap a) >> 16. 3705 SDValue DAGCombiner::MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1, 3706 bool DemandHighBits) { 3707 if (!LegalOperations) 3708 return SDValue(); 3709 3710 EVT VT = N->getValueType(0); 3711 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16) 3712 return SDValue(); 3713 if (!TLI.isOperationLegal(ISD::BSWAP, VT)) 3714 return SDValue(); 3715 3716 // Recognize (and (shl a, 8), 0xff), (and (srl a, 8), 0xff00) 3717 bool LookPassAnd0 = false; 3718 bool LookPassAnd1 = false; 3719 if (N0.getOpcode() == ISD::AND && N0.getOperand(0).getOpcode() == ISD::SRL) 3720 std::swap(N0, N1); 3721 if (N1.getOpcode() == ISD::AND && N1.getOperand(0).getOpcode() == ISD::SHL) 3722 std::swap(N0, N1); 3723 if (N0.getOpcode() == ISD::AND) { 3724 if (!N0.getNode()->hasOneUse()) 3725 return SDValue(); 3726 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); 3727 if (!N01C || N01C->getZExtValue() != 0xFF00) 3728 return SDValue(); 3729 N0 = N0.getOperand(0); 3730 LookPassAnd0 = true; 3731 } 3732 3733 if (N1.getOpcode() == ISD::AND) { 3734 if (!N1.getNode()->hasOneUse()) 3735 return SDValue(); 3736 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); 3737 if (!N11C || N11C->getZExtValue() != 0xFF) 3738 return SDValue(); 3739 N1 = N1.getOperand(0); 3740 LookPassAnd1 = true; 3741 } 3742 3743 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL) 3744 std::swap(N0, N1); 3745 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL) 3746 return SDValue(); 3747 if (!N0.getNode()->hasOneUse() || !N1.getNode()->hasOneUse()) 3748 return SDValue(); 3749 3750 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); 3751 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); 3752 if (!N01C || !N11C) 3753 return SDValue(); 3754 if (N01C->getZExtValue() != 8 || N11C->getZExtValue() != 8) 3755 return SDValue(); 3756 3757 // Look for (shl (and a, 0xff), 8), (srl (and a, 0xff00), 8) 3758 SDValue N00 = N0->getOperand(0); 3759 if (!LookPassAnd0 && N00.getOpcode() == ISD::AND) { 3760 if (!N00.getNode()->hasOneUse()) 3761 return SDValue(); 3762 ConstantSDNode *N001C = dyn_cast<ConstantSDNode>(N00.getOperand(1)); 3763 if (!N001C || N001C->getZExtValue() != 0xFF) 3764 return SDValue(); 3765 N00 = N00.getOperand(0); 3766 LookPassAnd0 = true; 3767 } 3768 3769 SDValue N10 = N1->getOperand(0); 3770 if (!LookPassAnd1 && N10.getOpcode() == ISD::AND) { 3771 if (!N10.getNode()->hasOneUse()) 3772 return SDValue(); 3773 ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N10.getOperand(1)); 3774 if (!N101C || N101C->getZExtValue() != 0xFF00) 3775 return SDValue(); 3776 N10 = N10.getOperand(0); 3777 LookPassAnd1 = true; 3778 } 3779 3780 if (N00 != N10) 3781 return SDValue(); 3782 3783 // Make sure everything beyond the low halfword gets set to zero since the SRL 3784 // 16 will clear the top bits. 3785 unsigned OpSizeInBits = VT.getSizeInBits(); 3786 if (DemandHighBits && OpSizeInBits > 16) { 3787 // If the left-shift isn't masked out then the only way this is a bswap is 3788 // if all bits beyond the low 8 are 0. In that case the entire pattern 3789 // reduces to a left shift anyway: leave it for other parts of the combiner. 3790 if (!LookPassAnd0) 3791 return SDValue(); 3792 3793 // However, if the right shift isn't masked out then it might be because 3794 // it's not needed. See if we can spot that too. 3795 if (!LookPassAnd1 && 3796 !DAG.MaskedValueIsZero( 3797 N10, APInt::getHighBitsSet(OpSizeInBits, OpSizeInBits - 16))) 3798 return SDValue(); 3799 } 3800 3801 SDValue Res = DAG.getNode(ISD::BSWAP, SDLoc(N), VT, N00); 3802 if (OpSizeInBits > 16) { 3803 SDLoc DL(N); 3804 Res = DAG.getNode(ISD::SRL, DL, VT, Res, 3805 DAG.getConstant(OpSizeInBits - 16, DL, 3806 getShiftAmountTy(VT))); 3807 } 3808 return Res; 3809 } 3810 3811 /// Return true if the specified node is an element that makes up a 32-bit 3812 /// packed halfword byteswap. 3813 /// ((x & 0x000000ff) << 8) | 3814 /// ((x & 0x0000ff00) >> 8) | 3815 /// ((x & 0x00ff0000) << 8) | 3816 /// ((x & 0xff000000) >> 8) 3817 static bool isBSwapHWordElement(SDValue N, MutableArrayRef<SDNode *> Parts) { 3818 if (!N.getNode()->hasOneUse()) 3819 return false; 3820 3821 unsigned Opc = N.getOpcode(); 3822 if (Opc != ISD::AND && Opc != ISD::SHL && Opc != ISD::SRL) 3823 return false; 3824 3825 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1)); 3826 if (!N1C) 3827 return false; 3828 3829 unsigned Num; 3830 switch (N1C->getZExtValue()) { 3831 default: 3832 return false; 3833 case 0xFF: Num = 0; break; 3834 case 0xFF00: Num = 1; break; 3835 case 0xFF0000: Num = 2; break; 3836 case 0xFF000000: Num = 3; break; 3837 } 3838 3839 // Look for (x & 0xff) << 8 as well as ((x << 8) & 0xff00). 3840 SDValue N0 = N.getOperand(0); 3841 if (Opc == ISD::AND) { 3842 if (Num == 0 || Num == 2) { 3843 // (x >> 8) & 0xff 3844 // (x >> 8) & 0xff0000 3845 if (N0.getOpcode() != ISD::SRL) 3846 return false; 3847 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); 3848 if (!C || C->getZExtValue() != 8) 3849 return false; 3850 } else { 3851 // (x << 8) & 0xff00 3852 // (x << 8) & 0xff000000 3853 if (N0.getOpcode() != ISD::SHL) 3854 return false; 3855 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); 3856 if (!C || C->getZExtValue() != 8) 3857 return false; 3858 } 3859 } else if (Opc == ISD::SHL) { 3860 // (x & 0xff) << 8 3861 // (x & 0xff0000) << 8 3862 if (Num != 0 && Num != 2) 3863 return false; 3864 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1)); 3865 if (!C || C->getZExtValue() != 8) 3866 return false; 3867 } else { // Opc == ISD::SRL 3868 // (x & 0xff00) >> 8 3869 // (x & 0xff000000) >> 8 3870 if (Num != 1 && Num != 3) 3871 return false; 3872 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1)); 3873 if (!C || C->getZExtValue() != 8) 3874 return false; 3875 } 3876 3877 if (Parts[Num]) 3878 return false; 3879 3880 Parts[Num] = N0.getOperand(0).getNode(); 3881 return true; 3882 } 3883 3884 /// Match a 32-bit packed halfword bswap. That is 3885 /// ((x & 0x000000ff) << 8) | 3886 /// ((x & 0x0000ff00) >> 8) | 3887 /// ((x & 0x00ff0000) << 8) | 3888 /// ((x & 0xff000000) >> 8) 3889 /// => (rotl (bswap x), 16) 3890 SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) { 3891 if (!LegalOperations) 3892 return SDValue(); 3893 3894 EVT VT = N->getValueType(0); 3895 if (VT != MVT::i32) 3896 return SDValue(); 3897 if (!TLI.isOperationLegal(ISD::BSWAP, VT)) 3898 return SDValue(); 3899 3900 // Look for either 3901 // (or (or (and), (and)), (or (and), (and))) 3902 // (or (or (or (and), (and)), (and)), (and)) 3903 if (N0.getOpcode() != ISD::OR) 3904 return SDValue(); 3905 SDValue N00 = N0.getOperand(0); 3906 SDValue N01 = N0.getOperand(1); 3907 SDNode *Parts[4] = {}; 3908 3909 if (N1.getOpcode() == ISD::OR && 3910 N00.getNumOperands() == 2 && N01.getNumOperands() == 2) { 3911 // (or (or (and), (and)), (or (and), (and))) 3912 SDValue N000 = N00.getOperand(0); 3913 if (!isBSwapHWordElement(N000, Parts)) 3914 return SDValue(); 3915 3916 SDValue N001 = N00.getOperand(1); 3917 if (!isBSwapHWordElement(N001, Parts)) 3918 return SDValue(); 3919 SDValue N010 = N01.getOperand(0); 3920 if (!isBSwapHWordElement(N010, Parts)) 3921 return SDValue(); 3922 SDValue N011 = N01.getOperand(1); 3923 if (!isBSwapHWordElement(N011, Parts)) 3924 return SDValue(); 3925 } else { 3926 // (or (or (or (and), (and)), (and)), (and)) 3927 if (!isBSwapHWordElement(N1, Parts)) 3928 return SDValue(); 3929 if (!isBSwapHWordElement(N01, Parts)) 3930 return SDValue(); 3931 if (N00.getOpcode() != ISD::OR) 3932 return SDValue(); 3933 SDValue N000 = N00.getOperand(0); 3934 if (!isBSwapHWordElement(N000, Parts)) 3935 return SDValue(); 3936 SDValue N001 = N00.getOperand(1); 3937 if (!isBSwapHWordElement(N001, Parts)) 3938 return SDValue(); 3939 } 3940 3941 // Make sure the parts are all coming from the same node. 3942 if (Parts[0] != Parts[1] || Parts[0] != Parts[2] || Parts[0] != Parts[3]) 3943 return SDValue(); 3944 3945 SDLoc DL(N); 3946 SDValue BSwap = DAG.getNode(ISD::BSWAP, DL, VT, 3947 SDValue(Parts[0], 0)); 3948 3949 // Result of the bswap should be rotated by 16. If it's not legal, then 3950 // do (x << 16) | (x >> 16). 3951 SDValue ShAmt = DAG.getConstant(16, DL, getShiftAmountTy(VT)); 3952 if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT)) 3953 return DAG.getNode(ISD::ROTL, DL, VT, BSwap, ShAmt); 3954 if (TLI.isOperationLegalOrCustom(ISD::ROTR, VT)) 3955 return DAG.getNode(ISD::ROTR, DL, VT, BSwap, ShAmt); 3956 return DAG.getNode(ISD::OR, DL, VT, 3957 DAG.getNode(ISD::SHL, DL, VT, BSwap, ShAmt), 3958 DAG.getNode(ISD::SRL, DL, VT, BSwap, ShAmt)); 3959 } 3960 3961 /// This contains all DAGCombine rules which reduce two values combined by 3962 /// an Or operation to a single value \see visitANDLike(). 3963 SDValue DAGCombiner::visitORLike(SDValue N0, SDValue N1, SDNode *LocReference) { 3964 EVT VT = N1.getValueType(); 3965 // fold (or x, undef) -> -1 3966 if (!LegalOperations && (N0.isUndef() || N1.isUndef())) 3967 return DAG.getAllOnesConstant(SDLoc(LocReference), VT); 3968 3969 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y)) 3970 SDValue LL, LR, RL, RR, CC0, CC1; 3971 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){ 3972 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get(); 3973 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get(); 3974 3975 if (LR == RR && Op0 == Op1 && LL.getValueType().isInteger()) { 3976 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0) 3977 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0) 3978 if (isNullConstant(LR) && (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) { 3979 EVT CCVT = getSetCCResultType(LR.getValueType()); 3980 if (VT == CCVT || (!LegalOperations && VT == MVT::i1)) { 3981 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(LR), 3982 LR.getValueType(), LL, RL); 3983 AddToWorklist(ORNode.getNode()); 3984 return DAG.getSetCC(SDLoc(LocReference), VT, ORNode, LR, Op1); 3985 } 3986 } 3987 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1) 3988 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1) 3989 if (isAllOnesConstant(LR) && (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) { 3990 EVT CCVT = getSetCCResultType(LR.getValueType()); 3991 if (VT == CCVT || (!LegalOperations && VT == MVT::i1)) { 3992 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(LR), 3993 LR.getValueType(), LL, RL); 3994 AddToWorklist(ANDNode.getNode()); 3995 return DAG.getSetCC(SDLoc(LocReference), VT, ANDNode, LR, Op1); 3996 } 3997 } 3998 } 3999 // canonicalize equivalent to ll == rl 4000 if (LL == RR && LR == RL) { 4001 Op1 = ISD::getSetCCSwappedOperands(Op1); 4002 std::swap(RL, RR); 4003 } 4004 if (LL == RL && LR == RR) { 4005 bool isInteger = LL.getValueType().isInteger(); 4006 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger); 4007 if (Result != ISD::SETCC_INVALID && 4008 (!LegalOperations || 4009 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) && 4010 TLI.isOperationLegal(ISD::SETCC, LL.getValueType())))) { 4011 EVT CCVT = getSetCCResultType(LL.getValueType()); 4012 if (N0.getValueType() == CCVT || 4013 (!LegalOperations && N0.getValueType() == MVT::i1)) 4014 return DAG.getSetCC(SDLoc(LocReference), N0.getValueType(), 4015 LL, LR, Result); 4016 } 4017 } 4018 } 4019 4020 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible. 4021 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == ISD::AND && 4022 // Don't increase # computations. 4023 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) { 4024 // We can only do this xform if we know that bits from X that are set in C2 4025 // but not in C1 are already zero. Likewise for Y. 4026 if (const ConstantSDNode *N0O1C = 4027 getAsNonOpaqueConstant(N0.getOperand(1))) { 4028 if (const ConstantSDNode *N1O1C = 4029 getAsNonOpaqueConstant(N1.getOperand(1))) { 4030 // We can only do this xform if we know that bits from X that are set in 4031 // C2 but not in C1 are already zero. Likewise for Y. 4032 const APInt &LHSMask = N0O1C->getAPIntValue(); 4033 const APInt &RHSMask = N1O1C->getAPIntValue(); 4034 4035 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) && 4036 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) { 4037 SDValue X = DAG.getNode(ISD::OR, SDLoc(N0), VT, 4038 N0.getOperand(0), N1.getOperand(0)); 4039 SDLoc DL(LocReference); 4040 return DAG.getNode(ISD::AND, DL, VT, X, 4041 DAG.getConstant(LHSMask | RHSMask, DL, VT)); 4042 } 4043 } 4044 } 4045 } 4046 4047 // (or (and X, M), (and X, N)) -> (and X, (or M, N)) 4048 if (N0.getOpcode() == ISD::AND && 4049 N1.getOpcode() == ISD::AND && 4050 N0.getOperand(0) == N1.getOperand(0) && 4051 // Don't increase # computations. 4052 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) { 4053 SDValue X = DAG.getNode(ISD::OR, SDLoc(N0), VT, 4054 N0.getOperand(1), N1.getOperand(1)); 4055 return DAG.getNode(ISD::AND, SDLoc(LocReference), VT, N0.getOperand(0), X); 4056 } 4057 4058 return SDValue(); 4059 } 4060 4061 SDValue DAGCombiner::visitOR(SDNode *N) { 4062 SDValue N0 = N->getOperand(0); 4063 SDValue N1 = N->getOperand(1); 4064 EVT VT = N1.getValueType(); 4065 4066 // x | x --> x 4067 if (N0 == N1) 4068 return N0; 4069 4070 // fold vector ops 4071 if (VT.isVector()) { 4072 if (SDValue FoldedVOp = SimplifyVBinOp(N)) 4073 return FoldedVOp; 4074 4075 // fold (or x, 0) -> x, vector edition 4076 if (ISD::isBuildVectorAllZeros(N0.getNode())) 4077 return N1; 4078 if (ISD::isBuildVectorAllZeros(N1.getNode())) 4079 return N0; 4080 4081 // fold (or x, -1) -> -1, vector edition 4082 if (ISD::isBuildVectorAllOnes(N0.getNode())) 4083 // do not return N0, because undef node may exist in N0 4084 return DAG.getAllOnesConstant(SDLoc(N), N0.getValueType()); 4085 if (ISD::isBuildVectorAllOnes(N1.getNode())) 4086 // do not return N1, because undef node may exist in N1 4087 return DAG.getAllOnesConstant(SDLoc(N), N1.getValueType()); 4088 4089 // fold (or (shuf A, V_0, MA), (shuf B, V_0, MB)) -> (shuf A, B, Mask) 4090 // Do this only if the resulting shuffle is legal. 4091 if (isa<ShuffleVectorSDNode>(N0) && 4092 isa<ShuffleVectorSDNode>(N1) && 4093 // Avoid folding a node with illegal type. 4094 TLI.isTypeLegal(VT)) { 4095 bool ZeroN00 = ISD::isBuildVectorAllZeros(N0.getOperand(0).getNode()); 4096 bool ZeroN01 = ISD::isBuildVectorAllZeros(N0.getOperand(1).getNode()); 4097 bool ZeroN10 = ISD::isBuildVectorAllZeros(N1.getOperand(0).getNode()); 4098 bool ZeroN11 = ISD::isBuildVectorAllZeros(N1.getOperand(1).getNode()); 4099 // Ensure both shuffles have a zero input. 4100 if ((ZeroN00 || ZeroN01) && (ZeroN10 || ZeroN11)) { 4101 assert((!ZeroN00 || !ZeroN01) && "Both inputs zero!"); 4102 assert((!ZeroN10 || !ZeroN11) && "Both inputs zero!"); 4103 const ShuffleVectorSDNode *SV0 = cast<ShuffleVectorSDNode>(N0); 4104 const ShuffleVectorSDNode *SV1 = cast<ShuffleVectorSDNode>(N1); 4105 bool CanFold = true; 4106 int NumElts = VT.getVectorNumElements(); 4107 SmallVector<int, 4> Mask(NumElts); 4108 4109 for (int i = 0; i != NumElts; ++i) { 4110 int M0 = SV0->getMaskElt(i); 4111 int M1 = SV1->getMaskElt(i); 4112 4113 // Determine if either index is pointing to a zero vector. 4114 bool M0Zero = M0 < 0 || (ZeroN00 == (M0 < NumElts)); 4115 bool M1Zero = M1 < 0 || (ZeroN10 == (M1 < NumElts)); 4116 4117 // If one element is zero and the otherside is undef, keep undef. 4118 // This also handles the case that both are undef. 4119 if ((M0Zero && M1 < 0) || (M1Zero && M0 < 0)) { 4120 Mask[i] = -1; 4121 continue; 4122 } 4123 4124 // Make sure only one of the elements is zero. 4125 if (M0Zero == M1Zero) { 4126 CanFold = false; 4127 break; 4128 } 4129 4130 assert((M0 >= 0 || M1 >= 0) && "Undef index!"); 4131 4132 // We have a zero and non-zero element. If the non-zero came from 4133 // SV0 make the index a LHS index. If it came from SV1, make it 4134 // a RHS index. We need to mod by NumElts because we don't care 4135 // which operand it came from in the original shuffles. 4136 Mask[i] = M1Zero ? M0 % NumElts : (M1 % NumElts) + NumElts; 4137 } 4138 4139 if (CanFold) { 4140 SDValue NewLHS = ZeroN00 ? N0.getOperand(1) : N0.getOperand(0); 4141 SDValue NewRHS = ZeroN10 ? N1.getOperand(1) : N1.getOperand(0); 4142 4143 bool LegalMask = TLI.isShuffleMaskLegal(Mask, VT); 4144 if (!LegalMask) { 4145 std::swap(NewLHS, NewRHS); 4146 ShuffleVectorSDNode::commuteMask(Mask); 4147 LegalMask = TLI.isShuffleMaskLegal(Mask, VT); 4148 } 4149 4150 if (LegalMask) 4151 return DAG.getVectorShuffle(VT, SDLoc(N), NewLHS, NewRHS, Mask); 4152 } 4153 } 4154 } 4155 } 4156 4157 // fold (or c1, c2) -> c1|c2 4158 ConstantSDNode *N0C = getAsNonOpaqueConstant(N0); 4159 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); 4160 if (N0C && N1C && !N1C->isOpaque()) 4161 return DAG.FoldConstantArithmetic(ISD::OR, SDLoc(N), VT, N0C, N1C); 4162 // canonicalize constant to RHS 4163 if (DAG.isConstantIntBuildVectorOrConstantInt(N0) && 4164 !DAG.isConstantIntBuildVectorOrConstantInt(N1)) 4165 return DAG.getNode(ISD::OR, SDLoc(N), VT, N1, N0); 4166 // fold (or x, 0) -> x 4167 if (isNullConstant(N1)) 4168 return N0; 4169 // fold (or x, -1) -> -1 4170 if (isAllOnesConstant(N1)) 4171 return N1; 4172 4173 if (SDValue NewSel = foldBinOpIntoSelect(N)) 4174 return NewSel; 4175 4176 // fold (or x, c) -> c iff (x & ~c) == 0 4177 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue())) 4178 return N1; 4179 4180 if (SDValue Combined = visitORLike(N0, N1, N)) 4181 return Combined; 4182 4183 // Recognize halfword bswaps as (bswap + rotl 16) or (bswap + shl 16) 4184 if (SDValue BSwap = MatchBSwapHWord(N, N0, N1)) 4185 return BSwap; 4186 if (SDValue BSwap = MatchBSwapHWordLow(N, N0, N1)) 4187 return BSwap; 4188 4189 // reassociate or 4190 if (SDValue ROR = ReassociateOps(ISD::OR, SDLoc(N), N0, N1)) 4191 return ROR; 4192 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2) 4193 // iff (c1 & c2) == 0. 4194 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() && 4195 isa<ConstantSDNode>(N0.getOperand(1))) { 4196 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1)); 4197 if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0) { 4198 if (SDValue COR = DAG.FoldConstantArithmetic(ISD::OR, SDLoc(N1), VT, 4199 N1C, C1)) 4200 return DAG.getNode( 4201 ISD::AND, SDLoc(N), VT, 4202 DAG.getNode(ISD::OR, SDLoc(N0), VT, N0.getOperand(0), N1), COR); 4203 return SDValue(); 4204 } 4205 } 4206 // Simplify: (or (op x...), (op y...)) -> (op (or x, y)) 4207 if (N0.getOpcode() == N1.getOpcode()) 4208 if (SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N)) 4209 return Tmp; 4210 4211 // See if this is some rotate idiom. 4212 if (SDNode *Rot = MatchRotate(N0, N1, SDLoc(N))) 4213 return SDValue(Rot, 0); 4214 4215 if (SDValue Load = MatchLoadCombine(N)) 4216 return Load; 4217 4218 // Simplify the operands using demanded-bits information. 4219 if (!VT.isVector() && 4220 SimplifyDemandedBits(SDValue(N, 0))) 4221 return SDValue(N, 0); 4222 4223 return SDValue(); 4224 } 4225 4226 /// Match "(X shl/srl V1) & V2" where V2 may not be present. 4227 bool DAGCombiner::MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) { 4228 if (Op.getOpcode() == ISD::AND) { 4229 if (DAG.isConstantIntBuildVectorOrConstantInt(Op.getOperand(1))) { 4230 Mask = Op.getOperand(1); 4231 Op = Op.getOperand(0); 4232 } else { 4233 return false; 4234 } 4235 } 4236 4237 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) { 4238 Shift = Op; 4239 return true; 4240 } 4241 4242 return false; 4243 } 4244 4245 // Return true if we can prove that, whenever Neg and Pos are both in the 4246 // range [0, EltSize), Neg == (Pos == 0 ? 0 : EltSize - Pos). This means that 4247 // for two opposing shifts shift1 and shift2 and a value X with OpBits bits: 4248 // 4249 // (or (shift1 X, Neg), (shift2 X, Pos)) 4250 // 4251 // reduces to a rotate in direction shift2 by Pos or (equivalently) a rotate 4252 // in direction shift1 by Neg. The range [0, EltSize) means that we only need 4253 // to consider shift amounts with defined behavior. 4254 static bool matchRotateSub(SDValue Pos, SDValue Neg, unsigned EltSize) { 4255 // If EltSize is a power of 2 then: 4256 // 4257 // (a) (Pos == 0 ? 0 : EltSize - Pos) == (EltSize - Pos) & (EltSize - 1) 4258 // (b) Neg == Neg & (EltSize - 1) whenever Neg is in [0, EltSize). 4259 // 4260 // So if EltSize is a power of 2 and Neg is (and Neg', EltSize-1), we check 4261 // for the stronger condition: 4262 // 4263 // Neg & (EltSize - 1) == (EltSize - Pos) & (EltSize - 1) [A] 4264 // 4265 // for all Neg and Pos. Since Neg & (EltSize - 1) == Neg' & (EltSize - 1) 4266 // we can just replace Neg with Neg' for the rest of the function. 4267 // 4268 // In other cases we check for the even stronger condition: 4269 // 4270 // Neg == EltSize - Pos [B] 4271 // 4272 // for all Neg and Pos. Note that the (or ...) then invokes undefined 4273 // behavior if Pos == 0 (and consequently Neg == EltSize). 4274 // 4275 // We could actually use [A] whenever EltSize is a power of 2, but the 4276 // only extra cases that it would match are those uninteresting ones 4277 // where Neg and Pos are never in range at the same time. E.g. for 4278 // EltSize == 32, using [A] would allow a Neg of the form (sub 64, Pos) 4279 // as well as (sub 32, Pos), but: 4280 // 4281 // (or (shift1 X, (sub 64, Pos)), (shift2 X, Pos)) 4282 // 4283 // always invokes undefined behavior for 32-bit X. 4284 // 4285 // Below, Mask == EltSize - 1 when using [A] and is all-ones otherwise. 4286 unsigned MaskLoBits = 0; 4287 if (Neg.getOpcode() == ISD::AND && isPowerOf2_64(EltSize)) { 4288 if (ConstantSDNode *NegC = isConstOrConstSplat(Neg.getOperand(1))) { 4289 if (NegC->getAPIntValue() == EltSize - 1) { 4290 Neg = Neg.getOperand(0); 4291 MaskLoBits = Log2_64(EltSize); 4292 } 4293 } 4294 } 4295 4296 // Check whether Neg has the form (sub NegC, NegOp1) for some NegC and NegOp1. 4297 if (Neg.getOpcode() != ISD::SUB) 4298 return false; 4299 ConstantSDNode *NegC = isConstOrConstSplat(Neg.getOperand(0)); 4300 if (!NegC) 4301 return false; 4302 SDValue NegOp1 = Neg.getOperand(1); 4303 4304 // On the RHS of [A], if Pos is Pos' & (EltSize - 1), just replace Pos with 4305 // Pos'. The truncation is redundant for the purpose of the equality. 4306 if (MaskLoBits && Pos.getOpcode() == ISD::AND) 4307 if (ConstantSDNode *PosC = isConstOrConstSplat(Pos.getOperand(1))) 4308 if (PosC->getAPIntValue() == EltSize - 1) 4309 Pos = Pos.getOperand(0); 4310 4311 // The condition we need is now: 4312 // 4313 // (NegC - NegOp1) & Mask == (EltSize - Pos) & Mask 4314 // 4315 // If NegOp1 == Pos then we need: 4316 // 4317 // EltSize & Mask == NegC & Mask 4318 // 4319 // (because "x & Mask" is a truncation and distributes through subtraction). 4320 APInt Width; 4321 if (Pos == NegOp1) 4322 Width = NegC->getAPIntValue(); 4323 4324 // Check for cases where Pos has the form (add NegOp1, PosC) for some PosC. 4325 // Then the condition we want to prove becomes: 4326 // 4327 // (NegC - NegOp1) & Mask == (EltSize - (NegOp1 + PosC)) & Mask 4328 // 4329 // which, again because "x & Mask" is a truncation, becomes: 4330 // 4331 // NegC & Mask == (EltSize - PosC) & Mask 4332 // EltSize & Mask == (NegC + PosC) & Mask 4333 else if (Pos.getOpcode() == ISD::ADD && Pos.getOperand(0) == NegOp1) { 4334 if (ConstantSDNode *PosC = isConstOrConstSplat(Pos.getOperand(1))) 4335 Width = PosC->getAPIntValue() + NegC->getAPIntValue(); 4336 else 4337 return false; 4338 } else 4339 return false; 4340 4341 // Now we just need to check that EltSize & Mask == Width & Mask. 4342 if (MaskLoBits) 4343 // EltSize & Mask is 0 since Mask is EltSize - 1. 4344 return Width.getLoBits(MaskLoBits) == 0; 4345 return Width == EltSize; 4346 } 4347 4348 // A subroutine of MatchRotate used once we have found an OR of two opposite 4349 // shifts of Shifted. If Neg == <operand size> - Pos then the OR reduces 4350 // to both (PosOpcode Shifted, Pos) and (NegOpcode Shifted, Neg), with the 4351 // former being preferred if supported. InnerPos and InnerNeg are Pos and 4352 // Neg with outer conversions stripped away. 4353 SDNode *DAGCombiner::MatchRotatePosNeg(SDValue Shifted, SDValue Pos, 4354 SDValue Neg, SDValue InnerPos, 4355 SDValue InnerNeg, unsigned PosOpcode, 4356 unsigned NegOpcode, const SDLoc &DL) { 4357 // fold (or (shl x, (*ext y)), 4358 // (srl x, (*ext (sub 32, y)))) -> 4359 // (rotl x, y) or (rotr x, (sub 32, y)) 4360 // 4361 // fold (or (shl x, (*ext (sub 32, y))), 4362 // (srl x, (*ext y))) -> 4363 // (rotr x, y) or (rotl x, (sub 32, y)) 4364 EVT VT = Shifted.getValueType(); 4365 if (matchRotateSub(InnerPos, InnerNeg, VT.getScalarSizeInBits())) { 4366 bool HasPos = TLI.isOperationLegalOrCustom(PosOpcode, VT); 4367 return DAG.getNode(HasPos ? PosOpcode : NegOpcode, DL, VT, Shifted, 4368 HasPos ? Pos : Neg).getNode(); 4369 } 4370 4371 return nullptr; 4372 } 4373 4374 // MatchRotate - Handle an 'or' of two operands. If this is one of the many 4375 // idioms for rotate, and if the target supports rotation instructions, generate 4376 // a rot[lr]. 4377 SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, const SDLoc &DL) { 4378 // Must be a legal type. Expanded 'n promoted things won't work with rotates. 4379 EVT VT = LHS.getValueType(); 4380 if (!TLI.isTypeLegal(VT)) return nullptr; 4381 4382 // The target must have at least one rotate flavor. 4383 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT); 4384 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT); 4385 if (!HasROTL && !HasROTR) return nullptr; 4386 4387 // Match "(X shl/srl V1) & V2" where V2 may not be present. 4388 SDValue LHSShift; // The shift. 4389 SDValue LHSMask; // AND value if any. 4390 if (!MatchRotateHalf(LHS, LHSShift, LHSMask)) 4391 return nullptr; // Not part of a rotate. 4392 4393 SDValue RHSShift; // The shift. 4394 SDValue RHSMask; // AND value if any. 4395 if (!MatchRotateHalf(RHS, RHSShift, RHSMask)) 4396 return nullptr; // Not part of a rotate. 4397 4398 if (LHSShift.getOperand(0) != RHSShift.getOperand(0)) 4399 return nullptr; // Not shifting the same value. 4400 4401 if (LHSShift.getOpcode() == RHSShift.getOpcode()) 4402 return nullptr; // Shifts must disagree. 4403 4404 // Canonicalize shl to left side in a shl/srl pair. 4405 if (RHSShift.getOpcode() == ISD::SHL) { 4406 std::swap(LHS, RHS); 4407 std::swap(LHSShift, RHSShift); 4408 std::swap(LHSMask, RHSMask); 4409 } 4410 4411 unsigned EltSizeInBits = VT.getScalarSizeInBits(); 4412 SDValue LHSShiftArg = LHSShift.getOperand(0); 4413 SDValue LHSShiftAmt = LHSShift.getOperand(1); 4414 SDValue RHSShiftArg = RHSShift.getOperand(0); 4415 SDValue RHSShiftAmt = RHSShift.getOperand(1); 4416 4417 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1) 4418 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2) 4419 if (isConstOrConstSplat(LHSShiftAmt) && isConstOrConstSplat(RHSShiftAmt)) { 4420 uint64_t LShVal = isConstOrConstSplat(LHSShiftAmt)->getZExtValue(); 4421 uint64_t RShVal = isConstOrConstSplat(RHSShiftAmt)->getZExtValue(); 4422 if ((LShVal + RShVal) != EltSizeInBits) 4423 return nullptr; 4424 4425 SDValue Rot = DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT, 4426 LHSShiftArg, HasROTL ? LHSShiftAmt : RHSShiftAmt); 4427 4428 // If there is an AND of either shifted operand, apply it to the result. 4429 if (LHSMask.getNode() || RHSMask.getNode()) { 4430 SDValue Mask = DAG.getAllOnesConstant(DL, VT); 4431 4432 if (LHSMask.getNode()) { 4433 APInt RHSBits = APInt::getLowBitsSet(EltSizeInBits, LShVal); 4434 Mask = DAG.getNode(ISD::AND, DL, VT, Mask, 4435 DAG.getNode(ISD::OR, DL, VT, LHSMask, 4436 DAG.getConstant(RHSBits, DL, VT))); 4437 } 4438 if (RHSMask.getNode()) { 4439 APInt LHSBits = APInt::getHighBitsSet(EltSizeInBits, RShVal); 4440 Mask = DAG.getNode(ISD::AND, DL, VT, Mask, 4441 DAG.getNode(ISD::OR, DL, VT, RHSMask, 4442 DAG.getConstant(LHSBits, DL, VT))); 4443 } 4444 4445 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, Mask); 4446 } 4447 4448 return Rot.getNode(); 4449 } 4450 4451 // If there is a mask here, and we have a variable shift, we can't be sure 4452 // that we're masking out the right stuff. 4453 if (LHSMask.getNode() || RHSMask.getNode()) 4454 return nullptr; 4455 4456 // If the shift amount is sign/zext/any-extended just peel it off. 4457 SDValue LExtOp0 = LHSShiftAmt; 4458 SDValue RExtOp0 = RHSShiftAmt; 4459 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND || 4460 LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND || 4461 LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND || 4462 LHSShiftAmt.getOpcode() == ISD::TRUNCATE) && 4463 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND || 4464 RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND || 4465 RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND || 4466 RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) { 4467 LExtOp0 = LHSShiftAmt.getOperand(0); 4468 RExtOp0 = RHSShiftAmt.getOperand(0); 4469 } 4470 4471 SDNode *TryL = MatchRotatePosNeg(LHSShiftArg, LHSShiftAmt, RHSShiftAmt, 4472 LExtOp0, RExtOp0, ISD::ROTL, ISD::ROTR, DL); 4473 if (TryL) 4474 return TryL; 4475 4476 SDNode *TryR = MatchRotatePosNeg(RHSShiftArg, RHSShiftAmt, LHSShiftAmt, 4477 RExtOp0, LExtOp0, ISD::ROTR, ISD::ROTL, DL); 4478 if (TryR) 4479 return TryR; 4480 4481 return nullptr; 4482 } 4483 4484 namespace { 4485 /// Helper struct to parse and store a memory address as base + index + offset. 4486 /// We ignore sign extensions when it is safe to do so. 4487 /// The following two expressions are not equivalent. To differentiate we need 4488 /// to store whether there was a sign extension involved in the index 4489 /// computation. 4490 /// (load (i64 add (i64 copyfromreg %c) 4491 /// (i64 signextend (add (i8 load %index) 4492 /// (i8 1)))) 4493 /// vs 4494 /// 4495 /// (load (i64 add (i64 copyfromreg %c) 4496 /// (i64 signextend (i32 add (i32 signextend (i8 load %index)) 4497 /// (i32 1))))) 4498 struct BaseIndexOffset { 4499 SDValue Base; 4500 SDValue Index; 4501 int64_t Offset; 4502 bool IsIndexSignExt; 4503 4504 BaseIndexOffset() : Offset(0), IsIndexSignExt(false) {} 4505 4506 BaseIndexOffset(SDValue Base, SDValue Index, int64_t Offset, 4507 bool IsIndexSignExt) : 4508 Base(Base), Index(Index), Offset(Offset), IsIndexSignExt(IsIndexSignExt) {} 4509 4510 bool equalBaseIndex(const BaseIndexOffset &Other) { 4511 return Other.Base == Base && Other.Index == Index && 4512 Other.IsIndexSignExt == IsIndexSignExt; 4513 } 4514 4515 /// Parses tree in Ptr for base, index, offset addresses. 4516 static BaseIndexOffset match(SDValue Ptr, SelectionDAG &DAG, 4517 int64_t PartialOffset = 0) { 4518 bool IsIndexSignExt = false; 4519 4520 // Split up a folded GlobalAddress+Offset into its component parts. 4521 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Ptr)) 4522 if (GA->getOpcode() == ISD::GlobalAddress && GA->getOffset() != 0) { 4523 return BaseIndexOffset(DAG.getGlobalAddress(GA->getGlobal(), 4524 SDLoc(GA), 4525 GA->getValueType(0), 4526 /*Offset=*/PartialOffset, 4527 /*isTargetGA=*/false, 4528 GA->getTargetFlags()), 4529 SDValue(), 4530 GA->getOffset(), 4531 IsIndexSignExt); 4532 } 4533 4534 // We only can pattern match BASE + INDEX + OFFSET. If Ptr is not an ADD 4535 // instruction, then it could be just the BASE or everything else we don't 4536 // know how to handle. Just use Ptr as BASE and give up. 4537 if (Ptr->getOpcode() != ISD::ADD) 4538 return BaseIndexOffset(Ptr, SDValue(), PartialOffset, IsIndexSignExt); 4539 4540 // We know that we have at least an ADD instruction. Try to pattern match 4541 // the simple case of BASE + OFFSET. 4542 if (isa<ConstantSDNode>(Ptr->getOperand(1))) { 4543 int64_t Offset = cast<ConstantSDNode>(Ptr->getOperand(1))->getSExtValue(); 4544 return match(Ptr->getOperand(0), DAG, Offset + PartialOffset); 4545 } 4546 4547 // Inside a loop the current BASE pointer is calculated using an ADD and a 4548 // MUL instruction. In this case Ptr is the actual BASE pointer. 4549 // (i64 add (i64 %array_ptr) 4550 // (i64 mul (i64 %induction_var) 4551 // (i64 %element_size))) 4552 if (Ptr->getOperand(1)->getOpcode() == ISD::MUL) 4553 return BaseIndexOffset(Ptr, SDValue(), PartialOffset, IsIndexSignExt); 4554 4555 // Look at Base + Index + Offset cases. 4556 SDValue Base = Ptr->getOperand(0); 4557 SDValue IndexOffset = Ptr->getOperand(1); 4558 4559 // Skip signextends. 4560 if (IndexOffset->getOpcode() == ISD::SIGN_EXTEND) { 4561 IndexOffset = IndexOffset->getOperand(0); 4562 IsIndexSignExt = true; 4563 } 4564 4565 // Either the case of Base + Index (no offset) or something else. 4566 if (IndexOffset->getOpcode() != ISD::ADD) 4567 return BaseIndexOffset(Base, IndexOffset, PartialOffset, IsIndexSignExt); 4568 4569 // Now we have the case of Base + Index + offset. 4570 SDValue Index = IndexOffset->getOperand(0); 4571 SDValue Offset = IndexOffset->getOperand(1); 4572 4573 if (!isa<ConstantSDNode>(Offset)) 4574 return BaseIndexOffset(Ptr, SDValue(), PartialOffset, IsIndexSignExt); 4575 4576 // Ignore signextends. 4577 if (Index->getOpcode() == ISD::SIGN_EXTEND) { 4578 Index = Index->getOperand(0); 4579 IsIndexSignExt = true; 4580 } else IsIndexSignExt = false; 4581 4582 int64_t Off = cast<ConstantSDNode>(Offset)->getSExtValue(); 4583 return BaseIndexOffset(Base, Index, Off + PartialOffset, IsIndexSignExt); 4584 } 4585 }; 4586 } // namespace 4587 4588 namespace { 4589 /// Represents known origin of an individual byte in load combine pattern. The 4590 /// value of the byte is either constant zero or comes from memory. 4591 struct ByteProvider { 4592 // For constant zero providers Load is set to nullptr. For memory providers 4593 // Load represents the node which loads the byte from memory. 4594 // ByteOffset is the offset of the byte in the value produced by the load. 4595 LoadSDNode *Load; 4596 unsigned ByteOffset; 4597 4598 ByteProvider() : Load(nullptr), ByteOffset(0) {} 4599 4600 static ByteProvider getMemory(LoadSDNode *Load, unsigned ByteOffset) { 4601 return ByteProvider(Load, ByteOffset); 4602 } 4603 static ByteProvider getConstantZero() { return ByteProvider(nullptr, 0); } 4604 4605 bool isConstantZero() const { return !Load; } 4606 bool isMemory() const { return Load; } 4607 4608 bool operator==(const ByteProvider &Other) const { 4609 return Other.Load == Load && Other.ByteOffset == ByteOffset; 4610 } 4611 4612 private: 4613 ByteProvider(LoadSDNode *Load, unsigned ByteOffset) 4614 : Load(Load), ByteOffset(ByteOffset) {} 4615 }; 4616 4617 /// Recursively traverses the expression calculating the origin of the requested 4618 /// byte of the given value. Returns None if the provider can't be calculated. 4619 /// 4620 /// For all the values except the root of the expression verifies that the value 4621 /// has exactly one use and if it's not true return None. This way if the origin 4622 /// of the byte is returned it's guaranteed that the values which contribute to 4623 /// the byte are not used outside of this expression. 4624 /// 4625 /// Because the parts of the expression are not allowed to have more than one 4626 /// use this function iterates over trees, not DAGs. So it never visits the same 4627 /// node more than once. 4628 const Optional<ByteProvider> calculateByteProvider(SDValue Op, unsigned Index, 4629 unsigned Depth, 4630 bool Root = false) { 4631 // Typical i64 by i8 pattern requires recursion up to 8 calls depth 4632 if (Depth == 10) 4633 return None; 4634 4635 if (!Root && !Op.hasOneUse()) 4636 return None; 4637 4638 assert(Op.getValueType().isScalarInteger() && "can't handle other types"); 4639 unsigned BitWidth = Op.getValueSizeInBits(); 4640 if (BitWidth % 8 != 0) 4641 return None; 4642 unsigned ByteWidth = BitWidth / 8; 4643 assert(Index < ByteWidth && "invalid index requested"); 4644 (void) ByteWidth; 4645 4646 switch (Op.getOpcode()) { 4647 case ISD::OR: { 4648 auto LHS = calculateByteProvider(Op->getOperand(0), Index, Depth + 1); 4649 if (!LHS) 4650 return None; 4651 auto RHS = calculateByteProvider(Op->getOperand(1), Index, Depth + 1); 4652 if (!RHS) 4653 return None; 4654 4655 if (LHS->isConstantZero()) 4656 return RHS; 4657 else if (RHS->isConstantZero()) 4658 return LHS; 4659 else 4660 return None; 4661 } 4662 case ISD::SHL: { 4663 auto ShiftOp = dyn_cast<ConstantSDNode>(Op->getOperand(1)); 4664 if (!ShiftOp) 4665 return None; 4666 4667 uint64_t BitShift = ShiftOp->getZExtValue(); 4668 if (BitShift % 8 != 0) 4669 return None; 4670 uint64_t ByteShift = BitShift / 8; 4671 4672 return Index < ByteShift 4673 ? ByteProvider::getConstantZero() 4674 : calculateByteProvider(Op->getOperand(0), Index - ByteShift, 4675 Depth + 1); 4676 } 4677 case ISD::ANY_EXTEND: 4678 case ISD::SIGN_EXTEND: 4679 case ISD::ZERO_EXTEND: { 4680 SDValue NarrowOp = Op->getOperand(0); 4681 unsigned NarrowBitWidth = NarrowOp.getScalarValueSizeInBits(); 4682 if (NarrowBitWidth % 8 != 0) 4683 return None; 4684 uint64_t NarrowByteWidth = NarrowBitWidth / 8; 4685 4686 if (Index >= NarrowByteWidth) 4687 return Op.getOpcode() == ISD::ZERO_EXTEND 4688 ? Optional<ByteProvider>(ByteProvider::getConstantZero()) 4689 : None; 4690 else 4691 return calculateByteProvider(NarrowOp, Index, Depth + 1); 4692 } 4693 case ISD::BSWAP: 4694 return calculateByteProvider(Op->getOperand(0), ByteWidth - Index - 1, 4695 Depth + 1); 4696 case ISD::LOAD: { 4697 auto L = cast<LoadSDNode>(Op.getNode()); 4698 if (L->isVolatile() || L->isIndexed()) 4699 return None; 4700 4701 unsigned NarrowBitWidth = L->getMemoryVT().getSizeInBits(); 4702 if (NarrowBitWidth % 8 != 0) 4703 return None; 4704 uint64_t NarrowByteWidth = NarrowBitWidth / 8; 4705 4706 if (Index >= NarrowByteWidth) 4707 return L->getExtensionType() == ISD::ZEXTLOAD 4708 ? Optional<ByteProvider>(ByteProvider::getConstantZero()) 4709 : None; 4710 else 4711 return ByteProvider::getMemory(L, Index); 4712 } 4713 } 4714 4715 return None; 4716 } 4717 } // namespace 4718 4719 /// Match a pattern where a wide type scalar value is loaded by several narrow 4720 /// loads and combined by shifts and ors. Fold it into a single load or a load 4721 /// and a BSWAP if the targets supports it. 4722 /// 4723 /// Assuming little endian target: 4724 /// i8 *a = ... 4725 /// i32 val = a[0] | (a[1] << 8) | (a[2] << 16) | (a[3] << 24) 4726 /// => 4727 /// i32 val = *((i32)a) 4728 /// 4729 /// i8 *a = ... 4730 /// i32 val = (a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3] 4731 /// => 4732 /// i32 val = BSWAP(*((i32)a)) 4733 /// 4734 /// TODO: This rule matches complex patterns with OR node roots and doesn't 4735 /// interact well with the worklist mechanism. When a part of the pattern is 4736 /// updated (e.g. one of the loads) its direct users are put into the worklist, 4737 /// but the root node of the pattern which triggers the load combine is not 4738 /// necessarily a direct user of the changed node. For example, once the address 4739 /// of t28 load is reassociated load combine won't be triggered: 4740 /// t25: i32 = add t4, Constant:i32<2> 4741 /// t26: i64 = sign_extend t25 4742 /// t27: i64 = add t2, t26 4743 /// t28: i8,ch = load<LD1[%tmp9]> t0, t27, undef:i64 4744 /// t29: i32 = zero_extend t28 4745 /// t32: i32 = shl t29, Constant:i8<8> 4746 /// t33: i32 = or t23, t32 4747 /// As a possible fix visitLoad can check if the load can be a part of a load 4748 /// combine pattern and add corresponding OR roots to the worklist. 4749 SDValue DAGCombiner::MatchLoadCombine(SDNode *N) { 4750 assert(N->getOpcode() == ISD::OR && 4751 "Can only match load combining against OR nodes"); 4752 4753 // Handles simple types only 4754 EVT VT = N->getValueType(0); 4755 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64) 4756 return SDValue(); 4757 unsigned ByteWidth = VT.getSizeInBits() / 8; 4758 4759 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 4760 // Before legalize we can introduce too wide illegal loads which will be later 4761 // split into legal sized loads. This enables us to combine i64 load by i8 4762 // patterns to a couple of i32 loads on 32 bit targets. 4763 if (LegalOperations && !TLI.isOperationLegal(ISD::LOAD, VT)) 4764 return SDValue(); 4765 4766 std::function<unsigned(unsigned, unsigned)> LittleEndianByteAt = []( 4767 unsigned BW, unsigned i) { return i; }; 4768 std::function<unsigned(unsigned, unsigned)> BigEndianByteAt = []( 4769 unsigned BW, unsigned i) { return BW - i - 1; }; 4770 4771 bool IsBigEndianTarget = DAG.getDataLayout().isBigEndian(); 4772 auto MemoryByteOffset = [&] (ByteProvider P) { 4773 assert(P.isMemory() && "Must be a memory byte provider"); 4774 unsigned LoadBitWidth = P.Load->getMemoryVT().getSizeInBits(); 4775 assert(LoadBitWidth % 8 == 0 && 4776 "can only analyze providers for individual bytes not bit"); 4777 unsigned LoadByteWidth = LoadBitWidth / 8; 4778 return IsBigEndianTarget 4779 ? BigEndianByteAt(LoadByteWidth, P.ByteOffset) 4780 : LittleEndianByteAt(LoadByteWidth, P.ByteOffset); 4781 }; 4782 4783 Optional<BaseIndexOffset> Base; 4784 SDValue Chain; 4785 4786 SmallSet<LoadSDNode *, 8> Loads; 4787 Optional<ByteProvider> FirstByteProvider; 4788 int64_t FirstOffset = INT64_MAX; 4789 4790 // Check if all the bytes of the OR we are looking at are loaded from the same 4791 // base address. Collect bytes offsets from Base address in ByteOffsets. 4792 SmallVector<int64_t, 4> ByteOffsets(ByteWidth); 4793 for (unsigned i = 0; i < ByteWidth; i++) { 4794 auto P = calculateByteProvider(SDValue(N, 0), i, 0, /*Root=*/true); 4795 if (!P || !P->isMemory()) // All the bytes must be loaded from memory 4796 return SDValue(); 4797 4798 LoadSDNode *L = P->Load; 4799 assert(L->hasNUsesOfValue(1, 0) && !L->isVolatile() && !L->isIndexed() && 4800 "Must be enforced by calculateByteProvider"); 4801 assert(L->getOffset().isUndef() && "Unindexed load must have undef offset"); 4802 4803 // All loads must share the same chain 4804 SDValue LChain = L->getChain(); 4805 if (!Chain) 4806 Chain = LChain; 4807 else if (Chain != LChain) 4808 return SDValue(); 4809 4810 // Loads must share the same base address 4811 BaseIndexOffset Ptr = BaseIndexOffset::match(L->getBasePtr(), DAG); 4812 if (!Base) 4813 Base = Ptr; 4814 else if (!Base->equalBaseIndex(Ptr)) 4815 return SDValue(); 4816 4817 // Calculate the offset of the current byte from the base address 4818 int64_t ByteOffsetFromBase = Ptr.Offset + MemoryByteOffset(*P); 4819 ByteOffsets[i] = ByteOffsetFromBase; 4820 4821 // Remember the first byte load 4822 if (ByteOffsetFromBase < FirstOffset) { 4823 FirstByteProvider = P; 4824 FirstOffset = ByteOffsetFromBase; 4825 } 4826 4827 Loads.insert(L); 4828 } 4829 assert(Loads.size() > 0 && "All the bytes of the value must be loaded from " 4830 "memory, so there must be at least one load which produces the value"); 4831 assert(Base && "Base address of the accessed memory location must be set"); 4832 assert(FirstOffset != INT64_MAX && "First byte offset must be set"); 4833 4834 // Check if the bytes of the OR we are looking at match with either big or 4835 // little endian value load 4836 bool BigEndian = true, LittleEndian = true; 4837 for (unsigned i = 0; i < ByteWidth; i++) { 4838 int64_t CurrentByteOffset = ByteOffsets[i] - FirstOffset; 4839 LittleEndian &= CurrentByteOffset == LittleEndianByteAt(ByteWidth, i); 4840 BigEndian &= CurrentByteOffset == BigEndianByteAt(ByteWidth, i); 4841 if (!BigEndian && !LittleEndian) 4842 return SDValue(); 4843 } 4844 assert((BigEndian != LittleEndian) && "should be either or"); 4845 assert(FirstByteProvider && "must be set"); 4846 4847 // Ensure that the first byte is loaded from zero offset of the first load. 4848 // So the combined value can be loaded from the first load address. 4849 if (MemoryByteOffset(*FirstByteProvider) != 0) 4850 return SDValue(); 4851 LoadSDNode *FirstLoad = FirstByteProvider->Load; 4852 4853 // The node we are looking at matches with the pattern, check if we can 4854 // replace it with a single load and bswap if needed. 4855 4856 // If the load needs byte swap check if the target supports it 4857 bool NeedsBswap = IsBigEndianTarget != BigEndian; 4858 4859 // Before legalize we can introduce illegal bswaps which will be later 4860 // converted to an explicit bswap sequence. This way we end up with a single 4861 // load and byte shuffling instead of several loads and byte shuffling. 4862 if (NeedsBswap && LegalOperations && !TLI.isOperationLegal(ISD::BSWAP, VT)) 4863 return SDValue(); 4864 4865 // Check that a load of the wide type is both allowed and fast on the target 4866 bool Fast = false; 4867 bool Allowed = TLI.allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), 4868 VT, FirstLoad->getAddressSpace(), 4869 FirstLoad->getAlignment(), &Fast); 4870 if (!Allowed || !Fast) 4871 return SDValue(); 4872 4873 SDValue NewLoad = 4874 DAG.getLoad(VT, SDLoc(N), Chain, FirstLoad->getBasePtr(), 4875 FirstLoad->getPointerInfo(), FirstLoad->getAlignment()); 4876 4877 // Transfer chain users from old loads to the new load. 4878 for (LoadSDNode *L : Loads) 4879 DAG.ReplaceAllUsesOfValueWith(SDValue(L, 1), SDValue(NewLoad.getNode(), 1)); 4880 4881 return NeedsBswap ? DAG.getNode(ISD::BSWAP, SDLoc(N), VT, NewLoad) : NewLoad; 4882 } 4883 4884 SDValue DAGCombiner::visitXOR(SDNode *N) { 4885 SDValue N0 = N->getOperand(0); 4886 SDValue N1 = N->getOperand(1); 4887 EVT VT = N0.getValueType(); 4888 4889 // fold vector ops 4890 if (VT.isVector()) { 4891 if (SDValue FoldedVOp = SimplifyVBinOp(N)) 4892 return FoldedVOp; 4893 4894 // fold (xor x, 0) -> x, vector edition 4895 if (ISD::isBuildVectorAllZeros(N0.getNode())) 4896 return N1; 4897 if (ISD::isBuildVectorAllZeros(N1.getNode())) 4898 return N0; 4899 } 4900 4901 // fold (xor undef, undef) -> 0. This is a common idiom (misuse). 4902 if (N0.isUndef() && N1.isUndef()) 4903 return DAG.getConstant(0, SDLoc(N), VT); 4904 // fold (xor x, undef) -> undef 4905 if (N0.isUndef()) 4906 return N0; 4907 if (N1.isUndef()) 4908 return N1; 4909 // fold (xor c1, c2) -> c1^c2 4910 ConstantSDNode *N0C = getAsNonOpaqueConstant(N0); 4911 ConstantSDNode *N1C = getAsNonOpaqueConstant(N1); 4912 if (N0C && N1C) 4913 return DAG.FoldConstantArithmetic(ISD::XOR, SDLoc(N), VT, N0C, N1C); 4914 // canonicalize constant to RHS 4915 if (DAG.isConstantIntBuildVectorOrConstantInt(N0) && 4916 !DAG.isConstantIntBuildVectorOrConstantInt(N1)) 4917 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0); 4918 // fold (xor x, 0) -> x 4919 if (isNullConstant(N1)) 4920 return N0; 4921 4922 if (SDValue NewSel = foldBinOpIntoSelect(N)) 4923 return NewSel; 4924 4925 // reassociate xor 4926 if (SDValue RXOR = ReassociateOps(ISD::XOR, SDLoc(N), N0, N1)) 4927 return RXOR; 4928 4929 // fold !(x cc y) -> (x !cc y) 4930 SDValue LHS, RHS, CC; 4931 if (TLI.isConstTrueVal(N1.getNode()) && isSetCCEquivalent(N0, LHS, RHS, CC)) { 4932 bool isInt = LHS.getValueType().isInteger(); 4933 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(), 4934 isInt); 4935 4936 if (!LegalOperations || 4937 TLI.isCondCodeLegal(NotCC, LHS.getSimpleValueType())) { 4938 switch (N0.getOpcode()) { 4939 default: 4940 llvm_unreachable("Unhandled SetCC Equivalent!"); 4941 case ISD::SETCC: 4942 return DAG.getSetCC(SDLoc(N0), VT, LHS, RHS, NotCC); 4943 case ISD::SELECT_CC: 4944 return DAG.getSelectCC(SDLoc(N0), LHS, RHS, N0.getOperand(2), 4945 N0.getOperand(3), NotCC); 4946 } 4947 } 4948 } 4949 4950 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y))) 4951 if (isOneConstant(N1) && N0.getOpcode() == ISD::ZERO_EXTEND && 4952 N0.getNode()->hasOneUse() && 4953 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){ 4954 SDValue V = N0.getOperand(0); 4955 SDLoc DL(N0); 4956 V = DAG.getNode(ISD::XOR, DL, V.getValueType(), V, 4957 DAG.getConstant(1, DL, V.getValueType())); 4958 AddToWorklist(V.getNode()); 4959 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, V); 4960 } 4961 4962 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc 4963 if (isOneConstant(N1) && VT == MVT::i1 && 4964 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) { 4965 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1); 4966 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) { 4967 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND; 4968 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS 4969 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS 4970 AddToWorklist(LHS.getNode()); AddToWorklist(RHS.getNode()); 4971 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS); 4972 } 4973 } 4974 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants 4975 if (isAllOnesConstant(N1) && 4976 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) { 4977 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1); 4978 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) { 4979 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND; 4980 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS 4981 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS 4982 AddToWorklist(LHS.getNode()); AddToWorklist(RHS.getNode()); 4983 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS); 4984 } 4985 } 4986 // fold (xor (and x, y), y) -> (and (not x), y) 4987 if (N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() && 4988 N0->getOperand(1) == N1) { 4989 SDValue X = N0->getOperand(0); 4990 SDValue NotX = DAG.getNOT(SDLoc(X), X, VT); 4991 AddToWorklist(NotX.getNode()); 4992 return DAG.getNode(ISD::AND, SDLoc(N), VT, NotX, N1); 4993 } 4994 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2)) 4995 if (N1C && N0.getOpcode() == ISD::XOR) { 4996 if (const ConstantSDNode *N00C = getAsNonOpaqueConstant(N0.getOperand(0))) { 4997 SDLoc DL(N); 4998 return DAG.getNode(ISD::XOR, DL, VT, N0.getOperand(1), 4999 DAG.getConstant(N1C->getAPIntValue() ^ 5000 N00C->getAPIntValue(), DL, VT)); 5001 } 5002 if (const ConstantSDNode *N01C = getAsNonOpaqueConstant(N0.getOperand(1))) { 5003 SDLoc DL(N); 5004 return DAG.getNode(ISD::XOR, DL, VT, N0.getOperand(0), 5005 DAG.getConstant(N1C->getAPIntValue() ^ 5006 N01C->getAPIntValue(), DL, VT)); 5007 } 5008 } 5009 5010 // fold Y = sra (X, size(X)-1); xor (add (X, Y), Y) -> (abs X) 5011 unsigned OpSizeInBits = VT.getScalarSizeInBits(); 5012 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1 && 5013 N1.getOpcode() == ISD::SRA && N1.getOperand(0) == N0.getOperand(0) && 5014 TLI.isOperationLegalOrCustom(ISD::ABS, VT)) { 5015 if (ConstantSDNode *C = isConstOrConstSplat(N1.getOperand(1))) 5016 if (C->getAPIntValue() == (OpSizeInBits - 1)) 5017 return DAG.getNode(ISD::ABS, SDLoc(N), VT, N0.getOperand(0)); 5018 } 5019 5020 // fold (xor x, x) -> 0 5021 if (N0 == N1) 5022 return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations, LegalTypes); 5023 5024 // fold (xor (shl 1, x), -1) -> (rotl ~1, x) 5025 // Here is a concrete example of this equivalence: 5026 // i16 x == 14 5027 // i16 shl == 1 << 14 == 16384 == 0b0100000000000000 5028 // i16 xor == ~(1 << 14) == 49151 == 0b1011111111111111 5029 // 5030 // => 5031 // 5032 // i16 ~1 == 0b1111111111111110 5033 // i16 rol(~1, 14) == 0b1011111111111111 5034 // 5035 // Some additional tips to help conceptualize this transform: 5036 // - Try to see the operation as placing a single zero in a value of all ones. 5037 // - There exists no value for x which would allow the result to contain zero. 5038 // - Values of x larger than the bitwidth are undefined and do not require a 5039 // consistent result. 5040 // - Pushing the zero left requires shifting one bits in from the right. 5041 // A rotate left of ~1 is a nice way of achieving the desired result. 5042 if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT) && N0.getOpcode() == ISD::SHL 5043 && isAllOnesConstant(N1) && isOneConstant(N0.getOperand(0))) { 5044 SDLoc DL(N); 5045 return DAG.getNode(ISD::ROTL, DL, VT, DAG.getConstant(~1, DL, VT), 5046 N0.getOperand(1)); 5047 } 5048 5049 // Simplify: xor (op x...), (op y...) -> (op (xor x, y)) 5050 if (N0.getOpcode() == N1.getOpcode()) 5051 if (SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N)) 5052 return Tmp; 5053 5054 // Simplify the expression using non-local knowledge. 5055 if (!VT.isVector() && 5056 SimplifyDemandedBits(SDValue(N, 0))) 5057 return SDValue(N, 0); 5058 5059 return SDValue(); 5060 } 5061 5062 /// Handle transforms common to the three shifts, when the shift amount is a 5063 /// constant. 5064 SDValue DAGCombiner::visitShiftByConstant(SDNode *N, ConstantSDNode *Amt) { 5065 SDNode *LHS = N->getOperand(0).getNode(); 5066 if (!LHS->hasOneUse()) return SDValue(); 5067 5068 // We want to pull some binops through shifts, so that we have (and (shift)) 5069 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of 5070 // thing happens with address calculations, so it's important to canonicalize 5071 // it. 5072 bool HighBitSet = false; // Can we transform this if the high bit is set? 5073 5074 switch (LHS->getOpcode()) { 5075 default: return SDValue(); 5076 case ISD::OR: 5077 case ISD::XOR: 5078 HighBitSet = false; // We can only transform sra if the high bit is clear. 5079 break; 5080 case ISD::AND: 5081 HighBitSet = true; // We can only transform sra if the high bit is set. 5082 break; 5083 case ISD::ADD: 5084 if (N->getOpcode() != ISD::SHL) 5085 return SDValue(); // only shl(add) not sr[al](add). 5086 HighBitSet = false; // We can only transform sra if the high bit is clear. 5087 break; 5088 } 5089 5090 // We require the RHS of the binop to be a constant and not opaque as well. 5091 ConstantSDNode *BinOpCst = getAsNonOpaqueConstant(LHS->getOperand(1)); 5092 if (!BinOpCst) return SDValue(); 5093 5094 // FIXME: disable this unless the input to the binop is a shift by a constant 5095 // or is copy/select.Enable this in other cases when figure out it's exactly profitable. 5096 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode(); 5097 bool isShift = BinOpLHSVal->getOpcode() == ISD::SHL || 5098 BinOpLHSVal->getOpcode() == ISD::SRA || 5099 BinOpLHSVal->getOpcode() == ISD::SRL; 5100 bool isCopyOrSelect = BinOpLHSVal->getOpcode() == ISD::CopyFromReg || 5101 BinOpLHSVal->getOpcode() == ISD::SELECT; 5102 5103 if ((!isShift || !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1))) && 5104 !isCopyOrSelect) 5105 return SDValue(); 5106 5107 if (isCopyOrSelect && N->hasOneUse()) 5108 return SDValue(); 5109 5110 EVT VT = N->getValueType(0); 5111 5112 // If this is a signed shift right, and the high bit is modified by the 5113 // logical operation, do not perform the transformation. The highBitSet 5114 // boolean indicates the value of the high bit of the constant which would 5115 // cause it to be modified for this operation. 5116 if (N->getOpcode() == ISD::SRA) { 5117 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative(); 5118 if (BinOpRHSSignSet != HighBitSet) 5119 return SDValue(); 5120 } 5121 5122 if (!TLI.isDesirableToCommuteWithShift(LHS)) 5123 return SDValue(); 5124 5125 // Fold the constants, shifting the binop RHS by the shift amount. 5126 SDValue NewRHS = DAG.getNode(N->getOpcode(), SDLoc(LHS->getOperand(1)), 5127 N->getValueType(0), 5128 LHS->getOperand(1), N->getOperand(1)); 5129 assert(isa<ConstantSDNode>(NewRHS) && "Folding was not successful!"); 5130 5131 // Create the new shift. 5132 SDValue NewShift = DAG.getNode(N->getOpcode(), 5133 SDLoc(LHS->getOperand(0)), 5134 VT, LHS->getOperand(0), N->getOperand(1)); 5135 5136 // Create the new binop. 5137 return DAG.getNode(LHS->getOpcode(), SDLoc(N), VT, NewShift, NewRHS); 5138 } 5139 5140 SDValue DAGCombiner::distributeTruncateThroughAnd(SDNode *N) { 5141 assert(N->getOpcode() == ISD::TRUNCATE); 5142 assert(N->getOperand(0).getOpcode() == ISD::AND); 5143 5144 // (truncate:TruncVT (and N00, N01C)) -> (and (truncate:TruncVT N00), TruncC) 5145 if (N->hasOneUse() && N->getOperand(0).hasOneUse()) { 5146 SDValue N01 = N->getOperand(0).getOperand(1); 5147 if (isConstantOrConstantVector(N01, /* NoOpaques */ true)) { 5148 SDLoc DL(N); 5149 EVT TruncVT = N->getValueType(0); 5150 SDValue N00 = N->getOperand(0).getOperand(0); 5151 SDValue Trunc00 = DAG.getNode(ISD::TRUNCATE, DL, TruncVT, N00); 5152 SDValue Trunc01 = DAG.getNode(ISD::TRUNCATE, DL, TruncVT, N01); 5153 AddToWorklist(Trunc00.getNode()); 5154 AddToWorklist(Trunc01.getNode()); 5155 return DAG.getNode(ISD::AND, DL, TruncVT, Trunc00, Trunc01); 5156 } 5157 } 5158 5159 return SDValue(); 5160 } 5161 5162 SDValue DAGCombiner::visitRotate(SDNode *N) { 5163 // fold (rot* x, (trunc (and y, c))) -> (rot* x, (and (trunc y), (trunc c))). 5164 if (N->getOperand(1).getOpcode() == ISD::TRUNCATE && 5165 N->getOperand(1).getOperand(0).getOpcode() == ISD::AND) { 5166 if (SDValue NewOp1 = 5167 distributeTruncateThroughAnd(N->getOperand(1).getNode())) 5168 return DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0), 5169 N->getOperand(0), NewOp1); 5170 } 5171 return SDValue(); 5172 } 5173 5174 SDValue DAGCombiner::visitSHL(SDNode *N) { 5175 SDValue N0 = N->getOperand(0); 5176 SDValue N1 = N->getOperand(1); 5177 EVT VT = N0.getValueType(); 5178 unsigned OpSizeInBits = VT.getScalarSizeInBits(); 5179 5180 // fold vector ops 5181 if (VT.isVector()) { 5182 if (SDValue FoldedVOp = SimplifyVBinOp(N)) 5183 return FoldedVOp; 5184 5185 BuildVectorSDNode *N1CV = dyn_cast<BuildVectorSDNode>(N1); 5186 // If setcc produces all-one true value then: 5187 // (shl (and (setcc) N01CV) N1CV) -> (and (setcc) N01CV<<N1CV) 5188 if (N1CV && N1CV->isConstant()) { 5189 if (N0.getOpcode() == ISD::AND) { 5190 SDValue N00 = N0->getOperand(0); 5191 SDValue N01 = N0->getOperand(1); 5192 BuildVectorSDNode *N01CV = dyn_cast<BuildVectorSDNode>(N01); 5193 5194 if (N01CV && N01CV->isConstant() && N00.getOpcode() == ISD::SETCC && 5195 TLI.getBooleanContents(N00.getOperand(0).getValueType()) == 5196 TargetLowering::ZeroOrNegativeOneBooleanContent) { 5197 if (SDValue C = DAG.FoldConstantArithmetic(ISD::SHL, SDLoc(N), VT, 5198 N01CV, N1CV)) 5199 return DAG.getNode(ISD::AND, SDLoc(N), VT, N00, C); 5200 } 5201 } 5202 } 5203 } 5204 5205 ConstantSDNode *N1C = isConstOrConstSplat(N1); 5206 5207 // fold (shl c1, c2) -> c1<<c2 5208 ConstantSDNode *N0C = getAsNonOpaqueConstant(N0); 5209 if (N0C && N1C && !N1C->isOpaque()) 5210 return DAG.FoldConstantArithmetic(ISD::SHL, SDLoc(N), VT, N0C, N1C); 5211 // fold (shl 0, x) -> 0 5212 if (isNullConstant(N0)) 5213 return N0; 5214 // fold (shl x, c >= size(x)) -> undef 5215 if (N1C && N1C->getAPIntValue().uge(OpSizeInBits)) 5216 return DAG.getUNDEF(VT); 5217 // fold (shl x, 0) -> x 5218 if (N1C && N1C->isNullValue()) 5219 return N0; 5220 // fold (shl undef, x) -> 0 5221 if (N0.isUndef()) 5222 return DAG.getConstant(0, SDLoc(N), VT); 5223 5224 if (SDValue NewSel = foldBinOpIntoSelect(N)) 5225 return NewSel; 5226 5227 // if (shl x, c) is known to be zero, return 0 5228 if (DAG.MaskedValueIsZero(SDValue(N, 0), 5229 APInt::getAllOnesValue(OpSizeInBits))) 5230 return DAG.getConstant(0, SDLoc(N), VT); 5231 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))). 5232 if (N1.getOpcode() == ISD::TRUNCATE && 5233 N1.getOperand(0).getOpcode() == ISD::AND) { 5234 if (SDValue NewOp1 = distributeTruncateThroughAnd(N1.getNode())) 5235 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0, NewOp1); 5236 } 5237 5238 if (N1C && SimplifyDemandedBits(SDValue(N, 0))) 5239 return SDValue(N, 0); 5240 5241 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2)) 5242 if (N1C && N0.getOpcode() == ISD::SHL) { 5243 if (ConstantSDNode *N0C1 = isConstOrConstSplat(N0.getOperand(1))) { 5244 SDLoc DL(N); 5245 APInt c1 = N0C1->getAPIntValue(); 5246 APInt c2 = N1C->getAPIntValue(); 5247 zeroExtendToMatch(c1, c2, 1 /* Overflow Bit */); 5248 5249 APInt Sum = c1 + c2; 5250 if (Sum.uge(OpSizeInBits)) 5251 return DAG.getConstant(0, DL, VT); 5252 5253 return DAG.getNode( 5254 ISD::SHL, DL, VT, N0.getOperand(0), 5255 DAG.getConstant(Sum.getZExtValue(), DL, N1.getValueType())); 5256 } 5257 } 5258 5259 // fold (shl (ext (shl x, c1)), c2) -> (ext (shl x, (add c1, c2))) 5260 // For this to be valid, the second form must not preserve any of the bits 5261 // that are shifted out by the inner shift in the first form. This means 5262 // the outer shift size must be >= the number of bits added by the ext. 5263 // As a corollary, we don't care what kind of ext it is. 5264 if (N1C && (N0.getOpcode() == ISD::ZERO_EXTEND || 5265 N0.getOpcode() == ISD::ANY_EXTEND || 5266 N0.getOpcode() == ISD::SIGN_EXTEND) && 5267 N0.getOperand(0).getOpcode() == ISD::SHL) { 5268 SDValue N0Op0 = N0.getOperand(0); 5269 if (ConstantSDNode *N0Op0C1 = isConstOrConstSplat(N0Op0.getOperand(1))) { 5270 APInt c1 = N0Op0C1->getAPIntValue(); 5271 APInt c2 = N1C->getAPIntValue(); 5272 zeroExtendToMatch(c1, c2, 1 /* Overflow Bit */); 5273 5274 EVT InnerShiftVT = N0Op0.getValueType(); 5275 uint64_t InnerShiftSize = InnerShiftVT.getScalarSizeInBits(); 5276 if (c2.uge(OpSizeInBits - InnerShiftSize)) { 5277 SDLoc DL(N0); 5278 APInt Sum = c1 + c2; 5279 if (Sum.uge(OpSizeInBits)) 5280 return DAG.getConstant(0, DL, VT); 5281 5282 return DAG.getNode( 5283 ISD::SHL, DL, VT, 5284 DAG.getNode(N0.getOpcode(), DL, VT, N0Op0->getOperand(0)), 5285 DAG.getConstant(Sum.getZExtValue(), DL, N1.getValueType())); 5286 } 5287 } 5288 } 5289 5290 // fold (shl (zext (srl x, C)), C) -> (zext (shl (srl x, C), C)) 5291 // Only fold this if the inner zext has no other uses to avoid increasing 5292 // the total number of instructions. 5293 if (N1C && N0.getOpcode() == ISD::ZERO_EXTEND && N0.hasOneUse() && 5294 N0.getOperand(0).getOpcode() == ISD::SRL) { 5295 SDValue N0Op0 = N0.getOperand(0); 5296 if (ConstantSDNode *N0Op0C1 = isConstOrConstSplat(N0Op0.getOperand(1))) { 5297 if (N0Op0C1->getAPIntValue().ult(VT.getScalarSizeInBits())) { 5298 uint64_t c1 = N0Op0C1->getZExtValue(); 5299 uint64_t c2 = N1C->getZExtValue(); 5300 if (c1 == c2) { 5301 SDValue NewOp0 = N0.getOperand(0); 5302 EVT CountVT = NewOp0.getOperand(1).getValueType(); 5303 SDLoc DL(N); 5304 SDValue NewSHL = DAG.getNode(ISD::SHL, DL, NewOp0.getValueType(), 5305 NewOp0, 5306 DAG.getConstant(c2, DL, CountVT)); 5307 AddToWorklist(NewSHL.getNode()); 5308 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N0), VT, NewSHL); 5309 } 5310 } 5311 } 5312 } 5313 5314 // fold (shl (sr[la] exact X, C1), C2) -> (shl X, (C2-C1)) if C1 <= C2 5315 // fold (shl (sr[la] exact X, C1), C2) -> (sr[la] X, (C2-C1)) if C1 > C2 5316 if (N1C && (N0.getOpcode() == ISD::SRL || N0.getOpcode() == ISD::SRA) && 5317 cast<BinaryWithFlagsSDNode>(N0)->Flags.hasExact()) { 5318 if (ConstantSDNode *N0C1 = isConstOrConstSplat(N0.getOperand(1))) { 5319 uint64_t C1 = N0C1->getZExtValue(); 5320 uint64_t C2 = N1C->getZExtValue(); 5321 SDLoc DL(N); 5322 if (C1 <= C2) 5323 return DAG.getNode(ISD::SHL, DL, VT, N0.getOperand(0), 5324 DAG.getConstant(C2 - C1, DL, N1.getValueType())); 5325 return DAG.getNode(N0.getOpcode(), DL, VT, N0.getOperand(0), 5326 DAG.getConstant(C1 - C2, DL, N1.getValueType())); 5327 } 5328 } 5329 5330 // fold (shl (srl x, c1), c2) -> (and (shl x, (sub c2, c1), MASK) or 5331 // (and (srl x, (sub c1, c2), MASK) 5332 // Only fold this if the inner shift has no other uses -- if it does, folding 5333 // this will increase the total number of instructions. 5334 if (N1C && N0.getOpcode() == ISD::SRL && N0.hasOneUse()) { 5335 if (ConstantSDNode *N0C1 = isConstOrConstSplat(N0.getOperand(1))) { 5336 uint64_t c1 = N0C1->getZExtValue(); 5337 if (c1 < OpSizeInBits) { 5338 uint64_t c2 = N1C->getZExtValue(); 5339 APInt Mask = APInt::getHighBitsSet(OpSizeInBits, OpSizeInBits - c1); 5340 SDValue Shift; 5341 if (c2 > c1) { 5342 Mask = Mask.shl(c2 - c1); 5343 SDLoc DL(N); 5344 Shift = DAG.getNode(ISD::SHL, DL, VT, N0.getOperand(0), 5345 DAG.getConstant(c2 - c1, DL, N1.getValueType())); 5346 } else { 5347 Mask = Mask.lshr(c1 - c2); 5348 SDLoc DL(N); 5349 Shift = DAG.getNode(ISD::SRL, DL, VT, N0.getOperand(0), 5350 DAG.getConstant(c1 - c2, DL, N1.getValueType())); 5351 } 5352 SDLoc DL(N0); 5353 return DAG.getNode(ISD::AND, DL, VT, Shift, 5354 DAG.getConstant(Mask, DL, VT)); 5355 } 5356 } 5357 } 5358 5359 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1)) 5360 if (N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1) && 5361 isConstantOrConstantVector(N1, /* No Opaques */ true)) { 5362 SDLoc DL(N); 5363 SDValue AllBits = DAG.getAllOnesConstant(DL, VT); 5364 SDValue HiBitsMask = DAG.getNode(ISD::SHL, DL, VT, AllBits, N1); 5365 return DAG.getNode(ISD::AND, DL, VT, N0.getOperand(0), HiBitsMask); 5366 } 5367 5368 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1 << c2) 5369 // Variant of version done on multiply, except mul by a power of 2 is turned 5370 // into a shift. 5371 if (N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() && 5372 isConstantOrConstantVector(N1, /* No Opaques */ true) && 5373 isConstantOrConstantVector(N0.getOperand(1), /* No Opaques */ true)) { 5374 SDValue Shl0 = DAG.getNode(ISD::SHL, SDLoc(N0), VT, N0.getOperand(0), N1); 5375 SDValue Shl1 = DAG.getNode(ISD::SHL, SDLoc(N1), VT, N0.getOperand(1), N1); 5376 AddToWorklist(Shl0.getNode()); 5377 AddToWorklist(Shl1.getNode()); 5378 return DAG.getNode(ISD::ADD, SDLoc(N), VT, Shl0, Shl1); 5379 } 5380 5381 // fold (shl (mul x, c1), c2) -> (mul x, c1 << c2) 5382 if (N0.getOpcode() == ISD::MUL && N0.getNode()->hasOneUse() && 5383 isConstantOrConstantVector(N1, /* No Opaques */ true) && 5384 isConstantOrConstantVector(N0.getOperand(1), /* No Opaques */ true)) { 5385 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(N1), VT, N0.getOperand(1), N1); 5386 if (isConstantOrConstantVector(Shl)) 5387 return DAG.getNode(ISD::MUL, SDLoc(N), VT, N0.getOperand(0), Shl); 5388 } 5389 5390 if (N1C && !N1C->isOpaque()) 5391 if (SDValue NewSHL = visitShiftByConstant(N, N1C)) 5392 return NewSHL; 5393 5394 return SDValue(); 5395 } 5396 5397 SDValue DAGCombiner::visitSRA(SDNode *N) { 5398 SDValue N0 = N->getOperand(0); 5399 SDValue N1 = N->getOperand(1); 5400 EVT VT = N0.getValueType(); 5401 unsigned OpSizeInBits = VT.getScalarSizeInBits(); 5402 5403 // Arithmetic shifting an all-sign-bit value is a no-op. 5404 if (DAG.ComputeNumSignBits(N0) == OpSizeInBits) 5405 return N0; 5406 5407 // fold vector ops 5408 if (VT.isVector()) 5409 if (SDValue FoldedVOp = SimplifyVBinOp(N)) 5410 return FoldedVOp; 5411 5412 ConstantSDNode *N1C = isConstOrConstSplat(N1); 5413 5414 // fold (sra c1, c2) -> (sra c1, c2) 5415 ConstantSDNode *N0C = getAsNonOpaqueConstant(N0); 5416 if (N0C && N1C && !N1C->isOpaque()) 5417 return DAG.FoldConstantArithmetic(ISD::SRA, SDLoc(N), VT, N0C, N1C); 5418 // fold (sra 0, x) -> 0 5419 if (isNullConstant(N0)) 5420 return N0; 5421 // fold (sra -1, x) -> -1 5422 if (isAllOnesConstant(N0)) 5423 return N0; 5424 // fold (sra x, c >= size(x)) -> undef 5425 if (N1C && N1C->getAPIntValue().uge(OpSizeInBits)) 5426 return DAG.getUNDEF(VT); 5427 // fold (sra x, 0) -> x 5428 if (N1C && N1C->isNullValue()) 5429 return N0; 5430 5431 if (SDValue NewSel = foldBinOpIntoSelect(N)) 5432 return NewSel; 5433 5434 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports 5435 // sext_inreg. 5436 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) { 5437 unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue(); 5438 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits); 5439 if (VT.isVector()) 5440 ExtVT = EVT::getVectorVT(*DAG.getContext(), 5441 ExtVT, VT.getVectorNumElements()); 5442 if ((!LegalOperations || 5443 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT))) 5444 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, 5445 N0.getOperand(0), DAG.getValueType(ExtVT)); 5446 } 5447 5448 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2)) 5449 if (N1C && N0.getOpcode() == ISD::SRA) { 5450 if (ConstantSDNode *N0C1 = isConstOrConstSplat(N0.getOperand(1))) { 5451 SDLoc DL(N); 5452 APInt c1 = N0C1->getAPIntValue(); 5453 APInt c2 = N1C->getAPIntValue(); 5454 zeroExtendToMatch(c1, c2, 1 /* Overflow Bit */); 5455 5456 APInt Sum = c1 + c2; 5457 if (Sum.uge(OpSizeInBits)) 5458 Sum = APInt(OpSizeInBits, OpSizeInBits - 1); 5459 5460 return DAG.getNode( 5461 ISD::SRA, DL, VT, N0.getOperand(0), 5462 DAG.getConstant(Sum.getZExtValue(), DL, N1.getValueType())); 5463 } 5464 } 5465 5466 // fold (sra (shl X, m), (sub result_size, n)) 5467 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for 5468 // result_size - n != m. 5469 // If truncate is free for the target sext(shl) is likely to result in better 5470 // code. 5471 if (N0.getOpcode() == ISD::SHL && N1C) { 5472 // Get the two constanst of the shifts, CN0 = m, CN = n. 5473 const ConstantSDNode *N01C = isConstOrConstSplat(N0.getOperand(1)); 5474 if (N01C) { 5475 LLVMContext &Ctx = *DAG.getContext(); 5476 // Determine what the truncate's result bitsize and type would be. 5477 EVT TruncVT = EVT::getIntegerVT(Ctx, OpSizeInBits - N1C->getZExtValue()); 5478 5479 if (VT.isVector()) 5480 TruncVT = EVT::getVectorVT(Ctx, TruncVT, VT.getVectorNumElements()); 5481 5482 // Determine the residual right-shift amount. 5483 int ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue(); 5484 5485 // If the shift is not a no-op (in which case this should be just a sign 5486 // extend already), the truncated to type is legal, sign_extend is legal 5487 // on that type, and the truncate to that type is both legal and free, 5488 // perform the transform. 5489 if ((ShiftAmt > 0) && 5490 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) && 5491 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) && 5492 TLI.isTruncateFree(VT, TruncVT)) { 5493 5494 SDLoc DL(N); 5495 SDValue Amt = DAG.getConstant(ShiftAmt, DL, 5496 getShiftAmountTy(N0.getOperand(0).getValueType())); 5497 SDValue Shift = DAG.getNode(ISD::SRL, DL, VT, 5498 N0.getOperand(0), Amt); 5499 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, TruncVT, 5500 Shift); 5501 return DAG.getNode(ISD::SIGN_EXTEND, DL, 5502 N->getValueType(0), Trunc); 5503 } 5504 } 5505 } 5506 5507 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))). 5508 if (N1.getOpcode() == ISD::TRUNCATE && 5509 N1.getOperand(0).getOpcode() == ISD::AND) { 5510 if (SDValue NewOp1 = distributeTruncateThroughAnd(N1.getNode())) 5511 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0, NewOp1); 5512 } 5513 5514 // fold (sra (trunc (srl x, c1)), c2) -> (trunc (sra x, c1 + c2)) 5515 // if c1 is equal to the number of bits the trunc removes 5516 if (N0.getOpcode() == ISD::TRUNCATE && 5517 (N0.getOperand(0).getOpcode() == ISD::SRL || 5518 N0.getOperand(0).getOpcode() == ISD::SRA) && 5519 N0.getOperand(0).hasOneUse() && 5520 N0.getOperand(0).getOperand(1).hasOneUse() && 5521 N1C) { 5522 SDValue N0Op0 = N0.getOperand(0); 5523 if (ConstantSDNode *LargeShift = isConstOrConstSplat(N0Op0.getOperand(1))) { 5524 unsigned LargeShiftVal = LargeShift->getZExtValue(); 5525 EVT LargeVT = N0Op0.getValueType(); 5526 5527 if (LargeVT.getScalarSizeInBits() - OpSizeInBits == LargeShiftVal) { 5528 SDLoc DL(N); 5529 SDValue Amt = 5530 DAG.getConstant(LargeShiftVal + N1C->getZExtValue(), DL, 5531 getShiftAmountTy(N0Op0.getOperand(0).getValueType())); 5532 SDValue SRA = DAG.getNode(ISD::SRA, DL, LargeVT, 5533 N0Op0.getOperand(0), Amt); 5534 return DAG.getNode(ISD::TRUNCATE, DL, VT, SRA); 5535 } 5536 } 5537 } 5538 5539 // Simplify, based on bits shifted out of the LHS. 5540 if (N1C && SimplifyDemandedBits(SDValue(N, 0))) 5541 return SDValue(N, 0); 5542 5543 5544 // If the sign bit is known to be zero, switch this to a SRL. 5545 if (DAG.SignBitIsZero(N0)) 5546 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, N1); 5547 5548 if (N1C && !N1C->isOpaque()) 5549 if (SDValue NewSRA = visitShiftByConstant(N, N1C)) 5550 return NewSRA; 5551 5552 return SDValue(); 5553 } 5554 5555 SDValue DAGCombiner::visitSRL(SDNode *N) { 5556 SDValue N0 = N->getOperand(0); 5557 SDValue N1 = N->getOperand(1); 5558 EVT VT = N0.getValueType(); 5559 unsigned OpSizeInBits = VT.getScalarSizeInBits(); 5560 5561 // fold vector ops 5562 if (VT.isVector()) 5563 if (SDValue FoldedVOp = SimplifyVBinOp(N)) 5564 return FoldedVOp; 5565 5566 ConstantSDNode *N1C = isConstOrConstSplat(N1); 5567 5568 // fold (srl c1, c2) -> c1 >>u c2 5569 ConstantSDNode *N0C = getAsNonOpaqueConstant(N0); 5570 if (N0C && N1C && !N1C->isOpaque()) 5571 return DAG.FoldConstantArithmetic(ISD::SRL, SDLoc(N), VT, N0C, N1C); 5572 // fold (srl 0, x) -> 0 5573 if (isNullConstant(N0)) 5574 return N0; 5575 // fold (srl x, c >= size(x)) -> undef 5576 if (N1C && N1C->getAPIntValue().uge(OpSizeInBits)) 5577 return DAG.getUNDEF(VT); 5578 // fold (srl x, 0) -> x 5579 if (N1C && N1C->isNullValue()) 5580 return N0; 5581 5582 if (SDValue NewSel = foldBinOpIntoSelect(N)) 5583 return NewSel; 5584 5585 // if (srl x, c) is known to be zero, return 0 5586 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0), 5587 APInt::getAllOnesValue(OpSizeInBits))) 5588 return DAG.getConstant(0, SDLoc(N), VT); 5589 5590 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2)) 5591 if (N1C && N0.getOpcode() == ISD::SRL) { 5592 if (ConstantSDNode *N0C1 = isConstOrConstSplat(N0.getOperand(1))) { 5593 SDLoc DL(N); 5594 APInt c1 = N0C1->getAPIntValue(); 5595 APInt c2 = N1C->getAPIntValue(); 5596 zeroExtendToMatch(c1, c2, 1 /* Overflow Bit */); 5597 5598 APInt Sum = c1 + c2; 5599 if (Sum.uge(OpSizeInBits)) 5600 return DAG.getConstant(0, DL, VT); 5601 5602 return DAG.getNode( 5603 ISD::SRL, DL, VT, N0.getOperand(0), 5604 DAG.getConstant(Sum.getZExtValue(), DL, N1.getValueType())); 5605 } 5606 } 5607 5608 // fold (srl (trunc (srl x, c1)), c2) -> 0 or (trunc (srl x, (add c1, c2))) 5609 if (N1C && N0.getOpcode() == ISD::TRUNCATE && 5610 N0.getOperand(0).getOpcode() == ISD::SRL && 5611 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) { 5612 uint64_t c1 = 5613 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue(); 5614 uint64_t c2 = N1C->getZExtValue(); 5615 EVT InnerShiftVT = N0.getOperand(0).getValueType(); 5616 EVT ShiftCountVT = N0.getOperand(0)->getOperand(1).getValueType(); 5617 uint64_t InnerShiftSize = InnerShiftVT.getScalarSizeInBits(); 5618 // This is only valid if the OpSizeInBits + c1 = size of inner shift. 5619 if (c1 + OpSizeInBits == InnerShiftSize) { 5620 SDLoc DL(N0); 5621 if (c1 + c2 >= InnerShiftSize) 5622 return DAG.getConstant(0, DL, VT); 5623 return DAG.getNode(ISD::TRUNCATE, DL, VT, 5624 DAG.getNode(ISD::SRL, DL, InnerShiftVT, 5625 N0.getOperand(0)->getOperand(0), 5626 DAG.getConstant(c1 + c2, DL, 5627 ShiftCountVT))); 5628 } 5629 } 5630 5631 // fold (srl (shl x, c), c) -> (and x, cst2) 5632 if (N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 && 5633 isConstantOrConstantVector(N1, /* NoOpaques */ true)) { 5634 SDLoc DL(N); 5635 SDValue Mask = 5636 DAG.getNode(ISD::SRL, DL, VT, DAG.getAllOnesConstant(DL, VT), N1); 5637 AddToWorklist(Mask.getNode()); 5638 return DAG.getNode(ISD::AND, DL, VT, N0.getOperand(0), Mask); 5639 } 5640 5641 // fold (srl (anyextend x), c) -> (and (anyextend (srl x, c)), mask) 5642 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) { 5643 // Shifting in all undef bits? 5644 EVT SmallVT = N0.getOperand(0).getValueType(); 5645 unsigned BitSize = SmallVT.getScalarSizeInBits(); 5646 if (N1C->getZExtValue() >= BitSize) 5647 return DAG.getUNDEF(VT); 5648 5649 if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) { 5650 uint64_t ShiftAmt = N1C->getZExtValue(); 5651 SDLoc DL0(N0); 5652 SDValue SmallShift = DAG.getNode(ISD::SRL, DL0, SmallVT, 5653 N0.getOperand(0), 5654 DAG.getConstant(ShiftAmt, DL0, 5655 getShiftAmountTy(SmallVT))); 5656 AddToWorklist(SmallShift.getNode()); 5657 APInt Mask = APInt::getAllOnesValue(OpSizeInBits).lshr(ShiftAmt); 5658 SDLoc DL(N); 5659 return DAG.getNode(ISD::AND, DL, VT, 5660 DAG.getNode(ISD::ANY_EXTEND, DL, VT, SmallShift), 5661 DAG.getConstant(Mask, DL, VT)); 5662 } 5663 } 5664 5665 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign 5666 // bit, which is unmodified by sra. 5667 if (N1C && N1C->getZExtValue() + 1 == OpSizeInBits) { 5668 if (N0.getOpcode() == ISD::SRA) 5669 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0), N1); 5670 } 5671 5672 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit). 5673 if (N1C && N0.getOpcode() == ISD::CTLZ && 5674 N1C->getAPIntValue() == Log2_32(OpSizeInBits)) { 5675 APInt KnownZero, KnownOne; 5676 DAG.computeKnownBits(N0.getOperand(0), KnownZero, KnownOne); 5677 5678 // If any of the input bits are KnownOne, then the input couldn't be all 5679 // zeros, thus the result of the srl will always be zero. 5680 if (KnownOne.getBoolValue()) return DAG.getConstant(0, SDLoc(N0), VT); 5681 5682 // If all of the bits input the to ctlz node are known to be zero, then 5683 // the result of the ctlz is "32" and the result of the shift is one. 5684 APInt UnknownBits = ~KnownZero; 5685 if (UnknownBits == 0) return DAG.getConstant(1, SDLoc(N0), VT); 5686 5687 // Otherwise, check to see if there is exactly one bit input to the ctlz. 5688 if ((UnknownBits & (UnknownBits - 1)) == 0) { 5689 // Okay, we know that only that the single bit specified by UnknownBits 5690 // could be set on input to the CTLZ node. If this bit is set, the SRL 5691 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair 5692 // to an SRL/XOR pair, which is likely to simplify more. 5693 unsigned ShAmt = UnknownBits.countTrailingZeros(); 5694 SDValue Op = N0.getOperand(0); 5695 5696 if (ShAmt) { 5697 SDLoc DL(N0); 5698 Op = DAG.getNode(ISD::SRL, DL, VT, Op, 5699 DAG.getConstant(ShAmt, DL, 5700 getShiftAmountTy(Op.getValueType()))); 5701 AddToWorklist(Op.getNode()); 5702 } 5703 5704 SDLoc DL(N); 5705 return DAG.getNode(ISD::XOR, DL, VT, 5706 Op, DAG.getConstant(1, DL, VT)); 5707 } 5708 } 5709 5710 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))). 5711 if (N1.getOpcode() == ISD::TRUNCATE && 5712 N1.getOperand(0).getOpcode() == ISD::AND) { 5713 if (SDValue NewOp1 = distributeTruncateThroughAnd(N1.getNode())) 5714 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, NewOp1); 5715 } 5716 5717 // fold operands of srl based on knowledge that the low bits are not 5718 // demanded. 5719 if (N1C && SimplifyDemandedBits(SDValue(N, 0))) 5720 return SDValue(N, 0); 5721 5722 if (N1C && !N1C->isOpaque()) 5723 if (SDValue NewSRL = visitShiftByConstant(N, N1C)) 5724 return NewSRL; 5725 5726 // Attempt to convert a srl of a load into a narrower zero-extending load. 5727 if (SDValue NarrowLoad = ReduceLoadWidth(N)) 5728 return NarrowLoad; 5729 5730 // Here is a common situation. We want to optimize: 5731 // 5732 // %a = ... 5733 // %b = and i32 %a, 2 5734 // %c = srl i32 %b, 1 5735 // brcond i32 %c ... 5736 // 5737 // into 5738 // 5739 // %a = ... 5740 // %b = and %a, 2 5741 // %c = setcc eq %b, 0 5742 // brcond %c ... 5743 // 5744 // However when after the source operand of SRL is optimized into AND, the SRL 5745 // itself may not be optimized further. Look for it and add the BRCOND into 5746 // the worklist. 5747 if (N->hasOneUse()) { 5748 SDNode *Use = *N->use_begin(); 5749 if (Use->getOpcode() == ISD::BRCOND) 5750 AddToWorklist(Use); 5751 else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) { 5752 // Also look pass the truncate. 5753 Use = *Use->use_begin(); 5754 if (Use->getOpcode() == ISD::BRCOND) 5755 AddToWorklist(Use); 5756 } 5757 } 5758 5759 return SDValue(); 5760 } 5761 5762 SDValue DAGCombiner::visitABS(SDNode *N) { 5763 SDValue N0 = N->getOperand(0); 5764 EVT VT = N->getValueType(0); 5765 5766 // fold (abs c1) -> c2 5767 if (DAG.isConstantIntBuildVectorOrConstantInt(N0)) 5768 return DAG.getNode(ISD::ABS, SDLoc(N), VT, N0); 5769 // fold (abs (abs x)) -> (abs x) 5770 if (N0.getOpcode() == ISD::ABS) 5771 return N0; 5772 // fold (abs x) -> x iff not-negative 5773 if (DAG.SignBitIsZero(N0)) 5774 return N0; 5775 return SDValue(); 5776 } 5777 5778 SDValue DAGCombiner::visitBSWAP(SDNode *N) { 5779 SDValue N0 = N->getOperand(0); 5780 EVT VT = N->getValueType(0); 5781 5782 // fold (bswap c1) -> c2 5783 if (DAG.isConstantIntBuildVectorOrConstantInt(N0)) 5784 return DAG.getNode(ISD::BSWAP, SDLoc(N), VT, N0); 5785 // fold (bswap (bswap x)) -> x 5786 if (N0.getOpcode() == ISD::BSWAP) 5787 return N0->getOperand(0); 5788 return SDValue(); 5789 } 5790 5791 SDValue DAGCombiner::visitBITREVERSE(SDNode *N) { 5792 SDValue N0 = N->getOperand(0); 5793 EVT VT = N->getValueType(0); 5794 5795 // fold (bitreverse c1) -> c2 5796 if (DAG.isConstantIntBuildVectorOrConstantInt(N0)) 5797 return DAG.getNode(ISD::BITREVERSE, SDLoc(N), VT, N0); 5798 // fold (bitreverse (bitreverse x)) -> x 5799 if (N0.getOpcode() == ISD::BITREVERSE) 5800 return N0.getOperand(0); 5801 return SDValue(); 5802 } 5803 5804 SDValue DAGCombiner::visitCTLZ(SDNode *N) { 5805 SDValue N0 = N->getOperand(0); 5806 EVT VT = N->getValueType(0); 5807 5808 // fold (ctlz c1) -> c2 5809 if (DAG.isConstantIntBuildVectorOrConstantInt(N0)) 5810 return DAG.getNode(ISD::CTLZ, SDLoc(N), VT, N0); 5811 return SDValue(); 5812 } 5813 5814 SDValue DAGCombiner::visitCTLZ_ZERO_UNDEF(SDNode *N) { 5815 SDValue N0 = N->getOperand(0); 5816 EVT VT = N->getValueType(0); 5817 5818 // fold (ctlz_zero_undef c1) -> c2 5819 if (DAG.isConstantIntBuildVectorOrConstantInt(N0)) 5820 return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SDLoc(N), VT, N0); 5821 return SDValue(); 5822 } 5823 5824 SDValue DAGCombiner::visitCTTZ(SDNode *N) { 5825 SDValue N0 = N->getOperand(0); 5826 EVT VT = N->getValueType(0); 5827 5828 // fold (cttz c1) -> c2 5829 if (DAG.isConstantIntBuildVectorOrConstantInt(N0)) 5830 return DAG.getNode(ISD::CTTZ, SDLoc(N), VT, N0); 5831 return SDValue(); 5832 } 5833 5834 SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) { 5835 SDValue N0 = N->getOperand(0); 5836 EVT VT = N->getValueType(0); 5837 5838 // fold (cttz_zero_undef c1) -> c2 5839 if (DAG.isConstantIntBuildVectorOrConstantInt(N0)) 5840 return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, SDLoc(N), VT, N0); 5841 return SDValue(); 5842 } 5843 5844 SDValue DAGCombiner::visitCTPOP(SDNode *N) { 5845 SDValue N0 = N->getOperand(0); 5846 EVT VT = N->getValueType(0); 5847 5848 // fold (ctpop c1) -> c2 5849 if (DAG.isConstantIntBuildVectorOrConstantInt(N0)) 5850 return DAG.getNode(ISD::CTPOP, SDLoc(N), VT, N0); 5851 return SDValue(); 5852 } 5853 5854 5855 /// \brief Generate Min/Max node 5856 static SDValue combineMinNumMaxNum(const SDLoc &DL, EVT VT, SDValue LHS, 5857 SDValue RHS, SDValue True, SDValue False, 5858 ISD::CondCode CC, const TargetLowering &TLI, 5859 SelectionDAG &DAG) { 5860 if (!(LHS == True && RHS == False) && !(LHS == False && RHS == True)) 5861 return SDValue(); 5862 5863 switch (CC) { 5864 case ISD::SETOLT: 5865 case ISD::SETOLE: 5866 case ISD::SETLT: 5867 case ISD::SETLE: 5868 case ISD::SETULT: 5869 case ISD::SETULE: { 5870 unsigned Opcode = (LHS == True) ? ISD::FMINNUM : ISD::FMAXNUM; 5871 if (TLI.isOperationLegal(Opcode, VT)) 5872 return DAG.getNode(Opcode, DL, VT, LHS, RHS); 5873 return SDValue(); 5874 } 5875 case ISD::SETOGT: 5876 case ISD::SETOGE: 5877 case ISD::SETGT: 5878 case ISD::SETGE: 5879 case ISD::SETUGT: 5880 case ISD::SETUGE: { 5881 unsigned Opcode = (LHS == True) ? ISD::FMAXNUM : ISD::FMINNUM; 5882 if (TLI.isOperationLegal(Opcode, VT)) 5883 return DAG.getNode(Opcode, DL, VT, LHS, RHS); 5884 return SDValue(); 5885 } 5886 default: 5887 return SDValue(); 5888 } 5889 } 5890 5891 SDValue DAGCombiner::foldSelectOfConstants(SDNode *N) { 5892 SDValue Cond = N->getOperand(0); 5893 SDValue N1 = N->getOperand(1); 5894 SDValue N2 = N->getOperand(2); 5895 EVT VT = N->getValueType(0); 5896 EVT CondVT = Cond.getValueType(); 5897 SDLoc DL(N); 5898 5899 if (!VT.isInteger()) 5900 return SDValue(); 5901 5902 auto *C1 = dyn_cast<ConstantSDNode>(N1); 5903 auto *C2 = dyn_cast<ConstantSDNode>(N2); 5904 if (!C1 || !C2) 5905 return SDValue(); 5906 5907 // Only do this before legalization to avoid conflicting with target-specific 5908 // transforms in the other direction (create a select from a zext/sext). There 5909 // is also a target-independent combine here in DAGCombiner in the other 5910 // direction for (select Cond, -1, 0) when the condition is not i1. 5911 if (CondVT == MVT::i1 && !LegalOperations) { 5912 if (C1->isNullValue() && C2->isOne()) { 5913 // select Cond, 0, 1 --> zext (!Cond) 5914 SDValue NotCond = DAG.getNOT(DL, Cond, MVT::i1); 5915 if (VT != MVT::i1) 5916 NotCond = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, NotCond); 5917 return NotCond; 5918 } 5919 if (C1->isNullValue() && C2->isAllOnesValue()) { 5920 // select Cond, 0, -1 --> sext (!Cond) 5921 SDValue NotCond = DAG.getNOT(DL, Cond, MVT::i1); 5922 if (VT != MVT::i1) 5923 NotCond = DAG.getNode(ISD::SIGN_EXTEND, DL, VT, NotCond); 5924 return NotCond; 5925 } 5926 if (C1->isOne() && C2->isNullValue()) { 5927 // select Cond, 1, 0 --> zext (Cond) 5928 if (VT != MVT::i1) 5929 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Cond); 5930 return Cond; 5931 } 5932 if (C1->isAllOnesValue() && C2->isNullValue()) { 5933 // select Cond, -1, 0 --> sext (Cond) 5934 if (VT != MVT::i1) 5935 Cond = DAG.getNode(ISD::SIGN_EXTEND, DL, VT, Cond); 5936 return Cond; 5937 } 5938 5939 // For any constants that differ by 1, we can transform the select into an 5940 // extend and add. Use a target hook because some targets may prefer to 5941 // transform in the other direction. 5942 if (TLI.convertSelectOfConstantsToMath()) { 5943 if (C1->getAPIntValue() - 1 == C2->getAPIntValue()) { 5944 // select Cond, C1, C1-1 --> add (zext Cond), C1-1 5945 if (VT != MVT::i1) 5946 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Cond); 5947 return DAG.getNode(ISD::ADD, DL, VT, Cond, N2); 5948 } 5949 if (C1->getAPIntValue() + 1 == C2->getAPIntValue()) { 5950 // select Cond, C1, C1+1 --> add (sext Cond), C1+1 5951 if (VT != MVT::i1) 5952 Cond = DAG.getNode(ISD::SIGN_EXTEND, DL, VT, Cond); 5953 return DAG.getNode(ISD::ADD, DL, VT, Cond, N2); 5954 } 5955 } 5956 5957 return SDValue(); 5958 } 5959 5960 // fold (select Cond, 0, 1) -> (xor Cond, 1) 5961 // We can't do this reliably if integer based booleans have different contents 5962 // to floating point based booleans. This is because we can't tell whether we 5963 // have an integer-based boolean or a floating-point-based boolean unless we 5964 // can find the SETCC that produced it and inspect its operands. This is 5965 // fairly easy if C is the SETCC node, but it can potentially be 5966 // undiscoverable (or not reasonably discoverable). For example, it could be 5967 // in another basic block or it could require searching a complicated 5968 // expression. 5969 if (CondVT.isInteger() && 5970 TLI.getBooleanContents(false, true) == 5971 TargetLowering::ZeroOrOneBooleanContent && 5972 TLI.getBooleanContents(false, false) == 5973 TargetLowering::ZeroOrOneBooleanContent && 5974 C1->isNullValue() && C2->isOne()) { 5975 SDValue NotCond = 5976 DAG.getNode(ISD::XOR, DL, CondVT, Cond, DAG.getConstant(1, DL, CondVT)); 5977 if (VT.bitsEq(CondVT)) 5978 return NotCond; 5979 return DAG.getZExtOrTrunc(NotCond, DL, VT); 5980 } 5981 5982 return SDValue(); 5983 } 5984 5985 SDValue DAGCombiner::visitSELECT(SDNode *N) { 5986 SDValue N0 = N->getOperand(0); 5987 SDValue N1 = N->getOperand(1); 5988 SDValue N2 = N->getOperand(2); 5989 EVT VT = N->getValueType(0); 5990 EVT VT0 = N0.getValueType(); 5991 5992 // fold (select C, X, X) -> X 5993 if (N1 == N2) 5994 return N1; 5995 if (const ConstantSDNode *N0C = dyn_cast<const ConstantSDNode>(N0)) { 5996 // fold (select true, X, Y) -> X 5997 // fold (select false, X, Y) -> Y 5998 return !N0C->isNullValue() ? N1 : N2; 5999 } 6000 // fold (select X, X, Y) -> (or X, Y) 6001 // fold (select X, 1, Y) -> (or C, Y) 6002 if (VT == VT0 && VT == MVT::i1 && (N0 == N1 || isOneConstant(N1))) 6003 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2); 6004 6005 if (SDValue V = foldSelectOfConstants(N)) 6006 return V; 6007 6008 // fold (select C, 0, X) -> (and (not C), X) 6009 if (VT == VT0 && VT == MVT::i1 && isNullConstant(N1)) { 6010 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT); 6011 AddToWorklist(NOTNode.getNode()); 6012 return DAG.getNode(ISD::AND, SDLoc(N), VT, NOTNode, N2); 6013 } 6014 // fold (select C, X, 1) -> (or (not C), X) 6015 if (VT == VT0 && VT == MVT::i1 && isOneConstant(N2)) { 6016 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT); 6017 AddToWorklist(NOTNode.getNode()); 6018 return DAG.getNode(ISD::OR, SDLoc(N), VT, NOTNode, N1); 6019 } 6020 // fold (select X, Y, X) -> (and X, Y) 6021 // fold (select X, Y, 0) -> (and X, Y) 6022 if (VT == VT0 && VT == MVT::i1 && (N0 == N2 || isNullConstant(N2))) 6023 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1); 6024 6025 // If we can fold this based on the true/false value, do so. 6026 if (SimplifySelectOps(N, N1, N2)) 6027 return SDValue(N, 0); // Don't revisit N. 6028 6029 if (VT0 == MVT::i1) { 6030 // The code in this block deals with the following 2 equivalences: 6031 // select(C0|C1, x, y) <=> select(C0, x, select(C1, x, y)) 6032 // select(C0&C1, x, y) <=> select(C0, select(C1, x, y), y) 6033 // The target can specify its preferred form with the 6034 // shouldNormalizeToSelectSequence() callback. However we always transform 6035 // to the right anyway if we find the inner select exists in the DAG anyway 6036 // and we always transform to the left side if we know that we can further 6037 // optimize the combination of the conditions. 6038 bool normalizeToSequence 6039 = TLI.shouldNormalizeToSelectSequence(*DAG.getContext(), VT); 6040 // select (and Cond0, Cond1), X, Y 6041 // -> select Cond0, (select Cond1, X, Y), Y 6042 if (N0->getOpcode() == ISD::AND && N0->hasOneUse()) { 6043 SDValue Cond0 = N0->getOperand(0); 6044 SDValue Cond1 = N0->getOperand(1); 6045 SDValue InnerSelect = DAG.getNode(ISD::SELECT, SDLoc(N), 6046 N1.getValueType(), Cond1, N1, N2); 6047 if (normalizeToSequence || !InnerSelect.use_empty()) 6048 return DAG.getNode(ISD::SELECT, SDLoc(N), N1.getValueType(), Cond0, 6049 InnerSelect, N2); 6050 } 6051 // select (or Cond0, Cond1), X, Y -> select Cond0, X, (select Cond1, X, Y) 6052 if (N0->getOpcode() == ISD::OR && N0->hasOneUse()) { 6053 SDValue Cond0 = N0->getOperand(0); 6054 SDValue Cond1 = N0->getOperand(1); 6055 SDValue InnerSelect = DAG.getNode(ISD::SELECT, SDLoc(N), 6056 N1.getValueType(), Cond1, N1, N2); 6057 if (normalizeToSequence || !InnerSelect.use_empty()) 6058 return DAG.getNode(ISD::SELECT, SDLoc(N), N1.getValueType(), Cond0, N1, 6059 InnerSelect); 6060 } 6061 6062 // select Cond0, (select Cond1, X, Y), Y -> select (and Cond0, Cond1), X, Y 6063 if (N1->getOpcode() == ISD::SELECT && N1->hasOneUse()) { 6064 SDValue N1_0 = N1->getOperand(0); 6065 SDValue N1_1 = N1->getOperand(1); 6066 SDValue N1_2 = N1->getOperand(2); 6067 if (N1_2 == N2 && N0.getValueType() == N1_0.getValueType()) { 6068 // Create the actual and node if we can generate good code for it. 6069 if (!normalizeToSequence) { 6070 SDValue And = DAG.getNode(ISD::AND, SDLoc(N), N0.getValueType(), 6071 N0, N1_0); 6072 return DAG.getNode(ISD::SELECT, SDLoc(N), N1.getValueType(), And, 6073 N1_1, N2); 6074 } 6075 // Otherwise see if we can optimize the "and" to a better pattern. 6076 if (SDValue Combined = visitANDLike(N0, N1_0, N)) 6077 return DAG.getNode(ISD::SELECT, SDLoc(N), N1.getValueType(), Combined, 6078 N1_1, N2); 6079 } 6080 } 6081 // select Cond0, X, (select Cond1, X, Y) -> select (or Cond0, Cond1), X, Y 6082 if (N2->getOpcode() == ISD::SELECT && N2->hasOneUse()) { 6083 SDValue N2_0 = N2->getOperand(0); 6084 SDValue N2_1 = N2->getOperand(1); 6085 SDValue N2_2 = N2->getOperand(2); 6086 if (N2_1 == N1 && N0.getValueType() == N2_0.getValueType()) { 6087 // Create the actual or node if we can generate good code for it. 6088 if (!normalizeToSequence) { 6089 SDValue Or = DAG.getNode(ISD::OR, SDLoc(N), N0.getValueType(), 6090 N0, N2_0); 6091 return DAG.getNode(ISD::SELECT, SDLoc(N), N1.getValueType(), Or, 6092 N1, N2_2); 6093 } 6094 // Otherwise see if we can optimize to a better pattern. 6095 if (SDValue Combined = visitORLike(N0, N2_0, N)) 6096 return DAG.getNode(ISD::SELECT, SDLoc(N), N1.getValueType(), Combined, 6097 N1, N2_2); 6098 } 6099 } 6100 } 6101 6102 // select (xor Cond, 1), X, Y -> select Cond, Y, X 6103 if (VT0 == MVT::i1) { 6104 if (N0->getOpcode() == ISD::XOR) { 6105 if (auto *C = dyn_cast<ConstantSDNode>(N0->getOperand(1))) { 6106 SDValue Cond0 = N0->getOperand(0); 6107 if (C->isOne()) 6108 return DAG.getNode(ISD::SELECT, SDLoc(N), N1.getValueType(), 6109 Cond0, N2, N1); 6110 } 6111 } 6112 } 6113 6114 // fold selects based on a setcc into other things, such as min/max/abs 6115 if (N0.getOpcode() == ISD::SETCC) { 6116 // select x, y (fcmp lt x, y) -> fminnum x, y 6117 // select x, y (fcmp gt x, y) -> fmaxnum x, y 6118 // 6119 // This is OK if we don't care about what happens if either operand is a 6120 // NaN. 6121 // 6122 6123 // FIXME: Instead of testing for UnsafeFPMath, this should be checking for 6124 // no signed zeros as well as no nans. 6125 const TargetOptions &Options = DAG.getTarget().Options; 6126 if (Options.UnsafeFPMath && 6127 VT.isFloatingPoint() && N0.hasOneUse() && 6128 DAG.isKnownNeverNaN(N1) && DAG.isKnownNeverNaN(N2)) { 6129 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get(); 6130 6131 if (SDValue FMinMax = combineMinNumMaxNum(SDLoc(N), VT, N0.getOperand(0), 6132 N0.getOperand(1), N1, N2, CC, 6133 TLI, DAG)) 6134 return FMinMax; 6135 } 6136 6137 if ((!LegalOperations && 6138 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT)) || 6139 TLI.isOperationLegal(ISD::SELECT_CC, VT)) 6140 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, 6141 N0.getOperand(0), N0.getOperand(1), 6142 N1, N2, N0.getOperand(2)); 6143 return SimplifySelect(SDLoc(N), N0, N1, N2); 6144 } 6145 6146 return SDValue(); 6147 } 6148 6149 static 6150 std::pair<SDValue, SDValue> SplitVSETCC(const SDNode *N, SelectionDAG &DAG) { 6151 SDLoc DL(N); 6152 EVT LoVT, HiVT; 6153 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0)); 6154 6155 // Split the inputs. 6156 SDValue Lo, Hi, LL, LH, RL, RH; 6157 std::tie(LL, LH) = DAG.SplitVectorOperand(N, 0); 6158 std::tie(RL, RH) = DAG.SplitVectorOperand(N, 1); 6159 6160 Lo = DAG.getNode(N->getOpcode(), DL, LoVT, LL, RL, N->getOperand(2)); 6161 Hi = DAG.getNode(N->getOpcode(), DL, HiVT, LH, RH, N->getOperand(2)); 6162 6163 return std::make_pair(Lo, Hi); 6164 } 6165 6166 // This function assumes all the vselect's arguments are CONCAT_VECTOR 6167 // nodes and that the condition is a BV of ConstantSDNodes (or undefs). 6168 static SDValue ConvertSelectToConcatVector(SDNode *N, SelectionDAG &DAG) { 6169 SDLoc DL(N); 6170 SDValue Cond = N->getOperand(0); 6171 SDValue LHS = N->getOperand(1); 6172 SDValue RHS = N->getOperand(2); 6173 EVT VT = N->getValueType(0); 6174 int NumElems = VT.getVectorNumElements(); 6175 assert(LHS.getOpcode() == ISD::CONCAT_VECTORS && 6176 RHS.getOpcode() == ISD::CONCAT_VECTORS && 6177 Cond.getOpcode() == ISD::BUILD_VECTOR); 6178 6179 // CONCAT_VECTOR can take an arbitrary number of arguments. We only care about 6180 // binary ones here. 6181 if (LHS->getNumOperands() != 2 || RHS->getNumOperands() != 2) 6182 return SDValue(); 6183 6184 // We're sure we have an even number of elements due to the 6185 // concat_vectors we have as arguments to vselect. 6186 // Skip BV elements until we find one that's not an UNDEF 6187 // After we find an UNDEF element, keep looping until we get to half the 6188 // length of the BV and see if all the non-undef nodes are the same. 6189 ConstantSDNode *BottomHalf = nullptr; 6190 for (int i = 0; i < NumElems / 2; ++i) { 6191 if (Cond->getOperand(i)->isUndef()) 6192 continue; 6193 6194 if (BottomHalf == nullptr) 6195 BottomHalf = cast<ConstantSDNode>(Cond.getOperand(i)); 6196 else if (Cond->getOperand(i).getNode() != BottomHalf) 6197 return SDValue(); 6198 } 6199 6200 // Do the same for the second half of the BuildVector 6201 ConstantSDNode *TopHalf = nullptr; 6202 for (int i = NumElems / 2; i < NumElems; ++i) { 6203 if (Cond->getOperand(i)->isUndef()) 6204 continue; 6205 6206 if (TopHalf == nullptr) 6207 TopHalf = cast<ConstantSDNode>(Cond.getOperand(i)); 6208 else if (Cond->getOperand(i).getNode() != TopHalf) 6209 return SDValue(); 6210 } 6211 6212 assert(TopHalf && BottomHalf && 6213 "One half of the selector was all UNDEFs and the other was all the " 6214 "same value. This should have been addressed before this function."); 6215 return DAG.getNode( 6216 ISD::CONCAT_VECTORS, DL, VT, 6217 BottomHalf->isNullValue() ? RHS->getOperand(0) : LHS->getOperand(0), 6218 TopHalf->isNullValue() ? RHS->getOperand(1) : LHS->getOperand(1)); 6219 } 6220 6221 SDValue DAGCombiner::visitMSCATTER(SDNode *N) { 6222 6223 if (Level >= AfterLegalizeTypes) 6224 return SDValue(); 6225 6226 MaskedScatterSDNode *MSC = cast<MaskedScatterSDNode>(N); 6227 SDValue Mask = MSC->getMask(); 6228 SDValue Data = MSC->getValue(); 6229 SDLoc DL(N); 6230 6231 // If the MSCATTER data type requires splitting and the mask is provided by a 6232 // SETCC, then split both nodes and its operands before legalization. This 6233 // prevents the type legalizer from unrolling SETCC into scalar comparisons 6234 // and enables future optimizations (e.g. min/max pattern matching on X86). 6235 if (Mask.getOpcode() != ISD::SETCC) 6236 return SDValue(); 6237 6238 // Check if any splitting is required. 6239 if (TLI.getTypeAction(*DAG.getContext(), Data.getValueType()) != 6240 TargetLowering::TypeSplitVector) 6241 return SDValue(); 6242 SDValue MaskLo, MaskHi, Lo, Hi; 6243 std::tie(MaskLo, MaskHi) = SplitVSETCC(Mask.getNode(), DAG); 6244 6245 EVT LoVT, HiVT; 6246 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(MSC->getValueType(0)); 6247 6248 SDValue Chain = MSC->getChain(); 6249 6250 EVT MemoryVT = MSC->getMemoryVT(); 6251 unsigned Alignment = MSC->getOriginalAlignment(); 6252 6253 EVT LoMemVT, HiMemVT; 6254 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemoryVT); 6255 6256 SDValue DataLo, DataHi; 6257 std::tie(DataLo, DataHi) = DAG.SplitVector(Data, DL); 6258 6259 SDValue BasePtr = MSC->getBasePtr(); 6260 SDValue IndexLo, IndexHi; 6261 std::tie(IndexLo, IndexHi) = DAG.SplitVector(MSC->getIndex(), DL); 6262 6263 MachineMemOperand *MMO = DAG.getMachineFunction(). 6264 getMachineMemOperand(MSC->getPointerInfo(), 6265 MachineMemOperand::MOStore, LoMemVT.getStoreSize(), 6266 Alignment, MSC->getAAInfo(), MSC->getRanges()); 6267 6268 SDValue OpsLo[] = { Chain, DataLo, MaskLo, BasePtr, IndexLo }; 6269 Lo = DAG.getMaskedScatter(DAG.getVTList(MVT::Other), DataLo.getValueType(), 6270 DL, OpsLo, MMO); 6271 6272 SDValue OpsHi[] = {Chain, DataHi, MaskHi, BasePtr, IndexHi}; 6273 Hi = DAG.getMaskedScatter(DAG.getVTList(MVT::Other), DataHi.getValueType(), 6274 DL, OpsHi, MMO); 6275 6276 AddToWorklist(Lo.getNode()); 6277 AddToWorklist(Hi.getNode()); 6278 6279 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Lo, Hi); 6280 } 6281 6282 SDValue DAGCombiner::visitMSTORE(SDNode *N) { 6283 6284 if (Level >= AfterLegalizeTypes) 6285 return SDValue(); 6286 6287 MaskedStoreSDNode *MST = dyn_cast<MaskedStoreSDNode>(N); 6288 SDValue Mask = MST->getMask(); 6289 SDValue Data = MST->getValue(); 6290 EVT VT = Data.getValueType(); 6291 SDLoc DL(N); 6292 6293 // If the MSTORE data type requires splitting and the mask is provided by a 6294 // SETCC, then split both nodes and its operands before legalization. This 6295 // prevents the type legalizer from unrolling SETCC into scalar comparisons 6296 // and enables future optimizations (e.g. min/max pattern matching on X86). 6297 if (Mask.getOpcode() == ISD::SETCC) { 6298 6299 // Check if any splitting is required. 6300 if (TLI.getTypeAction(*DAG.getContext(), VT) != 6301 TargetLowering::TypeSplitVector) 6302 return SDValue(); 6303 6304 SDValue MaskLo, MaskHi, Lo, Hi; 6305 std::tie(MaskLo, MaskHi) = SplitVSETCC(Mask.getNode(), DAG); 6306 6307 SDValue Chain = MST->getChain(); 6308 SDValue Ptr = MST->getBasePtr(); 6309 6310 EVT MemoryVT = MST->getMemoryVT(); 6311 unsigned Alignment = MST->getOriginalAlignment(); 6312 6313 // if Alignment is equal to the vector size, 6314 // take the half of it for the second part 6315 unsigned SecondHalfAlignment = 6316 (Alignment == VT.getSizeInBits() / 8) ? Alignment / 2 : Alignment; 6317 6318 EVT LoMemVT, HiMemVT; 6319 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemoryVT); 6320 6321 SDValue DataLo, DataHi; 6322 std::tie(DataLo, DataHi) = DAG.SplitVector(Data, DL); 6323 6324 MachineMemOperand *MMO = DAG.getMachineFunction(). 6325 getMachineMemOperand(MST->getPointerInfo(), 6326 MachineMemOperand::MOStore, LoMemVT.getStoreSize(), 6327 Alignment, MST->getAAInfo(), MST->getRanges()); 6328 6329 Lo = DAG.getMaskedStore(Chain, DL, DataLo, Ptr, MaskLo, LoMemVT, MMO, 6330 MST->isTruncatingStore(), 6331 MST->isCompressingStore()); 6332 6333 Ptr = TLI.IncrementMemoryAddress(Ptr, MaskLo, DL, LoMemVT, DAG, 6334 MST->isCompressingStore()); 6335 6336 MMO = DAG.getMachineFunction(). 6337 getMachineMemOperand(MST->getPointerInfo(), 6338 MachineMemOperand::MOStore, HiMemVT.getStoreSize(), 6339 SecondHalfAlignment, MST->getAAInfo(), 6340 MST->getRanges()); 6341 6342 Hi = DAG.getMaskedStore(Chain, DL, DataHi, Ptr, MaskHi, HiMemVT, MMO, 6343 MST->isTruncatingStore(), 6344 MST->isCompressingStore()); 6345 6346 AddToWorklist(Lo.getNode()); 6347 AddToWorklist(Hi.getNode()); 6348 6349 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Lo, Hi); 6350 } 6351 return SDValue(); 6352 } 6353 6354 SDValue DAGCombiner::visitMGATHER(SDNode *N) { 6355 6356 if (Level >= AfterLegalizeTypes) 6357 return SDValue(); 6358 6359 MaskedGatherSDNode *MGT = dyn_cast<MaskedGatherSDNode>(N); 6360 SDValue Mask = MGT->getMask(); 6361 SDLoc DL(N); 6362 6363 // If the MGATHER result requires splitting and the mask is provided by a 6364 // SETCC, then split both nodes and its operands before legalization. This 6365 // prevents the type legalizer from unrolling SETCC into scalar comparisons 6366 // and enables future optimizations (e.g. min/max pattern matching on X86). 6367 6368 if (Mask.getOpcode() != ISD::SETCC) 6369 return SDValue(); 6370 6371 EVT VT = N->getValueType(0); 6372 6373 // Check if any splitting is required. 6374 if (TLI.getTypeAction(*DAG.getContext(), VT) != 6375 TargetLowering::TypeSplitVector) 6376 return SDValue(); 6377 6378 SDValue MaskLo, MaskHi, Lo, Hi; 6379 std::tie(MaskLo, MaskHi) = SplitVSETCC(Mask.getNode(), DAG); 6380 6381 SDValue Src0 = MGT->getValue(); 6382 SDValue Src0Lo, Src0Hi; 6383 std::tie(Src0Lo, Src0Hi) = DAG.SplitVector(Src0, DL); 6384 6385 EVT LoVT, HiVT; 6386 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT); 6387 6388 SDValue Chain = MGT->getChain(); 6389 EVT MemoryVT = MGT->getMemoryVT(); 6390 unsigned Alignment = MGT->getOriginalAlignment(); 6391 6392 EVT LoMemVT, HiMemVT; 6393 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemoryVT); 6394 6395 SDValue BasePtr = MGT->getBasePtr(); 6396 SDValue Index = MGT->getIndex(); 6397 SDValue IndexLo, IndexHi; 6398 std::tie(IndexLo, IndexHi) = DAG.SplitVector(Index, DL); 6399 6400 MachineMemOperand *MMO = DAG.getMachineFunction(). 6401 getMachineMemOperand(MGT->getPointerInfo(), 6402 MachineMemOperand::MOLoad, LoMemVT.getStoreSize(), 6403 Alignment, MGT->getAAInfo(), MGT->getRanges()); 6404 6405 SDValue OpsLo[] = { Chain, Src0Lo, MaskLo, BasePtr, IndexLo }; 6406 Lo = DAG.getMaskedGather(DAG.getVTList(LoVT, MVT::Other), LoVT, DL, OpsLo, 6407 MMO); 6408 6409 SDValue OpsHi[] = {Chain, Src0Hi, MaskHi, BasePtr, IndexHi}; 6410 Hi = DAG.getMaskedGather(DAG.getVTList(HiVT, MVT::Other), HiVT, DL, OpsHi, 6411 MMO); 6412 6413 AddToWorklist(Lo.getNode()); 6414 AddToWorklist(Hi.getNode()); 6415 6416 // Build a factor node to remember that this load is independent of the 6417 // other one. 6418 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Lo.getValue(1), 6419 Hi.getValue(1)); 6420 6421 // Legalized the chain result - switch anything that used the old chain to 6422 // use the new one. 6423 DAG.ReplaceAllUsesOfValueWith(SDValue(MGT, 1), Chain); 6424 6425 SDValue GatherRes = DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, Lo, Hi); 6426 6427 SDValue RetOps[] = { GatherRes, Chain }; 6428 return DAG.getMergeValues(RetOps, DL); 6429 } 6430 6431 SDValue DAGCombiner::visitMLOAD(SDNode *N) { 6432 6433 if (Level >= AfterLegalizeTypes) 6434 return SDValue(); 6435 6436 MaskedLoadSDNode *MLD = dyn_cast<MaskedLoadSDNode>(N); 6437 SDValue Mask = MLD->getMask(); 6438 SDLoc DL(N); 6439 6440 // If the MLOAD result requires splitting and the mask is provided by a 6441 // SETCC, then split both nodes and its operands before legalization. This 6442 // prevents the type legalizer from unrolling SETCC into scalar comparisons 6443 // and enables future optimizations (e.g. min/max pattern matching on X86). 6444 6445 if (Mask.getOpcode() == ISD::SETCC) { 6446 EVT VT = N->getValueType(0); 6447 6448 // Check if any splitting is required. 6449 if (TLI.getTypeAction(*DAG.getContext(), VT) != 6450 TargetLowering::TypeSplitVector) 6451 return SDValue(); 6452 6453 SDValue MaskLo, MaskHi, Lo, Hi; 6454 std::tie(MaskLo, MaskHi) = SplitVSETCC(Mask.getNode(), DAG); 6455 6456 SDValue Src0 = MLD->getSrc0(); 6457 SDValue Src0Lo, Src0Hi; 6458 std::tie(Src0Lo, Src0Hi) = DAG.SplitVector(Src0, DL); 6459 6460 EVT LoVT, HiVT; 6461 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(MLD->getValueType(0)); 6462 6463 SDValue Chain = MLD->getChain(); 6464 SDValue Ptr = MLD->getBasePtr(); 6465 EVT MemoryVT = MLD->getMemoryVT(); 6466 unsigned Alignment = MLD->getOriginalAlignment(); 6467 6468 // if Alignment is equal to the vector size, 6469 // take the half of it for the second part 6470 unsigned SecondHalfAlignment = 6471 (Alignment == MLD->getValueType(0).getSizeInBits()/8) ? 6472 Alignment/2 : Alignment; 6473 6474 EVT LoMemVT, HiMemVT; 6475 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemoryVT); 6476 6477 MachineMemOperand *MMO = DAG.getMachineFunction(). 6478 getMachineMemOperand(MLD->getPointerInfo(), 6479 MachineMemOperand::MOLoad, LoMemVT.getStoreSize(), 6480 Alignment, MLD->getAAInfo(), MLD->getRanges()); 6481 6482 Lo = DAG.getMaskedLoad(LoVT, DL, Chain, Ptr, MaskLo, Src0Lo, LoMemVT, MMO, 6483 ISD::NON_EXTLOAD, MLD->isExpandingLoad()); 6484 6485 Ptr = TLI.IncrementMemoryAddress(Ptr, MaskLo, DL, LoMemVT, DAG, 6486 MLD->isExpandingLoad()); 6487 6488 MMO = DAG.getMachineFunction(). 6489 getMachineMemOperand(MLD->getPointerInfo(), 6490 MachineMemOperand::MOLoad, HiMemVT.getStoreSize(), 6491 SecondHalfAlignment, MLD->getAAInfo(), MLD->getRanges()); 6492 6493 Hi = DAG.getMaskedLoad(HiVT, DL, Chain, Ptr, MaskHi, Src0Hi, HiMemVT, MMO, 6494 ISD::NON_EXTLOAD, MLD->isExpandingLoad()); 6495 6496 AddToWorklist(Lo.getNode()); 6497 AddToWorklist(Hi.getNode()); 6498 6499 // Build a factor node to remember that this load is independent of the 6500 // other one. 6501 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Lo.getValue(1), 6502 Hi.getValue(1)); 6503 6504 // Legalized the chain result - switch anything that used the old chain to 6505 // use the new one. 6506 DAG.ReplaceAllUsesOfValueWith(SDValue(MLD, 1), Chain); 6507 6508 SDValue LoadRes = DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, Lo, Hi); 6509 6510 SDValue RetOps[] = { LoadRes, Chain }; 6511 return DAG.getMergeValues(RetOps, DL); 6512 } 6513 return SDValue(); 6514 } 6515 6516 SDValue DAGCombiner::visitVSELECT(SDNode *N) { 6517 SDValue N0 = N->getOperand(0); 6518 SDValue N1 = N->getOperand(1); 6519 SDValue N2 = N->getOperand(2); 6520 SDLoc DL(N); 6521 6522 // fold (vselect C, X, X) -> X 6523 if (N1 == N2) 6524 return N1; 6525 6526 // Canonicalize integer abs. 6527 // vselect (setg[te] X, 0), X, -X -> 6528 // vselect (setgt X, -1), X, -X -> 6529 // vselect (setl[te] X, 0), -X, X -> 6530 // Y = sra (X, size(X)-1); xor (add (X, Y), Y) 6531 if (N0.getOpcode() == ISD::SETCC) { 6532 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1); 6533 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get(); 6534 bool isAbs = false; 6535 bool RHSIsAllZeros = ISD::isBuildVectorAllZeros(RHS.getNode()); 6536 6537 if (((RHSIsAllZeros && (CC == ISD::SETGT || CC == ISD::SETGE)) || 6538 (ISD::isBuildVectorAllOnes(RHS.getNode()) && CC == ISD::SETGT)) && 6539 N1 == LHS && N2.getOpcode() == ISD::SUB && N1 == N2.getOperand(1)) 6540 isAbs = ISD::isBuildVectorAllZeros(N2.getOperand(0).getNode()); 6541 else if ((RHSIsAllZeros && (CC == ISD::SETLT || CC == ISD::SETLE)) && 6542 N2 == LHS && N1.getOpcode() == ISD::SUB && N2 == N1.getOperand(1)) 6543 isAbs = ISD::isBuildVectorAllZeros(N1.getOperand(0).getNode()); 6544 6545 if (isAbs) { 6546 EVT VT = LHS.getValueType(); 6547 SDValue Shift = DAG.getNode( 6548 ISD::SRA, DL, VT, LHS, 6549 DAG.getConstant(VT.getScalarSizeInBits() - 1, DL, VT)); 6550 SDValue Add = DAG.getNode(ISD::ADD, DL, VT, LHS, Shift); 6551 AddToWorklist(Shift.getNode()); 6552 AddToWorklist(Add.getNode()); 6553 return DAG.getNode(ISD::XOR, DL, VT, Add, Shift); 6554 } 6555 } 6556 6557 if (SimplifySelectOps(N, N1, N2)) 6558 return SDValue(N, 0); // Don't revisit N. 6559 6560 // Fold (vselect (build_vector all_ones), N1, N2) -> N1 6561 if (ISD::isBuildVectorAllOnes(N0.getNode())) 6562 return N1; 6563 // Fold (vselect (build_vector all_zeros), N1, N2) -> N2 6564 if (ISD::isBuildVectorAllZeros(N0.getNode())) 6565 return N2; 6566 6567 // The ConvertSelectToConcatVector function is assuming both the above 6568 // checks for (vselect (build_vector all{ones,zeros) ...) have been made 6569 // and addressed. 6570 if (N1.getOpcode() == ISD::CONCAT_VECTORS && 6571 N2.getOpcode() == ISD::CONCAT_VECTORS && 6572 ISD::isBuildVectorOfConstantSDNodes(N0.getNode())) { 6573 if (SDValue CV = ConvertSelectToConcatVector(N, DAG)) 6574 return CV; 6575 } 6576 6577 return SDValue(); 6578 } 6579 6580 SDValue DAGCombiner::visitSELECT_CC(SDNode *N) { 6581 SDValue N0 = N->getOperand(0); 6582 SDValue N1 = N->getOperand(1); 6583 SDValue N2 = N->getOperand(2); 6584 SDValue N3 = N->getOperand(3); 6585 SDValue N4 = N->getOperand(4); 6586 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get(); 6587 6588 // fold select_cc lhs, rhs, x, x, cc -> x 6589 if (N2 == N3) 6590 return N2; 6591 6592 // Determine if the condition we're dealing with is constant 6593 if (SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()), N0, N1, 6594 CC, SDLoc(N), false)) { 6595 AddToWorklist(SCC.getNode()); 6596 6597 if (ConstantSDNode *SCCC = dyn_cast<ConstantSDNode>(SCC.getNode())) { 6598 if (!SCCC->isNullValue()) 6599 return N2; // cond always true -> true val 6600 else 6601 return N3; // cond always false -> false val 6602 } else if (SCC->isUndef()) { 6603 // When the condition is UNDEF, just return the first operand. This is 6604 // coherent the DAG creation, no setcc node is created in this case 6605 return N2; 6606 } else if (SCC.getOpcode() == ISD::SETCC) { 6607 // Fold to a simpler select_cc 6608 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N2.getValueType(), 6609 SCC.getOperand(0), SCC.getOperand(1), N2, N3, 6610 SCC.getOperand(2)); 6611 } 6612 } 6613 6614 // If we can fold this based on the true/false value, do so. 6615 if (SimplifySelectOps(N, N2, N3)) 6616 return SDValue(N, 0); // Don't revisit N. 6617 6618 // fold select_cc into other things, such as min/max/abs 6619 return SimplifySelectCC(SDLoc(N), N0, N1, N2, N3, CC); 6620 } 6621 6622 SDValue DAGCombiner::visitSETCC(SDNode *N) { 6623 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1), 6624 cast<CondCodeSDNode>(N->getOperand(2))->get(), 6625 SDLoc(N)); 6626 } 6627 6628 SDValue DAGCombiner::visitSETCCE(SDNode *N) { 6629 SDValue LHS = N->getOperand(0); 6630 SDValue RHS = N->getOperand(1); 6631 SDValue Carry = N->getOperand(2); 6632 SDValue Cond = N->getOperand(3); 6633 6634 // If Carry is false, fold to a regular SETCC. 6635 if (Carry.getOpcode() == ISD::CARRY_FALSE) 6636 return DAG.getNode(ISD::SETCC, SDLoc(N), N->getVTList(), LHS, RHS, Cond); 6637 6638 return SDValue(); 6639 } 6640 6641 /// Try to fold a sext/zext/aext dag node into a ConstantSDNode or 6642 /// a build_vector of constants. 6643 /// This function is called by the DAGCombiner when visiting sext/zext/aext 6644 /// dag nodes (see for example method DAGCombiner::visitSIGN_EXTEND). 6645 /// Vector extends are not folded if operations are legal; this is to 6646 /// avoid introducing illegal build_vector dag nodes. 6647 static SDNode *tryToFoldExtendOfConstant(SDNode *N, const TargetLowering &TLI, 6648 SelectionDAG &DAG, bool LegalTypes, 6649 bool LegalOperations) { 6650 unsigned Opcode = N->getOpcode(); 6651 SDValue N0 = N->getOperand(0); 6652 EVT VT = N->getValueType(0); 6653 6654 assert((Opcode == ISD::SIGN_EXTEND || Opcode == ISD::ZERO_EXTEND || 6655 Opcode == ISD::ANY_EXTEND || Opcode == ISD::SIGN_EXTEND_VECTOR_INREG || 6656 Opcode == ISD::ZERO_EXTEND_VECTOR_INREG) 6657 && "Expected EXTEND dag node in input!"); 6658 6659 // fold (sext c1) -> c1 6660 // fold (zext c1) -> c1 6661 // fold (aext c1) -> c1 6662 if (isa<ConstantSDNode>(N0)) 6663 return DAG.getNode(Opcode, SDLoc(N), VT, N0).getNode(); 6664 6665 // fold (sext (build_vector AllConstants) -> (build_vector AllConstants) 6666 // fold (zext (build_vector AllConstants) -> (build_vector AllConstants) 6667 // fold (aext (build_vector AllConstants) -> (build_vector AllConstants) 6668 EVT SVT = VT.getScalarType(); 6669 if (!(VT.isVector() && 6670 (!LegalTypes || (!LegalOperations && TLI.isTypeLegal(SVT))) && 6671 ISD::isBuildVectorOfConstantSDNodes(N0.getNode()))) 6672 return nullptr; 6673 6674 // We can fold this node into a build_vector. 6675 unsigned VTBits = SVT.getSizeInBits(); 6676 unsigned EVTBits = N0->getValueType(0).getScalarSizeInBits(); 6677 SmallVector<SDValue, 8> Elts; 6678 unsigned NumElts = VT.getVectorNumElements(); 6679 SDLoc DL(N); 6680 6681 for (unsigned i=0; i != NumElts; ++i) { 6682 SDValue Op = N0->getOperand(i); 6683 if (Op->isUndef()) { 6684 Elts.push_back(DAG.getUNDEF(SVT)); 6685 continue; 6686 } 6687 6688 SDLoc DL(Op); 6689 // Get the constant value and if needed trunc it to the size of the type. 6690 // Nodes like build_vector might have constants wider than the scalar type. 6691 APInt C = cast<ConstantSDNode>(Op)->getAPIntValue().zextOrTrunc(EVTBits); 6692 if (Opcode == ISD::SIGN_EXTEND || Opcode == ISD::SIGN_EXTEND_VECTOR_INREG) 6693 Elts.push_back(DAG.getConstant(C.sext(VTBits), DL, SVT)); 6694 else 6695 Elts.push_back(DAG.getConstant(C.zext(VTBits), DL, SVT)); 6696 } 6697 6698 return DAG.getBuildVector(VT, DL, Elts).getNode(); 6699 } 6700 6701 // ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this: 6702 // "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))" 6703 // transformation. Returns true if extension are possible and the above 6704 // mentioned transformation is profitable. 6705 static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0, 6706 unsigned ExtOpc, 6707 SmallVectorImpl<SDNode *> &ExtendNodes, 6708 const TargetLowering &TLI) { 6709 bool HasCopyToRegUses = false; 6710 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType()); 6711 for (SDNode::use_iterator UI = N0.getNode()->use_begin(), 6712 UE = N0.getNode()->use_end(); 6713 UI != UE; ++UI) { 6714 SDNode *User = *UI; 6715 if (User == N) 6716 continue; 6717 if (UI.getUse().getResNo() != N0.getResNo()) 6718 continue; 6719 // FIXME: Only extend SETCC N, N and SETCC N, c for now. 6720 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) { 6721 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get(); 6722 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC)) 6723 // Sign bits will be lost after a zext. 6724 return false; 6725 bool Add = false; 6726 for (unsigned i = 0; i != 2; ++i) { 6727 SDValue UseOp = User->getOperand(i); 6728 if (UseOp == N0) 6729 continue; 6730 if (!isa<ConstantSDNode>(UseOp)) 6731 return false; 6732 Add = true; 6733 } 6734 if (Add) 6735 ExtendNodes.push_back(User); 6736 continue; 6737 } 6738 // If truncates aren't free and there are users we can't 6739 // extend, it isn't worthwhile. 6740 if (!isTruncFree) 6741 return false; 6742 // Remember if this value is live-out. 6743 if (User->getOpcode() == ISD::CopyToReg) 6744 HasCopyToRegUses = true; 6745 } 6746 6747 if (HasCopyToRegUses) { 6748 bool BothLiveOut = false; 6749 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); 6750 UI != UE; ++UI) { 6751 SDUse &Use = UI.getUse(); 6752 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) { 6753 BothLiveOut = true; 6754 break; 6755 } 6756 } 6757 if (BothLiveOut) 6758 // Both unextended and extended values are live out. There had better be 6759 // a good reason for the transformation. 6760 return ExtendNodes.size(); 6761 } 6762 return true; 6763 } 6764 6765 void DAGCombiner::ExtendSetCCUses(const SmallVectorImpl<SDNode *> &SetCCs, 6766 SDValue Trunc, SDValue ExtLoad, 6767 const SDLoc &DL, ISD::NodeType ExtType) { 6768 // Extend SetCC uses if necessary. 6769 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) { 6770 SDNode *SetCC = SetCCs[i]; 6771 SmallVector<SDValue, 4> Ops; 6772 6773 for (unsigned j = 0; j != 2; ++j) { 6774 SDValue SOp = SetCC->getOperand(j); 6775 if (SOp == Trunc) 6776 Ops.push_back(ExtLoad); 6777 else 6778 Ops.push_back(DAG.getNode(ExtType, DL, ExtLoad->getValueType(0), SOp)); 6779 } 6780 6781 Ops.push_back(SetCC->getOperand(2)); 6782 CombineTo(SetCC, DAG.getNode(ISD::SETCC, DL, SetCC->getValueType(0), Ops)); 6783 } 6784 } 6785 6786 // FIXME: Bring more similar combines here, common to sext/zext (maybe aext?). 6787 SDValue DAGCombiner::CombineExtLoad(SDNode *N) { 6788 SDValue N0 = N->getOperand(0); 6789 EVT DstVT = N->getValueType(0); 6790 EVT SrcVT = N0.getValueType(); 6791 6792 assert((N->getOpcode() == ISD::SIGN_EXTEND || 6793 N->getOpcode() == ISD::ZERO_EXTEND) && 6794 "Unexpected node type (not an extend)!"); 6795 6796 // fold (sext (load x)) to multiple smaller sextloads; same for zext. 6797 // For example, on a target with legal v4i32, but illegal v8i32, turn: 6798 // (v8i32 (sext (v8i16 (load x)))) 6799 // into: 6800 // (v8i32 (concat_vectors (v4i32 (sextload x)), 6801 // (v4i32 (sextload (x + 16))))) 6802 // Where uses of the original load, i.e.: 6803 // (v8i16 (load x)) 6804 // are replaced with: 6805 // (v8i16 (truncate 6806 // (v8i32 (concat_vectors (v4i32 (sextload x)), 6807 // (v4i32 (sextload (x + 16))))))) 6808 // 6809 // This combine is only applicable to illegal, but splittable, vectors. 6810 // All legal types, and illegal non-vector types, are handled elsewhere. 6811 // This combine is controlled by TargetLowering::isVectorLoadExtDesirable. 6812 // 6813 if (N0->getOpcode() != ISD::LOAD) 6814 return SDValue(); 6815 6816 LoadSDNode *LN0 = cast<LoadSDNode>(N0); 6817 6818 if (!ISD::isNON_EXTLoad(LN0) || !ISD::isUNINDEXEDLoad(LN0) || 6819 !N0.hasOneUse() || LN0->isVolatile() || !DstVT.isVector() || 6820 !DstVT.isPow2VectorType() || !TLI.isVectorLoadExtDesirable(SDValue(N, 0))) 6821 return SDValue(); 6822 6823 SmallVector<SDNode *, 4> SetCCs; 6824 if (!ExtendUsesToFormExtLoad(N, N0, N->getOpcode(), SetCCs, TLI)) 6825 return SDValue(); 6826 6827 ISD::LoadExtType ExtType = 6828 N->getOpcode() == ISD::SIGN_EXTEND ? ISD::SEXTLOAD : ISD::ZEXTLOAD; 6829 6830 // Try to split the vector types to get down to legal types. 6831 EVT SplitSrcVT = SrcVT; 6832 EVT SplitDstVT = DstVT; 6833 while (!TLI.isLoadExtLegalOrCustom(ExtType, SplitDstVT, SplitSrcVT) && 6834 SplitSrcVT.getVectorNumElements() > 1) { 6835 SplitDstVT = DAG.GetSplitDestVTs(SplitDstVT).first; 6836 SplitSrcVT = DAG.GetSplitDestVTs(SplitSrcVT).first; 6837 } 6838 6839 if (!TLI.isLoadExtLegalOrCustom(ExtType, SplitDstVT, SplitSrcVT)) 6840 return SDValue(); 6841 6842 SDLoc DL(N); 6843 const unsigned NumSplits = 6844 DstVT.getVectorNumElements() / SplitDstVT.getVectorNumElements(); 6845 const unsigned Stride = SplitSrcVT.getStoreSize(); 6846 SmallVector<SDValue, 4> Loads; 6847 SmallVector<SDValue, 4> Chains; 6848 6849 SDValue BasePtr = LN0->getBasePtr(); 6850 for (unsigned Idx = 0; Idx < NumSplits; Idx++) { 6851 const unsigned Offset = Idx * Stride; 6852 const unsigned Align = MinAlign(LN0->getAlignment(), Offset); 6853 6854 SDValue SplitLoad = DAG.getExtLoad( 6855 ExtType, DL, SplitDstVT, LN0->getChain(), BasePtr, 6856 LN0->getPointerInfo().getWithOffset(Offset), SplitSrcVT, Align, 6857 LN0->getMemOperand()->getFlags(), LN0->getAAInfo()); 6858 6859 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr, 6860 DAG.getConstant(Stride, DL, BasePtr.getValueType())); 6861 6862 Loads.push_back(SplitLoad.getValue(0)); 6863 Chains.push_back(SplitLoad.getValue(1)); 6864 } 6865 6866 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains); 6867 SDValue NewValue = DAG.getNode(ISD::CONCAT_VECTORS, DL, DstVT, Loads); 6868 6869 // Simplify TF. 6870 AddToWorklist(NewChain.getNode()); 6871 6872 CombineTo(N, NewValue); 6873 6874 // Replace uses of the original load (before extension) 6875 // with a truncate of the concatenated sextloaded vectors. 6876 SDValue Trunc = 6877 DAG.getNode(ISD::TRUNCATE, SDLoc(N0), N0.getValueType(), NewValue); 6878 CombineTo(N0.getNode(), Trunc, NewChain); 6879 ExtendSetCCUses(SetCCs, Trunc, NewValue, DL, 6880 (ISD::NodeType)N->getOpcode()); 6881 return SDValue(N, 0); // Return N so it doesn't get rechecked! 6882 } 6883 6884 SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) { 6885 SDValue N0 = N->getOperand(0); 6886 EVT VT = N->getValueType(0); 6887 SDLoc DL(N); 6888 6889 if (SDNode *Res = tryToFoldExtendOfConstant(N, TLI, DAG, LegalTypes, 6890 LegalOperations)) 6891 return SDValue(Res, 0); 6892 6893 // fold (sext (sext x)) -> (sext x) 6894 // fold (sext (aext x)) -> (sext x) 6895 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) 6896 return DAG.getNode(ISD::SIGN_EXTEND, DL, VT, N0.getOperand(0)); 6897 6898 if (N0.getOpcode() == ISD::TRUNCATE) { 6899 // fold (sext (truncate (load x))) -> (sext (smaller load x)) 6900 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n))) 6901 if (SDValue NarrowLoad = ReduceLoadWidth(N0.getNode())) { 6902 SDNode *oye = N0.getOperand(0).getNode(); 6903 if (NarrowLoad.getNode() != N0.getNode()) { 6904 CombineTo(N0.getNode(), NarrowLoad); 6905 // CombineTo deleted the truncate, if needed, but not what's under it. 6906 AddToWorklist(oye); 6907 } 6908 return SDValue(N, 0); // Return N so it doesn't get rechecked! 6909 } 6910 6911 // See if the value being truncated is already sign extended. If so, just 6912 // eliminate the trunc/sext pair. 6913 SDValue Op = N0.getOperand(0); 6914 unsigned OpBits = Op.getScalarValueSizeInBits(); 6915 unsigned MidBits = N0.getScalarValueSizeInBits(); 6916 unsigned DestBits = VT.getScalarSizeInBits(); 6917 unsigned NumSignBits = DAG.ComputeNumSignBits(Op); 6918 6919 if (OpBits == DestBits) { 6920 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign 6921 // bits, it is already ready. 6922 if (NumSignBits > DestBits-MidBits) 6923 return Op; 6924 } else if (OpBits < DestBits) { 6925 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign 6926 // bits, just sext from i32. 6927 if (NumSignBits > OpBits-MidBits) 6928 return DAG.getNode(ISD::SIGN_EXTEND, DL, VT, Op); 6929 } else { 6930 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign 6931 // bits, just truncate to i32. 6932 if (NumSignBits > OpBits-MidBits) 6933 return DAG.getNode(ISD::TRUNCATE, DL, VT, Op); 6934 } 6935 6936 // fold (sext (truncate x)) -> (sextinreg x). 6937 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, 6938 N0.getValueType())) { 6939 if (OpBits < DestBits) 6940 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N0), VT, Op); 6941 else if (OpBits > DestBits) 6942 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT, Op); 6943 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, Op, 6944 DAG.getValueType(N0.getValueType())); 6945 } 6946 } 6947 6948 // fold (sext (load x)) -> (sext (truncate (sextload x))) 6949 // Only generate vector extloads when 1) they're legal, and 2) they are 6950 // deemed desirable by the target. 6951 if (ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) && 6952 ((!LegalOperations && !VT.isVector() && 6953 !cast<LoadSDNode>(N0)->isVolatile()) || 6954 TLI.isLoadExtLegal(ISD::SEXTLOAD, VT, N0.getValueType()))) { 6955 bool DoXform = true; 6956 SmallVector<SDNode*, 4> SetCCs; 6957 if (!N0.hasOneUse()) 6958 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI); 6959 if (VT.isVector()) 6960 DoXform &= TLI.isVectorLoadExtDesirable(SDValue(N, 0)); 6961 if (DoXform) { 6962 LoadSDNode *LN0 = cast<LoadSDNode>(N0); 6963 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, DL, VT, LN0->getChain(), 6964 LN0->getBasePtr(), N0.getValueType(), 6965 LN0->getMemOperand()); 6966 CombineTo(N, ExtLoad); 6967 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), 6968 N0.getValueType(), ExtLoad); 6969 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1)); 6970 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, DL, ISD::SIGN_EXTEND); 6971 return SDValue(N, 0); // Return N so it doesn't get rechecked! 6972 } 6973 } 6974 6975 // fold (sext (load x)) to multiple smaller sextloads. 6976 // Only on illegal but splittable vectors. 6977 if (SDValue ExtLoad = CombineExtLoad(N)) 6978 return ExtLoad; 6979 6980 // fold (sext (sextload x)) -> (sext (truncate (sextload x))) 6981 // fold (sext ( extload x)) -> (sext (truncate (sextload x))) 6982 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) && 6983 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) { 6984 LoadSDNode *LN0 = cast<LoadSDNode>(N0); 6985 EVT MemVT = LN0->getMemoryVT(); 6986 if ((!LegalOperations && !LN0->isVolatile()) || 6987 TLI.isLoadExtLegal(ISD::SEXTLOAD, VT, MemVT)) { 6988 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, DL, VT, LN0->getChain(), 6989 LN0->getBasePtr(), MemVT, 6990 LN0->getMemOperand()); 6991 CombineTo(N, ExtLoad); 6992 CombineTo(N0.getNode(), 6993 DAG.getNode(ISD::TRUNCATE, SDLoc(N0), 6994 N0.getValueType(), ExtLoad), 6995 ExtLoad.getValue(1)); 6996 return SDValue(N, 0); // Return N so it doesn't get rechecked! 6997 } 6998 } 6999 7000 // fold (sext (and/or/xor (load x), cst)) -> 7001 // (and/or/xor (sextload x), (sext cst)) 7002 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR || 7003 N0.getOpcode() == ISD::XOR) && 7004 isa<LoadSDNode>(N0.getOperand(0)) && 7005 N0.getOperand(1).getOpcode() == ISD::Constant && 7006 TLI.isLoadExtLegal(ISD::SEXTLOAD, VT, N0.getValueType()) && 7007 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) { 7008 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0)); 7009 if (LN0->getExtensionType() != ISD::ZEXTLOAD && LN0->isUnindexed()) { 7010 bool DoXform = true; 7011 SmallVector<SDNode*, 4> SetCCs; 7012 if (!N0.hasOneUse()) 7013 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::SIGN_EXTEND, 7014 SetCCs, TLI); 7015 if (DoXform) { 7016 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(LN0), VT, 7017 LN0->getChain(), LN0->getBasePtr(), 7018 LN0->getMemoryVT(), 7019 LN0->getMemOperand()); 7020 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); 7021 Mask = Mask.sext(VT.getSizeInBits()); 7022 SDValue And = DAG.getNode(N0.getOpcode(), DL, VT, 7023 ExtLoad, DAG.getConstant(Mask, DL, VT)); 7024 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, 7025 SDLoc(N0.getOperand(0)), 7026 N0.getOperand(0).getValueType(), ExtLoad); 7027 CombineTo(N, And); 7028 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1)); 7029 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, DL, ISD::SIGN_EXTEND); 7030 return SDValue(N, 0); // Return N so it doesn't get rechecked! 7031 } 7032 } 7033 } 7034 7035 if (N0.getOpcode() == ISD::SETCC) { 7036 SDValue N00 = N0.getOperand(0); 7037 SDValue N01 = N0.getOperand(1); 7038 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get(); 7039 EVT N00VT = N0.getOperand(0).getValueType(); 7040 7041 // sext(setcc) -> sext_in_reg(vsetcc) for vectors. 7042 // Only do this before legalize for now. 7043 if (VT.isVector() && !LegalOperations && 7044 TLI.getBooleanContents(N00VT) == 7045 TargetLowering::ZeroOrNegativeOneBooleanContent) { 7046 // On some architectures (such as SSE/NEON/etc) the SETCC result type is 7047 // of the same size as the compared operands. Only optimize sext(setcc()) 7048 // if this is the case. 7049 EVT SVT = getSetCCResultType(N00VT); 7050 7051 // We know that the # elements of the results is the same as the 7052 // # elements of the compare (and the # elements of the compare result 7053 // for that matter). Check to see that they are the same size. If so, 7054 // we know that the element size of the sext'd result matches the 7055 // element size of the compare operands. 7056 if (VT.getSizeInBits() == SVT.getSizeInBits()) 7057 return DAG.getSetCC(DL, VT, N00, N01, CC); 7058 7059 // If the desired elements are smaller or larger than the source 7060 // elements, we can use a matching integer vector type and then 7061 // truncate/sign extend. 7062 EVT MatchingVecType = N00VT.changeVectorElementTypeToInteger(); 7063 if (SVT == MatchingVecType) { 7064 SDValue VsetCC = DAG.getSetCC(DL, MatchingVecType, N00, N01, CC); 7065 return DAG.getSExtOrTrunc(VsetCC, DL, VT); 7066 } 7067 } 7068 7069 // sext(setcc x, y, cc) -> (select (setcc x, y, cc), T, 0) 7070 // Here, T can be 1 or -1, depending on the type of the setcc and 7071 // getBooleanContents(). 7072 unsigned SetCCWidth = N0.getScalarValueSizeInBits(); 7073 7074 // To determine the "true" side of the select, we need to know the high bit 7075 // of the value returned by the setcc if it evaluates to true. 7076 // If the type of the setcc is i1, then the true case of the select is just 7077 // sext(i1 1), that is, -1. 7078 // If the type of the setcc is larger (say, i8) then the value of the high 7079 // bit depends on getBooleanContents(), so ask TLI for a real "true" value 7080 // of the appropriate width. 7081 SDValue ExtTrueVal = (SetCCWidth == 1) ? DAG.getAllOnesConstant(DL, VT) 7082 : TLI.getConstTrueVal(DAG, VT, DL); 7083 SDValue Zero = DAG.getConstant(0, DL, VT); 7084 if (SDValue SCC = 7085 SimplifySelectCC(DL, N00, N01, ExtTrueVal, Zero, CC, true)) 7086 return SCC; 7087 7088 if (!VT.isVector()) { 7089 EVT SetCCVT = getSetCCResultType(N00VT); 7090 // Don't do this transform for i1 because there's a select transform 7091 // that would reverse it. 7092 // TODO: We should not do this transform at all without a target hook 7093 // because a sext is likely cheaper than a select? 7094 if (SetCCVT.getScalarSizeInBits() != 1 && 7095 (!LegalOperations || TLI.isOperationLegal(ISD::SETCC, N00VT))) { 7096 SDValue SetCC = DAG.getSetCC(DL, SetCCVT, N00, N01, CC); 7097 return DAG.getSelect(DL, VT, SetCC, ExtTrueVal, Zero); 7098 } 7099 } 7100 } 7101 7102 // fold (sext x) -> (zext x) if the sign bit is known zero. 7103 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) && 7104 DAG.SignBitIsZero(N0)) 7105 return DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0); 7106 7107 return SDValue(); 7108 } 7109 7110 // isTruncateOf - If N is a truncate of some other value, return true, record 7111 // the value being truncated in Op and which of Op's bits are zero in KnownZero. 7112 // This function computes KnownZero to avoid a duplicated call to 7113 // computeKnownBits in the caller. 7114 static bool isTruncateOf(SelectionDAG &DAG, SDValue N, SDValue &Op, 7115 APInt &KnownZero) { 7116 APInt KnownOne; 7117 if (N->getOpcode() == ISD::TRUNCATE) { 7118 Op = N->getOperand(0); 7119 DAG.computeKnownBits(Op, KnownZero, KnownOne); 7120 return true; 7121 } 7122 7123 if (N->getOpcode() != ISD::SETCC || N->getValueType(0) != MVT::i1 || 7124 cast<CondCodeSDNode>(N->getOperand(2))->get() != ISD::SETNE) 7125 return false; 7126 7127 SDValue Op0 = N->getOperand(0); 7128 SDValue Op1 = N->getOperand(1); 7129 assert(Op0.getValueType() == Op1.getValueType()); 7130 7131 if (isNullConstant(Op0)) 7132 Op = Op1; 7133 else if (isNullConstant(Op1)) 7134 Op = Op0; 7135 else 7136 return false; 7137 7138 DAG.computeKnownBits(Op, KnownZero, KnownOne); 7139 7140 if (!(KnownZero | APInt(Op.getValueSizeInBits(), 1)).isAllOnesValue()) 7141 return false; 7142 7143 return true; 7144 } 7145 7146 SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) { 7147 SDValue N0 = N->getOperand(0); 7148 EVT VT = N->getValueType(0); 7149 7150 if (SDNode *Res = tryToFoldExtendOfConstant(N, TLI, DAG, LegalTypes, 7151 LegalOperations)) 7152 return SDValue(Res, 0); 7153 7154 // fold (zext (zext x)) -> (zext x) 7155 // fold (zext (aext x)) -> (zext x) 7156 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) 7157 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, 7158 N0.getOperand(0)); 7159 7160 // fold (zext (truncate x)) -> (zext x) or 7161 // (zext (truncate x)) -> (truncate x) 7162 // This is valid when the truncated bits of x are already zero. 7163 // FIXME: We should extend this to work for vectors too. 7164 SDValue Op; 7165 APInt KnownZero; 7166 if (!VT.isVector() && isTruncateOf(DAG, N0, Op, KnownZero)) { 7167 APInt TruncatedBits = 7168 (Op.getValueSizeInBits() == N0.getValueSizeInBits()) ? 7169 APInt(Op.getValueSizeInBits(), 0) : 7170 APInt::getBitsSet(Op.getValueSizeInBits(), 7171 N0.getValueSizeInBits(), 7172 std::min(Op.getValueSizeInBits(), 7173 VT.getSizeInBits())); 7174 if (TruncatedBits == (KnownZero & TruncatedBits)) { 7175 if (VT.bitsGT(Op.getValueType())) 7176 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, Op); 7177 if (VT.bitsLT(Op.getValueType())) 7178 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op); 7179 7180 return Op; 7181 } 7182 } 7183 7184 // fold (zext (truncate (load x))) -> (zext (smaller load x)) 7185 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n))) 7186 if (N0.getOpcode() == ISD::TRUNCATE) { 7187 if (SDValue NarrowLoad = ReduceLoadWidth(N0.getNode())) { 7188 SDNode *oye = N0.getOperand(0).getNode(); 7189 if (NarrowLoad.getNode() != N0.getNode()) { 7190 CombineTo(N0.getNode(), NarrowLoad); 7191 // CombineTo deleted the truncate, if needed, but not what's under it. 7192 AddToWorklist(oye); 7193 } 7194 return SDValue(N, 0); // Return N so it doesn't get rechecked! 7195 } 7196 } 7197 7198 // fold (zext (truncate x)) -> (and x, mask) 7199 if (N0.getOpcode() == ISD::TRUNCATE) { 7200 // fold (zext (truncate (load x))) -> (zext (smaller load x)) 7201 // fold (zext (truncate (srl (load x), c))) -> (zext (smaller load (x+c/n))) 7202 if (SDValue NarrowLoad = ReduceLoadWidth(N0.getNode())) { 7203 SDNode *oye = N0.getOperand(0).getNode(); 7204 if (NarrowLoad.getNode() != N0.getNode()) { 7205 CombineTo(N0.getNode(), NarrowLoad); 7206 // CombineTo deleted the truncate, if needed, but not what's under it. 7207 AddToWorklist(oye); 7208 } 7209 return SDValue(N, 0); // Return N so it doesn't get rechecked! 7210 } 7211 7212 EVT SrcVT = N0.getOperand(0).getValueType(); 7213 EVT MinVT = N0.getValueType(); 7214 7215 // Try to mask before the extension to avoid having to generate a larger mask, 7216 // possibly over several sub-vectors. 7217 if (SrcVT.bitsLT(VT)) { 7218 if (!LegalOperations || (TLI.isOperationLegal(ISD::AND, SrcVT) && 7219 TLI.isOperationLegal(ISD::ZERO_EXTEND, VT))) { 7220 SDValue Op = N0.getOperand(0); 7221 Op = DAG.getZeroExtendInReg(Op, SDLoc(N), MinVT.getScalarType()); 7222 AddToWorklist(Op.getNode()); 7223 return DAG.getZExtOrTrunc(Op, SDLoc(N), VT); 7224 } 7225 } 7226 7227 if (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT)) { 7228 SDValue Op = N0.getOperand(0); 7229 if (SrcVT.bitsLT(VT)) { 7230 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, Op); 7231 AddToWorklist(Op.getNode()); 7232 } else if (SrcVT.bitsGT(VT)) { 7233 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op); 7234 AddToWorklist(Op.getNode()); 7235 } 7236 return DAG.getZeroExtendInReg(Op, SDLoc(N), MinVT.getScalarType()); 7237 } 7238 } 7239 7240 // Fold (zext (and (trunc x), cst)) -> (and x, cst), 7241 // if either of the casts is not free. 7242 if (N0.getOpcode() == ISD::AND && 7243 N0.getOperand(0).getOpcode() == ISD::TRUNCATE && 7244 N0.getOperand(1).getOpcode() == ISD::Constant && 7245 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(), 7246 N0.getValueType()) || 7247 !TLI.isZExtFree(N0.getValueType(), VT))) { 7248 SDValue X = N0.getOperand(0).getOperand(0); 7249 if (X.getValueType().bitsLT(VT)) { 7250 X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(X), VT, X); 7251 } else if (X.getValueType().bitsGT(VT)) { 7252 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X); 7253 } 7254 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); 7255 Mask = Mask.zext(VT.getSizeInBits()); 7256 SDLoc DL(N); 7257 return DAG.getNode(ISD::AND, DL, VT, 7258 X, DAG.getConstant(Mask, DL, VT)); 7259 } 7260 7261 // fold (zext (load x)) -> (zext (truncate (zextload x))) 7262 // Only generate vector extloads when 1) they're legal, and 2) they are 7263 // deemed desirable by the target. 7264 if (ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) && 7265 ((!LegalOperations && !VT.isVector() && 7266 !cast<LoadSDNode>(N0)->isVolatile()) || 7267 TLI.isLoadExtLegal(ISD::ZEXTLOAD, VT, N0.getValueType()))) { 7268 bool DoXform = true; 7269 SmallVector<SDNode*, 4> SetCCs; 7270 if (!N0.hasOneUse()) 7271 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI); 7272 if (VT.isVector()) 7273 DoXform &= TLI.isVectorLoadExtDesirable(SDValue(N, 0)); 7274 if (DoXform) { 7275 LoadSDNode *LN0 = cast<LoadSDNode>(N0); 7276 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT, 7277 LN0->getChain(), 7278 LN0->getBasePtr(), N0.getValueType(), 7279 LN0->getMemOperand()); 7280 7281 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), 7282 N0.getValueType(), ExtLoad); 7283 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1)); 7284 7285 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N), 7286 ISD::ZERO_EXTEND); 7287 CombineTo(N, ExtLoad); 7288 return SDValue(N, 0); // Return N so it doesn't get rechecked! 7289 } 7290 } 7291 7292 // fold (zext (load x)) to multiple smaller zextloads. 7293 // Only on illegal but splittable vectors. 7294 if (SDValue ExtLoad = CombineExtLoad(N)) 7295 return ExtLoad; 7296 7297 // fold (zext (and/or/xor (load x), cst)) -> 7298 // (and/or/xor (zextload x), (zext cst)) 7299 // Unless (and (load x) cst) will match as a zextload already and has 7300 // additional users. 7301 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR || 7302 N0.getOpcode() == ISD::XOR) && 7303 isa<LoadSDNode>(N0.getOperand(0)) && 7304 N0.getOperand(1).getOpcode() == ISD::Constant && 7305 TLI.isLoadExtLegal(ISD::ZEXTLOAD, VT, N0.getValueType()) && 7306 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) { 7307 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0)); 7308 if (LN0->getExtensionType() != ISD::SEXTLOAD && LN0->isUnindexed()) { 7309 bool DoXform = true; 7310 SmallVector<SDNode*, 4> SetCCs; 7311 if (!N0.hasOneUse()) { 7312 if (N0.getOpcode() == ISD::AND) { 7313 auto *AndC = cast<ConstantSDNode>(N0.getOperand(1)); 7314 auto NarrowLoad = false; 7315 EVT LoadResultTy = AndC->getValueType(0); 7316 EVT ExtVT, LoadedVT; 7317 if (isAndLoadExtLoad(AndC, LN0, LoadResultTy, ExtVT, LoadedVT, 7318 NarrowLoad)) 7319 DoXform = false; 7320 } 7321 if (DoXform) 7322 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), 7323 ISD::ZERO_EXTEND, SetCCs, TLI); 7324 } 7325 if (DoXform) { 7326 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), VT, 7327 LN0->getChain(), LN0->getBasePtr(), 7328 LN0->getMemoryVT(), 7329 LN0->getMemOperand()); 7330 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); 7331 Mask = Mask.zext(VT.getSizeInBits()); 7332 SDLoc DL(N); 7333 SDValue And = DAG.getNode(N0.getOpcode(), DL, VT, 7334 ExtLoad, DAG.getConstant(Mask, DL, VT)); 7335 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, 7336 SDLoc(N0.getOperand(0)), 7337 N0.getOperand(0).getValueType(), ExtLoad); 7338 CombineTo(N, And); 7339 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1)); 7340 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, DL, 7341 ISD::ZERO_EXTEND); 7342 return SDValue(N, 0); // Return N so it doesn't get rechecked! 7343 } 7344 } 7345 } 7346 7347 // fold (zext (zextload x)) -> (zext (truncate (zextload x))) 7348 // fold (zext ( extload x)) -> (zext (truncate (zextload x))) 7349 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) && 7350 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) { 7351 LoadSDNode *LN0 = cast<LoadSDNode>(N0); 7352 EVT MemVT = LN0->getMemoryVT(); 7353 if ((!LegalOperations && !LN0->isVolatile()) || 7354 TLI.isLoadExtLegal(ISD::ZEXTLOAD, VT, MemVT)) { 7355 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT, 7356 LN0->getChain(), 7357 LN0->getBasePtr(), MemVT, 7358 LN0->getMemOperand()); 7359 CombineTo(N, ExtLoad); 7360 CombineTo(N0.getNode(), 7361 DAG.getNode(ISD::TRUNCATE, SDLoc(N0), N0.getValueType(), 7362 ExtLoad), 7363 ExtLoad.getValue(1)); 7364 return SDValue(N, 0); // Return N so it doesn't get rechecked! 7365 } 7366 } 7367 7368 if (N0.getOpcode() == ISD::SETCC) { 7369 // Only do this before legalize for now. 7370 if (!LegalOperations && VT.isVector() && 7371 N0.getValueType().getVectorElementType() == MVT::i1) { 7372 EVT N00VT = N0.getOperand(0).getValueType(); 7373 if (getSetCCResultType(N00VT) == N0.getValueType()) 7374 return SDValue(); 7375 7376 // We know that the # elements of the results is the same as the # 7377 // elements of the compare (and the # elements of the compare result for 7378 // that matter). Check to see that they are the same size. If so, we know 7379 // that the element size of the sext'd result matches the element size of 7380 // the compare operands. 7381 SDLoc DL(N); 7382 SDValue VecOnes = DAG.getConstant(1, DL, VT); 7383 if (VT.getSizeInBits() == N00VT.getSizeInBits()) { 7384 // zext(setcc) -> (and (vsetcc), (1, 1, ...) for vectors. 7385 SDValue VSetCC = DAG.getNode(ISD::SETCC, DL, VT, N0.getOperand(0), 7386 N0.getOperand(1), N0.getOperand(2)); 7387 return DAG.getNode(ISD::AND, DL, VT, VSetCC, VecOnes); 7388 } 7389 7390 // If the desired elements are smaller or larger than the source 7391 // elements we can use a matching integer vector type and then 7392 // truncate/sign extend. 7393 EVT MatchingElementType = EVT::getIntegerVT( 7394 *DAG.getContext(), N00VT.getScalarSizeInBits()); 7395 EVT MatchingVectorType = EVT::getVectorVT( 7396 *DAG.getContext(), MatchingElementType, N00VT.getVectorNumElements()); 7397 SDValue VsetCC = 7398 DAG.getNode(ISD::SETCC, DL, MatchingVectorType, N0.getOperand(0), 7399 N0.getOperand(1), N0.getOperand(2)); 7400 return DAG.getNode(ISD::AND, DL, VT, DAG.getSExtOrTrunc(VsetCC, DL, VT), 7401 VecOnes); 7402 } 7403 7404 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc 7405 SDLoc DL(N); 7406 if (SDValue SCC = SimplifySelectCC( 7407 DL, N0.getOperand(0), N0.getOperand(1), DAG.getConstant(1, DL, VT), 7408 DAG.getConstant(0, DL, VT), 7409 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true)) 7410 return SCC; 7411 } 7412 7413 // (zext (shl (zext x), cst)) -> (shl (zext x), cst) 7414 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) && 7415 isa<ConstantSDNode>(N0.getOperand(1)) && 7416 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND && 7417 N0.hasOneUse()) { 7418 SDValue ShAmt = N0.getOperand(1); 7419 unsigned ShAmtVal = cast<ConstantSDNode>(ShAmt)->getZExtValue(); 7420 if (N0.getOpcode() == ISD::SHL) { 7421 SDValue InnerZExt = N0.getOperand(0); 7422 // If the original shl may be shifting out bits, do not perform this 7423 // transformation. 7424 unsigned KnownZeroBits = InnerZExt.getValueSizeInBits() - 7425 InnerZExt.getOperand(0).getValueSizeInBits(); 7426 if (ShAmtVal > KnownZeroBits) 7427 return SDValue(); 7428 } 7429 7430 SDLoc DL(N); 7431 7432 // Ensure that the shift amount is wide enough for the shifted value. 7433 if (VT.getSizeInBits() >= 256) 7434 ShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, ShAmt); 7435 7436 return DAG.getNode(N0.getOpcode(), DL, VT, 7437 DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)), 7438 ShAmt); 7439 } 7440 7441 return SDValue(); 7442 } 7443 7444 SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) { 7445 SDValue N0 = N->getOperand(0); 7446 EVT VT = N->getValueType(0); 7447 7448 if (SDNode *Res = tryToFoldExtendOfConstant(N, TLI, DAG, LegalTypes, 7449 LegalOperations)) 7450 return SDValue(Res, 0); 7451 7452 // fold (aext (aext x)) -> (aext x) 7453 // fold (aext (zext x)) -> (zext x) 7454 // fold (aext (sext x)) -> (sext x) 7455 if (N0.getOpcode() == ISD::ANY_EXTEND || 7456 N0.getOpcode() == ISD::ZERO_EXTEND || 7457 N0.getOpcode() == ISD::SIGN_EXTEND) 7458 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, N0.getOperand(0)); 7459 7460 // fold (aext (truncate (load x))) -> (aext (smaller load x)) 7461 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n))) 7462 if (N0.getOpcode() == ISD::TRUNCATE) { 7463 if (SDValue NarrowLoad = ReduceLoadWidth(N0.getNode())) { 7464 SDNode *oye = N0.getOperand(0).getNode(); 7465 if (NarrowLoad.getNode() != N0.getNode()) { 7466 CombineTo(N0.getNode(), NarrowLoad); 7467 // CombineTo deleted the truncate, if needed, but not what's under it. 7468 AddToWorklist(oye); 7469 } 7470 return SDValue(N, 0); // Return N so it doesn't get rechecked! 7471 } 7472 } 7473 7474 // fold (aext (truncate x)) 7475 if (N0.getOpcode() == ISD::TRUNCATE) { 7476 SDValue TruncOp = N0.getOperand(0); 7477 if (TruncOp.getValueType() == VT) 7478 return TruncOp; // x iff x size == zext size. 7479 if (TruncOp.getValueType().bitsGT(VT)) 7480 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, TruncOp); 7481 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, TruncOp); 7482 } 7483 7484 // Fold (aext (and (trunc x), cst)) -> (and x, cst) 7485 // if the trunc is not free. 7486 if (N0.getOpcode() == ISD::AND && 7487 N0.getOperand(0).getOpcode() == ISD::TRUNCATE && 7488 N0.getOperand(1).getOpcode() == ISD::Constant && 7489 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(), 7490 N0.getValueType())) { 7491 SDLoc DL(N); 7492 SDValue X = N0.getOperand(0).getOperand(0); 7493 if (X.getValueType().bitsLT(VT)) { 7494 X = DAG.getNode(ISD::ANY_EXTEND, DL, VT, X); 7495 } else if (X.getValueType().bitsGT(VT)) { 7496 X = DAG.getNode(ISD::TRUNCATE, DL, VT, X); 7497 } 7498 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); 7499 Mask = Mask.zext(VT.getSizeInBits()); 7500 return DAG.getNode(ISD::AND, DL, VT, 7501 X, DAG.getConstant(Mask, DL, VT)); 7502 } 7503 7504 // fold (aext (load x)) -> (aext (truncate (extload x))) 7505 // None of the supported targets knows how to perform load and any_ext 7506 // on vectors in one instruction. We only perform this transformation on 7507 // scalars. 7508 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() && 7509 ISD::isUNINDEXEDLoad(N0.getNode()) && 7510 TLI.isLoadExtLegal(ISD::EXTLOAD, VT, N0.getValueType())) { 7511 bool DoXform = true; 7512 SmallVector<SDNode*, 4> SetCCs; 7513 if (!N0.hasOneUse()) 7514 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI); 7515 if (DoXform) { 7516 LoadSDNode *LN0 = cast<LoadSDNode>(N0); 7517 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT, 7518 LN0->getChain(), 7519 LN0->getBasePtr(), N0.getValueType(), 7520 LN0->getMemOperand()); 7521 CombineTo(N, ExtLoad); 7522 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), 7523 N0.getValueType(), ExtLoad); 7524 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1)); 7525 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N), 7526 ISD::ANY_EXTEND); 7527 return SDValue(N, 0); // Return N so it doesn't get rechecked! 7528 } 7529 } 7530 7531 // fold (aext (zextload x)) -> (aext (truncate (zextload x))) 7532 // fold (aext (sextload x)) -> (aext (truncate (sextload x))) 7533 // fold (aext ( extload x)) -> (aext (truncate (extload x))) 7534 if (N0.getOpcode() == ISD::LOAD && 7535 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) && 7536 N0.hasOneUse()) { 7537 LoadSDNode *LN0 = cast<LoadSDNode>(N0); 7538 ISD::LoadExtType ExtType = LN0->getExtensionType(); 7539 EVT MemVT = LN0->getMemoryVT(); 7540 if (!LegalOperations || TLI.isLoadExtLegal(ExtType, VT, MemVT)) { 7541 SDValue ExtLoad = DAG.getExtLoad(ExtType, SDLoc(N), 7542 VT, LN0->getChain(), LN0->getBasePtr(), 7543 MemVT, LN0->getMemOperand()); 7544 CombineTo(N, ExtLoad); 7545 CombineTo(N0.getNode(), 7546 DAG.getNode(ISD::TRUNCATE, SDLoc(N0), 7547 N0.getValueType(), ExtLoad), 7548 ExtLoad.getValue(1)); 7549 return SDValue(N, 0); // Return N so it doesn't get rechecked! 7550 } 7551 } 7552 7553 if (N0.getOpcode() == ISD::SETCC) { 7554 // For vectors: 7555 // aext(setcc) -> vsetcc 7556 // aext(setcc) -> truncate(vsetcc) 7557 // aext(setcc) -> aext(vsetcc) 7558 // Only do this before legalize for now. 7559 if (VT.isVector() && !LegalOperations) { 7560 EVT N0VT = N0.getOperand(0).getValueType(); 7561 // We know that the # elements of the results is the same as the 7562 // # elements of the compare (and the # elements of the compare result 7563 // for that matter). Check to see that they are the same size. If so, 7564 // we know that the element size of the sext'd result matches the 7565 // element size of the compare operands. 7566 if (VT.getSizeInBits() == N0VT.getSizeInBits()) 7567 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0), 7568 N0.getOperand(1), 7569 cast<CondCodeSDNode>(N0.getOperand(2))->get()); 7570 // If the desired elements are smaller or larger than the source 7571 // elements we can use a matching integer vector type and then 7572 // truncate/any extend 7573 else { 7574 EVT MatchingVectorType = N0VT.changeVectorElementTypeToInteger(); 7575 SDValue VsetCC = 7576 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0), 7577 N0.getOperand(1), 7578 cast<CondCodeSDNode>(N0.getOperand(2))->get()); 7579 return DAG.getAnyExtOrTrunc(VsetCC, SDLoc(N), VT); 7580 } 7581 } 7582 7583 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc 7584 SDLoc DL(N); 7585 if (SDValue SCC = SimplifySelectCC( 7586 DL, N0.getOperand(0), N0.getOperand(1), DAG.getConstant(1, DL, VT), 7587 DAG.getConstant(0, DL, VT), 7588 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true)) 7589 return SCC; 7590 } 7591 7592 return SDValue(); 7593 } 7594 7595 SDValue DAGCombiner::visitAssertZext(SDNode *N) { 7596 SDValue N0 = N->getOperand(0); 7597 SDValue N1 = N->getOperand(1); 7598 EVT EVT = cast<VTSDNode>(N1)->getVT(); 7599 7600 // fold (assertzext (assertzext x, vt), vt) -> (assertzext x, vt) 7601 if (N0.getOpcode() == ISD::AssertZext && 7602 EVT == cast<VTSDNode>(N0.getOperand(1))->getVT()) 7603 return N0; 7604 7605 return SDValue(); 7606 } 7607 7608 /// See if the specified operand can be simplified with the knowledge that only 7609 /// the bits specified by Mask are used. If so, return the simpler operand, 7610 /// otherwise return a null SDValue. 7611 /// 7612 /// (This exists alongside SimplifyDemandedBits because GetDemandedBits can 7613 /// simplify nodes with multiple uses more aggressively.) 7614 SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) { 7615 switch (V.getOpcode()) { 7616 default: break; 7617 case ISD::Constant: { 7618 const ConstantSDNode *CV = cast<ConstantSDNode>(V.getNode()); 7619 assert(CV && "Const value should be ConstSDNode."); 7620 const APInt &CVal = CV->getAPIntValue(); 7621 APInt NewVal = CVal & Mask; 7622 if (NewVal != CVal) 7623 return DAG.getConstant(NewVal, SDLoc(V), V.getValueType()); 7624 break; 7625 } 7626 case ISD::OR: 7627 case ISD::XOR: 7628 // If the LHS or RHS don't contribute bits to the or, drop them. 7629 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask)) 7630 return V.getOperand(1); 7631 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask)) 7632 return V.getOperand(0); 7633 break; 7634 case ISD::SRL: 7635 // Only look at single-use SRLs. 7636 if (!V.getNode()->hasOneUse()) 7637 break; 7638 if (ConstantSDNode *RHSC = getAsNonOpaqueConstant(V.getOperand(1))) { 7639 // See if we can recursively simplify the LHS. 7640 unsigned Amt = RHSC->getZExtValue(); 7641 7642 // Watch out for shift count overflow though. 7643 if (Amt >= Mask.getBitWidth()) break; 7644 APInt NewMask = Mask << Amt; 7645 if (SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask)) 7646 return DAG.getNode(ISD::SRL, SDLoc(V), V.getValueType(), 7647 SimplifyLHS, V.getOperand(1)); 7648 } 7649 break; 7650 case ISD::AND: { 7651 // X & -1 -> X (ignoring bits which aren't demanded). 7652 ConstantSDNode *AndVal = isConstOrConstSplat(V.getOperand(1)); 7653 if (AndVal && (AndVal->getAPIntValue() & Mask) == Mask) 7654 return V.getOperand(0); 7655 break; 7656 } 7657 } 7658 return SDValue(); 7659 } 7660 7661 /// If the result of a wider load is shifted to right of N bits and then 7662 /// truncated to a narrower type and where N is a multiple of number of bits of 7663 /// the narrower type, transform it to a narrower load from address + N / num of 7664 /// bits of new type. If the result is to be extended, also fold the extension 7665 /// to form a extending load. 7666 SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) { 7667 unsigned Opc = N->getOpcode(); 7668 7669 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD; 7670 SDValue N0 = N->getOperand(0); 7671 EVT VT = N->getValueType(0); 7672 EVT ExtVT = VT; 7673 7674 // This transformation isn't valid for vector loads. 7675 if (VT.isVector()) 7676 return SDValue(); 7677 7678 // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then 7679 // extended to VT. 7680 if (Opc == ISD::SIGN_EXTEND_INREG) { 7681 ExtType = ISD::SEXTLOAD; 7682 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT(); 7683 } else if (Opc == ISD::SRL) { 7684 // Another special-case: SRL is basically zero-extending a narrower value. 7685 ExtType = ISD::ZEXTLOAD; 7686 N0 = SDValue(N, 0); 7687 ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1)); 7688 if (!N01) return SDValue(); 7689 ExtVT = EVT::getIntegerVT(*DAG.getContext(), 7690 VT.getSizeInBits() - N01->getZExtValue()); 7691 } 7692 if (LegalOperations && !TLI.isLoadExtLegal(ExtType, VT, ExtVT)) 7693 return SDValue(); 7694 7695 unsigned EVTBits = ExtVT.getSizeInBits(); 7696 7697 // Do not generate loads of non-round integer types since these can 7698 // be expensive (and would be wrong if the type is not byte sized). 7699 if (!ExtVT.isRound()) 7700 return SDValue(); 7701 7702 unsigned ShAmt = 0; 7703 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) { 7704 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { 7705 ShAmt = N01->getZExtValue(); 7706 // Is the shift amount a multiple of size of VT? 7707 if ((ShAmt & (EVTBits-1)) == 0) { 7708 N0 = N0.getOperand(0); 7709 // Is the load width a multiple of size of VT? 7710 if ((N0.getValueSizeInBits() & (EVTBits-1)) != 0) 7711 return SDValue(); 7712 } 7713 7714 // At this point, we must have a load or else we can't do the transform. 7715 if (!isa<LoadSDNode>(N0)) return SDValue(); 7716 7717 // Because a SRL must be assumed to *need* to zero-extend the high bits 7718 // (as opposed to anyext the high bits), we can't combine the zextload 7719 // lowering of SRL and an sextload. 7720 if (cast<LoadSDNode>(N0)->getExtensionType() == ISD::SEXTLOAD) 7721 return SDValue(); 7722 7723 // If the shift amount is larger than the input type then we're not 7724 // accessing any of the loaded bytes. If the load was a zextload/extload 7725 // then the result of the shift+trunc is zero/undef (handled elsewhere). 7726 if (ShAmt >= cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits()) 7727 return SDValue(); 7728 } 7729 } 7730 7731 // If the load is shifted left (and the result isn't shifted back right), 7732 // we can fold the truncate through the shift. 7733 unsigned ShLeftAmt = 0; 7734 if (ShAmt == 0 && N0.getOpcode() == ISD::SHL && N0.hasOneUse() && 7735 ExtVT == VT && TLI.isNarrowingProfitable(N0.getValueType(), VT)) { 7736 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { 7737 ShLeftAmt = N01->getZExtValue(); 7738 N0 = N0.getOperand(0); 7739 } 7740 } 7741 7742 // If we haven't found a load, we can't narrow it. Don't transform one with 7743 // multiple uses, this would require adding a new load. 7744 if (!isa<LoadSDNode>(N0) || !N0.hasOneUse()) 7745 return SDValue(); 7746 7747 // Don't change the width of a volatile load. 7748 LoadSDNode *LN0 = cast<LoadSDNode>(N0); 7749 if (LN0->isVolatile()) 7750 return SDValue(); 7751 7752 // Verify that we are actually reducing a load width here. 7753 if (LN0->getMemoryVT().getSizeInBits() < EVTBits) 7754 return SDValue(); 7755 7756 // For the transform to be legal, the load must produce only two values 7757 // (the value loaded and the chain). Don't transform a pre-increment 7758 // load, for example, which produces an extra value. Otherwise the 7759 // transformation is not equivalent, and the downstream logic to replace 7760 // uses gets things wrong. 7761 if (LN0->getNumValues() > 2) 7762 return SDValue(); 7763 7764 // If the load that we're shrinking is an extload and we're not just 7765 // discarding the extension we can't simply shrink the load. Bail. 7766 // TODO: It would be possible to merge the extensions in some cases. 7767 if (LN0->getExtensionType() != ISD::NON_EXTLOAD && 7768 LN0->getMemoryVT().getSizeInBits() < ExtVT.getSizeInBits() + ShAmt) 7769 return SDValue(); 7770 7771 if (!TLI.shouldReduceLoadWidth(LN0, ExtType, ExtVT)) 7772 return SDValue(); 7773 7774 EVT PtrType = N0.getOperand(1).getValueType(); 7775 7776 if (PtrType == MVT::Untyped || PtrType.isExtended()) 7777 // It's not possible to generate a constant of extended or untyped type. 7778 return SDValue(); 7779 7780 // For big endian targets, we need to adjust the offset to the pointer to 7781 // load the correct bytes. 7782 if (DAG.getDataLayout().isBigEndian()) { 7783 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits(); 7784 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits(); 7785 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt; 7786 } 7787 7788 uint64_t PtrOff = ShAmt / 8; 7789 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff); 7790 SDLoc DL(LN0); 7791 // The original load itself didn't wrap, so an offset within it doesn't. 7792 SDNodeFlags Flags; 7793 Flags.setNoUnsignedWrap(true); 7794 SDValue NewPtr = DAG.getNode(ISD::ADD, DL, 7795 PtrType, LN0->getBasePtr(), 7796 DAG.getConstant(PtrOff, DL, PtrType), 7797 &Flags); 7798 AddToWorklist(NewPtr.getNode()); 7799 7800 SDValue Load; 7801 if (ExtType == ISD::NON_EXTLOAD) 7802 Load = DAG.getLoad(VT, SDLoc(N0), LN0->getChain(), NewPtr, 7803 LN0->getPointerInfo().getWithOffset(PtrOff), NewAlign, 7804 LN0->getMemOperand()->getFlags(), LN0->getAAInfo()); 7805 else 7806 Load = DAG.getExtLoad(ExtType, SDLoc(N0), VT, LN0->getChain(), NewPtr, 7807 LN0->getPointerInfo().getWithOffset(PtrOff), ExtVT, 7808 NewAlign, LN0->getMemOperand()->getFlags(), 7809 LN0->getAAInfo()); 7810 7811 // Replace the old load's chain with the new load's chain. 7812 WorklistRemover DeadNodes(*this); 7813 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1)); 7814 7815 // Shift the result left, if we've swallowed a left shift. 7816 SDValue Result = Load; 7817 if (ShLeftAmt != 0) { 7818 EVT ShImmTy = getShiftAmountTy(Result.getValueType()); 7819 if (!isUIntN(ShImmTy.getSizeInBits(), ShLeftAmt)) 7820 ShImmTy = VT; 7821 // If the shift amount is as large as the result size (but, presumably, 7822 // no larger than the source) then the useful bits of the result are 7823 // zero; we can't simply return the shortened shift, because the result 7824 // of that operation is undefined. 7825 SDLoc DL(N0); 7826 if (ShLeftAmt >= VT.getSizeInBits()) 7827 Result = DAG.getConstant(0, DL, VT); 7828 else 7829 Result = DAG.getNode(ISD::SHL, DL, VT, 7830 Result, DAG.getConstant(ShLeftAmt, DL, ShImmTy)); 7831 } 7832 7833 // Return the new loaded value. 7834 return Result; 7835 } 7836 7837 SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) { 7838 SDValue N0 = N->getOperand(0); 7839 SDValue N1 = N->getOperand(1); 7840 EVT VT = N->getValueType(0); 7841 EVT EVT = cast<VTSDNode>(N1)->getVT(); 7842 unsigned VTBits = VT.getScalarSizeInBits(); 7843 unsigned EVTBits = EVT.getScalarSizeInBits(); 7844 7845 if (N0.isUndef()) 7846 return DAG.getUNDEF(VT); 7847 7848 // fold (sext_in_reg c1) -> c1 7849 if (DAG.isConstantIntBuildVectorOrConstantInt(N0)) 7850 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, N0, N1); 7851 7852 // If the input is already sign extended, just drop the extension. 7853 if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1) 7854 return N0; 7855 7856 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2 7857 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG && 7858 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) 7859 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, 7860 N0.getOperand(0), N1); 7861 7862 // fold (sext_in_reg (sext x)) -> (sext x) 7863 // fold (sext_in_reg (aext x)) -> (sext x) 7864 // if x is small enough. 7865 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) { 7866 SDValue N00 = N0.getOperand(0); 7867 if (N00.getScalarValueSizeInBits() <= EVTBits && 7868 (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT))) 7869 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N00, N1); 7870 } 7871 7872 // fold (sext_in_reg (*_extend_vector_inreg x)) -> (sext_vector_in_reg x) 7873 if ((N0.getOpcode() == ISD::ANY_EXTEND_VECTOR_INREG || 7874 N0.getOpcode() == ISD::SIGN_EXTEND_VECTOR_INREG || 7875 N0.getOpcode() == ISD::ZERO_EXTEND_VECTOR_INREG) && 7876 N0.getOperand(0).getScalarValueSizeInBits() == EVTBits) { 7877 if (!LegalOperations || 7878 TLI.isOperationLegal(ISD::SIGN_EXTEND_VECTOR_INREG, VT)) 7879 return DAG.getSignExtendVectorInReg(N0.getOperand(0), SDLoc(N), VT); 7880 } 7881 7882 // fold (sext_in_reg (zext x)) -> (sext x) 7883 // iff we are extending the source sign bit. 7884 if (N0.getOpcode() == ISD::ZERO_EXTEND) { 7885 SDValue N00 = N0.getOperand(0); 7886 if (N00.getScalarValueSizeInBits() == EVTBits && 7887 (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT))) 7888 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N00, N1); 7889 } 7890 7891 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero. 7892 if (DAG.MaskedValueIsZero(N0, APInt::getOneBitSet(VTBits, EVTBits - 1))) 7893 return DAG.getZeroExtendInReg(N0, SDLoc(N), EVT.getScalarType()); 7894 7895 // fold operands of sext_in_reg based on knowledge that the top bits are not 7896 // demanded. 7897 if (SimplifyDemandedBits(SDValue(N, 0))) 7898 return SDValue(N, 0); 7899 7900 // fold (sext_in_reg (load x)) -> (smaller sextload x) 7901 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits)) 7902 if (SDValue NarrowLoad = ReduceLoadWidth(N)) 7903 return NarrowLoad; 7904 7905 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24) 7906 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible. 7907 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above. 7908 if (N0.getOpcode() == ISD::SRL) { 7909 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1))) 7910 if (ShAmt->getZExtValue()+EVTBits <= VTBits) { 7911 // We can turn this into an SRA iff the input to the SRL is already sign 7912 // extended enough. 7913 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0)); 7914 if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits) 7915 return DAG.getNode(ISD::SRA, SDLoc(N), VT, 7916 N0.getOperand(0), N0.getOperand(1)); 7917 } 7918 } 7919 7920 // fold (sext_inreg (extload x)) -> (sextload x) 7921 if (ISD::isEXTLoad(N0.getNode()) && 7922 ISD::isUNINDEXEDLoad(N0.getNode()) && 7923 EVT == cast<LoadSDNode>(N0)->getMemoryVT() && 7924 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || 7925 TLI.isLoadExtLegal(ISD::SEXTLOAD, VT, EVT))) { 7926 LoadSDNode *LN0 = cast<LoadSDNode>(N0); 7927 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT, 7928 LN0->getChain(), 7929 LN0->getBasePtr(), EVT, 7930 LN0->getMemOperand()); 7931 CombineTo(N, ExtLoad); 7932 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1)); 7933 AddToWorklist(ExtLoad.getNode()); 7934 return SDValue(N, 0); // Return N so it doesn't get rechecked! 7935 } 7936 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use 7937 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) && 7938 N0.hasOneUse() && 7939 EVT == cast<LoadSDNode>(N0)->getMemoryVT() && 7940 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || 7941 TLI.isLoadExtLegal(ISD::SEXTLOAD, VT, EVT))) { 7942 LoadSDNode *LN0 = cast<LoadSDNode>(N0); 7943 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT, 7944 LN0->getChain(), 7945 LN0->getBasePtr(), EVT, 7946 LN0->getMemOperand()); 7947 CombineTo(N, ExtLoad); 7948 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1)); 7949 return SDValue(N, 0); // Return N so it doesn't get rechecked! 7950 } 7951 7952 // Form (sext_inreg (bswap >> 16)) or (sext_inreg (rotl (bswap) 16)) 7953 if (EVTBits <= 16 && N0.getOpcode() == ISD::OR) { 7954 if (SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0), 7955 N0.getOperand(1), false)) 7956 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, 7957 BSwap, N1); 7958 } 7959 7960 return SDValue(); 7961 } 7962 7963 SDValue DAGCombiner::visitSIGN_EXTEND_VECTOR_INREG(SDNode *N) { 7964 SDValue N0 = N->getOperand(0); 7965 EVT VT = N->getValueType(0); 7966 7967 if (N0.isUndef()) 7968 return DAG.getUNDEF(VT); 7969 7970 if (SDNode *Res = tryToFoldExtendOfConstant(N, TLI, DAG, LegalTypes, 7971 LegalOperations)) 7972 return SDValue(Res, 0); 7973 7974 return SDValue(); 7975 } 7976 7977 SDValue DAGCombiner::visitZERO_EXTEND_VECTOR_INREG(SDNode *N) { 7978 SDValue N0 = N->getOperand(0); 7979 EVT VT = N->getValueType(0); 7980 7981 if (N0.isUndef()) 7982 return DAG.getUNDEF(VT); 7983 7984 if (SDNode *Res = tryToFoldExtendOfConstant(N, TLI, DAG, LegalTypes, 7985 LegalOperations)) 7986 return SDValue(Res, 0); 7987 7988 return SDValue(); 7989 } 7990 7991 SDValue DAGCombiner::visitTRUNCATE(SDNode *N) { 7992 SDValue N0 = N->getOperand(0); 7993 EVT VT = N->getValueType(0); 7994 bool isLE = DAG.getDataLayout().isLittleEndian(); 7995 7996 // noop truncate 7997 if (N0.getValueType() == N->getValueType(0)) 7998 return N0; 7999 // fold (truncate c1) -> c1 8000 if (DAG.isConstantIntBuildVectorOrConstantInt(N0)) 8001 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0); 8002 // fold (truncate (truncate x)) -> (truncate x) 8003 if (N0.getOpcode() == ISD::TRUNCATE) 8004 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0)); 8005 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x 8006 if (N0.getOpcode() == ISD::ZERO_EXTEND || 8007 N0.getOpcode() == ISD::SIGN_EXTEND || 8008 N0.getOpcode() == ISD::ANY_EXTEND) { 8009 // if the source is smaller than the dest, we still need an extend. 8010 if (N0.getOperand(0).getValueType().bitsLT(VT)) 8011 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, N0.getOperand(0)); 8012 // if the source is larger than the dest, than we just need the truncate. 8013 if (N0.getOperand(0).getValueType().bitsGT(VT)) 8014 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0)); 8015 // if the source and dest are the same type, we can drop both the extend 8016 // and the truncate. 8017 return N0.getOperand(0); 8018 } 8019 8020 // If this is anyext(trunc), don't fold it, allow ourselves to be folded. 8021 if (N->hasOneUse() && (N->use_begin()->getOpcode() == ISD::ANY_EXTEND)) 8022 return SDValue(); 8023 8024 // Fold extract-and-trunc into a narrow extract. For example: 8025 // i64 x = EXTRACT_VECTOR_ELT(v2i64 val, i32 1) 8026 // i32 y = TRUNCATE(i64 x) 8027 // -- becomes -- 8028 // v16i8 b = BITCAST (v2i64 val) 8029 // i8 x = EXTRACT_VECTOR_ELT(v16i8 b, i32 8) 8030 // 8031 // Note: We only run this optimization after type legalization (which often 8032 // creates this pattern) and before operation legalization after which 8033 // we need to be more careful about the vector instructions that we generate. 8034 if (N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT && 8035 LegalTypes && !LegalOperations && N0->hasOneUse() && VT != MVT::i1) { 8036 8037 EVT VecTy = N0.getOperand(0).getValueType(); 8038 EVT ExTy = N0.getValueType(); 8039 EVT TrTy = N->getValueType(0); 8040 8041 unsigned NumElem = VecTy.getVectorNumElements(); 8042 unsigned SizeRatio = ExTy.getSizeInBits()/TrTy.getSizeInBits(); 8043 8044 EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, SizeRatio * NumElem); 8045 assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size"); 8046 8047 SDValue EltNo = N0->getOperand(1); 8048 if (isa<ConstantSDNode>(EltNo) && isTypeLegal(NVT)) { 8049 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue(); 8050 EVT IndexTy = TLI.getVectorIdxTy(DAG.getDataLayout()); 8051 int Index = isLE ? (Elt*SizeRatio) : (Elt*SizeRatio + (SizeRatio-1)); 8052 8053 SDLoc DL(N); 8054 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, TrTy, 8055 DAG.getBitcast(NVT, N0.getOperand(0)), 8056 DAG.getConstant(Index, DL, IndexTy)); 8057 } 8058 } 8059 8060 // trunc (select c, a, b) -> select c, (trunc a), (trunc b) 8061 if (N0.getOpcode() == ISD::SELECT && N0.hasOneUse()) { 8062 EVT SrcVT = N0.getValueType(); 8063 if ((!LegalOperations || TLI.isOperationLegal(ISD::SELECT, SrcVT)) && 8064 TLI.isTruncateFree(SrcVT, VT)) { 8065 SDLoc SL(N0); 8066 SDValue Cond = N0.getOperand(0); 8067 SDValue TruncOp0 = DAG.getNode(ISD::TRUNCATE, SL, VT, N0.getOperand(1)); 8068 SDValue TruncOp1 = DAG.getNode(ISD::TRUNCATE, SL, VT, N0.getOperand(2)); 8069 return DAG.getNode(ISD::SELECT, SDLoc(N), VT, Cond, TruncOp0, TruncOp1); 8070 } 8071 } 8072 8073 // trunc (shl x, K) -> shl (trunc x), K => K < VT.getScalarSizeInBits() 8074 if (N0.getOpcode() == ISD::SHL && N0.hasOneUse() && 8075 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::SHL, VT)) && 8076 TLI.isTypeDesirableForOp(ISD::SHL, VT)) { 8077 if (const ConstantSDNode *CAmt = isConstOrConstSplat(N0.getOperand(1))) { 8078 uint64_t Amt = CAmt->getZExtValue(); 8079 unsigned Size = VT.getScalarSizeInBits(); 8080 8081 if (Amt < Size) { 8082 SDLoc SL(N); 8083 EVT AmtVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout()); 8084 8085 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SL, VT, N0.getOperand(0)); 8086 return DAG.getNode(ISD::SHL, SL, VT, Trunc, 8087 DAG.getConstant(Amt, SL, AmtVT)); 8088 } 8089 } 8090 } 8091 8092 // Fold a series of buildvector, bitcast, and truncate if possible. 8093 // For example fold 8094 // (2xi32 trunc (bitcast ((4xi32)buildvector x, x, y, y) 2xi64)) to 8095 // (2xi32 (buildvector x, y)). 8096 if (Level == AfterLegalizeVectorOps && VT.isVector() && 8097 N0.getOpcode() == ISD::BITCAST && N0.hasOneUse() && 8098 N0.getOperand(0).getOpcode() == ISD::BUILD_VECTOR && 8099 N0.getOperand(0).hasOneUse()) { 8100 8101 SDValue BuildVect = N0.getOperand(0); 8102 EVT BuildVectEltTy = BuildVect.getValueType().getVectorElementType(); 8103 EVT TruncVecEltTy = VT.getVectorElementType(); 8104 8105 // Check that the element types match. 8106 if (BuildVectEltTy == TruncVecEltTy) { 8107 // Now we only need to compute the offset of the truncated elements. 8108 unsigned BuildVecNumElts = BuildVect.getNumOperands(); 8109 unsigned TruncVecNumElts = VT.getVectorNumElements(); 8110 unsigned TruncEltOffset = BuildVecNumElts / TruncVecNumElts; 8111 8112 assert((BuildVecNumElts % TruncVecNumElts) == 0 && 8113 "Invalid number of elements"); 8114 8115 SmallVector<SDValue, 8> Opnds; 8116 for (unsigned i = 0, e = BuildVecNumElts; i != e; i += TruncEltOffset) 8117 Opnds.push_back(BuildVect.getOperand(i)); 8118 8119 return DAG.getBuildVector(VT, SDLoc(N), Opnds); 8120 } 8121 } 8122 8123 // See if we can simplify the input to this truncate through knowledge that 8124 // only the low bits are being used. 8125 // For example "trunc (or (shl x, 8), y)" // -> trunc y 8126 // Currently we only perform this optimization on scalars because vectors 8127 // may have different active low bits. 8128 if (!VT.isVector()) { 8129 if (SDValue Shorter = 8130 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(), 8131 VT.getSizeInBits()))) 8132 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Shorter); 8133 } 8134 8135 // fold (truncate (load x)) -> (smaller load x) 8136 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits)) 8137 if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT)) { 8138 if (SDValue Reduced = ReduceLoadWidth(N)) 8139 return Reduced; 8140 8141 // Handle the case where the load remains an extending load even 8142 // after truncation. 8143 if (N0.hasOneUse() && ISD::isUNINDEXEDLoad(N0.getNode())) { 8144 LoadSDNode *LN0 = cast<LoadSDNode>(N0); 8145 if (!LN0->isVolatile() && 8146 LN0->getMemoryVT().getStoreSizeInBits() < VT.getSizeInBits()) { 8147 SDValue NewLoad = DAG.getExtLoad(LN0->getExtensionType(), SDLoc(LN0), 8148 VT, LN0->getChain(), LN0->getBasePtr(), 8149 LN0->getMemoryVT(), 8150 LN0->getMemOperand()); 8151 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLoad.getValue(1)); 8152 return NewLoad; 8153 } 8154 } 8155 } 8156 8157 // fold (trunc (concat ... x ...)) -> (concat ..., (trunc x), ...)), 8158 // where ... are all 'undef'. 8159 if (N0.getOpcode() == ISD::CONCAT_VECTORS && !LegalTypes) { 8160 SmallVector<EVT, 8> VTs; 8161 SDValue V; 8162 unsigned Idx = 0; 8163 unsigned NumDefs = 0; 8164 8165 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) { 8166 SDValue X = N0.getOperand(i); 8167 if (!X.isUndef()) { 8168 V = X; 8169 Idx = i; 8170 NumDefs++; 8171 } 8172 // Stop if more than one members are non-undef. 8173 if (NumDefs > 1) 8174 break; 8175 VTs.push_back(EVT::getVectorVT(*DAG.getContext(), 8176 VT.getVectorElementType(), 8177 X.getValueType().getVectorNumElements())); 8178 } 8179 8180 if (NumDefs == 0) 8181 return DAG.getUNDEF(VT); 8182 8183 if (NumDefs == 1) { 8184 assert(V.getNode() && "The single defined operand is empty!"); 8185 SmallVector<SDValue, 8> Opnds; 8186 for (unsigned i = 0, e = VTs.size(); i != e; ++i) { 8187 if (i != Idx) { 8188 Opnds.push_back(DAG.getUNDEF(VTs[i])); 8189 continue; 8190 } 8191 SDValue NV = DAG.getNode(ISD::TRUNCATE, SDLoc(V), VTs[i], V); 8192 AddToWorklist(NV.getNode()); 8193 Opnds.push_back(NV); 8194 } 8195 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, Opnds); 8196 } 8197 } 8198 8199 // Fold truncate of a bitcast of a vector to an extract of the low vector 8200 // element. 8201 // 8202 // e.g. trunc (i64 (bitcast v2i32:x)) -> extract_vector_elt v2i32:x, 0 8203 if (N0.getOpcode() == ISD::BITCAST && !VT.isVector()) { 8204 SDValue VecSrc = N0.getOperand(0); 8205 EVT SrcVT = VecSrc.getValueType(); 8206 if (SrcVT.isVector() && SrcVT.getScalarType() == VT && 8207 (!LegalOperations || 8208 TLI.isOperationLegal(ISD::EXTRACT_VECTOR_ELT, SrcVT))) { 8209 SDLoc SL(N); 8210 8211 EVT IdxVT = TLI.getVectorIdxTy(DAG.getDataLayout()); 8212 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, VT, 8213 VecSrc, DAG.getConstant(0, SL, IdxVT)); 8214 } 8215 } 8216 8217 // Simplify the operands using demanded-bits information. 8218 if (!VT.isVector() && 8219 SimplifyDemandedBits(SDValue(N, 0))) 8220 return SDValue(N, 0); 8221 8222 // (trunc adde(X, Y, Carry)) -> (adde trunc(X), trunc(Y), Carry) 8223 // When the adde's carry is not used. 8224 if (N0.getOpcode() == ISD::ADDE && N0.hasOneUse() && 8225 !N0.getNode()->hasAnyUseOfValue(1) && 8226 (!LegalOperations || TLI.isOperationLegal(ISD::ADDE, VT))) { 8227 SDLoc SL(N); 8228 auto X = DAG.getNode(ISD::TRUNCATE, SL, VT, N0.getOperand(0)); 8229 auto Y = DAG.getNode(ISD::TRUNCATE, SL, VT, N0.getOperand(1)); 8230 return DAG.getNode(ISD::ADDE, SL, DAG.getVTList(VT, MVT::Glue), 8231 X, Y, N0.getOperand(2)); 8232 } 8233 8234 return SDValue(); 8235 } 8236 8237 static SDNode *getBuildPairElt(SDNode *N, unsigned i) { 8238 SDValue Elt = N->getOperand(i); 8239 if (Elt.getOpcode() != ISD::MERGE_VALUES) 8240 return Elt.getNode(); 8241 return Elt.getOperand(Elt.getResNo()).getNode(); 8242 } 8243 8244 /// build_pair (load, load) -> load 8245 /// if load locations are consecutive. 8246 SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) { 8247 assert(N->getOpcode() == ISD::BUILD_PAIR); 8248 8249 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0)); 8250 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1)); 8251 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse() || 8252 LD1->getAddressSpace() != LD2->getAddressSpace()) 8253 return SDValue(); 8254 EVT LD1VT = LD1->getValueType(0); 8255 unsigned LD1Bytes = LD1VT.getSizeInBits() / 8; 8256 if (ISD::isNON_EXTLoad(LD2) && LD2->hasOneUse() && 8257 DAG.areNonVolatileConsecutiveLoads(LD2, LD1, LD1Bytes, 1)) { 8258 unsigned Align = LD1->getAlignment(); 8259 unsigned NewAlign = DAG.getDataLayout().getABITypeAlignment( 8260 VT.getTypeForEVT(*DAG.getContext())); 8261 8262 if (NewAlign <= Align && 8263 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) 8264 return DAG.getLoad(VT, SDLoc(N), LD1->getChain(), LD1->getBasePtr(), 8265 LD1->getPointerInfo(), Align); 8266 } 8267 8268 return SDValue(); 8269 } 8270 8271 static unsigned getPPCf128HiElementSelector(const SelectionDAG &DAG) { 8272 // On little-endian machines, bitcasting from ppcf128 to i128 does swap the Hi 8273 // and Lo parts; on big-endian machines it doesn't. 8274 return DAG.getDataLayout().isBigEndian() ? 1 : 0; 8275 } 8276 8277 static SDValue foldBitcastedFPLogic(SDNode *N, SelectionDAG &DAG, 8278 const TargetLowering &TLI) { 8279 // If this is not a bitcast to an FP type or if the target doesn't have 8280 // IEEE754-compliant FP logic, we're done. 8281 EVT VT = N->getValueType(0); 8282 if (!VT.isFloatingPoint() || !TLI.hasBitPreservingFPLogic(VT)) 8283 return SDValue(); 8284 8285 // TODO: Use splat values for the constant-checking below and remove this 8286 // restriction. 8287 SDValue N0 = N->getOperand(0); 8288 EVT SourceVT = N0.getValueType(); 8289 if (SourceVT.isVector()) 8290 return SDValue(); 8291 8292 unsigned FPOpcode; 8293 APInt SignMask; 8294 switch (N0.getOpcode()) { 8295 case ISD::AND: 8296 FPOpcode = ISD::FABS; 8297 SignMask = ~APInt::getSignBit(SourceVT.getSizeInBits()); 8298 break; 8299 case ISD::XOR: 8300 FPOpcode = ISD::FNEG; 8301 SignMask = APInt::getSignBit(SourceVT.getSizeInBits()); 8302 break; 8303 // TODO: ISD::OR --> ISD::FNABS? 8304 default: 8305 return SDValue(); 8306 } 8307 8308 // Fold (bitcast int (and (bitcast fp X to int), 0x7fff...) to fp) -> fabs X 8309 // Fold (bitcast int (xor (bitcast fp X to int), 0x8000...) to fp) -> fneg X 8310 SDValue LogicOp0 = N0.getOperand(0); 8311 ConstantSDNode *LogicOp1 = dyn_cast<ConstantSDNode>(N0.getOperand(1)); 8312 if (LogicOp1 && LogicOp1->getAPIntValue() == SignMask && 8313 LogicOp0.getOpcode() == ISD::BITCAST && 8314 LogicOp0->getOperand(0).getValueType() == VT) 8315 return DAG.getNode(FPOpcode, SDLoc(N), VT, LogicOp0->getOperand(0)); 8316 8317 return SDValue(); 8318 } 8319 8320 SDValue DAGCombiner::visitBITCAST(SDNode *N) { 8321 SDValue N0 = N->getOperand(0); 8322 EVT VT = N->getValueType(0); 8323 8324 if (N0.isUndef()) 8325 return DAG.getUNDEF(VT); 8326 8327 // If the input is a BUILD_VECTOR with all constant elements, fold this now. 8328 // Only do this before legalize, since afterward the target may be depending 8329 // on the bitconvert. 8330 // First check to see if this is all constant. 8331 if (!LegalTypes && 8332 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() && 8333 VT.isVector()) { 8334 bool isSimple = cast<BuildVectorSDNode>(N0)->isConstant(); 8335 8336 EVT DestEltVT = N->getValueType(0).getVectorElementType(); 8337 assert(!DestEltVT.isVector() && 8338 "Element type of vector ValueType must not be vector!"); 8339 if (isSimple) 8340 return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(), DestEltVT); 8341 } 8342 8343 // If the input is a constant, let getNode fold it. 8344 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) { 8345 // If we can't allow illegal operations, we need to check that this is just 8346 // a fp -> int or int -> conversion and that the resulting operation will 8347 // be legal. 8348 if (!LegalOperations || 8349 (isa<ConstantSDNode>(N0) && VT.isFloatingPoint() && !VT.isVector() && 8350 TLI.isOperationLegal(ISD::ConstantFP, VT)) || 8351 (isa<ConstantFPSDNode>(N0) && VT.isInteger() && !VT.isVector() && 8352 TLI.isOperationLegal(ISD::Constant, VT))) 8353 return DAG.getBitcast(VT, N0); 8354 } 8355 8356 // (conv (conv x, t1), t2) -> (conv x, t2) 8357 if (N0.getOpcode() == ISD::BITCAST) 8358 return DAG.getBitcast(VT, N0.getOperand(0)); 8359 8360 // fold (conv (load x)) -> (load (conv*)x) 8361 // If the resultant load doesn't need a higher alignment than the original! 8362 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() && 8363 // Do not change the width of a volatile load. 8364 !cast<LoadSDNode>(N0)->isVolatile() && 8365 // Do not remove the cast if the types differ in endian layout. 8366 TLI.hasBigEndianPartOrdering(N0.getValueType(), DAG.getDataLayout()) == 8367 TLI.hasBigEndianPartOrdering(VT, DAG.getDataLayout()) && 8368 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)) && 8369 TLI.isLoadBitCastBeneficial(N0.getValueType(), VT)) { 8370 LoadSDNode *LN0 = cast<LoadSDNode>(N0); 8371 unsigned OrigAlign = LN0->getAlignment(); 8372 8373 bool Fast = false; 8374 if (TLI.allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), VT, 8375 LN0->getAddressSpace(), OrigAlign, &Fast) && 8376 Fast) { 8377 SDValue Load = 8378 DAG.getLoad(VT, SDLoc(N), LN0->getChain(), LN0->getBasePtr(), 8379 LN0->getPointerInfo(), OrigAlign, 8380 LN0->getMemOperand()->getFlags(), LN0->getAAInfo()); 8381 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1)); 8382 return Load; 8383 } 8384 } 8385 8386 if (SDValue V = foldBitcastedFPLogic(N, DAG, TLI)) 8387 return V; 8388 8389 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit) 8390 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit)) 8391 // 8392 // For ppc_fp128: 8393 // fold (bitcast (fneg x)) -> 8394 // flipbit = signbit 8395 // (xor (bitcast x) (build_pair flipbit, flipbit)) 8396 // 8397 // fold (bitcast (fabs x)) -> 8398 // flipbit = (and (extract_element (bitcast x), 0), signbit) 8399 // (xor (bitcast x) (build_pair flipbit, flipbit)) 8400 // This often reduces constant pool loads. 8401 if (((N0.getOpcode() == ISD::FNEG && !TLI.isFNegFree(N0.getValueType())) || 8402 (N0.getOpcode() == ISD::FABS && !TLI.isFAbsFree(N0.getValueType()))) && 8403 N0.getNode()->hasOneUse() && VT.isInteger() && 8404 !VT.isVector() && !N0.getValueType().isVector()) { 8405 SDValue NewConv = DAG.getBitcast(VT, N0.getOperand(0)); 8406 AddToWorklist(NewConv.getNode()); 8407 8408 SDLoc DL(N); 8409 if (N0.getValueType() == MVT::ppcf128 && !LegalTypes) { 8410 assert(VT.getSizeInBits() == 128); 8411 SDValue SignBit = DAG.getConstant( 8412 APInt::getSignBit(VT.getSizeInBits() / 2), SDLoc(N0), MVT::i64); 8413 SDValue FlipBit; 8414 if (N0.getOpcode() == ISD::FNEG) { 8415 FlipBit = SignBit; 8416 AddToWorklist(FlipBit.getNode()); 8417 } else { 8418 assert(N0.getOpcode() == ISD::FABS); 8419 SDValue Hi = 8420 DAG.getNode(ISD::EXTRACT_ELEMENT, SDLoc(NewConv), MVT::i64, NewConv, 8421 DAG.getIntPtrConstant(getPPCf128HiElementSelector(DAG), 8422 SDLoc(NewConv))); 8423 AddToWorklist(Hi.getNode()); 8424 FlipBit = DAG.getNode(ISD::AND, SDLoc(N0), MVT::i64, Hi, SignBit); 8425 AddToWorklist(FlipBit.getNode()); 8426 } 8427 SDValue FlipBits = 8428 DAG.getNode(ISD::BUILD_PAIR, SDLoc(N0), VT, FlipBit, FlipBit); 8429 AddToWorklist(FlipBits.getNode()); 8430 return DAG.getNode(ISD::XOR, DL, VT, NewConv, FlipBits); 8431 } 8432 APInt SignBit = APInt::getSignBit(VT.getSizeInBits()); 8433 if (N0.getOpcode() == ISD::FNEG) 8434 return DAG.getNode(ISD::XOR, DL, VT, 8435 NewConv, DAG.getConstant(SignBit, DL, VT)); 8436 assert(N0.getOpcode() == ISD::FABS); 8437 return DAG.getNode(ISD::AND, DL, VT, 8438 NewConv, DAG.getConstant(~SignBit, DL, VT)); 8439 } 8440 8441 // fold (bitconvert (fcopysign cst, x)) -> 8442 // (or (and (bitconvert x), sign), (and cst, (not sign))) 8443 // Note that we don't handle (copysign x, cst) because this can always be 8444 // folded to an fneg or fabs. 8445 // 8446 // For ppc_fp128: 8447 // fold (bitcast (fcopysign cst, x)) -> 8448 // flipbit = (and (extract_element 8449 // (xor (bitcast cst), (bitcast x)), 0), 8450 // signbit) 8451 // (xor (bitcast cst) (build_pair flipbit, flipbit)) 8452 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() && 8453 isa<ConstantFPSDNode>(N0.getOperand(0)) && 8454 VT.isInteger() && !VT.isVector()) { 8455 unsigned OrigXWidth = N0.getOperand(1).getValueSizeInBits(); 8456 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth); 8457 if (isTypeLegal(IntXVT)) { 8458 SDValue X = DAG.getBitcast(IntXVT, N0.getOperand(1)); 8459 AddToWorklist(X.getNode()); 8460 8461 // If X has a different width than the result/lhs, sext it or truncate it. 8462 unsigned VTWidth = VT.getSizeInBits(); 8463 if (OrigXWidth < VTWidth) { 8464 X = DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, X); 8465 AddToWorklist(X.getNode()); 8466 } else if (OrigXWidth > VTWidth) { 8467 // To get the sign bit in the right place, we have to shift it right 8468 // before truncating. 8469 SDLoc DL(X); 8470 X = DAG.getNode(ISD::SRL, DL, 8471 X.getValueType(), X, 8472 DAG.getConstant(OrigXWidth-VTWidth, DL, 8473 X.getValueType())); 8474 AddToWorklist(X.getNode()); 8475 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X); 8476 AddToWorklist(X.getNode()); 8477 } 8478 8479 if (N0.getValueType() == MVT::ppcf128 && !LegalTypes) { 8480 APInt SignBit = APInt::getSignBit(VT.getSizeInBits() / 2); 8481 SDValue Cst = DAG.getBitcast(VT, N0.getOperand(0)); 8482 AddToWorklist(Cst.getNode()); 8483 SDValue X = DAG.getBitcast(VT, N0.getOperand(1)); 8484 AddToWorklist(X.getNode()); 8485 SDValue XorResult = DAG.getNode(ISD::XOR, SDLoc(N0), VT, Cst, X); 8486 AddToWorklist(XorResult.getNode()); 8487 SDValue XorResult64 = DAG.getNode( 8488 ISD::EXTRACT_ELEMENT, SDLoc(XorResult), MVT::i64, XorResult, 8489 DAG.getIntPtrConstant(getPPCf128HiElementSelector(DAG), 8490 SDLoc(XorResult))); 8491 AddToWorklist(XorResult64.getNode()); 8492 SDValue FlipBit = 8493 DAG.getNode(ISD::AND, SDLoc(XorResult64), MVT::i64, XorResult64, 8494 DAG.getConstant(SignBit, SDLoc(XorResult64), MVT::i64)); 8495 AddToWorklist(FlipBit.getNode()); 8496 SDValue FlipBits = 8497 DAG.getNode(ISD::BUILD_PAIR, SDLoc(N0), VT, FlipBit, FlipBit); 8498 AddToWorklist(FlipBits.getNode()); 8499 return DAG.getNode(ISD::XOR, SDLoc(N), VT, Cst, FlipBits); 8500 } 8501 APInt SignBit = APInt::getSignBit(VT.getSizeInBits()); 8502 X = DAG.getNode(ISD::AND, SDLoc(X), VT, 8503 X, DAG.getConstant(SignBit, SDLoc(X), VT)); 8504 AddToWorklist(X.getNode()); 8505 8506 SDValue Cst = DAG.getBitcast(VT, N0.getOperand(0)); 8507 Cst = DAG.getNode(ISD::AND, SDLoc(Cst), VT, 8508 Cst, DAG.getConstant(~SignBit, SDLoc(Cst), VT)); 8509 AddToWorklist(Cst.getNode()); 8510 8511 return DAG.getNode(ISD::OR, SDLoc(N), VT, X, Cst); 8512 } 8513 } 8514 8515 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive. 8516 if (N0.getOpcode() == ISD::BUILD_PAIR) 8517 if (SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT)) 8518 return CombineLD; 8519 8520 // Remove double bitcasts from shuffles - this is often a legacy of 8521 // XformToShuffleWithZero being used to combine bitmaskings (of 8522 // float vectors bitcast to integer vectors) into shuffles. 8523 // bitcast(shuffle(bitcast(s0),bitcast(s1))) -> shuffle(s0,s1) 8524 if (Level < AfterLegalizeDAG && TLI.isTypeLegal(VT) && VT.isVector() && 8525 N0->getOpcode() == ISD::VECTOR_SHUFFLE && 8526 VT.getVectorNumElements() >= N0.getValueType().getVectorNumElements() && 8527 !(VT.getVectorNumElements() % N0.getValueType().getVectorNumElements())) { 8528 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N0); 8529 8530 // If operands are a bitcast, peek through if it casts the original VT. 8531 // If operands are a constant, just bitcast back to original VT. 8532 auto PeekThroughBitcast = [&](SDValue Op) { 8533 if (Op.getOpcode() == ISD::BITCAST && 8534 Op.getOperand(0).getValueType() == VT) 8535 return SDValue(Op.getOperand(0)); 8536 if (ISD::isBuildVectorOfConstantSDNodes(Op.getNode()) || 8537 ISD::isBuildVectorOfConstantFPSDNodes(Op.getNode())) 8538 return DAG.getBitcast(VT, Op); 8539 return SDValue(); 8540 }; 8541 8542 SDValue SV0 = PeekThroughBitcast(N0->getOperand(0)); 8543 SDValue SV1 = PeekThroughBitcast(N0->getOperand(1)); 8544 if (!(SV0 && SV1)) 8545 return SDValue(); 8546 8547 int MaskScale = 8548 VT.getVectorNumElements() / N0.getValueType().getVectorNumElements(); 8549 SmallVector<int, 8> NewMask; 8550 for (int M : SVN->getMask()) 8551 for (int i = 0; i != MaskScale; ++i) 8552 NewMask.push_back(M < 0 ? -1 : M * MaskScale + i); 8553 8554 bool LegalMask = TLI.isShuffleMaskLegal(NewMask, VT); 8555 if (!LegalMask) { 8556 std::swap(SV0, SV1); 8557 ShuffleVectorSDNode::commuteMask(NewMask); 8558 LegalMask = TLI.isShuffleMaskLegal(NewMask, VT); 8559 } 8560 8561 if (LegalMask) 8562 return DAG.getVectorShuffle(VT, SDLoc(N), SV0, SV1, NewMask); 8563 } 8564 8565 return SDValue(); 8566 } 8567 8568 SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) { 8569 EVT VT = N->getValueType(0); 8570 return CombineConsecutiveLoads(N, VT); 8571 } 8572 8573 /// We know that BV is a build_vector node with Constant, ConstantFP or Undef 8574 /// operands. DstEltVT indicates the destination element value type. 8575 SDValue DAGCombiner:: 8576 ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) { 8577 EVT SrcEltVT = BV->getValueType(0).getVectorElementType(); 8578 8579 // If this is already the right type, we're done. 8580 if (SrcEltVT == DstEltVT) return SDValue(BV, 0); 8581 8582 unsigned SrcBitSize = SrcEltVT.getSizeInBits(); 8583 unsigned DstBitSize = DstEltVT.getSizeInBits(); 8584 8585 // If this is a conversion of N elements of one type to N elements of another 8586 // type, convert each element. This handles FP<->INT cases. 8587 if (SrcBitSize == DstBitSize) { 8588 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, 8589 BV->getValueType(0).getVectorNumElements()); 8590 8591 // Due to the FP element handling below calling this routine recursively, 8592 // we can end up with a scalar-to-vector node here. 8593 if (BV->getOpcode() == ISD::SCALAR_TO_VECTOR) 8594 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT, 8595 DAG.getBitcast(DstEltVT, BV->getOperand(0))); 8596 8597 SmallVector<SDValue, 8> Ops; 8598 for (SDValue Op : BV->op_values()) { 8599 // If the vector element type is not legal, the BUILD_VECTOR operands 8600 // are promoted and implicitly truncated. Make that explicit here. 8601 if (Op.getValueType() != SrcEltVT) 8602 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(BV), SrcEltVT, Op); 8603 Ops.push_back(DAG.getBitcast(DstEltVT, Op)); 8604 AddToWorklist(Ops.back().getNode()); 8605 } 8606 return DAG.getBuildVector(VT, SDLoc(BV), Ops); 8607 } 8608 8609 // Otherwise, we're growing or shrinking the elements. To avoid having to 8610 // handle annoying details of growing/shrinking FP values, we convert them to 8611 // int first. 8612 if (SrcEltVT.isFloatingPoint()) { 8613 // Convert the input float vector to a int vector where the elements are the 8614 // same sizes. 8615 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits()); 8616 BV = ConstantFoldBITCASTofBUILD_VECTOR(BV, IntVT).getNode(); 8617 SrcEltVT = IntVT; 8618 } 8619 8620 // Now we know the input is an integer vector. If the output is a FP type, 8621 // convert to integer first, then to FP of the right size. 8622 if (DstEltVT.isFloatingPoint()) { 8623 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits()); 8624 SDNode *Tmp = ConstantFoldBITCASTofBUILD_VECTOR(BV, TmpVT).getNode(); 8625 8626 // Next, convert to FP elements of the same size. 8627 return ConstantFoldBITCASTofBUILD_VECTOR(Tmp, DstEltVT); 8628 } 8629 8630 SDLoc DL(BV); 8631 8632 // Okay, we know the src/dst types are both integers of differing types. 8633 // Handling growing first. 8634 assert(SrcEltVT.isInteger() && DstEltVT.isInteger()); 8635 if (SrcBitSize < DstBitSize) { 8636 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize; 8637 8638 SmallVector<SDValue, 8> Ops; 8639 for (unsigned i = 0, e = BV->getNumOperands(); i != e; 8640 i += NumInputsPerOutput) { 8641 bool isLE = DAG.getDataLayout().isLittleEndian(); 8642 APInt NewBits = APInt(DstBitSize, 0); 8643 bool EltIsUndef = true; 8644 for (unsigned j = 0; j != NumInputsPerOutput; ++j) { 8645 // Shift the previously computed bits over. 8646 NewBits <<= SrcBitSize; 8647 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j)); 8648 if (Op.isUndef()) continue; 8649 EltIsUndef = false; 8650 8651 NewBits |= cast<ConstantSDNode>(Op)->getAPIntValue(). 8652 zextOrTrunc(SrcBitSize).zext(DstBitSize); 8653 } 8654 8655 if (EltIsUndef) 8656 Ops.push_back(DAG.getUNDEF(DstEltVT)); 8657 else 8658 Ops.push_back(DAG.getConstant(NewBits, DL, DstEltVT)); 8659 } 8660 8661 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size()); 8662 return DAG.getBuildVector(VT, DL, Ops); 8663 } 8664 8665 // Finally, this must be the case where we are shrinking elements: each input 8666 // turns into multiple outputs. 8667 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize; 8668 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, 8669 NumOutputsPerInput*BV->getNumOperands()); 8670 SmallVector<SDValue, 8> Ops; 8671 8672 for (const SDValue &Op : BV->op_values()) { 8673 if (Op.isUndef()) { 8674 Ops.append(NumOutputsPerInput, DAG.getUNDEF(DstEltVT)); 8675 continue; 8676 } 8677 8678 APInt OpVal = cast<ConstantSDNode>(Op)-> 8679 getAPIntValue().zextOrTrunc(SrcBitSize); 8680 8681 for (unsigned j = 0; j != NumOutputsPerInput; ++j) { 8682 APInt ThisVal = OpVal.trunc(DstBitSize); 8683 Ops.push_back(DAG.getConstant(ThisVal, DL, DstEltVT)); 8684 OpVal = OpVal.lshr(DstBitSize); 8685 } 8686 8687 // For big endian targets, swap the order of the pieces of each element. 8688 if (DAG.getDataLayout().isBigEndian()) 8689 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end()); 8690 } 8691 8692 return DAG.getBuildVector(VT, DL, Ops); 8693 } 8694 8695 /// Try to perform FMA combining on a given FADD node. 8696 SDValue DAGCombiner::visitFADDForFMACombine(SDNode *N) { 8697 SDValue N0 = N->getOperand(0); 8698 SDValue N1 = N->getOperand(1); 8699 EVT VT = N->getValueType(0); 8700 SDLoc SL(N); 8701 8702 const TargetOptions &Options = DAG.getTarget().Options; 8703 bool AllowFusion = 8704 (Options.AllowFPOpFusion == FPOpFusion::Fast || Options.UnsafeFPMath); 8705 8706 // Floating-point multiply-add with intermediate rounding. 8707 bool HasFMAD = (LegalOperations && TLI.isOperationLegal(ISD::FMAD, VT)); 8708 8709 // Floating-point multiply-add without intermediate rounding. 8710 bool HasFMA = 8711 AllowFusion && TLI.isFMAFasterThanFMulAndFAdd(VT) && 8712 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT)); 8713 8714 // No valid opcode, do not combine. 8715 if (!HasFMAD && !HasFMA) 8716 return SDValue(); 8717 8718 const SelectionDAGTargetInfo *STI = DAG.getSubtarget().getSelectionDAGInfo(); 8719 ; 8720 if (AllowFusion && STI && STI->generateFMAsInMachineCombiner(OptLevel)) 8721 return SDValue(); 8722 8723 // Always prefer FMAD to FMA for precision. 8724 unsigned PreferredFusedOpcode = HasFMAD ? ISD::FMAD : ISD::FMA; 8725 bool Aggressive = TLI.enableAggressiveFMAFusion(VT); 8726 bool LookThroughFPExt = TLI.isFPExtFree(VT); 8727 8728 // If we have two choices trying to fold (fadd (fmul u, v), (fmul x, y)), 8729 // prefer to fold the multiply with fewer uses. 8730 if (Aggressive && N0.getOpcode() == ISD::FMUL && 8731 N1.getOpcode() == ISD::FMUL) { 8732 if (N0.getNode()->use_size() > N1.getNode()->use_size()) 8733 std::swap(N0, N1); 8734 } 8735 8736 // fold (fadd (fmul x, y), z) -> (fma x, y, z) 8737 if (N0.getOpcode() == ISD::FMUL && 8738 (Aggressive || N0->hasOneUse())) { 8739 return DAG.getNode(PreferredFusedOpcode, SL, VT, 8740 N0.getOperand(0), N0.getOperand(1), N1); 8741 } 8742 8743 // fold (fadd x, (fmul y, z)) -> (fma y, z, x) 8744 // Note: Commutes FADD operands. 8745 if (N1.getOpcode() == ISD::FMUL && 8746 (Aggressive || N1->hasOneUse())) { 8747 return DAG.getNode(PreferredFusedOpcode, SL, VT, 8748 N1.getOperand(0), N1.getOperand(1), N0); 8749 } 8750 8751 // Look through FP_EXTEND nodes to do more combining. 8752 if (AllowFusion && LookThroughFPExt) { 8753 // fold (fadd (fpext (fmul x, y)), z) -> (fma (fpext x), (fpext y), z) 8754 if (N0.getOpcode() == ISD::FP_EXTEND) { 8755 SDValue N00 = N0.getOperand(0); 8756 if (N00.getOpcode() == ISD::FMUL) 8757 return DAG.getNode(PreferredFusedOpcode, SL, VT, 8758 DAG.getNode(ISD::FP_EXTEND, SL, VT, 8759 N00.getOperand(0)), 8760 DAG.getNode(ISD::FP_EXTEND, SL, VT, 8761 N00.getOperand(1)), N1); 8762 } 8763 8764 // fold (fadd x, (fpext (fmul y, z))) -> (fma (fpext y), (fpext z), x) 8765 // Note: Commutes FADD operands. 8766 if (N1.getOpcode() == ISD::FP_EXTEND) { 8767 SDValue N10 = N1.getOperand(0); 8768 if (N10.getOpcode() == ISD::FMUL) 8769 return DAG.getNode(PreferredFusedOpcode, SL, VT, 8770 DAG.getNode(ISD::FP_EXTEND, SL, VT, 8771 N10.getOperand(0)), 8772 DAG.getNode(ISD::FP_EXTEND, SL, VT, 8773 N10.getOperand(1)), N0); 8774 } 8775 } 8776 8777 // More folding opportunities when target permits. 8778 if (Aggressive) { 8779 // fold (fadd (fma x, y, (fmul u, v)), z) -> (fma x, y (fma u, v, z)) 8780 // FIXME: The UnsafeAlgebra flag should be propagated to FMA/FMAD, but FMF 8781 // are currently only supported on binary nodes. 8782 if (Options.UnsafeFPMath && 8783 N0.getOpcode() == PreferredFusedOpcode && 8784 N0.getOperand(2).getOpcode() == ISD::FMUL && 8785 N0->hasOneUse() && N0.getOperand(2)->hasOneUse()) { 8786 return DAG.getNode(PreferredFusedOpcode, SL, VT, 8787 N0.getOperand(0), N0.getOperand(1), 8788 DAG.getNode(PreferredFusedOpcode, SL, VT, 8789 N0.getOperand(2).getOperand(0), 8790 N0.getOperand(2).getOperand(1), 8791 N1)); 8792 } 8793 8794 // fold (fadd x, (fma y, z, (fmul u, v)) -> (fma y, z (fma u, v, x)) 8795 // FIXME: The UnsafeAlgebra flag should be propagated to FMA/FMAD, but FMF 8796 // are currently only supported on binary nodes. 8797 if (Options.UnsafeFPMath && 8798 N1->getOpcode() == PreferredFusedOpcode && 8799 N1.getOperand(2).getOpcode() == ISD::FMUL && 8800 N1->hasOneUse() && N1.getOperand(2)->hasOneUse()) { 8801 return DAG.getNode(PreferredFusedOpcode, SL, VT, 8802 N1.getOperand(0), N1.getOperand(1), 8803 DAG.getNode(PreferredFusedOpcode, SL, VT, 8804 N1.getOperand(2).getOperand(0), 8805 N1.getOperand(2).getOperand(1), 8806 N0)); 8807 } 8808 8809 if (AllowFusion && LookThroughFPExt) { 8810 // fold (fadd (fma x, y, (fpext (fmul u, v))), z) 8811 // -> (fma x, y, (fma (fpext u), (fpext v), z)) 8812 auto FoldFAddFMAFPExtFMul = [&] ( 8813 SDValue X, SDValue Y, SDValue U, SDValue V, SDValue Z) { 8814 return DAG.getNode(PreferredFusedOpcode, SL, VT, X, Y, 8815 DAG.getNode(PreferredFusedOpcode, SL, VT, 8816 DAG.getNode(ISD::FP_EXTEND, SL, VT, U), 8817 DAG.getNode(ISD::FP_EXTEND, SL, VT, V), 8818 Z)); 8819 }; 8820 if (N0.getOpcode() == PreferredFusedOpcode) { 8821 SDValue N02 = N0.getOperand(2); 8822 if (N02.getOpcode() == ISD::FP_EXTEND) { 8823 SDValue N020 = N02.getOperand(0); 8824 if (N020.getOpcode() == ISD::FMUL) 8825 return FoldFAddFMAFPExtFMul(N0.getOperand(0), N0.getOperand(1), 8826 N020.getOperand(0), N020.getOperand(1), 8827 N1); 8828 } 8829 } 8830 8831 // fold (fadd (fpext (fma x, y, (fmul u, v))), z) 8832 // -> (fma (fpext x), (fpext y), (fma (fpext u), (fpext v), z)) 8833 // FIXME: This turns two single-precision and one double-precision 8834 // operation into two double-precision operations, which might not be 8835 // interesting for all targets, especially GPUs. 8836 auto FoldFAddFPExtFMAFMul = [&] ( 8837 SDValue X, SDValue Y, SDValue U, SDValue V, SDValue Z) { 8838 return DAG.getNode(PreferredFusedOpcode, SL, VT, 8839 DAG.getNode(ISD::FP_EXTEND, SL, VT, X), 8840 DAG.getNode(ISD::FP_EXTEND, SL, VT, Y), 8841 DAG.getNode(PreferredFusedOpcode, SL, VT, 8842 DAG.getNode(ISD::FP_EXTEND, SL, VT, U), 8843 DAG.getNode(ISD::FP_EXTEND, SL, VT, V), 8844 Z)); 8845 }; 8846 if (N0.getOpcode() == ISD::FP_EXTEND) { 8847 SDValue N00 = N0.getOperand(0); 8848 if (N00.getOpcode() == PreferredFusedOpcode) { 8849 SDValue N002 = N00.getOperand(2); 8850 if (N002.getOpcode() == ISD::FMUL) 8851 return FoldFAddFPExtFMAFMul(N00.getOperand(0), N00.getOperand(1), 8852 N002.getOperand(0), N002.getOperand(1), 8853 N1); 8854 } 8855 } 8856 8857 // fold (fadd x, (fma y, z, (fpext (fmul u, v))) 8858 // -> (fma y, z, (fma (fpext u), (fpext v), x)) 8859 if (N1.getOpcode() == PreferredFusedOpcode) { 8860 SDValue N12 = N1.getOperand(2); 8861 if (N12.getOpcode() == ISD::FP_EXTEND) { 8862 SDValue N120 = N12.getOperand(0); 8863 if (N120.getOpcode() == ISD::FMUL) 8864 return FoldFAddFMAFPExtFMul(N1.getOperand(0), N1.getOperand(1), 8865 N120.getOperand(0), N120.getOperand(1), 8866 N0); 8867 } 8868 } 8869 8870 // fold (fadd x, (fpext (fma y, z, (fmul u, v))) 8871 // -> (fma (fpext y), (fpext z), (fma (fpext u), (fpext v), x)) 8872 // FIXME: This turns two single-precision and one double-precision 8873 // operation into two double-precision operations, which might not be 8874 // interesting for all targets, especially GPUs. 8875 if (N1.getOpcode() == ISD::FP_EXTEND) { 8876 SDValue N10 = N1.getOperand(0); 8877 if (N10.getOpcode() == PreferredFusedOpcode) { 8878 SDValue N102 = N10.getOperand(2); 8879 if (N102.getOpcode() == ISD::FMUL) 8880 return FoldFAddFPExtFMAFMul(N10.getOperand(0), N10.getOperand(1), 8881 N102.getOperand(0), N102.getOperand(1), 8882 N0); 8883 } 8884 } 8885 } 8886 } 8887 8888 return SDValue(); 8889 } 8890 8891 /// Try to perform FMA combining on a given FSUB node. 8892 SDValue DAGCombiner::visitFSUBForFMACombine(SDNode *N) { 8893 SDValue N0 = N->getOperand(0); 8894 SDValue N1 = N->getOperand(1); 8895 EVT VT = N->getValueType(0); 8896 SDLoc SL(N); 8897 8898 const TargetOptions &Options = DAG.getTarget().Options; 8899 bool AllowFusion = 8900 (Options.AllowFPOpFusion == FPOpFusion::Fast || Options.UnsafeFPMath); 8901 8902 // Floating-point multiply-add with intermediate rounding. 8903 bool HasFMAD = (LegalOperations && TLI.isOperationLegal(ISD::FMAD, VT)); 8904 8905 // Floating-point multiply-add without intermediate rounding. 8906 bool HasFMA = 8907 AllowFusion && TLI.isFMAFasterThanFMulAndFAdd(VT) && 8908 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT)); 8909 8910 // No valid opcode, do not combine. 8911 if (!HasFMAD && !HasFMA) 8912 return SDValue(); 8913 8914 const SelectionDAGTargetInfo *STI = DAG.getSubtarget().getSelectionDAGInfo(); 8915 if (AllowFusion && STI && STI->generateFMAsInMachineCombiner(OptLevel)) 8916 return SDValue(); 8917 8918 // Always prefer FMAD to FMA for precision. 8919 unsigned PreferredFusedOpcode = HasFMAD ? ISD::FMAD : ISD::FMA; 8920 bool Aggressive = TLI.enableAggressiveFMAFusion(VT); 8921 bool LookThroughFPExt = TLI.isFPExtFree(VT); 8922 8923 // fold (fsub (fmul x, y), z) -> (fma x, y, (fneg z)) 8924 if (N0.getOpcode() == ISD::FMUL && 8925 (Aggressive || N0->hasOneUse())) { 8926 return DAG.getNode(PreferredFusedOpcode, SL, VT, 8927 N0.getOperand(0), N0.getOperand(1), 8928 DAG.getNode(ISD::FNEG, SL, VT, N1)); 8929 } 8930 8931 // fold (fsub x, (fmul y, z)) -> (fma (fneg y), z, x) 8932 // Note: Commutes FSUB operands. 8933 if (N1.getOpcode() == ISD::FMUL && 8934 (Aggressive || N1->hasOneUse())) 8935 return DAG.getNode(PreferredFusedOpcode, SL, VT, 8936 DAG.getNode(ISD::FNEG, SL, VT, 8937 N1.getOperand(0)), 8938 N1.getOperand(1), N0); 8939 8940 // fold (fsub (fneg (fmul, x, y)), z) -> (fma (fneg x), y, (fneg z)) 8941 if (N0.getOpcode() == ISD::FNEG && 8942 N0.getOperand(0).getOpcode() == ISD::FMUL && 8943 (Aggressive || (N0->hasOneUse() && N0.getOperand(0).hasOneUse()))) { 8944 SDValue N00 = N0.getOperand(0).getOperand(0); 8945 SDValue N01 = N0.getOperand(0).getOperand(1); 8946 return DAG.getNode(PreferredFusedOpcode, SL, VT, 8947 DAG.getNode(ISD::FNEG, SL, VT, N00), N01, 8948 DAG.getNode(ISD::FNEG, SL, VT, N1)); 8949 } 8950 8951 // Look through FP_EXTEND nodes to do more combining. 8952 if (AllowFusion && LookThroughFPExt) { 8953 // fold (fsub (fpext (fmul x, y)), z) 8954 // -> (fma (fpext x), (fpext y), (fneg z)) 8955 if (N0.getOpcode() == ISD::FP_EXTEND) { 8956 SDValue N00 = N0.getOperand(0); 8957 if (N00.getOpcode() == ISD::FMUL) 8958 return DAG.getNode(PreferredFusedOpcode, SL, VT, 8959 DAG.getNode(ISD::FP_EXTEND, SL, VT, 8960 N00.getOperand(0)), 8961 DAG.getNode(ISD::FP_EXTEND, SL, VT, 8962 N00.getOperand(1)), 8963 DAG.getNode(ISD::FNEG, SL, VT, N1)); 8964 } 8965 8966 // fold (fsub x, (fpext (fmul y, z))) 8967 // -> (fma (fneg (fpext y)), (fpext z), x) 8968 // Note: Commutes FSUB operands. 8969 if (N1.getOpcode() == ISD::FP_EXTEND) { 8970 SDValue N10 = N1.getOperand(0); 8971 if (N10.getOpcode() == ISD::FMUL) 8972 return DAG.getNode(PreferredFusedOpcode, SL, VT, 8973 DAG.getNode(ISD::FNEG, SL, VT, 8974 DAG.getNode(ISD::FP_EXTEND, SL, VT, 8975 N10.getOperand(0))), 8976 DAG.getNode(ISD::FP_EXTEND, SL, VT, 8977 N10.getOperand(1)), 8978 N0); 8979 } 8980 8981 // fold (fsub (fpext (fneg (fmul, x, y))), z) 8982 // -> (fneg (fma (fpext x), (fpext y), z)) 8983 // Note: This could be removed with appropriate canonicalization of the 8984 // input expression into (fneg (fadd (fpext (fmul, x, y)), z). However, the 8985 // orthogonal flags -fp-contract=fast and -enable-unsafe-fp-math prevent 8986 // from implementing the canonicalization in visitFSUB. 8987 if (N0.getOpcode() == ISD::FP_EXTEND) { 8988 SDValue N00 = N0.getOperand(0); 8989 if (N00.getOpcode() == ISD::FNEG) { 8990 SDValue N000 = N00.getOperand(0); 8991 if (N000.getOpcode() == ISD::FMUL) { 8992 return DAG.getNode(ISD::FNEG, SL, VT, 8993 DAG.getNode(PreferredFusedOpcode, SL, VT, 8994 DAG.getNode(ISD::FP_EXTEND, SL, VT, 8995 N000.getOperand(0)), 8996 DAG.getNode(ISD::FP_EXTEND, SL, VT, 8997 N000.getOperand(1)), 8998 N1)); 8999 } 9000 } 9001 } 9002 9003 // fold (fsub (fneg (fpext (fmul, x, y))), z) 9004 // -> (fneg (fma (fpext x)), (fpext y), z) 9005 // Note: This could be removed with appropriate canonicalization of the 9006 // input expression into (fneg (fadd (fpext (fmul, x, y)), z). However, the 9007 // orthogonal flags -fp-contract=fast and -enable-unsafe-fp-math prevent 9008 // from implementing the canonicalization in visitFSUB. 9009 if (N0.getOpcode() == ISD::FNEG) { 9010 SDValue N00 = N0.getOperand(0); 9011 if (N00.getOpcode() == ISD::FP_EXTEND) { 9012 SDValue N000 = N00.getOperand(0); 9013 if (N000.getOpcode() == ISD::FMUL) { 9014 return DAG.getNode(ISD::FNEG, SL, VT, 9015 DAG.getNode(PreferredFusedOpcode, SL, VT, 9016 DAG.getNode(ISD::FP_EXTEND, SL, VT, 9017 N000.getOperand(0)), 9018 DAG.getNode(ISD::FP_EXTEND, SL, VT, 9019 N000.getOperand(1)), 9020 N1)); 9021 } 9022 } 9023 } 9024 9025 } 9026 9027 // More folding opportunities when target permits. 9028 if (Aggressive) { 9029 // fold (fsub (fma x, y, (fmul u, v)), z) 9030 // -> (fma x, y (fma u, v, (fneg z))) 9031 // FIXME: The UnsafeAlgebra flag should be propagated to FMA/FMAD, but FMF 9032 // are currently only supported on binary nodes. 9033 if (Options.UnsafeFPMath && 9034 N0.getOpcode() == PreferredFusedOpcode && 9035 N0.getOperand(2).getOpcode() == ISD::FMUL && 9036 N0->hasOneUse() && N0.getOperand(2)->hasOneUse()) { 9037 return DAG.getNode(PreferredFusedOpcode, SL, VT, 9038 N0.getOperand(0), N0.getOperand(1), 9039 DAG.getNode(PreferredFusedOpcode, SL, VT, 9040 N0.getOperand(2).getOperand(0), 9041 N0.getOperand(2).getOperand(1), 9042 DAG.getNode(ISD::FNEG, SL, VT, 9043 N1))); 9044 } 9045 9046 // fold (fsub x, (fma y, z, (fmul u, v))) 9047 // -> (fma (fneg y), z, (fma (fneg u), v, x)) 9048 // FIXME: The UnsafeAlgebra flag should be propagated to FMA/FMAD, but FMF 9049 // are currently only supported on binary nodes. 9050 if (Options.UnsafeFPMath && 9051 N1.getOpcode() == PreferredFusedOpcode && 9052 N1.getOperand(2).getOpcode() == ISD::FMUL) { 9053 SDValue N20 = N1.getOperand(2).getOperand(0); 9054 SDValue N21 = N1.getOperand(2).getOperand(1); 9055 return DAG.getNode(PreferredFusedOpcode, SL, VT, 9056 DAG.getNode(ISD::FNEG, SL, VT, 9057 N1.getOperand(0)), 9058 N1.getOperand(1), 9059 DAG.getNode(PreferredFusedOpcode, SL, VT, 9060 DAG.getNode(ISD::FNEG, SL, VT, N20), 9061 9062 N21, N0)); 9063 } 9064 9065 if (AllowFusion && LookThroughFPExt) { 9066 // fold (fsub (fma x, y, (fpext (fmul u, v))), z) 9067 // -> (fma x, y (fma (fpext u), (fpext v), (fneg z))) 9068 if (N0.getOpcode() == PreferredFusedOpcode) { 9069 SDValue N02 = N0.getOperand(2); 9070 if (N02.getOpcode() == ISD::FP_EXTEND) { 9071 SDValue N020 = N02.getOperand(0); 9072 if (N020.getOpcode() == ISD::FMUL) 9073 return DAG.getNode(PreferredFusedOpcode, SL, VT, 9074 N0.getOperand(0), N0.getOperand(1), 9075 DAG.getNode(PreferredFusedOpcode, SL, VT, 9076 DAG.getNode(ISD::FP_EXTEND, SL, VT, 9077 N020.getOperand(0)), 9078 DAG.getNode(ISD::FP_EXTEND, SL, VT, 9079 N020.getOperand(1)), 9080 DAG.getNode(ISD::FNEG, SL, VT, 9081 N1))); 9082 } 9083 } 9084 9085 // fold (fsub (fpext (fma x, y, (fmul u, v))), z) 9086 // -> (fma (fpext x), (fpext y), 9087 // (fma (fpext u), (fpext v), (fneg z))) 9088 // FIXME: This turns two single-precision and one double-precision 9089 // operation into two double-precision operations, which might not be 9090 // interesting for all targets, especially GPUs. 9091 if (N0.getOpcode() == ISD::FP_EXTEND) { 9092 SDValue N00 = N0.getOperand(0); 9093 if (N00.getOpcode() == PreferredFusedOpcode) { 9094 SDValue N002 = N00.getOperand(2); 9095 if (N002.getOpcode() == ISD::FMUL) 9096 return DAG.getNode(PreferredFusedOpcode, SL, VT, 9097 DAG.getNode(ISD::FP_EXTEND, SL, VT, 9098 N00.getOperand(0)), 9099 DAG.getNode(ISD::FP_EXTEND, SL, VT, 9100 N00.getOperand(1)), 9101 DAG.getNode(PreferredFusedOpcode, SL, VT, 9102 DAG.getNode(ISD::FP_EXTEND, SL, VT, 9103 N002.getOperand(0)), 9104 DAG.getNode(ISD::FP_EXTEND, SL, VT, 9105 N002.getOperand(1)), 9106 DAG.getNode(ISD::FNEG, SL, VT, 9107 N1))); 9108 } 9109 } 9110 9111 // fold (fsub x, (fma y, z, (fpext (fmul u, v)))) 9112 // -> (fma (fneg y), z, (fma (fneg (fpext u)), (fpext v), x)) 9113 if (N1.getOpcode() == PreferredFusedOpcode && 9114 N1.getOperand(2).getOpcode() == ISD::FP_EXTEND) { 9115 SDValue N120 = N1.getOperand(2).getOperand(0); 9116 if (N120.getOpcode() == ISD::FMUL) { 9117 SDValue N1200 = N120.getOperand(0); 9118 SDValue N1201 = N120.getOperand(1); 9119 return DAG.getNode(PreferredFusedOpcode, SL, VT, 9120 DAG.getNode(ISD::FNEG, SL, VT, N1.getOperand(0)), 9121 N1.getOperand(1), 9122 DAG.getNode(PreferredFusedOpcode, SL, VT, 9123 DAG.getNode(ISD::FNEG, SL, VT, 9124 DAG.getNode(ISD::FP_EXTEND, SL, 9125 VT, N1200)), 9126 DAG.getNode(ISD::FP_EXTEND, SL, VT, 9127 N1201), 9128 N0)); 9129 } 9130 } 9131 9132 // fold (fsub x, (fpext (fma y, z, (fmul u, v)))) 9133 // -> (fma (fneg (fpext y)), (fpext z), 9134 // (fma (fneg (fpext u)), (fpext v), x)) 9135 // FIXME: This turns two single-precision and one double-precision 9136 // operation into two double-precision operations, which might not be 9137 // interesting for all targets, especially GPUs. 9138 if (N1.getOpcode() == ISD::FP_EXTEND && 9139 N1.getOperand(0).getOpcode() == PreferredFusedOpcode) { 9140 SDValue N100 = N1.getOperand(0).getOperand(0); 9141 SDValue N101 = N1.getOperand(0).getOperand(1); 9142 SDValue N102 = N1.getOperand(0).getOperand(2); 9143 if (N102.getOpcode() == ISD::FMUL) { 9144 SDValue N1020 = N102.getOperand(0); 9145 SDValue N1021 = N102.getOperand(1); 9146 return DAG.getNode(PreferredFusedOpcode, SL, VT, 9147 DAG.getNode(ISD::FNEG, SL, VT, 9148 DAG.getNode(ISD::FP_EXTEND, SL, VT, 9149 N100)), 9150 DAG.getNode(ISD::FP_EXTEND, SL, VT, N101), 9151 DAG.getNode(PreferredFusedOpcode, SL, VT, 9152 DAG.getNode(ISD::FNEG, SL, VT, 9153 DAG.getNode(ISD::FP_EXTEND, SL, 9154 VT, N1020)), 9155 DAG.getNode(ISD::FP_EXTEND, SL, VT, 9156 N1021), 9157 N0)); 9158 } 9159 } 9160 } 9161 } 9162 9163 return SDValue(); 9164 } 9165 9166 /// Try to perform FMA combining on a given FMUL node based on the distributive 9167 /// law x * (y + 1) = x * y + x and variants thereof (commuted versions, 9168 /// subtraction instead of addition). 9169 SDValue DAGCombiner::visitFMULForFMADistributiveCombine(SDNode *N) { 9170 SDValue N0 = N->getOperand(0); 9171 SDValue N1 = N->getOperand(1); 9172 EVT VT = N->getValueType(0); 9173 SDLoc SL(N); 9174 9175 assert(N->getOpcode() == ISD::FMUL && "Expected FMUL Operation"); 9176 9177 const TargetOptions &Options = DAG.getTarget().Options; 9178 9179 // The transforms below are incorrect when x == 0 and y == inf, because the 9180 // intermediate multiplication produces a nan. 9181 if (!Options.NoInfsFPMath) 9182 return SDValue(); 9183 9184 // Floating-point multiply-add without intermediate rounding. 9185 bool HasFMA = 9186 (Options.AllowFPOpFusion == FPOpFusion::Fast || Options.UnsafeFPMath) && 9187 TLI.isFMAFasterThanFMulAndFAdd(VT) && 9188 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT)); 9189 9190 // Floating-point multiply-add with intermediate rounding. This can result 9191 // in a less precise result due to the changed rounding order. 9192 bool HasFMAD = Options.UnsafeFPMath && 9193 (LegalOperations && TLI.isOperationLegal(ISD::FMAD, VT)); 9194 9195 // No valid opcode, do not combine. 9196 if (!HasFMAD && !HasFMA) 9197 return SDValue(); 9198 9199 // Always prefer FMAD to FMA for precision. 9200 unsigned PreferredFusedOpcode = HasFMAD ? ISD::FMAD : ISD::FMA; 9201 bool Aggressive = TLI.enableAggressiveFMAFusion(VT); 9202 9203 // fold (fmul (fadd x, +1.0), y) -> (fma x, y, y) 9204 // fold (fmul (fadd x, -1.0), y) -> (fma x, y, (fneg y)) 9205 auto FuseFADD = [&](SDValue X, SDValue Y) { 9206 if (X.getOpcode() == ISD::FADD && (Aggressive || X->hasOneUse())) { 9207 auto XC1 = isConstOrConstSplatFP(X.getOperand(1)); 9208 if (XC1 && XC1->isExactlyValue(+1.0)) 9209 return DAG.getNode(PreferredFusedOpcode, SL, VT, X.getOperand(0), Y, Y); 9210 if (XC1 && XC1->isExactlyValue(-1.0)) 9211 return DAG.getNode(PreferredFusedOpcode, SL, VT, X.getOperand(0), Y, 9212 DAG.getNode(ISD::FNEG, SL, VT, Y)); 9213 } 9214 return SDValue(); 9215 }; 9216 9217 if (SDValue FMA = FuseFADD(N0, N1)) 9218 return FMA; 9219 if (SDValue FMA = FuseFADD(N1, N0)) 9220 return FMA; 9221 9222 // fold (fmul (fsub +1.0, x), y) -> (fma (fneg x), y, y) 9223 // fold (fmul (fsub -1.0, x), y) -> (fma (fneg x), y, (fneg y)) 9224 // fold (fmul (fsub x, +1.0), y) -> (fma x, y, (fneg y)) 9225 // fold (fmul (fsub x, -1.0), y) -> (fma x, y, y) 9226 auto FuseFSUB = [&](SDValue X, SDValue Y) { 9227 if (X.getOpcode() == ISD::FSUB && (Aggressive || X->hasOneUse())) { 9228 auto XC0 = isConstOrConstSplatFP(X.getOperand(0)); 9229 if (XC0 && XC0->isExactlyValue(+1.0)) 9230 return DAG.getNode(PreferredFusedOpcode, SL, VT, 9231 DAG.getNode(ISD::FNEG, SL, VT, X.getOperand(1)), Y, 9232 Y); 9233 if (XC0 && XC0->isExactlyValue(-1.0)) 9234 return DAG.getNode(PreferredFusedOpcode, SL, VT, 9235 DAG.getNode(ISD::FNEG, SL, VT, X.getOperand(1)), Y, 9236 DAG.getNode(ISD::FNEG, SL, VT, Y)); 9237 9238 auto XC1 = isConstOrConstSplatFP(X.getOperand(1)); 9239 if (XC1 && XC1->isExactlyValue(+1.0)) 9240 return DAG.getNode(PreferredFusedOpcode, SL, VT, X.getOperand(0), Y, 9241 DAG.getNode(ISD::FNEG, SL, VT, Y)); 9242 if (XC1 && XC1->isExactlyValue(-1.0)) 9243 return DAG.getNode(PreferredFusedOpcode, SL, VT, X.getOperand(0), Y, Y); 9244 } 9245 return SDValue(); 9246 }; 9247 9248 if (SDValue FMA = FuseFSUB(N0, N1)) 9249 return FMA; 9250 if (SDValue FMA = FuseFSUB(N1, N0)) 9251 return FMA; 9252 9253 return SDValue(); 9254 } 9255 9256 SDValue DAGCombiner::visitFADD(SDNode *N) { 9257 SDValue N0 = N->getOperand(0); 9258 SDValue N1 = N->getOperand(1); 9259 bool N0CFP = isConstantFPBuildVectorOrConstantFP(N0); 9260 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1); 9261 EVT VT = N->getValueType(0); 9262 SDLoc DL(N); 9263 const TargetOptions &Options = DAG.getTarget().Options; 9264 const SDNodeFlags *Flags = &cast<BinaryWithFlagsSDNode>(N)->Flags; 9265 9266 // fold vector ops 9267 if (VT.isVector()) 9268 if (SDValue FoldedVOp = SimplifyVBinOp(N)) 9269 return FoldedVOp; 9270 9271 // fold (fadd c1, c2) -> c1 + c2 9272 if (N0CFP && N1CFP) 9273 return DAG.getNode(ISD::FADD, DL, VT, N0, N1, Flags); 9274 9275 // canonicalize constant to RHS 9276 if (N0CFP && !N1CFP) 9277 return DAG.getNode(ISD::FADD, DL, VT, N1, N0, Flags); 9278 9279 if (SDValue NewSel = foldBinOpIntoSelect(N)) 9280 return NewSel; 9281 9282 // fold (fadd A, (fneg B)) -> (fsub A, B) 9283 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) && 9284 isNegatibleForFree(N1, LegalOperations, TLI, &Options) == 2) 9285 return DAG.getNode(ISD::FSUB, DL, VT, N0, 9286 GetNegatedExpression(N1, DAG, LegalOperations), Flags); 9287 9288 // fold (fadd (fneg A), B) -> (fsub B, A) 9289 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) && 9290 isNegatibleForFree(N0, LegalOperations, TLI, &Options) == 2) 9291 return DAG.getNode(ISD::FSUB, DL, VT, N1, 9292 GetNegatedExpression(N0, DAG, LegalOperations), Flags); 9293 9294 // FIXME: Auto-upgrade the target/function-level option. 9295 if (Options.NoSignedZerosFPMath || N->getFlags()->hasNoSignedZeros()) { 9296 // fold (fadd A, 0) -> A 9297 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1)) 9298 if (N1C->isZero()) 9299 return N0; 9300 } 9301 9302 // If 'unsafe math' is enabled, fold lots of things. 9303 if (Options.UnsafeFPMath) { 9304 // No FP constant should be created after legalization as Instruction 9305 // Selection pass has a hard time dealing with FP constants. 9306 bool AllowNewConst = (Level < AfterLegalizeDAG); 9307 9308 // fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2)) 9309 if (N1CFP && N0.getOpcode() == ISD::FADD && N0.getNode()->hasOneUse() && 9310 isConstantFPBuildVectorOrConstantFP(N0.getOperand(1))) 9311 return DAG.getNode(ISD::FADD, DL, VT, N0.getOperand(0), 9312 DAG.getNode(ISD::FADD, DL, VT, N0.getOperand(1), N1, 9313 Flags), 9314 Flags); 9315 9316 // If allowed, fold (fadd (fneg x), x) -> 0.0 9317 if (AllowNewConst && N0.getOpcode() == ISD::FNEG && N0.getOperand(0) == N1) 9318 return DAG.getConstantFP(0.0, DL, VT); 9319 9320 // If allowed, fold (fadd x, (fneg x)) -> 0.0 9321 if (AllowNewConst && N1.getOpcode() == ISD::FNEG && N1.getOperand(0) == N0) 9322 return DAG.getConstantFP(0.0, DL, VT); 9323 9324 // We can fold chains of FADD's of the same value into multiplications. 9325 // This transform is not safe in general because we are reducing the number 9326 // of rounding steps. 9327 if (TLI.isOperationLegalOrCustom(ISD::FMUL, VT) && !N0CFP && !N1CFP) { 9328 if (N0.getOpcode() == ISD::FMUL) { 9329 bool CFP00 = isConstantFPBuildVectorOrConstantFP(N0.getOperand(0)); 9330 bool CFP01 = isConstantFPBuildVectorOrConstantFP(N0.getOperand(1)); 9331 9332 // (fadd (fmul x, c), x) -> (fmul x, c+1) 9333 if (CFP01 && !CFP00 && N0.getOperand(0) == N1) { 9334 SDValue NewCFP = DAG.getNode(ISD::FADD, DL, VT, N0.getOperand(1), 9335 DAG.getConstantFP(1.0, DL, VT), Flags); 9336 return DAG.getNode(ISD::FMUL, DL, VT, N1, NewCFP, Flags); 9337 } 9338 9339 // (fadd (fmul x, c), (fadd x, x)) -> (fmul x, c+2) 9340 if (CFP01 && !CFP00 && N1.getOpcode() == ISD::FADD && 9341 N1.getOperand(0) == N1.getOperand(1) && 9342 N0.getOperand(0) == N1.getOperand(0)) { 9343 SDValue NewCFP = DAG.getNode(ISD::FADD, DL, VT, N0.getOperand(1), 9344 DAG.getConstantFP(2.0, DL, VT), Flags); 9345 return DAG.getNode(ISD::FMUL, DL, VT, N0.getOperand(0), NewCFP, Flags); 9346 } 9347 } 9348 9349 if (N1.getOpcode() == ISD::FMUL) { 9350 bool CFP10 = isConstantFPBuildVectorOrConstantFP(N1.getOperand(0)); 9351 bool CFP11 = isConstantFPBuildVectorOrConstantFP(N1.getOperand(1)); 9352 9353 // (fadd x, (fmul x, c)) -> (fmul x, c+1) 9354 if (CFP11 && !CFP10 && N1.getOperand(0) == N0) { 9355 SDValue NewCFP = DAG.getNode(ISD::FADD, DL, VT, N1.getOperand(1), 9356 DAG.getConstantFP(1.0, DL, VT), Flags); 9357 return DAG.getNode(ISD::FMUL, DL, VT, N0, NewCFP, Flags); 9358 } 9359 9360 // (fadd (fadd x, x), (fmul x, c)) -> (fmul x, c+2) 9361 if (CFP11 && !CFP10 && N0.getOpcode() == ISD::FADD && 9362 N0.getOperand(0) == N0.getOperand(1) && 9363 N1.getOperand(0) == N0.getOperand(0)) { 9364 SDValue NewCFP = DAG.getNode(ISD::FADD, DL, VT, N1.getOperand(1), 9365 DAG.getConstantFP(2.0, DL, VT), Flags); 9366 return DAG.getNode(ISD::FMUL, DL, VT, N1.getOperand(0), NewCFP, Flags); 9367 } 9368 } 9369 9370 if (N0.getOpcode() == ISD::FADD && AllowNewConst) { 9371 bool CFP00 = isConstantFPBuildVectorOrConstantFP(N0.getOperand(0)); 9372 // (fadd (fadd x, x), x) -> (fmul x, 3.0) 9373 if (!CFP00 && N0.getOperand(0) == N0.getOperand(1) && 9374 (N0.getOperand(0) == N1)) { 9375 return DAG.getNode(ISD::FMUL, DL, VT, 9376 N1, DAG.getConstantFP(3.0, DL, VT), Flags); 9377 } 9378 } 9379 9380 if (N1.getOpcode() == ISD::FADD && AllowNewConst) { 9381 bool CFP10 = isConstantFPBuildVectorOrConstantFP(N1.getOperand(0)); 9382 // (fadd x, (fadd x, x)) -> (fmul x, 3.0) 9383 if (!CFP10 && N1.getOperand(0) == N1.getOperand(1) && 9384 N1.getOperand(0) == N0) { 9385 return DAG.getNode(ISD::FMUL, DL, VT, 9386 N0, DAG.getConstantFP(3.0, DL, VT), Flags); 9387 } 9388 } 9389 9390 // (fadd (fadd x, x), (fadd x, x)) -> (fmul x, 4.0) 9391 if (AllowNewConst && 9392 N0.getOpcode() == ISD::FADD && N1.getOpcode() == ISD::FADD && 9393 N0.getOperand(0) == N0.getOperand(1) && 9394 N1.getOperand(0) == N1.getOperand(1) && 9395 N0.getOperand(0) == N1.getOperand(0)) { 9396 return DAG.getNode(ISD::FMUL, DL, VT, N0.getOperand(0), 9397 DAG.getConstantFP(4.0, DL, VT), Flags); 9398 } 9399 } 9400 } // enable-unsafe-fp-math 9401 9402 // FADD -> FMA combines: 9403 if (SDValue Fused = visitFADDForFMACombine(N)) { 9404 AddToWorklist(Fused.getNode()); 9405 return Fused; 9406 } 9407 return SDValue(); 9408 } 9409 9410 SDValue DAGCombiner::visitFSUB(SDNode *N) { 9411 SDValue N0 = N->getOperand(0); 9412 SDValue N1 = N->getOperand(1); 9413 ConstantFPSDNode *N0CFP = isConstOrConstSplatFP(N0); 9414 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1); 9415 EVT VT = N->getValueType(0); 9416 SDLoc DL(N); 9417 const TargetOptions &Options = DAG.getTarget().Options; 9418 const SDNodeFlags *Flags = &cast<BinaryWithFlagsSDNode>(N)->Flags; 9419 9420 // fold vector ops 9421 if (VT.isVector()) 9422 if (SDValue FoldedVOp = SimplifyVBinOp(N)) 9423 return FoldedVOp; 9424 9425 // fold (fsub c1, c2) -> c1-c2 9426 if (N0CFP && N1CFP) 9427 return DAG.getNode(ISD::FSUB, DL, VT, N0, N1, Flags); 9428 9429 if (SDValue NewSel = foldBinOpIntoSelect(N)) 9430 return NewSel; 9431 9432 // fold (fsub A, (fneg B)) -> (fadd A, B) 9433 if (isNegatibleForFree(N1, LegalOperations, TLI, &Options)) 9434 return DAG.getNode(ISD::FADD, DL, VT, N0, 9435 GetNegatedExpression(N1, DAG, LegalOperations), Flags); 9436 9437 // FIXME: Auto-upgrade the target/function-level option. 9438 if (Options.NoSignedZerosFPMath || N->getFlags()->hasNoSignedZeros()) { 9439 // (fsub 0, B) -> -B 9440 if (N0CFP && N0CFP->isZero()) { 9441 if (isNegatibleForFree(N1, LegalOperations, TLI, &Options)) 9442 return GetNegatedExpression(N1, DAG, LegalOperations); 9443 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT)) 9444 return DAG.getNode(ISD::FNEG, DL, VT, N1, Flags); 9445 } 9446 } 9447 9448 // If 'unsafe math' is enabled, fold lots of things. 9449 if (Options.UnsafeFPMath) { 9450 // (fsub A, 0) -> A 9451 if (N1CFP && N1CFP->isZero()) 9452 return N0; 9453 9454 // (fsub x, x) -> 0.0 9455 if (N0 == N1) 9456 return DAG.getConstantFP(0.0f, DL, VT); 9457 9458 // (fsub x, (fadd x, y)) -> (fneg y) 9459 // (fsub x, (fadd y, x)) -> (fneg y) 9460 if (N1.getOpcode() == ISD::FADD) { 9461 SDValue N10 = N1->getOperand(0); 9462 SDValue N11 = N1->getOperand(1); 9463 9464 if (N10 == N0 && isNegatibleForFree(N11, LegalOperations, TLI, &Options)) 9465 return GetNegatedExpression(N11, DAG, LegalOperations); 9466 9467 if (N11 == N0 && isNegatibleForFree(N10, LegalOperations, TLI, &Options)) 9468 return GetNegatedExpression(N10, DAG, LegalOperations); 9469 } 9470 } 9471 9472 // FSUB -> FMA combines: 9473 if (SDValue Fused = visitFSUBForFMACombine(N)) { 9474 AddToWorklist(Fused.getNode()); 9475 return Fused; 9476 } 9477 9478 return SDValue(); 9479 } 9480 9481 SDValue DAGCombiner::visitFMUL(SDNode *N) { 9482 SDValue N0 = N->getOperand(0); 9483 SDValue N1 = N->getOperand(1); 9484 ConstantFPSDNode *N0CFP = isConstOrConstSplatFP(N0); 9485 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1); 9486 EVT VT = N->getValueType(0); 9487 SDLoc DL(N); 9488 const TargetOptions &Options = DAG.getTarget().Options; 9489 const SDNodeFlags *Flags = &cast<BinaryWithFlagsSDNode>(N)->Flags; 9490 9491 // fold vector ops 9492 if (VT.isVector()) { 9493 // This just handles C1 * C2 for vectors. Other vector folds are below. 9494 if (SDValue FoldedVOp = SimplifyVBinOp(N)) 9495 return FoldedVOp; 9496 } 9497 9498 // fold (fmul c1, c2) -> c1*c2 9499 if (N0CFP && N1CFP) 9500 return DAG.getNode(ISD::FMUL, DL, VT, N0, N1, Flags); 9501 9502 // canonicalize constant to RHS 9503 if (isConstantFPBuildVectorOrConstantFP(N0) && 9504 !isConstantFPBuildVectorOrConstantFP(N1)) 9505 return DAG.getNode(ISD::FMUL, DL, VT, N1, N0, Flags); 9506 9507 // fold (fmul A, 1.0) -> A 9508 if (N1CFP && N1CFP->isExactlyValue(1.0)) 9509 return N0; 9510 9511 if (SDValue NewSel = foldBinOpIntoSelect(N)) 9512 return NewSel; 9513 9514 if (Options.UnsafeFPMath) { 9515 // fold (fmul A, 0) -> 0 9516 if (N1CFP && N1CFP->isZero()) 9517 return N1; 9518 9519 // fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2)) 9520 if (N0.getOpcode() == ISD::FMUL) { 9521 // Fold scalars or any vector constants (not just splats). 9522 // This fold is done in general by InstCombine, but extra fmul insts 9523 // may have been generated during lowering. 9524 SDValue N00 = N0.getOperand(0); 9525 SDValue N01 = N0.getOperand(1); 9526 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1); 9527 auto *BV00 = dyn_cast<BuildVectorSDNode>(N00); 9528 auto *BV01 = dyn_cast<BuildVectorSDNode>(N01); 9529 9530 // Check 1: Make sure that the first operand of the inner multiply is NOT 9531 // a constant. Otherwise, we may induce infinite looping. 9532 if (!(isConstOrConstSplatFP(N00) || (BV00 && BV00->isConstant()))) { 9533 // Check 2: Make sure that the second operand of the inner multiply and 9534 // the second operand of the outer multiply are constants. 9535 if ((N1CFP && isConstOrConstSplatFP(N01)) || 9536 (BV1 && BV01 && BV1->isConstant() && BV01->isConstant())) { 9537 SDValue MulConsts = DAG.getNode(ISD::FMUL, DL, VT, N01, N1, Flags); 9538 return DAG.getNode(ISD::FMUL, DL, VT, N00, MulConsts, Flags); 9539 } 9540 } 9541 } 9542 9543 // fold (fmul (fadd x, x), c) -> (fmul x, (fmul 2.0, c)) 9544 // Undo the fmul 2.0, x -> fadd x, x transformation, since if it occurs 9545 // during an early run of DAGCombiner can prevent folding with fmuls 9546 // inserted during lowering. 9547 if (N0.getOpcode() == ISD::FADD && 9548 (N0.getOperand(0) == N0.getOperand(1)) && 9549 N0.hasOneUse()) { 9550 const SDValue Two = DAG.getConstantFP(2.0, DL, VT); 9551 SDValue MulConsts = DAG.getNode(ISD::FMUL, DL, VT, Two, N1, Flags); 9552 return DAG.getNode(ISD::FMUL, DL, VT, N0.getOperand(0), MulConsts, Flags); 9553 } 9554 } 9555 9556 // fold (fmul X, 2.0) -> (fadd X, X) 9557 if (N1CFP && N1CFP->isExactlyValue(+2.0)) 9558 return DAG.getNode(ISD::FADD, DL, VT, N0, N0, Flags); 9559 9560 // fold (fmul X, -1.0) -> (fneg X) 9561 if (N1CFP && N1CFP->isExactlyValue(-1.0)) 9562 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT)) 9563 return DAG.getNode(ISD::FNEG, DL, VT, N0); 9564 9565 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y) 9566 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI, &Options)) { 9567 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI, &Options)) { 9568 // Both can be negated for free, check to see if at least one is cheaper 9569 // negated. 9570 if (LHSNeg == 2 || RHSNeg == 2) 9571 return DAG.getNode(ISD::FMUL, DL, VT, 9572 GetNegatedExpression(N0, DAG, LegalOperations), 9573 GetNegatedExpression(N1, DAG, LegalOperations), 9574 Flags); 9575 } 9576 } 9577 9578 // FMUL -> FMA combines: 9579 if (SDValue Fused = visitFMULForFMADistributiveCombine(N)) { 9580 AddToWorklist(Fused.getNode()); 9581 return Fused; 9582 } 9583 9584 return SDValue(); 9585 } 9586 9587 SDValue DAGCombiner::visitFMA(SDNode *N) { 9588 SDValue N0 = N->getOperand(0); 9589 SDValue N1 = N->getOperand(1); 9590 SDValue N2 = N->getOperand(2); 9591 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); 9592 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); 9593 EVT VT = N->getValueType(0); 9594 SDLoc DL(N); 9595 const TargetOptions &Options = DAG.getTarget().Options; 9596 9597 // Constant fold FMA. 9598 if (isa<ConstantFPSDNode>(N0) && 9599 isa<ConstantFPSDNode>(N1) && 9600 isa<ConstantFPSDNode>(N2)) { 9601 return DAG.getNode(ISD::FMA, DL, VT, N0, N1, N2); 9602 } 9603 9604 if (Options.UnsafeFPMath) { 9605 if (N0CFP && N0CFP->isZero()) 9606 return N2; 9607 if (N1CFP && N1CFP->isZero()) 9608 return N2; 9609 } 9610 // TODO: The FMA node should have flags that propagate to these nodes. 9611 if (N0CFP && N0CFP->isExactlyValue(1.0)) 9612 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N2); 9613 if (N1CFP && N1CFP->isExactlyValue(1.0)) 9614 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N2); 9615 9616 // Canonicalize (fma c, x, y) -> (fma x, c, y) 9617 if (isConstantFPBuildVectorOrConstantFP(N0) && 9618 !isConstantFPBuildVectorOrConstantFP(N1)) 9619 return DAG.getNode(ISD::FMA, SDLoc(N), VT, N1, N0, N2); 9620 9621 // TODO: FMA nodes should have flags that propagate to the created nodes. 9622 // For now, create a Flags object for use with all unsafe math transforms. 9623 SDNodeFlags Flags; 9624 Flags.setUnsafeAlgebra(true); 9625 9626 if (Options.UnsafeFPMath) { 9627 // (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2) 9628 if (N2.getOpcode() == ISD::FMUL && N0 == N2.getOperand(0) && 9629 isConstantFPBuildVectorOrConstantFP(N1) && 9630 isConstantFPBuildVectorOrConstantFP(N2.getOperand(1))) { 9631 return DAG.getNode(ISD::FMUL, DL, VT, N0, 9632 DAG.getNode(ISD::FADD, DL, VT, N1, N2.getOperand(1), 9633 &Flags), &Flags); 9634 } 9635 9636 // (fma (fmul x, c1), c2, y) -> (fma x, c1*c2, y) 9637 if (N0.getOpcode() == ISD::FMUL && 9638 isConstantFPBuildVectorOrConstantFP(N1) && 9639 isConstantFPBuildVectorOrConstantFP(N0.getOperand(1))) { 9640 return DAG.getNode(ISD::FMA, DL, VT, 9641 N0.getOperand(0), 9642 DAG.getNode(ISD::FMUL, DL, VT, N1, N0.getOperand(1), 9643 &Flags), 9644 N2); 9645 } 9646 } 9647 9648 // (fma x, 1, y) -> (fadd x, y) 9649 // (fma x, -1, y) -> (fadd (fneg x), y) 9650 if (N1CFP) { 9651 if (N1CFP->isExactlyValue(1.0)) 9652 // TODO: The FMA node should have flags that propagate to this node. 9653 return DAG.getNode(ISD::FADD, DL, VT, N0, N2); 9654 9655 if (N1CFP->isExactlyValue(-1.0) && 9656 (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))) { 9657 SDValue RHSNeg = DAG.getNode(ISD::FNEG, DL, VT, N0); 9658 AddToWorklist(RHSNeg.getNode()); 9659 // TODO: The FMA node should have flags that propagate to this node. 9660 return DAG.getNode(ISD::FADD, DL, VT, N2, RHSNeg); 9661 } 9662 } 9663 9664 if (Options.UnsafeFPMath) { 9665 // (fma x, c, x) -> (fmul x, (c+1)) 9666 if (N1CFP && N0 == N2) { 9667 return DAG.getNode(ISD::FMUL, DL, VT, N0, 9668 DAG.getNode(ISD::FADD, DL, VT, N1, 9669 DAG.getConstantFP(1.0, DL, VT), &Flags), 9670 &Flags); 9671 } 9672 9673 // (fma x, c, (fneg x)) -> (fmul x, (c-1)) 9674 if (N1CFP && N2.getOpcode() == ISD::FNEG && N2.getOperand(0) == N0) { 9675 return DAG.getNode(ISD::FMUL, DL, VT, N0, 9676 DAG.getNode(ISD::FADD, DL, VT, N1, 9677 DAG.getConstantFP(-1.0, DL, VT), &Flags), 9678 &Flags); 9679 } 9680 } 9681 9682 return SDValue(); 9683 } 9684 9685 // Combine multiple FDIVs with the same divisor into multiple FMULs by the 9686 // reciprocal. 9687 // E.g., (a / D; b / D;) -> (recip = 1.0 / D; a * recip; b * recip) 9688 // Notice that this is not always beneficial. One reason is different targets 9689 // may have different costs for FDIV and FMUL, so sometimes the cost of two 9690 // FDIVs may be lower than the cost of one FDIV and two FMULs. Another reason 9691 // is the critical path is increased from "one FDIV" to "one FDIV + one FMUL". 9692 SDValue DAGCombiner::combineRepeatedFPDivisors(SDNode *N) { 9693 bool UnsafeMath = DAG.getTarget().Options.UnsafeFPMath; 9694 const SDNodeFlags *Flags = N->getFlags(); 9695 if (!UnsafeMath && !Flags->hasAllowReciprocal()) 9696 return SDValue(); 9697 9698 // Skip if current node is a reciprocal. 9699 SDValue N0 = N->getOperand(0); 9700 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); 9701 if (N0CFP && N0CFP->isExactlyValue(1.0)) 9702 return SDValue(); 9703 9704 // Exit early if the target does not want this transform or if there can't 9705 // possibly be enough uses of the divisor to make the transform worthwhile. 9706 SDValue N1 = N->getOperand(1); 9707 unsigned MinUses = TLI.combineRepeatedFPDivisors(); 9708 if (!MinUses || N1->use_size() < MinUses) 9709 return SDValue(); 9710 9711 // Find all FDIV users of the same divisor. 9712 // Use a set because duplicates may be present in the user list. 9713 SetVector<SDNode *> Users; 9714 for (auto *U : N1->uses()) { 9715 if (U->getOpcode() == ISD::FDIV && U->getOperand(1) == N1) { 9716 // This division is eligible for optimization only if global unsafe math 9717 // is enabled or if this division allows reciprocal formation. 9718 if (UnsafeMath || U->getFlags()->hasAllowReciprocal()) 9719 Users.insert(U); 9720 } 9721 } 9722 9723 // Now that we have the actual number of divisor uses, make sure it meets 9724 // the minimum threshold specified by the target. 9725 if (Users.size() < MinUses) 9726 return SDValue(); 9727 9728 EVT VT = N->getValueType(0); 9729 SDLoc DL(N); 9730 SDValue FPOne = DAG.getConstantFP(1.0, DL, VT); 9731 SDValue Reciprocal = DAG.getNode(ISD::FDIV, DL, VT, FPOne, N1, Flags); 9732 9733 // Dividend / Divisor -> Dividend * Reciprocal 9734 for (auto *U : Users) { 9735 SDValue Dividend = U->getOperand(0); 9736 if (Dividend != FPOne) { 9737 SDValue NewNode = DAG.getNode(ISD::FMUL, SDLoc(U), VT, Dividend, 9738 Reciprocal, Flags); 9739 CombineTo(U, NewNode); 9740 } else if (U != Reciprocal.getNode()) { 9741 // In the absence of fast-math-flags, this user node is always the 9742 // same node as Reciprocal, but with FMF they may be different nodes. 9743 CombineTo(U, Reciprocal); 9744 } 9745 } 9746 return SDValue(N, 0); // N was replaced. 9747 } 9748 9749 SDValue DAGCombiner::visitFDIV(SDNode *N) { 9750 SDValue N0 = N->getOperand(0); 9751 SDValue N1 = N->getOperand(1); 9752 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); 9753 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); 9754 EVT VT = N->getValueType(0); 9755 SDLoc DL(N); 9756 const TargetOptions &Options = DAG.getTarget().Options; 9757 SDNodeFlags *Flags = &cast<BinaryWithFlagsSDNode>(N)->Flags; 9758 9759 // fold vector ops 9760 if (VT.isVector()) 9761 if (SDValue FoldedVOp = SimplifyVBinOp(N)) 9762 return FoldedVOp; 9763 9764 // fold (fdiv c1, c2) -> c1/c2 9765 if (N0CFP && N1CFP) 9766 return DAG.getNode(ISD::FDIV, SDLoc(N), VT, N0, N1, Flags); 9767 9768 if (SDValue NewSel = foldBinOpIntoSelect(N)) 9769 return NewSel; 9770 9771 if (Options.UnsafeFPMath) { 9772 // fold (fdiv X, c2) -> fmul X, 1/c2 if losing precision is acceptable. 9773 if (N1CFP) { 9774 // Compute the reciprocal 1.0 / c2. 9775 const APFloat &N1APF = N1CFP->getValueAPF(); 9776 APFloat Recip(N1APF.getSemantics(), 1); // 1.0 9777 APFloat::opStatus st = Recip.divide(N1APF, APFloat::rmNearestTiesToEven); 9778 // Only do the transform if the reciprocal is a legal fp immediate that 9779 // isn't too nasty (eg NaN, denormal, ...). 9780 if ((st == APFloat::opOK || st == APFloat::opInexact) && // Not too nasty 9781 (!LegalOperations || 9782 // FIXME: custom lowering of ConstantFP might fail (see e.g. ARM 9783 // backend)... we should handle this gracefully after Legalize. 9784 // TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT) || 9785 TLI.isOperationLegal(llvm::ISD::ConstantFP, VT) || 9786 TLI.isFPImmLegal(Recip, VT))) 9787 return DAG.getNode(ISD::FMUL, DL, VT, N0, 9788 DAG.getConstantFP(Recip, DL, VT), Flags); 9789 } 9790 9791 // If this FDIV is part of a reciprocal square root, it may be folded 9792 // into a target-specific square root estimate instruction. 9793 if (N1.getOpcode() == ISD::FSQRT) { 9794 if (SDValue RV = buildRsqrtEstimate(N1.getOperand(0), Flags)) { 9795 return DAG.getNode(ISD::FMUL, DL, VT, N0, RV, Flags); 9796 } 9797 } else if (N1.getOpcode() == ISD::FP_EXTEND && 9798 N1.getOperand(0).getOpcode() == ISD::FSQRT) { 9799 if (SDValue RV = buildRsqrtEstimate(N1.getOperand(0).getOperand(0), 9800 Flags)) { 9801 RV = DAG.getNode(ISD::FP_EXTEND, SDLoc(N1), VT, RV); 9802 AddToWorklist(RV.getNode()); 9803 return DAG.getNode(ISD::FMUL, DL, VT, N0, RV, Flags); 9804 } 9805 } else if (N1.getOpcode() == ISD::FP_ROUND && 9806 N1.getOperand(0).getOpcode() == ISD::FSQRT) { 9807 if (SDValue RV = buildRsqrtEstimate(N1.getOperand(0).getOperand(0), 9808 Flags)) { 9809 RV = DAG.getNode(ISD::FP_ROUND, SDLoc(N1), VT, RV, N1.getOperand(1)); 9810 AddToWorklist(RV.getNode()); 9811 return DAG.getNode(ISD::FMUL, DL, VT, N0, RV, Flags); 9812 } 9813 } else if (N1.getOpcode() == ISD::FMUL) { 9814 // Look through an FMUL. Even though this won't remove the FDIV directly, 9815 // it's still worthwhile to get rid of the FSQRT if possible. 9816 SDValue SqrtOp; 9817 SDValue OtherOp; 9818 if (N1.getOperand(0).getOpcode() == ISD::FSQRT) { 9819 SqrtOp = N1.getOperand(0); 9820 OtherOp = N1.getOperand(1); 9821 } else if (N1.getOperand(1).getOpcode() == ISD::FSQRT) { 9822 SqrtOp = N1.getOperand(1); 9823 OtherOp = N1.getOperand(0); 9824 } 9825 if (SqrtOp.getNode()) { 9826 // We found a FSQRT, so try to make this fold: 9827 // x / (y * sqrt(z)) -> x * (rsqrt(z) / y) 9828 if (SDValue RV = buildRsqrtEstimate(SqrtOp.getOperand(0), Flags)) { 9829 RV = DAG.getNode(ISD::FDIV, SDLoc(N1), VT, RV, OtherOp, Flags); 9830 AddToWorklist(RV.getNode()); 9831 return DAG.getNode(ISD::FMUL, DL, VT, N0, RV, Flags); 9832 } 9833 } 9834 } 9835 9836 // Fold into a reciprocal estimate and multiply instead of a real divide. 9837 if (SDValue RV = BuildReciprocalEstimate(N1, Flags)) { 9838 AddToWorklist(RV.getNode()); 9839 return DAG.getNode(ISD::FMUL, DL, VT, N0, RV, Flags); 9840 } 9841 } 9842 9843 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y) 9844 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI, &Options)) { 9845 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI, &Options)) { 9846 // Both can be negated for free, check to see if at least one is cheaper 9847 // negated. 9848 if (LHSNeg == 2 || RHSNeg == 2) 9849 return DAG.getNode(ISD::FDIV, SDLoc(N), VT, 9850 GetNegatedExpression(N0, DAG, LegalOperations), 9851 GetNegatedExpression(N1, DAG, LegalOperations), 9852 Flags); 9853 } 9854 } 9855 9856 if (SDValue CombineRepeatedDivisors = combineRepeatedFPDivisors(N)) 9857 return CombineRepeatedDivisors; 9858 9859 return SDValue(); 9860 } 9861 9862 SDValue DAGCombiner::visitFREM(SDNode *N) { 9863 SDValue N0 = N->getOperand(0); 9864 SDValue N1 = N->getOperand(1); 9865 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); 9866 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); 9867 EVT VT = N->getValueType(0); 9868 9869 // fold (frem c1, c2) -> fmod(c1,c2) 9870 if (N0CFP && N1CFP) 9871 return DAG.getNode(ISD::FREM, SDLoc(N), VT, N0, N1, 9872 &cast<BinaryWithFlagsSDNode>(N)->Flags); 9873 9874 if (SDValue NewSel = foldBinOpIntoSelect(N)) 9875 return NewSel; 9876 9877 return SDValue(); 9878 } 9879 9880 SDValue DAGCombiner::visitFSQRT(SDNode *N) { 9881 if (!DAG.getTarget().Options.UnsafeFPMath) 9882 return SDValue(); 9883 9884 SDValue N0 = N->getOperand(0); 9885 if (TLI.isFsqrtCheap(N0, DAG)) 9886 return SDValue(); 9887 9888 // TODO: FSQRT nodes should have flags that propagate to the created nodes. 9889 // For now, create a Flags object for use with all unsafe math transforms. 9890 SDNodeFlags Flags; 9891 Flags.setUnsafeAlgebra(true); 9892 return buildSqrtEstimate(N0, &Flags); 9893 } 9894 9895 /// copysign(x, fp_extend(y)) -> copysign(x, y) 9896 /// copysign(x, fp_round(y)) -> copysign(x, y) 9897 static inline bool CanCombineFCOPYSIGN_EXTEND_ROUND(SDNode *N) { 9898 SDValue N1 = N->getOperand(1); 9899 if ((N1.getOpcode() == ISD::FP_EXTEND || 9900 N1.getOpcode() == ISD::FP_ROUND)) { 9901 // Do not optimize out type conversion of f128 type yet. 9902 // For some targets like x86_64, configuration is changed to keep one f128 9903 // value in one SSE register, but instruction selection cannot handle 9904 // FCOPYSIGN on SSE registers yet. 9905 EVT N1VT = N1->getValueType(0); 9906 EVT N1Op0VT = N1->getOperand(0)->getValueType(0); 9907 return (N1VT == N1Op0VT || N1Op0VT != MVT::f128); 9908 } 9909 return false; 9910 } 9911 9912 SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) { 9913 SDValue N0 = N->getOperand(0); 9914 SDValue N1 = N->getOperand(1); 9915 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); 9916 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); 9917 EVT VT = N->getValueType(0); 9918 9919 if (N0CFP && N1CFP) // Constant fold 9920 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, N0, N1); 9921 9922 if (N1CFP) { 9923 const APFloat &V = N1CFP->getValueAPF(); 9924 // copysign(x, c1) -> fabs(x) iff ispos(c1) 9925 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1) 9926 if (!V.isNegative()) { 9927 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT)) 9928 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0); 9929 } else { 9930 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT)) 9931 return DAG.getNode(ISD::FNEG, SDLoc(N), VT, 9932 DAG.getNode(ISD::FABS, SDLoc(N0), VT, N0)); 9933 } 9934 } 9935 9936 // copysign(fabs(x), y) -> copysign(x, y) 9937 // copysign(fneg(x), y) -> copysign(x, y) 9938 // copysign(copysign(x,z), y) -> copysign(x, y) 9939 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG || 9940 N0.getOpcode() == ISD::FCOPYSIGN) 9941 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, N0.getOperand(0), N1); 9942 9943 // copysign(x, abs(y)) -> abs(x) 9944 if (N1.getOpcode() == ISD::FABS) 9945 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0); 9946 9947 // copysign(x, copysign(y,z)) -> copysign(x, z) 9948 if (N1.getOpcode() == ISD::FCOPYSIGN) 9949 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, N0, N1.getOperand(1)); 9950 9951 // copysign(x, fp_extend(y)) -> copysign(x, y) 9952 // copysign(x, fp_round(y)) -> copysign(x, y) 9953 if (CanCombineFCOPYSIGN_EXTEND_ROUND(N)) 9954 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, N0, N1.getOperand(0)); 9955 9956 return SDValue(); 9957 } 9958 9959 SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) { 9960 SDValue N0 = N->getOperand(0); 9961 EVT VT = N->getValueType(0); 9962 EVT OpVT = N0.getValueType(); 9963 9964 // fold (sint_to_fp c1) -> c1fp 9965 if (DAG.isConstantIntBuildVectorOrConstantInt(N0) && 9966 // ...but only if the target supports immediate floating-point values 9967 (!LegalOperations || 9968 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) 9969 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0); 9970 9971 // If the input is a legal type, and SINT_TO_FP is not legal on this target, 9972 // but UINT_TO_FP is legal on this target, try to convert. 9973 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) && 9974 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) { 9975 // If the sign bit is known to be zero, we can change this to UINT_TO_FP. 9976 if (DAG.SignBitIsZero(N0)) 9977 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0); 9978 } 9979 9980 // The next optimizations are desirable only if SELECT_CC can be lowered. 9981 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT) || !LegalOperations) { 9982 // fold (sint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc) 9983 if (N0.getOpcode() == ISD::SETCC && N0.getValueType() == MVT::i1 && 9984 !VT.isVector() && 9985 (!LegalOperations || 9986 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) { 9987 SDLoc DL(N); 9988 SDValue Ops[] = 9989 { N0.getOperand(0), N0.getOperand(1), 9990 DAG.getConstantFP(-1.0, DL, VT), DAG.getConstantFP(0.0, DL, VT), 9991 N0.getOperand(2) }; 9992 return DAG.getNode(ISD::SELECT_CC, DL, VT, Ops); 9993 } 9994 9995 // fold (sint_to_fp (zext (setcc x, y, cc))) -> 9996 // (select_cc x, y, 1.0, 0.0,, cc) 9997 if (N0.getOpcode() == ISD::ZERO_EXTEND && 9998 N0.getOperand(0).getOpcode() == ISD::SETCC &&!VT.isVector() && 9999 (!LegalOperations || 10000 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) { 10001 SDLoc DL(N); 10002 SDValue Ops[] = 10003 { N0.getOperand(0).getOperand(0), N0.getOperand(0).getOperand(1), 10004 DAG.getConstantFP(1.0, DL, VT), DAG.getConstantFP(0.0, DL, VT), 10005 N0.getOperand(0).getOperand(2) }; 10006 return DAG.getNode(ISD::SELECT_CC, DL, VT, Ops); 10007 } 10008 } 10009 10010 return SDValue(); 10011 } 10012 10013 SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) { 10014 SDValue N0 = N->getOperand(0); 10015 EVT VT = N->getValueType(0); 10016 EVT OpVT = N0.getValueType(); 10017 10018 // fold (uint_to_fp c1) -> c1fp 10019 if (DAG.isConstantIntBuildVectorOrConstantInt(N0) && 10020 // ...but only if the target supports immediate floating-point values 10021 (!LegalOperations || 10022 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) 10023 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0); 10024 10025 // If the input is a legal type, and UINT_TO_FP is not legal on this target, 10026 // but SINT_TO_FP is legal on this target, try to convert. 10027 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) && 10028 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) { 10029 // If the sign bit is known to be zero, we can change this to SINT_TO_FP. 10030 if (DAG.SignBitIsZero(N0)) 10031 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0); 10032 } 10033 10034 // The next optimizations are desirable only if SELECT_CC can be lowered. 10035 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT) || !LegalOperations) { 10036 // fold (uint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc) 10037 10038 if (N0.getOpcode() == ISD::SETCC && !VT.isVector() && 10039 (!LegalOperations || 10040 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) { 10041 SDLoc DL(N); 10042 SDValue Ops[] = 10043 { N0.getOperand(0), N0.getOperand(1), 10044 DAG.getConstantFP(1.0, DL, VT), DAG.getConstantFP(0.0, DL, VT), 10045 N0.getOperand(2) }; 10046 return DAG.getNode(ISD::SELECT_CC, DL, VT, Ops); 10047 } 10048 } 10049 10050 return SDValue(); 10051 } 10052 10053 // Fold (fp_to_{s/u}int ({s/u}int_to_fpx)) -> zext x, sext x, trunc x, or x 10054 static SDValue FoldIntToFPToInt(SDNode *N, SelectionDAG &DAG) { 10055 SDValue N0 = N->getOperand(0); 10056 EVT VT = N->getValueType(0); 10057 10058 if (N0.getOpcode() != ISD::UINT_TO_FP && N0.getOpcode() != ISD::SINT_TO_FP) 10059 return SDValue(); 10060 10061 SDValue Src = N0.getOperand(0); 10062 EVT SrcVT = Src.getValueType(); 10063 bool IsInputSigned = N0.getOpcode() == ISD::SINT_TO_FP; 10064 bool IsOutputSigned = N->getOpcode() == ISD::FP_TO_SINT; 10065 10066 // We can safely assume the conversion won't overflow the output range, 10067 // because (for example) (uint8_t)18293.f is undefined behavior. 10068 10069 // Since we can assume the conversion won't overflow, our decision as to 10070 // whether the input will fit in the float should depend on the minimum 10071 // of the input range and output range. 10072 10073 // This means this is also safe for a signed input and unsigned output, since 10074 // a negative input would lead to undefined behavior. 10075 unsigned InputSize = (int)SrcVT.getScalarSizeInBits() - IsInputSigned; 10076 unsigned OutputSize = (int)VT.getScalarSizeInBits() - IsOutputSigned; 10077 unsigned ActualSize = std::min(InputSize, OutputSize); 10078 const fltSemantics &sem = DAG.EVTToAPFloatSemantics(N0.getValueType()); 10079 10080 // We can only fold away the float conversion if the input range can be 10081 // represented exactly in the float range. 10082 if (APFloat::semanticsPrecision(sem) >= ActualSize) { 10083 if (VT.getScalarSizeInBits() > SrcVT.getScalarSizeInBits()) { 10084 unsigned ExtOp = IsInputSigned && IsOutputSigned ? ISD::SIGN_EXTEND 10085 : ISD::ZERO_EXTEND; 10086 return DAG.getNode(ExtOp, SDLoc(N), VT, Src); 10087 } 10088 if (VT.getScalarSizeInBits() < SrcVT.getScalarSizeInBits()) 10089 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Src); 10090 return DAG.getBitcast(VT, Src); 10091 } 10092 return SDValue(); 10093 } 10094 10095 SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) { 10096 SDValue N0 = N->getOperand(0); 10097 EVT VT = N->getValueType(0); 10098 10099 // fold (fp_to_sint c1fp) -> c1 10100 if (isConstantFPBuildVectorOrConstantFP(N0)) 10101 return DAG.getNode(ISD::FP_TO_SINT, SDLoc(N), VT, N0); 10102 10103 return FoldIntToFPToInt(N, DAG); 10104 } 10105 10106 SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) { 10107 SDValue N0 = N->getOperand(0); 10108 EVT VT = N->getValueType(0); 10109 10110 // fold (fp_to_uint c1fp) -> c1 10111 if (isConstantFPBuildVectorOrConstantFP(N0)) 10112 return DAG.getNode(ISD::FP_TO_UINT, SDLoc(N), VT, N0); 10113 10114 return FoldIntToFPToInt(N, DAG); 10115 } 10116 10117 SDValue DAGCombiner::visitFP_ROUND(SDNode *N) { 10118 SDValue N0 = N->getOperand(0); 10119 SDValue N1 = N->getOperand(1); 10120 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); 10121 EVT VT = N->getValueType(0); 10122 10123 // fold (fp_round c1fp) -> c1fp 10124 if (N0CFP) 10125 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0, N1); 10126 10127 // fold (fp_round (fp_extend x)) -> x 10128 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType()) 10129 return N0.getOperand(0); 10130 10131 // fold (fp_round (fp_round x)) -> (fp_round x) 10132 if (N0.getOpcode() == ISD::FP_ROUND) { 10133 const bool NIsTrunc = N->getConstantOperandVal(1) == 1; 10134 const bool N0IsTrunc = N0.getConstantOperandVal(1) == 1; 10135 10136 // Skip this folding if it results in an fp_round from f80 to f16. 10137 // 10138 // f80 to f16 always generates an expensive (and as yet, unimplemented) 10139 // libcall to __truncxfhf2 instead of selecting native f16 conversion 10140 // instructions from f32 or f64. Moreover, the first (value-preserving) 10141 // fp_round from f80 to either f32 or f64 may become a NOP in platforms like 10142 // x86. 10143 if (N0.getOperand(0).getValueType() == MVT::f80 && VT == MVT::f16) 10144 return SDValue(); 10145 10146 // If the first fp_round isn't a value preserving truncation, it might 10147 // introduce a tie in the second fp_round, that wouldn't occur in the 10148 // single-step fp_round we want to fold to. 10149 // In other words, double rounding isn't the same as rounding. 10150 // Also, this is a value preserving truncation iff both fp_round's are. 10151 if (DAG.getTarget().Options.UnsafeFPMath || N0IsTrunc) { 10152 SDLoc DL(N); 10153 return DAG.getNode(ISD::FP_ROUND, DL, VT, N0.getOperand(0), 10154 DAG.getIntPtrConstant(NIsTrunc && N0IsTrunc, DL)); 10155 } 10156 } 10157 10158 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y) 10159 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) { 10160 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, SDLoc(N0), VT, 10161 N0.getOperand(0), N1); 10162 AddToWorklist(Tmp.getNode()); 10163 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, 10164 Tmp, N0.getOperand(1)); 10165 } 10166 10167 return SDValue(); 10168 } 10169 10170 SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) { 10171 SDValue N0 = N->getOperand(0); 10172 EVT VT = N->getValueType(0); 10173 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT(); 10174 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); 10175 10176 // fold (fp_round_inreg c1fp) -> c1fp 10177 if (N0CFP && isTypeLegal(EVT)) { 10178 SDLoc DL(N); 10179 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), DL, EVT); 10180 return DAG.getNode(ISD::FP_EXTEND, DL, VT, Round); 10181 } 10182 10183 return SDValue(); 10184 } 10185 10186 SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) { 10187 SDValue N0 = N->getOperand(0); 10188 EVT VT = N->getValueType(0); 10189 10190 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded. 10191 if (N->hasOneUse() && 10192 N->use_begin()->getOpcode() == ISD::FP_ROUND) 10193 return SDValue(); 10194 10195 // fold (fp_extend c1fp) -> c1fp 10196 if (isConstantFPBuildVectorOrConstantFP(N0)) 10197 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, N0); 10198 10199 // fold (fp_extend (fp16_to_fp op)) -> (fp16_to_fp op) 10200 if (N0.getOpcode() == ISD::FP16_TO_FP && 10201 TLI.getOperationAction(ISD::FP16_TO_FP, VT) == TargetLowering::Legal) 10202 return DAG.getNode(ISD::FP16_TO_FP, SDLoc(N), VT, N0.getOperand(0)); 10203 10204 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the 10205 // value of X. 10206 if (N0.getOpcode() == ISD::FP_ROUND 10207 && N0.getConstantOperandVal(1) == 1) { 10208 SDValue In = N0.getOperand(0); 10209 if (In.getValueType() == VT) return In; 10210 if (VT.bitsLT(In.getValueType())) 10211 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, 10212 In, N0.getOperand(1)); 10213 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, In); 10214 } 10215 10216 // fold (fpext (load x)) -> (fpext (fptrunc (extload x))) 10217 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() && 10218 TLI.isLoadExtLegal(ISD::EXTLOAD, VT, N0.getValueType())) { 10219 LoadSDNode *LN0 = cast<LoadSDNode>(N0); 10220 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT, 10221 LN0->getChain(), 10222 LN0->getBasePtr(), N0.getValueType(), 10223 LN0->getMemOperand()); 10224 CombineTo(N, ExtLoad); 10225 CombineTo(N0.getNode(), 10226 DAG.getNode(ISD::FP_ROUND, SDLoc(N0), 10227 N0.getValueType(), ExtLoad, 10228 DAG.getIntPtrConstant(1, SDLoc(N0))), 10229 ExtLoad.getValue(1)); 10230 return SDValue(N, 0); // Return N so it doesn't get rechecked! 10231 } 10232 10233 return SDValue(); 10234 } 10235 10236 SDValue DAGCombiner::visitFCEIL(SDNode *N) { 10237 SDValue N0 = N->getOperand(0); 10238 EVT VT = N->getValueType(0); 10239 10240 // fold (fceil c1) -> fceil(c1) 10241 if (isConstantFPBuildVectorOrConstantFP(N0)) 10242 return DAG.getNode(ISD::FCEIL, SDLoc(N), VT, N0); 10243 10244 return SDValue(); 10245 } 10246 10247 SDValue DAGCombiner::visitFTRUNC(SDNode *N) { 10248 SDValue N0 = N->getOperand(0); 10249 EVT VT = N->getValueType(0); 10250 10251 // fold (ftrunc c1) -> ftrunc(c1) 10252 if (isConstantFPBuildVectorOrConstantFP(N0)) 10253 return DAG.getNode(ISD::FTRUNC, SDLoc(N), VT, N0); 10254 10255 return SDValue(); 10256 } 10257 10258 SDValue DAGCombiner::visitFFLOOR(SDNode *N) { 10259 SDValue N0 = N->getOperand(0); 10260 EVT VT = N->getValueType(0); 10261 10262 // fold (ffloor c1) -> ffloor(c1) 10263 if (isConstantFPBuildVectorOrConstantFP(N0)) 10264 return DAG.getNode(ISD::FFLOOR, SDLoc(N), VT, N0); 10265 10266 return SDValue(); 10267 } 10268 10269 // FIXME: FNEG and FABS have a lot in common; refactor. 10270 SDValue DAGCombiner::visitFNEG(SDNode *N) { 10271 SDValue N0 = N->getOperand(0); 10272 EVT VT = N->getValueType(0); 10273 10274 // Constant fold FNEG. 10275 if (isConstantFPBuildVectorOrConstantFP(N0)) 10276 return DAG.getNode(ISD::FNEG, SDLoc(N), VT, N0); 10277 10278 if (isNegatibleForFree(N0, LegalOperations, DAG.getTargetLoweringInfo(), 10279 &DAG.getTarget().Options)) 10280 return GetNegatedExpression(N0, DAG, LegalOperations); 10281 10282 // Transform fneg(bitconvert(x)) -> bitconvert(x ^ sign) to avoid loading 10283 // constant pool values. 10284 if (!TLI.isFNegFree(VT) && 10285 N0.getOpcode() == ISD::BITCAST && 10286 N0.getNode()->hasOneUse()) { 10287 SDValue Int = N0.getOperand(0); 10288 EVT IntVT = Int.getValueType(); 10289 if (IntVT.isInteger() && !IntVT.isVector()) { 10290 APInt SignMask; 10291 if (N0.getValueType().isVector()) { 10292 // For a vector, get a mask such as 0x80... per scalar element 10293 // and splat it. 10294 SignMask = APInt::getSignBit(N0.getScalarValueSizeInBits()); 10295 SignMask = APInt::getSplat(IntVT.getSizeInBits(), SignMask); 10296 } else { 10297 // For a scalar, just generate 0x80... 10298 SignMask = APInt::getSignBit(IntVT.getSizeInBits()); 10299 } 10300 SDLoc DL0(N0); 10301 Int = DAG.getNode(ISD::XOR, DL0, IntVT, Int, 10302 DAG.getConstant(SignMask, DL0, IntVT)); 10303 AddToWorklist(Int.getNode()); 10304 return DAG.getBitcast(VT, Int); 10305 } 10306 } 10307 10308 // (fneg (fmul c, x)) -> (fmul -c, x) 10309 if (N0.getOpcode() == ISD::FMUL && 10310 (N0.getNode()->hasOneUse() || !TLI.isFNegFree(VT))) { 10311 ConstantFPSDNode *CFP1 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1)); 10312 if (CFP1) { 10313 APFloat CVal = CFP1->getValueAPF(); 10314 CVal.changeSign(); 10315 if (Level >= AfterLegalizeDAG && 10316 (TLI.isFPImmLegal(CVal, VT) || 10317 TLI.isOperationLegal(ISD::ConstantFP, VT))) 10318 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0.getOperand(0), 10319 DAG.getNode(ISD::FNEG, SDLoc(N), VT, 10320 N0.getOperand(1)), 10321 &cast<BinaryWithFlagsSDNode>(N0)->Flags); 10322 } 10323 } 10324 10325 return SDValue(); 10326 } 10327 10328 SDValue DAGCombiner::visitFMINNUM(SDNode *N) { 10329 SDValue N0 = N->getOperand(0); 10330 SDValue N1 = N->getOperand(1); 10331 EVT VT = N->getValueType(0); 10332 const ConstantFPSDNode *N0CFP = isConstOrConstSplatFP(N0); 10333 const ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1); 10334 10335 if (N0CFP && N1CFP) { 10336 const APFloat &C0 = N0CFP->getValueAPF(); 10337 const APFloat &C1 = N1CFP->getValueAPF(); 10338 return DAG.getConstantFP(minnum(C0, C1), SDLoc(N), VT); 10339 } 10340 10341 // Canonicalize to constant on RHS. 10342 if (isConstantFPBuildVectorOrConstantFP(N0) && 10343 !isConstantFPBuildVectorOrConstantFP(N1)) 10344 return DAG.getNode(ISD::FMINNUM, SDLoc(N), VT, N1, N0); 10345 10346 return SDValue(); 10347 } 10348 10349 SDValue DAGCombiner::visitFMAXNUM(SDNode *N) { 10350 SDValue N0 = N->getOperand(0); 10351 SDValue N1 = N->getOperand(1); 10352 EVT VT = N->getValueType(0); 10353 const ConstantFPSDNode *N0CFP = isConstOrConstSplatFP(N0); 10354 const ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1); 10355 10356 if (N0CFP && N1CFP) { 10357 const APFloat &C0 = N0CFP->getValueAPF(); 10358 const APFloat &C1 = N1CFP->getValueAPF(); 10359 return DAG.getConstantFP(maxnum(C0, C1), SDLoc(N), VT); 10360 } 10361 10362 // Canonicalize to constant on RHS. 10363 if (isConstantFPBuildVectorOrConstantFP(N0) && 10364 !isConstantFPBuildVectorOrConstantFP(N1)) 10365 return DAG.getNode(ISD::FMAXNUM, SDLoc(N), VT, N1, N0); 10366 10367 return SDValue(); 10368 } 10369 10370 SDValue DAGCombiner::visitFABS(SDNode *N) { 10371 SDValue N0 = N->getOperand(0); 10372 EVT VT = N->getValueType(0); 10373 10374 // fold (fabs c1) -> fabs(c1) 10375 if (isConstantFPBuildVectorOrConstantFP(N0)) 10376 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0); 10377 10378 // fold (fabs (fabs x)) -> (fabs x) 10379 if (N0.getOpcode() == ISD::FABS) 10380 return N->getOperand(0); 10381 10382 // fold (fabs (fneg x)) -> (fabs x) 10383 // fold (fabs (fcopysign x, y)) -> (fabs x) 10384 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN) 10385 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0.getOperand(0)); 10386 10387 // Transform fabs(bitconvert(x)) -> bitconvert(x & ~sign) to avoid loading 10388 // constant pool values. 10389 if (!TLI.isFAbsFree(VT) && 10390 N0.getOpcode() == ISD::BITCAST && 10391 N0.getNode()->hasOneUse()) { 10392 SDValue Int = N0.getOperand(0); 10393 EVT IntVT = Int.getValueType(); 10394 if (IntVT.isInteger() && !IntVT.isVector()) { 10395 APInt SignMask; 10396 if (N0.getValueType().isVector()) { 10397 // For a vector, get a mask such as 0x7f... per scalar element 10398 // and splat it. 10399 SignMask = ~APInt::getSignBit(N0.getScalarValueSizeInBits()); 10400 SignMask = APInt::getSplat(IntVT.getSizeInBits(), SignMask); 10401 } else { 10402 // For a scalar, just generate 0x7f... 10403 SignMask = ~APInt::getSignBit(IntVT.getSizeInBits()); 10404 } 10405 SDLoc DL(N0); 10406 Int = DAG.getNode(ISD::AND, DL, IntVT, Int, 10407 DAG.getConstant(SignMask, DL, IntVT)); 10408 AddToWorklist(Int.getNode()); 10409 return DAG.getBitcast(N->getValueType(0), Int); 10410 } 10411 } 10412 10413 return SDValue(); 10414 } 10415 10416 SDValue DAGCombiner::visitBRCOND(SDNode *N) { 10417 SDValue Chain = N->getOperand(0); 10418 SDValue N1 = N->getOperand(1); 10419 SDValue N2 = N->getOperand(2); 10420 10421 // If N is a constant we could fold this into a fallthrough or unconditional 10422 // branch. However that doesn't happen very often in normal code, because 10423 // Instcombine/SimplifyCFG should have handled the available opportunities. 10424 // If we did this folding here, it would be necessary to update the 10425 // MachineBasicBlock CFG, which is awkward. 10426 10427 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal 10428 // on the target. 10429 if (N1.getOpcode() == ISD::SETCC && 10430 TLI.isOperationLegalOrCustom(ISD::BR_CC, 10431 N1.getOperand(0).getValueType())) { 10432 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other, 10433 Chain, N1.getOperand(2), 10434 N1.getOperand(0), N1.getOperand(1), N2); 10435 } 10436 10437 if ((N1.hasOneUse() && N1.getOpcode() == ISD::SRL) || 10438 ((N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) && 10439 (N1.getOperand(0).hasOneUse() && 10440 N1.getOperand(0).getOpcode() == ISD::SRL))) { 10441 SDNode *Trunc = nullptr; 10442 if (N1.getOpcode() == ISD::TRUNCATE) { 10443 // Look pass the truncate. 10444 Trunc = N1.getNode(); 10445 N1 = N1.getOperand(0); 10446 } 10447 10448 // Match this pattern so that we can generate simpler code: 10449 // 10450 // %a = ... 10451 // %b = and i32 %a, 2 10452 // %c = srl i32 %b, 1 10453 // brcond i32 %c ... 10454 // 10455 // into 10456 // 10457 // %a = ... 10458 // %b = and i32 %a, 2 10459 // %c = setcc eq %b, 0 10460 // brcond %c ... 10461 // 10462 // This applies only when the AND constant value has one bit set and the 10463 // SRL constant is equal to the log2 of the AND constant. The back-end is 10464 // smart enough to convert the result into a TEST/JMP sequence. 10465 SDValue Op0 = N1.getOperand(0); 10466 SDValue Op1 = N1.getOperand(1); 10467 10468 if (Op0.getOpcode() == ISD::AND && 10469 Op1.getOpcode() == ISD::Constant) { 10470 SDValue AndOp1 = Op0.getOperand(1); 10471 10472 if (AndOp1.getOpcode() == ISD::Constant) { 10473 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue(); 10474 10475 if (AndConst.isPowerOf2() && 10476 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) { 10477 SDLoc DL(N); 10478 SDValue SetCC = 10479 DAG.getSetCC(DL, 10480 getSetCCResultType(Op0.getValueType()), 10481 Op0, DAG.getConstant(0, DL, Op0.getValueType()), 10482 ISD::SETNE); 10483 10484 SDValue NewBRCond = DAG.getNode(ISD::BRCOND, DL, 10485 MVT::Other, Chain, SetCC, N2); 10486 // Don't add the new BRCond into the worklist or else SimplifySelectCC 10487 // will convert it back to (X & C1) >> C2. 10488 CombineTo(N, NewBRCond, false); 10489 // Truncate is dead. 10490 if (Trunc) 10491 deleteAndRecombine(Trunc); 10492 // Replace the uses of SRL with SETCC 10493 WorklistRemover DeadNodes(*this); 10494 DAG.ReplaceAllUsesOfValueWith(N1, SetCC); 10495 deleteAndRecombine(N1.getNode()); 10496 return SDValue(N, 0); // Return N so it doesn't get rechecked! 10497 } 10498 } 10499 } 10500 10501 if (Trunc) 10502 // Restore N1 if the above transformation doesn't match. 10503 N1 = N->getOperand(1); 10504 } 10505 10506 // Transform br(xor(x, y)) -> br(x != y) 10507 // Transform br(xor(xor(x,y), 1)) -> br (x == y) 10508 if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) { 10509 SDNode *TheXor = N1.getNode(); 10510 SDValue Op0 = TheXor->getOperand(0); 10511 SDValue Op1 = TheXor->getOperand(1); 10512 if (Op0.getOpcode() == Op1.getOpcode()) { 10513 // Avoid missing important xor optimizations. 10514 if (SDValue Tmp = visitXOR(TheXor)) { 10515 if (Tmp.getNode() != TheXor) { 10516 DEBUG(dbgs() << "\nReplacing.8 "; 10517 TheXor->dump(&DAG); 10518 dbgs() << "\nWith: "; 10519 Tmp.getNode()->dump(&DAG); 10520 dbgs() << '\n'); 10521 WorklistRemover DeadNodes(*this); 10522 DAG.ReplaceAllUsesOfValueWith(N1, Tmp); 10523 deleteAndRecombine(TheXor); 10524 return DAG.getNode(ISD::BRCOND, SDLoc(N), 10525 MVT::Other, Chain, Tmp, N2); 10526 } 10527 10528 // visitXOR has changed XOR's operands or replaced the XOR completely, 10529 // bail out. 10530 return SDValue(N, 0); 10531 } 10532 } 10533 10534 if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) { 10535 bool Equal = false; 10536 if (isOneConstant(Op0) && Op0.hasOneUse() && 10537 Op0.getOpcode() == ISD::XOR) { 10538 TheXor = Op0.getNode(); 10539 Equal = true; 10540 } 10541 10542 EVT SetCCVT = N1.getValueType(); 10543 if (LegalTypes) 10544 SetCCVT = getSetCCResultType(SetCCVT); 10545 SDValue SetCC = DAG.getSetCC(SDLoc(TheXor), 10546 SetCCVT, 10547 Op0, Op1, 10548 Equal ? ISD::SETEQ : ISD::SETNE); 10549 // Replace the uses of XOR with SETCC 10550 WorklistRemover DeadNodes(*this); 10551 DAG.ReplaceAllUsesOfValueWith(N1, SetCC); 10552 deleteAndRecombine(N1.getNode()); 10553 return DAG.getNode(ISD::BRCOND, SDLoc(N), 10554 MVT::Other, Chain, SetCC, N2); 10555 } 10556 } 10557 10558 return SDValue(); 10559 } 10560 10561 // Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB. 10562 // 10563 SDValue DAGCombiner::visitBR_CC(SDNode *N) { 10564 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1)); 10565 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3); 10566 10567 // If N is a constant we could fold this into a fallthrough or unconditional 10568 // branch. However that doesn't happen very often in normal code, because 10569 // Instcombine/SimplifyCFG should have handled the available opportunities. 10570 // If we did this folding here, it would be necessary to update the 10571 // MachineBasicBlock CFG, which is awkward. 10572 10573 // Use SimplifySetCC to simplify SETCC's. 10574 SDValue Simp = SimplifySetCC(getSetCCResultType(CondLHS.getValueType()), 10575 CondLHS, CondRHS, CC->get(), SDLoc(N), 10576 false); 10577 if (Simp.getNode()) AddToWorklist(Simp.getNode()); 10578 10579 // fold to a simpler setcc 10580 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC) 10581 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other, 10582 N->getOperand(0), Simp.getOperand(2), 10583 Simp.getOperand(0), Simp.getOperand(1), 10584 N->getOperand(4)); 10585 10586 return SDValue(); 10587 } 10588 10589 /// Return true if 'Use' is a load or a store that uses N as its base pointer 10590 /// and that N may be folded in the load / store addressing mode. 10591 static bool canFoldInAddressingMode(SDNode *N, SDNode *Use, 10592 SelectionDAG &DAG, 10593 const TargetLowering &TLI) { 10594 EVT VT; 10595 unsigned AS; 10596 10597 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Use)) { 10598 if (LD->isIndexed() || LD->getBasePtr().getNode() != N) 10599 return false; 10600 VT = LD->getMemoryVT(); 10601 AS = LD->getAddressSpace(); 10602 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(Use)) { 10603 if (ST->isIndexed() || ST->getBasePtr().getNode() != N) 10604 return false; 10605 VT = ST->getMemoryVT(); 10606 AS = ST->getAddressSpace(); 10607 } else 10608 return false; 10609 10610 TargetLowering::AddrMode AM; 10611 if (N->getOpcode() == ISD::ADD) { 10612 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1)); 10613 if (Offset) 10614 // [reg +/- imm] 10615 AM.BaseOffs = Offset->getSExtValue(); 10616 else 10617 // [reg +/- reg] 10618 AM.Scale = 1; 10619 } else if (N->getOpcode() == ISD::SUB) { 10620 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1)); 10621 if (Offset) 10622 // [reg +/- imm] 10623 AM.BaseOffs = -Offset->getSExtValue(); 10624 else 10625 // [reg +/- reg] 10626 AM.Scale = 1; 10627 } else 10628 return false; 10629 10630 return TLI.isLegalAddressingMode(DAG.getDataLayout(), AM, 10631 VT.getTypeForEVT(*DAG.getContext()), AS); 10632 } 10633 10634 /// Try turning a load/store into a pre-indexed load/store when the base 10635 /// pointer is an add or subtract and it has other uses besides the load/store. 10636 /// After the transformation, the new indexed load/store has effectively folded 10637 /// the add/subtract in and all of its other uses are redirected to the 10638 /// new load/store. 10639 bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) { 10640 if (Level < AfterLegalizeDAG) 10641 return false; 10642 10643 bool isLoad = true; 10644 SDValue Ptr; 10645 EVT VT; 10646 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 10647 if (LD->isIndexed()) 10648 return false; 10649 VT = LD->getMemoryVT(); 10650 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) && 10651 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT)) 10652 return false; 10653 Ptr = LD->getBasePtr(); 10654 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 10655 if (ST->isIndexed()) 10656 return false; 10657 VT = ST->getMemoryVT(); 10658 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) && 10659 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT)) 10660 return false; 10661 Ptr = ST->getBasePtr(); 10662 isLoad = false; 10663 } else { 10664 return false; 10665 } 10666 10667 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail 10668 // out. There is no reason to make this a preinc/predec. 10669 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) || 10670 Ptr.getNode()->hasOneUse()) 10671 return false; 10672 10673 // Ask the target to do addressing mode selection. 10674 SDValue BasePtr; 10675 SDValue Offset; 10676 ISD::MemIndexedMode AM = ISD::UNINDEXED; 10677 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG)) 10678 return false; 10679 10680 // Backends without true r+i pre-indexed forms may need to pass a 10681 // constant base with a variable offset so that constant coercion 10682 // will work with the patterns in canonical form. 10683 bool Swapped = false; 10684 if (isa<ConstantSDNode>(BasePtr)) { 10685 std::swap(BasePtr, Offset); 10686 Swapped = true; 10687 } 10688 10689 // Don't create a indexed load / store with zero offset. 10690 if (isNullConstant(Offset)) 10691 return false; 10692 10693 // Try turning it into a pre-indexed load / store except when: 10694 // 1) The new base ptr is a frame index. 10695 // 2) If N is a store and the new base ptr is either the same as or is a 10696 // predecessor of the value being stored. 10697 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded 10698 // that would create a cycle. 10699 // 4) All uses are load / store ops that use it as old base ptr. 10700 10701 // Check #1. Preinc'ing a frame index would require copying the stack pointer 10702 // (plus the implicit offset) to a register to preinc anyway. 10703 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr)) 10704 return false; 10705 10706 // Check #2. 10707 if (!isLoad) { 10708 SDValue Val = cast<StoreSDNode>(N)->getValue(); 10709 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode())) 10710 return false; 10711 } 10712 10713 // Caches for hasPredecessorHelper. 10714 SmallPtrSet<const SDNode *, 32> Visited; 10715 SmallVector<const SDNode *, 16> Worklist; 10716 Worklist.push_back(N); 10717 10718 // If the offset is a constant, there may be other adds of constants that 10719 // can be folded with this one. We should do this to avoid having to keep 10720 // a copy of the original base pointer. 10721 SmallVector<SDNode *, 16> OtherUses; 10722 if (isa<ConstantSDNode>(Offset)) 10723 for (SDNode::use_iterator UI = BasePtr.getNode()->use_begin(), 10724 UE = BasePtr.getNode()->use_end(); 10725 UI != UE; ++UI) { 10726 SDUse &Use = UI.getUse(); 10727 // Skip the use that is Ptr and uses of other results from BasePtr's 10728 // node (important for nodes that return multiple results). 10729 if (Use.getUser() == Ptr.getNode() || Use != BasePtr) 10730 continue; 10731 10732 if (SDNode::hasPredecessorHelper(Use.getUser(), Visited, Worklist)) 10733 continue; 10734 10735 if (Use.getUser()->getOpcode() != ISD::ADD && 10736 Use.getUser()->getOpcode() != ISD::SUB) { 10737 OtherUses.clear(); 10738 break; 10739 } 10740 10741 SDValue Op1 = Use.getUser()->getOperand((UI.getOperandNo() + 1) & 1); 10742 if (!isa<ConstantSDNode>(Op1)) { 10743 OtherUses.clear(); 10744 break; 10745 } 10746 10747 // FIXME: In some cases, we can be smarter about this. 10748 if (Op1.getValueType() != Offset.getValueType()) { 10749 OtherUses.clear(); 10750 break; 10751 } 10752 10753 OtherUses.push_back(Use.getUser()); 10754 } 10755 10756 if (Swapped) 10757 std::swap(BasePtr, Offset); 10758 10759 // Now check for #3 and #4. 10760 bool RealUse = false; 10761 10762 for (SDNode *Use : Ptr.getNode()->uses()) { 10763 if (Use == N) 10764 continue; 10765 if (SDNode::hasPredecessorHelper(Use, Visited, Worklist)) 10766 return false; 10767 10768 // If Ptr may be folded in addressing mode of other use, then it's 10769 // not profitable to do this transformation. 10770 if (!canFoldInAddressingMode(Ptr.getNode(), Use, DAG, TLI)) 10771 RealUse = true; 10772 } 10773 10774 if (!RealUse) 10775 return false; 10776 10777 SDValue Result; 10778 if (isLoad) 10779 Result = DAG.getIndexedLoad(SDValue(N,0), SDLoc(N), 10780 BasePtr, Offset, AM); 10781 else 10782 Result = DAG.getIndexedStore(SDValue(N,0), SDLoc(N), 10783 BasePtr, Offset, AM); 10784 ++PreIndexedNodes; 10785 ++NodesCombined; 10786 DEBUG(dbgs() << "\nReplacing.4 "; 10787 N->dump(&DAG); 10788 dbgs() << "\nWith: "; 10789 Result.getNode()->dump(&DAG); 10790 dbgs() << '\n'); 10791 WorklistRemover DeadNodes(*this); 10792 if (isLoad) { 10793 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0)); 10794 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2)); 10795 } else { 10796 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1)); 10797 } 10798 10799 // Finally, since the node is now dead, remove it from the graph. 10800 deleteAndRecombine(N); 10801 10802 if (Swapped) 10803 std::swap(BasePtr, Offset); 10804 10805 // Replace other uses of BasePtr that can be updated to use Ptr 10806 for (unsigned i = 0, e = OtherUses.size(); i != e; ++i) { 10807 unsigned OffsetIdx = 1; 10808 if (OtherUses[i]->getOperand(OffsetIdx).getNode() == BasePtr.getNode()) 10809 OffsetIdx = 0; 10810 assert(OtherUses[i]->getOperand(!OffsetIdx).getNode() == 10811 BasePtr.getNode() && "Expected BasePtr operand"); 10812 10813 // We need to replace ptr0 in the following expression: 10814 // x0 * offset0 + y0 * ptr0 = t0 10815 // knowing that 10816 // x1 * offset1 + y1 * ptr0 = t1 (the indexed load/store) 10817 // 10818 // where x0, x1, y0 and y1 in {-1, 1} are given by the types of the 10819 // indexed load/store and the expresion that needs to be re-written. 10820 // 10821 // Therefore, we have: 10822 // t0 = (x0 * offset0 - x1 * y0 * y1 *offset1) + (y0 * y1) * t1 10823 10824 ConstantSDNode *CN = 10825 cast<ConstantSDNode>(OtherUses[i]->getOperand(OffsetIdx)); 10826 int X0, X1, Y0, Y1; 10827 const APInt &Offset0 = CN->getAPIntValue(); 10828 APInt Offset1 = cast<ConstantSDNode>(Offset)->getAPIntValue(); 10829 10830 X0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 1) ? -1 : 1; 10831 Y0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 0) ? -1 : 1; 10832 X1 = (AM == ISD::PRE_DEC && !Swapped) ? -1 : 1; 10833 Y1 = (AM == ISD::PRE_DEC && Swapped) ? -1 : 1; 10834 10835 unsigned Opcode = (Y0 * Y1 < 0) ? ISD::SUB : ISD::ADD; 10836 10837 APInt CNV = Offset0; 10838 if (X0 < 0) CNV = -CNV; 10839 if (X1 * Y0 * Y1 < 0) CNV = CNV + Offset1; 10840 else CNV = CNV - Offset1; 10841 10842 SDLoc DL(OtherUses[i]); 10843 10844 // We can now generate the new expression. 10845 SDValue NewOp1 = DAG.getConstant(CNV, DL, CN->getValueType(0)); 10846 SDValue NewOp2 = Result.getValue(isLoad ? 1 : 0); 10847 10848 SDValue NewUse = DAG.getNode(Opcode, 10849 DL, 10850 OtherUses[i]->getValueType(0), NewOp1, NewOp2); 10851 DAG.ReplaceAllUsesOfValueWith(SDValue(OtherUses[i], 0), NewUse); 10852 deleteAndRecombine(OtherUses[i]); 10853 } 10854 10855 // Replace the uses of Ptr with uses of the updated base value. 10856 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0)); 10857 deleteAndRecombine(Ptr.getNode()); 10858 10859 return true; 10860 } 10861 10862 /// Try to combine a load/store with a add/sub of the base pointer node into a 10863 /// post-indexed load/store. The transformation folded the add/subtract into the 10864 /// new indexed load/store effectively and all of its uses are redirected to the 10865 /// new load/store. 10866 bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) { 10867 if (Level < AfterLegalizeDAG) 10868 return false; 10869 10870 bool isLoad = true; 10871 SDValue Ptr; 10872 EVT VT; 10873 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 10874 if (LD->isIndexed()) 10875 return false; 10876 VT = LD->getMemoryVT(); 10877 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) && 10878 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT)) 10879 return false; 10880 Ptr = LD->getBasePtr(); 10881 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 10882 if (ST->isIndexed()) 10883 return false; 10884 VT = ST->getMemoryVT(); 10885 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) && 10886 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT)) 10887 return false; 10888 Ptr = ST->getBasePtr(); 10889 isLoad = false; 10890 } else { 10891 return false; 10892 } 10893 10894 if (Ptr.getNode()->hasOneUse()) 10895 return false; 10896 10897 for (SDNode *Op : Ptr.getNode()->uses()) { 10898 if (Op == N || 10899 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB)) 10900 continue; 10901 10902 SDValue BasePtr; 10903 SDValue Offset; 10904 ISD::MemIndexedMode AM = ISD::UNINDEXED; 10905 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) { 10906 // Don't create a indexed load / store with zero offset. 10907 if (isNullConstant(Offset)) 10908 continue; 10909 10910 // Try turning it into a post-indexed load / store except when 10911 // 1) All uses are load / store ops that use it as base ptr (and 10912 // it may be folded as addressing mmode). 10913 // 2) Op must be independent of N, i.e. Op is neither a predecessor 10914 // nor a successor of N. Otherwise, if Op is folded that would 10915 // create a cycle. 10916 10917 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr)) 10918 continue; 10919 10920 // Check for #1. 10921 bool TryNext = false; 10922 for (SDNode *Use : BasePtr.getNode()->uses()) { 10923 if (Use == Ptr.getNode()) 10924 continue; 10925 10926 // If all the uses are load / store addresses, then don't do the 10927 // transformation. 10928 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){ 10929 bool RealUse = false; 10930 for (SDNode *UseUse : Use->uses()) { 10931 if (!canFoldInAddressingMode(Use, UseUse, DAG, TLI)) 10932 RealUse = true; 10933 } 10934 10935 if (!RealUse) { 10936 TryNext = true; 10937 break; 10938 } 10939 } 10940 } 10941 10942 if (TryNext) 10943 continue; 10944 10945 // Check for #2 10946 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) { 10947 SDValue Result = isLoad 10948 ? DAG.getIndexedLoad(SDValue(N,0), SDLoc(N), 10949 BasePtr, Offset, AM) 10950 : DAG.getIndexedStore(SDValue(N,0), SDLoc(N), 10951 BasePtr, Offset, AM); 10952 ++PostIndexedNodes; 10953 ++NodesCombined; 10954 DEBUG(dbgs() << "\nReplacing.5 "; 10955 N->dump(&DAG); 10956 dbgs() << "\nWith: "; 10957 Result.getNode()->dump(&DAG); 10958 dbgs() << '\n'); 10959 WorklistRemover DeadNodes(*this); 10960 if (isLoad) { 10961 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0)); 10962 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2)); 10963 } else { 10964 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1)); 10965 } 10966 10967 // Finally, since the node is now dead, remove it from the graph. 10968 deleteAndRecombine(N); 10969 10970 // Replace the uses of Use with uses of the updated base value. 10971 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0), 10972 Result.getValue(isLoad ? 1 : 0)); 10973 deleteAndRecombine(Op); 10974 return true; 10975 } 10976 } 10977 } 10978 10979 return false; 10980 } 10981 10982 /// \brief Return the base-pointer arithmetic from an indexed \p LD. 10983 SDValue DAGCombiner::SplitIndexingFromLoad(LoadSDNode *LD) { 10984 ISD::MemIndexedMode AM = LD->getAddressingMode(); 10985 assert(AM != ISD::UNINDEXED); 10986 SDValue BP = LD->getOperand(1); 10987 SDValue Inc = LD->getOperand(2); 10988 10989 // Some backends use TargetConstants for load offsets, but don't expect 10990 // TargetConstants in general ADD nodes. We can convert these constants into 10991 // regular Constants (if the constant is not opaque). 10992 assert((Inc.getOpcode() != ISD::TargetConstant || 10993 !cast<ConstantSDNode>(Inc)->isOpaque()) && 10994 "Cannot split out indexing using opaque target constants"); 10995 if (Inc.getOpcode() == ISD::TargetConstant) { 10996 ConstantSDNode *ConstInc = cast<ConstantSDNode>(Inc); 10997 Inc = DAG.getConstant(*ConstInc->getConstantIntValue(), SDLoc(Inc), 10998 ConstInc->getValueType(0)); 10999 } 11000 11001 unsigned Opc = 11002 (AM == ISD::PRE_INC || AM == ISD::POST_INC ? ISD::ADD : ISD::SUB); 11003 return DAG.getNode(Opc, SDLoc(LD), BP.getSimpleValueType(), BP, Inc); 11004 } 11005 11006 SDValue DAGCombiner::visitLOAD(SDNode *N) { 11007 LoadSDNode *LD = cast<LoadSDNode>(N); 11008 SDValue Chain = LD->getChain(); 11009 SDValue Ptr = LD->getBasePtr(); 11010 11011 // If load is not volatile and there are no uses of the loaded value (and 11012 // the updated indexed value in case of indexed loads), change uses of the 11013 // chain value into uses of the chain input (i.e. delete the dead load). 11014 if (!LD->isVolatile()) { 11015 if (N->getValueType(1) == MVT::Other) { 11016 // Unindexed loads. 11017 if (!N->hasAnyUseOfValue(0)) { 11018 // It's not safe to use the two value CombineTo variant here. e.g. 11019 // v1, chain2 = load chain1, loc 11020 // v2, chain3 = load chain2, loc 11021 // v3 = add v2, c 11022 // Now we replace use of chain2 with chain1. This makes the second load 11023 // isomorphic to the one we are deleting, and thus makes this load live. 11024 DEBUG(dbgs() << "\nReplacing.6 "; 11025 N->dump(&DAG); 11026 dbgs() << "\nWith chain: "; 11027 Chain.getNode()->dump(&DAG); 11028 dbgs() << "\n"); 11029 WorklistRemover DeadNodes(*this); 11030 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain); 11031 AddUsersToWorklist(Chain.getNode()); 11032 if (N->use_empty()) 11033 deleteAndRecombine(N); 11034 11035 return SDValue(N, 0); // Return N so it doesn't get rechecked! 11036 } 11037 } else { 11038 // Indexed loads. 11039 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?"); 11040 11041 // If this load has an opaque TargetConstant offset, then we cannot split 11042 // the indexing into an add/sub directly (that TargetConstant may not be 11043 // valid for a different type of node, and we cannot convert an opaque 11044 // target constant into a regular constant). 11045 bool HasOTCInc = LD->getOperand(2).getOpcode() == ISD::TargetConstant && 11046 cast<ConstantSDNode>(LD->getOperand(2))->isOpaque(); 11047 11048 if (!N->hasAnyUseOfValue(0) && 11049 ((MaySplitLoadIndex && !HasOTCInc) || !N->hasAnyUseOfValue(1))) { 11050 SDValue Undef = DAG.getUNDEF(N->getValueType(0)); 11051 SDValue Index; 11052 if (N->hasAnyUseOfValue(1) && MaySplitLoadIndex && !HasOTCInc) { 11053 Index = SplitIndexingFromLoad(LD); 11054 // Try to fold the base pointer arithmetic into subsequent loads and 11055 // stores. 11056 AddUsersToWorklist(N); 11057 } else 11058 Index = DAG.getUNDEF(N->getValueType(1)); 11059 DEBUG(dbgs() << "\nReplacing.7 "; 11060 N->dump(&DAG); 11061 dbgs() << "\nWith: "; 11062 Undef.getNode()->dump(&DAG); 11063 dbgs() << " and 2 other values\n"); 11064 WorklistRemover DeadNodes(*this); 11065 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef); 11066 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Index); 11067 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain); 11068 deleteAndRecombine(N); 11069 return SDValue(N, 0); // Return N so it doesn't get rechecked! 11070 } 11071 } 11072 } 11073 11074 // If this load is directly stored, replace the load value with the stored 11075 // value. 11076 // TODO: Handle store large -> read small portion. 11077 // TODO: Handle TRUNCSTORE/LOADEXT 11078 if (OptLevel != CodeGenOpt::None && 11079 ISD::isNormalLoad(N) && !LD->isVolatile()) { 11080 if (ISD::isNON_TRUNCStore(Chain.getNode())) { 11081 StoreSDNode *PrevST = cast<StoreSDNode>(Chain); 11082 if (PrevST->getBasePtr() == Ptr && 11083 PrevST->getValue().getValueType() == N->getValueType(0)) 11084 return CombineTo(N, PrevST->getOperand(1), Chain); 11085 } 11086 } 11087 11088 // Try to infer better alignment information than the load already has. 11089 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) { 11090 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) { 11091 if (Align > LD->getMemOperand()->getBaseAlignment()) { 11092 SDValue NewLoad = DAG.getExtLoad( 11093 LD->getExtensionType(), SDLoc(N), LD->getValueType(0), Chain, Ptr, 11094 LD->getPointerInfo(), LD->getMemoryVT(), Align, 11095 LD->getMemOperand()->getFlags(), LD->getAAInfo()); 11096 if (NewLoad.getNode() != N) 11097 return CombineTo(N, NewLoad, SDValue(NewLoad.getNode(), 1), true); 11098 } 11099 } 11100 } 11101 11102 if (LD->isUnindexed()) { 11103 // Walk up chain skipping non-aliasing memory nodes. 11104 SDValue BetterChain = FindBetterChain(N, Chain); 11105 11106 // If there is a better chain. 11107 if (Chain != BetterChain) { 11108 SDValue ReplLoad; 11109 11110 // Replace the chain to void dependency. 11111 if (LD->getExtensionType() == ISD::NON_EXTLOAD) { 11112 ReplLoad = DAG.getLoad(N->getValueType(0), SDLoc(LD), 11113 BetterChain, Ptr, LD->getMemOperand()); 11114 } else { 11115 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), 11116 LD->getValueType(0), 11117 BetterChain, Ptr, LD->getMemoryVT(), 11118 LD->getMemOperand()); 11119 } 11120 11121 // Create token factor to keep old chain connected. 11122 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N), 11123 MVT::Other, Chain, ReplLoad.getValue(1)); 11124 11125 // Make sure the new and old chains are cleaned up. 11126 AddToWorklist(Token.getNode()); 11127 11128 // Replace uses with load result and token factor. Don't add users 11129 // to work list. 11130 return CombineTo(N, ReplLoad.getValue(0), Token, false); 11131 } 11132 } 11133 11134 // Try transforming N to an indexed load. 11135 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N)) 11136 return SDValue(N, 0); 11137 11138 // Try to slice up N to more direct loads if the slices are mapped to 11139 // different register banks or pairing can take place. 11140 if (SliceUpLoad(N)) 11141 return SDValue(N, 0); 11142 11143 return SDValue(); 11144 } 11145 11146 namespace { 11147 /// \brief Helper structure used to slice a load in smaller loads. 11148 /// Basically a slice is obtained from the following sequence: 11149 /// Origin = load Ty1, Base 11150 /// Shift = srl Ty1 Origin, CstTy Amount 11151 /// Inst = trunc Shift to Ty2 11152 /// 11153 /// Then, it will be rewriten into: 11154 /// Slice = load SliceTy, Base + SliceOffset 11155 /// [Inst = zext Slice to Ty2], only if SliceTy <> Ty2 11156 /// 11157 /// SliceTy is deduced from the number of bits that are actually used to 11158 /// build Inst. 11159 struct LoadedSlice { 11160 /// \brief Helper structure used to compute the cost of a slice. 11161 struct Cost { 11162 /// Are we optimizing for code size. 11163 bool ForCodeSize; 11164 /// Various cost. 11165 unsigned Loads; 11166 unsigned Truncates; 11167 unsigned CrossRegisterBanksCopies; 11168 unsigned ZExts; 11169 unsigned Shift; 11170 11171 Cost(bool ForCodeSize = false) 11172 : ForCodeSize(ForCodeSize), Loads(0), Truncates(0), 11173 CrossRegisterBanksCopies(0), ZExts(0), Shift(0) {} 11174 11175 /// \brief Get the cost of one isolated slice. 11176 Cost(const LoadedSlice &LS, bool ForCodeSize = false) 11177 : ForCodeSize(ForCodeSize), Loads(1), Truncates(0), 11178 CrossRegisterBanksCopies(0), ZExts(0), Shift(0) { 11179 EVT TruncType = LS.Inst->getValueType(0); 11180 EVT LoadedType = LS.getLoadedType(); 11181 if (TruncType != LoadedType && 11182 !LS.DAG->getTargetLoweringInfo().isZExtFree(LoadedType, TruncType)) 11183 ZExts = 1; 11184 } 11185 11186 /// \brief Account for slicing gain in the current cost. 11187 /// Slicing provide a few gains like removing a shift or a 11188 /// truncate. This method allows to grow the cost of the original 11189 /// load with the gain from this slice. 11190 void addSliceGain(const LoadedSlice &LS) { 11191 // Each slice saves a truncate. 11192 const TargetLowering &TLI = LS.DAG->getTargetLoweringInfo(); 11193 if (!TLI.isTruncateFree(LS.Inst->getOperand(0).getValueType(), 11194 LS.Inst->getValueType(0))) 11195 ++Truncates; 11196 // If there is a shift amount, this slice gets rid of it. 11197 if (LS.Shift) 11198 ++Shift; 11199 // If this slice can merge a cross register bank copy, account for it. 11200 if (LS.canMergeExpensiveCrossRegisterBankCopy()) 11201 ++CrossRegisterBanksCopies; 11202 } 11203 11204 Cost &operator+=(const Cost &RHS) { 11205 Loads += RHS.Loads; 11206 Truncates += RHS.Truncates; 11207 CrossRegisterBanksCopies += RHS.CrossRegisterBanksCopies; 11208 ZExts += RHS.ZExts; 11209 Shift += RHS.Shift; 11210 return *this; 11211 } 11212 11213 bool operator==(const Cost &RHS) const { 11214 return Loads == RHS.Loads && Truncates == RHS.Truncates && 11215 CrossRegisterBanksCopies == RHS.CrossRegisterBanksCopies && 11216 ZExts == RHS.ZExts && Shift == RHS.Shift; 11217 } 11218 11219 bool operator!=(const Cost &RHS) const { return !(*this == RHS); } 11220 11221 bool operator<(const Cost &RHS) const { 11222 // Assume cross register banks copies are as expensive as loads. 11223 // FIXME: Do we want some more target hooks? 11224 unsigned ExpensiveOpsLHS = Loads + CrossRegisterBanksCopies; 11225 unsigned ExpensiveOpsRHS = RHS.Loads + RHS.CrossRegisterBanksCopies; 11226 // Unless we are optimizing for code size, consider the 11227 // expensive operation first. 11228 if (!ForCodeSize && ExpensiveOpsLHS != ExpensiveOpsRHS) 11229 return ExpensiveOpsLHS < ExpensiveOpsRHS; 11230 return (Truncates + ZExts + Shift + ExpensiveOpsLHS) < 11231 (RHS.Truncates + RHS.ZExts + RHS.Shift + ExpensiveOpsRHS); 11232 } 11233 11234 bool operator>(const Cost &RHS) const { return RHS < *this; } 11235 11236 bool operator<=(const Cost &RHS) const { return !(RHS < *this); } 11237 11238 bool operator>=(const Cost &RHS) const { return !(*this < RHS); } 11239 }; 11240 // The last instruction that represent the slice. This should be a 11241 // truncate instruction. 11242 SDNode *Inst; 11243 // The original load instruction. 11244 LoadSDNode *Origin; 11245 // The right shift amount in bits from the original load. 11246 unsigned Shift; 11247 // The DAG from which Origin came from. 11248 // This is used to get some contextual information about legal types, etc. 11249 SelectionDAG *DAG; 11250 11251 LoadedSlice(SDNode *Inst = nullptr, LoadSDNode *Origin = nullptr, 11252 unsigned Shift = 0, SelectionDAG *DAG = nullptr) 11253 : Inst(Inst), Origin(Origin), Shift(Shift), DAG(DAG) {} 11254 11255 /// \brief Get the bits used in a chunk of bits \p BitWidth large. 11256 /// \return Result is \p BitWidth and has used bits set to 1 and 11257 /// not used bits set to 0. 11258 APInt getUsedBits() const { 11259 // Reproduce the trunc(lshr) sequence: 11260 // - Start from the truncated value. 11261 // - Zero extend to the desired bit width. 11262 // - Shift left. 11263 assert(Origin && "No original load to compare against."); 11264 unsigned BitWidth = Origin->getValueSizeInBits(0); 11265 assert(Inst && "This slice is not bound to an instruction"); 11266 assert(Inst->getValueSizeInBits(0) <= BitWidth && 11267 "Extracted slice is bigger than the whole type!"); 11268 APInt UsedBits(Inst->getValueSizeInBits(0), 0); 11269 UsedBits.setAllBits(); 11270 UsedBits = UsedBits.zext(BitWidth); 11271 UsedBits <<= Shift; 11272 return UsedBits; 11273 } 11274 11275 /// \brief Get the size of the slice to be loaded in bytes. 11276 unsigned getLoadedSize() const { 11277 unsigned SliceSize = getUsedBits().countPopulation(); 11278 assert(!(SliceSize & 0x7) && "Size is not a multiple of a byte."); 11279 return SliceSize / 8; 11280 } 11281 11282 /// \brief Get the type that will be loaded for this slice. 11283 /// Note: This may not be the final type for the slice. 11284 EVT getLoadedType() const { 11285 assert(DAG && "Missing context"); 11286 LLVMContext &Ctxt = *DAG->getContext(); 11287 return EVT::getIntegerVT(Ctxt, getLoadedSize() * 8); 11288 } 11289 11290 /// \brief Get the alignment of the load used for this slice. 11291 unsigned getAlignment() const { 11292 unsigned Alignment = Origin->getAlignment(); 11293 unsigned Offset = getOffsetFromBase(); 11294 if (Offset != 0) 11295 Alignment = MinAlign(Alignment, Alignment + Offset); 11296 return Alignment; 11297 } 11298 11299 /// \brief Check if this slice can be rewritten with legal operations. 11300 bool isLegal() const { 11301 // An invalid slice is not legal. 11302 if (!Origin || !Inst || !DAG) 11303 return false; 11304 11305 // Offsets are for indexed load only, we do not handle that. 11306 if (!Origin->getOffset().isUndef()) 11307 return false; 11308 11309 const TargetLowering &TLI = DAG->getTargetLoweringInfo(); 11310 11311 // Check that the type is legal. 11312 EVT SliceType = getLoadedType(); 11313 if (!TLI.isTypeLegal(SliceType)) 11314 return false; 11315 11316 // Check that the load is legal for this type. 11317 if (!TLI.isOperationLegal(ISD::LOAD, SliceType)) 11318 return false; 11319 11320 // Check that the offset can be computed. 11321 // 1. Check its type. 11322 EVT PtrType = Origin->getBasePtr().getValueType(); 11323 if (PtrType == MVT::Untyped || PtrType.isExtended()) 11324 return false; 11325 11326 // 2. Check that it fits in the immediate. 11327 if (!TLI.isLegalAddImmediate(getOffsetFromBase())) 11328 return false; 11329 11330 // 3. Check that the computation is legal. 11331 if (!TLI.isOperationLegal(ISD::ADD, PtrType)) 11332 return false; 11333 11334 // Check that the zext is legal if it needs one. 11335 EVT TruncateType = Inst->getValueType(0); 11336 if (TruncateType != SliceType && 11337 !TLI.isOperationLegal(ISD::ZERO_EXTEND, TruncateType)) 11338 return false; 11339 11340 return true; 11341 } 11342 11343 /// \brief Get the offset in bytes of this slice in the original chunk of 11344 /// bits. 11345 /// \pre DAG != nullptr. 11346 uint64_t getOffsetFromBase() const { 11347 assert(DAG && "Missing context."); 11348 bool IsBigEndian = DAG->getDataLayout().isBigEndian(); 11349 assert(!(Shift & 0x7) && "Shifts not aligned on Bytes are not supported."); 11350 uint64_t Offset = Shift / 8; 11351 unsigned TySizeInBytes = Origin->getValueSizeInBits(0) / 8; 11352 assert(!(Origin->getValueSizeInBits(0) & 0x7) && 11353 "The size of the original loaded type is not a multiple of a" 11354 " byte."); 11355 // If Offset is bigger than TySizeInBytes, it means we are loading all 11356 // zeros. This should have been optimized before in the process. 11357 assert(TySizeInBytes > Offset && 11358 "Invalid shift amount for given loaded size"); 11359 if (IsBigEndian) 11360 Offset = TySizeInBytes - Offset - getLoadedSize(); 11361 return Offset; 11362 } 11363 11364 /// \brief Generate the sequence of instructions to load the slice 11365 /// represented by this object and redirect the uses of this slice to 11366 /// this new sequence of instructions. 11367 /// \pre this->Inst && this->Origin are valid Instructions and this 11368 /// object passed the legal check: LoadedSlice::isLegal returned true. 11369 /// \return The last instruction of the sequence used to load the slice. 11370 SDValue loadSlice() const { 11371 assert(Inst && Origin && "Unable to replace a non-existing slice."); 11372 const SDValue &OldBaseAddr = Origin->getBasePtr(); 11373 SDValue BaseAddr = OldBaseAddr; 11374 // Get the offset in that chunk of bytes w.r.t. the endianness. 11375 int64_t Offset = static_cast<int64_t>(getOffsetFromBase()); 11376 assert(Offset >= 0 && "Offset too big to fit in int64_t!"); 11377 if (Offset) { 11378 // BaseAddr = BaseAddr + Offset. 11379 EVT ArithType = BaseAddr.getValueType(); 11380 SDLoc DL(Origin); 11381 BaseAddr = DAG->getNode(ISD::ADD, DL, ArithType, BaseAddr, 11382 DAG->getConstant(Offset, DL, ArithType)); 11383 } 11384 11385 // Create the type of the loaded slice according to its size. 11386 EVT SliceType = getLoadedType(); 11387 11388 // Create the load for the slice. 11389 SDValue LastInst = 11390 DAG->getLoad(SliceType, SDLoc(Origin), Origin->getChain(), BaseAddr, 11391 Origin->getPointerInfo().getWithOffset(Offset), 11392 getAlignment(), Origin->getMemOperand()->getFlags()); 11393 // If the final type is not the same as the loaded type, this means that 11394 // we have to pad with zero. Create a zero extend for that. 11395 EVT FinalType = Inst->getValueType(0); 11396 if (SliceType != FinalType) 11397 LastInst = 11398 DAG->getNode(ISD::ZERO_EXTEND, SDLoc(LastInst), FinalType, LastInst); 11399 return LastInst; 11400 } 11401 11402 /// \brief Check if this slice can be merged with an expensive cross register 11403 /// bank copy. E.g., 11404 /// i = load i32 11405 /// f = bitcast i32 i to float 11406 bool canMergeExpensiveCrossRegisterBankCopy() const { 11407 if (!Inst || !Inst->hasOneUse()) 11408 return false; 11409 SDNode *Use = *Inst->use_begin(); 11410 if (Use->getOpcode() != ISD::BITCAST) 11411 return false; 11412 assert(DAG && "Missing context"); 11413 const TargetLowering &TLI = DAG->getTargetLoweringInfo(); 11414 EVT ResVT = Use->getValueType(0); 11415 const TargetRegisterClass *ResRC = TLI.getRegClassFor(ResVT.getSimpleVT()); 11416 const TargetRegisterClass *ArgRC = 11417 TLI.getRegClassFor(Use->getOperand(0).getValueType().getSimpleVT()); 11418 if (ArgRC == ResRC || !TLI.isOperationLegal(ISD::LOAD, ResVT)) 11419 return false; 11420 11421 // At this point, we know that we perform a cross-register-bank copy. 11422 // Check if it is expensive. 11423 const TargetRegisterInfo *TRI = DAG->getSubtarget().getRegisterInfo(); 11424 // Assume bitcasts are cheap, unless both register classes do not 11425 // explicitly share a common sub class. 11426 if (!TRI || TRI->getCommonSubClass(ArgRC, ResRC)) 11427 return false; 11428 11429 // Check if it will be merged with the load. 11430 // 1. Check the alignment constraint. 11431 unsigned RequiredAlignment = DAG->getDataLayout().getABITypeAlignment( 11432 ResVT.getTypeForEVT(*DAG->getContext())); 11433 11434 if (RequiredAlignment > getAlignment()) 11435 return false; 11436 11437 // 2. Check that the load is a legal operation for that type. 11438 if (!TLI.isOperationLegal(ISD::LOAD, ResVT)) 11439 return false; 11440 11441 // 3. Check that we do not have a zext in the way. 11442 if (Inst->getValueType(0) != getLoadedType()) 11443 return false; 11444 11445 return true; 11446 } 11447 }; 11448 } 11449 11450 /// \brief Check that all bits set in \p UsedBits form a dense region, i.e., 11451 /// \p UsedBits looks like 0..0 1..1 0..0. 11452 static bool areUsedBitsDense(const APInt &UsedBits) { 11453 // If all the bits are one, this is dense! 11454 if (UsedBits.isAllOnesValue()) 11455 return true; 11456 11457 // Get rid of the unused bits on the right. 11458 APInt NarrowedUsedBits = UsedBits.lshr(UsedBits.countTrailingZeros()); 11459 // Get rid of the unused bits on the left. 11460 if (NarrowedUsedBits.countLeadingZeros()) 11461 NarrowedUsedBits = NarrowedUsedBits.trunc(NarrowedUsedBits.getActiveBits()); 11462 // Check that the chunk of bits is completely used. 11463 return NarrowedUsedBits.isAllOnesValue(); 11464 } 11465 11466 /// \brief Check whether or not \p First and \p Second are next to each other 11467 /// in memory. This means that there is no hole between the bits loaded 11468 /// by \p First and the bits loaded by \p Second. 11469 static bool areSlicesNextToEachOther(const LoadedSlice &First, 11470 const LoadedSlice &Second) { 11471 assert(First.Origin == Second.Origin && First.Origin && 11472 "Unable to match different memory origins."); 11473 APInt UsedBits = First.getUsedBits(); 11474 assert((UsedBits & Second.getUsedBits()) == 0 && 11475 "Slices are not supposed to overlap."); 11476 UsedBits |= Second.getUsedBits(); 11477 return areUsedBitsDense(UsedBits); 11478 } 11479 11480 /// \brief Adjust the \p GlobalLSCost according to the target 11481 /// paring capabilities and the layout of the slices. 11482 /// \pre \p GlobalLSCost should account for at least as many loads as 11483 /// there is in the slices in \p LoadedSlices. 11484 static void adjustCostForPairing(SmallVectorImpl<LoadedSlice> &LoadedSlices, 11485 LoadedSlice::Cost &GlobalLSCost) { 11486 unsigned NumberOfSlices = LoadedSlices.size(); 11487 // If there is less than 2 elements, no pairing is possible. 11488 if (NumberOfSlices < 2) 11489 return; 11490 11491 // Sort the slices so that elements that are likely to be next to each 11492 // other in memory are next to each other in the list. 11493 std::sort(LoadedSlices.begin(), LoadedSlices.end(), 11494 [](const LoadedSlice &LHS, const LoadedSlice &RHS) { 11495 assert(LHS.Origin == RHS.Origin && "Different bases not implemented."); 11496 return LHS.getOffsetFromBase() < RHS.getOffsetFromBase(); 11497 }); 11498 const TargetLowering &TLI = LoadedSlices[0].DAG->getTargetLoweringInfo(); 11499 // First (resp. Second) is the first (resp. Second) potentially candidate 11500 // to be placed in a paired load. 11501 const LoadedSlice *First = nullptr; 11502 const LoadedSlice *Second = nullptr; 11503 for (unsigned CurrSlice = 0; CurrSlice < NumberOfSlices; ++CurrSlice, 11504 // Set the beginning of the pair. 11505 First = Second) { 11506 11507 Second = &LoadedSlices[CurrSlice]; 11508 11509 // If First is NULL, it means we start a new pair. 11510 // Get to the next slice. 11511 if (!First) 11512 continue; 11513 11514 EVT LoadedType = First->getLoadedType(); 11515 11516 // If the types of the slices are different, we cannot pair them. 11517 if (LoadedType != Second->getLoadedType()) 11518 continue; 11519 11520 // Check if the target supplies paired loads for this type. 11521 unsigned RequiredAlignment = 0; 11522 if (!TLI.hasPairedLoad(LoadedType, RequiredAlignment)) { 11523 // move to the next pair, this type is hopeless. 11524 Second = nullptr; 11525 continue; 11526 } 11527 // Check if we meet the alignment requirement. 11528 if (RequiredAlignment > First->getAlignment()) 11529 continue; 11530 11531 // Check that both loads are next to each other in memory. 11532 if (!areSlicesNextToEachOther(*First, *Second)) 11533 continue; 11534 11535 assert(GlobalLSCost.Loads > 0 && "We save more loads than we created!"); 11536 --GlobalLSCost.Loads; 11537 // Move to the next pair. 11538 Second = nullptr; 11539 } 11540 } 11541 11542 /// \brief Check the profitability of all involved LoadedSlice. 11543 /// Currently, it is considered profitable if there is exactly two 11544 /// involved slices (1) which are (2) next to each other in memory, and 11545 /// whose cost (\see LoadedSlice::Cost) is smaller than the original load (3). 11546 /// 11547 /// Note: The order of the elements in \p LoadedSlices may be modified, but not 11548 /// the elements themselves. 11549 /// 11550 /// FIXME: When the cost model will be mature enough, we can relax 11551 /// constraints (1) and (2). 11552 static bool isSlicingProfitable(SmallVectorImpl<LoadedSlice> &LoadedSlices, 11553 const APInt &UsedBits, bool ForCodeSize) { 11554 unsigned NumberOfSlices = LoadedSlices.size(); 11555 if (StressLoadSlicing) 11556 return NumberOfSlices > 1; 11557 11558 // Check (1). 11559 if (NumberOfSlices != 2) 11560 return false; 11561 11562 // Check (2). 11563 if (!areUsedBitsDense(UsedBits)) 11564 return false; 11565 11566 // Check (3). 11567 LoadedSlice::Cost OrigCost(ForCodeSize), GlobalSlicingCost(ForCodeSize); 11568 // The original code has one big load. 11569 OrigCost.Loads = 1; 11570 for (unsigned CurrSlice = 0; CurrSlice < NumberOfSlices; ++CurrSlice) { 11571 const LoadedSlice &LS = LoadedSlices[CurrSlice]; 11572 // Accumulate the cost of all the slices. 11573 LoadedSlice::Cost SliceCost(LS, ForCodeSize); 11574 GlobalSlicingCost += SliceCost; 11575 11576 // Account as cost in the original configuration the gain obtained 11577 // with the current slices. 11578 OrigCost.addSliceGain(LS); 11579 } 11580 11581 // If the target supports paired load, adjust the cost accordingly. 11582 adjustCostForPairing(LoadedSlices, GlobalSlicingCost); 11583 return OrigCost > GlobalSlicingCost; 11584 } 11585 11586 /// \brief If the given load, \p LI, is used only by trunc or trunc(lshr) 11587 /// operations, split it in the various pieces being extracted. 11588 /// 11589 /// This sort of thing is introduced by SROA. 11590 /// This slicing takes care not to insert overlapping loads. 11591 /// \pre LI is a simple load (i.e., not an atomic or volatile load). 11592 bool DAGCombiner::SliceUpLoad(SDNode *N) { 11593 if (Level < AfterLegalizeDAG) 11594 return false; 11595 11596 LoadSDNode *LD = cast<LoadSDNode>(N); 11597 if (LD->isVolatile() || !ISD::isNormalLoad(LD) || 11598 !LD->getValueType(0).isInteger()) 11599 return false; 11600 11601 // Keep track of already used bits to detect overlapping values. 11602 // In that case, we will just abort the transformation. 11603 APInt UsedBits(LD->getValueSizeInBits(0), 0); 11604 11605 SmallVector<LoadedSlice, 4> LoadedSlices; 11606 11607 // Check if this load is used as several smaller chunks of bits. 11608 // Basically, look for uses in trunc or trunc(lshr) and record a new chain 11609 // of computation for each trunc. 11610 for (SDNode::use_iterator UI = LD->use_begin(), UIEnd = LD->use_end(); 11611 UI != UIEnd; ++UI) { 11612 // Skip the uses of the chain. 11613 if (UI.getUse().getResNo() != 0) 11614 continue; 11615 11616 SDNode *User = *UI; 11617 unsigned Shift = 0; 11618 11619 // Check if this is a trunc(lshr). 11620 if (User->getOpcode() == ISD::SRL && User->hasOneUse() && 11621 isa<ConstantSDNode>(User->getOperand(1))) { 11622 Shift = cast<ConstantSDNode>(User->getOperand(1))->getZExtValue(); 11623 User = *User->use_begin(); 11624 } 11625 11626 // At this point, User is a Truncate, iff we encountered, trunc or 11627 // trunc(lshr). 11628 if (User->getOpcode() != ISD::TRUNCATE) 11629 return false; 11630 11631 // The width of the type must be a power of 2 and greater than 8-bits. 11632 // Otherwise the load cannot be represented in LLVM IR. 11633 // Moreover, if we shifted with a non-8-bits multiple, the slice 11634 // will be across several bytes. We do not support that. 11635 unsigned Width = User->getValueSizeInBits(0); 11636 if (Width < 8 || !isPowerOf2_32(Width) || (Shift & 0x7)) 11637 return 0; 11638 11639 // Build the slice for this chain of computations. 11640 LoadedSlice LS(User, LD, Shift, &DAG); 11641 APInt CurrentUsedBits = LS.getUsedBits(); 11642 11643 // Check if this slice overlaps with another. 11644 if ((CurrentUsedBits & UsedBits) != 0) 11645 return false; 11646 // Update the bits used globally. 11647 UsedBits |= CurrentUsedBits; 11648 11649 // Check if the new slice would be legal. 11650 if (!LS.isLegal()) 11651 return false; 11652 11653 // Record the slice. 11654 LoadedSlices.push_back(LS); 11655 } 11656 11657 // Abort slicing if it does not seem to be profitable. 11658 if (!isSlicingProfitable(LoadedSlices, UsedBits, ForCodeSize)) 11659 return false; 11660 11661 ++SlicedLoads; 11662 11663 // Rewrite each chain to use an independent load. 11664 // By construction, each chain can be represented by a unique load. 11665 11666 // Prepare the argument for the new token factor for all the slices. 11667 SmallVector<SDValue, 8> ArgChains; 11668 for (SmallVectorImpl<LoadedSlice>::const_iterator 11669 LSIt = LoadedSlices.begin(), 11670 LSItEnd = LoadedSlices.end(); 11671 LSIt != LSItEnd; ++LSIt) { 11672 SDValue SliceInst = LSIt->loadSlice(); 11673 CombineTo(LSIt->Inst, SliceInst, true); 11674 if (SliceInst.getOpcode() != ISD::LOAD) 11675 SliceInst = SliceInst.getOperand(0); 11676 assert(SliceInst->getOpcode() == ISD::LOAD && 11677 "It takes more than a zext to get to the loaded slice!!"); 11678 ArgChains.push_back(SliceInst.getValue(1)); 11679 } 11680 11681 SDValue Chain = DAG.getNode(ISD::TokenFactor, SDLoc(LD), MVT::Other, 11682 ArgChains); 11683 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain); 11684 AddToWorklist(Chain.getNode()); 11685 return true; 11686 } 11687 11688 /// Check to see if V is (and load (ptr), imm), where the load is having 11689 /// specific bytes cleared out. If so, return the byte size being masked out 11690 /// and the shift amount. 11691 static std::pair<unsigned, unsigned> 11692 CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) { 11693 std::pair<unsigned, unsigned> Result(0, 0); 11694 11695 // Check for the structure we're looking for. 11696 if (V->getOpcode() != ISD::AND || 11697 !isa<ConstantSDNode>(V->getOperand(1)) || 11698 !ISD::isNormalLoad(V->getOperand(0).getNode())) 11699 return Result; 11700 11701 // Check the chain and pointer. 11702 LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0)); 11703 if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer. 11704 11705 // The store should be chained directly to the load or be an operand of a 11706 // tokenfactor. 11707 if (LD == Chain.getNode()) 11708 ; // ok. 11709 else if (Chain->getOpcode() != ISD::TokenFactor) 11710 return Result; // Fail. 11711 else { 11712 bool isOk = false; 11713 for (const SDValue &ChainOp : Chain->op_values()) 11714 if (ChainOp.getNode() == LD) { 11715 isOk = true; 11716 break; 11717 } 11718 if (!isOk) return Result; 11719 } 11720 11721 // This only handles simple types. 11722 if (V.getValueType() != MVT::i16 && 11723 V.getValueType() != MVT::i32 && 11724 V.getValueType() != MVT::i64) 11725 return Result; 11726 11727 // Check the constant mask. Invert it so that the bits being masked out are 11728 // 0 and the bits being kept are 1. Use getSExtValue so that leading bits 11729 // follow the sign bit for uniformity. 11730 uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue(); 11731 unsigned NotMaskLZ = countLeadingZeros(NotMask); 11732 if (NotMaskLZ & 7) return Result; // Must be multiple of a byte. 11733 unsigned NotMaskTZ = countTrailingZeros(NotMask); 11734 if (NotMaskTZ & 7) return Result; // Must be multiple of a byte. 11735 if (NotMaskLZ == 64) return Result; // All zero mask. 11736 11737 // See if we have a continuous run of bits. If so, we have 0*1+0* 11738 if (countTrailingOnes(NotMask >> NotMaskTZ) + NotMaskTZ + NotMaskLZ != 64) 11739 return Result; 11740 11741 // Adjust NotMaskLZ down to be from the actual size of the int instead of i64. 11742 if (V.getValueType() != MVT::i64 && NotMaskLZ) 11743 NotMaskLZ -= 64-V.getValueSizeInBits(); 11744 11745 unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8; 11746 switch (MaskedBytes) { 11747 case 1: 11748 case 2: 11749 case 4: break; 11750 default: return Result; // All one mask, or 5-byte mask. 11751 } 11752 11753 // Verify that the first bit starts at a multiple of mask so that the access 11754 // is aligned the same as the access width. 11755 if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result; 11756 11757 Result.first = MaskedBytes; 11758 Result.second = NotMaskTZ/8; 11759 return Result; 11760 } 11761 11762 11763 /// Check to see if IVal is something that provides a value as specified by 11764 /// MaskInfo. If so, replace the specified store with a narrower store of 11765 /// truncated IVal. 11766 static SDNode * 11767 ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo, 11768 SDValue IVal, StoreSDNode *St, 11769 DAGCombiner *DC) { 11770 unsigned NumBytes = MaskInfo.first; 11771 unsigned ByteShift = MaskInfo.second; 11772 SelectionDAG &DAG = DC->getDAG(); 11773 11774 // Check to see if IVal is all zeros in the part being masked in by the 'or' 11775 // that uses this. If not, this is not a replacement. 11776 APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(), 11777 ByteShift*8, (ByteShift+NumBytes)*8); 11778 if (!DAG.MaskedValueIsZero(IVal, Mask)) return nullptr; 11779 11780 // Check that it is legal on the target to do this. It is legal if the new 11781 // VT we're shrinking to (i8/i16/i32) is legal or we're still before type 11782 // legalization. 11783 MVT VT = MVT::getIntegerVT(NumBytes*8); 11784 if (!DC->isTypeLegal(VT)) 11785 return nullptr; 11786 11787 // Okay, we can do this! Replace the 'St' store with a store of IVal that is 11788 // shifted by ByteShift and truncated down to NumBytes. 11789 if (ByteShift) { 11790 SDLoc DL(IVal); 11791 IVal = DAG.getNode(ISD::SRL, DL, IVal.getValueType(), IVal, 11792 DAG.getConstant(ByteShift*8, DL, 11793 DC->getShiftAmountTy(IVal.getValueType()))); 11794 } 11795 11796 // Figure out the offset for the store and the alignment of the access. 11797 unsigned StOffset; 11798 unsigned NewAlign = St->getAlignment(); 11799 11800 if (DAG.getDataLayout().isLittleEndian()) 11801 StOffset = ByteShift; 11802 else 11803 StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes; 11804 11805 SDValue Ptr = St->getBasePtr(); 11806 if (StOffset) { 11807 SDLoc DL(IVal); 11808 Ptr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), 11809 Ptr, DAG.getConstant(StOffset, DL, Ptr.getValueType())); 11810 NewAlign = MinAlign(NewAlign, StOffset); 11811 } 11812 11813 // Truncate down to the new size. 11814 IVal = DAG.getNode(ISD::TRUNCATE, SDLoc(IVal), VT, IVal); 11815 11816 ++OpsNarrowed; 11817 return DAG 11818 .getStore(St->getChain(), SDLoc(St), IVal, Ptr, 11819 St->getPointerInfo().getWithOffset(StOffset), NewAlign) 11820 .getNode(); 11821 } 11822 11823 11824 /// Look for sequence of load / op / store where op is one of 'or', 'xor', and 11825 /// 'and' of immediates. If 'op' is only touching some of the loaded bits, try 11826 /// narrowing the load and store if it would end up being a win for performance 11827 /// or code size. 11828 SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) { 11829 StoreSDNode *ST = cast<StoreSDNode>(N); 11830 if (ST->isVolatile()) 11831 return SDValue(); 11832 11833 SDValue Chain = ST->getChain(); 11834 SDValue Value = ST->getValue(); 11835 SDValue Ptr = ST->getBasePtr(); 11836 EVT VT = Value.getValueType(); 11837 11838 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse()) 11839 return SDValue(); 11840 11841 unsigned Opc = Value.getOpcode(); 11842 11843 // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst 11844 // is a byte mask indicating a consecutive number of bytes, check to see if 11845 // Y is known to provide just those bytes. If so, we try to replace the 11846 // load + replace + store sequence with a single (narrower) store, which makes 11847 // the load dead. 11848 if (Opc == ISD::OR) { 11849 std::pair<unsigned, unsigned> MaskedLoad; 11850 MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain); 11851 if (MaskedLoad.first) 11852 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad, 11853 Value.getOperand(1), ST,this)) 11854 return SDValue(NewST, 0); 11855 11856 // Or is commutative, so try swapping X and Y. 11857 MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain); 11858 if (MaskedLoad.first) 11859 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad, 11860 Value.getOperand(0), ST,this)) 11861 return SDValue(NewST, 0); 11862 } 11863 11864 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) || 11865 Value.getOperand(1).getOpcode() != ISD::Constant) 11866 return SDValue(); 11867 11868 SDValue N0 = Value.getOperand(0); 11869 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() && 11870 Chain == SDValue(N0.getNode(), 1)) { 11871 LoadSDNode *LD = cast<LoadSDNode>(N0); 11872 if (LD->getBasePtr() != Ptr || 11873 LD->getPointerInfo().getAddrSpace() != 11874 ST->getPointerInfo().getAddrSpace()) 11875 return SDValue(); 11876 11877 // Find the type to narrow it the load / op / store to. 11878 SDValue N1 = Value.getOperand(1); 11879 unsigned BitWidth = N1.getValueSizeInBits(); 11880 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue(); 11881 if (Opc == ISD::AND) 11882 Imm ^= APInt::getAllOnesValue(BitWidth); 11883 if (Imm == 0 || Imm.isAllOnesValue()) 11884 return SDValue(); 11885 unsigned ShAmt = Imm.countTrailingZeros(); 11886 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1; 11887 unsigned NewBW = NextPowerOf2(MSB - ShAmt); 11888 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW); 11889 // The narrowing should be profitable, the load/store operation should be 11890 // legal (or custom) and the store size should be equal to the NewVT width. 11891 while (NewBW < BitWidth && 11892 (NewVT.getStoreSizeInBits() != NewBW || 11893 !TLI.isOperationLegalOrCustom(Opc, NewVT) || 11894 !TLI.isNarrowingProfitable(VT, NewVT))) { 11895 NewBW = NextPowerOf2(NewBW); 11896 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW); 11897 } 11898 if (NewBW >= BitWidth) 11899 return SDValue(); 11900 11901 // If the lsb changed does not start at the type bitwidth boundary, 11902 // start at the previous one. 11903 if (ShAmt % NewBW) 11904 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW; 11905 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt, 11906 std::min(BitWidth, ShAmt + NewBW)); 11907 if ((Imm & Mask) == Imm) { 11908 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW); 11909 if (Opc == ISD::AND) 11910 NewImm ^= APInt::getAllOnesValue(NewBW); 11911 uint64_t PtrOff = ShAmt / 8; 11912 // For big endian targets, we need to adjust the offset to the pointer to 11913 // load the correct bytes. 11914 if (DAG.getDataLayout().isBigEndian()) 11915 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff; 11916 11917 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff); 11918 Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext()); 11919 if (NewAlign < DAG.getDataLayout().getABITypeAlignment(NewVTTy)) 11920 return SDValue(); 11921 11922 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LD), 11923 Ptr.getValueType(), Ptr, 11924 DAG.getConstant(PtrOff, SDLoc(LD), 11925 Ptr.getValueType())); 11926 SDValue NewLD = 11927 DAG.getLoad(NewVT, SDLoc(N0), LD->getChain(), NewPtr, 11928 LD->getPointerInfo().getWithOffset(PtrOff), NewAlign, 11929 LD->getMemOperand()->getFlags(), LD->getAAInfo()); 11930 SDValue NewVal = DAG.getNode(Opc, SDLoc(Value), NewVT, NewLD, 11931 DAG.getConstant(NewImm, SDLoc(Value), 11932 NewVT)); 11933 SDValue NewST = 11934 DAG.getStore(Chain, SDLoc(N), NewVal, NewPtr, 11935 ST->getPointerInfo().getWithOffset(PtrOff), NewAlign); 11936 11937 AddToWorklist(NewPtr.getNode()); 11938 AddToWorklist(NewLD.getNode()); 11939 AddToWorklist(NewVal.getNode()); 11940 WorklistRemover DeadNodes(*this); 11941 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1)); 11942 ++OpsNarrowed; 11943 return NewST; 11944 } 11945 } 11946 11947 return SDValue(); 11948 } 11949 11950 /// For a given floating point load / store pair, if the load value isn't used 11951 /// by any other operations, then consider transforming the pair to integer 11952 /// load / store operations if the target deems the transformation profitable. 11953 SDValue DAGCombiner::TransformFPLoadStorePair(SDNode *N) { 11954 StoreSDNode *ST = cast<StoreSDNode>(N); 11955 SDValue Chain = ST->getChain(); 11956 SDValue Value = ST->getValue(); 11957 if (ISD::isNormalStore(ST) && ISD::isNormalLoad(Value.getNode()) && 11958 Value.hasOneUse() && 11959 Chain == SDValue(Value.getNode(), 1)) { 11960 LoadSDNode *LD = cast<LoadSDNode>(Value); 11961 EVT VT = LD->getMemoryVT(); 11962 if (!VT.isFloatingPoint() || 11963 VT != ST->getMemoryVT() || 11964 LD->isNonTemporal() || 11965 ST->isNonTemporal() || 11966 LD->getPointerInfo().getAddrSpace() != 0 || 11967 ST->getPointerInfo().getAddrSpace() != 0) 11968 return SDValue(); 11969 11970 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits()); 11971 if (!TLI.isOperationLegal(ISD::LOAD, IntVT) || 11972 !TLI.isOperationLegal(ISD::STORE, IntVT) || 11973 !TLI.isDesirableToTransformToIntegerOp(ISD::LOAD, VT) || 11974 !TLI.isDesirableToTransformToIntegerOp(ISD::STORE, VT)) 11975 return SDValue(); 11976 11977 unsigned LDAlign = LD->getAlignment(); 11978 unsigned STAlign = ST->getAlignment(); 11979 Type *IntVTTy = IntVT.getTypeForEVT(*DAG.getContext()); 11980 unsigned ABIAlign = DAG.getDataLayout().getABITypeAlignment(IntVTTy); 11981 if (LDAlign < ABIAlign || STAlign < ABIAlign) 11982 return SDValue(); 11983 11984 SDValue NewLD = 11985 DAG.getLoad(IntVT, SDLoc(Value), LD->getChain(), LD->getBasePtr(), 11986 LD->getPointerInfo(), LDAlign); 11987 11988 SDValue NewST = 11989 DAG.getStore(NewLD.getValue(1), SDLoc(N), NewLD, ST->getBasePtr(), 11990 ST->getPointerInfo(), STAlign); 11991 11992 AddToWorklist(NewLD.getNode()); 11993 AddToWorklist(NewST.getNode()); 11994 WorklistRemover DeadNodes(*this); 11995 DAG.ReplaceAllUsesOfValueWith(Value.getValue(1), NewLD.getValue(1)); 11996 ++LdStFP2Int; 11997 return NewST; 11998 } 11999 12000 return SDValue(); 12001 } 12002 12003 // This is a helper function for visitMUL to check the profitability 12004 // of folding (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2). 12005 // MulNode is the original multiply, AddNode is (add x, c1), 12006 // and ConstNode is c2. 12007 // 12008 // If the (add x, c1) has multiple uses, we could increase 12009 // the number of adds if we make this transformation. 12010 // It would only be worth doing this if we can remove a 12011 // multiply in the process. Check for that here. 12012 // To illustrate: 12013 // (A + c1) * c3 12014 // (A + c2) * c3 12015 // We're checking for cases where we have common "c3 * A" expressions. 12016 bool DAGCombiner::isMulAddWithConstProfitable(SDNode *MulNode, 12017 SDValue &AddNode, 12018 SDValue &ConstNode) { 12019 APInt Val; 12020 12021 // If the add only has one use, this would be OK to do. 12022 if (AddNode.getNode()->hasOneUse()) 12023 return true; 12024 12025 // Walk all the users of the constant with which we're multiplying. 12026 for (SDNode *Use : ConstNode->uses()) { 12027 12028 if (Use == MulNode) // This use is the one we're on right now. Skip it. 12029 continue; 12030 12031 if (Use->getOpcode() == ISD::MUL) { // We have another multiply use. 12032 SDNode *OtherOp; 12033 SDNode *MulVar = AddNode.getOperand(0).getNode(); 12034 12035 // OtherOp is what we're multiplying against the constant. 12036 if (Use->getOperand(0) == ConstNode) 12037 OtherOp = Use->getOperand(1).getNode(); 12038 else 12039 OtherOp = Use->getOperand(0).getNode(); 12040 12041 // Check to see if multiply is with the same operand of our "add". 12042 // 12043 // ConstNode = CONST 12044 // Use = ConstNode * A <-- visiting Use. OtherOp is A. 12045 // ... 12046 // AddNode = (A + c1) <-- MulVar is A. 12047 // = AddNode * ConstNode <-- current visiting instruction. 12048 // 12049 // If we make this transformation, we will have a common 12050 // multiply (ConstNode * A) that we can save. 12051 if (OtherOp == MulVar) 12052 return true; 12053 12054 // Now check to see if a future expansion will give us a common 12055 // multiply. 12056 // 12057 // ConstNode = CONST 12058 // AddNode = (A + c1) 12059 // ... = AddNode * ConstNode <-- current visiting instruction. 12060 // ... 12061 // OtherOp = (A + c2) 12062 // Use = OtherOp * ConstNode <-- visiting Use. 12063 // 12064 // If we make this transformation, we will have a common 12065 // multiply (CONST * A) after we also do the same transformation 12066 // to the "t2" instruction. 12067 if (OtherOp->getOpcode() == ISD::ADD && 12068 DAG.isConstantIntBuildVectorOrConstantInt(OtherOp->getOperand(1)) && 12069 OtherOp->getOperand(0).getNode() == MulVar) 12070 return true; 12071 } 12072 } 12073 12074 // Didn't find a case where this would be profitable. 12075 return false; 12076 } 12077 12078 bool DAGCombiner::MergeStoresOfConstantsOrVecElts( 12079 SmallVectorImpl<MemOpLink> &StoreNodes, EVT MemVT, 12080 unsigned NumStores, bool IsConstantSrc, bool UseVector) { 12081 // Make sure we have something to merge. 12082 if (NumStores < 2) 12083 return false; 12084 12085 int64_t ElementSizeBytes = MemVT.getSizeInBits() / 8; 12086 12087 // The latest Node in the DAG. 12088 SDLoc DL(StoreNodes[0].MemNode); 12089 12090 SDValue StoredVal; 12091 if (UseVector) { 12092 bool IsVec = MemVT.isVector(); 12093 unsigned Elts = NumStores; 12094 if (IsVec) { 12095 // When merging vector stores, get the total number of elements. 12096 Elts *= MemVT.getVectorNumElements(); 12097 } 12098 // Get the type for the merged vector store. 12099 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(), Elts); 12100 assert(TLI.isTypeLegal(Ty) && "Illegal vector store"); 12101 12102 if (IsConstantSrc) { 12103 SmallVector<SDValue, 8> BuildVector; 12104 for (unsigned I = 0, E = Ty.getVectorNumElements(); I != E; ++I) { 12105 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[I].MemNode); 12106 SDValue Val = St->getValue(); 12107 if (MemVT.getScalarType().isInteger()) 12108 if (auto *CFP = dyn_cast<ConstantFPSDNode>(St->getValue())) 12109 Val = DAG.getConstant( 12110 (uint32_t)CFP->getValueAPF().bitcastToAPInt().getZExtValue(), 12111 SDLoc(CFP), MemVT); 12112 BuildVector.push_back(Val); 12113 } 12114 StoredVal = DAG.getBuildVector(Ty, DL, BuildVector); 12115 } else { 12116 SmallVector<SDValue, 8> Ops; 12117 for (unsigned i = 0; i < NumStores; ++i) { 12118 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode); 12119 SDValue Val = St->getValue(); 12120 // All operands of BUILD_VECTOR / CONCAT_VECTOR must have the same type. 12121 if (Val.getValueType() != MemVT) 12122 return false; 12123 Ops.push_back(Val); 12124 } 12125 12126 // Build the extracted vector elements back into a vector. 12127 StoredVal = DAG.getNode(IsVec ? ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, 12128 DL, Ty, Ops); } 12129 } else { 12130 // We should always use a vector store when merging extracted vector 12131 // elements, so this path implies a store of constants. 12132 assert(IsConstantSrc && "Merged vector elements should use vector store"); 12133 12134 unsigned SizeInBits = NumStores * ElementSizeBytes * 8; 12135 APInt StoreInt(SizeInBits, 0); 12136 12137 // Construct a single integer constant which is made of the smaller 12138 // constant inputs. 12139 bool IsLE = DAG.getDataLayout().isLittleEndian(); 12140 for (unsigned i = 0; i < NumStores; ++i) { 12141 unsigned Idx = IsLE ? (NumStores - 1 - i) : i; 12142 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[Idx].MemNode); 12143 12144 SDValue Val = St->getValue(); 12145 StoreInt <<= ElementSizeBytes * 8; 12146 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val)) { 12147 StoreInt |= C->getAPIntValue().zext(SizeInBits); 12148 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Val)) { 12149 StoreInt |= C->getValueAPF().bitcastToAPInt().zext(SizeInBits); 12150 } else { 12151 llvm_unreachable("Invalid constant element type"); 12152 } 12153 } 12154 12155 // Create the new Load and Store operations. 12156 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), SizeInBits); 12157 StoredVal = DAG.getConstant(StoreInt, DL, StoreTy); 12158 } 12159 12160 SmallVector<SDValue, 8> Chains; 12161 12162 // Gather all Chains we're inheriting. As generally all chains are 12163 // equal, do minor check to remove obvious redundancies. 12164 Chains.push_back(StoreNodes[0].MemNode->getChain()); 12165 for (unsigned i = 1; i < NumStores; ++i) 12166 if (StoreNodes[0].MemNode->getChain() != StoreNodes[i].MemNode->getChain()) 12167 Chains.push_back(StoreNodes[i].MemNode->getChain()); 12168 12169 LSBaseSDNode *FirstInChain = StoreNodes[0].MemNode; 12170 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains); 12171 SDValue NewStore = DAG.getStore(NewChain, DL, StoredVal, 12172 FirstInChain->getBasePtr(), 12173 FirstInChain->getPointerInfo(), 12174 FirstInChain->getAlignment()); 12175 12176 // Replace all merged stores with the new store. 12177 for (unsigned i = 0; i < NumStores; ++i) 12178 CombineTo(StoreNodes[i].MemNode, NewStore); 12179 12180 AddToWorklist(NewChain.getNode()); 12181 return true; 12182 } 12183 12184 void DAGCombiner::getStoreMergeCandidates( 12185 StoreSDNode *St, SmallVectorImpl<MemOpLink> &StoreNodes) { 12186 // This holds the base pointer, index, and the offset in bytes from the base 12187 // pointer. 12188 BaseIndexOffset BasePtr = BaseIndexOffset::match(St->getBasePtr(), DAG); 12189 EVT MemVT = St->getMemoryVT(); 12190 12191 // We must have a base and an offset. 12192 if (!BasePtr.Base.getNode()) 12193 return; 12194 12195 // Do not handle stores to undef base pointers. 12196 if (BasePtr.Base.isUndef()) 12197 return; 12198 12199 // We looking for a root node which is an ancestor to all mergable 12200 // stores. We search up through a load, to our root and then down 12201 // through all children. For instance we will find Store{1,2,3} if 12202 // St is Store1, Store2. or Store3 where the root is not a load 12203 // which always true for nonvolatile ops. TODO: Expand 12204 // the search to find all valid candidates through multiple layers of loads. 12205 // 12206 // Root 12207 // |-------|-------| 12208 // Load Load Store3 12209 // | | 12210 // Store1 Store2 12211 // 12212 // FIXME: We should be able to climb and 12213 // descend TokenFactors to find candidates as well. 12214 12215 SDNode *RootNode = (St->getChain()).getNode(); 12216 12217 // Set of Parents of Candidates 12218 std::set<SDNode *> CandidateParents; 12219 12220 if (LoadSDNode *Ldn = dyn_cast<LoadSDNode>(RootNode)) { 12221 RootNode = Ldn->getChain().getNode(); 12222 for (auto I = RootNode->use_begin(), E = RootNode->use_end(); I != E; ++I) 12223 if (I.getOperandNo() == 0 && isa<LoadSDNode>(*I)) // walk down chain 12224 CandidateParents.insert(*I); 12225 } else 12226 CandidateParents.insert(RootNode); 12227 12228 bool IsLoadSrc = isa<LoadSDNode>(St->getValue()); 12229 bool IsConstantSrc = isa<ConstantSDNode>(St->getValue()) || 12230 isa<ConstantFPSDNode>(St->getValue()); 12231 bool IsExtractVecSrc = 12232 (St->getValue().getOpcode() == ISD::EXTRACT_VECTOR_ELT || 12233 St->getValue().getOpcode() == ISD::EXTRACT_SUBVECTOR); 12234 auto CorrectValueKind = [&](StoreSDNode *Other) -> bool { 12235 if (IsLoadSrc) 12236 return isa<LoadSDNode>(Other->getValue()); 12237 if (IsConstantSrc) 12238 return (isa<ConstantSDNode>(Other->getValue()) || 12239 isa<ConstantFPSDNode>(Other->getValue())); 12240 if (IsExtractVecSrc) 12241 return (Other->getValue().getOpcode() == ISD::EXTRACT_VECTOR_ELT || 12242 Other->getValue().getOpcode() == ISD::EXTRACT_SUBVECTOR); 12243 return false; 12244 }; 12245 12246 // check all parents of mergable children 12247 for (auto P = CandidateParents.begin(); P != CandidateParents.end(); ++P) 12248 for (auto I = (*P)->use_begin(), E = (*P)->use_end(); I != E; ++I) 12249 if (I.getOperandNo() == 0) 12250 if (StoreSDNode *OtherST = dyn_cast<StoreSDNode>(*I)) { 12251 if (OtherST->isVolatile() || OtherST->isIndexed()) 12252 continue; 12253 // We can merge constant floats to equivalent integers 12254 if (OtherST->getMemoryVT() != MemVT) 12255 if (!(MemVT.isInteger() && MemVT.bitsEq(OtherST->getMemoryVT()) && 12256 isa<ConstantFPSDNode>(OtherST->getValue()))) 12257 continue; 12258 BaseIndexOffset Ptr = 12259 BaseIndexOffset::match(OtherST->getBasePtr(), DAG); 12260 if (Ptr.equalBaseIndex(BasePtr) && CorrectValueKind(OtherST)) 12261 StoreNodes.push_back(MemOpLink(OtherST, Ptr.Offset)); 12262 } 12263 } 12264 12265 // We need to check that merging these stores does not cause a loop 12266 // in the DAG. Any store candidate may depend on another candidate 12267 // indirectly through its operand (we already consider dependencies 12268 // through the chain). Check in parallel by searching up from 12269 // non-chain operands of candidates. 12270 bool DAGCombiner::checkMergeStoreCandidatesForDependencies( 12271 SmallVectorImpl<MemOpLink> &StoreNodes) { 12272 SmallPtrSet<const SDNode *, 16> Visited; 12273 SmallVector<const SDNode *, 8> Worklist; 12274 // search ops of store candidates 12275 for (unsigned i = 0; i < StoreNodes.size(); ++i) { 12276 SDNode *n = StoreNodes[i].MemNode; 12277 // Potential loops may happen only through non-chain operands 12278 for (unsigned j = 1; j < n->getNumOperands(); ++j) 12279 Worklist.push_back(n->getOperand(j).getNode()); 12280 } 12281 // search through DAG. We can stop early if we find a storenode 12282 for (unsigned i = 0; i < StoreNodes.size(); ++i) { 12283 if (SDNode::hasPredecessorHelper(StoreNodes[i].MemNode, Visited, Worklist)) 12284 return false; 12285 } 12286 return true; 12287 } 12288 12289 bool DAGCombiner::MergeConsecutiveStores(StoreSDNode *St) { 12290 if (OptLevel == CodeGenOpt::None) 12291 return false; 12292 12293 EVT MemVT = St->getMemoryVT(); 12294 int64_t ElementSizeBytes = MemVT.getSizeInBits() / 8; 12295 12296 if (MemVT.getSizeInBits() * 2 > MaximumLegalStoreInBits) 12297 return false; 12298 12299 bool NoVectors = DAG.getMachineFunction().getFunction()->hasFnAttribute( 12300 Attribute::NoImplicitFloat); 12301 12302 // This function cannot currently deal with non-byte-sized memory sizes. 12303 if (ElementSizeBytes * 8 != MemVT.getSizeInBits()) 12304 return false; 12305 12306 if (!MemVT.isSimple()) 12307 return false; 12308 12309 // Perform an early exit check. Do not bother looking at stored values that 12310 // are not constants, loads, or extracted vector elements. 12311 SDValue StoredVal = St->getValue(); 12312 bool IsLoadSrc = isa<LoadSDNode>(StoredVal); 12313 bool IsConstantSrc = isa<ConstantSDNode>(StoredVal) || 12314 isa<ConstantFPSDNode>(StoredVal); 12315 bool IsExtractVecSrc = (StoredVal.getOpcode() == ISD::EXTRACT_VECTOR_ELT || 12316 StoredVal.getOpcode() == ISD::EXTRACT_SUBVECTOR); 12317 12318 if (!IsConstantSrc && !IsLoadSrc && !IsExtractVecSrc) 12319 return false; 12320 12321 // Don't merge vectors into wider vectors if the source data comes from loads. 12322 // TODO: This restriction can be lifted by using logic similar to the 12323 // ExtractVecSrc case. 12324 if (MemVT.isVector() && IsLoadSrc) 12325 return false; 12326 12327 SmallVector<MemOpLink, 8> StoreNodes; 12328 // Find potential store merge candidates by searching through chain sub-DAG 12329 getStoreMergeCandidates(St, StoreNodes); 12330 12331 // Check if there is anything to merge. 12332 if (StoreNodes.size() < 2) 12333 return false; 12334 12335 // Check that we can merge these candidates without causing a cycle 12336 if (!checkMergeStoreCandidatesForDependencies(StoreNodes)) 12337 return false; 12338 12339 // Sort the memory operands according to their distance from the 12340 // base pointer. 12341 std::sort(StoreNodes.begin(), StoreNodes.end(), 12342 [](MemOpLink LHS, MemOpLink RHS) { 12343 return LHS.OffsetFromBase < RHS.OffsetFromBase; 12344 }); 12345 12346 // Scan the memory operations on the chain and find the first non-consecutive 12347 // store memory address. 12348 unsigned NumConsecutiveStores = 0; 12349 int64_t StartAddress = StoreNodes[0].OffsetFromBase; 12350 12351 // Check that the addresses are consecutive starting from the second 12352 // element in the list of stores. 12353 for (unsigned i = 1, e = StoreNodes.size(); i < e; ++i) { 12354 int64_t CurrAddress = StoreNodes[i].OffsetFromBase; 12355 if (CurrAddress - StartAddress != (ElementSizeBytes * i)) 12356 break; 12357 NumConsecutiveStores = i + 1; 12358 } 12359 12360 if (NumConsecutiveStores < 2) 12361 return false; 12362 12363 // The node with the lowest store address. 12364 LLVMContext &Context = *DAG.getContext(); 12365 const DataLayout &DL = DAG.getDataLayout(); 12366 12367 // Store the constants into memory as one consecutive store. 12368 if (IsConstantSrc) { 12369 bool RV = false; 12370 while (NumConsecutiveStores > 1) { 12371 LSBaseSDNode *FirstInChain = StoreNodes[0].MemNode; 12372 unsigned FirstStoreAS = FirstInChain->getAddressSpace(); 12373 unsigned FirstStoreAlign = FirstInChain->getAlignment(); 12374 unsigned LastLegalType = 0; 12375 unsigned LastLegalVectorType = 0; 12376 bool NonZero = false; 12377 for (unsigned i = 0; i < NumConsecutiveStores; ++i) { 12378 StoreSDNode *ST = cast<StoreSDNode>(StoreNodes[i].MemNode); 12379 SDValue StoredVal = ST->getValue(); 12380 12381 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(StoredVal)) { 12382 NonZero |= !C->isNullValue(); 12383 } else if (ConstantFPSDNode *C = 12384 dyn_cast<ConstantFPSDNode>(StoredVal)) { 12385 NonZero |= !C->getConstantFPValue()->isNullValue(); 12386 } else { 12387 // Non-constant. 12388 break; 12389 } 12390 12391 // Find a legal type for the constant store. 12392 unsigned SizeInBits = (i + 1) * ElementSizeBytes * 8; 12393 EVT StoreTy = EVT::getIntegerVT(Context, SizeInBits); 12394 bool IsFast = false; 12395 if (TLI.isTypeLegal(StoreTy) && 12396 TLI.allowsMemoryAccess(Context, DL, StoreTy, FirstStoreAS, 12397 FirstStoreAlign, &IsFast) && 12398 IsFast) { 12399 LastLegalType = i + 1; 12400 // Or check whether a truncstore is legal. 12401 } else if (TLI.getTypeAction(Context, StoreTy) == 12402 TargetLowering::TypePromoteInteger) { 12403 EVT LegalizedStoredValueTy = 12404 TLI.getTypeToTransformTo(Context, StoredVal.getValueType()); 12405 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy) && 12406 TLI.allowsMemoryAccess(Context, DL, LegalizedStoredValueTy, 12407 FirstStoreAS, FirstStoreAlign, &IsFast) && 12408 IsFast) { 12409 LastLegalType = i + 1; 12410 } 12411 } 12412 12413 // We only use vectors if the constant is known to be zero or the target 12414 // allows it and the function is not marked with the noimplicitfloat 12415 // attribute. 12416 if ((!NonZero || 12417 TLI.storeOfVectorConstantIsCheap(MemVT, i + 1, FirstStoreAS)) && 12418 !NoVectors) { 12419 // Find a legal type for the vector store. 12420 EVT Ty = EVT::getVectorVT(Context, MemVT, i + 1); 12421 if (TLI.isTypeLegal(Ty) && TLI.canMergeStoresTo(Ty) && 12422 TLI.allowsMemoryAccess(Context, DL, Ty, FirstStoreAS, 12423 FirstStoreAlign, &IsFast) && 12424 IsFast) 12425 LastLegalVectorType = i + 1; 12426 } 12427 } 12428 12429 // Check if we found a legal integer type that creates a meaningful merge. 12430 if (LastLegalType < 2 && LastLegalVectorType < 2) 12431 break; 12432 12433 bool UseVector = (LastLegalVectorType > LastLegalType) && !NoVectors; 12434 unsigned NumElem = (UseVector) ? LastLegalVectorType : LastLegalType; 12435 12436 bool Merged = MergeStoresOfConstantsOrVecElts(StoreNodes, MemVT, NumElem, 12437 true, UseVector); 12438 if (!Merged) 12439 break; 12440 // Remove merged stores for next iteration. 12441 StoreNodes.erase(StoreNodes.begin(), StoreNodes.begin() + NumElem); 12442 RV = true; 12443 NumConsecutiveStores -= NumElem; 12444 } 12445 return RV; 12446 } 12447 12448 // When extracting multiple vector elements, try to store them 12449 // in one vector store rather than a sequence of scalar stores. 12450 if (IsExtractVecSrc) { 12451 LSBaseSDNode *FirstInChain = StoreNodes[0].MemNode; 12452 unsigned FirstStoreAS = FirstInChain->getAddressSpace(); 12453 unsigned FirstStoreAlign = FirstInChain->getAlignment(); 12454 unsigned NumStoresToMerge = 0; 12455 bool IsVec = MemVT.isVector(); 12456 for (unsigned i = 0; i < NumConsecutiveStores; ++i) { 12457 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode); 12458 unsigned StoreValOpcode = St->getValue().getOpcode(); 12459 // This restriction could be loosened. 12460 // Bail out if any stored values are not elements extracted from a vector. 12461 // It should be possible to handle mixed sources, but load sources need 12462 // more careful handling (see the block of code below that handles 12463 // consecutive loads). 12464 if (StoreValOpcode != ISD::EXTRACT_VECTOR_ELT && 12465 StoreValOpcode != ISD::EXTRACT_SUBVECTOR) 12466 return false; 12467 12468 // Find a legal type for the vector store. 12469 unsigned Elts = i + 1; 12470 if (IsVec) { 12471 // When merging vector stores, get the total number of elements. 12472 Elts *= MemVT.getVectorNumElements(); 12473 } 12474 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(), Elts); 12475 bool IsFast; 12476 if (TLI.isTypeLegal(Ty) && 12477 TLI.allowsMemoryAccess(Context, DL, Ty, FirstStoreAS, 12478 FirstStoreAlign, &IsFast) && IsFast) 12479 NumStoresToMerge = i + 1; 12480 } 12481 12482 return MergeStoresOfConstantsOrVecElts(StoreNodes, MemVT, NumStoresToMerge, 12483 false, true); 12484 } 12485 12486 // Below we handle the case of multiple consecutive stores that 12487 // come from multiple consecutive loads. We merge them into a single 12488 // wide load and a single wide store. 12489 12490 // Look for load nodes which are used by the stored values. 12491 SmallVector<MemOpLink, 8> LoadNodes; 12492 12493 // Find acceptable loads. Loads need to have the same chain (token factor), 12494 // must not be zext, volatile, indexed, and they must be consecutive. 12495 BaseIndexOffset LdBasePtr; 12496 for (unsigned i = 0; i < NumConsecutiveStores; ++i) { 12497 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode); 12498 LoadSDNode *Ld = dyn_cast<LoadSDNode>(St->getValue()); 12499 if (!Ld) break; 12500 12501 // Loads must only have one use. 12502 if (!Ld->hasNUsesOfValue(1, 0)) 12503 break; 12504 12505 // The memory operands must not be volatile. 12506 if (Ld->isVolatile() || Ld->isIndexed()) 12507 break; 12508 12509 // We do not accept ext loads. 12510 if (Ld->getExtensionType() != ISD::NON_EXTLOAD) 12511 break; 12512 12513 // The stored memory type must be the same. 12514 if (Ld->getMemoryVT() != MemVT) 12515 break; 12516 12517 BaseIndexOffset LdPtr = BaseIndexOffset::match(Ld->getBasePtr(), DAG); 12518 // If this is not the first ptr that we check. 12519 if (LdBasePtr.Base.getNode()) { 12520 // The base ptr must be the same. 12521 if (!LdPtr.equalBaseIndex(LdBasePtr)) 12522 break; 12523 } else { 12524 // Check that all other base pointers are the same as this one. 12525 LdBasePtr = LdPtr; 12526 } 12527 12528 // We found a potential memory operand to merge. 12529 LoadNodes.push_back(MemOpLink(Ld, LdPtr.Offset)); 12530 } 12531 12532 if (LoadNodes.size() < 2) 12533 return false; 12534 12535 // If we have load/store pair instructions and we only have two values, 12536 // don't bother. 12537 unsigned RequiredAlignment; 12538 if (LoadNodes.size() == 2 && TLI.hasPairedLoad(MemVT, RequiredAlignment) && 12539 St->getAlignment() >= RequiredAlignment) 12540 return false; 12541 LSBaseSDNode *FirstInChain = StoreNodes[0].MemNode; 12542 unsigned FirstStoreAS = FirstInChain->getAddressSpace(); 12543 unsigned FirstStoreAlign = FirstInChain->getAlignment(); 12544 LoadSDNode *FirstLoad = cast<LoadSDNode>(LoadNodes[0].MemNode); 12545 unsigned FirstLoadAS = FirstLoad->getAddressSpace(); 12546 unsigned FirstLoadAlign = FirstLoad->getAlignment(); 12547 12548 // Scan the memory operations on the chain and find the first non-consecutive 12549 // load memory address. These variables hold the index in the store node 12550 // array. 12551 unsigned LastConsecutiveLoad = 0; 12552 // This variable refers to the size and not index in the array. 12553 unsigned LastLegalVectorType = 0; 12554 unsigned LastLegalIntegerType = 0; 12555 StartAddress = LoadNodes[0].OffsetFromBase; 12556 SDValue FirstChain = FirstLoad->getChain(); 12557 for (unsigned i = 1; i < LoadNodes.size(); ++i) { 12558 // All loads must share the same chain. 12559 if (LoadNodes[i].MemNode->getChain() != FirstChain) 12560 break; 12561 12562 int64_t CurrAddress = LoadNodes[i].OffsetFromBase; 12563 if (CurrAddress - StartAddress != (ElementSizeBytes * i)) 12564 break; 12565 LastConsecutiveLoad = i; 12566 // Find a legal type for the vector store. 12567 EVT StoreTy = EVT::getVectorVT(Context, MemVT, i+1); 12568 bool IsFastSt, IsFastLd; 12569 if (TLI.isTypeLegal(StoreTy) && 12570 TLI.allowsMemoryAccess(Context, DL, StoreTy, FirstStoreAS, 12571 FirstStoreAlign, &IsFastSt) && IsFastSt && 12572 TLI.allowsMemoryAccess(Context, DL, StoreTy, FirstLoadAS, 12573 FirstLoadAlign, &IsFastLd) && IsFastLd) { 12574 LastLegalVectorType = i + 1; 12575 } 12576 12577 // Find a legal type for the integer store. 12578 unsigned SizeInBits = (i+1) * ElementSizeBytes * 8; 12579 StoreTy = EVT::getIntegerVT(Context, SizeInBits); 12580 if (TLI.isTypeLegal(StoreTy) && 12581 TLI.allowsMemoryAccess(Context, DL, StoreTy, FirstStoreAS, 12582 FirstStoreAlign, &IsFastSt) && IsFastSt && 12583 TLI.allowsMemoryAccess(Context, DL, StoreTy, FirstLoadAS, 12584 FirstLoadAlign, &IsFastLd) && IsFastLd) 12585 LastLegalIntegerType = i + 1; 12586 // Or check whether a truncstore and extload is legal. 12587 else if (TLI.getTypeAction(Context, StoreTy) == 12588 TargetLowering::TypePromoteInteger) { 12589 EVT LegalizedStoredValueTy = 12590 TLI.getTypeToTransformTo(Context, StoreTy); 12591 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy) && 12592 TLI.isLoadExtLegal(ISD::ZEXTLOAD, LegalizedStoredValueTy, StoreTy) && 12593 TLI.isLoadExtLegal(ISD::SEXTLOAD, LegalizedStoredValueTy, StoreTy) && 12594 TLI.isLoadExtLegal(ISD::EXTLOAD, LegalizedStoredValueTy, StoreTy) && 12595 TLI.allowsMemoryAccess(Context, DL, LegalizedStoredValueTy, 12596 FirstStoreAS, FirstStoreAlign, &IsFastSt) && 12597 IsFastSt && 12598 TLI.allowsMemoryAccess(Context, DL, LegalizedStoredValueTy, 12599 FirstLoadAS, FirstLoadAlign, &IsFastLd) && 12600 IsFastLd) 12601 LastLegalIntegerType = i+1; 12602 } 12603 } 12604 12605 // Only use vector types if the vector type is larger than the integer type. 12606 // If they are the same, use integers. 12607 bool UseVectorTy = LastLegalVectorType > LastLegalIntegerType && !NoVectors; 12608 unsigned LastLegalType = std::max(LastLegalVectorType, LastLegalIntegerType); 12609 12610 // We add +1 here because the LastXXX variables refer to location while 12611 // the NumElem refers to array/index size. 12612 unsigned NumElem = std::min(NumConsecutiveStores, LastConsecutiveLoad + 1); 12613 NumElem = std::min(LastLegalType, NumElem); 12614 12615 if (NumElem < 2) 12616 return false; 12617 12618 // Collect the chains from all merged stores. Because the common case 12619 // all chains are the same, check if we match the first Chain. 12620 SmallVector<SDValue, 8> MergeStoreChains; 12621 MergeStoreChains.push_back(StoreNodes[0].MemNode->getChain()); 12622 for (unsigned i = 1; i < NumElem; ++i) 12623 if (StoreNodes[0].MemNode->getChain() != StoreNodes[i].MemNode->getChain()) 12624 MergeStoreChains.push_back(StoreNodes[i].MemNode->getChain()); 12625 12626 // Find if it is better to use vectors or integers to load and store 12627 // to memory. 12628 EVT JointMemOpVT; 12629 if (UseVectorTy) { 12630 JointMemOpVT = EVT::getVectorVT(Context, MemVT, NumElem); 12631 } else { 12632 unsigned SizeInBits = NumElem * ElementSizeBytes * 8; 12633 JointMemOpVT = EVT::getIntegerVT(Context, SizeInBits); 12634 } 12635 12636 SDLoc LoadDL(LoadNodes[0].MemNode); 12637 SDLoc StoreDL(StoreNodes[0].MemNode); 12638 12639 // The merged loads are required to have the same incoming chain, so 12640 // using the first's chain is acceptable. 12641 SDValue NewLoad = DAG.getLoad(JointMemOpVT, LoadDL, FirstLoad->getChain(), 12642 FirstLoad->getBasePtr(), 12643 FirstLoad->getPointerInfo(), FirstLoadAlign); 12644 12645 SDValue NewStoreChain = 12646 DAG.getNode(ISD::TokenFactor, StoreDL, MVT::Other, MergeStoreChains); 12647 12648 AddToWorklist(NewStoreChain.getNode()); 12649 12650 SDValue NewStore = 12651 DAG.getStore(NewStoreChain, StoreDL, NewLoad, FirstInChain->getBasePtr(), 12652 FirstInChain->getPointerInfo(), FirstStoreAlign); 12653 12654 // Transfer chain users from old loads to the new load. 12655 for (unsigned i = 0; i < NumElem; ++i) { 12656 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[i].MemNode); 12657 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), 12658 SDValue(NewLoad.getNode(), 1)); 12659 } 12660 12661 // Replace the all stores with the new store. 12662 for (unsigned i = 0; i < NumElem; ++i) 12663 CombineTo(StoreNodes[i].MemNode, NewStore); 12664 return true; 12665 } 12666 12667 SDValue DAGCombiner::replaceStoreChain(StoreSDNode *ST, SDValue BetterChain) { 12668 SDLoc SL(ST); 12669 SDValue ReplStore; 12670 12671 // Replace the chain to avoid dependency. 12672 if (ST->isTruncatingStore()) { 12673 ReplStore = DAG.getTruncStore(BetterChain, SL, ST->getValue(), 12674 ST->getBasePtr(), ST->getMemoryVT(), 12675 ST->getMemOperand()); 12676 } else { 12677 ReplStore = DAG.getStore(BetterChain, SL, ST->getValue(), ST->getBasePtr(), 12678 ST->getMemOperand()); 12679 } 12680 12681 // Create token to keep both nodes around. 12682 SDValue Token = DAG.getNode(ISD::TokenFactor, SL, 12683 MVT::Other, ST->getChain(), ReplStore); 12684 12685 // Make sure the new and old chains are cleaned up. 12686 AddToWorklist(Token.getNode()); 12687 12688 // Don't add users to work list. 12689 return CombineTo(ST, Token, false); 12690 } 12691 12692 SDValue DAGCombiner::replaceStoreOfFPConstant(StoreSDNode *ST) { 12693 SDValue Value = ST->getValue(); 12694 if (Value.getOpcode() == ISD::TargetConstantFP) 12695 return SDValue(); 12696 12697 SDLoc DL(ST); 12698 12699 SDValue Chain = ST->getChain(); 12700 SDValue Ptr = ST->getBasePtr(); 12701 12702 const ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Value); 12703 12704 // NOTE: If the original store is volatile, this transform must not increase 12705 // the number of stores. For example, on x86-32 an f64 can be stored in one 12706 // processor operation but an i64 (which is not legal) requires two. So the 12707 // transform should not be done in this case. 12708 12709 SDValue Tmp; 12710 switch (CFP->getSimpleValueType(0).SimpleTy) { 12711 default: 12712 llvm_unreachable("Unknown FP type"); 12713 case MVT::f16: // We don't do this for these yet. 12714 case MVT::f80: 12715 case MVT::f128: 12716 case MVT::ppcf128: 12717 return SDValue(); 12718 case MVT::f32: 12719 if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) || 12720 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) { 12721 ; 12722 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF(). 12723 bitcastToAPInt().getZExtValue(), SDLoc(CFP), 12724 MVT::i32); 12725 return DAG.getStore(Chain, DL, Tmp, Ptr, ST->getMemOperand()); 12726 } 12727 12728 return SDValue(); 12729 case MVT::f64: 12730 if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations && 12731 !ST->isVolatile()) || 12732 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) { 12733 ; 12734 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt(). 12735 getZExtValue(), SDLoc(CFP), MVT::i64); 12736 return DAG.getStore(Chain, DL, Tmp, 12737 Ptr, ST->getMemOperand()); 12738 } 12739 12740 if (!ST->isVolatile() && 12741 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) { 12742 // Many FP stores are not made apparent until after legalize, e.g. for 12743 // argument passing. Since this is so common, custom legalize the 12744 // 64-bit integer store into two 32-bit stores. 12745 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue(); 12746 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, SDLoc(CFP), MVT::i32); 12747 SDValue Hi = DAG.getConstant(Val >> 32, SDLoc(CFP), MVT::i32); 12748 if (DAG.getDataLayout().isBigEndian()) 12749 std::swap(Lo, Hi); 12750 12751 unsigned Alignment = ST->getAlignment(); 12752 MachineMemOperand::Flags MMOFlags = ST->getMemOperand()->getFlags(); 12753 AAMDNodes AAInfo = ST->getAAInfo(); 12754 12755 SDValue St0 = DAG.getStore(Chain, DL, Lo, Ptr, ST->getPointerInfo(), 12756 ST->getAlignment(), MMOFlags, AAInfo); 12757 Ptr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr, 12758 DAG.getConstant(4, DL, Ptr.getValueType())); 12759 Alignment = MinAlign(Alignment, 4U); 12760 SDValue St1 = DAG.getStore(Chain, DL, Hi, Ptr, 12761 ST->getPointerInfo().getWithOffset(4), 12762 Alignment, MMOFlags, AAInfo); 12763 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, 12764 St0, St1); 12765 } 12766 12767 return SDValue(); 12768 } 12769 } 12770 12771 SDValue DAGCombiner::visitSTORE(SDNode *N) { 12772 StoreSDNode *ST = cast<StoreSDNode>(N); 12773 SDValue Chain = ST->getChain(); 12774 SDValue Value = ST->getValue(); 12775 SDValue Ptr = ST->getBasePtr(); 12776 12777 // If this is a store of a bit convert, store the input value if the 12778 // resultant store does not need a higher alignment than the original. 12779 if (Value.getOpcode() == ISD::BITCAST && !ST->isTruncatingStore() && 12780 ST->isUnindexed()) { 12781 EVT SVT = Value.getOperand(0).getValueType(); 12782 if (((!LegalOperations && !ST->isVolatile()) || 12783 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)) && 12784 TLI.isStoreBitCastBeneficial(Value.getValueType(), SVT)) { 12785 unsigned OrigAlign = ST->getAlignment(); 12786 bool Fast = false; 12787 if (TLI.allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), SVT, 12788 ST->getAddressSpace(), OrigAlign, &Fast) && 12789 Fast) { 12790 return DAG.getStore(Chain, SDLoc(N), Value.getOperand(0), Ptr, 12791 ST->getPointerInfo(), OrigAlign, 12792 ST->getMemOperand()->getFlags(), ST->getAAInfo()); 12793 } 12794 } 12795 } 12796 12797 // Turn 'store undef, Ptr' -> nothing. 12798 if (Value.isUndef() && ST->isUnindexed()) 12799 return Chain; 12800 12801 // Try to infer better alignment information than the store already has. 12802 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) { 12803 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) { 12804 if (Align > ST->getAlignment()) { 12805 SDValue NewStore = 12806 DAG.getTruncStore(Chain, SDLoc(N), Value, Ptr, ST->getPointerInfo(), 12807 ST->getMemoryVT(), Align, 12808 ST->getMemOperand()->getFlags(), ST->getAAInfo()); 12809 if (NewStore.getNode() != N) 12810 return CombineTo(ST, NewStore, true); 12811 } 12812 } 12813 } 12814 12815 // Try transforming a pair floating point load / store ops to integer 12816 // load / store ops. 12817 if (SDValue NewST = TransformFPLoadStorePair(N)) 12818 return NewST; 12819 12820 if (ST->isUnindexed()) { 12821 // Walk up chain skipping non-aliasing memory nodes, on this store and any 12822 // adjacent stores. 12823 if (findBetterNeighborChains(ST)) { 12824 // replaceStoreChain uses CombineTo, which handled all of the worklist 12825 // manipulation. Return the original node to not do anything else. 12826 return SDValue(ST, 0); 12827 } 12828 Chain = ST->getChain(); 12829 } 12830 12831 // Try transforming N to an indexed store. 12832 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N)) 12833 return SDValue(N, 0); 12834 12835 // FIXME: is there such a thing as a truncating indexed store? 12836 if (ST->isTruncatingStore() && ST->isUnindexed() && 12837 Value.getValueType().isInteger()) { 12838 // See if we can simplify the input to this truncstore with knowledge that 12839 // only the low bits are being used. For example: 12840 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8" 12841 SDValue Shorter = GetDemandedBits( 12842 Value, APInt::getLowBitsSet(Value.getScalarValueSizeInBits(), 12843 ST->getMemoryVT().getScalarSizeInBits())); 12844 AddToWorklist(Value.getNode()); 12845 if (Shorter.getNode()) 12846 return DAG.getTruncStore(Chain, SDLoc(N), Shorter, 12847 Ptr, ST->getMemoryVT(), ST->getMemOperand()); 12848 12849 // Otherwise, see if we can simplify the operation with 12850 // SimplifyDemandedBits, which only works if the value has a single use. 12851 if (SimplifyDemandedBits( 12852 Value, 12853 APInt::getLowBitsSet(Value.getScalarValueSizeInBits(), 12854 ST->getMemoryVT().getScalarSizeInBits()))) { 12855 // Re-visit the store if anything changed and the store hasn't been merged 12856 // with another node (N is deleted) SimplifyDemandedBits will add Value's 12857 // node back to the worklist if necessary, but we also need to re-visit 12858 // the Store node itself. 12859 if (N->getOpcode() != ISD::DELETED_NODE) 12860 AddToWorklist(N); 12861 return SDValue(N, 0); 12862 } 12863 } 12864 12865 // If this is a load followed by a store to the same location, then the store 12866 // is dead/noop. 12867 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) { 12868 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() && 12869 ST->isUnindexed() && !ST->isVolatile() && 12870 // There can't be any side effects between the load and store, such as 12871 // a call or store. 12872 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) { 12873 // The store is dead, remove it. 12874 return Chain; 12875 } 12876 } 12877 12878 // If this is a store followed by a store with the same value to the same 12879 // location, then the store is dead/noop. 12880 if (StoreSDNode *ST1 = dyn_cast<StoreSDNode>(Chain)) { 12881 if (ST1->getBasePtr() == Ptr && ST->getMemoryVT() == ST1->getMemoryVT() && 12882 ST1->getValue() == Value && ST->isUnindexed() && !ST->isVolatile() && 12883 ST1->isUnindexed() && !ST1->isVolatile()) { 12884 // The store is dead, remove it. 12885 return Chain; 12886 } 12887 } 12888 12889 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a 12890 // truncating store. We can do this even if this is already a truncstore. 12891 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE) 12892 && Value.getNode()->hasOneUse() && ST->isUnindexed() && 12893 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(), 12894 ST->getMemoryVT())) { 12895 return DAG.getTruncStore(Chain, SDLoc(N), Value.getOperand(0), 12896 Ptr, ST->getMemoryVT(), ST->getMemOperand()); 12897 } 12898 12899 // Only perform this optimization before the types are legal, because we 12900 // don't want to perform this optimization on every DAGCombine invocation. 12901 if (!LegalTypes) { 12902 for (;;) { 12903 // There can be multiple store sequences on the same chain. 12904 // Keep trying to merge store sequences until we are unable to do so 12905 // or until we merge the last store on the chain. 12906 bool Changed = MergeConsecutiveStores(ST); 12907 if (!Changed) break; 12908 // Return N as merge only uses CombineTo and no worklist clean 12909 // up is necessary. 12910 if (N->getOpcode() == ISD::DELETED_NODE || !isa<StoreSDNode>(N)) 12911 return SDValue(N, 0); 12912 } 12913 } 12914 12915 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' 12916 // 12917 // Make sure to do this only after attempting to merge stores in order to 12918 // avoid changing the types of some subset of stores due to visit order, 12919 // preventing their merging. 12920 if (isa<ConstantFPSDNode>(ST->getValue())) { 12921 if (SDValue NewSt = replaceStoreOfFPConstant(ST)) 12922 return NewSt; 12923 } 12924 12925 if (SDValue NewSt = splitMergedValStore(ST)) 12926 return NewSt; 12927 12928 return ReduceLoadOpStoreWidth(N); 12929 } 12930 12931 /// For the instruction sequence of store below, F and I values 12932 /// are bundled together as an i64 value before being stored into memory. 12933 /// Sometimes it is more efficent to generate separate stores for F and I, 12934 /// which can remove the bitwise instructions or sink them to colder places. 12935 /// 12936 /// (store (or (zext (bitcast F to i32) to i64), 12937 /// (shl (zext I to i64), 32)), addr) --> 12938 /// (store F, addr) and (store I, addr+4) 12939 /// 12940 /// Similarly, splitting for other merged store can also be beneficial, like: 12941 /// For pair of {i32, i32}, i64 store --> two i32 stores. 12942 /// For pair of {i32, i16}, i64 store --> two i32 stores. 12943 /// For pair of {i16, i16}, i32 store --> two i16 stores. 12944 /// For pair of {i16, i8}, i32 store --> two i16 stores. 12945 /// For pair of {i8, i8}, i16 store --> two i8 stores. 12946 /// 12947 /// We allow each target to determine specifically which kind of splitting is 12948 /// supported. 12949 /// 12950 /// The store patterns are commonly seen from the simple code snippet below 12951 /// if only std::make_pair(...) is sroa transformed before inlined into hoo. 12952 /// void goo(const std::pair<int, float> &); 12953 /// hoo() { 12954 /// ... 12955 /// goo(std::make_pair(tmp, ftmp)); 12956 /// ... 12957 /// } 12958 /// 12959 SDValue DAGCombiner::splitMergedValStore(StoreSDNode *ST) { 12960 if (OptLevel == CodeGenOpt::None) 12961 return SDValue(); 12962 12963 SDValue Val = ST->getValue(); 12964 SDLoc DL(ST); 12965 12966 // Match OR operand. 12967 if (!Val.getValueType().isScalarInteger() || Val.getOpcode() != ISD::OR) 12968 return SDValue(); 12969 12970 // Match SHL operand and get Lower and Higher parts of Val. 12971 SDValue Op1 = Val.getOperand(0); 12972 SDValue Op2 = Val.getOperand(1); 12973 SDValue Lo, Hi; 12974 if (Op1.getOpcode() != ISD::SHL) { 12975 std::swap(Op1, Op2); 12976 if (Op1.getOpcode() != ISD::SHL) 12977 return SDValue(); 12978 } 12979 Lo = Op2; 12980 Hi = Op1.getOperand(0); 12981 if (!Op1.hasOneUse()) 12982 return SDValue(); 12983 12984 // Match shift amount to HalfValBitSize. 12985 unsigned HalfValBitSize = Val.getValueSizeInBits() / 2; 12986 ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op1.getOperand(1)); 12987 if (!ShAmt || ShAmt->getAPIntValue() != HalfValBitSize) 12988 return SDValue(); 12989 12990 // Lo and Hi are zero-extended from int with size less equal than 32 12991 // to i64. 12992 if (Lo.getOpcode() != ISD::ZERO_EXTEND || !Lo.hasOneUse() || 12993 !Lo.getOperand(0).getValueType().isScalarInteger() || 12994 Lo.getOperand(0).getValueSizeInBits() > HalfValBitSize || 12995 Hi.getOpcode() != ISD::ZERO_EXTEND || !Hi.hasOneUse() || 12996 !Hi.getOperand(0).getValueType().isScalarInteger() || 12997 Hi.getOperand(0).getValueSizeInBits() > HalfValBitSize) 12998 return SDValue(); 12999 13000 // Use the EVT of low and high parts before bitcast as the input 13001 // of target query. 13002 EVT LowTy = (Lo.getOperand(0).getOpcode() == ISD::BITCAST) 13003 ? Lo.getOperand(0).getValueType() 13004 : Lo.getValueType(); 13005 EVT HighTy = (Hi.getOperand(0).getOpcode() == ISD::BITCAST) 13006 ? Hi.getOperand(0).getValueType() 13007 : Hi.getValueType(); 13008 if (!TLI.isMultiStoresCheaperThanBitsMerge(LowTy, HighTy)) 13009 return SDValue(); 13010 13011 // Start to split store. 13012 unsigned Alignment = ST->getAlignment(); 13013 MachineMemOperand::Flags MMOFlags = ST->getMemOperand()->getFlags(); 13014 AAMDNodes AAInfo = ST->getAAInfo(); 13015 13016 // Change the sizes of Lo and Hi's value types to HalfValBitSize. 13017 EVT VT = EVT::getIntegerVT(*DAG.getContext(), HalfValBitSize); 13018 Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Lo.getOperand(0)); 13019 Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Hi.getOperand(0)); 13020 13021 SDValue Chain = ST->getChain(); 13022 SDValue Ptr = ST->getBasePtr(); 13023 // Lower value store. 13024 SDValue St0 = DAG.getStore(Chain, DL, Lo, Ptr, ST->getPointerInfo(), 13025 ST->getAlignment(), MMOFlags, AAInfo); 13026 Ptr = 13027 DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr, 13028 DAG.getConstant(HalfValBitSize / 8, DL, Ptr.getValueType())); 13029 // Higher value store. 13030 SDValue St1 = 13031 DAG.getStore(St0, DL, Hi, Ptr, 13032 ST->getPointerInfo().getWithOffset(HalfValBitSize / 8), 13033 Alignment / 2, MMOFlags, AAInfo); 13034 return St1; 13035 } 13036 13037 SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) { 13038 SDValue InVec = N->getOperand(0); 13039 SDValue InVal = N->getOperand(1); 13040 SDValue EltNo = N->getOperand(2); 13041 SDLoc DL(N); 13042 13043 // If the inserted element is an UNDEF, just use the input vector. 13044 if (InVal.isUndef()) 13045 return InVec; 13046 13047 EVT VT = InVec.getValueType(); 13048 13049 // Check that we know which element is being inserted 13050 if (!isa<ConstantSDNode>(EltNo)) 13051 return SDValue(); 13052 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue(); 13053 13054 // Canonicalize insert_vector_elt dag nodes. 13055 // Example: 13056 // (insert_vector_elt (insert_vector_elt A, Idx0), Idx1) 13057 // -> (insert_vector_elt (insert_vector_elt A, Idx1), Idx0) 13058 // 13059 // Do this only if the child insert_vector node has one use; also 13060 // do this only if indices are both constants and Idx1 < Idx0. 13061 if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT && InVec.hasOneUse() 13062 && isa<ConstantSDNode>(InVec.getOperand(2))) { 13063 unsigned OtherElt = 13064 cast<ConstantSDNode>(InVec.getOperand(2))->getZExtValue(); 13065 if (Elt < OtherElt) { 13066 // Swap nodes. 13067 SDValue NewOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, 13068 InVec.getOperand(0), InVal, EltNo); 13069 AddToWorklist(NewOp.getNode()); 13070 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(InVec.getNode()), 13071 VT, NewOp, InVec.getOperand(1), InVec.getOperand(2)); 13072 } 13073 } 13074 13075 // If we can't generate a legal BUILD_VECTOR, exit 13076 if (LegalOperations && !TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) 13077 return SDValue(); 13078 13079 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially 13080 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the 13081 // vector elements. 13082 SmallVector<SDValue, 8> Ops; 13083 // Do not combine these two vectors if the output vector will not replace 13084 // the input vector. 13085 if (InVec.getOpcode() == ISD::BUILD_VECTOR && InVec.hasOneUse()) { 13086 Ops.append(InVec.getNode()->op_begin(), 13087 InVec.getNode()->op_end()); 13088 } else if (InVec.isUndef()) { 13089 unsigned NElts = VT.getVectorNumElements(); 13090 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType())); 13091 } else { 13092 return SDValue(); 13093 } 13094 13095 // Insert the element 13096 if (Elt < Ops.size()) { 13097 // All the operands of BUILD_VECTOR must have the same type; 13098 // we enforce that here. 13099 EVT OpVT = Ops[0].getValueType(); 13100 Ops[Elt] = OpVT.isInteger() ? DAG.getAnyExtOrTrunc(InVal, DL, OpVT) : InVal; 13101 } 13102 13103 // Return the new vector 13104 return DAG.getBuildVector(VT, DL, Ops); 13105 } 13106 13107 SDValue DAGCombiner::ReplaceExtractVectorEltOfLoadWithNarrowedLoad( 13108 SDNode *EVE, EVT InVecVT, SDValue EltNo, LoadSDNode *OriginalLoad) { 13109 assert(!OriginalLoad->isVolatile()); 13110 13111 EVT ResultVT = EVE->getValueType(0); 13112 EVT VecEltVT = InVecVT.getVectorElementType(); 13113 unsigned Align = OriginalLoad->getAlignment(); 13114 unsigned NewAlign = DAG.getDataLayout().getABITypeAlignment( 13115 VecEltVT.getTypeForEVT(*DAG.getContext())); 13116 13117 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VecEltVT)) 13118 return SDValue(); 13119 13120 ISD::LoadExtType ExtTy = ResultVT.bitsGT(VecEltVT) ? 13121 ISD::NON_EXTLOAD : ISD::EXTLOAD; 13122 if (!TLI.shouldReduceLoadWidth(OriginalLoad, ExtTy, VecEltVT)) 13123 return SDValue(); 13124 13125 Align = NewAlign; 13126 13127 SDValue NewPtr = OriginalLoad->getBasePtr(); 13128 SDValue Offset; 13129 EVT PtrType = NewPtr.getValueType(); 13130 MachinePointerInfo MPI; 13131 SDLoc DL(EVE); 13132 if (auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo)) { 13133 int Elt = ConstEltNo->getZExtValue(); 13134 unsigned PtrOff = VecEltVT.getSizeInBits() * Elt / 8; 13135 Offset = DAG.getConstant(PtrOff, DL, PtrType); 13136 MPI = OriginalLoad->getPointerInfo().getWithOffset(PtrOff); 13137 } else { 13138 Offset = DAG.getZExtOrTrunc(EltNo, DL, PtrType); 13139 Offset = DAG.getNode( 13140 ISD::MUL, DL, PtrType, Offset, 13141 DAG.getConstant(VecEltVT.getStoreSize(), DL, PtrType)); 13142 MPI = OriginalLoad->getPointerInfo(); 13143 } 13144 NewPtr = DAG.getNode(ISD::ADD, DL, PtrType, NewPtr, Offset); 13145 13146 // The replacement we need to do here is a little tricky: we need to 13147 // replace an extractelement of a load with a load. 13148 // Use ReplaceAllUsesOfValuesWith to do the replacement. 13149 // Note that this replacement assumes that the extractvalue is the only 13150 // use of the load; that's okay because we don't want to perform this 13151 // transformation in other cases anyway. 13152 SDValue Load; 13153 SDValue Chain; 13154 if (ResultVT.bitsGT(VecEltVT)) { 13155 // If the result type of vextract is wider than the load, then issue an 13156 // extending load instead. 13157 ISD::LoadExtType ExtType = TLI.isLoadExtLegal(ISD::ZEXTLOAD, ResultVT, 13158 VecEltVT) 13159 ? ISD::ZEXTLOAD 13160 : ISD::EXTLOAD; 13161 Load = DAG.getExtLoad(ExtType, SDLoc(EVE), ResultVT, 13162 OriginalLoad->getChain(), NewPtr, MPI, VecEltVT, 13163 Align, OriginalLoad->getMemOperand()->getFlags(), 13164 OriginalLoad->getAAInfo()); 13165 Chain = Load.getValue(1); 13166 } else { 13167 Load = DAG.getLoad(VecEltVT, SDLoc(EVE), OriginalLoad->getChain(), NewPtr, 13168 MPI, Align, OriginalLoad->getMemOperand()->getFlags(), 13169 OriginalLoad->getAAInfo()); 13170 Chain = Load.getValue(1); 13171 if (ResultVT.bitsLT(VecEltVT)) 13172 Load = DAG.getNode(ISD::TRUNCATE, SDLoc(EVE), ResultVT, Load); 13173 else 13174 Load = DAG.getBitcast(ResultVT, Load); 13175 } 13176 WorklistRemover DeadNodes(*this); 13177 SDValue From[] = { SDValue(EVE, 0), SDValue(OriginalLoad, 1) }; 13178 SDValue To[] = { Load, Chain }; 13179 DAG.ReplaceAllUsesOfValuesWith(From, To, 2); 13180 // Since we're explicitly calling ReplaceAllUses, add the new node to the 13181 // worklist explicitly as well. 13182 AddToWorklist(Load.getNode()); 13183 AddUsersToWorklist(Load.getNode()); // Add users too 13184 // Make sure to revisit this node to clean it up; it will usually be dead. 13185 AddToWorklist(EVE); 13186 ++OpsNarrowed; 13187 return SDValue(EVE, 0); 13188 } 13189 13190 SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) { 13191 // (vextract (scalar_to_vector val, 0) -> val 13192 SDValue InVec = N->getOperand(0); 13193 EVT VT = InVec.getValueType(); 13194 EVT NVT = N->getValueType(0); 13195 13196 if (InVec.isUndef()) 13197 return DAG.getUNDEF(NVT); 13198 13199 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) { 13200 // Check if the result type doesn't match the inserted element type. A 13201 // SCALAR_TO_VECTOR may truncate the inserted element and the 13202 // EXTRACT_VECTOR_ELT may widen the extracted vector. 13203 SDValue InOp = InVec.getOperand(0); 13204 if (InOp.getValueType() != NVT) { 13205 assert(InOp.getValueType().isInteger() && NVT.isInteger()); 13206 return DAG.getSExtOrTrunc(InOp, SDLoc(InVec), NVT); 13207 } 13208 return InOp; 13209 } 13210 13211 SDValue EltNo = N->getOperand(1); 13212 ConstantSDNode *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo); 13213 13214 // extract_vector_elt (build_vector x, y), 1 -> y 13215 if (ConstEltNo && 13216 InVec.getOpcode() == ISD::BUILD_VECTOR && 13217 TLI.isTypeLegal(VT) && 13218 (InVec.hasOneUse() || 13219 TLI.aggressivelyPreferBuildVectorSources(VT))) { 13220 SDValue Elt = InVec.getOperand(ConstEltNo->getZExtValue()); 13221 EVT InEltVT = Elt.getValueType(); 13222 13223 // Sometimes build_vector's scalar input types do not match result type. 13224 if (NVT == InEltVT) 13225 return Elt; 13226 13227 // TODO: It may be useful to truncate if free if the build_vector implicitly 13228 // converts. 13229 } 13230 13231 // extract_vector_elt (v2i32 (bitcast i64:x)), 0 -> i32 (trunc i64:x) 13232 if (ConstEltNo && InVec.getOpcode() == ISD::BITCAST && InVec.hasOneUse() && 13233 ConstEltNo->isNullValue() && VT.isInteger()) { 13234 SDValue BCSrc = InVec.getOperand(0); 13235 if (BCSrc.getValueType().isScalarInteger()) 13236 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), NVT, BCSrc); 13237 } 13238 13239 // extract_vector_elt (insert_vector_elt vec, val, idx), idx) -> val 13240 // 13241 // This only really matters if the index is non-constant since other combines 13242 // on the constant elements already work. 13243 if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT && 13244 EltNo == InVec.getOperand(2)) { 13245 SDValue Elt = InVec.getOperand(1); 13246 return VT.isInteger() ? DAG.getAnyExtOrTrunc(Elt, SDLoc(N), NVT) : Elt; 13247 } 13248 13249 // Transform: (EXTRACT_VECTOR_ELT( VECTOR_SHUFFLE )) -> EXTRACT_VECTOR_ELT. 13250 // We only perform this optimization before the op legalization phase because 13251 // we may introduce new vector instructions which are not backed by TD 13252 // patterns. For example on AVX, extracting elements from a wide vector 13253 // without using extract_subvector. However, if we can find an underlying 13254 // scalar value, then we can always use that. 13255 if (ConstEltNo && InVec.getOpcode() == ISD::VECTOR_SHUFFLE) { 13256 int NumElem = VT.getVectorNumElements(); 13257 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(InVec); 13258 // Find the new index to extract from. 13259 int OrigElt = SVOp->getMaskElt(ConstEltNo->getZExtValue()); 13260 13261 // Extracting an undef index is undef. 13262 if (OrigElt == -1) 13263 return DAG.getUNDEF(NVT); 13264 13265 // Select the right vector half to extract from. 13266 SDValue SVInVec; 13267 if (OrigElt < NumElem) { 13268 SVInVec = InVec->getOperand(0); 13269 } else { 13270 SVInVec = InVec->getOperand(1); 13271 OrigElt -= NumElem; 13272 } 13273 13274 if (SVInVec.getOpcode() == ISD::BUILD_VECTOR) { 13275 SDValue InOp = SVInVec.getOperand(OrigElt); 13276 if (InOp.getValueType() != NVT) { 13277 assert(InOp.getValueType().isInteger() && NVT.isInteger()); 13278 InOp = DAG.getSExtOrTrunc(InOp, SDLoc(SVInVec), NVT); 13279 } 13280 13281 return InOp; 13282 } 13283 13284 // FIXME: We should handle recursing on other vector shuffles and 13285 // scalar_to_vector here as well. 13286 13287 if (!LegalOperations) { 13288 EVT IndexTy = TLI.getVectorIdxTy(DAG.getDataLayout()); 13289 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N), NVT, SVInVec, 13290 DAG.getConstant(OrigElt, SDLoc(SVOp), IndexTy)); 13291 } 13292 } 13293 13294 bool BCNumEltsChanged = false; 13295 EVT ExtVT = VT.getVectorElementType(); 13296 EVT LVT = ExtVT; 13297 13298 // If the result of load has to be truncated, then it's not necessarily 13299 // profitable. 13300 if (NVT.bitsLT(LVT) && !TLI.isTruncateFree(LVT, NVT)) 13301 return SDValue(); 13302 13303 if (InVec.getOpcode() == ISD::BITCAST) { 13304 // Don't duplicate a load with other uses. 13305 if (!InVec.hasOneUse()) 13306 return SDValue(); 13307 13308 EVT BCVT = InVec.getOperand(0).getValueType(); 13309 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType())) 13310 return SDValue(); 13311 if (VT.getVectorNumElements() != BCVT.getVectorNumElements()) 13312 BCNumEltsChanged = true; 13313 InVec = InVec.getOperand(0); 13314 ExtVT = BCVT.getVectorElementType(); 13315 } 13316 13317 // (vextract (vN[if]M load $addr), i) -> ([if]M load $addr + i * size) 13318 if (!LegalOperations && !ConstEltNo && InVec.hasOneUse() && 13319 ISD::isNormalLoad(InVec.getNode()) && 13320 !N->getOperand(1)->hasPredecessor(InVec.getNode())) { 13321 SDValue Index = N->getOperand(1); 13322 if (LoadSDNode *OrigLoad = dyn_cast<LoadSDNode>(InVec)) { 13323 if (!OrigLoad->isVolatile()) { 13324 return ReplaceExtractVectorEltOfLoadWithNarrowedLoad(N, VT, Index, 13325 OrigLoad); 13326 } 13327 } 13328 } 13329 13330 // Perform only after legalization to ensure build_vector / vector_shuffle 13331 // optimizations have already been done. 13332 if (!LegalOperations) return SDValue(); 13333 13334 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size) 13335 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size) 13336 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr) 13337 13338 if (ConstEltNo) { 13339 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue(); 13340 13341 LoadSDNode *LN0 = nullptr; 13342 const ShuffleVectorSDNode *SVN = nullptr; 13343 if (ISD::isNormalLoad(InVec.getNode())) { 13344 LN0 = cast<LoadSDNode>(InVec); 13345 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR && 13346 InVec.getOperand(0).getValueType() == ExtVT && 13347 ISD::isNormalLoad(InVec.getOperand(0).getNode())) { 13348 // Don't duplicate a load with other uses. 13349 if (!InVec.hasOneUse()) 13350 return SDValue(); 13351 13352 LN0 = cast<LoadSDNode>(InVec.getOperand(0)); 13353 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) { 13354 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1) 13355 // => 13356 // (load $addr+1*size) 13357 13358 // Don't duplicate a load with other uses. 13359 if (!InVec.hasOneUse()) 13360 return SDValue(); 13361 13362 // If the bit convert changed the number of elements, it is unsafe 13363 // to examine the mask. 13364 if (BCNumEltsChanged) 13365 return SDValue(); 13366 13367 // Select the input vector, guarding against out of range extract vector. 13368 unsigned NumElems = VT.getVectorNumElements(); 13369 int Idx = (Elt > (int)NumElems) ? -1 : SVN->getMaskElt(Elt); 13370 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1); 13371 13372 if (InVec.getOpcode() == ISD::BITCAST) { 13373 // Don't duplicate a load with other uses. 13374 if (!InVec.hasOneUse()) 13375 return SDValue(); 13376 13377 InVec = InVec.getOperand(0); 13378 } 13379 if (ISD::isNormalLoad(InVec.getNode())) { 13380 LN0 = cast<LoadSDNode>(InVec); 13381 Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems; 13382 EltNo = DAG.getConstant(Elt, SDLoc(EltNo), EltNo.getValueType()); 13383 } 13384 } 13385 13386 // Make sure we found a non-volatile load and the extractelement is 13387 // the only use. 13388 if (!LN0 || !LN0->hasNUsesOfValue(1,0) || LN0->isVolatile()) 13389 return SDValue(); 13390 13391 // If Idx was -1 above, Elt is going to be -1, so just return undef. 13392 if (Elt == -1) 13393 return DAG.getUNDEF(LVT); 13394 13395 return ReplaceExtractVectorEltOfLoadWithNarrowedLoad(N, VT, EltNo, LN0); 13396 } 13397 13398 return SDValue(); 13399 } 13400 13401 // Simplify (build_vec (ext )) to (bitcast (build_vec )) 13402 SDValue DAGCombiner::reduceBuildVecExtToExtBuildVec(SDNode *N) { 13403 // We perform this optimization post type-legalization because 13404 // the type-legalizer often scalarizes integer-promoted vectors. 13405 // Performing this optimization before may create bit-casts which 13406 // will be type-legalized to complex code sequences. 13407 // We perform this optimization only before the operation legalizer because we 13408 // may introduce illegal operations. 13409 if (Level != AfterLegalizeVectorOps && Level != AfterLegalizeTypes) 13410 return SDValue(); 13411 13412 unsigned NumInScalars = N->getNumOperands(); 13413 SDLoc DL(N); 13414 EVT VT = N->getValueType(0); 13415 13416 // Check to see if this is a BUILD_VECTOR of a bunch of values 13417 // which come from any_extend or zero_extend nodes. If so, we can create 13418 // a new BUILD_VECTOR using bit-casts which may enable other BUILD_VECTOR 13419 // optimizations. We do not handle sign-extend because we can't fill the sign 13420 // using shuffles. 13421 EVT SourceType = MVT::Other; 13422 bool AllAnyExt = true; 13423 13424 for (unsigned i = 0; i != NumInScalars; ++i) { 13425 SDValue In = N->getOperand(i); 13426 // Ignore undef inputs. 13427 if (In.isUndef()) continue; 13428 13429 bool AnyExt = In.getOpcode() == ISD::ANY_EXTEND; 13430 bool ZeroExt = In.getOpcode() == ISD::ZERO_EXTEND; 13431 13432 // Abort if the element is not an extension. 13433 if (!ZeroExt && !AnyExt) { 13434 SourceType = MVT::Other; 13435 break; 13436 } 13437 13438 // The input is a ZeroExt or AnyExt. Check the original type. 13439 EVT InTy = In.getOperand(0).getValueType(); 13440 13441 // Check that all of the widened source types are the same. 13442 if (SourceType == MVT::Other) 13443 // First time. 13444 SourceType = InTy; 13445 else if (InTy != SourceType) { 13446 // Multiple income types. Abort. 13447 SourceType = MVT::Other; 13448 break; 13449 } 13450 13451 // Check if all of the extends are ANY_EXTENDs. 13452 AllAnyExt &= AnyExt; 13453 } 13454 13455 // In order to have valid types, all of the inputs must be extended from the 13456 // same source type and all of the inputs must be any or zero extend. 13457 // Scalar sizes must be a power of two. 13458 EVT OutScalarTy = VT.getScalarType(); 13459 bool ValidTypes = SourceType != MVT::Other && 13460 isPowerOf2_32(OutScalarTy.getSizeInBits()) && 13461 isPowerOf2_32(SourceType.getSizeInBits()); 13462 13463 // Create a new simpler BUILD_VECTOR sequence which other optimizations can 13464 // turn into a single shuffle instruction. 13465 if (!ValidTypes) 13466 return SDValue(); 13467 13468 bool isLE = DAG.getDataLayout().isLittleEndian(); 13469 unsigned ElemRatio = OutScalarTy.getSizeInBits()/SourceType.getSizeInBits(); 13470 assert(ElemRatio > 1 && "Invalid element size ratio"); 13471 SDValue Filler = AllAnyExt ? DAG.getUNDEF(SourceType): 13472 DAG.getConstant(0, DL, SourceType); 13473 13474 unsigned NewBVElems = ElemRatio * VT.getVectorNumElements(); 13475 SmallVector<SDValue, 8> Ops(NewBVElems, Filler); 13476 13477 // Populate the new build_vector 13478 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 13479 SDValue Cast = N->getOperand(i); 13480 assert((Cast.getOpcode() == ISD::ANY_EXTEND || 13481 Cast.getOpcode() == ISD::ZERO_EXTEND || 13482 Cast.isUndef()) && "Invalid cast opcode"); 13483 SDValue In; 13484 if (Cast.isUndef()) 13485 In = DAG.getUNDEF(SourceType); 13486 else 13487 In = Cast->getOperand(0); 13488 unsigned Index = isLE ? (i * ElemRatio) : 13489 (i * ElemRatio + (ElemRatio - 1)); 13490 13491 assert(Index < Ops.size() && "Invalid index"); 13492 Ops[Index] = In; 13493 } 13494 13495 // The type of the new BUILD_VECTOR node. 13496 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SourceType, NewBVElems); 13497 assert(VecVT.getSizeInBits() == VT.getSizeInBits() && 13498 "Invalid vector size"); 13499 // Check if the new vector type is legal. 13500 if (!isTypeLegal(VecVT)) return SDValue(); 13501 13502 // Make the new BUILD_VECTOR. 13503 SDValue BV = DAG.getBuildVector(VecVT, DL, Ops); 13504 13505 // The new BUILD_VECTOR node has the potential to be further optimized. 13506 AddToWorklist(BV.getNode()); 13507 // Bitcast to the desired type. 13508 return DAG.getBitcast(VT, BV); 13509 } 13510 13511 SDValue DAGCombiner::reduceBuildVecConvertToConvertBuildVec(SDNode *N) { 13512 EVT VT = N->getValueType(0); 13513 13514 unsigned NumInScalars = N->getNumOperands(); 13515 SDLoc DL(N); 13516 13517 EVT SrcVT = MVT::Other; 13518 unsigned Opcode = ISD::DELETED_NODE; 13519 unsigned NumDefs = 0; 13520 13521 for (unsigned i = 0; i != NumInScalars; ++i) { 13522 SDValue In = N->getOperand(i); 13523 unsigned Opc = In.getOpcode(); 13524 13525 if (Opc == ISD::UNDEF) 13526 continue; 13527 13528 // If all scalar values are floats and converted from integers. 13529 if (Opcode == ISD::DELETED_NODE && 13530 (Opc == ISD::UINT_TO_FP || Opc == ISD::SINT_TO_FP)) { 13531 Opcode = Opc; 13532 } 13533 13534 if (Opc != Opcode) 13535 return SDValue(); 13536 13537 EVT InVT = In.getOperand(0).getValueType(); 13538 13539 // If all scalar values are typed differently, bail out. It's chosen to 13540 // simplify BUILD_VECTOR of integer types. 13541 if (SrcVT == MVT::Other) 13542 SrcVT = InVT; 13543 if (SrcVT != InVT) 13544 return SDValue(); 13545 NumDefs++; 13546 } 13547 13548 // If the vector has just one element defined, it's not worth to fold it into 13549 // a vectorized one. 13550 if (NumDefs < 2) 13551 return SDValue(); 13552 13553 assert((Opcode == ISD::UINT_TO_FP || Opcode == ISD::SINT_TO_FP) 13554 && "Should only handle conversion from integer to float."); 13555 assert(SrcVT != MVT::Other && "Cannot determine source type!"); 13556 13557 EVT NVT = EVT::getVectorVT(*DAG.getContext(), SrcVT, NumInScalars); 13558 13559 if (!TLI.isOperationLegalOrCustom(Opcode, NVT)) 13560 return SDValue(); 13561 13562 // Just because the floating-point vector type is legal does not necessarily 13563 // mean that the corresponding integer vector type is. 13564 if (!isTypeLegal(NVT)) 13565 return SDValue(); 13566 13567 SmallVector<SDValue, 8> Opnds; 13568 for (unsigned i = 0; i != NumInScalars; ++i) { 13569 SDValue In = N->getOperand(i); 13570 13571 if (In.isUndef()) 13572 Opnds.push_back(DAG.getUNDEF(SrcVT)); 13573 else 13574 Opnds.push_back(In.getOperand(0)); 13575 } 13576 SDValue BV = DAG.getBuildVector(NVT, DL, Opnds); 13577 AddToWorklist(BV.getNode()); 13578 13579 return DAG.getNode(Opcode, DL, VT, BV); 13580 } 13581 13582 SDValue DAGCombiner::createBuildVecShuffle(const SDLoc &DL, SDNode *N, 13583 ArrayRef<int> VectorMask, 13584 SDValue VecIn1, SDValue VecIn2, 13585 unsigned LeftIdx) { 13586 MVT IdxTy = TLI.getVectorIdxTy(DAG.getDataLayout()); 13587 SDValue ZeroIdx = DAG.getConstant(0, DL, IdxTy); 13588 13589 EVT VT = N->getValueType(0); 13590 EVT InVT1 = VecIn1.getValueType(); 13591 EVT InVT2 = VecIn2.getNode() ? VecIn2.getValueType() : InVT1; 13592 13593 unsigned Vec2Offset = InVT1.getVectorNumElements(); 13594 unsigned NumElems = VT.getVectorNumElements(); 13595 unsigned ShuffleNumElems = NumElems; 13596 13597 // We can't generate a shuffle node with mismatched input and output types. 13598 // Try to make the types match the type of the output. 13599 if (InVT1 != VT || InVT2 != VT) { 13600 if ((VT.getSizeInBits() % InVT1.getSizeInBits() == 0) && InVT1 == InVT2) { 13601 // If the output vector length is a multiple of both input lengths, 13602 // we can concatenate them and pad the rest with undefs. 13603 unsigned NumConcats = VT.getSizeInBits() / InVT1.getSizeInBits(); 13604 assert(NumConcats >= 2 && "Concat needs at least two inputs!"); 13605 SmallVector<SDValue, 2> ConcatOps(NumConcats, DAG.getUNDEF(InVT1)); 13606 ConcatOps[0] = VecIn1; 13607 ConcatOps[1] = VecIn2 ? VecIn2 : DAG.getUNDEF(InVT1); 13608 VecIn1 = DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, ConcatOps); 13609 VecIn2 = SDValue(); 13610 } else if (InVT1.getSizeInBits() == VT.getSizeInBits() * 2) { 13611 if (!TLI.isExtractSubvectorCheap(VT, NumElems)) 13612 return SDValue(); 13613 13614 if (!VecIn2.getNode()) { 13615 // If we only have one input vector, and it's twice the size of the 13616 // output, split it in two. 13617 VecIn2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, VecIn1, 13618 DAG.getConstant(NumElems, DL, IdxTy)); 13619 VecIn1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, VecIn1, ZeroIdx); 13620 // Since we now have shorter input vectors, adjust the offset of the 13621 // second vector's start. 13622 Vec2Offset = NumElems; 13623 } else if (InVT2.getSizeInBits() <= InVT1.getSizeInBits()) { 13624 // VecIn1 is wider than the output, and we have another, possibly 13625 // smaller input. Pad the smaller input with undefs, shuffle at the 13626 // input vector width, and extract the output. 13627 // The shuffle type is different than VT, so check legality again. 13628 if (LegalOperations && 13629 !TLI.isOperationLegal(ISD::VECTOR_SHUFFLE, InVT1)) 13630 return SDValue(); 13631 13632 // Legalizing INSERT_SUBVECTOR is tricky - you basically have to 13633 // lower it back into a BUILD_VECTOR. So if the inserted type is 13634 // illegal, don't even try. 13635 if (InVT1 != InVT2) { 13636 if (!TLI.isTypeLegal(InVT2)) 13637 return SDValue(); 13638 VecIn2 = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, InVT1, 13639 DAG.getUNDEF(InVT1), VecIn2, ZeroIdx); 13640 } 13641 ShuffleNumElems = NumElems * 2; 13642 } else { 13643 // Both VecIn1 and VecIn2 are wider than the output, and VecIn2 is wider 13644 // than VecIn1. We can't handle this for now - this case will disappear 13645 // when we start sorting the vectors by type. 13646 return SDValue(); 13647 } 13648 } else { 13649 // TODO: Support cases where the length mismatch isn't exactly by a 13650 // factor of 2. 13651 // TODO: Move this check upwards, so that if we have bad type 13652 // mismatches, we don't create any DAG nodes. 13653 return SDValue(); 13654 } 13655 } 13656 13657 // Initialize mask to undef. 13658 SmallVector<int, 8> Mask(ShuffleNumElems, -1); 13659 13660 // Only need to run up to the number of elements actually used, not the 13661 // total number of elements in the shuffle - if we are shuffling a wider 13662 // vector, the high lanes should be set to undef. 13663 for (unsigned i = 0; i != NumElems; ++i) { 13664 if (VectorMask[i] <= 0) 13665 continue; 13666 13667 unsigned ExtIndex = N->getOperand(i).getConstantOperandVal(1); 13668 if (VectorMask[i] == (int)LeftIdx) { 13669 Mask[i] = ExtIndex; 13670 } else if (VectorMask[i] == (int)LeftIdx + 1) { 13671 Mask[i] = Vec2Offset + ExtIndex; 13672 } 13673 } 13674 13675 // The type the input vectors may have changed above. 13676 InVT1 = VecIn1.getValueType(); 13677 13678 // If we already have a VecIn2, it should have the same type as VecIn1. 13679 // If we don't, get an undef/zero vector of the appropriate type. 13680 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(InVT1); 13681 assert(InVT1 == VecIn2.getValueType() && "Unexpected second input type."); 13682 13683 SDValue Shuffle = DAG.getVectorShuffle(InVT1, DL, VecIn1, VecIn2, Mask); 13684 if (ShuffleNumElems > NumElems) 13685 Shuffle = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Shuffle, ZeroIdx); 13686 13687 return Shuffle; 13688 } 13689 13690 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT 13691 // operations. If the types of the vectors we're extracting from allow it, 13692 // turn this into a vector_shuffle node. 13693 SDValue DAGCombiner::reduceBuildVecToShuffle(SDNode *N) { 13694 SDLoc DL(N); 13695 EVT VT = N->getValueType(0); 13696 13697 // Only type-legal BUILD_VECTOR nodes are converted to shuffle nodes. 13698 if (!isTypeLegal(VT)) 13699 return SDValue(); 13700 13701 // May only combine to shuffle after legalize if shuffle is legal. 13702 if (LegalOperations && !TLI.isOperationLegal(ISD::VECTOR_SHUFFLE, VT)) 13703 return SDValue(); 13704 13705 bool UsesZeroVector = false; 13706 unsigned NumElems = N->getNumOperands(); 13707 13708 // Record, for each element of the newly built vector, which input vector 13709 // that element comes from. -1 stands for undef, 0 for the zero vector, 13710 // and positive values for the input vectors. 13711 // VectorMask maps each element to its vector number, and VecIn maps vector 13712 // numbers to their initial SDValues. 13713 13714 SmallVector<int, 8> VectorMask(NumElems, -1); 13715 SmallVector<SDValue, 8> VecIn; 13716 VecIn.push_back(SDValue()); 13717 13718 for (unsigned i = 0; i != NumElems; ++i) { 13719 SDValue Op = N->getOperand(i); 13720 13721 if (Op.isUndef()) 13722 continue; 13723 13724 // See if we can use a blend with a zero vector. 13725 // TODO: Should we generalize this to a blend with an arbitrary constant 13726 // vector? 13727 if (isNullConstant(Op) || isNullFPConstant(Op)) { 13728 UsesZeroVector = true; 13729 VectorMask[i] = 0; 13730 continue; 13731 } 13732 13733 // Not an undef or zero. If the input is something other than an 13734 // EXTRACT_VECTOR_ELT with a constant index, bail out. 13735 if (Op.getOpcode() != ISD::EXTRACT_VECTOR_ELT || 13736 !isa<ConstantSDNode>(Op.getOperand(1))) 13737 return SDValue(); 13738 13739 SDValue ExtractedFromVec = Op.getOperand(0); 13740 13741 // All inputs must have the same element type as the output. 13742 if (VT.getVectorElementType() != 13743 ExtractedFromVec.getValueType().getVectorElementType()) 13744 return SDValue(); 13745 13746 // Have we seen this input vector before? 13747 // The vectors are expected to be tiny (usually 1 or 2 elements), so using 13748 // a map back from SDValues to numbers isn't worth it. 13749 unsigned Idx = std::distance( 13750 VecIn.begin(), std::find(VecIn.begin(), VecIn.end(), ExtractedFromVec)); 13751 if (Idx == VecIn.size()) 13752 VecIn.push_back(ExtractedFromVec); 13753 13754 VectorMask[i] = Idx; 13755 } 13756 13757 // If we didn't find at least one input vector, bail out. 13758 if (VecIn.size() < 2) 13759 return SDValue(); 13760 13761 // TODO: We want to sort the vectors by descending length, so that adjacent 13762 // pairs have similar length, and the longer vector is always first in the 13763 // pair. 13764 13765 // TODO: Should this fire if some of the input vectors has illegal type (like 13766 // it does now), or should we let legalization run its course first? 13767 13768 // Shuffle phase: 13769 // Take pairs of vectors, and shuffle them so that the result has elements 13770 // from these vectors in the correct places. 13771 // For example, given: 13772 // t10: i32 = extract_vector_elt t1, Constant:i64<0> 13773 // t11: i32 = extract_vector_elt t2, Constant:i64<0> 13774 // t12: i32 = extract_vector_elt t3, Constant:i64<0> 13775 // t13: i32 = extract_vector_elt t1, Constant:i64<1> 13776 // t14: v4i32 = BUILD_VECTOR t10, t11, t12, t13 13777 // We will generate: 13778 // t20: v4i32 = vector_shuffle<0,4,u,1> t1, t2 13779 // t21: v4i32 = vector_shuffle<u,u,0,u> t3, undef 13780 SmallVector<SDValue, 4> Shuffles; 13781 for (unsigned In = 0, Len = (VecIn.size() / 2); In < Len; ++In) { 13782 unsigned LeftIdx = 2 * In + 1; 13783 SDValue VecLeft = VecIn[LeftIdx]; 13784 SDValue VecRight = 13785 (LeftIdx + 1) < VecIn.size() ? VecIn[LeftIdx + 1] : SDValue(); 13786 13787 if (SDValue Shuffle = createBuildVecShuffle(DL, N, VectorMask, VecLeft, 13788 VecRight, LeftIdx)) 13789 Shuffles.push_back(Shuffle); 13790 else 13791 return SDValue(); 13792 } 13793 13794 // If we need the zero vector as an "ingredient" in the blend tree, add it 13795 // to the list of shuffles. 13796 if (UsesZeroVector) 13797 Shuffles.push_back(VT.isInteger() ? DAG.getConstant(0, DL, VT) 13798 : DAG.getConstantFP(0.0, DL, VT)); 13799 13800 // If we only have one shuffle, we're done. 13801 if (Shuffles.size() == 1) 13802 return Shuffles[0]; 13803 13804 // Update the vector mask to point to the post-shuffle vectors. 13805 for (int &Vec : VectorMask) 13806 if (Vec == 0) 13807 Vec = Shuffles.size() - 1; 13808 else 13809 Vec = (Vec - 1) / 2; 13810 13811 // More than one shuffle. Generate a binary tree of blends, e.g. if from 13812 // the previous step we got the set of shuffles t10, t11, t12, t13, we will 13813 // generate: 13814 // t10: v8i32 = vector_shuffle<0,8,u,u,u,u,u,u> t1, t2 13815 // t11: v8i32 = vector_shuffle<u,u,0,8,u,u,u,u> t3, t4 13816 // t12: v8i32 = vector_shuffle<u,u,u,u,0,8,u,u> t5, t6 13817 // t13: v8i32 = vector_shuffle<u,u,u,u,u,u,0,8> t7, t8 13818 // t20: v8i32 = vector_shuffle<0,1,10,11,u,u,u,u> t10, t11 13819 // t21: v8i32 = vector_shuffle<u,u,u,u,4,5,14,15> t12, t13 13820 // t30: v8i32 = vector_shuffle<0,1,2,3,12,13,14,15> t20, t21 13821 13822 // Make sure the initial size of the shuffle list is even. 13823 if (Shuffles.size() % 2) 13824 Shuffles.push_back(DAG.getUNDEF(VT)); 13825 13826 for (unsigned CurSize = Shuffles.size(); CurSize > 1; CurSize /= 2) { 13827 if (CurSize % 2) { 13828 Shuffles[CurSize] = DAG.getUNDEF(VT); 13829 CurSize++; 13830 } 13831 for (unsigned In = 0, Len = CurSize / 2; In < Len; ++In) { 13832 int Left = 2 * In; 13833 int Right = 2 * In + 1; 13834 SmallVector<int, 8> Mask(NumElems, -1); 13835 for (unsigned i = 0; i != NumElems; ++i) { 13836 if (VectorMask[i] == Left) { 13837 Mask[i] = i; 13838 VectorMask[i] = In; 13839 } else if (VectorMask[i] == Right) { 13840 Mask[i] = i + NumElems; 13841 VectorMask[i] = In; 13842 } 13843 } 13844 13845 Shuffles[In] = 13846 DAG.getVectorShuffle(VT, DL, Shuffles[Left], Shuffles[Right], Mask); 13847 } 13848 } 13849 13850 return Shuffles[0]; 13851 } 13852 13853 SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) { 13854 EVT VT = N->getValueType(0); 13855 13856 // A vector built entirely of undefs is undef. 13857 if (ISD::allOperandsUndef(N)) 13858 return DAG.getUNDEF(VT); 13859 13860 // Check if we can express BUILD VECTOR via subvector extract. 13861 if (!LegalTypes && (N->getNumOperands() > 1)) { 13862 SDValue Op0 = N->getOperand(0); 13863 auto checkElem = [&](SDValue Op) -> uint64_t { 13864 if ((Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT) && 13865 (Op0.getOperand(0) == Op.getOperand(0))) 13866 if (auto CNode = dyn_cast<ConstantSDNode>(Op.getOperand(1))) 13867 return CNode->getZExtValue(); 13868 return -1; 13869 }; 13870 13871 int Offset = checkElem(Op0); 13872 for (unsigned i = 0; i < N->getNumOperands(); ++i) { 13873 if (Offset + i != checkElem(N->getOperand(i))) { 13874 Offset = -1; 13875 break; 13876 } 13877 } 13878 13879 if ((Offset == 0) && 13880 (Op0.getOperand(0).getValueType() == N->getValueType(0))) 13881 return Op0.getOperand(0); 13882 if ((Offset != -1) && 13883 ((Offset % N->getValueType(0).getVectorNumElements()) == 13884 0)) // IDX must be multiple of output size. 13885 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(N), N->getValueType(0), 13886 Op0.getOperand(0), Op0.getOperand(1)); 13887 } 13888 13889 if (SDValue V = reduceBuildVecExtToExtBuildVec(N)) 13890 return V; 13891 13892 if (SDValue V = reduceBuildVecConvertToConvertBuildVec(N)) 13893 return V; 13894 13895 if (SDValue V = reduceBuildVecToShuffle(N)) 13896 return V; 13897 13898 return SDValue(); 13899 } 13900 13901 static SDValue combineConcatVectorOfScalars(SDNode *N, SelectionDAG &DAG) { 13902 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 13903 EVT OpVT = N->getOperand(0).getValueType(); 13904 13905 // If the operands are legal vectors, leave them alone. 13906 if (TLI.isTypeLegal(OpVT)) 13907 return SDValue(); 13908 13909 SDLoc DL(N); 13910 EVT VT = N->getValueType(0); 13911 SmallVector<SDValue, 8> Ops; 13912 13913 EVT SVT = EVT::getIntegerVT(*DAG.getContext(), OpVT.getSizeInBits()); 13914 SDValue ScalarUndef = DAG.getNode(ISD::UNDEF, DL, SVT); 13915 13916 // Keep track of what we encounter. 13917 bool AnyInteger = false; 13918 bool AnyFP = false; 13919 for (const SDValue &Op : N->ops()) { 13920 if (ISD::BITCAST == Op.getOpcode() && 13921 !Op.getOperand(0).getValueType().isVector()) 13922 Ops.push_back(Op.getOperand(0)); 13923 else if (ISD::UNDEF == Op.getOpcode()) 13924 Ops.push_back(ScalarUndef); 13925 else 13926 return SDValue(); 13927 13928 // Note whether we encounter an integer or floating point scalar. 13929 // If it's neither, bail out, it could be something weird like x86mmx. 13930 EVT LastOpVT = Ops.back().getValueType(); 13931 if (LastOpVT.isFloatingPoint()) 13932 AnyFP = true; 13933 else if (LastOpVT.isInteger()) 13934 AnyInteger = true; 13935 else 13936 return SDValue(); 13937 } 13938 13939 // If any of the operands is a floating point scalar bitcast to a vector, 13940 // use floating point types throughout, and bitcast everything. 13941 // Replace UNDEFs by another scalar UNDEF node, of the final desired type. 13942 if (AnyFP) { 13943 SVT = EVT::getFloatingPointVT(OpVT.getSizeInBits()); 13944 ScalarUndef = DAG.getNode(ISD::UNDEF, DL, SVT); 13945 if (AnyInteger) { 13946 for (SDValue &Op : Ops) { 13947 if (Op.getValueType() == SVT) 13948 continue; 13949 if (Op.isUndef()) 13950 Op = ScalarUndef; 13951 else 13952 Op = DAG.getBitcast(SVT, Op); 13953 } 13954 } 13955 } 13956 13957 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SVT, 13958 VT.getSizeInBits() / SVT.getSizeInBits()); 13959 return DAG.getBitcast(VT, DAG.getBuildVector(VecVT, DL, Ops)); 13960 } 13961 13962 // Check to see if this is a CONCAT_VECTORS of a bunch of EXTRACT_SUBVECTOR 13963 // operations. If so, and if the EXTRACT_SUBVECTOR vector inputs come from at 13964 // most two distinct vectors the same size as the result, attempt to turn this 13965 // into a legal shuffle. 13966 static SDValue combineConcatVectorOfExtracts(SDNode *N, SelectionDAG &DAG) { 13967 EVT VT = N->getValueType(0); 13968 EVT OpVT = N->getOperand(0).getValueType(); 13969 int NumElts = VT.getVectorNumElements(); 13970 int NumOpElts = OpVT.getVectorNumElements(); 13971 13972 SDValue SV0 = DAG.getUNDEF(VT), SV1 = DAG.getUNDEF(VT); 13973 SmallVector<int, 8> Mask; 13974 13975 for (SDValue Op : N->ops()) { 13976 // Peek through any bitcast. 13977 while (Op.getOpcode() == ISD::BITCAST) 13978 Op = Op.getOperand(0); 13979 13980 // UNDEF nodes convert to UNDEF shuffle mask values. 13981 if (Op.isUndef()) { 13982 Mask.append((unsigned)NumOpElts, -1); 13983 continue; 13984 } 13985 13986 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR) 13987 return SDValue(); 13988 13989 // What vector are we extracting the subvector from and at what index? 13990 SDValue ExtVec = Op.getOperand(0); 13991 13992 // We want the EVT of the original extraction to correctly scale the 13993 // extraction index. 13994 EVT ExtVT = ExtVec.getValueType(); 13995 13996 // Peek through any bitcast. 13997 while (ExtVec.getOpcode() == ISD::BITCAST) 13998 ExtVec = ExtVec.getOperand(0); 13999 14000 // UNDEF nodes convert to UNDEF shuffle mask values. 14001 if (ExtVec.isUndef()) { 14002 Mask.append((unsigned)NumOpElts, -1); 14003 continue; 14004 } 14005 14006 if (!isa<ConstantSDNode>(Op.getOperand(1))) 14007 return SDValue(); 14008 int ExtIdx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 14009 14010 // Ensure that we are extracting a subvector from a vector the same 14011 // size as the result. 14012 if (ExtVT.getSizeInBits() != VT.getSizeInBits()) 14013 return SDValue(); 14014 14015 // Scale the subvector index to account for any bitcast. 14016 int NumExtElts = ExtVT.getVectorNumElements(); 14017 if (0 == (NumExtElts % NumElts)) 14018 ExtIdx /= (NumExtElts / NumElts); 14019 else if (0 == (NumElts % NumExtElts)) 14020 ExtIdx *= (NumElts / NumExtElts); 14021 else 14022 return SDValue(); 14023 14024 // At most we can reference 2 inputs in the final shuffle. 14025 if (SV0.isUndef() || SV0 == ExtVec) { 14026 SV0 = ExtVec; 14027 for (int i = 0; i != NumOpElts; ++i) 14028 Mask.push_back(i + ExtIdx); 14029 } else if (SV1.isUndef() || SV1 == ExtVec) { 14030 SV1 = ExtVec; 14031 for (int i = 0; i != NumOpElts; ++i) 14032 Mask.push_back(i + ExtIdx + NumElts); 14033 } else { 14034 return SDValue(); 14035 } 14036 } 14037 14038 if (!DAG.getTargetLoweringInfo().isShuffleMaskLegal(Mask, VT)) 14039 return SDValue(); 14040 14041 return DAG.getVectorShuffle(VT, SDLoc(N), DAG.getBitcast(VT, SV0), 14042 DAG.getBitcast(VT, SV1), Mask); 14043 } 14044 14045 SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) { 14046 // If we only have one input vector, we don't need to do any concatenation. 14047 if (N->getNumOperands() == 1) 14048 return N->getOperand(0); 14049 14050 // Check if all of the operands are undefs. 14051 EVT VT = N->getValueType(0); 14052 if (ISD::allOperandsUndef(N)) 14053 return DAG.getUNDEF(VT); 14054 14055 // Optimize concat_vectors where all but the first of the vectors are undef. 14056 if (std::all_of(std::next(N->op_begin()), N->op_end(), [](const SDValue &Op) { 14057 return Op.isUndef(); 14058 })) { 14059 SDValue In = N->getOperand(0); 14060 assert(In.getValueType().isVector() && "Must concat vectors"); 14061 14062 // Transform: concat_vectors(scalar, undef) -> scalar_to_vector(sclr). 14063 if (In->getOpcode() == ISD::BITCAST && 14064 !In->getOperand(0)->getValueType(0).isVector()) { 14065 SDValue Scalar = In->getOperand(0); 14066 14067 // If the bitcast type isn't legal, it might be a trunc of a legal type; 14068 // look through the trunc so we can still do the transform: 14069 // concat_vectors(trunc(scalar), undef) -> scalar_to_vector(scalar) 14070 if (Scalar->getOpcode() == ISD::TRUNCATE && 14071 !TLI.isTypeLegal(Scalar.getValueType()) && 14072 TLI.isTypeLegal(Scalar->getOperand(0).getValueType())) 14073 Scalar = Scalar->getOperand(0); 14074 14075 EVT SclTy = Scalar->getValueType(0); 14076 14077 if (!SclTy.isFloatingPoint() && !SclTy.isInteger()) 14078 return SDValue(); 14079 14080 unsigned VNTNumElms = VT.getSizeInBits() / SclTy.getSizeInBits(); 14081 if (VNTNumElms < 2) 14082 return SDValue(); 14083 14084 EVT NVT = EVT::getVectorVT(*DAG.getContext(), SclTy, VNTNumElms); 14085 if (!TLI.isTypeLegal(NVT) || !TLI.isTypeLegal(Scalar.getValueType())) 14086 return SDValue(); 14087 14088 SDValue Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), NVT, Scalar); 14089 return DAG.getBitcast(VT, Res); 14090 } 14091 } 14092 14093 // Fold any combination of BUILD_VECTOR or UNDEF nodes into one BUILD_VECTOR. 14094 // We have already tested above for an UNDEF only concatenation. 14095 // fold (concat_vectors (BUILD_VECTOR A, B, ...), (BUILD_VECTOR C, D, ...)) 14096 // -> (BUILD_VECTOR A, B, ..., C, D, ...) 14097 auto IsBuildVectorOrUndef = [](const SDValue &Op) { 14098 return ISD::UNDEF == Op.getOpcode() || ISD::BUILD_VECTOR == Op.getOpcode(); 14099 }; 14100 if (llvm::all_of(N->ops(), IsBuildVectorOrUndef)) { 14101 SmallVector<SDValue, 8> Opnds; 14102 EVT SVT = VT.getScalarType(); 14103 14104 EVT MinVT = SVT; 14105 if (!SVT.isFloatingPoint()) { 14106 // If BUILD_VECTOR are from built from integer, they may have different 14107 // operand types. Get the smallest type and truncate all operands to it. 14108 bool FoundMinVT = false; 14109 for (const SDValue &Op : N->ops()) 14110 if (ISD::BUILD_VECTOR == Op.getOpcode()) { 14111 EVT OpSVT = Op.getOperand(0)->getValueType(0); 14112 MinVT = (!FoundMinVT || OpSVT.bitsLE(MinVT)) ? OpSVT : MinVT; 14113 FoundMinVT = true; 14114 } 14115 assert(FoundMinVT && "Concat vector type mismatch"); 14116 } 14117 14118 for (const SDValue &Op : N->ops()) { 14119 EVT OpVT = Op.getValueType(); 14120 unsigned NumElts = OpVT.getVectorNumElements(); 14121 14122 if (ISD::UNDEF == Op.getOpcode()) 14123 Opnds.append(NumElts, DAG.getUNDEF(MinVT)); 14124 14125 if (ISD::BUILD_VECTOR == Op.getOpcode()) { 14126 if (SVT.isFloatingPoint()) { 14127 assert(SVT == OpVT.getScalarType() && "Concat vector type mismatch"); 14128 Opnds.append(Op->op_begin(), Op->op_begin() + NumElts); 14129 } else { 14130 for (unsigned i = 0; i != NumElts; ++i) 14131 Opnds.push_back( 14132 DAG.getNode(ISD::TRUNCATE, SDLoc(N), MinVT, Op.getOperand(i))); 14133 } 14134 } 14135 } 14136 14137 assert(VT.getVectorNumElements() == Opnds.size() && 14138 "Concat vector type mismatch"); 14139 return DAG.getBuildVector(VT, SDLoc(N), Opnds); 14140 } 14141 14142 // Fold CONCAT_VECTORS of only bitcast scalars (or undef) to BUILD_VECTOR. 14143 if (SDValue V = combineConcatVectorOfScalars(N, DAG)) 14144 return V; 14145 14146 // Fold CONCAT_VECTORS of EXTRACT_SUBVECTOR (or undef) to VECTOR_SHUFFLE. 14147 if (Level < AfterLegalizeVectorOps && TLI.isTypeLegal(VT)) 14148 if (SDValue V = combineConcatVectorOfExtracts(N, DAG)) 14149 return V; 14150 14151 // Type legalization of vectors and DAG canonicalization of SHUFFLE_VECTOR 14152 // nodes often generate nop CONCAT_VECTOR nodes. 14153 // Scan the CONCAT_VECTOR operands and look for a CONCAT operations that 14154 // place the incoming vectors at the exact same location. 14155 SDValue SingleSource = SDValue(); 14156 unsigned PartNumElem = N->getOperand(0).getValueType().getVectorNumElements(); 14157 14158 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 14159 SDValue Op = N->getOperand(i); 14160 14161 if (Op.isUndef()) 14162 continue; 14163 14164 // Check if this is the identity extract: 14165 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR) 14166 return SDValue(); 14167 14168 // Find the single incoming vector for the extract_subvector. 14169 if (SingleSource.getNode()) { 14170 if (Op.getOperand(0) != SingleSource) 14171 return SDValue(); 14172 } else { 14173 SingleSource = Op.getOperand(0); 14174 14175 // Check the source type is the same as the type of the result. 14176 // If not, this concat may extend the vector, so we can not 14177 // optimize it away. 14178 if (SingleSource.getValueType() != N->getValueType(0)) 14179 return SDValue(); 14180 } 14181 14182 unsigned IdentityIndex = i * PartNumElem; 14183 ConstantSDNode *CS = dyn_cast<ConstantSDNode>(Op.getOperand(1)); 14184 // The extract index must be constant. 14185 if (!CS) 14186 return SDValue(); 14187 14188 // Check that we are reading from the identity index. 14189 if (CS->getZExtValue() != IdentityIndex) 14190 return SDValue(); 14191 } 14192 14193 if (SingleSource.getNode()) 14194 return SingleSource; 14195 14196 return SDValue(); 14197 } 14198 14199 SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode* N) { 14200 EVT NVT = N->getValueType(0); 14201 SDValue V = N->getOperand(0); 14202 14203 // Extract from UNDEF is UNDEF. 14204 if (V.isUndef()) 14205 return DAG.getUNDEF(NVT); 14206 14207 // Combine: 14208 // (extract_subvec (concat V1, V2, ...), i) 14209 // Into: 14210 // Vi if possible 14211 // Only operand 0 is checked as 'concat' assumes all inputs of the same 14212 // type. 14213 if (V->getOpcode() == ISD::CONCAT_VECTORS && 14214 isa<ConstantSDNode>(N->getOperand(1)) && 14215 V->getOperand(0).getValueType() == NVT) { 14216 unsigned Idx = N->getConstantOperandVal(1); 14217 unsigned NumElems = NVT.getVectorNumElements(); 14218 assert((Idx % NumElems) == 0 && 14219 "IDX in concat is not a multiple of the result vector length."); 14220 return V->getOperand(Idx / NumElems); 14221 } 14222 14223 // Skip bitcasting 14224 if (V->getOpcode() == ISD::BITCAST) 14225 V = V.getOperand(0); 14226 14227 if (V->getOpcode() == ISD::INSERT_SUBVECTOR) { 14228 // Handle only simple case where vector being inserted and vector 14229 // being extracted are of same size. 14230 EVT SmallVT = V->getOperand(1).getValueType(); 14231 if (!NVT.bitsEq(SmallVT)) 14232 return SDValue(); 14233 14234 // Only handle cases where both indexes are constants. 14235 ConstantSDNode *ExtIdx = dyn_cast<ConstantSDNode>(N->getOperand(1)); 14236 ConstantSDNode *InsIdx = dyn_cast<ConstantSDNode>(V->getOperand(2)); 14237 14238 if (InsIdx && ExtIdx) { 14239 // Combine: 14240 // (extract_subvec (insert_subvec V1, V2, InsIdx), ExtIdx) 14241 // Into: 14242 // indices are equal or bit offsets are equal => V1 14243 // otherwise => (extract_subvec V1, ExtIdx) 14244 if (InsIdx->getZExtValue() * SmallVT.getScalarSizeInBits() == 14245 ExtIdx->getZExtValue() * NVT.getScalarSizeInBits()) 14246 return DAG.getBitcast(NVT, V->getOperand(1)); 14247 return DAG.getNode( 14248 ISD::EXTRACT_SUBVECTOR, SDLoc(N), NVT, 14249 DAG.getBitcast(N->getOperand(0).getValueType(), V->getOperand(0)), 14250 N->getOperand(1)); 14251 } 14252 } 14253 14254 return SDValue(); 14255 } 14256 14257 static SDValue simplifyShuffleOperandRecursively(SmallBitVector &UsedElements, 14258 SDValue V, SelectionDAG &DAG) { 14259 SDLoc DL(V); 14260 EVT VT = V.getValueType(); 14261 14262 switch (V.getOpcode()) { 14263 default: 14264 return V; 14265 14266 case ISD::CONCAT_VECTORS: { 14267 EVT OpVT = V->getOperand(0).getValueType(); 14268 int OpSize = OpVT.getVectorNumElements(); 14269 SmallBitVector OpUsedElements(OpSize, false); 14270 bool FoundSimplification = false; 14271 SmallVector<SDValue, 4> NewOps; 14272 NewOps.reserve(V->getNumOperands()); 14273 for (int i = 0, NumOps = V->getNumOperands(); i < NumOps; ++i) { 14274 SDValue Op = V->getOperand(i); 14275 bool OpUsed = false; 14276 for (int j = 0; j < OpSize; ++j) 14277 if (UsedElements[i * OpSize + j]) { 14278 OpUsedElements[j] = true; 14279 OpUsed = true; 14280 } 14281 NewOps.push_back( 14282 OpUsed ? simplifyShuffleOperandRecursively(OpUsedElements, Op, DAG) 14283 : DAG.getUNDEF(OpVT)); 14284 FoundSimplification |= Op == NewOps.back(); 14285 OpUsedElements.reset(); 14286 } 14287 if (FoundSimplification) 14288 V = DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, NewOps); 14289 return V; 14290 } 14291 14292 case ISD::INSERT_SUBVECTOR: { 14293 SDValue BaseV = V->getOperand(0); 14294 SDValue SubV = V->getOperand(1); 14295 auto *IdxN = dyn_cast<ConstantSDNode>(V->getOperand(2)); 14296 if (!IdxN) 14297 return V; 14298 14299 int SubSize = SubV.getValueType().getVectorNumElements(); 14300 int Idx = IdxN->getZExtValue(); 14301 bool SubVectorUsed = false; 14302 SmallBitVector SubUsedElements(SubSize, false); 14303 for (int i = 0; i < SubSize; ++i) 14304 if (UsedElements[i + Idx]) { 14305 SubVectorUsed = true; 14306 SubUsedElements[i] = true; 14307 UsedElements[i + Idx] = false; 14308 } 14309 14310 // Now recurse on both the base and sub vectors. 14311 SDValue SimplifiedSubV = 14312 SubVectorUsed 14313 ? simplifyShuffleOperandRecursively(SubUsedElements, SubV, DAG) 14314 : DAG.getUNDEF(SubV.getValueType()); 14315 SDValue SimplifiedBaseV = simplifyShuffleOperandRecursively(UsedElements, BaseV, DAG); 14316 if (SimplifiedSubV != SubV || SimplifiedBaseV != BaseV) 14317 V = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VT, 14318 SimplifiedBaseV, SimplifiedSubV, V->getOperand(2)); 14319 return V; 14320 } 14321 } 14322 } 14323 14324 static SDValue simplifyShuffleOperands(ShuffleVectorSDNode *SVN, SDValue N0, 14325 SDValue N1, SelectionDAG &DAG) { 14326 EVT VT = SVN->getValueType(0); 14327 int NumElts = VT.getVectorNumElements(); 14328 SmallBitVector N0UsedElements(NumElts, false), N1UsedElements(NumElts, false); 14329 for (int M : SVN->getMask()) 14330 if (M >= 0 && M < NumElts) 14331 N0UsedElements[M] = true; 14332 else if (M >= NumElts) 14333 N1UsedElements[M - NumElts] = true; 14334 14335 SDValue S0 = simplifyShuffleOperandRecursively(N0UsedElements, N0, DAG); 14336 SDValue S1 = simplifyShuffleOperandRecursively(N1UsedElements, N1, DAG); 14337 if (S0 == N0 && S1 == N1) 14338 return SDValue(); 14339 14340 return DAG.getVectorShuffle(VT, SDLoc(SVN), S0, S1, SVN->getMask()); 14341 } 14342 14343 // Tries to turn a shuffle of two CONCAT_VECTORS into a single concat, 14344 // or turn a shuffle of a single concat into simpler shuffle then concat. 14345 static SDValue partitionShuffleOfConcats(SDNode *N, SelectionDAG &DAG) { 14346 EVT VT = N->getValueType(0); 14347 unsigned NumElts = VT.getVectorNumElements(); 14348 14349 SDValue N0 = N->getOperand(0); 14350 SDValue N1 = N->getOperand(1); 14351 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); 14352 14353 SmallVector<SDValue, 4> Ops; 14354 EVT ConcatVT = N0.getOperand(0).getValueType(); 14355 unsigned NumElemsPerConcat = ConcatVT.getVectorNumElements(); 14356 unsigned NumConcats = NumElts / NumElemsPerConcat; 14357 14358 // Special case: shuffle(concat(A,B)) can be more efficiently represented 14359 // as concat(shuffle(A,B),UNDEF) if the shuffle doesn't set any of the high 14360 // half vector elements. 14361 if (NumElemsPerConcat * 2 == NumElts && N1.isUndef() && 14362 std::all_of(SVN->getMask().begin() + NumElemsPerConcat, 14363 SVN->getMask().end(), [](int i) { return i == -1; })) { 14364 N0 = DAG.getVectorShuffle(ConcatVT, SDLoc(N), N0.getOperand(0), N0.getOperand(1), 14365 makeArrayRef(SVN->getMask().begin(), NumElemsPerConcat)); 14366 N1 = DAG.getUNDEF(ConcatVT); 14367 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, N0, N1); 14368 } 14369 14370 // Look at every vector that's inserted. We're looking for exact 14371 // subvector-sized copies from a concatenated vector 14372 for (unsigned I = 0; I != NumConcats; ++I) { 14373 // Make sure we're dealing with a copy. 14374 unsigned Begin = I * NumElemsPerConcat; 14375 bool AllUndef = true, NoUndef = true; 14376 for (unsigned J = Begin; J != Begin + NumElemsPerConcat; ++J) { 14377 if (SVN->getMaskElt(J) >= 0) 14378 AllUndef = false; 14379 else 14380 NoUndef = false; 14381 } 14382 14383 if (NoUndef) { 14384 if (SVN->getMaskElt(Begin) % NumElemsPerConcat != 0) 14385 return SDValue(); 14386 14387 for (unsigned J = 1; J != NumElemsPerConcat; ++J) 14388 if (SVN->getMaskElt(Begin + J - 1) + 1 != SVN->getMaskElt(Begin + J)) 14389 return SDValue(); 14390 14391 unsigned FirstElt = SVN->getMaskElt(Begin) / NumElemsPerConcat; 14392 if (FirstElt < N0.getNumOperands()) 14393 Ops.push_back(N0.getOperand(FirstElt)); 14394 else 14395 Ops.push_back(N1.getOperand(FirstElt - N0.getNumOperands())); 14396 14397 } else if (AllUndef) { 14398 Ops.push_back(DAG.getUNDEF(N0.getOperand(0).getValueType())); 14399 } else { // Mixed with general masks and undefs, can't do optimization. 14400 return SDValue(); 14401 } 14402 } 14403 14404 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, Ops); 14405 } 14406 14407 // Attempt to combine a shuffle of 2 inputs of 'scalar sources' - 14408 // BUILD_VECTOR or SCALAR_TO_VECTOR into a single BUILD_VECTOR. 14409 // 14410 // SHUFFLE(BUILD_VECTOR(), BUILD_VECTOR()) -> BUILD_VECTOR() is always 14411 // a simplification in some sense, but it isn't appropriate in general: some 14412 // BUILD_VECTORs are substantially cheaper than others. The general case 14413 // of a BUILD_VECTOR requires inserting each element individually (or 14414 // performing the equivalent in a temporary stack variable). A BUILD_VECTOR of 14415 // all constants is a single constant pool load. A BUILD_VECTOR where each 14416 // element is identical is a splat. A BUILD_VECTOR where most of the operands 14417 // are undef lowers to a small number of element insertions. 14418 // 14419 // To deal with this, we currently use a bunch of mostly arbitrary heuristics. 14420 // We don't fold shuffles where one side is a non-zero constant, and we don't 14421 // fold shuffles if the resulting BUILD_VECTOR would have duplicate 14422 // non-constant operands. This seems to work out reasonably well in practice. 14423 static SDValue combineShuffleOfScalars(ShuffleVectorSDNode *SVN, 14424 SelectionDAG &DAG, 14425 const TargetLowering &TLI) { 14426 EVT VT = SVN->getValueType(0); 14427 unsigned NumElts = VT.getVectorNumElements(); 14428 SDValue N0 = SVN->getOperand(0); 14429 SDValue N1 = SVN->getOperand(1); 14430 14431 if (!N0->hasOneUse() || !N1->hasOneUse()) 14432 return SDValue(); 14433 // If only one of N1,N2 is constant, bail out if it is not ALL_ZEROS as 14434 // discussed above. 14435 if (!N1.isUndef()) { 14436 bool N0AnyConst = isAnyConstantBuildVector(N0.getNode()); 14437 bool N1AnyConst = isAnyConstantBuildVector(N1.getNode()); 14438 if (N0AnyConst && !N1AnyConst && !ISD::isBuildVectorAllZeros(N0.getNode())) 14439 return SDValue(); 14440 if (!N0AnyConst && N1AnyConst && !ISD::isBuildVectorAllZeros(N1.getNode())) 14441 return SDValue(); 14442 } 14443 14444 SmallVector<SDValue, 8> Ops; 14445 SmallSet<SDValue, 16> DuplicateOps; 14446 for (int M : SVN->getMask()) { 14447 SDValue Op = DAG.getUNDEF(VT.getScalarType()); 14448 if (M >= 0) { 14449 int Idx = M < (int)NumElts ? M : M - NumElts; 14450 SDValue &S = (M < (int)NumElts ? N0 : N1); 14451 if (S.getOpcode() == ISD::BUILD_VECTOR) { 14452 Op = S.getOperand(Idx); 14453 } else if (S.getOpcode() == ISD::SCALAR_TO_VECTOR) { 14454 if (Idx == 0) 14455 Op = S.getOperand(0); 14456 } else { 14457 // Operand can't be combined - bail out. 14458 return SDValue(); 14459 } 14460 } 14461 14462 // Don't duplicate a non-constant BUILD_VECTOR operand; semantically, this is 14463 // fine, but it's likely to generate low-quality code if the target can't 14464 // reconstruct an appropriate shuffle. 14465 if (!Op.isUndef() && !isa<ConstantSDNode>(Op) && !isa<ConstantFPSDNode>(Op)) 14466 if (!DuplicateOps.insert(Op).second) 14467 return SDValue(); 14468 14469 Ops.push_back(Op); 14470 } 14471 // BUILD_VECTOR requires all inputs to be of the same type, find the 14472 // maximum type and extend them all. 14473 EVT SVT = VT.getScalarType(); 14474 if (SVT.isInteger()) 14475 for (SDValue &Op : Ops) 14476 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT); 14477 if (SVT != VT.getScalarType()) 14478 for (SDValue &Op : Ops) 14479 Op = TLI.isZExtFree(Op.getValueType(), SVT) 14480 ? DAG.getZExtOrTrunc(Op, SDLoc(SVN), SVT) 14481 : DAG.getSExtOrTrunc(Op, SDLoc(SVN), SVT); 14482 return DAG.getBuildVector(VT, SDLoc(SVN), Ops); 14483 } 14484 14485 // Match shuffles that can be converted to any_vector_extend_in_reg. 14486 // This is often generated during legalization. 14487 // e.g. v4i32 <0,u,1,u> -> (v2i64 any_vector_extend_in_reg(v4i32 src)) 14488 // TODO Add support for ZERO_EXTEND_VECTOR_INREG when we have a test case. 14489 SDValue combineShuffleToVectorExtend(ShuffleVectorSDNode *SVN, 14490 SelectionDAG &DAG, 14491 const TargetLowering &TLI, 14492 bool LegalOperations) { 14493 EVT VT = SVN->getValueType(0); 14494 bool IsBigEndian = DAG.getDataLayout().isBigEndian(); 14495 14496 // TODO Add support for big-endian when we have a test case. 14497 if (!VT.isInteger() || IsBigEndian) 14498 return SDValue(); 14499 14500 unsigned NumElts = VT.getVectorNumElements(); 14501 unsigned EltSizeInBits = VT.getScalarSizeInBits(); 14502 ArrayRef<int> Mask = SVN->getMask(); 14503 SDValue N0 = SVN->getOperand(0); 14504 14505 // shuffle<0,-1,1,-1> == (v2i64 anyextend_vector_inreg(v4i32)) 14506 auto isAnyExtend = [&Mask, &NumElts](unsigned Scale) { 14507 for (unsigned i = 0; i != NumElts; ++i) { 14508 if (Mask[i] < 0) 14509 continue; 14510 if ((i % Scale) == 0 && Mask[i] == (int)(i / Scale)) 14511 continue; 14512 return false; 14513 } 14514 return true; 14515 }; 14516 14517 // Attempt to match a '*_extend_vector_inreg' shuffle, we just search for 14518 // power-of-2 extensions as they are the most likely. 14519 for (unsigned Scale = 2; Scale < NumElts; Scale *= 2) { 14520 if (!isAnyExtend(Scale)) 14521 continue; 14522 14523 EVT OutSVT = EVT::getIntegerVT(*DAG.getContext(), EltSizeInBits * Scale); 14524 EVT OutVT = EVT::getVectorVT(*DAG.getContext(), OutSVT, NumElts / Scale); 14525 if (!LegalOperations || 14526 TLI.isOperationLegalOrCustom(ISD::ANY_EXTEND_VECTOR_INREG, OutVT)) 14527 return DAG.getBitcast(VT, 14528 DAG.getAnyExtendVectorInReg(N0, SDLoc(SVN), OutVT)); 14529 } 14530 14531 return SDValue(); 14532 } 14533 14534 // Detect 'truncate_vector_inreg' style shuffles that pack the lower parts of 14535 // each source element of a large type into the lowest elements of a smaller 14536 // destination type. This is often generated during legalization. 14537 // If the source node itself was a '*_extend_vector_inreg' node then we should 14538 // then be able to remove it. 14539 SDValue combineTruncationShuffle(ShuffleVectorSDNode *SVN, SelectionDAG &DAG) { 14540 EVT VT = SVN->getValueType(0); 14541 bool IsBigEndian = DAG.getDataLayout().isBigEndian(); 14542 14543 // TODO Add support for big-endian when we have a test case. 14544 if (!VT.isInteger() || IsBigEndian) 14545 return SDValue(); 14546 14547 SDValue N0 = SVN->getOperand(0); 14548 while (N0.getOpcode() == ISD::BITCAST) 14549 N0 = N0.getOperand(0); 14550 14551 unsigned Opcode = N0.getOpcode(); 14552 if (Opcode != ISD::ANY_EXTEND_VECTOR_INREG && 14553 Opcode != ISD::SIGN_EXTEND_VECTOR_INREG && 14554 Opcode != ISD::ZERO_EXTEND_VECTOR_INREG) 14555 return SDValue(); 14556 14557 SDValue N00 = N0.getOperand(0); 14558 ArrayRef<int> Mask = SVN->getMask(); 14559 unsigned NumElts = VT.getVectorNumElements(); 14560 unsigned EltSizeInBits = VT.getScalarSizeInBits(); 14561 unsigned ExtSrcSizeInBits = N00.getScalarValueSizeInBits(); 14562 14563 // (v4i32 truncate_vector_inreg(v2i64)) == shuffle<0,2-1,-1> 14564 // (v8i16 truncate_vector_inreg(v4i32)) == shuffle<0,2,4,6,-1,-1,-1,-1> 14565 // (v8i16 truncate_vector_inreg(v2i64)) == shuffle<0,4,-1,-1,-1,-1,-1,-1> 14566 auto isTruncate = [&Mask, &NumElts](unsigned Scale) { 14567 for (unsigned i = 0; i != NumElts; ++i) { 14568 if (Mask[i] < 0) 14569 continue; 14570 if ((i * Scale) < NumElts && Mask[i] == (int)(i * Scale)) 14571 continue; 14572 return false; 14573 } 14574 return true; 14575 }; 14576 14577 // At the moment we just handle the case where we've truncated back to the 14578 // same size as before the extension. 14579 // TODO: handle more extension/truncation cases as cases arise. 14580 if (EltSizeInBits != ExtSrcSizeInBits) 14581 return SDValue(); 14582 14583 // Attempt to match a 'truncate_vector_inreg' shuffle, we just search for 14584 // power-of-2 truncations as they are the most likely. 14585 for (unsigned Scale = 2; Scale < NumElts; Scale *= 2) 14586 if (isTruncate(Scale)) 14587 return DAG.getBitcast(VT, N00); 14588 14589 return SDValue(); 14590 } 14591 14592 SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) { 14593 EVT VT = N->getValueType(0); 14594 unsigned NumElts = VT.getVectorNumElements(); 14595 14596 SDValue N0 = N->getOperand(0); 14597 SDValue N1 = N->getOperand(1); 14598 14599 assert(N0.getValueType() == VT && "Vector shuffle must be normalized in DAG"); 14600 14601 // Canonicalize shuffle undef, undef -> undef 14602 if (N0.isUndef() && N1.isUndef()) 14603 return DAG.getUNDEF(VT); 14604 14605 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); 14606 14607 // Canonicalize shuffle v, v -> v, undef 14608 if (N0 == N1) { 14609 SmallVector<int, 8> NewMask; 14610 for (unsigned i = 0; i != NumElts; ++i) { 14611 int Idx = SVN->getMaskElt(i); 14612 if (Idx >= (int)NumElts) Idx -= NumElts; 14613 NewMask.push_back(Idx); 14614 } 14615 return DAG.getVectorShuffle(VT, SDLoc(N), N0, DAG.getUNDEF(VT), NewMask); 14616 } 14617 14618 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask. 14619 if (N0.isUndef()) 14620 return DAG.getCommutedVectorShuffle(*SVN); 14621 14622 // Remove references to rhs if it is undef 14623 if (N1.isUndef()) { 14624 bool Changed = false; 14625 SmallVector<int, 8> NewMask; 14626 for (unsigned i = 0; i != NumElts; ++i) { 14627 int Idx = SVN->getMaskElt(i); 14628 if (Idx >= (int)NumElts) { 14629 Idx = -1; 14630 Changed = true; 14631 } 14632 NewMask.push_back(Idx); 14633 } 14634 if (Changed) 14635 return DAG.getVectorShuffle(VT, SDLoc(N), N0, N1, NewMask); 14636 } 14637 14638 // If it is a splat, check if the argument vector is another splat or a 14639 // build_vector. 14640 if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) { 14641 SDNode *V = N0.getNode(); 14642 14643 // If this is a bit convert that changes the element type of the vector but 14644 // not the number of vector elements, look through it. Be careful not to 14645 // look though conversions that change things like v4f32 to v2f64. 14646 if (V->getOpcode() == ISD::BITCAST) { 14647 SDValue ConvInput = V->getOperand(0); 14648 if (ConvInput.getValueType().isVector() && 14649 ConvInput.getValueType().getVectorNumElements() == NumElts) 14650 V = ConvInput.getNode(); 14651 } 14652 14653 if (V->getOpcode() == ISD::BUILD_VECTOR) { 14654 assert(V->getNumOperands() == NumElts && 14655 "BUILD_VECTOR has wrong number of operands"); 14656 SDValue Base; 14657 bool AllSame = true; 14658 for (unsigned i = 0; i != NumElts; ++i) { 14659 if (!V->getOperand(i).isUndef()) { 14660 Base = V->getOperand(i); 14661 break; 14662 } 14663 } 14664 // Splat of <u, u, u, u>, return <u, u, u, u> 14665 if (!Base.getNode()) 14666 return N0; 14667 for (unsigned i = 0; i != NumElts; ++i) { 14668 if (V->getOperand(i) != Base) { 14669 AllSame = false; 14670 break; 14671 } 14672 } 14673 // Splat of <x, x, x, x>, return <x, x, x, x> 14674 if (AllSame) 14675 return N0; 14676 14677 // Canonicalize any other splat as a build_vector. 14678 const SDValue &Splatted = V->getOperand(SVN->getSplatIndex()); 14679 SmallVector<SDValue, 8> Ops(NumElts, Splatted); 14680 SDValue NewBV = DAG.getBuildVector(V->getValueType(0), SDLoc(N), Ops); 14681 14682 // We may have jumped through bitcasts, so the type of the 14683 // BUILD_VECTOR may not match the type of the shuffle. 14684 if (V->getValueType(0) != VT) 14685 NewBV = DAG.getBitcast(VT, NewBV); 14686 return NewBV; 14687 } 14688 } 14689 14690 // There are various patterns used to build up a vector from smaller vectors, 14691 // subvectors, or elements. Scan chains of these and replace unused insertions 14692 // or components with undef. 14693 if (SDValue S = simplifyShuffleOperands(SVN, N0, N1, DAG)) 14694 return S; 14695 14696 // Match shuffles that can be converted to any_vector_extend_in_reg. 14697 if (SDValue V = combineShuffleToVectorExtend(SVN, DAG, TLI, LegalOperations)) 14698 return V; 14699 14700 // Combine "truncate_vector_in_reg" style shuffles. 14701 if (SDValue V = combineTruncationShuffle(SVN, DAG)) 14702 return V; 14703 14704 if (N0.getOpcode() == ISD::CONCAT_VECTORS && 14705 Level < AfterLegalizeVectorOps && 14706 (N1.isUndef() || 14707 (N1.getOpcode() == ISD::CONCAT_VECTORS && 14708 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()))) { 14709 if (SDValue V = partitionShuffleOfConcats(N, DAG)) 14710 return V; 14711 } 14712 14713 // Attempt to combine a shuffle of 2 inputs of 'scalar sources' - 14714 // BUILD_VECTOR or SCALAR_TO_VECTOR into a single BUILD_VECTOR. 14715 if (Level < AfterLegalizeVectorOps && TLI.isTypeLegal(VT)) 14716 if (SDValue Res = combineShuffleOfScalars(SVN, DAG, TLI)) 14717 return Res; 14718 14719 // If this shuffle only has a single input that is a bitcasted shuffle, 14720 // attempt to merge the 2 shuffles and suitably bitcast the inputs/output 14721 // back to their original types. 14722 if (N0.getOpcode() == ISD::BITCAST && N0.hasOneUse() && 14723 N1.isUndef() && Level < AfterLegalizeVectorOps && 14724 TLI.isTypeLegal(VT)) { 14725 14726 // Peek through the bitcast only if there is one user. 14727 SDValue BC0 = N0; 14728 while (BC0.getOpcode() == ISD::BITCAST) { 14729 if (!BC0.hasOneUse()) 14730 break; 14731 BC0 = BC0.getOperand(0); 14732 } 14733 14734 auto ScaleShuffleMask = [](ArrayRef<int> Mask, int Scale) { 14735 if (Scale == 1) 14736 return SmallVector<int, 8>(Mask.begin(), Mask.end()); 14737 14738 SmallVector<int, 8> NewMask; 14739 for (int M : Mask) 14740 for (int s = 0; s != Scale; ++s) 14741 NewMask.push_back(M < 0 ? -1 : Scale * M + s); 14742 return NewMask; 14743 }; 14744 14745 if (BC0.getOpcode() == ISD::VECTOR_SHUFFLE && BC0.hasOneUse()) { 14746 EVT SVT = VT.getScalarType(); 14747 EVT InnerVT = BC0->getValueType(0); 14748 EVT InnerSVT = InnerVT.getScalarType(); 14749 14750 // Determine which shuffle works with the smaller scalar type. 14751 EVT ScaleVT = SVT.bitsLT(InnerSVT) ? VT : InnerVT; 14752 EVT ScaleSVT = ScaleVT.getScalarType(); 14753 14754 if (TLI.isTypeLegal(ScaleVT) && 14755 0 == (InnerSVT.getSizeInBits() % ScaleSVT.getSizeInBits()) && 14756 0 == (SVT.getSizeInBits() % ScaleSVT.getSizeInBits())) { 14757 14758 int InnerScale = InnerSVT.getSizeInBits() / ScaleSVT.getSizeInBits(); 14759 int OuterScale = SVT.getSizeInBits() / ScaleSVT.getSizeInBits(); 14760 14761 // Scale the shuffle masks to the smaller scalar type. 14762 ShuffleVectorSDNode *InnerSVN = cast<ShuffleVectorSDNode>(BC0); 14763 SmallVector<int, 8> InnerMask = 14764 ScaleShuffleMask(InnerSVN->getMask(), InnerScale); 14765 SmallVector<int, 8> OuterMask = 14766 ScaleShuffleMask(SVN->getMask(), OuterScale); 14767 14768 // Merge the shuffle masks. 14769 SmallVector<int, 8> NewMask; 14770 for (int M : OuterMask) 14771 NewMask.push_back(M < 0 ? -1 : InnerMask[M]); 14772 14773 // Test for shuffle mask legality over both commutations. 14774 SDValue SV0 = BC0->getOperand(0); 14775 SDValue SV1 = BC0->getOperand(1); 14776 bool LegalMask = TLI.isShuffleMaskLegal(NewMask, ScaleVT); 14777 if (!LegalMask) { 14778 std::swap(SV0, SV1); 14779 ShuffleVectorSDNode::commuteMask(NewMask); 14780 LegalMask = TLI.isShuffleMaskLegal(NewMask, ScaleVT); 14781 } 14782 14783 if (LegalMask) { 14784 SV0 = DAG.getBitcast(ScaleVT, SV0); 14785 SV1 = DAG.getBitcast(ScaleVT, SV1); 14786 return DAG.getBitcast( 14787 VT, DAG.getVectorShuffle(ScaleVT, SDLoc(N), SV0, SV1, NewMask)); 14788 } 14789 } 14790 } 14791 } 14792 14793 // Canonicalize shuffles according to rules: 14794 // shuffle(A, shuffle(A, B)) -> shuffle(shuffle(A,B), A) 14795 // shuffle(B, shuffle(A, B)) -> shuffle(shuffle(A,B), B) 14796 // shuffle(B, shuffle(A, Undef)) -> shuffle(shuffle(A, Undef), B) 14797 if (N1.getOpcode() == ISD::VECTOR_SHUFFLE && 14798 N0.getOpcode() != ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG && 14799 TLI.isTypeLegal(VT)) { 14800 // The incoming shuffle must be of the same type as the result of the 14801 // current shuffle. 14802 assert(N1->getOperand(0).getValueType() == VT && 14803 "Shuffle types don't match"); 14804 14805 SDValue SV0 = N1->getOperand(0); 14806 SDValue SV1 = N1->getOperand(1); 14807 bool HasSameOp0 = N0 == SV0; 14808 bool IsSV1Undef = SV1.isUndef(); 14809 if (HasSameOp0 || IsSV1Undef || N0 == SV1) 14810 // Commute the operands of this shuffle so that next rule 14811 // will trigger. 14812 return DAG.getCommutedVectorShuffle(*SVN); 14813 } 14814 14815 // Try to fold according to rules: 14816 // shuffle(shuffle(A, B, M0), C, M1) -> shuffle(A, B, M2) 14817 // shuffle(shuffle(A, B, M0), C, M1) -> shuffle(A, C, M2) 14818 // shuffle(shuffle(A, B, M0), C, M1) -> shuffle(B, C, M2) 14819 // Don't try to fold shuffles with illegal type. 14820 // Only fold if this shuffle is the only user of the other shuffle. 14821 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && N->isOnlyUserOf(N0.getNode()) && 14822 Level < AfterLegalizeDAG && TLI.isTypeLegal(VT)) { 14823 ShuffleVectorSDNode *OtherSV = cast<ShuffleVectorSDNode>(N0); 14824 14825 // Don't try to fold splats; they're likely to simplify somehow, or they 14826 // might be free. 14827 if (OtherSV->isSplat()) 14828 return SDValue(); 14829 14830 // The incoming shuffle must be of the same type as the result of the 14831 // current shuffle. 14832 assert(OtherSV->getOperand(0).getValueType() == VT && 14833 "Shuffle types don't match"); 14834 14835 SDValue SV0, SV1; 14836 SmallVector<int, 4> Mask; 14837 // Compute the combined shuffle mask for a shuffle with SV0 as the first 14838 // operand, and SV1 as the second operand. 14839 for (unsigned i = 0; i != NumElts; ++i) { 14840 int Idx = SVN->getMaskElt(i); 14841 if (Idx < 0) { 14842 // Propagate Undef. 14843 Mask.push_back(Idx); 14844 continue; 14845 } 14846 14847 SDValue CurrentVec; 14848 if (Idx < (int)NumElts) { 14849 // This shuffle index refers to the inner shuffle N0. Lookup the inner 14850 // shuffle mask to identify which vector is actually referenced. 14851 Idx = OtherSV->getMaskElt(Idx); 14852 if (Idx < 0) { 14853 // Propagate Undef. 14854 Mask.push_back(Idx); 14855 continue; 14856 } 14857 14858 CurrentVec = (Idx < (int) NumElts) ? OtherSV->getOperand(0) 14859 : OtherSV->getOperand(1); 14860 } else { 14861 // This shuffle index references an element within N1. 14862 CurrentVec = N1; 14863 } 14864 14865 // Simple case where 'CurrentVec' is UNDEF. 14866 if (CurrentVec.isUndef()) { 14867 Mask.push_back(-1); 14868 continue; 14869 } 14870 14871 // Canonicalize the shuffle index. We don't know yet if CurrentVec 14872 // will be the first or second operand of the combined shuffle. 14873 Idx = Idx % NumElts; 14874 if (!SV0.getNode() || SV0 == CurrentVec) { 14875 // Ok. CurrentVec is the left hand side. 14876 // Update the mask accordingly. 14877 SV0 = CurrentVec; 14878 Mask.push_back(Idx); 14879 continue; 14880 } 14881 14882 // Bail out if we cannot convert the shuffle pair into a single shuffle. 14883 if (SV1.getNode() && SV1 != CurrentVec) 14884 return SDValue(); 14885 14886 // Ok. CurrentVec is the right hand side. 14887 // Update the mask accordingly. 14888 SV1 = CurrentVec; 14889 Mask.push_back(Idx + NumElts); 14890 } 14891 14892 // Check if all indices in Mask are Undef. In case, propagate Undef. 14893 bool isUndefMask = true; 14894 for (unsigned i = 0; i != NumElts && isUndefMask; ++i) 14895 isUndefMask &= Mask[i] < 0; 14896 14897 if (isUndefMask) 14898 return DAG.getUNDEF(VT); 14899 14900 if (!SV0.getNode()) 14901 SV0 = DAG.getUNDEF(VT); 14902 if (!SV1.getNode()) 14903 SV1 = DAG.getUNDEF(VT); 14904 14905 // Avoid introducing shuffles with illegal mask. 14906 if (!TLI.isShuffleMaskLegal(Mask, VT)) { 14907 ShuffleVectorSDNode::commuteMask(Mask); 14908 14909 if (!TLI.isShuffleMaskLegal(Mask, VT)) 14910 return SDValue(); 14911 14912 // shuffle(shuffle(A, B, M0), C, M1) -> shuffle(B, A, M2) 14913 // shuffle(shuffle(A, B, M0), C, M1) -> shuffle(C, A, M2) 14914 // shuffle(shuffle(A, B, M0), C, M1) -> shuffle(C, B, M2) 14915 std::swap(SV0, SV1); 14916 } 14917 14918 // shuffle(shuffle(A, B, M0), C, M1) -> shuffle(A, B, M2) 14919 // shuffle(shuffle(A, B, M0), C, M1) -> shuffle(A, C, M2) 14920 // shuffle(shuffle(A, B, M0), C, M1) -> shuffle(B, C, M2) 14921 return DAG.getVectorShuffle(VT, SDLoc(N), SV0, SV1, Mask); 14922 } 14923 14924 return SDValue(); 14925 } 14926 14927 SDValue DAGCombiner::visitSCALAR_TO_VECTOR(SDNode *N) { 14928 SDValue InVal = N->getOperand(0); 14929 EVT VT = N->getValueType(0); 14930 14931 // Replace a SCALAR_TO_VECTOR(EXTRACT_VECTOR_ELT(V,C0)) pattern 14932 // with a VECTOR_SHUFFLE. 14933 if (InVal.getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 14934 SDValue InVec = InVal->getOperand(0); 14935 SDValue EltNo = InVal->getOperand(1); 14936 14937 // FIXME: We could support implicit truncation if the shuffle can be 14938 // scaled to a smaller vector scalar type. 14939 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(EltNo); 14940 if (C0 && VT == InVec.getValueType() && 14941 VT.getScalarType() == InVal.getValueType()) { 14942 SmallVector<int, 8> NewMask(VT.getVectorNumElements(), -1); 14943 int Elt = C0->getZExtValue(); 14944 NewMask[0] = Elt; 14945 14946 if (TLI.isShuffleMaskLegal(NewMask, VT)) 14947 return DAG.getVectorShuffle(VT, SDLoc(N), InVec, DAG.getUNDEF(VT), 14948 NewMask); 14949 } 14950 } 14951 14952 return SDValue(); 14953 } 14954 14955 SDValue DAGCombiner::visitINSERT_SUBVECTOR(SDNode *N) { 14956 EVT VT = N->getValueType(0); 14957 SDValue N0 = N->getOperand(0); 14958 SDValue N1 = N->getOperand(1); 14959 SDValue N2 = N->getOperand(2); 14960 14961 // If inserting an UNDEF, just return the original vector. 14962 if (N1.isUndef()) 14963 return N0; 14964 14965 // If this is an insert of an extracted vector into an undef vector, we can 14966 // just use the input to the extract. 14967 if (N0.isUndef() && N1.getOpcode() == ISD::EXTRACT_SUBVECTOR && 14968 N1.getOperand(1) == N2 && N1.getOperand(0).getValueType() == VT) 14969 return N1.getOperand(0); 14970 14971 // Combine INSERT_SUBVECTORs where we are inserting to the same index. 14972 // INSERT_SUBVECTOR( INSERT_SUBVECTOR( Vec, SubOld, Idx ), SubNew, Idx ) 14973 // --> INSERT_SUBVECTOR( Vec, SubNew, Idx ) 14974 if (N0.getOpcode() == ISD::INSERT_SUBVECTOR && 14975 N0.getOperand(1).getValueType() == N1.getValueType() && 14976 N0.getOperand(2) == N2) 14977 return DAG.getNode(ISD::INSERT_SUBVECTOR, SDLoc(N), VT, N0.getOperand(0), 14978 N1, N2); 14979 14980 if (!isa<ConstantSDNode>(N2)) 14981 return SDValue(); 14982 14983 unsigned InsIdx = cast<ConstantSDNode>(N2)->getZExtValue(); 14984 14985 // Canonicalize insert_subvector dag nodes. 14986 // Example: 14987 // (insert_subvector (insert_subvector A, Idx0), Idx1) 14988 // -> (insert_subvector (insert_subvector A, Idx1), Idx0) 14989 if (N0.getOpcode() == ISD::INSERT_SUBVECTOR && N0.hasOneUse() && 14990 N1.getValueType() == N0.getOperand(1).getValueType() && 14991 isa<ConstantSDNode>(N0.getOperand(2))) { 14992 unsigned OtherIdx = cast<ConstantSDNode>(N0.getOperand(2))->getZExtValue(); 14993 if (InsIdx < OtherIdx) { 14994 // Swap nodes. 14995 SDValue NewOp = DAG.getNode(ISD::INSERT_SUBVECTOR, SDLoc(N), VT, 14996 N0.getOperand(0), N1, N2); 14997 AddToWorklist(NewOp.getNode()); 14998 return DAG.getNode(ISD::INSERT_SUBVECTOR, SDLoc(N0.getNode()), 14999 VT, NewOp, N0.getOperand(1), N0.getOperand(2)); 15000 } 15001 } 15002 15003 // If the input vector is a concatenation, and the insert replaces 15004 // one of the pieces, we can optimize into a single concat_vectors. 15005 if (N0.getOpcode() == ISD::CONCAT_VECTORS && N0.hasOneUse() && 15006 N0.getOperand(0).getValueType() == N1.getValueType()) { 15007 unsigned Factor = N1.getValueType().getVectorNumElements(); 15008 15009 SmallVector<SDValue, 8> Ops(N0->op_begin(), N0->op_end()); 15010 Ops[cast<ConstantSDNode>(N2)->getZExtValue() / Factor] = N1; 15011 15012 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, Ops); 15013 } 15014 15015 return SDValue(); 15016 } 15017 15018 SDValue DAGCombiner::visitFP_TO_FP16(SDNode *N) { 15019 SDValue N0 = N->getOperand(0); 15020 15021 // fold (fp_to_fp16 (fp16_to_fp op)) -> op 15022 if (N0->getOpcode() == ISD::FP16_TO_FP) 15023 return N0->getOperand(0); 15024 15025 return SDValue(); 15026 } 15027 15028 SDValue DAGCombiner::visitFP16_TO_FP(SDNode *N) { 15029 SDValue N0 = N->getOperand(0); 15030 15031 // fold fp16_to_fp(op & 0xffff) -> fp16_to_fp(op) 15032 if (N0->getOpcode() == ISD::AND) { 15033 ConstantSDNode *AndConst = getAsNonOpaqueConstant(N0.getOperand(1)); 15034 if (AndConst && AndConst->getAPIntValue() == 0xffff) { 15035 return DAG.getNode(ISD::FP16_TO_FP, SDLoc(N), N->getValueType(0), 15036 N0.getOperand(0)); 15037 } 15038 } 15039 15040 return SDValue(); 15041 } 15042 15043 /// Returns a vector_shuffle if it able to transform an AND to a vector_shuffle 15044 /// with the destination vector and a zero vector. 15045 /// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==> 15046 /// vector_shuffle V, Zero, <0, 4, 2, 4> 15047 SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) { 15048 EVT VT = N->getValueType(0); 15049 SDValue LHS = N->getOperand(0); 15050 SDValue RHS = N->getOperand(1); 15051 SDLoc DL(N); 15052 15053 // Make sure we're not running after operation legalization where it 15054 // may have custom lowered the vector shuffles. 15055 if (LegalOperations) 15056 return SDValue(); 15057 15058 if (N->getOpcode() != ISD::AND) 15059 return SDValue(); 15060 15061 if (RHS.getOpcode() == ISD::BITCAST) 15062 RHS = RHS.getOperand(0); 15063 15064 if (RHS.getOpcode() != ISD::BUILD_VECTOR) 15065 return SDValue(); 15066 15067 EVT RVT = RHS.getValueType(); 15068 unsigned NumElts = RHS.getNumOperands(); 15069 15070 // Attempt to create a valid clear mask, splitting the mask into 15071 // sub elements and checking to see if each is 15072 // all zeros or all ones - suitable for shuffle masking. 15073 auto BuildClearMask = [&](int Split) { 15074 int NumSubElts = NumElts * Split; 15075 int NumSubBits = RVT.getScalarSizeInBits() / Split; 15076 15077 SmallVector<int, 8> Indices; 15078 for (int i = 0; i != NumSubElts; ++i) { 15079 int EltIdx = i / Split; 15080 int SubIdx = i % Split; 15081 SDValue Elt = RHS.getOperand(EltIdx); 15082 if (Elt.isUndef()) { 15083 Indices.push_back(-1); 15084 continue; 15085 } 15086 15087 APInt Bits; 15088 if (isa<ConstantSDNode>(Elt)) 15089 Bits = cast<ConstantSDNode>(Elt)->getAPIntValue(); 15090 else if (isa<ConstantFPSDNode>(Elt)) 15091 Bits = cast<ConstantFPSDNode>(Elt)->getValueAPF().bitcastToAPInt(); 15092 else 15093 return SDValue(); 15094 15095 // Extract the sub element from the constant bit mask. 15096 if (DAG.getDataLayout().isBigEndian()) { 15097 Bits = Bits.lshr((Split - SubIdx - 1) * NumSubBits); 15098 } else { 15099 Bits = Bits.lshr(SubIdx * NumSubBits); 15100 } 15101 15102 if (Split > 1) 15103 Bits = Bits.trunc(NumSubBits); 15104 15105 if (Bits.isAllOnesValue()) 15106 Indices.push_back(i); 15107 else if (Bits == 0) 15108 Indices.push_back(i + NumSubElts); 15109 else 15110 return SDValue(); 15111 } 15112 15113 // Let's see if the target supports this vector_shuffle. 15114 EVT ClearSVT = EVT::getIntegerVT(*DAG.getContext(), NumSubBits); 15115 EVT ClearVT = EVT::getVectorVT(*DAG.getContext(), ClearSVT, NumSubElts); 15116 if (!TLI.isVectorClearMaskLegal(Indices, ClearVT)) 15117 return SDValue(); 15118 15119 SDValue Zero = DAG.getConstant(0, DL, ClearVT); 15120 return DAG.getBitcast(VT, DAG.getVectorShuffle(ClearVT, DL, 15121 DAG.getBitcast(ClearVT, LHS), 15122 Zero, Indices)); 15123 }; 15124 15125 // Determine maximum split level (byte level masking). 15126 int MaxSplit = 1; 15127 if (RVT.getScalarSizeInBits() % 8 == 0) 15128 MaxSplit = RVT.getScalarSizeInBits() / 8; 15129 15130 for (int Split = 1; Split <= MaxSplit; ++Split) 15131 if (RVT.getScalarSizeInBits() % Split == 0) 15132 if (SDValue S = BuildClearMask(Split)) 15133 return S; 15134 15135 return SDValue(); 15136 } 15137 15138 /// Visit a binary vector operation, like ADD. 15139 SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) { 15140 assert(N->getValueType(0).isVector() && 15141 "SimplifyVBinOp only works on vectors!"); 15142 15143 SDValue LHS = N->getOperand(0); 15144 SDValue RHS = N->getOperand(1); 15145 SDValue Ops[] = {LHS, RHS}; 15146 15147 // See if we can constant fold the vector operation. 15148 if (SDValue Fold = DAG.FoldConstantVectorArithmetic( 15149 N->getOpcode(), SDLoc(LHS), LHS.getValueType(), Ops, N->getFlags())) 15150 return Fold; 15151 15152 // Try to convert a constant mask AND into a shuffle clear mask. 15153 if (SDValue Shuffle = XformToShuffleWithZero(N)) 15154 return Shuffle; 15155 15156 // Type legalization might introduce new shuffles in the DAG. 15157 // Fold (VBinOp (shuffle (A, Undef, Mask)), (shuffle (B, Undef, Mask))) 15158 // -> (shuffle (VBinOp (A, B)), Undef, Mask). 15159 if (LegalTypes && isa<ShuffleVectorSDNode>(LHS) && 15160 isa<ShuffleVectorSDNode>(RHS) && LHS.hasOneUse() && RHS.hasOneUse() && 15161 LHS.getOperand(1).isUndef() && 15162 RHS.getOperand(1).isUndef()) { 15163 ShuffleVectorSDNode *SVN0 = cast<ShuffleVectorSDNode>(LHS); 15164 ShuffleVectorSDNode *SVN1 = cast<ShuffleVectorSDNode>(RHS); 15165 15166 if (SVN0->getMask().equals(SVN1->getMask())) { 15167 EVT VT = N->getValueType(0); 15168 SDValue UndefVector = LHS.getOperand(1); 15169 SDValue NewBinOp = DAG.getNode(N->getOpcode(), SDLoc(N), VT, 15170 LHS.getOperand(0), RHS.getOperand(0), 15171 N->getFlags()); 15172 AddUsersToWorklist(N); 15173 return DAG.getVectorShuffle(VT, SDLoc(N), NewBinOp, UndefVector, 15174 SVN0->getMask()); 15175 } 15176 } 15177 15178 return SDValue(); 15179 } 15180 15181 SDValue DAGCombiner::SimplifySelect(const SDLoc &DL, SDValue N0, SDValue N1, 15182 SDValue N2) { 15183 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!"); 15184 15185 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2, 15186 cast<CondCodeSDNode>(N0.getOperand(2))->get()); 15187 15188 // If we got a simplified select_cc node back from SimplifySelectCC, then 15189 // break it down into a new SETCC node, and a new SELECT node, and then return 15190 // the SELECT node, since we were called with a SELECT node. 15191 if (SCC.getNode()) { 15192 // Check to see if we got a select_cc back (to turn into setcc/select). 15193 // Otherwise, just return whatever node we got back, like fabs. 15194 if (SCC.getOpcode() == ISD::SELECT_CC) { 15195 SDValue SETCC = DAG.getNode(ISD::SETCC, SDLoc(N0), 15196 N0.getValueType(), 15197 SCC.getOperand(0), SCC.getOperand(1), 15198 SCC.getOperand(4)); 15199 AddToWorklist(SETCC.getNode()); 15200 return DAG.getSelect(SDLoc(SCC), SCC.getValueType(), SETCC, 15201 SCC.getOperand(2), SCC.getOperand(3)); 15202 } 15203 15204 return SCC; 15205 } 15206 return SDValue(); 15207 } 15208 15209 /// Given a SELECT or a SELECT_CC node, where LHS and RHS are the two values 15210 /// being selected between, see if we can simplify the select. Callers of this 15211 /// should assume that TheSelect is deleted if this returns true. As such, they 15212 /// should return the appropriate thing (e.g. the node) back to the top-level of 15213 /// the DAG combiner loop to avoid it being looked at. 15214 bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS, 15215 SDValue RHS) { 15216 15217 // fold (select (setcc x, [+-]0.0, *lt), NaN, (fsqrt x)) 15218 // The select + setcc is redundant, because fsqrt returns NaN for X < 0. 15219 if (const ConstantFPSDNode *NaN = isConstOrConstSplatFP(LHS)) { 15220 if (NaN->isNaN() && RHS.getOpcode() == ISD::FSQRT) { 15221 // We have: (select (setcc ?, ?, ?), NaN, (fsqrt ?)) 15222 SDValue Sqrt = RHS; 15223 ISD::CondCode CC; 15224 SDValue CmpLHS; 15225 const ConstantFPSDNode *Zero = nullptr; 15226 15227 if (TheSelect->getOpcode() == ISD::SELECT_CC) { 15228 CC = dyn_cast<CondCodeSDNode>(TheSelect->getOperand(4))->get(); 15229 CmpLHS = TheSelect->getOperand(0); 15230 Zero = isConstOrConstSplatFP(TheSelect->getOperand(1)); 15231 } else { 15232 // SELECT or VSELECT 15233 SDValue Cmp = TheSelect->getOperand(0); 15234 if (Cmp.getOpcode() == ISD::SETCC) { 15235 CC = dyn_cast<CondCodeSDNode>(Cmp.getOperand(2))->get(); 15236 CmpLHS = Cmp.getOperand(0); 15237 Zero = isConstOrConstSplatFP(Cmp.getOperand(1)); 15238 } 15239 } 15240 if (Zero && Zero->isZero() && 15241 Sqrt.getOperand(0) == CmpLHS && (CC == ISD::SETOLT || 15242 CC == ISD::SETULT || CC == ISD::SETLT)) { 15243 // We have: (select (setcc x, [+-]0.0, *lt), NaN, (fsqrt x)) 15244 CombineTo(TheSelect, Sqrt); 15245 return true; 15246 } 15247 } 15248 } 15249 // Cannot simplify select with vector condition 15250 if (TheSelect->getOperand(0).getValueType().isVector()) return false; 15251 15252 // If this is a select from two identical things, try to pull the operation 15253 // through the select. 15254 if (LHS.getOpcode() != RHS.getOpcode() || 15255 !LHS.hasOneUse() || !RHS.hasOneUse()) 15256 return false; 15257 15258 // If this is a load and the token chain is identical, replace the select 15259 // of two loads with a load through a select of the address to load from. 15260 // This triggers in things like "select bool X, 10.0, 123.0" after the FP 15261 // constants have been dropped into the constant pool. 15262 if (LHS.getOpcode() == ISD::LOAD) { 15263 LoadSDNode *LLD = cast<LoadSDNode>(LHS); 15264 LoadSDNode *RLD = cast<LoadSDNode>(RHS); 15265 15266 // Token chains must be identical. 15267 if (LHS.getOperand(0) != RHS.getOperand(0) || 15268 // Do not let this transformation reduce the number of volatile loads. 15269 LLD->isVolatile() || RLD->isVolatile() || 15270 // FIXME: If either is a pre/post inc/dec load, 15271 // we'd need to split out the address adjustment. 15272 LLD->isIndexed() || RLD->isIndexed() || 15273 // If this is an EXTLOAD, the VT's must match. 15274 LLD->getMemoryVT() != RLD->getMemoryVT() || 15275 // If this is an EXTLOAD, the kind of extension must match. 15276 (LLD->getExtensionType() != RLD->getExtensionType() && 15277 // The only exception is if one of the extensions is anyext. 15278 LLD->getExtensionType() != ISD::EXTLOAD && 15279 RLD->getExtensionType() != ISD::EXTLOAD) || 15280 // FIXME: this discards src value information. This is 15281 // over-conservative. It would be beneficial to be able to remember 15282 // both potential memory locations. Since we are discarding 15283 // src value info, don't do the transformation if the memory 15284 // locations are not in the default address space. 15285 LLD->getPointerInfo().getAddrSpace() != 0 || 15286 RLD->getPointerInfo().getAddrSpace() != 0 || 15287 !TLI.isOperationLegalOrCustom(TheSelect->getOpcode(), 15288 LLD->getBasePtr().getValueType())) 15289 return false; 15290 15291 // Check that the select condition doesn't reach either load. If so, 15292 // folding this will induce a cycle into the DAG. If not, this is safe to 15293 // xform, so create a select of the addresses. 15294 SDValue Addr; 15295 if (TheSelect->getOpcode() == ISD::SELECT) { 15296 SDNode *CondNode = TheSelect->getOperand(0).getNode(); 15297 if ((LLD->hasAnyUseOfValue(1) && LLD->isPredecessorOf(CondNode)) || 15298 (RLD->hasAnyUseOfValue(1) && RLD->isPredecessorOf(CondNode))) 15299 return false; 15300 // The loads must not depend on one another. 15301 if (LLD->isPredecessorOf(RLD) || 15302 RLD->isPredecessorOf(LLD)) 15303 return false; 15304 Addr = DAG.getSelect(SDLoc(TheSelect), 15305 LLD->getBasePtr().getValueType(), 15306 TheSelect->getOperand(0), LLD->getBasePtr(), 15307 RLD->getBasePtr()); 15308 } else { // Otherwise SELECT_CC 15309 SDNode *CondLHS = TheSelect->getOperand(0).getNode(); 15310 SDNode *CondRHS = TheSelect->getOperand(1).getNode(); 15311 15312 if ((LLD->hasAnyUseOfValue(1) && 15313 (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS))) || 15314 (RLD->hasAnyUseOfValue(1) && 15315 (RLD->isPredecessorOf(CondLHS) || RLD->isPredecessorOf(CondRHS)))) 15316 return false; 15317 15318 Addr = DAG.getNode(ISD::SELECT_CC, SDLoc(TheSelect), 15319 LLD->getBasePtr().getValueType(), 15320 TheSelect->getOperand(0), 15321 TheSelect->getOperand(1), 15322 LLD->getBasePtr(), RLD->getBasePtr(), 15323 TheSelect->getOperand(4)); 15324 } 15325 15326 SDValue Load; 15327 // It is safe to replace the two loads if they have different alignments, 15328 // but the new load must be the minimum (most restrictive) alignment of the 15329 // inputs. 15330 unsigned Alignment = std::min(LLD->getAlignment(), RLD->getAlignment()); 15331 MachineMemOperand::Flags MMOFlags = LLD->getMemOperand()->getFlags(); 15332 if (!RLD->isInvariant()) 15333 MMOFlags &= ~MachineMemOperand::MOInvariant; 15334 if (!RLD->isDereferenceable()) 15335 MMOFlags &= ~MachineMemOperand::MODereferenceable; 15336 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) { 15337 // FIXME: Discards pointer and AA info. 15338 Load = DAG.getLoad(TheSelect->getValueType(0), SDLoc(TheSelect), 15339 LLD->getChain(), Addr, MachinePointerInfo(), Alignment, 15340 MMOFlags); 15341 } else { 15342 // FIXME: Discards pointer and AA info. 15343 Load = DAG.getExtLoad( 15344 LLD->getExtensionType() == ISD::EXTLOAD ? RLD->getExtensionType() 15345 : LLD->getExtensionType(), 15346 SDLoc(TheSelect), TheSelect->getValueType(0), LLD->getChain(), Addr, 15347 MachinePointerInfo(), LLD->getMemoryVT(), Alignment, MMOFlags); 15348 } 15349 15350 // Users of the select now use the result of the load. 15351 CombineTo(TheSelect, Load); 15352 15353 // Users of the old loads now use the new load's chain. We know the 15354 // old-load value is dead now. 15355 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1)); 15356 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1)); 15357 return true; 15358 } 15359 15360 return false; 15361 } 15362 15363 /// Try to fold an expression of the form (N0 cond N1) ? N2 : N3 to a shift and 15364 /// bitwise 'and'. 15365 SDValue DAGCombiner::foldSelectCCToShiftAnd(const SDLoc &DL, SDValue N0, 15366 SDValue N1, SDValue N2, SDValue N3, 15367 ISD::CondCode CC) { 15368 // If this is a select where the false operand is zero and the compare is a 15369 // check of the sign bit, see if we can perform the "gzip trick": 15370 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A 15371 // select_cc setgt X, 0, A, 0 -> and (not (sra X, size(X)-1)), A 15372 EVT XType = N0.getValueType(); 15373 EVT AType = N2.getValueType(); 15374 if (!isNullConstant(N3) || !XType.bitsGE(AType)) 15375 return SDValue(); 15376 15377 // If the comparison is testing for a positive value, we have to invert 15378 // the sign bit mask, so only do that transform if the target has a bitwise 15379 // 'and not' instruction (the invert is free). 15380 if (CC == ISD::SETGT && TLI.hasAndNot(N2)) { 15381 // (X > -1) ? A : 0 15382 // (X > 0) ? X : 0 <-- This is canonical signed max. 15383 if (!(isAllOnesConstant(N1) || (isNullConstant(N1) && N0 == N2))) 15384 return SDValue(); 15385 } else if (CC == ISD::SETLT) { 15386 // (X < 0) ? A : 0 15387 // (X < 1) ? X : 0 <-- This is un-canonicalized signed min. 15388 if (!(isNullConstant(N1) || (isOneConstant(N1) && N0 == N2))) 15389 return SDValue(); 15390 } else { 15391 return SDValue(); 15392 } 15393 15394 // and (sra X, size(X)-1), A -> "and (srl X, C2), A" iff A is a single-bit 15395 // constant. 15396 EVT ShiftAmtTy = getShiftAmountTy(N0.getValueType()); 15397 auto *N2C = dyn_cast<ConstantSDNode>(N2.getNode()); 15398 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue() - 1)) == 0)) { 15399 unsigned ShCt = XType.getSizeInBits() - N2C->getAPIntValue().logBase2() - 1; 15400 SDValue ShiftAmt = DAG.getConstant(ShCt, DL, ShiftAmtTy); 15401 SDValue Shift = DAG.getNode(ISD::SRL, DL, XType, N0, ShiftAmt); 15402 AddToWorklist(Shift.getNode()); 15403 15404 if (XType.bitsGT(AType)) { 15405 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift); 15406 AddToWorklist(Shift.getNode()); 15407 } 15408 15409 if (CC == ISD::SETGT) 15410 Shift = DAG.getNOT(DL, Shift, AType); 15411 15412 return DAG.getNode(ISD::AND, DL, AType, Shift, N2); 15413 } 15414 15415 SDValue ShiftAmt = DAG.getConstant(XType.getSizeInBits() - 1, DL, ShiftAmtTy); 15416 SDValue Shift = DAG.getNode(ISD::SRA, DL, XType, N0, ShiftAmt); 15417 AddToWorklist(Shift.getNode()); 15418 15419 if (XType.bitsGT(AType)) { 15420 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift); 15421 AddToWorklist(Shift.getNode()); 15422 } 15423 15424 if (CC == ISD::SETGT) 15425 Shift = DAG.getNOT(DL, Shift, AType); 15426 15427 return DAG.getNode(ISD::AND, DL, AType, Shift, N2); 15428 } 15429 15430 /// Simplify an expression of the form (N0 cond N1) ? N2 : N3 15431 /// where 'cond' is the comparison specified by CC. 15432 SDValue DAGCombiner::SimplifySelectCC(const SDLoc &DL, SDValue N0, SDValue N1, 15433 SDValue N2, SDValue N3, ISD::CondCode CC, 15434 bool NotExtCompare) { 15435 // (x ? y : y) -> y. 15436 if (N2 == N3) return N2; 15437 15438 EVT VT = N2.getValueType(); 15439 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); 15440 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode()); 15441 15442 // Determine if the condition we're dealing with is constant 15443 SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()), 15444 N0, N1, CC, DL, false); 15445 if (SCC.getNode()) AddToWorklist(SCC.getNode()); 15446 15447 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) { 15448 // fold select_cc true, x, y -> x 15449 // fold select_cc false, x, y -> y 15450 return !SCCC->isNullValue() ? N2 : N3; 15451 } 15452 15453 // Check to see if we can simplify the select into an fabs node 15454 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) { 15455 // Allow either -0.0 or 0.0 15456 if (CFP->isZero()) { 15457 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs 15458 if ((CC == ISD::SETGE || CC == ISD::SETGT) && 15459 N0 == N2 && N3.getOpcode() == ISD::FNEG && 15460 N2 == N3.getOperand(0)) 15461 return DAG.getNode(ISD::FABS, DL, VT, N0); 15462 15463 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs 15464 if ((CC == ISD::SETLT || CC == ISD::SETLE) && 15465 N0 == N3 && N2.getOpcode() == ISD::FNEG && 15466 N2.getOperand(0) == N3) 15467 return DAG.getNode(ISD::FABS, DL, VT, N3); 15468 } 15469 } 15470 15471 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)" 15472 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0 15473 // in it. This is a win when the constant is not otherwise available because 15474 // it replaces two constant pool loads with one. We only do this if the FP 15475 // type is known to be legal, because if it isn't, then we are before legalize 15476 // types an we want the other legalization to happen first (e.g. to avoid 15477 // messing with soft float) and if the ConstantFP is not legal, because if 15478 // it is legal, we may not need to store the FP constant in a constant pool. 15479 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2)) 15480 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) { 15481 if (TLI.isTypeLegal(N2.getValueType()) && 15482 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) != 15483 TargetLowering::Legal && 15484 !TLI.isFPImmLegal(TV->getValueAPF(), TV->getValueType(0)) && 15485 !TLI.isFPImmLegal(FV->getValueAPF(), FV->getValueType(0))) && 15486 // If both constants have multiple uses, then we won't need to do an 15487 // extra load, they are likely around in registers for other users. 15488 (TV->hasOneUse() || FV->hasOneUse())) { 15489 Constant *Elts[] = { 15490 const_cast<ConstantFP*>(FV->getConstantFPValue()), 15491 const_cast<ConstantFP*>(TV->getConstantFPValue()) 15492 }; 15493 Type *FPTy = Elts[0]->getType(); 15494 const DataLayout &TD = DAG.getDataLayout(); 15495 15496 // Create a ConstantArray of the two constants. 15497 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts); 15498 SDValue CPIdx = 15499 DAG.getConstantPool(CA, TLI.getPointerTy(DAG.getDataLayout()), 15500 TD.getPrefTypeAlignment(FPTy)); 15501 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment(); 15502 15503 // Get the offsets to the 0 and 1 element of the array so that we can 15504 // select between them. 15505 SDValue Zero = DAG.getIntPtrConstant(0, DL); 15506 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType()); 15507 SDValue One = DAG.getIntPtrConstant(EltSize, SDLoc(FV)); 15508 15509 SDValue Cond = DAG.getSetCC(DL, 15510 getSetCCResultType(N0.getValueType()), 15511 N0, N1, CC); 15512 AddToWorklist(Cond.getNode()); 15513 SDValue CstOffset = DAG.getSelect(DL, Zero.getValueType(), 15514 Cond, One, Zero); 15515 AddToWorklist(CstOffset.getNode()); 15516 CPIdx = DAG.getNode(ISD::ADD, DL, CPIdx.getValueType(), CPIdx, 15517 CstOffset); 15518 AddToWorklist(CPIdx.getNode()); 15519 return DAG.getLoad( 15520 TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx, 15521 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), 15522 Alignment); 15523 } 15524 } 15525 15526 if (SDValue V = foldSelectCCToShiftAnd(DL, N0, N1, N2, N3, CC)) 15527 return V; 15528 15529 // fold (select_cc seteq (and x, y), 0, 0, A) -> (and (shr (shl x)) A) 15530 // where y is has a single bit set. 15531 // A plaintext description would be, we can turn the SELECT_CC into an AND 15532 // when the condition can be materialized as an all-ones register. Any 15533 // single bit-test can be materialized as an all-ones register with 15534 // shift-left and shift-right-arith. 15535 if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND && 15536 N0->getValueType(0) == VT && isNullConstant(N1) && isNullConstant(N2)) { 15537 SDValue AndLHS = N0->getOperand(0); 15538 ConstantSDNode *ConstAndRHS = dyn_cast<ConstantSDNode>(N0->getOperand(1)); 15539 if (ConstAndRHS && ConstAndRHS->getAPIntValue().countPopulation() == 1) { 15540 // Shift the tested bit over the sign bit. 15541 const APInt &AndMask = ConstAndRHS->getAPIntValue(); 15542 SDValue ShlAmt = 15543 DAG.getConstant(AndMask.countLeadingZeros(), SDLoc(AndLHS), 15544 getShiftAmountTy(AndLHS.getValueType())); 15545 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(N0), VT, AndLHS, ShlAmt); 15546 15547 // Now arithmetic right shift it all the way over, so the result is either 15548 // all-ones, or zero. 15549 SDValue ShrAmt = 15550 DAG.getConstant(AndMask.getBitWidth() - 1, SDLoc(Shl), 15551 getShiftAmountTy(Shl.getValueType())); 15552 SDValue Shr = DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl, ShrAmt); 15553 15554 return DAG.getNode(ISD::AND, DL, VT, Shr, N3); 15555 } 15556 } 15557 15558 // fold select C, 16, 0 -> shl C, 4 15559 if (N2C && isNullConstant(N3) && N2C->getAPIntValue().isPowerOf2() && 15560 TLI.getBooleanContents(N0.getValueType()) == 15561 TargetLowering::ZeroOrOneBooleanContent) { 15562 15563 // If the caller doesn't want us to simplify this into a zext of a compare, 15564 // don't do it. 15565 if (NotExtCompare && N2C->isOne()) 15566 return SDValue(); 15567 15568 // Get a SetCC of the condition 15569 // NOTE: Don't create a SETCC if it's not legal on this target. 15570 if (!LegalOperations || 15571 TLI.isOperationLegal(ISD::SETCC, N0.getValueType())) { 15572 SDValue Temp, SCC; 15573 // cast from setcc result type to select result type 15574 if (LegalTypes) { 15575 SCC = DAG.getSetCC(DL, getSetCCResultType(N0.getValueType()), 15576 N0, N1, CC); 15577 if (N2.getValueType().bitsLT(SCC.getValueType())) 15578 Temp = DAG.getZeroExtendInReg(SCC, SDLoc(N2), 15579 N2.getValueType()); 15580 else 15581 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2), 15582 N2.getValueType(), SCC); 15583 } else { 15584 SCC = DAG.getSetCC(SDLoc(N0), MVT::i1, N0, N1, CC); 15585 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2), 15586 N2.getValueType(), SCC); 15587 } 15588 15589 AddToWorklist(SCC.getNode()); 15590 AddToWorklist(Temp.getNode()); 15591 15592 if (N2C->isOne()) 15593 return Temp; 15594 15595 // shl setcc result by log2 n2c 15596 return DAG.getNode( 15597 ISD::SHL, DL, N2.getValueType(), Temp, 15598 DAG.getConstant(N2C->getAPIntValue().logBase2(), SDLoc(Temp), 15599 getShiftAmountTy(Temp.getValueType()))); 15600 } 15601 } 15602 15603 // Check to see if this is an integer abs. 15604 // select_cc setg[te] X, 0, X, -X -> 15605 // select_cc setgt X, -1, X, -X -> 15606 // select_cc setl[te] X, 0, -X, X -> 15607 // select_cc setlt X, 1, -X, X -> 15608 // Y = sra (X, size(X)-1); xor (add (X, Y), Y) 15609 if (N1C) { 15610 ConstantSDNode *SubC = nullptr; 15611 if (((N1C->isNullValue() && (CC == ISD::SETGT || CC == ISD::SETGE)) || 15612 (N1C->isAllOnesValue() && CC == ISD::SETGT)) && 15613 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) 15614 SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0)); 15615 else if (((N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE)) || 15616 (N1C->isOne() && CC == ISD::SETLT)) && 15617 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) 15618 SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0)); 15619 15620 EVT XType = N0.getValueType(); 15621 if (SubC && SubC->isNullValue() && XType.isInteger()) { 15622 SDLoc DL(N0); 15623 SDValue Shift = DAG.getNode(ISD::SRA, DL, XType, 15624 N0, 15625 DAG.getConstant(XType.getSizeInBits() - 1, DL, 15626 getShiftAmountTy(N0.getValueType()))); 15627 SDValue Add = DAG.getNode(ISD::ADD, DL, 15628 XType, N0, Shift); 15629 AddToWorklist(Shift.getNode()); 15630 AddToWorklist(Add.getNode()); 15631 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift); 15632 } 15633 } 15634 15635 // select_cc seteq X, 0, sizeof(X), ctlz(X) -> ctlz(X) 15636 // select_cc seteq X, 0, sizeof(X), ctlz_zero_undef(X) -> ctlz(X) 15637 // select_cc seteq X, 0, sizeof(X), cttz(X) -> cttz(X) 15638 // select_cc seteq X, 0, sizeof(X), cttz_zero_undef(X) -> cttz(X) 15639 // select_cc setne X, 0, ctlz(X), sizeof(X) -> ctlz(X) 15640 // select_cc setne X, 0, ctlz_zero_undef(X), sizeof(X) -> ctlz(X) 15641 // select_cc setne X, 0, cttz(X), sizeof(X) -> cttz(X) 15642 // select_cc setne X, 0, cttz_zero_undef(X), sizeof(X) -> cttz(X) 15643 if (N1C && N1C->isNullValue() && (CC == ISD::SETEQ || CC == ISD::SETNE)) { 15644 SDValue ValueOnZero = N2; 15645 SDValue Count = N3; 15646 // If the condition is NE instead of E, swap the operands. 15647 if (CC == ISD::SETNE) 15648 std::swap(ValueOnZero, Count); 15649 // Check if the value on zero is a constant equal to the bits in the type. 15650 if (auto *ValueOnZeroC = dyn_cast<ConstantSDNode>(ValueOnZero)) { 15651 if (ValueOnZeroC->getAPIntValue() == VT.getSizeInBits()) { 15652 // If the other operand is cttz/cttz_zero_undef of N0, and cttz is 15653 // legal, combine to just cttz. 15654 if ((Count.getOpcode() == ISD::CTTZ || 15655 Count.getOpcode() == ISD::CTTZ_ZERO_UNDEF) && 15656 N0 == Count.getOperand(0) && 15657 (!LegalOperations || TLI.isOperationLegal(ISD::CTTZ, VT))) 15658 return DAG.getNode(ISD::CTTZ, DL, VT, N0); 15659 // If the other operand is ctlz/ctlz_zero_undef of N0, and ctlz is 15660 // legal, combine to just ctlz. 15661 if ((Count.getOpcode() == ISD::CTLZ || 15662 Count.getOpcode() == ISD::CTLZ_ZERO_UNDEF) && 15663 N0 == Count.getOperand(0) && 15664 (!LegalOperations || TLI.isOperationLegal(ISD::CTLZ, VT))) 15665 return DAG.getNode(ISD::CTLZ, DL, VT, N0); 15666 } 15667 } 15668 } 15669 15670 return SDValue(); 15671 } 15672 15673 /// This is a stub for TargetLowering::SimplifySetCC. 15674 SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0, SDValue N1, 15675 ISD::CondCode Cond, const SDLoc &DL, 15676 bool foldBooleans) { 15677 TargetLowering::DAGCombinerInfo 15678 DagCombineInfo(DAG, Level, false, this); 15679 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL); 15680 } 15681 15682 /// Given an ISD::SDIV node expressing a divide by constant, return 15683 /// a DAG expression to select that will generate the same value by multiplying 15684 /// by a magic number. 15685 /// Ref: "Hacker's Delight" or "The PowerPC Compiler Writer's Guide". 15686 SDValue DAGCombiner::BuildSDIV(SDNode *N) { 15687 // when optimising for minimum size, we don't want to expand a div to a mul 15688 // and a shift. 15689 if (DAG.getMachineFunction().getFunction()->optForMinSize()) 15690 return SDValue(); 15691 15692 ConstantSDNode *C = isConstOrConstSplat(N->getOperand(1)); 15693 if (!C) 15694 return SDValue(); 15695 15696 // Avoid division by zero. 15697 if (C->isNullValue()) 15698 return SDValue(); 15699 15700 std::vector<SDNode*> Built; 15701 SDValue S = 15702 TLI.BuildSDIV(N, C->getAPIntValue(), DAG, LegalOperations, &Built); 15703 15704 for (SDNode *N : Built) 15705 AddToWorklist(N); 15706 return S; 15707 } 15708 15709 /// Given an ISD::SDIV node expressing a divide by constant power of 2, return a 15710 /// DAG expression that will generate the same value by right shifting. 15711 SDValue DAGCombiner::BuildSDIVPow2(SDNode *N) { 15712 ConstantSDNode *C = isConstOrConstSplat(N->getOperand(1)); 15713 if (!C) 15714 return SDValue(); 15715 15716 // Avoid division by zero. 15717 if (C->isNullValue()) 15718 return SDValue(); 15719 15720 std::vector<SDNode *> Built; 15721 SDValue S = TLI.BuildSDIVPow2(N, C->getAPIntValue(), DAG, &Built); 15722 15723 for (SDNode *N : Built) 15724 AddToWorklist(N); 15725 return S; 15726 } 15727 15728 /// Given an ISD::UDIV node expressing a divide by constant, return a DAG 15729 /// expression that will generate the same value by multiplying by a magic 15730 /// number. 15731 /// Ref: "Hacker's Delight" or "The PowerPC Compiler Writer's Guide". 15732 SDValue DAGCombiner::BuildUDIV(SDNode *N) { 15733 // when optimising for minimum size, we don't want to expand a div to a mul 15734 // and a shift. 15735 if (DAG.getMachineFunction().getFunction()->optForMinSize()) 15736 return SDValue(); 15737 15738 ConstantSDNode *C = isConstOrConstSplat(N->getOperand(1)); 15739 if (!C) 15740 return SDValue(); 15741 15742 // Avoid division by zero. 15743 if (C->isNullValue()) 15744 return SDValue(); 15745 15746 std::vector<SDNode*> Built; 15747 SDValue S = 15748 TLI.BuildUDIV(N, C->getAPIntValue(), DAG, LegalOperations, &Built); 15749 15750 for (SDNode *N : Built) 15751 AddToWorklist(N); 15752 return S; 15753 } 15754 15755 /// Determines the LogBase2 value for a non-null input value using the 15756 /// transform: LogBase2(V) = (EltBits - 1) - ctlz(V). 15757 SDValue DAGCombiner::BuildLogBase2(SDValue V, const SDLoc &DL) { 15758 EVT VT = V.getValueType(); 15759 unsigned EltBits = VT.getScalarSizeInBits(); 15760 SDValue Ctlz = DAG.getNode(ISD::CTLZ, DL, VT, V); 15761 SDValue Base = DAG.getConstant(EltBits - 1, DL, VT); 15762 SDValue LogBase2 = DAG.getNode(ISD::SUB, DL, VT, Base, Ctlz); 15763 return LogBase2; 15764 } 15765 15766 /// Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i) 15767 /// For the reciprocal, we need to find the zero of the function: 15768 /// F(X) = A X - 1 [which has a zero at X = 1/A] 15769 /// => 15770 /// X_{i+1} = X_i (2 - A X_i) = X_i + X_i (1 - A X_i) [this second form 15771 /// does not require additional intermediate precision] 15772 SDValue DAGCombiner::BuildReciprocalEstimate(SDValue Op, SDNodeFlags *Flags) { 15773 if (Level >= AfterLegalizeDAG) 15774 return SDValue(); 15775 15776 // TODO: Handle half and/or extended types? 15777 EVT VT = Op.getValueType(); 15778 if (VT.getScalarType() != MVT::f32 && VT.getScalarType() != MVT::f64) 15779 return SDValue(); 15780 15781 // If estimates are explicitly disabled for this function, we're done. 15782 MachineFunction &MF = DAG.getMachineFunction(); 15783 int Enabled = TLI.getRecipEstimateDivEnabled(VT, MF); 15784 if (Enabled == TLI.ReciprocalEstimate::Disabled) 15785 return SDValue(); 15786 15787 // Estimates may be explicitly enabled for this type with a custom number of 15788 // refinement steps. 15789 int Iterations = TLI.getDivRefinementSteps(VT, MF); 15790 if (SDValue Est = TLI.getRecipEstimate(Op, DAG, Enabled, Iterations)) { 15791 AddToWorklist(Est.getNode()); 15792 15793 if (Iterations) { 15794 EVT VT = Op.getValueType(); 15795 SDLoc DL(Op); 15796 SDValue FPOne = DAG.getConstantFP(1.0, DL, VT); 15797 15798 // Newton iterations: Est = Est + Est (1 - Arg * Est) 15799 for (int i = 0; i < Iterations; ++i) { 15800 SDValue NewEst = DAG.getNode(ISD::FMUL, DL, VT, Op, Est, Flags); 15801 AddToWorklist(NewEst.getNode()); 15802 15803 NewEst = DAG.getNode(ISD::FSUB, DL, VT, FPOne, NewEst, Flags); 15804 AddToWorklist(NewEst.getNode()); 15805 15806 NewEst = DAG.getNode(ISD::FMUL, DL, VT, Est, NewEst, Flags); 15807 AddToWorklist(NewEst.getNode()); 15808 15809 Est = DAG.getNode(ISD::FADD, DL, VT, Est, NewEst, Flags); 15810 AddToWorklist(Est.getNode()); 15811 } 15812 } 15813 return Est; 15814 } 15815 15816 return SDValue(); 15817 } 15818 15819 /// Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i) 15820 /// For the reciprocal sqrt, we need to find the zero of the function: 15821 /// F(X) = 1/X^2 - A [which has a zero at X = 1/sqrt(A)] 15822 /// => 15823 /// X_{i+1} = X_i (1.5 - A X_i^2 / 2) 15824 /// As a result, we precompute A/2 prior to the iteration loop. 15825 SDValue DAGCombiner::buildSqrtNROneConst(SDValue Arg, SDValue Est, 15826 unsigned Iterations, 15827 SDNodeFlags *Flags, bool Reciprocal) { 15828 EVT VT = Arg.getValueType(); 15829 SDLoc DL(Arg); 15830 SDValue ThreeHalves = DAG.getConstantFP(1.5, DL, VT); 15831 15832 // We now need 0.5 * Arg which we can write as (1.5 * Arg - Arg) so that 15833 // this entire sequence requires only one FP constant. 15834 SDValue HalfArg = DAG.getNode(ISD::FMUL, DL, VT, ThreeHalves, Arg, Flags); 15835 AddToWorklist(HalfArg.getNode()); 15836 15837 HalfArg = DAG.getNode(ISD::FSUB, DL, VT, HalfArg, Arg, Flags); 15838 AddToWorklist(HalfArg.getNode()); 15839 15840 // Newton iterations: Est = Est * (1.5 - HalfArg * Est * Est) 15841 for (unsigned i = 0; i < Iterations; ++i) { 15842 SDValue NewEst = DAG.getNode(ISD::FMUL, DL, VT, Est, Est, Flags); 15843 AddToWorklist(NewEst.getNode()); 15844 15845 NewEst = DAG.getNode(ISD::FMUL, DL, VT, HalfArg, NewEst, Flags); 15846 AddToWorklist(NewEst.getNode()); 15847 15848 NewEst = DAG.getNode(ISD::FSUB, DL, VT, ThreeHalves, NewEst, Flags); 15849 AddToWorklist(NewEst.getNode()); 15850 15851 Est = DAG.getNode(ISD::FMUL, DL, VT, Est, NewEst, Flags); 15852 AddToWorklist(Est.getNode()); 15853 } 15854 15855 // If non-reciprocal square root is requested, multiply the result by Arg. 15856 if (!Reciprocal) { 15857 Est = DAG.getNode(ISD::FMUL, DL, VT, Est, Arg, Flags); 15858 AddToWorklist(Est.getNode()); 15859 } 15860 15861 return Est; 15862 } 15863 15864 /// Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i) 15865 /// For the reciprocal sqrt, we need to find the zero of the function: 15866 /// F(X) = 1/X^2 - A [which has a zero at X = 1/sqrt(A)] 15867 /// => 15868 /// X_{i+1} = (-0.5 * X_i) * (A * X_i * X_i + (-3.0)) 15869 SDValue DAGCombiner::buildSqrtNRTwoConst(SDValue Arg, SDValue Est, 15870 unsigned Iterations, 15871 SDNodeFlags *Flags, bool Reciprocal) { 15872 EVT VT = Arg.getValueType(); 15873 SDLoc DL(Arg); 15874 SDValue MinusThree = DAG.getConstantFP(-3.0, DL, VT); 15875 SDValue MinusHalf = DAG.getConstantFP(-0.5, DL, VT); 15876 15877 // This routine must enter the loop below to work correctly 15878 // when (Reciprocal == false). 15879 assert(Iterations > 0); 15880 15881 // Newton iterations for reciprocal square root: 15882 // E = (E * -0.5) * ((A * E) * E + -3.0) 15883 for (unsigned i = 0; i < Iterations; ++i) { 15884 SDValue AE = DAG.getNode(ISD::FMUL, DL, VT, Arg, Est, Flags); 15885 AddToWorklist(AE.getNode()); 15886 15887 SDValue AEE = DAG.getNode(ISD::FMUL, DL, VT, AE, Est, Flags); 15888 AddToWorklist(AEE.getNode()); 15889 15890 SDValue RHS = DAG.getNode(ISD::FADD, DL, VT, AEE, MinusThree, Flags); 15891 AddToWorklist(RHS.getNode()); 15892 15893 // When calculating a square root at the last iteration build: 15894 // S = ((A * E) * -0.5) * ((A * E) * E + -3.0) 15895 // (notice a common subexpression) 15896 SDValue LHS; 15897 if (Reciprocal || (i + 1) < Iterations) { 15898 // RSQRT: LHS = (E * -0.5) 15899 LHS = DAG.getNode(ISD::FMUL, DL, VT, Est, MinusHalf, Flags); 15900 } else { 15901 // SQRT: LHS = (A * E) * -0.5 15902 LHS = DAG.getNode(ISD::FMUL, DL, VT, AE, MinusHalf, Flags); 15903 } 15904 AddToWorklist(LHS.getNode()); 15905 15906 Est = DAG.getNode(ISD::FMUL, DL, VT, LHS, RHS, Flags); 15907 AddToWorklist(Est.getNode()); 15908 } 15909 15910 return Est; 15911 } 15912 15913 /// Build code to calculate either rsqrt(Op) or sqrt(Op). In the latter case 15914 /// Op*rsqrt(Op) is actually computed, so additional postprocessing is needed if 15915 /// Op can be zero. 15916 SDValue DAGCombiner::buildSqrtEstimateImpl(SDValue Op, SDNodeFlags *Flags, 15917 bool Reciprocal) { 15918 if (Level >= AfterLegalizeDAG) 15919 return SDValue(); 15920 15921 // TODO: Handle half and/or extended types? 15922 EVT VT = Op.getValueType(); 15923 if (VT.getScalarType() != MVT::f32 && VT.getScalarType() != MVT::f64) 15924 return SDValue(); 15925 15926 // If estimates are explicitly disabled for this function, we're done. 15927 MachineFunction &MF = DAG.getMachineFunction(); 15928 int Enabled = TLI.getRecipEstimateSqrtEnabled(VT, MF); 15929 if (Enabled == TLI.ReciprocalEstimate::Disabled) 15930 return SDValue(); 15931 15932 // Estimates may be explicitly enabled for this type with a custom number of 15933 // refinement steps. 15934 int Iterations = TLI.getSqrtRefinementSteps(VT, MF); 15935 15936 bool UseOneConstNR = false; 15937 if (SDValue Est = 15938 TLI.getSqrtEstimate(Op, DAG, Enabled, Iterations, UseOneConstNR, 15939 Reciprocal)) { 15940 AddToWorklist(Est.getNode()); 15941 15942 if (Iterations) { 15943 Est = UseOneConstNR 15944 ? buildSqrtNROneConst(Op, Est, Iterations, Flags, Reciprocal) 15945 : buildSqrtNRTwoConst(Op, Est, Iterations, Flags, Reciprocal); 15946 15947 if (!Reciprocal) { 15948 // Unfortunately, Est is now NaN if the input was exactly 0.0. 15949 // Select out this case and force the answer to 0.0. 15950 EVT VT = Op.getValueType(); 15951 SDLoc DL(Op); 15952 15953 SDValue FPZero = DAG.getConstantFP(0.0, DL, VT); 15954 EVT CCVT = getSetCCResultType(VT); 15955 SDValue ZeroCmp = DAG.getSetCC(DL, CCVT, Op, FPZero, ISD::SETEQ); 15956 AddToWorklist(ZeroCmp.getNode()); 15957 15958 Est = DAG.getNode(VT.isVector() ? ISD::VSELECT : ISD::SELECT, DL, VT, 15959 ZeroCmp, FPZero, Est); 15960 AddToWorklist(Est.getNode()); 15961 } 15962 } 15963 return Est; 15964 } 15965 15966 return SDValue(); 15967 } 15968 15969 SDValue DAGCombiner::buildRsqrtEstimate(SDValue Op, SDNodeFlags *Flags) { 15970 return buildSqrtEstimateImpl(Op, Flags, true); 15971 } 15972 15973 SDValue DAGCombiner::buildSqrtEstimate(SDValue Op, SDNodeFlags *Flags) { 15974 return buildSqrtEstimateImpl(Op, Flags, false); 15975 } 15976 15977 /// Return true if base is a frame index, which is known not to alias with 15978 /// anything but itself. Provides base object and offset as results. 15979 static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset, 15980 const GlobalValue *&GV, const void *&CV) { 15981 // Assume it is a primitive operation. 15982 Base = Ptr; Offset = 0; GV = nullptr; CV = nullptr; 15983 15984 // If it's an adding a simple constant then integrate the offset. 15985 if (Base.getOpcode() == ISD::ADD) { 15986 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) { 15987 Base = Base.getOperand(0); 15988 Offset += C->getSExtValue(); 15989 } 15990 } 15991 15992 // Return the underlying GlobalValue, and update the Offset. Return false 15993 // for GlobalAddressSDNode since the same GlobalAddress may be represented 15994 // by multiple nodes with different offsets. 15995 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) { 15996 GV = G->getGlobal(); 15997 Offset += G->getOffset(); 15998 return false; 15999 } 16000 16001 // Return the underlying Constant value, and update the Offset. Return false 16002 // for ConstantSDNodes since the same constant pool entry may be represented 16003 // by multiple nodes with different offsets. 16004 if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) { 16005 CV = C->isMachineConstantPoolEntry() ? (const void *)C->getMachineCPVal() 16006 : (const void *)C->getConstVal(); 16007 Offset += C->getOffset(); 16008 return false; 16009 } 16010 // If it's any of the following then it can't alias with anything but itself. 16011 return isa<FrameIndexSDNode>(Base); 16012 } 16013 16014 /// Return true if there is any possibility that the two addresses overlap. 16015 bool DAGCombiner::isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1) const { 16016 // If they are the same then they must be aliases. 16017 if (Op0->getBasePtr() == Op1->getBasePtr()) return true; 16018 16019 // If they are both volatile then they cannot be reordered. 16020 if (Op0->isVolatile() && Op1->isVolatile()) return true; 16021 16022 // If one operation reads from invariant memory, and the other may store, they 16023 // cannot alias. These should really be checking the equivalent of mayWrite, 16024 // but it only matters for memory nodes other than load /store. 16025 if (Op0->isInvariant() && Op1->writeMem()) 16026 return false; 16027 16028 if (Op1->isInvariant() && Op0->writeMem()) 16029 return false; 16030 16031 // Gather base node and offset information. 16032 SDValue Base1, Base2; 16033 int64_t Offset1, Offset2; 16034 const GlobalValue *GV1, *GV2; 16035 const void *CV1, *CV2; 16036 bool isFrameIndex1 = FindBaseOffset(Op0->getBasePtr(), 16037 Base1, Offset1, GV1, CV1); 16038 bool isFrameIndex2 = FindBaseOffset(Op1->getBasePtr(), 16039 Base2, Offset2, GV2, CV2); 16040 16041 // If they have a same base address then check to see if they overlap. 16042 if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2))) 16043 return !((Offset1 + (Op0->getMemoryVT().getSizeInBits() >> 3)) <= Offset2 || 16044 (Offset2 + (Op1->getMemoryVT().getSizeInBits() >> 3)) <= Offset1); 16045 16046 // It is possible for different frame indices to alias each other, mostly 16047 // when tail call optimization reuses return address slots for arguments. 16048 // To catch this case, look up the actual index of frame indices to compute 16049 // the real alias relationship. 16050 if (isFrameIndex1 && isFrameIndex2) { 16051 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); 16052 Offset1 += MFI.getObjectOffset(cast<FrameIndexSDNode>(Base1)->getIndex()); 16053 Offset2 += MFI.getObjectOffset(cast<FrameIndexSDNode>(Base2)->getIndex()); 16054 return !((Offset1 + (Op0->getMemoryVT().getSizeInBits() >> 3)) <= Offset2 || 16055 (Offset2 + (Op1->getMemoryVT().getSizeInBits() >> 3)) <= Offset1); 16056 } 16057 16058 // Otherwise, if we know what the bases are, and they aren't identical, then 16059 // we know they cannot alias. 16060 if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2)) 16061 return false; 16062 16063 // If we know required SrcValue1 and SrcValue2 have relatively large alignment 16064 // compared to the size and offset of the access, we may be able to prove they 16065 // do not alias. This check is conservative for now to catch cases created by 16066 // splitting vector types. 16067 if ((Op0->getOriginalAlignment() == Op1->getOriginalAlignment()) && 16068 (Op0->getSrcValueOffset() != Op1->getSrcValueOffset()) && 16069 (Op0->getMemoryVT().getSizeInBits() >> 3 == 16070 Op1->getMemoryVT().getSizeInBits() >> 3) && 16071 (Op0->getOriginalAlignment() > (Op0->getMemoryVT().getSizeInBits() >> 3))) { 16072 int64_t OffAlign1 = Op0->getSrcValueOffset() % Op0->getOriginalAlignment(); 16073 int64_t OffAlign2 = Op1->getSrcValueOffset() % Op1->getOriginalAlignment(); 16074 16075 // There is no overlap between these relatively aligned accesses of similar 16076 // size, return no alias. 16077 if ((OffAlign1 + (Op0->getMemoryVT().getSizeInBits() >> 3)) <= OffAlign2 || 16078 (OffAlign2 + (Op1->getMemoryVT().getSizeInBits() >> 3)) <= OffAlign1) 16079 return false; 16080 } 16081 16082 bool UseAA = CombinerGlobalAA.getNumOccurrences() > 0 16083 ? CombinerGlobalAA 16084 : DAG.getSubtarget().useAA(); 16085 #ifndef NDEBUG 16086 if (CombinerAAOnlyFunc.getNumOccurrences() && 16087 CombinerAAOnlyFunc != DAG.getMachineFunction().getName()) 16088 UseAA = false; 16089 #endif 16090 if (UseAA && 16091 Op0->getMemOperand()->getValue() && Op1->getMemOperand()->getValue()) { 16092 // Use alias analysis information. 16093 int64_t MinOffset = std::min(Op0->getSrcValueOffset(), 16094 Op1->getSrcValueOffset()); 16095 int64_t Overlap1 = (Op0->getMemoryVT().getSizeInBits() >> 3) + 16096 Op0->getSrcValueOffset() - MinOffset; 16097 int64_t Overlap2 = (Op1->getMemoryVT().getSizeInBits() >> 3) + 16098 Op1->getSrcValueOffset() - MinOffset; 16099 AliasResult AAResult = 16100 AA.alias(MemoryLocation(Op0->getMemOperand()->getValue(), Overlap1, 16101 UseTBAA ? Op0->getAAInfo() : AAMDNodes()), 16102 MemoryLocation(Op1->getMemOperand()->getValue(), Overlap2, 16103 UseTBAA ? Op1->getAAInfo() : AAMDNodes())); 16104 if (AAResult == NoAlias) 16105 return false; 16106 } 16107 16108 // Otherwise we have to assume they alias. 16109 return true; 16110 } 16111 16112 /// Walk up chain skipping non-aliasing memory nodes, 16113 /// looking for aliasing nodes and adding them to the Aliases vector. 16114 void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain, 16115 SmallVectorImpl<SDValue> &Aliases) { 16116 SmallVector<SDValue, 8> Chains; // List of chains to visit. 16117 SmallPtrSet<SDNode *, 16> Visited; // Visited node set. 16118 16119 // Get alias information for node. 16120 bool IsLoad = isa<LoadSDNode>(N) && !cast<LSBaseSDNode>(N)->isVolatile(); 16121 16122 // Starting off. 16123 Chains.push_back(OriginalChain); 16124 unsigned Depth = 0; 16125 16126 // Look at each chain and determine if it is an alias. If so, add it to the 16127 // aliases list. If not, then continue up the chain looking for the next 16128 // candidate. 16129 while (!Chains.empty()) { 16130 SDValue Chain = Chains.pop_back_val(); 16131 16132 // For TokenFactor nodes, look at each operand and only continue up the 16133 // chain until we reach the depth limit. 16134 // 16135 // FIXME: The depth check could be made to return the last non-aliasing 16136 // chain we found before we hit a tokenfactor rather than the original 16137 // chain. 16138 if (Depth > TLI.getGatherAllAliasesMaxDepth()) { 16139 Aliases.clear(); 16140 Aliases.push_back(OriginalChain); 16141 return; 16142 } 16143 16144 // Don't bother if we've been before. 16145 if (!Visited.insert(Chain.getNode()).second) 16146 continue; 16147 16148 switch (Chain.getOpcode()) { 16149 case ISD::EntryToken: 16150 // Entry token is ideal chain operand, but handled in FindBetterChain. 16151 break; 16152 16153 case ISD::LOAD: 16154 case ISD::STORE: { 16155 // Get alias information for Chain. 16156 bool IsOpLoad = isa<LoadSDNode>(Chain.getNode()) && 16157 !cast<LSBaseSDNode>(Chain.getNode())->isVolatile(); 16158 16159 // If chain is alias then stop here. 16160 if (!(IsLoad && IsOpLoad) && 16161 isAlias(cast<LSBaseSDNode>(N), cast<LSBaseSDNode>(Chain.getNode()))) { 16162 Aliases.push_back(Chain); 16163 } else { 16164 // Look further up the chain. 16165 Chains.push_back(Chain.getOperand(0)); 16166 ++Depth; 16167 } 16168 break; 16169 } 16170 16171 case ISD::TokenFactor: 16172 // We have to check each of the operands of the token factor for "small" 16173 // token factors, so we queue them up. Adding the operands to the queue 16174 // (stack) in reverse order maintains the original order and increases the 16175 // likelihood that getNode will find a matching token factor (CSE.) 16176 if (Chain.getNumOperands() > 16) { 16177 Aliases.push_back(Chain); 16178 break; 16179 } 16180 for (unsigned n = Chain.getNumOperands(); n;) 16181 Chains.push_back(Chain.getOperand(--n)); 16182 ++Depth; 16183 break; 16184 16185 case ISD::CopyFromReg: 16186 // Forward past CopyFromReg. 16187 Chains.push_back(Chain.getOperand(0)); 16188 ++Depth; 16189 break; 16190 16191 default: 16192 // For all other instructions we will just have to take what we can get. 16193 Aliases.push_back(Chain); 16194 break; 16195 } 16196 } 16197 } 16198 16199 /// Walk up chain skipping non-aliasing memory nodes, looking for a better chain 16200 /// (aliasing node.) 16201 SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) { 16202 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor. 16203 16204 // Accumulate all the aliases to this node. 16205 GatherAllAliases(N, OldChain, Aliases); 16206 16207 // If no operands then chain to entry token. 16208 if (Aliases.size() == 0) 16209 return DAG.getEntryNode(); 16210 16211 // If a single operand then chain to it. We don't need to revisit it. 16212 if (Aliases.size() == 1) 16213 return Aliases[0]; 16214 16215 // Construct a custom tailored token factor. 16216 return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other, Aliases); 16217 } 16218 16219 // This function tries to collect a bunch of potentially interesting 16220 // nodes to improve the chains of, all at once. This might seem 16221 // redundant, as this function gets called when visiting every store 16222 // node, so why not let the work be done on each store as it's visited? 16223 // 16224 // I believe this is mainly important because MergeConsecutiveStores 16225 // is unable to deal with merging stores of different sizes, so unless 16226 // we improve the chains of all the potential candidates up-front 16227 // before running MergeConsecutiveStores, it might only see some of 16228 // the nodes that will eventually be candidates, and then not be able 16229 // to go from a partially-merged state to the desired final 16230 // fully-merged state. 16231 bool DAGCombiner::findBetterNeighborChains(StoreSDNode *St) { 16232 // This holds the base pointer, index, and the offset in bytes from the base 16233 // pointer. 16234 BaseIndexOffset BasePtr = BaseIndexOffset::match(St->getBasePtr(), DAG); 16235 16236 // We must have a base and an offset. 16237 if (!BasePtr.Base.getNode()) 16238 return false; 16239 16240 // Do not handle stores to undef base pointers. 16241 if (BasePtr.Base.isUndef()) 16242 return false; 16243 16244 SmallVector<StoreSDNode *, 8> ChainedStores; 16245 ChainedStores.push_back(St); 16246 16247 // Walk up the chain and look for nodes with offsets from the same 16248 // base pointer. Stop when reaching an instruction with a different kind 16249 // or instruction which has a different base pointer. 16250 StoreSDNode *Index = St; 16251 while (Index) { 16252 // If the chain has more than one use, then we can't reorder the mem ops. 16253 if (Index != St && !SDValue(Index, 0)->hasOneUse()) 16254 break; 16255 16256 if (Index->isVolatile() || Index->isIndexed()) 16257 break; 16258 16259 // Find the base pointer and offset for this memory node. 16260 BaseIndexOffset Ptr = BaseIndexOffset::match(Index->getBasePtr(), DAG); 16261 16262 // Check that the base pointer is the same as the original one. 16263 if (!Ptr.equalBaseIndex(BasePtr)) 16264 break; 16265 16266 // Walk up the chain to find the next store node, ignoring any 16267 // intermediate loads. Any other kind of node will halt the loop. 16268 SDNode *NextInChain = Index->getChain().getNode(); 16269 while (true) { 16270 if (StoreSDNode *STn = dyn_cast<StoreSDNode>(NextInChain)) { 16271 // We found a store node. Use it for the next iteration. 16272 if (STn->isVolatile() || STn->isIndexed()) { 16273 Index = nullptr; 16274 break; 16275 } 16276 ChainedStores.push_back(STn); 16277 Index = STn; 16278 break; 16279 } else if (LoadSDNode *Ldn = dyn_cast<LoadSDNode>(NextInChain)) { 16280 NextInChain = Ldn->getChain().getNode(); 16281 continue; 16282 } else { 16283 Index = nullptr; 16284 break; 16285 } 16286 } // end while 16287 } 16288 16289 // At this point, ChainedStores lists all of the Store nodes 16290 // reachable by iterating up through chain nodes matching the above 16291 // conditions. For each such store identified, try to find an 16292 // earlier chain to attach the store to which won't violate the 16293 // required ordering. 16294 bool MadeChangeToSt = false; 16295 SmallVector<std::pair<StoreSDNode *, SDValue>, 8> BetterChains; 16296 16297 for (StoreSDNode *ChainedStore : ChainedStores) { 16298 SDValue Chain = ChainedStore->getChain(); 16299 SDValue BetterChain = FindBetterChain(ChainedStore, Chain); 16300 16301 if (Chain != BetterChain) { 16302 if (ChainedStore == St) 16303 MadeChangeToSt = true; 16304 BetterChains.push_back(std::make_pair(ChainedStore, BetterChain)); 16305 } 16306 } 16307 16308 // Do all replacements after finding the replacements to make to avoid making 16309 // the chains more complicated by introducing new TokenFactors. 16310 for (auto Replacement : BetterChains) 16311 replaceStoreChain(Replacement.first, Replacement.second); 16312 16313 return MadeChangeToSt; 16314 } 16315 16316 /// This is the entry point for the file. 16317 void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA, 16318 CodeGenOpt::Level OptLevel) { 16319 /// This is the main entry point to this class. 16320 DAGCombiner(*this, AA, OptLevel).Run(Level); 16321 } 16322