Skip to content

Commit f2284e3

Browse files
committed
[Sink] Optimize/simplify sink candidate finding with nearest common dominator
For an instruction in the basic block BB, SinkingPass enumerates basic blocks dominated by BB and BB's successors. For each enumerated basic block, SinkingPass uses `AllUsesDominatedByBlock` to check whether the basic block dominates all of the instruction's users. This is inefficient. Use the nearest common dominator of all users to avoid enumerating the candidate. The nearest common dominator may be in a parent loop which is not beneficial. In that case, find the ancestors in the dominator tree. In the case that the instruction has no user, with this change we will not perform unnecessary move. This causes some amdgpu test changes. A stage-2 x86-64 clang is a byte identical with this change.
1 parent 389fd30 commit f2284e3

File tree

6 files changed

+106
-126
lines changed

6 files changed

+106
-126
lines changed

llvm/lib/Transforms/Scalar/Sink.cpp

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,6 @@ using namespace llvm;
3232
STATISTIC(NumSunk, "Number of instructions sunk");
3333
STATISTIC(NumSinkIter, "Number of sinking iterations");
3434

35-
/// AllUsesDominatedByBlock - Return true if all uses of the specified value
36-
/// occur in blocks dominated by the specified block.
37-
static bool AllUsesDominatedByBlock(Instruction *Inst, BasicBlock *BB,
38-
DominatorTree &DT) {
39-
// Ignoring debug uses is necessary so debug info doesn't affect the code.
40-
// This may leave a referencing dbg_value in the original block, before
41-
// the definition of the vreg. Dwarf generator handles this although the
42-
// user might not get the right info at runtime.
43-
for (Use &U : Inst->uses()) {
44-
// Determine the block of the use.
45-
Instruction *UseInst = cast<Instruction>(U.getUser());
46-
BasicBlock *UseBlock = UseInst->getParent();
47-
if (PHINode *PN = dyn_cast<PHINode>(UseInst)) {
48-
// PHI nodes use the operand in the predecessor block, not the block with
49-
// the PHI.
50-
unsigned Num = PHINode::getIncomingValueNumForOperand(U.getOperandNo());
51-
UseBlock = PN->getIncomingBlock(Num);
52-
}
53-
// Check that it dominates.
54-
if (!DT.dominates(BB, UseBlock))
55-
return false;
56-
}
57-
return true;
58-
}
59-
6035
static bool isSafeToMove(Instruction *Inst, AliasAnalysis &AA,
6136
SmallPtrSetImpl<Instruction *> &Stores) {
6237

@@ -97,11 +72,6 @@ static bool IsAcceptableTarget(Instruction *Inst, BasicBlock *SuccToSinkTo,
9772
assert(Inst && "Instruction to be sunk is null");
9873
assert(SuccToSinkTo && "Candidate sink target is null");
9974

100-
// It is not possible to sink an instruction into its own block. This can
101-
// happen with loops.
102-
if (Inst->getParent() == SuccToSinkTo)
103-
return false;
104-
10575
// It's never legal to sink an instruction into a block which terminates in an
10676
// EH-pad.
10777
if (SuccToSinkTo->getTerminator()->isExceptionalTerminator())
@@ -129,9 +99,7 @@ static bool IsAcceptableTarget(Instruction *Inst, BasicBlock *SuccToSinkTo,
12999
return false;
130100
}
131101

132-
// Finally, check that all the uses of the instruction are actually
133-
// dominated by the candidate
134-
return AllUsesDominatedByBlock(Inst, SuccToSinkTo, DT);
102+
return true;
135103
}
136104

137105
/// SinkInstruction - Determine whether it is safe to sink the specified machine
@@ -162,25 +130,34 @@ static bool SinkInstruction(Instruction *Inst,
162130
// decide.
163131
BasicBlock *SuccToSinkTo = nullptr;
164132

165-
// Instructions can only be sunk if all their uses are in blocks
166-
// dominated by one of the successors.
167-
// Look at all the dominated blocks and see if we can sink it in one.
168-
DomTreeNode *DTN = DT.getNode(Inst->getParent());
169-
for (auto I = DTN->begin(), E = DTN->end(); I != E && SuccToSinkTo == nullptr;
170-
++I) {
171-
BasicBlock *Candidate = (*I)->getBlock();
172-
// A node always immediate-dominates its children on the dominator
173-
// tree.
174-
if (IsAcceptableTarget(Inst, Candidate, DT, LI))
175-
SuccToSinkTo = Candidate;
133+
// Find the nearest common dominator of all users as the candidate.
134+
BasicBlock *BB = Inst->getParent();
135+
for (Use &U : Inst->uses()) {
136+
Instruction *UseInst = cast<Instruction>(U.getUser());
137+
BasicBlock *UseBlock = UseInst->getParent();
138+
if (PHINode *PN = dyn_cast<PHINode>(UseInst)) {
139+
// PHI nodes use the operand in the predecessor block, not the block with
140+
// the PHI.
141+
unsigned Num = PHINode::getIncomingValueNumForOperand(U.getOperandNo());
142+
UseBlock = PN->getIncomingBlock(Num);
143+
}
144+
if (SuccToSinkTo)
145+
SuccToSinkTo = DT.findNearestCommonDominator(SuccToSinkTo, UseBlock);
146+
else
147+
SuccToSinkTo = UseBlock;
148+
// The current basic block needs to dominate the candidate.
149+
if (!DT.dominates(BB, SuccToSinkTo))
150+
return false;
176151
}
177152

178-
// If no suitable postdominator was found, look at all the successors and
179-
// decide which one we should sink to, if any.
180-
for (succ_iterator I = succ_begin(Inst->getParent()),
181-
E = succ_end(Inst->getParent()); I != E && !SuccToSinkTo; ++I) {
182-
if (IsAcceptableTarget(Inst, *I, DT, LI))
183-
SuccToSinkTo = *I;
153+
if (SuccToSinkTo) {
154+
// The nearest common dominator may be in a parent loop of BB, which may not
155+
// be beneficial. Find an ancestor.
156+
while (SuccToSinkTo != BB &&
157+
!IsAcceptableTarget(Inst, SuccToSinkTo, DT, LI))
158+
SuccToSinkTo = DT.getNode(SuccToSinkTo)->getIDom()->getBlock();
159+
if (SuccToSinkTo == BB)
160+
SuccToSinkTo = nullptr;
184161
}
185162

186163
// If we couldn't find a block to sink to, ignore this instruction.

llvm/test/CodeGen/AMDGPU/sdiv64.ll

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -240,15 +240,15 @@ define amdgpu_kernel void @s_test_sdiv(i64 addrspace(1)* %out, i64 %x, i64 %y) {
240240
; GCN-IR-NEXT: v_lshl_b64 v[0:1], v[0:1], 1
241241
; GCN-IR-NEXT: v_or_b32_e32 v0, v2, v0
242242
; GCN-IR-NEXT: v_or_b32_e32 v1, v3, v1
243-
; GCN-IR-NEXT: BB0_7: ; %Flow7
243+
; GCN-IR-NEXT: BB0_7: ; %udiv-end
244244
; GCN-IR-NEXT: s_xor_b64 s[0:1], s[8:9], s[2:3]
245245
; GCN-IR-NEXT: v_xor_b32_e32 v0, s0, v0
246246
; GCN-IR-NEXT: v_xor_b32_e32 v1, s1, v1
247247
; GCN-IR-NEXT: v_mov_b32_e32 v2, s1
248248
; GCN-IR-NEXT: v_subrev_i32_e32 v0, vcc, s0, v0
249-
; GCN-IR-NEXT: v_subb_u32_e32 v1, vcc, v1, v2, vcc
250249
; GCN-IR-NEXT: s_mov_b32 s7, 0xf000
251250
; GCN-IR-NEXT: s_mov_b32 s6, -1
251+
; GCN-IR-NEXT: v_subb_u32_e32 v1, vcc, v1, v2, vcc
252252
; GCN-IR-NEXT: buffer_store_dwordx2 v[0:1], off, s[4:7], 0
253253
; GCN-IR-NEXT: s_endpgm
254254
%result = sdiv i64 %x, %y
@@ -411,26 +411,26 @@ define i64 @v_test_sdiv(i64 %x, i64 %y) {
411411
; GCN-IR-NEXT: v_ffbh_u32_e32 v7, v10
412412
; GCN-IR-NEXT: v_cmp_eq_u32_e32 vcc, 0, v10
413413
; GCN-IR-NEXT: v_cndmask_b32_e32 v14, v7, v0, vcc
414-
; GCN-IR-NEXT: v_sub_i32_e32 v11, vcc, v13, v14
415-
; GCN-IR-NEXT: v_subb_u32_e64 v12, s[4:5], 0, 0, vcc
416-
; GCN-IR-NEXT: v_cmp_lt_u64_e32 vcc, 63, v[11:12]
417-
; GCN-IR-NEXT: v_cmp_ne_u64_e64 s[4:5], 63, v[11:12]
414+
; GCN-IR-NEXT: v_sub_i32_e32 v7, vcc, v13, v14
415+
; GCN-IR-NEXT: v_subb_u32_e64 v8, s[4:5], 0, 0, vcc
416+
; GCN-IR-NEXT: v_cmp_lt_u64_e32 vcc, 63, v[7:8]
417+
; GCN-IR-NEXT: v_cmp_ne_u64_e64 s[4:5], 63, v[7:8]
418418
; GCN-IR-NEXT: s_or_b64 s[6:7], s[6:7], vcc
419419
; GCN-IR-NEXT: s_xor_b64 s[8:9], s[6:7], -1
420420
; GCN-IR-NEXT: v_mov_b32_e32 v18, 0
421421
; GCN-IR-NEXT: v_mov_b32_e32 v6, v4
422422
; GCN-IR-NEXT: v_mov_b32_e32 v1, v5
423-
; GCN-IR-NEXT: v_cndmask_b32_e64 v7, v10, 0, s[6:7]
423+
; GCN-IR-NEXT: v_cndmask_b32_e64 v12, v10, 0, s[6:7]
424424
; GCN-IR-NEXT: s_and_b64 s[4:5], s[8:9], s[4:5]
425425
; GCN-IR-NEXT: v_mov_b32_e32 v15, v18
426426
; GCN-IR-NEXT: v_cndmask_b32_e64 v0, v9, 0, s[6:7]
427427
; GCN-IR-NEXT: s_and_saveexec_b64 s[6:7], s[4:5]
428428
; GCN-IR-NEXT: s_cbranch_execz BB1_6
429429
; GCN-IR-NEXT: ; %bb.1: ; %udiv-bb1
430-
; GCN-IR-NEXT: v_add_i32_e32 v16, vcc, 1, v11
431-
; GCN-IR-NEXT: v_addc_u32_e32 v17, vcc, 0, v12, vcc
432-
; GCN-IR-NEXT: v_sub_i32_e64 v0, s[4:5], 63, v11
433-
; GCN-IR-NEXT: v_cmp_ge_u64_e32 vcc, v[16:17], v[11:12]
430+
; GCN-IR-NEXT: v_add_i32_e32 v16, vcc, 1, v7
431+
; GCN-IR-NEXT: v_addc_u32_e32 v17, vcc, 0, v8, vcc
432+
; GCN-IR-NEXT: v_sub_i32_e64 v0, s[4:5], 63, v7
433+
; GCN-IR-NEXT: v_cmp_ge_u64_e32 vcc, v[16:17], v[7:8]
434434
; GCN-IR-NEXT: v_mov_b32_e32 v11, 0
435435
; GCN-IR-NEXT: v_lshl_b64 v[7:8], v[9:10], v0
436436
; GCN-IR-NEXT: s_mov_b64 s[8:9], 0
@@ -480,14 +480,14 @@ define i64 @v_test_sdiv(i64 %x, i64 %y) {
480480
; GCN-IR-NEXT: BB1_5: ; %Flow3
481481
; GCN-IR-NEXT: s_or_b64 exec, exec, s[10:11]
482482
; GCN-IR-NEXT: v_lshl_b64 v[2:3], v[7:8], 1
483-
; GCN-IR-NEXT: v_or_b32_e32 v7, v12, v3
483+
; GCN-IR-NEXT: v_or_b32_e32 v12, v12, v3
484484
; GCN-IR-NEXT: v_or_b32_e32 v0, v11, v2
485485
; GCN-IR-NEXT: BB1_6: ; %Flow4
486486
; GCN-IR-NEXT: s_or_b64 exec, exec, s[6:7]
487487
; GCN-IR-NEXT: v_xor_b32_e32 v2, v5, v4
488488
; GCN-IR-NEXT: v_xor_b32_e32 v0, v0, v2
489489
; GCN-IR-NEXT: v_xor_b32_e32 v1, v1, v6
490-
; GCN-IR-NEXT: v_xor_b32_e32 v3, v7, v1
490+
; GCN-IR-NEXT: v_xor_b32_e32 v3, v12, v1
491491
; GCN-IR-NEXT: v_sub_i32_e32 v0, vcc, v0, v2
492492
; GCN-IR-NEXT: v_subb_u32_e32 v1, vcc, v3, v1, vcc
493493
; GCN-IR-NEXT: s_setpc_b64 s[30:31]
@@ -1111,7 +1111,7 @@ define amdgpu_kernel void @s_test_sdiv24_48(i48 addrspace(1)* %out, i48 %x, i48
11111111
; GCN-IR-NEXT: v_lshl_b64 v[0:1], v[0:1], 1
11121112
; GCN-IR-NEXT: v_or_b32_e32 v0, v2, v0
11131113
; GCN-IR-NEXT: v_or_b32_e32 v1, v3, v1
1114-
; GCN-IR-NEXT: BB9_7: ; %Flow4
1114+
; GCN-IR-NEXT: BB9_7: ; %udiv-end
11151115
; GCN-IR-NEXT: s_xor_b64 s[0:1], s[6:7], s[2:3]
11161116
; GCN-IR-NEXT: v_xor_b32_e32 v0, s0, v0
11171117
; GCN-IR-NEXT: v_xor_b32_e32 v1, s1, v1
@@ -1341,9 +1341,9 @@ define amdgpu_kernel void @s_test_sdiv_k_num_i64(i64 addrspace(1)* %out, i64 %x)
13411341
; GCN-IR-NEXT: v_xor_b32_e32 v1, s3, v1
13421342
; GCN-IR-NEXT: v_mov_b32_e32 v2, s3
13431343
; GCN-IR-NEXT: v_subrev_i32_e32 v0, vcc, s2, v0
1344-
; GCN-IR-NEXT: v_subb_u32_e32 v1, vcc, v1, v2, vcc
13451344
; GCN-IR-NEXT: s_mov_b32 s7, 0xf000
13461345
; GCN-IR-NEXT: s_mov_b32 s6, -1
1346+
; GCN-IR-NEXT: v_subb_u32_e32 v1, vcc, v1, v2, vcc
13471347
; GCN-IR-NEXT: buffer_store_dwordx2 v[0:1], off, s[4:7], 0
13481348
; GCN-IR-NEXT: s_endpgm
13491349
%result = sdiv i64 24, %x

llvm/test/CodeGen/AMDGPU/setcc.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,9 @@ endif:
397397
}
398398

399399
; FUNC-LABEL: setcc-i1-and-xor
400-
; GCN-DAG: v_cmp_ge_f32_e64 [[A:s\[[0-9]+:[0-9]+\]]], s{{[0-9]+}}, 0{{$}}
401-
; GCN-DAG: v_cmp_le_f32_e64 [[B:s\[[0-9]+:[0-9]+\]]], s{{[0-9]+}}, 1.0
402-
; GCN: s_and_b64 s[2:3], [[A]], [[B]]
400+
; GCN-DAG: v_cmp_nge_f32_e64 [[A:s\[[0-9]+:[0-9]+\]]], s{{[0-9]+}}, 0{{$}}
401+
; GCN-DAG: v_cmp_nle_f32_e64 [[B:s\[[0-9]+:[0-9]+\]]], s{{[0-9]+}}, 1.0
402+
; GCN: s_or_b64 s[2:3], [[A]], [[B]]
403403
define amdgpu_kernel void @setcc-i1-and-xor(i32 addrspace(1)* %out, float %cond) #0 {
404404
bb0:
405405
%tmp5 = fcmp oge float %cond, 0.000000e+00

llvm/test/CodeGen/AMDGPU/si-annotate-cf.ll

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,8 @@ define amdgpu_kernel void @loop_land_info_assert(i32 %c0, i32 %c1, i32 %c2, i32
221221
; SI-NEXT: s_and_b64 vcc, exec, s[14:15]
222222
; SI-NEXT: s_cbranch_vccz BB3_13
223223
; SI-NEXT: ; %bb.10: ; %for.cond.preheader
224-
; SI-NEXT: s_waitcnt expcnt(0)
225-
; SI-NEXT: v_mov_b32_e32 v0, 0x3e8
226-
; SI-NEXT: v_cmp_lt_i32_e32 vcc, s8, v0
227-
; SI-NEXT: s_and_b64 vcc, exec, vcc
228-
; SI-NEXT: s_cbranch_vccz BB3_13
224+
; SI-NEXT: s_cmpk_lt_i32 s8, 0x3e8
225+
; SI-NEXT: s_cbranch_scc0 BB3_13
229226
; SI-NEXT: ; %bb.11: ; %for.body
230227
; SI-NEXT: s_and_b64 vcc, exec, 0
231228
; SI-NEXT: BB3_12: ; %self.loop
@@ -295,10 +292,8 @@ define amdgpu_kernel void @loop_land_info_assert(i32 %c0, i32 %c1, i32 %c2, i32
295292
; FLAT-NEXT: s_and_b64 vcc, exec, s[14:15]
296293
; FLAT-NEXT: s_cbranch_vccz BB3_13
297294
; FLAT-NEXT: ; %bb.10: ; %for.cond.preheader
298-
; FLAT-NEXT: v_mov_b32_e32 v0, 0x3e8
299-
; FLAT-NEXT: v_cmp_lt_i32_e32 vcc, s8, v0
300-
; FLAT-NEXT: s_and_b64 vcc, exec, vcc
301-
; FLAT-NEXT: s_cbranch_vccz BB3_13
295+
; FLAT-NEXT: s_cmpk_lt_i32 s8, 0x3e8
296+
; FLAT-NEXT: s_cbranch_scc0 BB3_13
302297
; FLAT-NEXT: ; %bb.11: ; %for.body
303298
; FLAT-NEXT: s_and_b64 vcc, exec, 0
304299
; FLAT-NEXT: BB3_12: ; %self.loop

0 commit comments

Comments
 (0)