Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/lib/CodeGen/CGExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4447,6 +4447,9 @@ Value *ScalarExprEmitter::EmitSub(const BinOpInfo &op) {
divisor = CGF.CGM.getSize(elementSize);
}

// With -fms-kernel we emit normal sdiv to mitigate UB.
if (CGF.getLangOpts().Kernel)
return Builder.CreateSDiv(diffInChars, divisor, "sub.ptr.div");
// Otherwise, do a full sdiv. This uses the "exact" form of sdiv, since
// pointer difference in C is only defined in the case where both operands
// are pointing to elements of an array.
Expand Down
13 changes: 13 additions & 0 deletions clang/test/CodeGen/MSKernel/subptr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: %clang_cc1 -fms-kernel -triple x86_64-windows-msvc -O2 -emit-llvm %s -o - | FileCheck %s
// CHECK: define {{.*}} i64 @sub_ptrs(ptr noundef %p1, ptr noundef %p2)
// CHECK-NEXT: entry:
// CHECK-NEXT: %sub.ptr.lhs.cast = ptrtoint ptr %p1 to i64
// CHECK-NEXT: %sub.ptr.rhs.cast = ptrtoint ptr %p2 to i64
// CHECK-NEXT: %sub.ptr.sub = sub i64 %sub.ptr.lhs.cast, %sub.ptr.rhs.cast
// CHECK-NEXT: %sub.ptr.div = sdiv i64 %sub.ptr.sub, 4
// CHECK-NEXT: ret i64 %sub.ptr.div

long long sub_ptrs(int* p1, int* p2) {
return p1 - p2;
}