Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit f45aefe

Browse files
author
Jun Bum Lim
committed
[JumpThread] Use AA in SimplifyPartiallyRedundantLoad()
Summary: Use AA when scanning to find an available load value. Reviewers: rengolin, mcrosier, hfinkel, trentxintong, dberlin Reviewed By: rengolin, dberlin Subscribers: aemerson, dberlin, llvm-commits Differential Revision: https://reviews.llvm.org/D30352 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@297284 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 48a60dc commit f45aefe

File tree

3 files changed

+134
-13
lines changed

3 files changed

+134
-13
lines changed

Diff for: include/llvm/Transforms/Scalar/JumpThreading.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/ADT/DenseSet.h"
1818
#include "llvm/ADT/SmallPtrSet.h"
1919
#include "llvm/ADT/SmallSet.h"
20+
#include "llvm/Analysis/AliasAnalysis.h"
2021
#include "llvm/Analysis/BlockFrequencyInfo.h"
2122
#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
2223
#include "llvm/Analysis/BranchProbabilityInfo.h"
@@ -60,6 +61,7 @@ enum ConstantPreference { WantInteger, WantBlockAddress };
6061
class JumpThreadingPass : public PassInfoMixin<JumpThreadingPass> {
6162
TargetLibraryInfo *TLI;
6263
LazyValueInfo *LVI;
64+
AliasAnalysis *AA;
6365
std::unique_ptr<BlockFrequencyInfo> BFI;
6466
std::unique_ptr<BranchProbabilityInfo> BPI;
6567
bool HasProfileData = false;
@@ -90,7 +92,8 @@ class JumpThreadingPass : public PassInfoMixin<JumpThreadingPass> {
9092

9193
// Glue for old PM.
9294
bool runImpl(Function &F, TargetLibraryInfo *TLI_, LazyValueInfo *LVI_,
93-
bool HasProfileData_, std::unique_ptr<BlockFrequencyInfo> BFI_,
95+
AliasAnalysis *AA_, bool HasProfileData_,
96+
std::unique_ptr<BlockFrequencyInfo> BFI_,
9497
std::unique_ptr<BranchProbabilityInfo> BPI_);
9598

9699
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);

Diff for: lib/Transforms/Scalar/JumpThreading.cpp

+20-11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/ADT/DenseSet.h"
1818
#include "llvm/ADT/STLExtras.h"
1919
#include "llvm/ADT/Statistic.h"
20+
#include "llvm/Analysis/AliasAnalysis.h"
2021
#include "llvm/Analysis/GlobalsModRef.h"
2122
#include "llvm/Analysis/CFG.h"
2223
#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
@@ -91,6 +92,7 @@ namespace {
9192
bool runOnFunction(Function &F) override;
9293

9394
void getAnalysisUsage(AnalysisUsage &AU) const override {
95+
AU.addRequired<AAResultsWrapperPass>();
9496
AU.addRequired<LazyValueInfoWrapperPass>();
9597
AU.addPreserved<LazyValueInfoWrapperPass>();
9698
AU.addPreserved<GlobalsAAWrapperPass>();
@@ -106,6 +108,7 @@ INITIALIZE_PASS_BEGIN(JumpThreading, "jump-threading",
106108
"Jump Threading", false, false)
107109
INITIALIZE_PASS_DEPENDENCY(LazyValueInfoWrapperPass)
108110
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
111+
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
109112
INITIALIZE_PASS_END(JumpThreading, "jump-threading",
110113
"Jump Threading", false, false)
111114

@@ -123,6 +126,7 @@ bool JumpThreading::runOnFunction(Function &F) {
123126
return false;
124127
auto TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
125128
auto LVI = &getAnalysis<LazyValueInfoWrapperPass>().getLVI();
129+
auto AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
126130
std::unique_ptr<BlockFrequencyInfo> BFI;
127131
std::unique_ptr<BranchProbabilityInfo> BPI;
128132
bool HasProfileData = F.getEntryCount().hasValue();
@@ -131,7 +135,8 @@ bool JumpThreading::runOnFunction(Function &F) {
131135
BPI.reset(new BranchProbabilityInfo(F, LI));
132136
BFI.reset(new BlockFrequencyInfo(F, *BPI, LI));
133137
}
134-
return Impl.runImpl(F, TLI, LVI, HasProfileData, std::move(BFI),
138+
139+
return Impl.runImpl(F, TLI, LVI, AA, HasProfileData, std::move(BFI),
135140
std::move(BPI));
136141
}
137142

@@ -140,6 +145,8 @@ PreservedAnalyses JumpThreadingPass::run(Function &F,
140145

141146
auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
142147
auto &LVI = AM.getResult<LazyValueAnalysis>(F);
148+
auto &AA = AM.getResult<AAManager>(F);
149+
143150
std::unique_ptr<BlockFrequencyInfo> BFI;
144151
std::unique_ptr<BranchProbabilityInfo> BPI;
145152
bool HasProfileData = F.getEntryCount().hasValue();
@@ -148,8 +155,9 @@ PreservedAnalyses JumpThreadingPass::run(Function &F,
148155
BPI.reset(new BranchProbabilityInfo(F, LI));
149156
BFI.reset(new BlockFrequencyInfo(F, *BPI, LI));
150157
}
151-
bool Changed =
152-
runImpl(F, &TLI, &LVI, HasProfileData, std::move(BFI), std::move(BPI));
158+
159+
bool Changed = runImpl(F, &TLI, &LVI, &AA, HasProfileData, std::move(BFI),
160+
std::move(BPI));
153161

154162
if (!Changed)
155163
return PreservedAnalyses::all();
@@ -159,13 +167,15 @@ PreservedAnalyses JumpThreadingPass::run(Function &F,
159167
}
160168

161169
bool JumpThreadingPass::runImpl(Function &F, TargetLibraryInfo *TLI_,
162-
LazyValueInfo *LVI_, bool HasProfileData_,
170+
LazyValueInfo *LVI_, AliasAnalysis *AA_,
171+
bool HasProfileData_,
163172
std::unique_ptr<BlockFrequencyInfo> BFI_,
164173
std::unique_ptr<BranchProbabilityInfo> BPI_) {
165174

166175
DEBUG(dbgs() << "Jump threading on function '" << F.getName() << "'\n");
167176
TLI = TLI_;
168177
LVI = LVI_;
178+
AA = AA_;
169179
BFI.reset();
170180
BPI.reset();
171181
// When profile data is available, we need to update edge weights after
@@ -953,8 +963,8 @@ bool JumpThreadingPass::SimplifyPartiallyRedundantLoad(LoadInst *LI) {
953963
// the entry to its block.
954964
BasicBlock::iterator BBIt(LI);
955965
bool IsLoadCSE;
956-
if (Value *AvailableVal =
957-
FindAvailableLoadedValue(LI, LoadBB, BBIt, DefMaxInstsToScan, nullptr, &IsLoadCSE)) {
966+
if (Value *AvailableVal = FindAvailableLoadedValue(
967+
LI, LoadBB, BBIt, DefMaxInstsToScan, AA, &IsLoadCSE)) {
958968
// If the value of the load is locally available within the block, just use
959969
// it. This frequently occurs for reg2mem'd allocas.
960970

@@ -1001,9 +1011,8 @@ bool JumpThreadingPass::SimplifyPartiallyRedundantLoad(LoadInst *LI) {
10011011
// Scan the predecessor to see if the value is available in the pred.
10021012
BBIt = PredBB->end();
10031013
unsigned NumScanedInst = 0;
1004-
Value *PredAvailable =
1005-
FindAvailableLoadedValue(LI, PredBB, BBIt, DefMaxInstsToScan, nullptr,
1006-
&IsLoadCSE, &NumScanedInst);
1014+
Value *PredAvailable = FindAvailableLoadedValue(
1015+
LI, PredBB, BBIt, DefMaxInstsToScan, AA, &IsLoadCSE, &NumScanedInst);
10071016

10081017
// If PredBB has a single predecessor, continue scanning through the single
10091018
// predecessor.
@@ -1014,8 +1023,8 @@ bool JumpThreadingPass::SimplifyPartiallyRedundantLoad(LoadInst *LI) {
10141023
if (SinglePredBB) {
10151024
BBIt = SinglePredBB->end();
10161025
PredAvailable = FindAvailableLoadedValue(
1017-
LI, SinglePredBB, BBIt, (DefMaxInstsToScan - NumScanedInst),
1018-
nullptr, &IsLoadCSE, &NumScanedInst);
1026+
LI, SinglePredBB, BBIt, (DefMaxInstsToScan - NumScanedInst), AA,
1027+
&IsLoadCSE, &NumScanedInst);
10191028
}
10201029
}
10211030

Diff for: test/Transforms/JumpThreading/thread-loads.ll

+110-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; RUN: opt < %s -jump-threading -S | FileCheck %s
2-
; RUN: opt < %s -passes=jump-threading -S | FileCheck %s
2+
; RUN: opt < %s -aa-pipeline=basic-aa -passes=jump-threading -S | FileCheck %s
33

44
target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
55
target triple = "i386-apple-darwin7"
@@ -302,6 +302,115 @@ ret2:
302302
ret void
303303
}
304304

305+
define i32 @fn_noalias(i1 %c2,i64* noalias %P, i64* noalias %P2) {
306+
; CHECK-LABEL: @fn_noalias
307+
; CHECK-LABEL: cond1:
308+
; CHECK: %[[LD1:.*]] = load i64, i64* %P
309+
; CHECK: br i1 %c, label %[[THREAD:.*]], label %end
310+
; CHECK-LABEL: cond2:
311+
; CHECK: %[[LD2:.*]] = load i64, i64* %P
312+
; CHECK-LABEL: cond3:
313+
; CHECK: %[[PHI:.*]] = phi i64 [ %[[LD1]], %[[THREAD]] ], [ %[[LD2]], %cond2 ]
314+
; CHECK: call void @fn3(i64 %[[PHI]])
315+
entry:
316+
br i1 %c2, label %cond2, label %cond1
317+
318+
cond1:
319+
%l1 = load i64, i64* %P
320+
store i64 42, i64* %P2
321+
%c = icmp eq i64 %l1, 0
322+
br i1 %c, label %cond2, label %end
323+
324+
cond2:
325+
%l2 = load i64, i64* %P
326+
call void @fn2(i64 %l2)
327+
%c3 = icmp eq i64 %l2, 0
328+
br i1 %c3, label %cond3, label %end
329+
330+
cond3:
331+
call void @fn3(i64 %l2)
332+
br label %end
333+
334+
end:
335+
ret i32 0
336+
}
337+
338+
; This tests if we can thread from %sw.bb.i to %do.body.preheader.i67 through
339+
; %sw.bb21.i. To make this happen, %l2 should be detected as a partically
340+
; redundant load with %l3 across the store to %phase in %sw.bb21.i.
341+
342+
%struct.NEXT_MOVE = type { i32, i32, i32* }
343+
@hash_move = unnamed_addr global [65 x i32] zeroinitializer, align 4
344+
@current_move = internal global [65 x i32] zeroinitializer, align 4
345+
@last = internal unnamed_addr global [65 x i32*] zeroinitializer, align 8
346+
@next_status = internal unnamed_addr global [65 x %struct.NEXT_MOVE] zeroinitializer, align 8
347+
define fastcc i32 @Search(i64 %idxprom.i, i64 %idxprom.i89, i32 %c) {
348+
; CHECK-LABEL: @Search
349+
; CHECK-LABEL: sw.bb.i:
350+
; CHECK: %[[LD1:.*]] = load i32, i32* %arrayidx185, align 4
351+
; CHECK: %[[C1:.*]] = icmp eq i32 %[[LD1]], 0
352+
; CHECK: br i1 %[[C1]], label %sw.bb21.i.thread, label %if.then.i64
353+
; CHECK-LABEL: sw.bb21.i.thread:
354+
; CHECK: br label %[[THREAD_TO:.*]]
355+
; CHECK-LABEL: sw.bb21.i:
356+
; CHECK: %[[LD2:.*]] = load i32, i32* %arrayidx185, align 4
357+
; CHECK: %[[C2:.*]] = icmp eq i32 %[[LD2]], 0
358+
; CHECK:br i1 %[[C2]], label %[[THREAD_TO]], label %cleanup
359+
entry:
360+
%arrayidx185 = getelementptr inbounds [65 x i32], [65 x i32]* @hash_move, i64 0, i64 %idxprom.i
361+
%arrayidx307 = getelementptr inbounds [65 x i32], [65 x i32]* @current_move, i64 0, i64 %idxprom.i
362+
%arrayidx89 = getelementptr inbounds [65 x i32*], [65 x i32*]* @last, i64 0, i64 %idxprom.i
363+
%phase = getelementptr inbounds [65 x %struct.NEXT_MOVE], [65 x %struct.NEXT_MOVE]* @next_status, i64 0, i64 %idxprom.i, i32 0
364+
br label %cond.true282
365+
366+
cond.true282:
367+
switch i32 %c, label %sw.default.i [
368+
i32 1, label %sw.bb.i
369+
i32 0, label %sw.bb21.i
370+
]
371+
372+
sw.default.i:
373+
br label %cleanup
374+
375+
sw.bb.i:
376+
%call.i62 = call fastcc i32* @GenerateCheckEvasions()
377+
store i32* %call.i62, i32** %arrayidx89, align 8
378+
%l2 = load i32, i32* %arrayidx185, align 4
379+
%tobool.i63 = icmp eq i32 %l2, 0
380+
br i1 %tobool.i63, label %sw.bb21.i, label %if.then.i64
381+
382+
if.then.i64: ; preds = %sw.bb.i
383+
store i32 7, i32* %phase, align 8
384+
store i32 %l2, i32* %arrayidx307, align 4
385+
%call16.i = call fastcc i32 @ValidMove(i32 %l2)
386+
%tobool17.i = icmp eq i32 %call16.i, 0
387+
br i1 %tobool17.i, label %if.else.i65, label %cleanup
388+
389+
if.else.i65:
390+
call void @f65()
391+
br label %sw.bb21.i
392+
393+
sw.bb21.i:
394+
store i32 10, i32* %phase, align 8
395+
%l3= load i32, i32* %arrayidx185, align 4
396+
%tobool27.i = icmp eq i32 %l3, 0
397+
br i1 %tobool27.i, label %do.body.preheader.i67, label %cleanup
398+
399+
do.body.preheader.i67:
400+
call void @f67()
401+
ret i32 67
402+
403+
cleanup:
404+
call void @Cleanup()
405+
ret i32 0
406+
}
407+
408+
declare fastcc i32* @GenerateCheckEvasions()
409+
declare fastcc i32 @ValidMove(i32 %move)
410+
declare void @f67()
411+
declare void @Cleanup()
412+
declare void @f65()
413+
305414
define i32 @fn_SinglePred(i1 %c2,i64* %P) {
306415
; CHECK-LABEL: @fn_SinglePred
307416
; CHECK-LABEL: entry:

0 commit comments

Comments
 (0)