@@ -2355,8 +2355,7 @@ void SelectionDAGLegalize::ExpandSinCosLibCall(
23552355
23562356 // Find users of the node that store the results. The destination pointers
23572357 // can be used instead of creating stack allocations.
2358- StoreSDNode *SinST = nullptr ;
2359- StoreSDNode *CosST = nullptr ;
2358+ std::array<StoreSDNode *, 2 > ResultStores = {nullptr };
23602359 for (SDNode::use_iterator UI = Node->use_begin (), UE = Node->use_end ();
23612360 UI != UE; ++UI) {
23622361 SDUse &Use = UI.getUse ();
@@ -2367,22 +2366,18 @@ void SelectionDAGLegalize::ExpandSinCosLibCall(
23672366 if (!ST->isSimple () || ST->getPointerInfo ().getAddrSpace () != 0 ||
23682367 ST->getAlign () < DAG.getDataLayout ().getABITypeAlign (RetTy))
23692368 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 ());
23862381
23872382 TargetLowering::ArgListTy Args;
23882383 TargetLowering::ArgListEntry Entry{};
@@ -2392,28 +2387,19 @@ void SelectionDAGLegalize::ExpandSinCosLibCall(
23922387 Entry.Ty = RetTy;
23932388 Args.push_back (Entry);
23942389
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);
24122398 }
2413- if (InChains.empty ())
2414- InChains.push_back (DAG.getEntryNode ());
24152399
24162400 SDLoc DL (Node);
2401+
2402+ // Combine any input chains from the stores.
24172403 SDValue InChain = DAG.getTokenFactor (DL, InChains);
24182404 SDValue Callee = DAG.getExternalSymbol (TLI.getLibcallName (LC),
24192405 TLI.getPointerTy (DAG.getDataLayout ()));
@@ -2424,16 +2410,19 @@ void SelectionDAGLegalize::ExpandSinCosLibCall(
24242410
24252411 auto [Call, OutChain] = TLI.LowerCallTo (CLI);
24262412
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);
24372426 }
24382427}
24392428
0 commit comments