Skip to content

[SROA] Unfold gep of index phi (round 2) #83494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 76 additions & 39 deletions llvm/lib/Transforms/Scalar/SROA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3956,11 +3956,11 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
return false;
}

// Fold gep (select cond, ptr1, ptr2), idx
// Unfold gep (select cond, ptr1, ptr2), idx
// => select cond, gep(ptr1, idx), gep(ptr2, idx)
// and gep ptr, (select cond, idx1, idx2)
// => select cond, gep(ptr, idx1), gep(ptr, idx2)
bool foldGEPSelect(GetElementPtrInst &GEPI) {
bool unfoldGEPSelect(GetElementPtrInst &GEPI) {
// Check whether the GEP has exactly one select operand and all indices
// will become constant after the transform.
SelectInst *Sel = dyn_cast<SelectInst>(GEPI.getPointerOperand());
Expand Down Expand Up @@ -4029,67 +4029,104 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
return true;
}

// Fold gep (phi ptr1, ptr2) => phi gep(ptr1), gep(ptr2)
bool foldGEPPhi(GetElementPtrInst &GEPI) {
if (!GEPI.hasAllConstantIndices())
return false;
// Unfold gep (phi ptr1, ptr2), idx
// => phi ((gep ptr1, idx), (gep ptr2, idx))
// and gep ptr, (phi idx1, idx2)
// => phi ((gep ptr, idx1), (gep ptr, idx2))
bool unfoldGEPPhi(GetElementPtrInst &GEPI) {
// To prevent infinitely expanding recursive phis, bail if the GEP pointer
// operand (looking through the phi if it is the phi we want to unfold) is
// an instruction besides an alloca.
PHINode *Phi = dyn_cast<PHINode>(GEPI.getPointerOperand());
auto IsInvalidPointerOperand = [](Value *V) {
return isa<Instruction>(V) && !isa<AllocaInst>(V);
};
if (Phi) {
if (any_of(Phi->operands(), IsInvalidPointerOperand))
return false;
} else {
if (IsInvalidPointerOperand(GEPI.getPointerOperand()))
return false;
}
// Check whether the GEP has exactly one phi operand (including the pointer
// operand) and all indices will become constant after the transform.
for (Value *Op : GEPI.indices()) {
if (auto *SI = dyn_cast<PHINode>(Op)) {
if (Phi)
return false;

Phi = SI;
if (!all_of(Phi->incoming_values(),
[](Value *V) { return isa<ConstantInt>(V); }))
return false;
continue;
}

PHINode *PHI = cast<PHINode>(GEPI.getPointerOperand());
if (GEPI.getParent() != PHI->getParent() ||
llvm::any_of(PHI->incoming_values(), [](Value *In) {
Instruction *I = dyn_cast<Instruction>(In);
return !I || isa<GetElementPtrInst>(I) || isa<PHINode>(I) ||
succ_empty(I->getParent()) ||
!I->getParent()->isLegalToHoistInto();
}))
if (!isa<ConstantInt>(Op))
return false;
}

if (!Phi)
return false;

LLVM_DEBUG(dbgs() << " Rewriting gep(phi) -> phi(gep):\n";
dbgs() << " original: " << *PHI << "\n";
dbgs() << " original: " << *Phi << "\n";
dbgs() << " " << GEPI << "\n";);

SmallVector<Value *, 4> Index(GEPI.indices());
auto GetNewOps = [&](Value *PhiOp) {
SmallVector<Value *> NewOps;
for (Value *Op : GEPI.operands())
if (Op == Phi)
NewOps.push_back(PhiOp);
else
NewOps.push_back(Op);
return NewOps;
};

IRB.SetInsertPoint(Phi);
PHINode *NewPhi = IRB.CreatePHI(GEPI.getType(), Phi->getNumIncomingValues(),
Phi->getName() + ".sroa.phi");

bool IsInBounds = GEPI.isInBounds();
IRB.SetInsertPoint(GEPI.getParent(), GEPI.getParent()->getFirstNonPHIIt());
PHINode *NewPN = IRB.CreatePHI(GEPI.getType(), PHI->getNumIncomingValues(),
PHI->getName() + ".sroa.phi");
for (unsigned I = 0, E = PHI->getNumIncomingValues(); I != E; ++I) {
BasicBlock *B = PHI->getIncomingBlock(I);
Value *NewVal = nullptr;
int Idx = NewPN->getBasicBlockIndex(B);
if (Idx >= 0) {
NewVal = NewPN->getIncomingValue(Idx);
Type *SourceTy = GEPI.getSourceElementType();
// We only handle arguments, constants, and static allocas here, so we can
// insert GEPs at the end of the entry block.
IRB.SetInsertPoint(GEPI.getFunction()->getEntryBlock().getTerminator());
for (unsigned I = 0, E = Phi->getNumIncomingValues(); I != E; ++I) {
Value *Op = Phi->getIncomingValue(I);
BasicBlock *BB = Phi->getIncomingBlock(I);
Value *NewGEP;
if (int NI = NewPhi->getBasicBlockIndex(BB); NI >= 0) {
NewGEP = NewPhi->getIncomingValue(NI);
} else {
Instruction *In = cast<Instruction>(PHI->getIncomingValue(I));

IRB.SetInsertPoint(In->getParent(), std::next(In->getIterator()));
Type *Ty = GEPI.getSourceElementType();
NewVal = IRB.CreateGEP(Ty, In, Index, In->getName() + ".sroa.gep",
IsInBounds);
SmallVector<Value *> NewOps = GetNewOps(Op);
NewGEP =
IRB.CreateGEP(SourceTy, NewOps[0], ArrayRef(NewOps).drop_front(),
Phi->getName() + ".sroa.gep", IsInBounds);
}
NewPN->addIncoming(NewVal, B);
NewPhi->addIncoming(NewGEP, BB);
}

Visited.erase(&GEPI);
GEPI.replaceAllUsesWith(NewPN);
GEPI.replaceAllUsesWith(NewPhi);
GEPI.eraseFromParent();
Visited.insert(NewPN);
enqueueUsers(*NewPN);
Visited.insert(NewPhi);
enqueueUsers(*NewPhi);

LLVM_DEBUG(dbgs() << " to: ";
for (Value *In
: NewPN->incoming_values()) dbgs()
: NewPhi->incoming_values()) dbgs()
<< "\n " << *In;
dbgs() << "\n " << *NewPN << '\n');
dbgs() << "\n " << *NewPhi << '\n');

return true;
}

bool visitGetElementPtrInst(GetElementPtrInst &GEPI) {
if (foldGEPSelect(GEPI))
if (unfoldGEPSelect(GEPI))
return true;

if (isa<PHINode>(GEPI.getPointerOperand()) && foldGEPPhi(GEPI))
if (unfoldGEPPhi(GEPI))
return true;

enqueueUsers(GEPI);
Expand Down
20 changes: 10 additions & 10 deletions llvm/test/Transforms/SROA/phi-and-select.ll
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ define i32 @test3(i32 %x) {
; CHECK-LABEL: @test3(
; CHECK-NEXT: entry:
; CHECK-NEXT: switch i32 [[X:%.*]], label [[BB0:%.*]] [
; CHECK-NEXT: i32 1, label [[BB1:%.*]]
; CHECK-NEXT: i32 2, label [[BB2:%.*]]
; CHECK-NEXT: i32 3, label [[BB3:%.*]]
; CHECK-NEXT: i32 4, label [[BB4:%.*]]
; CHECK-NEXT: i32 5, label [[BB5:%.*]]
; CHECK-NEXT: i32 6, label [[BB6:%.*]]
; CHECK-NEXT: i32 7, label [[BB7:%.*]]
; CHECK-NEXT: i32 1, label [[BB1:%.*]]
; CHECK-NEXT: i32 2, label [[BB2:%.*]]
; CHECK-NEXT: i32 3, label [[BB3:%.*]]
; CHECK-NEXT: i32 4, label [[BB4:%.*]]
; CHECK-NEXT: i32 5, label [[BB5:%.*]]
; CHECK-NEXT: i32 6, label [[BB6:%.*]]
; CHECK-NEXT: i32 7, label [[BB7:%.*]]
; CHECK-NEXT: ]
; CHECK: bb0:
; CHECK-NEXT: br label [[EXIT:%.*]]
Expand Down Expand Up @@ -733,6 +733,7 @@ define void @PR20822(i1 %c1, i1 %c2, ptr %ptr) {
; CHECK-LABEL: @PR20822(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[F_SROA_0:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[F1_SROA_GEP:%.*]] = getelementptr inbounds [[STRUCT_S:%.*]], ptr [[PTR:%.*]], i32 0, i32 0
; CHECK-NEXT: br i1 [[C1:%.*]], label [[IF_END:%.*]], label [[FOR_COND:%.*]]
; CHECK: for.cond:
; CHECK-NEXT: br label [[IF_END]]
Expand All @@ -742,9 +743,8 @@ define void @PR20822(i1 %c1, i1 %c2, ptr %ptr) {
; CHECK: if.then2:
; CHECK-NEXT: br label [[IF_THEN5]]
; CHECK: if.then5:
; CHECK-NEXT: [[F1:%.*]] = phi ptr [ [[PTR:%.*]], [[IF_THEN2]] ], [ [[F_SROA_0]], [[IF_END]] ]
; CHECK-NEXT: [[DOTFCA_0_GEP:%.*]] = getelementptr inbounds [[STRUCT_S:%.*]], ptr [[F1]], i32 0, i32 0
; CHECK-NEXT: store i32 0, ptr [[DOTFCA_0_GEP]], align 4
; CHECK-NEXT: [[F1_SROA_PHI:%.*]] = phi ptr [ [[F1_SROA_GEP]], [[IF_THEN2]] ], [ [[F_SROA_0]], [[IF_END]] ]
; CHECK-NEXT: store i32 0, ptr [[F1_SROA_PHI]], align 4
; CHECK-NEXT: ret void
;
entry:
Expand Down
Loading