Skip to content

Commit 529a94e

Browse files
committed
Avoid cyclic in chains
1 parent 988273f commit 529a94e

File tree

2 files changed

+39
-50
lines changed

2 files changed

+39
-50
lines changed

llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -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

llvm/test/CodeGen/AArch64/sincos-stack-slots.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ entry:
118118

119119
; Negative test. We can't fold volatile stores into the library call.
120120
define void @sincos_volatile_result_stores(float %x, ptr %out_sin, ptr %out_cos) {
121-
; CHECK-LABEL: negative_fold_sincos_volatile_store:
121+
; CHECK-LABEL: sincos_volatile_result_stores:
122122
; CHECK: // %bb.0: // %entry
123123
; CHECK-NEXT: str x30, [sp, #-32]! // 8-byte Folded Spill
124124
; CHECK-NEXT: stp x20, x19, [sp, #16] // 16-byte Folded Spill
@@ -147,7 +147,7 @@ entry:
147147

148148
; Negative test. We can't fold atomic stores into the library call.
149149
define void @sincos_atomic_result_stores(float %x, ptr %out_sin, ptr %out_cos) {
150-
; CHECK-LABEL: negative_fold_sincos_atomic_store:
150+
; CHECK-LABEL: sincos_atomic_result_stores:
151151
; CHECK: // %bb.0: // %entry
152152
; CHECK-NEXT: str x30, [sp, #-32]! // 8-byte Folded Spill
153153
; CHECK-NEXT: stp x20, x19, [sp, #16] // 16-byte Folded Spill
@@ -177,7 +177,7 @@ entry:
177177

178178
; Negative test. We can't fold misaligned stores into the library call.
179179
define void @sincos_misaligned_result_stores(double %x, ptr %out_sin, ptr %out_cos) {
180-
; CHECK-LABEL: negative_sincos_bad_alignment:
180+
; CHECK-LABEL: sincos_misaligned_result_stores:
181181
; CHECK: // %bb.0: // %entry
182182
; CHECK-NEXT: sub sp, sp, #48
183183
; CHECK-NEXT: str x30, [sp, #16] // 8-byte Folded Spill

0 commit comments

Comments
 (0)