@@ -2355,8 +2355,7 @@ void SelectionDAGLegalize::ExpandSinCosLibCall(
2355
2355
2356
2356
// Find users of the node that store the results. The destination pointers
2357
2357
// can be used instead of creating stack allocations.
2358
- StoreSDNode *SinST = nullptr ;
2359
- StoreSDNode *CosST = nullptr ;
2358
+ std::array<StoreSDNode *, 2 > ResultStores = {nullptr };
2360
2359
for (SDNode::use_iterator UI = Node->use_begin (), UE = Node->use_end ();
2361
2360
UI != UE; ++UI) {
2362
2361
SDUse &Use = UI.getUse ();
@@ -2367,22 +2366,18 @@ void SelectionDAGLegalize::ExpandSinCosLibCall(
2367
2366
if (!ST->isSimple () || ST->getPointerInfo ().getAddrSpace () != 0 ||
2368
2367
ST->getAlign () < DAG.getDataLayout ().getABITypeAlign (RetTy))
2369
2368
continue ;
2370
- if (Use.getResNo () == 0 )
2371
- SinST = ST;
2372
- if (Use.getResNo () == 1 )
2373
- CosST = ST;
2374
- }
2375
-
2376
- auto GetOrCreateOutPointer = [&](StoreSDNode *MaybeStore) {
2377
- if (MaybeStore)
2378
- return std::make_pair (MaybeStore->getBasePtr (),
2379
- MaybeStore->getPointerInfo ());
2380
- SDValue StackSlot = DAG.CreateStackTemporary (RetVT);
2381
- auto PtrInfo = MachinePointerInfo::getFixedStack (
2382
- DAG.getMachineFunction (),
2383
- cast<FrameIndexSDNode>(StackSlot)->getIndex ());
2384
- return std::make_pair (StackSlot, PtrInfo);
2385
- };
2369
+ ResultStores[Use.getResNo ()] = ST;
2370
+ }
2371
+
2372
+ // Collect input chains (and avoid chains referring to one of the stores).
2373
+ SmallVector<SDValue> InChains;
2374
+ for (auto [ResNum, ST] : llvm::enumerate (ResultStores)) {
2375
+ unsigned OtherResNum = ResNum == 0 ? 1 : 0 ;
2376
+ if (ST && ST->getChain ().getNode () != ResultStores[OtherResNum])
2377
+ InChains.push_back (ST->getChain ());
2378
+ }
2379
+ if (InChains.empty ())
2380
+ InChains.push_back (DAG.getEntryNode ());
2386
2381
2387
2382
TargetLowering::ArgListTy Args;
2388
2383
TargetLowering::ArgListEntry Entry{};
@@ -2392,28 +2387,19 @@ void SelectionDAGLegalize::ExpandSinCosLibCall(
2392
2387
Entry.Ty = RetTy;
2393
2388
Args.push_back (Entry);
2394
2389
2395
- // Pass the return address of sin.
2396
- auto SinPtr = GetOrCreateOutPointer (SinST);
2397
- Entry.Node = SinPtr.first ;
2398
- Entry.Ty = PointerType::getUnqual (RetTy->getContext ());
2399
- Args.push_back (Entry);
2400
-
2401
- // Also pass the return address of the cos.
2402
- auto CosPtr = GetOrCreateOutPointer (CosST);
2403
- Entry.Node = CosPtr.first ;
2404
- Entry.Ty = PointerType::getUnqual (RetTy->getContext ());
2405
- Args.push_back (Entry);
2406
-
2407
- // Combine any input chains from the stores.
2408
- SmallVector<SDValue, 2 > InChains{};
2409
- for (StoreSDNode *ST : {SinST, CosST}) {
2410
- if (ST)
2411
- InChains.push_back (ST->getChain ());
2390
+ // Pass the output pointers for sin and cos.
2391
+ SmallVector<SDValue, 2 > ResultPtrs{};
2392
+ for (StoreSDNode *ST : ResultStores) {
2393
+ SDValue ResultPtr = ST ? ST->getBasePtr () : DAG.CreateStackTemporary (RetVT);
2394
+ Entry.Node = ResultPtr;
2395
+ Entry.Ty = PointerType::getUnqual (RetTy->getContext ());
2396
+ Args.push_back (Entry);
2397
+ ResultPtrs.push_back (ResultPtr);
2412
2398
}
2413
- if (InChains.empty ())
2414
- InChains.push_back (DAG.getEntryNode ());
2415
2399
2416
2400
SDLoc DL (Node);
2401
+
2402
+ // Combine any input chains from the stores.
2417
2403
SDValue InChain = DAG.getTokenFactor (DL, InChains);
2418
2404
SDValue Callee = DAG.getExternalSymbol (TLI.getLibcallName (LC),
2419
2405
TLI.getPointerTy (DAG.getDataLayout ()));
@@ -2424,16 +2410,19 @@ void SelectionDAGLegalize::ExpandSinCosLibCall(
2424
2410
2425
2411
auto [Call, OutChain] = TLI.LowerCallTo (CLI);
2426
2412
2427
- // Replace the stores with the library call.
2428
- for (StoreSDNode *ST : {SinST, CosST}) {
2429
- if (!ST)
2430
- continue ;
2431
- DAG.ReplaceAllUsesOfValueWith (SDValue (ST, 0 ), OutChain);
2432
- }
2433
-
2434
- for (auto [Ptr, PtrInfo] : {SinPtr, CosPtr}) {
2435
- SDValue LoadExp = DAG.getLoad (RetVT, DL, OutChain, Ptr, PtrInfo);
2436
- Results.push_back (LoadExp);
2413
+ for (auto [ResNo, ResultPtr] : llvm::enumerate (ResultPtrs)) {
2414
+ MachinePointerInfo PtrInfo;
2415
+ if (StoreSDNode *ST = ResultStores[ResNo]) {
2416
+ // Replace store with the library call.
2417
+ DAG.ReplaceAllUsesOfValueWith (SDValue (ST, 0 ), OutChain);
2418
+ PtrInfo = ST->getPointerInfo ();
2419
+ } else {
2420
+ PtrInfo = MachinePointerInfo::getFixedStack (
2421
+ DAG.getMachineFunction (),
2422
+ cast<FrameIndexSDNode>(ResultPtr)->getIndex ());
2423
+ }
2424
+ SDValue LoadResult = DAG.getLoad (RetVT, DL, OutChain, ResultPtr, PtrInfo);
2425
+ Results.push_back (LoadResult);
2437
2426
}
2438
2427
}
2439
2428
0 commit comments