The PIC and PIE levels are not independent. In fact, if PIE is defined
it is always the same as PIC.

This is clear in the driver where ParsePICArgs returns a PIC level and
a IsPIE boolean. Unfortunately that is currently lost and we pass two
redundant levels down the pipeline.

This patch keeps a bool and a PIC level all the way down to codegen. I
intend to send a patch for LLVM tomorrow so that CodeGen too can be
simplified.

Cheers,
Rafael
From e36fea8d8fb20a549da60c22a1cd0751fa6ea70d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rafael=20=C3=81vila=20de=20Esp=C3=ADndola?=
 <rafael.espind...@gmail.com>
Date: Tue, 21 Jun 2016 18:46:31 -0400
Subject: [PATCH] Restructure the propagation of -fPIC/-fPIE.

The PIC and PIE levels are not independent. In fact, if PIE is defined
it is always the same as PIC.

This is clear in the driver where ParsePICArgs returns a PIC level and
a IsPIE boolean. Unfortunately that is currently lost and we pass two
redundant levels down the pipeline.

This patch keeps a bool and a PIC level all the way down to codegen. I
intend to send a patch for LLVM tomorrow so that CodeGen too can be
simplified.
---
 include/clang/Basic/LangOptions.def   |  2 +-
 include/clang/Driver/CC1Options.td    |  2 --
 include/clang/Driver/Options.td       |  2 +-
 lib/CodeGen/CGObjCGNU.cpp             |  2 +-
 lib/CodeGen/CodeGenModule.cpp         |  7 ++-----
 lib/Driver/Tools.cpp                  |  6 ++----
 lib/Frontend/CompilerInvocation.cpp   |  4 ++--
 lib/Frontend/InitPreprocessor.cpp     |  8 ++++----
 test/Driver/fsanitize.c               |  2 +-
 test/Driver/pic.c                     | 10 +++++-----
 test/Driver/ps4-pic.c                 |  4 ++--
 test/Modules/explicit-build-flags.cpp |  2 +-
 test/Preprocessor/pic.c               | 12 ++++++------
 13 files changed, 28 insertions(+), 35 deletions(-)

diff --git a/include/clang/Basic/LangOptions.def b/include/clang/Basic/LangOptions.def
index 3fb4e97..0356196 100644
--- a/include/clang/Basic/LangOptions.def
+++ b/include/clang/Basic/LangOptions.def
@@ -158,7 +158,7 @@ VALUE_LANGOPT(MaxTypeAlign  , 32, 0,
               "default maximum alignment for types")
 VALUE_LANGOPT(AlignDouble            , 1, 0, "Controls if doubles should be aligned to 8 bytes (x86 only)")
 COMPATIBLE_VALUE_LANGOPT(PICLevel    , 2, 0, "__PIC__ level")
-COMPATIBLE_VALUE_LANGOPT(PIELevel    , 2, 0, "__PIE__ level")
+COMPATIBLE_VALUE_LANGOPT(PIE         , 1, 0, "is pie")
 COMPATIBLE_LANGOPT(GNUInline         , 1, 0, "GNU inline semantics")
 COMPATIBLE_LANGOPT(NoInlineDefine    , 1, 0, "__NO_INLINE__ predefined macro")
 COMPATIBLE_LANGOPT(Deprecated        , 1, 0, "__DEPRECATED predefined macro")
diff --git a/include/clang/Driver/CC1Options.td b/include/clang/Driver/CC1Options.td
index f816844..d1dbda8 100644
--- a/include/clang/Driver/CC1Options.td
+++ b/include/clang/Driver/CC1Options.td
@@ -546,8 +546,6 @@ def fencode_extended_block_signature : Flag<["-"], "fencode-extended-block-signa
   HelpText<"enable extended encoding of block type signature">;
 def pic_level : Separate<["-"], "pic-level">,
   HelpText<"Value for __PIC__">;
-def pie_level : Separate<["-"], "pie-level">,
-  HelpText<"Value for __PIE__">;
 def fno_validate_pch : Flag<["-"], "fno-validate-pch">,
   HelpText<"Disable validation of precompiled headers">;
 def dump_deserialized_pch_decls : Flag<["-"], "dump-deserialized-decls">,
