Skip to content

Commit

Permalink
Revert "SROA: Enhance speculateSelectInstLoads"
Browse files Browse the repository at this point in the history
This reverts commit ffc3fb6.
  • Loading branch information
cdevadas committed Aug 9, 2021
1 parent b5e470a commit fcf2d5f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 66 deletions.
29 changes: 3 additions & 26 deletions llvm/lib/Transforms/Scalar/SROA.cpp
Expand Up @@ -1330,21 +1330,14 @@ static void speculatePHINodeLoads(PHINode &PN) {
/// %V = select i1 %cond, i32 %V1, i32 %V2
///
/// We can do this to a select if its only uses are loads and if the operand
/// to the select can be loaded unconditionally. If found an intervening bitcast
/// with a single use of the load, allow the promotion.
/// to the select can be loaded unconditionally.
static bool isSafeSelectToSpeculate(SelectInst &SI) {
Value *TValue = SI.getTrueValue();
Value *FValue = SI.getFalseValue();
const DataLayout &DL = SI.getModule()->getDataLayout();

for (User *U : SI.users()) {
LoadInst *LI;
BitCastInst *BC = dyn_cast<BitCastInst>(U);
if (BC && BC->hasOneUse())
LI = dyn_cast<LoadInst>(*BC->user_begin());
else
LI = dyn_cast<LoadInst>(U);

LoadInst *LI = dyn_cast<LoadInst>(U);
if (!LI || !LI->isSimple())
return false;

Expand All @@ -1370,24 +1363,10 @@ static void speculateSelectInstLoads(SelectInst &SI) {
Value *FV = SI.getFalseValue();
// Replace the loads of the select with a select of two loads.
while (!SI.use_empty()) {
LoadInst *LI;
BitCastInst *BC = dyn_cast<BitCastInst>(SI.user_back());
if (BC) {
assert(BC->hasOneUse() && "Bitcast should have a single use.");
LI = cast<LoadInst>(BC->user_back());
} else {
LI = cast<LoadInst>(SI.user_back());
}

LoadInst *LI = cast<LoadInst>(SI.user_back());
assert(LI->isSimple() && "We only speculate simple loads");

IRB.SetInsertPoint(LI);
if (BC) {
// Cast the operands to bitcast's target type.
TV = IRB.CreateBitCast(TV, BC->getType(), TV->getName() + ".sroa.cast");
FV = IRB.CreateBitCast(FV, BC->getType(), FV->getName() + ".sroa.cast");
}

LoadInst *TL = IRB.CreateLoad(LI->getType(), TV,
LI->getName() + ".sroa.speculate.load.true");
LoadInst *FL = IRB.CreateLoad(LI->getType(), FV,
Expand All @@ -1411,8 +1390,6 @@ static void speculateSelectInstLoads(SelectInst &SI) {
LLVM_DEBUG(dbgs() << " speculated to: " << *V << "\n");
LI->replaceAllUsesWith(V);
LI->eraseFromParent();
if (BC)
BC->eraseFromParent();
}
SI.eraseFromParent();
}
Expand Down
19 changes: 14 additions & 5 deletions llvm/test/Transforms/SROA/phi-and-select.ll
Expand Up @@ -60,14 +60,23 @@ entry:
ret i32 %result
}

; If bitcast isn't considered a safe phi/select use, the alloca
; remains as an array.
; FIXME: Why isn't this identical to test2?
define float @test2_bitcast() {
; CHECK-LABEL: @test2_bitcast(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[COND:%.*]] = icmp sle i32 0, 1
; CHECK-NEXT: [[TMP0:%.*]] = bitcast i32 1 to float
; CHECK-NEXT: [[TMP1:%.*]] = bitcast i32 0 to float
; CHECK-NEXT: [[RESULT_SROA_SPECULATED:%.*]] = select i1 [[COND]], float [[TMP0]], float [[TMP1]]
; CHECK-NEXT: ret float [[RESULT_SROA_SPECULATED]]
; CHECK-NEXT: [[A_SROA_0:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[A_SROA_3:%.*]] = alloca i32, align 4
; CHECK-NEXT: store i32 0, i32* [[A_SROA_0]], align 4
; CHECK-NEXT: store i32 1, i32* [[A_SROA_3]], align 4
; CHECK-NEXT: [[A_SROA_0_0_A_SROA_0_0_V0:%.*]] = load i32, i32* [[A_SROA_0]], align 4
; CHECK-NEXT: [[A_SROA_3_0_A_SROA_3_4_V1:%.*]] = load i32, i32* [[A_SROA_3]], align 4
; CHECK-NEXT: [[COND:%.*]] = icmp sle i32 [[A_SROA_0_0_A_SROA_0_0_V0]], [[A_SROA_3_0_A_SROA_3_4_V1]]
; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[COND]], i32* [[A_SROA_3]], i32* [[A_SROA_0]]
; CHECK-NEXT: [[SELECT_BC:%.*]] = bitcast i32* [[SELECT]] to float*
; CHECK-NEXT: [[RESULT:%.*]] = load float, float* [[SELECT_BC]], align 4
; CHECK-NEXT: ret float [[RESULT]]
;
entry:
%a = alloca [2 x i32]
Expand Down
35 changes: 0 additions & 35 deletions llvm/test/Transforms/SROA/select-load.ll

This file was deleted.

0 comments on commit fcf2d5f

Please sign in to comment.