Skip to content

Commit 30cdf1e

Browse files
authored
[SimplifyCFG] Pass context instruction to isSafeToSpeculativelyExecute() (#109132)
Pass speculation target and assumption cache to isSafeToSpeculativelyExecute() calls. This allows speculating based on dereferenceable/align assumptions, but the primary motivation here is to avoid regressions from planned changes to fix #108854.
1 parent 752e103 commit 30cdf1e

File tree

2 files changed

+20
-31
lines changed

2 files changed

+20
-31
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,6 @@ static void addPredecessorToBlock(BasicBlock *Succ, BasicBlock *NewPred,
398398
/// expensive.
399399
static InstructionCost computeSpeculationCost(const User *I,
400400
const TargetTransformInfo &TTI) {
401-
assert((!isa<Instruction>(I) ||
402-
isSafeToSpeculativelyExecute(cast<Instruction>(I))) &&
403-
"Instruction is not safe to speculatively execute!");
404401
return TTI.getInstructionCost(I, TargetTransformInfo::TCK_SizeAndLatency);
405402
}
406403

@@ -421,12 +418,11 @@ static InstructionCost computeSpeculationCost(const User *I,
421418
/// After this function returns, Cost is increased by the cost of
422419
/// V plus its non-dominating operands. If that cost is greater than
423420
/// Budget, false is returned and Cost is undefined.
424-
static bool dominatesMergePoint(Value *V, BasicBlock *BB,
421+
static bool dominatesMergePoint(Value *V, BasicBlock *BB, Instruction *InsertPt,
425422
SmallPtrSetImpl<Instruction *> &AggressiveInsts,
426-
InstructionCost &Cost,
427-
InstructionCost Budget,
423+
InstructionCost &Cost, InstructionCost Budget,
428424
const TargetTransformInfo &TTI,
429-
unsigned Depth = 0) {
425+
AssumptionCache *AC, unsigned Depth = 0) {
430426
// It is possible to hit a zero-cost cycle (phi/gep instructions for example),
431427
// so limit the recursion depth.
432428
// TODO: While this recursion limit does prevent pathological behavior, it
@@ -461,7 +457,7 @@ static bool dominatesMergePoint(Value *V, BasicBlock *BB,
461457
// Okay, it looks like the instruction IS in the "condition". Check to
462458
// see if it's a cheap instruction to unconditionally compute, and if it
463459
// only uses stuff defined outside of the condition. If so, hoist it out.
464-
if (!isSafeToSpeculativelyExecute(I))
460+
if (!isSafeToSpeculativelyExecute(I, InsertPt, AC))
465461
return false;
466462

467463
Cost += computeSpeculationCost(I, TTI);
@@ -480,8 +476,8 @@ static bool dominatesMergePoint(Value *V, BasicBlock *BB,
480476
// Okay, we can only really hoist these out if their operands do
481477
// not take us over the cost threshold.
482478
for (Use &Op : I->operands())
483-
if (!dominatesMergePoint(Op, BB, AggressiveInsts, Cost, Budget, TTI,
484-
Depth + 1))
479+
if (!dominatesMergePoint(Op, BB, InsertPt, AggressiveInsts, Cost, Budget,
480+
TTI, AC, Depth + 1))
485481
return false;
486482
// Okay, it's safe to do this! Remember this instruction.
487483
AggressiveInsts.insert(I);
@@ -3140,7 +3136,8 @@ bool SimplifyCFGOpt::speculativelyExecuteBB(BranchInst *BI,
31403136
return false;
31413137

31423138
// Don't hoist the instruction if it's unsafe or expensive.
3143-
if (!IsSafeCheapLoadStore && !isSafeToSpeculativelyExecute(&I) &&
3139+
if (!IsSafeCheapLoadStore &&
3140+
!isSafeToSpeculativelyExecute(&I, BI, Options.AC) &&
31443141
!(HoistCondStores && !SpeculatedStoreValue &&
31453142
(SpeculatedStoreValue =
31463143
isSafeToSpeculateStore(&I, BB, ThenBB, EndBB))))
@@ -3651,7 +3648,8 @@ static bool foldCondBranchOnValueKnownInPredecessor(BranchInst *BI,
36513648
/// Given a BB that starts with the specified two-entry PHI node,
36523649
/// see if we can eliminate it.
36533650
static bool foldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
3654-
DomTreeUpdater *DTU, const DataLayout &DL,
3651+
DomTreeUpdater *DTU, AssumptionCache *AC,
3652+
const DataLayout &DL,
36553653
bool SpeculateUnpredictables) {
36563654
// Ok, this is a two entry PHI node. Check to see if this is a simple "if
36573655
// statement", which has a very simple dominance structure. Basically, we
@@ -3741,10 +3739,10 @@ static bool foldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
37413739
continue;
37423740
}
37433741

3744-
if (!dominatesMergePoint(PN->getIncomingValue(0), BB, AggressiveInsts,
3745-
Cost, Budget, TTI) ||
3746-
!dominatesMergePoint(PN->getIncomingValue(1), BB, AggressiveInsts,
3747-
Cost, Budget, TTI))
3742+
if (!dominatesMergePoint(PN->getIncomingValue(0), BB, DomBI,
3743+
AggressiveInsts, Cost, Budget, TTI, AC) ||
3744+
!dominatesMergePoint(PN->getIncomingValue(1), BB, DomBI,
3745+
AggressiveInsts, Cost, Budget, TTI, AC))
37483746
return Changed;
37493747
}
37503748

@@ -8116,7 +8114,7 @@ bool SimplifyCFGOpt::simplifyOnce(BasicBlock *BB) {
81168114
// eliminate it, do so now.
81178115
if (auto *PN = dyn_cast<PHINode>(BB->begin()))
81188116
if (PN->getNumIncomingValues() == 2)
8119-
if (foldTwoEntryPHINode(PN, TTI, DTU, DL,
8117+
if (foldTwoEntryPHINode(PN, TTI, DTU, Options.AC, DL,
81208118
Options.SpeculateUnpredictables))
81218119
return true;
81228120
}

llvm/test/Transforms/SimplifyCFG/speculate-derefable-load.ll

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@
44
define i64 @align_deref_align(i1 %c, ptr %p) {
55
; CHECK-LABEL: define i64 @align_deref_align(
66
; CHECK-SAME: i1 [[C:%.*]], ptr [[P:%.*]]) {
7-
; CHECK-NEXT: [[ENTRY:.*]]:
7+
; CHECK-NEXT: [[ENTRY:.*:]]
88
; CHECK-NEXT: call void @llvm.assume(i1 true) [ "dereferenceable"(ptr [[P]], i64 8), "align"(ptr [[P]], i64 8) ]
9-
; CHECK-NEXT: br i1 [[C]], label %[[IF:.*]], label %[[EXIT:.*]]
10-
; CHECK: [[IF]]:
119
; CHECK-NEXT: [[V:%.*]] = load i64, ptr [[P]], align 8
12-
; CHECK-NEXT: br label %[[EXIT]]
13-
; CHECK: [[EXIT]]:
14-
; CHECK-NEXT: [[RES:%.*]] = phi i64 [ [[V]], %[[IF]] ], [ 0, %[[ENTRY]] ]
10+
; CHECK-NEXT: [[RES:%.*]] = select i1 [[C]], i64 [[V]], i64 0
1511
; CHECK-NEXT: ret i64 [[RES]]
1612
;
1713
entry:
@@ -30,17 +26,12 @@ exit:
3026
define i64 @assume_deref_align2(i1 %c1, i32 %x, ptr %p) {
3127
; CHECK-LABEL: define i64 @assume_deref_align2(
3228
; CHECK-SAME: i1 [[C1:%.*]], i32 [[X:%.*]], ptr [[P:%.*]]) {
33-
; CHECK-NEXT: [[ENTRY:.*]]:
29+
; CHECK-NEXT: [[ENTRY:.*:]]
3430
; CHECK-NEXT: call void @llvm.assume(i1 true) [ "dereferenceable"(ptr [[P]], i64 8), "align"(ptr [[P]], i64 8) ]
35-
; CHECK-NEXT: br i1 [[C1]], label %[[IF1:.*]], label %[[EXIT:.*]]
36-
; CHECK: [[IF1]]:
3731
; CHECK-NEXT: [[C2:%.*]] = icmp ugt i32 [[X]], 10
38-
; CHECK-NEXT: br i1 [[C2]], label %[[IF2:.*]], label %[[EXIT]]
39-
; CHECK: [[IF2]]:
4032
; CHECK-NEXT: [[V:%.*]] = load i64, ptr [[P]], align 8
41-
; CHECK-NEXT: br label %[[EXIT]]
42-
; CHECK: [[EXIT]]:
43-
; CHECK-NEXT: [[RES:%.*]] = phi i64 [ [[V]], %[[IF2]] ], [ 1, %[[IF1]] ], [ 0, %[[ENTRY]] ]
33+
; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[C2]], i64 [[V]], i64 1
34+
; CHECK-NEXT: [[RES:%.*]] = select i1 [[C1]], i64 [[SPEC_SELECT]], i64 0
4435
; CHECK-NEXT: ret i64 [[RES]]
4536
;
4637
entry:

0 commit comments

Comments
 (0)