tmsriram created this revision.
tmsriram added reviewers: rnk, davidxl.
tmsriram added a subscriber: cfe-commits.

clang does not pass pie-level and pic-level option values to the code generator 
with "-x ir" due to the following code in CompilerInvocation.cpp:

   if (DashX == IK_AST || DashX == IK_LLVM_IR) {
       ....;
    } else {
       ParseLangArgs(*Res.getLangOpts(), Args, DashX, Res.getTargetOpts(), 
Diags);
    }
pie-level and pic-level are LangArgs and are not parsed with "-x ir". They 
define macros __PIC__ and __PIE___ when set, see 
lib/Frontend/InitPreprocessor.cpp

Not setting LangOpts.PIELevel means that Options.PositionIndependentExecutable 
is always false even when -fPIE is used.  This patch fixes that.

I considered an alternative of making pie-level and pic-level CodeGen Options.  
However, since these options are used to set macros __PIE__, __PIC__, etc. this 
looked better as LangOpts.  Please let me know what you think.

For a broader context, I am working on optimizing accesses to global variables  
with -fPIE on x86-64 and this patch is the first step to get there.  In 
particular, I noticed the following code generated with clang:

hello.cpp:

int a = 0;

int main() {
  return a;
}

$ clang -O2 -fPIE hello.cc -S
$ cat hello.s

main:                                   # @main
        movq    a@GOTPCREL(%rip), %rax
        movl    (%rax), %eax
        retq

Creating a GOT entry for global 'a' is unnecessary as 'a' is defined in 
hello.cpp which will be linked into a position independent executable (fPIE).  
Hence, the definition of 'a' cannot be overridden and we can remove a load. The 
efficient access is this:

main:                                   # @main
        movl    a(%rip), %eax
        retq

I plan to address this in a separate patch.


http://reviews.llvm.org/D18843

Files:
  lib/Frontend/CompilerInvocation.cpp

Index: lib/Frontend/CompilerInvocation.cpp
===================================================================
--- lib/Frontend/CompilerInvocation.cpp
+++ lib/Frontend/CompilerInvocation.cpp
@@ -2142,6 +2142,12 @@
     // what the input type is.
     if (Args.hasArg(OPT_fobjc_arc))
       Res.getLangOpts()->ObjCAutoRefCount = 1;
+    // PIClevel and PIELevel are needed during code generation and this should 
be
+    // set regardless of the input type.
+    Res.getLangOpts()->PICLevel = getLastArgIntValue(Args, OPT_pic_level,
+                                                     0, Diags);
+    Res.getLangOpts()->PIELevel = getLastArgIntValue(Args, OPT_pie_level,
+                                                     0, Diags);
     parseSanitizerKinds("-fsanitize=", Args.getAllArgValues(OPT_fsanitize_EQ),
                         Diags, Res.getLangOpts()->Sanitize);
   } else {


Index: lib/Frontend/CompilerInvocation.cpp
===================================================================
--- lib/Frontend/CompilerInvocation.cpp
+++ lib/Frontend/CompilerInvocation.cpp
@@ -2142,6 +2142,12 @@
     // what the input type is.
     if (Args.hasArg(OPT_fobjc_arc))
       Res.getLangOpts()->ObjCAutoRefCount = 1;
+    // PIClevel and PIELevel are needed during code generation and this should be
+    // set regardless of the input type.
+    Res.getLangOpts()->PICLevel = getLastArgIntValue(Args, OPT_pic_level,
+                                                     0, Diags);
+    Res.getLangOpts()->PIELevel = getLastArgIntValue(Args, OPT_pie_level,
+                                                     0, Diags);
     parseSanitizerKinds("-fsanitize=", Args.getAllArgValues(OPT_fsanitize_EQ),
                         Diags, Res.getLangOpts()->Sanitize);
   } else {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to