Skip to content

Commit 7e42a82

Browse files
committed
Enable -fextend-lifetimes at -Og
1 parent c5cd1e9 commit 7e42a82

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

clang/docs/CommandGuide/clang.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,11 @@ Code Generation Options
442442
:option:`-Oz` Like :option:`-Os` (and thus :option:`-O2`), but reduces code
443443
size further.
444444

445-
:option:`-Og` Like :option:`-O1`. In future versions, this option might
446-
disable different optimizations in order to improve debuggability.
445+
:option:`-Og` Similar to :option:`-O1`, but with slightly reduced
446+
optimization and better variable visibility. The same optimizations are run
447+
as at :option:`-O1`, but the :option:`-fextend-lifetimes` flag is also
448+
set, which tries to prevent optimizations from reducing the lifetime of
449+
user variables.
447450

448451
:option:`-O` Equivalent to :option:`-O1`.
449452

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,10 @@ Modified Compiler Flags
441441
``memset`` and similar functions for which it is a documented undefined
442442
behavior. It is implied by ``-Wnontrivial-memaccess``
443443

444+
- The ``-Og`` optimization flag now sets ``-fextend-lifetimes``, a new compiler
445+
flag, resulting in slightly reduced optimization compared to ``-O1`` in
446+
exchange for improved variable visibility.
447+
444448
Removed Compiler Flags
445449
-------------------------
446450

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,6 +1834,14 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args,
18341834
Opts.setInlining(CodeGenOptions::NormalInlining);
18351835
}
18361836

1837+
// If we have specified -Og and have not set any explicit -fextend-lifetimes
1838+
// value, then default to -fextend-lifetimes=all.
1839+
if (Arg *A = Args.getLastArg(options::OPT_O_Group);
1840+
A && A->containsValue("g")) {
1841+
if (!Args.hasArg(options::OPT_fextend_lifetimes))
1842+
Opts.ExtendLifetimes = true;
1843+
}
1844+
18371845
// PIC defaults to -fno-direct-access-external-data while non-PIC defaults to
18381846
// -fdirect-access-external-data.
18391847
Opts.DirectAccessExternalData =
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/// Check that we generate fake uses only when -fextend-lifetimes is set and we
2+
/// are not setting optnone, or when we have optimizations set to -Og and we have
3+
/// not passed -fno-extend-lifetimes.
4+
// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -O0 -fextend-lifetimes %s -o - | FileCheck %s
5+
// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -O0 -disable-O0-optnone -fextend-lifetimes %s -o - | FileCheck %s --check-prefixes=CHECK,EXTEND
6+
// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -Og %s -o - | FileCheck %s --check-prefixes=CHECK,EXTEND
7+
// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -Og -fno-extend-lifetimes %s -o - | FileCheck %s
8+
9+
/// Test various optimization flags with -fextend-lifetimes...
10+
// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -O1 %s -o - | FileCheck %s
11+
// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -O1 -fextend-lifetimes %s -o - | FileCheck %s --check-prefixes=CHECK,EXTEND
12+
// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -O2 %s -o - | FileCheck %s
13+
// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -O2 -fextend-lifetimes %s -o - | FileCheck %s --check-prefixes=CHECK,EXTEND
14+
// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -O3 %s -o - | FileCheck %s
15+
// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -O3 -fextend-lifetimes %s -o - | FileCheck %s --check-prefixes=CHECK,EXTEND
16+
// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -Os %s -o - | FileCheck %s
17+
// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -Os -fextend-lifetimes %s -o - | FileCheck %s --check-prefixes=CHECK,EXTEND
18+
// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -Oz %s -o - | FileCheck %s
19+
// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -Oz -fextend-lifetimes %s -o - | FileCheck %s --check-prefixes=CHECK,EXTEND
20+
21+
// CHECK-LABEL: define{{.*}} void @_Z3fooi(i32{{.*}} %a)
22+
// CHECK-NEXT: entry:
23+
// CHECK-NEXT: %a.addr = alloca i32
24+
// CHECK-NEXT: store i32 %a, ptr %a.addr
25+
// EXTEND-NEXT: %fake.use = load i32, ptr %a.addr
26+
// EXTEND-NEXT: call void (...) @llvm.fake.use(i32 %fake.use)
27+
// CHECK-NEXT: ret void
28+
// CHECK-NEXT: }
29+
30+
void foo(int a) {}

0 commit comments

Comments
 (0)