[MergedLoadStoreMotion] Don't transform across may-throw calls

It is unsafe to hoist a load before a function call which may throw, the
throw might prevent a pointer dereference.

Likewise, it is unsafe to sink a store after a call which may throw.
The caller might be able to observe the difference.

This fixes PR27858.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@270828 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
David Majnemer
2016-05-26 07:11:09 +00:00
parent b09e1d33f6
commit 8ac7be5005
3 changed files with 90 additions and 25 deletions
+32 -24
View File
@@ -79,7 +79,6 @@
#include "llvm/Analysis/Loads.h"
#include "llvm/Analysis/MemoryBuiltins.h"
#include "llvm/Analysis/MemoryDependenceAnalysis.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/Support/Debug.h"
@@ -114,7 +113,6 @@ private:
// This transformation requires dominator postdominator info
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesCFG();
AU.addRequired<TargetLibraryInfoWrapperPass>();
AU.addRequired<AAResultsWrapperPass>();
AU.addPreserved<GlobalsAAWrapperPass>();
AU.addPreserved<MemoryDependenceWrapperPass>();
@@ -130,9 +128,9 @@ private:
BasicBlock *getDiamondTail(BasicBlock *BB);
bool isDiamondHead(BasicBlock *BB);
// Routines for hoisting loads
bool isLoadHoistBarrierInRange(const Instruction& Start,
const Instruction& End,
LoadInst* LI);
bool isLoadHoistBarrierInRange(const Instruction &Start,
const Instruction &End, LoadInst *LI,
bool SafeToLoadUnconditionally);
LoadInst *canHoistFromBlock(BasicBlock *BB, LoadInst *LI);
void hoistInstruction(BasicBlock *BB, Instruction *HoistCand,
Instruction *ElseInst);
@@ -166,7 +164,6 @@ FunctionPass *llvm::createMergedLoadStoreMotionPass() {
INITIALIZE_PASS_BEGIN(MergedLoadStoreMotion, "mldst-motion",
"MergedLoadStoreMotion", false, false)
INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
INITIALIZE_PASS_END(MergedLoadStoreMotion, "mldst-motion",
@@ -229,9 +226,14 @@ bool MergedLoadStoreMotion::isDiamondHead(BasicBlock *BB) {
/// being loaded or protect against the load from happening
/// it is considered a hoist barrier.
///
bool MergedLoadStoreMotion::isLoadHoistBarrierInRange(const Instruction& Start,
const Instruction& End,
LoadInst* LI) {
bool MergedLoadStoreMotion::isLoadHoistBarrierInRange(
const Instruction &Start, const Instruction &End, LoadInst *LI,
bool SafeToLoadUnconditionally) {
if (!SafeToLoadUnconditionally)
for (const Instruction &Inst :
make_range(Start.getIterator(), End.getIterator()))
if (Inst.mayThrow())
return true;
MemoryLocation Loc = MemoryLocation::get(LI);
return AA->canInstructionRangeModRef(Start, End, Loc, MRI_Mod);
}
@@ -245,23 +247,28 @@ bool MergedLoadStoreMotion::isLoadHoistBarrierInRange(const Instruction& Start,
///
LoadInst *MergedLoadStoreMotion::canHoistFromBlock(BasicBlock *BB1,
LoadInst *Load0) {
BasicBlock *BB0 = Load0->getParent();
BasicBlock *Head = BB0->getSinglePredecessor();
bool SafeToLoadUnconditionally = isSafeToLoadUnconditionally(
Load0->getPointerOperand(), Load0->getAlignment(),
Load0->getModule()->getDataLayout(),
/*ScanFrom=*/Head->getTerminator());
for (BasicBlock::iterator BBI = BB1->begin(), BBE = BB1->end(); BBI != BBE;
++BBI) {
Instruction *Inst = &*BBI;
// Only merge and hoist loads when their result in used only in BB
if (!isa<LoadInst>(Inst) || Inst->isUsedOutsideOfBlock(BB1))
auto *Load1 = dyn_cast<LoadInst>(Inst);
if (!Load1 || Inst->isUsedOutsideOfBlock(BB1))
continue;
auto *Load1 = cast<LoadInst>(Inst);
BasicBlock *BB0 = Load0->getParent();
MemoryLocation Loc0 = MemoryLocation::get(Load0);
MemoryLocation Loc1 = MemoryLocation::get(Load1);
if (AA->isMustAlias(Loc0, Loc1) && Load0->isSameOperationAs(Load1) &&
!isLoadHoistBarrierInRange(BB1->front(), *Load1, Load1) &&
!isLoadHoistBarrierInRange(BB0->front(), *Load0, Load0)) {
!isLoadHoistBarrierInRange(BB1->front(), *Load1, Load1,
SafeToLoadUnconditionally) &&
!isLoadHoistBarrierInRange(BB0->front(), *Load0, Load0,
SafeToLoadUnconditionally)) {
return Load1;
}
}
@@ -387,6 +394,10 @@ bool MergedLoadStoreMotion::mergeLoads(BasicBlock *BB) {
bool MergedLoadStoreMotion::isStoreSinkBarrierInRange(const Instruction &Start,
const Instruction &End,
MemoryLocation Loc) {
for (const Instruction &Inst :
make_range(Start.getIterator(), End.getIterator()))
if (Inst.mayThrow())
return true;
return AA->canInstructionRangeModRef(Start, End, Loc, MRI_ModRef);
}
@@ -403,18 +414,15 @@ StoreInst *MergedLoadStoreMotion::canSinkFromBlock(BasicBlock *BB1,
RBI != RBE; ++RBI) {
Instruction *Inst = &*RBI;
if (!isa<StoreInst>(Inst))
continue;
StoreInst *Store1 = cast<StoreInst>(Inst);
auto *Store1 = dyn_cast<StoreInst>(Inst);
if (!Store1)
continue;
MemoryLocation Loc0 = MemoryLocation::get(Store0);
MemoryLocation Loc1 = MemoryLocation::get(Store1);
if (AA->isMustAlias(Loc0, Loc1) && Store0->isSameOperationAs(Store1) &&
!isStoreSinkBarrierInRange(*(std::next(BasicBlock::iterator(Store1))),
BB1->back(), Loc1) &&
!isStoreSinkBarrierInRange(*(std::next(BasicBlock::iterator(Store0))),
BB0->back(), Loc0)) {
!isStoreSinkBarrierInRange(*Store1->getNextNode(), BB1->back(), Loc1) &&
!isStoreSinkBarrierInRange(*Store0->getNextNode(), BB0->back(), Loc0)) {
return Store1;
}
}
+57
View File
@@ -0,0 +1,57 @@
; RUN: opt -basicaa -memdep -mldst-motion -S < %s | FileCheck %s
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
@r = common global i32 0, align 4
@s = common global i32 0, align 4
; CHECK-LABEL: define void @test1(
define void @test1(i1 %cmp, i32* noalias %p) {
entry:
br i1 %cmp, label %if.then, label %if.else
if.then: ; preds = %entry
call void @may_throw()
%arrayidx = getelementptr inbounds i32, i32* %p, i64 1
%0 = load i32, i32* %arrayidx, align 4
store i32 %0, i32* @r, align 4
br label %if.end
; CHECK: call void @may_throw()
; CHECK-NEXT: %[[gep:.*]] = getelementptr inbounds i32, i32* %p, i64 1
; CHECK-NEXT: %[[load:.*]] = load i32, i32* %[[gep]], align 4
; CHECK-NEXT: store i32 %[[load]], i32* @r, align 4
if.else: ; preds = %entry
%arrayidx1 = getelementptr inbounds i32, i32* %p, i64 1
%1 = load i32, i32* %arrayidx1, align 4
store i32 %1, i32* @s, align 4
br label %if.end
if.end: ; preds = %if.else, %if.then
ret void
}
; CHECK-LABEL: define void @test2(
define void @test2(i1 %cmp, i32* noalias %p) {
entry:
br i1 %cmp, label %if.then, label %if.else
if.then: ; preds = %entry
%arrayidx = getelementptr inbounds i32, i32* %p, i64 1
store i32 1, i32* %arrayidx, align 4
call void @may_throw()
; CHECK: %[[gep:.*]] = getelementptr inbounds i32, i32* %p, i64 1
; CHECK-NEXT: store i32 1, i32* %[[gep]], align 4
; CHECK-NEXT: call void @may_throw()
br label %if.end
if.else: ; preds = %entry
%arrayidx1 = getelementptr inbounds i32, i32* %p, i64 1
store i32 2, i32* %arrayidx1, align 4
br label %if.end
if.end: ; preds = %if.else, %if.then
ret void
}
declare void @may_throw()
@@ -33,7 +33,7 @@ if.else: ; preds = %entry
%p3 = getelementptr inbounds %struct.node, %struct.node* %node.017, i32 0, i32 6
; CHECK-NOT: store i32
store i32 %add, i32* %p3, align 4
call i32 @foo(i32 5) ;not a barrier
call i32 @foo(i32 5) nounwind ;not a barrier
br label %if.end
; CHECK: if.end