#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"
// 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>();
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);
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",
/// 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);
}
///
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;
}
}
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);
}
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;
}
}
--- /dev/null
+; 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()