[PATCH] D79217: Process gep (select ptr1, ptr2) in SROA
Stanislav Mekhanoshin via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 30 17:11:17 PDT 2020
rampitec created this revision.
rampitec added a reviewer: arsenm.
Herald added subscribers: hiraditya, wdng.
Herald added a project: LLVM.
https://reviews.llvm.org/D79217
Files:
llvm/lib/Transforms/Scalar/SROA.cpp
llvm/test/Transforms/SROA/select-gep.ll
Index: llvm/test/Transforms/SROA/select-gep.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/SROA/select-gep.ll
@@ -0,0 +1,35 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -sroa < %s | FileCheck %s
+
+%pair = type { i32, i32 }
+
+define i32 @test_sroa_select_gep(i1 %cond) {
+; CHECK-LABEL: @test_sroa_select_gep(
+; CHECK-NEXT: bb:
+; CHECK-NEXT: [[LOAD_SROA_SPECULATED:%.*]] = select i1 [[COND:%.*]], i32 undef, i32 undef
+; CHECK-NEXT: ret i32 [[LOAD_SROA_SPECULATED]]
+;
+bb:
+ %a = alloca %pair, align 4
+ %b = alloca %pair, align 4
+ %select = select i1 %cond, %pair* %a, %pair* %b
+ %gep = getelementptr inbounds %pair, %pair* %select, i32 0, i32 1
+ %load = load i32, i32* %gep, align 4
+ ret i32 %load
+}
+
+define i32 @test_sroa_select_gep_undef(i1 %cond) {
+; CHECK-LABEL: @test_sroa_select_gep_undef(
+; CHECK-NEXT: bb:
+; CHECK-NEXT: [[A_SROA_0:%.*]] = alloca i32
+; CHECK-NEXT: [[SELECT_SROA_SEL:%.*]] = select i1 [[COND:%.*]], i32* [[A_SROA_0]], i32* undef
+; CHECK-NEXT: [[LOAD:%.*]] = load i32, i32* [[SELECT_SROA_SEL]], align 4
+; CHECK-NEXT: ret i32 [[LOAD]]
+;
+bb:
+ %a = alloca %pair, align 4
+ %select = select i1 %cond, %pair* %a, %pair* undef
+ %gep = getelementptr inbounds %pair, %pair* %select, i32 0, i32 1
+ %load = load i32, i32* %gep, align 4
+ ret i32 %load
+}
Index: llvm/lib/Transforms/Scalar/SROA.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/SROA.cpp
+++ llvm/lib/Transforms/Scalar/SROA.cpp
@@ -3464,6 +3464,46 @@
}
bool visitGetElementPtrInst(GetElementPtrInst &GEPI) {
+ // Fold gep (select cond, ptr1, ptr2) => select cond, gep(ptr1), gep(ptr2)
+ if (SelectInst *Sel = dyn_cast<SelectInst>(GEPI.getPointerOperand())) {
+ LLVM_DEBUG(dbgs() << " Rewriting gep(select) -> select(gep):"
+ << "\n original: " << *Sel
+ << "\n " << GEPI);
+ IRBuilderTy Builder(&GEPI);
+ SmallVector<Value *, 4> Index(GEPI.idx_begin(), GEPI.idx_end());
+ bool IsInBounds = GEPI.isInBounds();
+
+ Value *True = Sel->getTrueValue();
+ Value *NTrue =
+ IsInBounds
+ ? Builder.CreateInBoundsGEP(True, Index,
+ True->getName() + ".sroa.gep")
+ : Builder.CreateGEP(True, Index, True->getName() + ".sroa.gep");
+
+ Value *False = Sel->getFalseValue();
+
+ Value *NFalse =
+ IsInBounds
+ ? Builder.CreateInBoundsGEP(False, Index,
+ False->getName() + ".sroa.gep")
+ : Builder.CreateGEP(False, Index, False->getName() + ".sroa.gep");
+
+ Value *NSel = Builder.CreateSelect(Sel->getCondition(), NTrue, NFalse,
+ Sel->getName() + ".sroa.sel");
+ GEPI.replaceAllUsesWith(NSel);
+ GEPI.eraseFromParent();
+
+ LLVM_DEBUG(dbgs() << "\n to: " << *NTrue
+ << "\n " << *NFalse
+ << "\n " << *NSel << '\n');
+
+ if (isa<Instruction>(NTrue))
+ visit(cast<Instruction>(NTrue));
+ if (isa<Instruction>(NFalse))
+ visit(cast<Instruction>(NFalse));
+ return true;
+ }
+
enqueueUsers(GEPI);
return false;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D79217.261394.patch
Type: text/x-patch
Size: 3450 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200501/139a4083/attachment.bin>
More information about the llvm-commits
mailing list