Skip to content

Commit 61a8da9

Browse files
authored
[Clang][CodeGen] Add workaround for old glibc __PTR_ALIGN macro (#137851)
This patch adds a workaround for the old glibc `__PTR_ALIGN` macro: ``` ((sizeof(long int) < sizeof(void *) ? (base) : (char *)0) + (((pointer) - (sizeof(long int) < sizeof(void *) ? (base) : (char *)0) + (align_mask)) & ~(align_mask))); ``` Closes #137833.
1 parent 4923cff commit 61a8da9

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

clang/lib/AST/Expr.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2247,6 +2247,16 @@ bool BinaryOperator::isNullPointerArithmeticExtension(ASTContext &Ctx,
22472247
return false;
22482248
}
22492249

2250+
// Workaround for old glibc's __PTR_ALIGN macro
2251+
if (auto *Select =
2252+
dyn_cast<ConditionalOperator>(PExp->IgnoreParenNoopCasts(Ctx))) {
2253+
// If the condition can be constant evaluated, we check the selected arm.
2254+
bool EvalResult;
2255+
if (!Select->getCond()->EvaluateAsBooleanCondition(EvalResult, Ctx))
2256+
return false;
2257+
PExp = EvalResult ? Select->getTrueExpr() : Select->getFalseExpr();
2258+
}
2259+
22502260
// Check that the pointer is a nullptr.
22512261
if (!PExp->IgnoreParenCasts()
22522262
->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull))

clang/test/CodeGen/glibc_ptr_align.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5
2+
// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -O3 -o - -emit-llvm %s | FileCheck %s
3+
4+
// Make sure that we do not set inbounds flag if the base pointer may be a constant null.
5+
6+
// CHECK-LABEL: define dso_local ptr @glibc_ptr_align(
7+
// CHECK-SAME: ptr noundef readnone captures(none) [[BASE:%.*]], ptr noundef [[POINTER:%.*]], i64 noundef [[ALIGN_MASK:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
8+
// CHECK-NEXT: [[ENTRY:.*:]]
9+
// CHECK-NEXT: [[SUB_PTR_LHS_CAST:%.*]] = ptrtoint ptr [[POINTER]] to i64
10+
// CHECK-NEXT: [[ADD:%.*]] = add nsw i64 [[ALIGN_MASK]], [[SUB_PTR_LHS_CAST]]
11+
// CHECK-NEXT: [[NOT:%.*]] = xor i64 [[ALIGN_MASK]], -1
12+
// CHECK-NEXT: [[AND:%.*]] = and i64 [[ADD]], [[NOT]]
13+
// CHECK-NEXT: [[TMP0:%.*]] = inttoptr i64 [[AND]] to ptr
14+
// CHECK-NEXT: ret ptr [[TMP0]]
15+
//
16+
char *glibc_ptr_align(char *base, char *pointer, long align_mask) {
17+
return (sizeof(long int) < sizeof(void *) ? (base) : (char *)0) +
18+
(((pointer) -
19+
(sizeof(long int) < sizeof(void *) ? (base) : (char *)0) +
20+
(align_mask)) &
21+
~(align_mask));
22+
}
23+
24+
// CHECK-LABEL: define dso_local ptr @glibc_ptr_align_commuted(
25+
// CHECK-SAME: ptr noundef readnone captures(none) [[BASE:%.*]], ptr noundef [[POINTER:%.*]], i64 noundef [[ALIGN_MASK:%.*]]) local_unnamed_addr #[[ATTR0]] {
26+
// CHECK-NEXT: [[ENTRY:.*:]]
27+
// CHECK-NEXT: [[SUB_PTR_LHS_CAST:%.*]] = ptrtoint ptr [[POINTER]] to i64
28+
// CHECK-NEXT: [[ADD:%.*]] = add nsw i64 [[ALIGN_MASK]], [[SUB_PTR_LHS_CAST]]
29+
// CHECK-NEXT: [[NOT:%.*]] = xor i64 [[ALIGN_MASK]], -1
30+
// CHECK-NEXT: [[AND:%.*]] = and i64 [[ADD]], [[NOT]]
31+
// CHECK-NEXT: [[TMP0:%.*]] = inttoptr i64 [[AND]] to ptr
32+
// CHECK-NEXT: ret ptr [[TMP0]]
33+
//
34+
char *glibc_ptr_align_commuted(char *base, char *pointer, long align_mask) {
35+
return (sizeof(long int) >= sizeof(void *) ? (char *)0 : (base)) +
36+
(((pointer) -
37+
(sizeof(long int) >= sizeof(void *) ? (char *)0 : (base)) +
38+
(align_mask)) &
39+
~(align_mask));
40+
}
41+
42+
// CHECK-LABEL: define dso_local ptr @glibc_ptr_align_non_constexpr(
43+
// CHECK-SAME: ptr noundef [[BASE:%.*]], ptr noundef [[POINTER:%.*]], i64 noundef [[ALIGN_MASK:%.*]], i32 noundef [[COND:%.*]]) local_unnamed_addr #[[ATTR0]] {
44+
// CHECK-NEXT: [[ENTRY:.*:]]
45+
// CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq i32 [[COND]], 0
46+
// CHECK-NEXT: [[COND1:%.*]] = select i1 [[TOBOOL_NOT]], ptr null, ptr [[BASE]]
47+
// CHECK-NEXT: [[SUB_PTR_LHS_CAST:%.*]] = ptrtoint ptr [[POINTER]] to i64
48+
// CHECK-NEXT: [[SUB_PTR_RHS_CAST:%.*]] = ptrtoint ptr [[COND1]] to i64
49+
// CHECK-NEXT: [[SUB_PTR_SUB:%.*]] = add i64 [[ALIGN_MASK]], [[SUB_PTR_LHS_CAST]]
50+
// CHECK-NEXT: [[ADD:%.*]] = sub i64 [[SUB_PTR_SUB]], [[SUB_PTR_RHS_CAST]]
51+
// CHECK-NEXT: [[NOT:%.*]] = xor i64 [[ALIGN_MASK]], -1
52+
// CHECK-NEXT: [[AND:%.*]] = and i64 [[ADD]], [[NOT]]
53+
// CHECK-NEXT: [[ADD_PTR:%.*]] = getelementptr inbounds i8, ptr [[COND1]], i64 [[AND]]
54+
// CHECK-NEXT: ret ptr [[ADD_PTR]]
55+
//
56+
char *glibc_ptr_align_non_constexpr(char *base, char *pointer, long align_mask, int cond) {
57+
return (cond ? (base) : (char *)0) +
58+
(((pointer) -
59+
(cond ? (base) : (char *)0) +
60+
(align_mask)) &
61+
~(align_mask));
62+
}

0 commit comments

Comments
 (0)