diff --git a/include/clang/Driver/Options.td b/include/clang/Driver/Options.td
index 9928758..d4c82bf 100644
--- a/include/clang/Driver/Options.td
+++ b/include/clang/Driver/Options.td
@@ -1034,7 +1034,7 @@ def fpcc_struct_return : Flag<["-"], "fpcc-struct-return">, Group<f_Group>, Flag
 def fpch_preprocess : Flag<["-"], "fpch-preprocess">, Group<f_Group>;
 def fpic : Flag<["-"], "fpic">, Group<f_Group>;
 def fno_pic : Flag<["-"], "fno-pic">, Group<f_Group>;
-def fpie : Flag<["-"], "fpie">, Group<f_Group>;
+def fpie : Flag<["-"], "fpie">, Group<f_Group>, Flags<[CC1Option]>;
 def fno_pie : Flag<["-"], "fno-pie">, Group<f_Group>;
 def fplugin_EQ : Joined<["-"], "fplugin=">, Group<f_Group>, Flags<[DriverOption]>, MetaVarName<"<dsopath>">,
   HelpText<"Load the named plugin (dynamic shared object)">;
diff --git a/lib/CodeGen/CGObjCGNU.cpp b/lib/CodeGen/CGObjCGNU.cpp
index bbe1b8b..f9f48c8 100644
--- a/lib/CodeGen/CGObjCGNU.cpp
+++ b/lib/CodeGen/CGObjCGNU.cpp
@@ -2830,7 +2830,7 @@ llvm::GlobalVariable *CGObjCGNU::ObjCIvarOffsetVariable(
     // to replace it with the real version for a library.  In non-PIC code you
     // must compile with the fragile ABI if you want to use ivars from a
     // GCC-compiled class.
-    if (CGM.getLangOpts().PICLevel || CGM.getLangOpts().PIELevel) {
+    if (CGM.getLangOpts().PICLevel) {
       llvm::GlobalVariable *IvarOffsetGV = new llvm::GlobalVariable(TheModule,
             Int32Ty, false,
             llvm::GlobalValue::PrivateLinkage, OffsetGuess, Name+".guess");
diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp
index a8abe72..5077727 100644
--- a/lib/CodeGen/CodeGenModule.cpp
+++ b/lib/CodeGen/CodeGenModule.cpp
@@ -480,11 +480,8 @@ void CodeGenModule::Release() {
   if (uint32_t PLevel = Context.getLangOpts().PICLevel) {
     assert(PLevel < 3 && "Invalid PIC Level");
     getModule().setPICLevel(static_cast<llvm::PICLevel::Level>(PLevel));
-  }
-
-  if (uint32_t PLevel = Context.getLangOpts().PIELevel) {
-    assert(PLevel < 3 && "Invalid PIE Level");
-    getModule().setPIELevel(static_cast<llvm::PIELevel::Level>(PLevel));
+    if (Context.getLangOpts().PIE)
+      getModule().setPIELevel(static_cast<llvm::PIELevel::Level>(PLevel));
   }
 
   SimplifyPersonality();
diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp
index c0d6c52..8d5413b 100644
--- a/lib/Driver/Tools.cpp
+++ b/lib/Driver/Tools.cpp
@@ -4016,10 +4016,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
   if (PICLevel > 0) {
     CmdArgs.push_back("-pic-level");
     CmdArgs.push_back(PICLevel == 1 ? "1" : "2");
-    if (IsPIE) {
-      CmdArgs.push_back("-pie-level");
-      CmdArgs.push_back(PICLevel == 1 ? "1" : "2");
-    }
+    if (IsPIE)
+      CmdArgs.push_back("-fpie");
   }
 
   if (Arg *A = Args.getLastArg(options::OPT_meabi)) {
diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp
index 0836bdc..a25339a 100644
--- a/lib/Frontend/CompilerInvocation.cpp
+++ b/lib/Frontend/CompilerInvocation.cpp
@@ -1899,7 +1899,7 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
   Opts.MaxTypeAlign = getLastArgIntValue(Args, OPT_fmax_type_align_EQ, 0, Diags);
   Opts.AlignDouble = Args.hasArg(OPT_malign_double);
   Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
-  Opts.PIELevel = getLastArgIntValue(Args, OPT_pie_level, 0, Diags);
+  Opts.PIE = Args.hasArg(OPT_fpie);
   Opts.Static = Args.hasArg(OPT_static_define);
   Opts.DumpRecordLayoutsSimple = Args.hasArg(OPT_fdump_record_layouts_simple);
   Opts.DumpRecordLayouts = Opts.DumpRecordLayoutsSimple
@@ -2334,7 +2334,7 @@ bool CompilerInvocation::CreateFromArgs(CompilerInvocation &Res,
     // PIClevel and PIELevel are needed during code generation and this should be
     // set regardless of the input type.
     LangOpts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
-    LangOpts.PIELevel = getLastArgIntValue(Args, OPT_pie_level, 0, Diags);
+    LangOpts.PIE = Args.hasArg(OPT_fpie);
     parseSanitizerKinds("-fsanitize=", Args.getAllArgValues(OPT_fsanitize_EQ),
                         Diags, LangOpts.Sanitize);
   } else {
diff --git a/lib/Frontend/InitPreprocessor.cpp b/lib/Frontend/InitPreprocessor.cpp
index 27ef59a..6b93c69 100644
--- a/lib/Frontend/InitPreprocessor.cpp
+++ b/lib/Frontend/InitPreprocessor.cpp
@@ -873,10 +873,10 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
   if (unsigned PICLevel = LangOpts.PICLevel) {
     Builder.defineMacro("__PIC__", Twine(PICLevel));
     Builder.defineMacro("__pic__", Twine(PICLevel));
-  }
-  if (unsigned PIELevel = LangOpts.PIELevel) {
-    Builder.defineMacro("__PIE__", Twine(PIELevel));
-    Builder.defineMacro("__pie__", Twine(PIELevel));
+    if (LangOpts.PIE) {
+      Builder.defineMacro("__PIE__", Twine(PICLevel));
+      Builder.defineMacro("__pie__", Twine(PICLevel));
+    }
   }
 
   // Macros to control C99 numerics and <float.h>
diff --git a/test/Driver/fsanitize.c b/test/Driver/fsanitize.c
index fa43bfb..cda5d1f 100644
--- a/test/Driver/fsanitize.c
+++ b/test/Driver/fsanitize.c
@@ -188,7 +188,7 @@
 // CHECK-NO-PIE: "-mrelocation-model" "static"
 // CHECK-NO-PIE-NOT: "-pie"
 
-// CHECK-PIE: "-mrelocation-model" "pic" "-pic-level" "2" "-pie-level" "2"
+// CHECK-PIE: "-mrelocation-model" "pic" "-pic-level" "2" "-fpie"
 // CHECK-PIE: "-pie"
 
 // RUN: %clang -target arm-linux-androideabi %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ANDROID-NO-ASAN
diff --git a/test/Driver/pic.c b/test/Driver/pic.c
index aeb2ee3..f84bc97 100644
--- a/test/Driver/pic.c
+++ b/test/Driver/pic.c
@@ -3,7 +3,7 @@
 //
 // CHECK-NO-PIC: "-mrelocation-model" "static"
 // CHECK-NO-PIC-NOT: "-pic-level"
-// CHECK-NO-PIC-NOT: "-pie-level"
+// CHECK-NO-PIC-NOT: "-fpie"
 //
 // CHECK-PIC1: "-mrelocation-model" "pic"
 // CHECK-PIC1: "-pic-level" "1"
@@ -16,11 +16,11 @@
 //
 // CHECK-PIE1: "-mrelocation-model" "pic"
 // CHECK-PIE1: "-pic-level" "1"
-// CHECK-PIE1: "-pie-level" "1"
+// CHECK-PIE1: "-fpie"
 //
 // CHECK-PIE2: "-mrelocation-model" "pic"
 // CHECK-PIE2: "-pic-level" "2"
-// CHECK-PIE2: "-pie-level" "2"
+// CHECK-PIE2: "-fpie"
 //
 // CHECK-PIE-LD: "{{.*}}ld{{(.exe)?}}"
 // CHECK-PIE-LD: "-pie"
@@ -31,11 +31,11 @@
 //
 // CHECK-DYNAMIC-NO-PIC-32: "-mrelocation-model" "dynamic-no-pic"
 // CHECK-DYNAMIC-NO-PIC-32-NOT: "-pic-level"
-// CHECK-DYNAMIC-NO-PIC-32-NOT: "-pie-level"
+// CHECK-DYNAMIC-NO-PIC-32-NOT: "-fpie"
 //
 // CHECK-DYNAMIC-NO-PIC-64: "-mrelocation-model" "dynamic-no-pic"
 // CHECK-DYNAMIC-NO-PIC-64: "-pic-level" "2"
-// CHECK-DYNAMIC-NO-PIC-64-NOT: "-pie-level"
+// CHECK-DYNAMIC-NO-PIC-64-NOT: "-fpie"
 //
 // CHECK-NON-DARWIN-DYNAMIC-NO-PIC: error: unsupported option '-mdynamic-no-pic' for target 'i386-unknown-unknown'
 //
diff --git a/test/Driver/ps4-pic.c b/test/Driver/ps4-pic.c
index 0cf9ad5..95e1fad 100644
--- a/test/Driver/ps4-pic.c
+++ b/test/Driver/ps4-pic.c
@@ -6,7 +6,7 @@
 //
 // CHECK-NO-PIC: "-mrelocation-model" "static"
 // CHECK-NO-PIC-NOT: "-pic-level"
-// CHECK-NO-PIC-NOT: "-pie-level"
+// CHECK-NO-PIC-NOT: "-fpie"
 //
 // CHECK-DYNAMIC-NO-PIC2: unsupported option '-mdynamic-no-pic'
 // CHECK-DYNAMIC-NO-PIC2: "-mrelocation-model" "dynamic-no-pic"
@@ -15,7 +15,7 @@
 // CHECK-PIC2: "-pic-level" "2"
 //
 // CHECK-PIE2: "-mrelocation-model" "pic"
-// CHECK-PIE2: "-pie-level" "2"
+// CHECK-PIE2: "-fpie"
 //
 // CHECK-NOPIC-IGNORED: using '-fPIC'
 // CHECK-NOPIC-IGNORED: "-mrelocation-model" "pic"
diff --git a/test/Modules/explicit-build-flags.cpp b/test/Modules/explicit-build-flags.cpp
index 39fd8d1..7d6bf5f 100644
--- a/test/Modules/explicit-build-flags.cpp
+++ b/test/Modules/explicit-build-flags.cpp
@@ -24,7 +24,7 @@
 
 // Can use the module if -fPIC/-fPIE flags change.
 // RUN: %clang_cc1 -fmodules -DBAR=2 -pic-level 2 -x c++ -fmodule-map-file=%t/map -fmodule-file=%t/tmp.pcm -verify -I%t %s
-// RUN: %clang_cc1 -fmodules -DBAR=2 -pie-level 1 -x c++ -fmodule-map-file=%t/map -fmodule-file=%t/tmp.pcm -verify -I%t %s
+// RUN: %clang_cc1 -fmodules -DBAR=2 -pic-level 1 -fpie -x c++ -fmodule-map-file=%t/map -fmodule-file=%t/tmp.pcm -verify -I%t %s
 
 // Can use the module if -static flag changes.
 // RUN: %clang_cc1 -fmodules -DBAR=2 -static-define -x c++ -fmodule-map-file=%t/map -fmodule-file=%t/tmp.pcm -verify -I%t %s
diff --git a/test/Preprocessor/pic.c b/test/Preprocessor/pic.c
index 3e649ee..3519033 100644
--- a/test/Preprocessor/pic.c
+++ b/test/Preprocessor/pic.c
@@ -19,16 +19,16 @@
 // CHECK-PIC2: #define __pic__ 2
 // CHECK-PIC2-NOT: #define __pie__
 //
-// RUN: %clang_cc1 -pie-level 1 -dM -E -o - %s \
+// RUN: %clang_cc1 -pic-level 1 -fpie -dM -E -o - %s \
 // RUN:   | FileCheck --check-prefix=CHECK-PIE1 %s
-// CHECK-PIE1-NOT: #define __PIC__
+// CHECK-PIE1: #define __PIC__ 1
 // CHECK-PIE1: #define __PIE__ 1
-// CHECK-PIE1-NOT: #define __pic__
+// CHECK-PIE1: #define __pic__ 1
 // CHECK-PIE1: #define __pie__ 1
 //
-// RUN: %clang_cc1 -pie-level 2 -dM -E -o - %s \
+// RUN: %clang_cc1 -pic-level 2 -fpie -dM -E -o - %s \
 // RUN:   | FileCheck --check-prefix=CHECK-PIE2 %s
-// CHECK-PIE2-NOT: #define __PIC__
+// CHECK-PIE2: #define __PIC__ 2
 // CHECK-PIE2: #define __PIE__ 2
-// CHECK-PIE2-NOT: #define __pic__
+// CHECK-PIE2: #define __pic__ 2
 // CHECK-PIE2: #define __pie__ 2
-- 
2.5.5

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to