Skip to content

Commit 115a17a

Browse files
author
Jin Huang
committed
[profcheck] Fix profile metadata missing in ExpandLargeDivRem
1 parent 60e7c47 commit 115a17a

File tree

6 files changed

+161
-65
lines changed

6 files changed

+161
-65
lines changed

llvm/lib/Transforms/Utils/IntegerDivision.cpp

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,22 @@
1616
#include "llvm/Transforms/Utils/IntegerDivision.h"
1717
#include "llvm/IR/Function.h"
1818
#include "llvm/IR/IRBuilder.h"
19+
#include "llvm/IR/Instruction.h"
1920
#include "llvm/IR/Instructions.h"
2021
#include "llvm/IR/Intrinsics.h"
22+
#include "llvm/IR/MDBuilder.h"
23+
#include "llvm/IR/ProfDataUtils.h"
24+
#include "llvm/Support/Casting.h"
25+
#include "llvm/Support/CommandLine.h"
2126

2227
using namespace llvm;
2328

2429
#define DEBUG_TYPE "integer-division"
2530

31+
namespace llvm {
32+
extern cl::opt<bool> ProfcheckDisableMetadataFixes;
33+
}
34+
2635
/// Generate code to compute the remainder of two signed integers. Returns the
2736
/// remainder, which will have the sign of the dividend. Builder's insert point
2837
/// should be pointing where the caller wants code generated, e.g. at the srem
@@ -235,11 +244,53 @@ static Value *generateUnsignedDivisionCode(Value *Dividend, Value *Divisor,
235244
Value *Tmp1 = Builder.CreateCall(CTLZ, {Dividend, True});
236245
Value *SR = Builder.CreateSub(Tmp0, Tmp1);
237246
Value *Ret0_4 = Builder.CreateICmpUGT(SR, MSB);
247+
248+
// Add 'unlikely' branch weights to the select instruction ('Ret0') generated
249+
// by 'CreateLogicalOr'. We assume the 'true' path, where either the divisor
250+
// or dividend is zero ('Ret0_3'), is less likely than the 'false' path,
251+
// which corresponds to the |divisor| > |dividend| ('Ret0_4').
238252
Value *Ret0 = Builder.CreateLogicalOr(Ret0_3, Ret0_4);
253+
if (!ProfcheckDisableMetadataFixes) {
254+
if (Instruction *Ret0SelectI = dyn_cast<Instruction>(Ret0)) {
255+
Ret0SelectI->setMetadata(
256+
LLVMContext::MD_prof,
257+
MDBuilder(Ret0SelectI->getContext()).createUnlikelyBranchWeights());
258+
}
259+
}
239260
Value *RetDividend = Builder.CreateICmpEQ(SR, MSB);
240-
Value *RetVal = Builder.CreateSelect(Ret0, Zero, Dividend);
241-
Value *EarlyRet = Builder.CreateLogicalOr(Ret0, RetDividend);
242-
Builder.CreateCondBr(EarlyRet, End, BB1);
261+
262+
// This select instruction (RetVal) immediately follows the Ret0 check. Since
263+
// Ret0_4 determines if the divisor is greater then the dividend so the
264+
// unknown (50/50) branch weights are fairly accurate for RetVal.
265+
Value *RetVal;
266+
if (!ProfcheckDisableMetadataFixes) {
267+
RetVal = Builder.CreateSelectWithUnknownProfile(Ret0, Zero, Dividend,
268+
DEBUG_TYPE);
269+
} else {
270+
RetVal = Builder.CreateSelect(Ret0, Zero, Dividend);
271+
}
272+
// This select instruction (EarlyRet) is used to check another edge case, and
273+
// it share the same branch weights as RetVal so the unknown (50/50)branch
274+
// weights are also accurate for EarlyRet.
275+
Value *EarlyRet;
276+
if (!ProfcheckDisableMetadataFixes) {
277+
EarlyRet = Builder.CreateSelectWithUnknownProfile(
278+
Ret0, ConstantInt::getAllOnesValue(Ret0->getType()), RetDividend,
279+
DEBUG_TYPE);
280+
} else {
281+
EarlyRet = Builder.CreateLogicalOr(Ret0, RetDividend);
282+
}
283+
284+
// The condition of this branch is based on `EarlyRet`. `EarlyRet` is true
285+
// only for special cases like dividend or divisor being zero, or the divisor
286+
// being greater than the dividend. Thus, the branch to `End` is unlikely,
287+
// and we expect to more frequently enter `BB1`.
288+
Instruction *ConBrSpecialCases = Builder.CreateCondBr(EarlyRet, End, BB1);
289+
if (!ProfcheckDisableMetadataFixes) {
290+
ConBrSpecialCases->setMetadata(LLVMContext::MD_prof,
291+
MDBuilder(ConBrSpecialCases->getContext())
292+
.createUnlikelyBranchWeights());
293+
}
243294

244295
// ; bb1: ; preds = %special-cases
245296
// ; %sr_1 = add i32 %sr, 1
@@ -252,7 +303,15 @@ static Value *generateUnsignedDivisionCode(Value *Dividend, Value *Divisor,
252303
Value *Tmp2 = Builder.CreateSub(MSB, SR);
253304
Value *Q = Builder.CreateShl(Dividend, Tmp2);
254305
Value *SkipLoop = Builder.CreateICmpEQ(SR_1, Zero);
255-
Builder.CreateCondBr(SkipLoop, LoopExit, Preheader);
306+
// This branch is highly unlikely. SR_1 (SR + 1) is expected to be in [1, 129]
307+
// because the case where |divisor| > |dividend| (which would make SR
308+
// negative) has alredy been intercepted in the previous SpecialCases BB.
309+
Instruction *ConBrBB1 = Builder.CreateCondBr(SkipLoop, LoopExit, Preheader);
310+
if (!ProfcheckDisableMetadataFixes) {
311+
ConBrBB1->setMetadata(LLVMContext::MD_prof,
312+
MDBuilder(ConBrBB1->getContext())
313+
.createUnlikelyBranchWeights());
314+
}
256315

257316
// ; preheader: ; preds = %bb1
258317
// ; %tmp3 = lshr i32 %dividend, %sr_1
@@ -298,7 +357,13 @@ static Value *generateUnsignedDivisionCode(Value *Dividend, Value *Divisor,
298357
Value *R = Builder.CreateSub(Tmp7, Tmp11);
299358
Value *SR_2 = Builder.CreateAdd(SR_3, NegOne);
300359
Value *Tmp12 = Builder.CreateICmpEQ(SR_2, Zero);
301-
Builder.CreateCondBr(Tmp12, LoopExit, DoWhile);
360+
// The loop implements the core bit-by-bit binary long division algorithm.
361+
// Since each iteration generates one bit of the quotient until covering all
362+
// significant bits so we would label it as unknown (50/50) branch weights.
363+
Instruction *ConBrDoWhile = Builder.CreateCondBr(Tmp12, LoopExit, DoWhile);
364+
if (!ProfcheckDisableMetadataFixes) {
365+
setExplicitlyUnknownBranchWeightsIfProfiled(*ConBrDoWhile, DEBUG_TYPE, F);
366+
}
302367

303368
// ; loop-exit: ; preds = %do-while, %bb1
304369
// ; %carry_2 = phi i32 [ 0, %bb1 ], [ %carry, %do-while ]

llvm/test/Transforms/ExpandIRInsts/X86/sdiv129.ll

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
; RUN: opt -S -mtriple=x86_64-- -expand-ir-insts -expand-div-rem-bits 128 < %s | FileCheck %s
33
; RUN: opt -S -mtriple=x86_64-- -passes='require<libcall-lowering-info>,expand-ir-insts' -expand-div-rem-bits 128 < %s | FileCheck %s
44

5-
define void @sdiv129(ptr %ptr, ptr %out) nounwind {
5+
define void @sdiv129(ptr %ptr, ptr %out) nounwind !prof !0 {
66
; CHECK-LABEL: @sdiv129(
7+
; CHECK: !prof [[PROF_0:![0-9]+]] {
78
; CHECK-NEXT: _udiv-special-cases:
89
; CHECK-NEXT: [[A:%.*]] = load i129, ptr [[PTR:%.*]], align 16
910
; CHECK-NEXT: [[TMP0:%.*]] = freeze i129 [[A]]
@@ -24,11 +25,11 @@ define void @sdiv129(ptr %ptr, ptr %out) nounwind {
2425
; CHECK-NEXT: [[TMP15:%.*]] = call i129 @llvm.ctlz.i129(i129 [[TMP10]], i1 true)
2526
; CHECK-NEXT: [[TMP16:%.*]] = sub i129 [[TMP14]], [[TMP15]]
2627
; CHECK-NEXT: [[TMP17:%.*]] = icmp ugt i129 [[TMP16]], 128
27-
; CHECK-NEXT: [[TMP18:%.*]] = select i1 [[TMP13]], i1 true, i1 [[TMP17]]
28+
; CHECK-NEXT: [[TMP18:%.*]] = select i1 [[TMP13]], i1 true, i1 [[TMP17]], !prof [[PROF_1:![0-9]+]]
2829
; CHECK-NEXT: [[TMP19:%.*]] = icmp eq i129 [[TMP16]], 128
29-
; CHECK-NEXT: [[TMP20:%.*]] = select i1 [[TMP18]], i129 0, i129 [[TMP10]]
30-
; CHECK-NEXT: [[TMP21:%.*]] = select i1 [[TMP18]], i1 true, i1 [[TMP19]]
31-
; CHECK-NEXT: br i1 [[TMP21]], label [[UDIV_END:%.*]], label [[UDIV_BB1:%.*]]
30+
; CHECK-NEXT: [[TMP20:%.*]] = select i1 [[TMP18]], i129 0, i129 [[TMP10]], !prof [[PROF_2:![0-9]+]]
31+
; CHECK-NEXT: [[TMP21:%.*]] = select i1 [[TMP18]], i1 true, i1 [[TMP19]], !prof [[PROF_2:![0-9]+]]
32+
; CHECK-NEXT: br i1 [[TMP21]], label [[UDIV_END:%.*]], label [[UDIV_BB1:%.*]], !prof [[PROF_1:![0-9]+]]
3233
; CHECK: udiv-loop-exit:
3334
; CHECK-NEXT: [[TMP22:%.*]] = phi i129 [ 0, [[UDIV_BB1]] ], [ [[TMP37:%.*]], [[UDIV_DO_WHILE:%.*]] ]
3435
; CHECK-NEXT: [[TMP23:%.*]] = phi i129 [ [[TMP46:%.*]], [[UDIV_BB1]] ], [ [[TMP34:%.*]], [[UDIV_DO_WHILE]] ]
@@ -52,7 +53,7 @@ define void @sdiv129(ptr %ptr, ptr %out) nounwind {
5253
; CHECK-NEXT: [[TMP39]] = sub i129 [[TMP32]], [[TMP38]]
5354
; CHECK-NEXT: [[TMP40]] = add i129 [[TMP27]], -1
5455
; CHECK-NEXT: [[TMP41:%.*]] = icmp eq i129 [[TMP40]], 0
55-
; CHECK-NEXT: br i1 [[TMP41]], label [[UDIV_LOOP_EXIT:%.*]], label [[UDIV_DO_WHILE]]
56+
; CHECK-NEXT: br i1 [[TMP41]], label [[UDIV_LOOP_EXIT:%.*]], label [[UDIV_DO_WHILE]], !prof [[PROF_2:![0-9]+]]
5657
; CHECK: udiv-preheader:
5758
; CHECK-NEXT: [[TMP42]] = lshr i129 [[TMP10]], [[TMP44]]
5859
; CHECK-NEXT: [[TMP43]] = add i129 [[TMP9]], -1
@@ -62,7 +63,7 @@ define void @sdiv129(ptr %ptr, ptr %out) nounwind {
6263
; CHECK-NEXT: [[TMP45:%.*]] = sub i129 128, [[TMP16]]
6364
; CHECK-NEXT: [[TMP46]] = shl i129 [[TMP10]], [[TMP45]]
6465
; CHECK-NEXT: [[TMP47:%.*]] = icmp eq i129 [[TMP44]], 0
65-
; CHECK-NEXT: br i1 [[TMP47]], label [[UDIV_LOOP_EXIT]], label [[UDIV_PREHEADER]]
66+
; CHECK-NEXT: br i1 [[TMP47]], label [[UDIV_LOOP_EXIT]], label [[UDIV_PREHEADER]], !prof [[PROF_1:![0-9]+]]
6667
; CHECK: udiv-end:
6768
; CHECK-NEXT: [[TMP48:%.*]] = phi i129 [ [[TMP25]], [[UDIV_LOOP_EXIT]] ], [ [[TMP20]], [[_UDIV_SPECIAL_CASES:%.*]] ]
6869
; CHECK-NEXT: [[TMP49:%.*]] = xor i129 [[TMP48]], [[TMP8]]
@@ -75,3 +76,8 @@ define void @sdiv129(ptr %ptr, ptr %out) nounwind {
7576
store i129 %res, ptr %out
7677
ret void
7778
}
79+
80+
!0 = !{!"function_entry_count", i64 1000}
81+
; CHECK: [[PROF_0]] = !{!"function_entry_count", i64 1000}
82+
; CHECK: [[PROF_1]] = !{!"branch_weights", i32 1, i32 1048575}
83+
; CHECK: [[PROF_2]] = !{!"unknown", !"integer-division"}

llvm/test/Transforms/ExpandIRInsts/X86/srem129.ll

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
; RUN: opt -S -mtriple=x86_64-- -expand-ir-insts -expand-div-rem-bits 128 < %s | FileCheck %s
33
; RUN: opt -S -mtriple=x86_64-- -passes='require<libcall-lowering-info>,expand-ir-insts' -expand-div-rem-bits 128 < %s | FileCheck %s
44

5-
define void @test(ptr %ptr, ptr %out) nounwind {
5+
define void @test(ptr %ptr, ptr %out) nounwind !prof !0 {
66
; CHECK-LABEL: @test(
7+
; CHECK: !prof [[PROF_0:![0-9]+]] {
78
; CHECK-NEXT: _udiv-special-cases:
89
; CHECK-NEXT: [[A:%.*]] = load i129, ptr [[PTR:%.*]], align 16
910
; CHECK-NEXT: [[TMP0:%.*]] = freeze i129 [[A]]
@@ -25,11 +26,11 @@ define void @test(ptr %ptr, ptr %out) nounwind {
2526
; CHECK-NEXT: [[TMP16:%.*]] = call i129 @llvm.ctlz.i129(i129 [[TMP11]], i1 true)
2627
; CHECK-NEXT: [[TMP17:%.*]] = sub i129 [[TMP15]], [[TMP16]]
2728
; CHECK-NEXT: [[TMP18:%.*]] = icmp ugt i129 [[TMP17]], 128
28-
; CHECK-NEXT: [[TMP19:%.*]] = select i1 [[TMP14]], i1 true, i1 [[TMP18]]
29+
; CHECK-NEXT: [[TMP19:%.*]] = select i1 [[TMP14]], i1 true, i1 [[TMP18]], !prof [[PROF_1:![0-9]+]]
2930
; CHECK-NEXT: [[TMP20:%.*]] = icmp eq i129 [[TMP17]], 128
30-
; CHECK-NEXT: [[TMP21:%.*]] = select i1 [[TMP19]], i129 0, i129 [[TMP11]]
31-
; CHECK-NEXT: [[TMP22:%.*]] = select i1 [[TMP19]], i1 true, i1 [[TMP20]]
32-
; CHECK-NEXT: br i1 [[TMP22]], label [[UDIV_END:%.*]], label [[UDIV_BB1:%.*]]
31+
; CHECK-NEXT: [[TMP21:%.*]] = select i1 [[TMP19]], i129 0, i129 [[TMP11]], !prof [[PROF_2:![0-9]+]]
32+
; CHECK-NEXT: [[TMP22:%.*]] = select i1 [[TMP19]], i1 true, i1 [[TMP20]], !prof [[PROF_2:![0-9]+]]
33+
; CHECK-NEXT: br i1 [[TMP22]], label [[UDIV_END:%.*]], label [[UDIV_BB1:%.*]], !prof [[PROF_1:![0-9]+]]
3334
; CHECK: udiv-loop-exit:
3435
; CHECK-NEXT: [[TMP23:%.*]] = phi i129 [ 0, [[UDIV_BB1]] ], [ [[TMP38:%.*]], [[UDIV_DO_WHILE:%.*]] ]
3536
; CHECK-NEXT: [[TMP24:%.*]] = phi i129 [ [[TMP47:%.*]], [[UDIV_BB1]] ], [ [[TMP35:%.*]], [[UDIV_DO_WHILE]] ]
@@ -53,7 +54,7 @@ define void @test(ptr %ptr, ptr %out) nounwind {
5354
; CHECK-NEXT: [[TMP40]] = sub i129 [[TMP33]], [[TMP39]]
5455
; CHECK-NEXT: [[TMP41]] = add i129 [[TMP28]], -1
5556
; CHECK-NEXT: [[TMP42:%.*]] = icmp eq i129 [[TMP41]], 0
56-
; CHECK-NEXT: br i1 [[TMP42]], label [[UDIV_LOOP_EXIT:%.*]], label [[UDIV_DO_WHILE]]
57+
; CHECK-NEXT: br i1 [[TMP42]], label [[UDIV_LOOP_EXIT:%.*]], label [[UDIV_DO_WHILE]], !prof [[PROF_2:![0-9]+]]
5758
; CHECK: udiv-preheader:
5859
; CHECK-NEXT: [[TMP43]] = lshr i129 [[TMP11]], [[TMP45]]
5960
; CHECK-NEXT: [[TMP44]] = add i129 [[TMP10]], -1
@@ -63,7 +64,7 @@ define void @test(ptr %ptr, ptr %out) nounwind {
6364
; CHECK-NEXT: [[TMP46:%.*]] = sub i129 128, [[TMP17]]
6465
; CHECK-NEXT: [[TMP47]] = shl i129 [[TMP11]], [[TMP46]]
6566
; CHECK-NEXT: [[TMP48:%.*]] = icmp eq i129 [[TMP45]], 0
66-
; CHECK-NEXT: br i1 [[TMP48]], label [[UDIV_LOOP_EXIT]], label [[UDIV_PREHEADER]]
67+
; CHECK-NEXT: br i1 [[TMP48]], label [[UDIV_LOOP_EXIT]], label [[UDIV_PREHEADER]], !prof [[PROF_1:![0-9]+]]
6768
; CHECK: udiv-end:
6869
; CHECK-NEXT: [[TMP49:%.*]] = phi i129 [ [[TMP26]], [[UDIV_LOOP_EXIT]] ], [ [[TMP21]], [[_UDIV_SPECIAL_CASES:%.*]] ]
6970
; CHECK-NEXT: [[TMP50:%.*]] = mul i129 [[TMP9]], [[TMP49]]
@@ -78,3 +79,8 @@ define void @test(ptr %ptr, ptr %out) nounwind {
7879
store i129 %res, ptr %out
7980
ret void
8081
}
82+
83+
!0 = !{!"function_entry_count", i64 1000}
84+
; CHECK: [[PROF_0]] = !{!"function_entry_count", i64 1000}
85+
; CHECK: [[PROF_1]] = !{!"branch_weights", i32 1, i32 1048575}
86+
; CHECK: [[PROF_2]] = !{!"unknown", !"integer-division"}

llvm/test/Transforms/ExpandIRInsts/X86/udiv129.ll

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
; RUN: opt -S -mtriple=x86_64-- -expand-ir-insts -expand-div-rem-bits 128 < %s | FileCheck %s
33
; RUN: opt -S -mtriple=x86_64-- -passes='require<libcall-lowering-info>,expand-ir-insts' -expand-div-rem-bits 128 < %s | FileCheck %s
44

5-
define void @test(ptr %ptr, ptr %out) nounwind {
5+
define void @test(ptr %ptr, ptr %out) nounwind !prof !0 {
66
; CHECK-LABEL: @test(
7+
; CHECK: !prof [[PROF_0:![0-9]+]] {
78
; CHECK-NEXT: _udiv-special-cases:
89
; CHECK-NEXT: [[A:%.*]] = load i129, ptr [[PTR:%.*]], align 16
910
; CHECK-NEXT: [[TMP0:%.*]] = freeze i129 3
@@ -15,11 +16,11 @@ define void @test(ptr %ptr, ptr %out) nounwind {
1516
; CHECK-NEXT: [[TMP6:%.*]] = call i129 @llvm.ctlz.i129(i129 [[TMP1]], i1 true)
1617
; CHECK-NEXT: [[TMP7:%.*]] = sub i129 [[TMP5]], [[TMP6]]
1718
; CHECK-NEXT: [[TMP8:%.*]] = icmp ugt i129 [[TMP7]], 128
18-
; CHECK-NEXT: [[TMP9:%.*]] = select i1 [[TMP4]], i1 true, i1 [[TMP8]]
19+
; CHECK-NEXT: [[TMP9:%.*]] = select i1 [[TMP4]], i1 true, i1 [[TMP8]], !prof [[PROF_1:![0-9]+]]
1920
; CHECK-NEXT: [[TMP10:%.*]] = icmp eq i129 [[TMP7]], 128
20-
; CHECK-NEXT: [[TMP11:%.*]] = select i1 [[TMP9]], i129 0, i129 [[TMP1]]
21-
; CHECK-NEXT: [[TMP12:%.*]] = select i1 [[TMP9]], i1 true, i1 [[TMP10]]
22-
; CHECK-NEXT: br i1 [[TMP12]], label [[UDIV_END:%.*]], label [[UDIV_BB1:%.*]]
21+
; CHECK-NEXT: [[TMP11:%.*]] = select i1 [[TMP9]], i129 0, i129 [[TMP1]], !prof [[PROF_2:![0-9]+]]
22+
; CHECK-NEXT: [[TMP12:%.*]] = select i1 [[TMP9]], i1 true, i1 [[TMP10]], !prof [[PROF_2:![0-9]+]]
23+
; CHECK-NEXT: br i1 [[TMP12]], label [[UDIV_END:%.*]], label [[UDIV_BB1:%.*]], !prof [[PROF_1:![0-9]+]]
2324
; CHECK: udiv-loop-exit:
2425
; CHECK-NEXT: [[TMP13:%.*]] = phi i129 [ 0, [[UDIV_BB1]] ], [ [[TMP28:%.*]], [[UDIV_DO_WHILE:%.*]] ]
2526
; CHECK-NEXT: [[TMP14:%.*]] = phi i129 [ [[TMP37:%.*]], [[UDIV_BB1]] ], [ [[TMP25:%.*]], [[UDIV_DO_WHILE]] ]
@@ -43,7 +44,7 @@ define void @test(ptr %ptr, ptr %out) nounwind {
4344
; CHECK-NEXT: [[TMP30]] = sub i129 [[TMP23]], [[TMP29]]
4445
; CHECK-NEXT: [[TMP31]] = add i129 [[TMP18]], -1
4546
; CHECK-NEXT: [[TMP32:%.*]] = icmp eq i129 [[TMP31]], 0
46-
; CHECK-NEXT: br i1 [[TMP32]], label [[UDIV_LOOP_EXIT:%.*]], label [[UDIV_DO_WHILE]]
47+
; CHECK-NEXT: br i1 [[TMP32]], label [[UDIV_LOOP_EXIT:%.*]], label [[UDIV_DO_WHILE]], !prof [[PROF_2:![0-9]+]]
4748
; CHECK: udiv-preheader:
4849
; CHECK-NEXT: [[TMP33]] = lshr i129 [[TMP1]], [[TMP35]]
4950
; CHECK-NEXT: [[TMP34]] = add i129 [[TMP0]], -1
@@ -53,7 +54,7 @@ define void @test(ptr %ptr, ptr %out) nounwind {
5354
; CHECK-NEXT: [[TMP36:%.*]] = sub i129 128, [[TMP7]]
5455
; CHECK-NEXT: [[TMP37]] = shl i129 [[TMP1]], [[TMP36]]
5556
; CHECK-NEXT: [[TMP38:%.*]] = icmp eq i129 [[TMP35]], 0
56-
; CHECK-NEXT: br i1 [[TMP38]], label [[UDIV_LOOP_EXIT]], label [[UDIV_PREHEADER]]
57+
; CHECK-NEXT: br i1 [[TMP38]], label [[UDIV_LOOP_EXIT]], label [[UDIV_PREHEADER]], !prof [[PROF_1:![0-9]+]]
5758
; CHECK: udiv-end:
5859
; CHECK-NEXT: [[TMP39:%.*]] = phi i129 [ [[TMP16]], [[UDIV_LOOP_EXIT]] ], [ [[TMP11]], [[_UDIV_SPECIAL_CASES:%.*]] ]
5960
; CHECK-NEXT: store i129 [[TMP39]], ptr [[OUT:%.*]], align 16
@@ -64,3 +65,8 @@ define void @test(ptr %ptr, ptr %out) nounwind {
6465
store i129 %res, ptr %out
6566
ret void
6667
}
68+
69+
!0 = !{!"function_entry_count", i64 1000}
70+
; CHECK: [[PROF_0]] = !{!"function_entry_count", i64 1000}
71+
; CHECK: [[PROF_1]] = !{!"branch_weights", i32 1, i32 1048575}
72+
; CHECK: [[PROF_2]] = !{!"unknown", !"integer-division"}

llvm/test/Transforms/ExpandIRInsts/X86/urem129.ll

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
; RUN: opt -S -mtriple=x86_64-- -expand-ir-insts -expand-div-rem-bits 128 < %s | FileCheck %s
33
; RUN: opt -S -mtriple=x86_64-- -passes='require<libcall-lowering-info>,expand-ir-insts' -expand-div-rem-bits 128 < %s | FileCheck %s
44

5-
define void @test(ptr %ptr, ptr %out) nounwind {
5+
define void @test(ptr %ptr, ptr %out) nounwind !prof !0 {
66
; CHECK-LABEL: @test(
7+
; CHECK: !prof [[PROF_0:![0-9]+]] {
78
; CHECK-NEXT: _udiv-special-cases:
89
; CHECK-NEXT: [[A:%.*]] = load i129, ptr [[PTR:%.*]], align 16
910
; CHECK-NEXT: [[TMP0:%.*]] = freeze i129 [[A]]
@@ -17,11 +18,11 @@ define void @test(ptr %ptr, ptr %out) nounwind {
1718
; CHECK-NEXT: [[TMP8:%.*]] = call i129 @llvm.ctlz.i129(i129 [[TMP3]], i1 true)
1819
; CHECK-NEXT: [[TMP9:%.*]] = sub i129 [[TMP7]], [[TMP8]]
1920
; CHECK-NEXT: [[TMP10:%.*]] = icmp ugt i129 [[TMP9]], 128
20-
; CHECK-NEXT: [[TMP11:%.*]] = select i1 [[TMP6]], i1 true, i1 [[TMP10]]
21+
; CHECK-NEXT: [[TMP11:%.*]] = select i1 [[TMP6]], i1 true, i1 [[TMP10]], !prof [[PROF_1:![0-9]+]]
2122
; CHECK-NEXT: [[TMP12:%.*]] = icmp eq i129 [[TMP9]], 128
22-
; CHECK-NEXT: [[TMP13:%.*]] = select i1 [[TMP11]], i129 0, i129 [[TMP3]]
23-
; CHECK-NEXT: [[TMP14:%.*]] = select i1 [[TMP11]], i1 true, i1 [[TMP12]]
24-
; CHECK-NEXT: br i1 [[TMP14]], label [[UDIV_END:%.*]], label [[UDIV_BB1:%.*]]
23+
; CHECK-NEXT: [[TMP13:%.*]] = select i1 [[TMP11]], i129 0, i129 [[TMP3]], !prof [[PROF_2:![0-9]+]]
24+
; CHECK-NEXT: [[TMP14:%.*]] = select i1 [[TMP11]], i1 true, i1 [[TMP12]], !prof [[PROF_2:![0-9]+]]
25+
; CHECK-NEXT: br i1 [[TMP14]], label [[UDIV_END:%.*]], label [[UDIV_BB1:%.*]], !prof [[PROF_1:![0-9]+]]
2526
; CHECK: udiv-loop-exit:
2627
; CHECK-NEXT: [[TMP15:%.*]] = phi i129 [ 0, [[UDIV_BB1]] ], [ [[TMP30:%.*]], [[UDIV_DO_WHILE:%.*]] ]
2728
; CHECK-NEXT: [[TMP16:%.*]] = phi i129 [ [[TMP39:%.*]], [[UDIV_BB1]] ], [ [[TMP27:%.*]], [[UDIV_DO_WHILE]] ]
@@ -45,7 +46,7 @@ define void @test(ptr %ptr, ptr %out) nounwind {
4546
; CHECK-NEXT: [[TMP32]] = sub i129 [[TMP25]], [[TMP31]]
4647
; CHECK-NEXT: [[TMP33]] = add i129 [[TMP20]], -1
4748
; CHECK-NEXT: [[TMP34:%.*]] = icmp eq i129 [[TMP33]], 0
48-
; CHECK-NEXT: br i1 [[TMP34]], label [[UDIV_LOOP_EXIT:%.*]], label [[UDIV_DO_WHILE]]
49+
; CHECK-NEXT: br i1 [[TMP34]], label [[UDIV_LOOP_EXIT:%.*]], label [[UDIV_DO_WHILE]], !prof [[PROF_2:![0-9]+]]
4950
; CHECK: udiv-preheader:
5051
; CHECK-NEXT: [[TMP35]] = lshr i129 [[TMP3]], [[TMP37]]
5152
; CHECK-NEXT: [[TMP36]] = add i129 [[TMP2]], -1
@@ -55,7 +56,7 @@ define void @test(ptr %ptr, ptr %out) nounwind {
5556
; CHECK-NEXT: [[TMP38:%.*]] = sub i129 128, [[TMP9]]
5657
; CHECK-NEXT: [[TMP39]] = shl i129 [[TMP3]], [[TMP38]]
5758
; CHECK-NEXT: [[TMP40:%.*]] = icmp eq i129 [[TMP37]], 0
58-
; CHECK-NEXT: br i1 [[TMP40]], label [[UDIV_LOOP_EXIT]], label [[UDIV_PREHEADER]]
59+
; CHECK-NEXT: br i1 [[TMP40]], label [[UDIV_LOOP_EXIT]], label [[UDIV_PREHEADER]], !prof [[PROF_1:![0-9]+]]
5960
; CHECK: udiv-end:
6061
; CHECK-NEXT: [[TMP41:%.*]] = phi i129 [ [[TMP18]], [[UDIV_LOOP_EXIT]] ], [ [[TMP13]], [[_UDIV_SPECIAL_CASES:%.*]] ]
6162
; CHECK-NEXT: [[TMP42:%.*]] = mul i129 [[TMP1]], [[TMP41]]
@@ -68,3 +69,8 @@ define void @test(ptr %ptr, ptr %out) nounwind {
6869
store i129 %res, ptr %out
6970
ret void
7071
}
72+
73+
!0 = !{!"function_entry_count", i64 1000}
74+
; CHECK: [[PROF_0]] = !{!"function_entry_count", i64 1000}
75+
; CHECK: [[PROF_1]] = !{!"branch_weights", i32 1, i32 1048575}
76+
; CHECK: [[PROF_2]] = !{!"unknown", !"integer-division"}

0 commit comments

Comments
 (0)