Skip to content

Commit 419863e

Browse files
committed
[Clang][CodeGen] Add workaround for old glibc __PTR_ALIGN macro
1 parent ba68a78 commit 419863e

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

clang/lib/AST/Expr.cpp

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

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

clang/test/CodeGen/glibc_ptr_align.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
// Make sure that we do not set inbounds flag if the base pointer may be a constant null.
55

6-
// CHECK-LABEL: define dso_local noalias ptr @glibc_ptr_align(
6+
// CHECK-LABEL: define dso_local ptr @glibc_ptr_align(
77
// CHECK-SAME: ptr noundef readnone captures(none) [[BASE:%.*]], ptr noundef [[POINTER:%.*]], i64 noundef [[ALIGN_MASK:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
88
// CHECK-NEXT: [[ENTRY:.*:]]
99
// CHECK-NEXT: [[SUB_PTR_LHS_CAST:%.*]] = ptrtoint ptr [[POINTER]] to i64
1010
// CHECK-NEXT: [[ADD:%.*]] = add nsw i64 [[ALIGN_MASK]], [[SUB_PTR_LHS_CAST]]
1111
// CHECK-NEXT: [[NOT:%.*]] = xor i64 [[ALIGN_MASK]], -1
1212
// CHECK-NEXT: [[AND:%.*]] = and i64 [[ADD]], [[NOT]]
13-
// CHECK-NEXT: [[ADD_PTR:%.*]] = getelementptr inbounds i8, ptr null, i64 [[AND]]
14-
// CHECK-NEXT: ret ptr [[ADD_PTR]]
13+
// CHECK-NEXT: [[TMP0:%.*]] = inttoptr i64 [[AND]] to ptr
14+
// CHECK-NEXT: ret ptr [[TMP0]]
1515
//
1616
char *glibc_ptr_align(char *base, char *pointer, long align_mask) {
1717
return (sizeof(long int) < sizeof(void *) ? (base) : (char *)0) +
@@ -21,15 +21,15 @@ char *glibc_ptr_align(char *base, char *pointer, long align_mask) {
2121
~(align_mask));
2222
}
2323

24-
// CHECK-LABEL: define dso_local noalias ptr @glibc_ptr_align_commuted(
24+
// CHECK-LABEL: define dso_local ptr @glibc_ptr_align_commuted(
2525
// CHECK-SAME: ptr noundef readnone captures(none) [[BASE:%.*]], ptr noundef [[POINTER:%.*]], i64 noundef [[ALIGN_MASK:%.*]]) local_unnamed_addr #[[ATTR0]] {
2626
// CHECK-NEXT: [[ENTRY:.*:]]
2727
// CHECK-NEXT: [[SUB_PTR_LHS_CAST:%.*]] = ptrtoint ptr [[POINTER]] to i64
2828
// CHECK-NEXT: [[ADD:%.*]] = add nsw i64 [[ALIGN_MASK]], [[SUB_PTR_LHS_CAST]]
2929
// CHECK-NEXT: [[NOT:%.*]] = xor i64 [[ALIGN_MASK]], -1
3030
// CHECK-NEXT: [[AND:%.*]] = and i64 [[ADD]], [[NOT]]
31-
// CHECK-NEXT: [[ADD_PTR:%.*]] = getelementptr inbounds i8, ptr null, i64 [[AND]]
32-
// CHECK-NEXT: ret ptr [[ADD_PTR]]
31+
// CHECK-NEXT: [[TMP0:%.*]] = inttoptr i64 [[AND]] to ptr
32+
// CHECK-NEXT: ret ptr [[TMP0]]
3333
//
3434
char *glibc_ptr_align_commuted(char *base, char *pointer, long align_mask) {
3535
return (sizeof(long int) >= sizeof(void *) ? (char *)0 : (base)) +

0 commit comments

Comments
 (0)