<div dir="ltr">Not quite, at least not for aarch64 which allows only globals <span style="font-size:13.1999998092651px;line-height:1.5">as the entire module and will optimize accordingly.</span><div><br></div><div>-eric</div></div><br><div class="gmail_quote">On Mon Feb 23 2015 at 12:32:00 PM Akira Hatanaka <<a href="mailto:ahatanak@gmail.com">ahatanak@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Feb 23, 2015 at 11:28 AM, Eric Christopher <span dir="ltr"><<a href="mailto:echristo@gmail.com" target="_blank">echristo@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: echristo<br>
Date: Mon Feb 23 13:28:45 2015<br>
New Revision: 230245<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=230245&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=230245&view=rev</a><br>
Log:<br>
Rewrite the global merge pass to be subprogram agnostic for now.<br>
It was previously using the subtarget to get values for the global<br>
offset without actually checking each function as it was generating<br>
code. Go ahead and solidify the current behavior and make the<br>
existing FIXMEs more prominent.<br>
<br>
As a note the ARM backend previously had a thumb1 and non-thumb1<br>
set of defaults. Only the former was tested so I've changed the<br>
behavior to only use that for now.<br>
<br>
Modified:<br>
    llvm/trunk/include/llvm/Target/TargetLowering.h<br>
    llvm/trunk/include/llvm/Transforms/Scalar.h<br>
    llvm/trunk/lib/CodeGen/GlobalMerge.cpp<br>
    llvm/trunk/lib/Target/AArch64/AArch64ISelLowering.cpp<br>
    llvm/trunk/lib/Target/AArch64/AArch64ISelLowering.h<br>
    llvm/trunk/lib/Target/AArch64/AArch64TargetMachine.cpp<br>
    llvm/trunk/lib/Target/ARM/ARMISelLowering.cpp<br>
    llvm/trunk/lib/Target/ARM/ARMISelLowering.h<br>
    llvm/trunk/lib/Target/ARM/ARMTargetMachine.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/Target/TargetLowering.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Target/TargetLowering.h?rev=230245&r1=230244&r2=230245&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Target/TargetLowering.h?rev=230245&r1=230244&r2=230245&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Target/TargetLowering.h (original)<br>
+++ llvm/trunk/include/llvm/Target/TargetLowering.h Mon Feb 23 13:28:45 2015<br>
@@ -959,12 +959,6 @@ public:<br>
     return false;<br>
   }<br>
<br>
-  /// Returns the maximal possible offset which can be used for loads / stores<br>
-  /// from the global.<br>
-  virtual unsigned getMaximalGlobalOffset() const {<br>
-    return 0;<br>
-  }<br>
-<br>
   /// Returns true if a cast between SrcAS and DestAS is a noop.<br>
   virtual bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const {<br>
     return false;<br>
<br>
Modified: llvm/trunk/include/llvm/Transforms/Scalar.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Scalar.h?rev=230245&r1=230244&r2=230245&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Scalar.h?rev=230245&r1=230244&r2=230245&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Transforms/Scalar.h (original)<br>
+++ llvm/trunk/include/llvm/Transforms/Scalar.h Mon Feb 23 13:28:45 2015<br>
@@ -145,7 +145,7 @@ Pass *createLICMPass();<br>
 //<br>
 Pass *createLoopStrengthReducePass();<br>
<br>
-Pass *createGlobalMergePass(const TargetMachine *TM = nullptr);<br>
+Pass *createGlobalMergePass(const TargetMachine *TM, unsigned MaximalOffset);<br>
<br>
 //===----------------------------------------------------------------------===//<br>
 //<br>
<br>
Modified: llvm/trunk/lib/CodeGen/GlobalMerge.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/GlobalMerge.cpp?rev=230245&r1=230244&r2=230245&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/GlobalMerge.cpp?rev=230245&r1=230244&r2=230245&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/CodeGen/GlobalMerge.cpp (original)<br>
+++ llvm/trunk/lib/CodeGen/GlobalMerge.cpp Mon Feb 23 13:28:45 2015<br>
@@ -90,10 +90,16 @@ EnableGlobalMergeOnExternal("global-merg<br>
      cl::desc("Enable global merge pass on external linkage"),<br>
      cl::init(false));<br>
<br>
-STATISTIC(NumMerged      , "Number of globals merged");<br>
+STATISTIC(NumMerged, "Number of globals merged");<br>
 namespace {<br>
   class GlobalMerge : public FunctionPass {<br>
     const TargetMachine *TM;<br>
+    const DataLayout *DL;<br>
+    // FIXME: Infer the maximum possible offset depending on the actual users<br>
+    // (these max offsets are different for the users inside Thumb or ARM<br>
+    // functions), see the code that passes in the offset in the ARM backend<br>
+    // for more information.<br>
+    unsigned MaxOffset;<br>
<br>
     bool doMerge(SmallVectorImpl<GlobalVariable*> &Globals,<br>
                  Module &M, bool isConst, unsigned AddrSpace) const;<br>
@@ -117,8 +123,10 @@ namespace {<br>
<br>
   public:<br>
     static char ID;             // Pass identification, replacement for typeid.<br>
-    explicit GlobalMerge(const TargetMachine *TM = nullptr)<br>
-      : FunctionPass(ID), TM(TM) {<br>
+    explicit GlobalMerge(const TargetMachine *TM = nullptr,<br>
+                         unsigned MaximalOffset = 0)<br>
+        : FunctionPass(ID), TM(TM), DL(TM->getDataLayout()),<br>
+          MaxOffset(MaximalOffset) {<br>
       initializeGlobalMergePass(*PassRegistry::getPassRegistry());<br>
     }<br>
<br>
@@ -138,22 +146,16 @@ namespace {<br>
 } // end anonymous namespace<br>
<br>
 char GlobalMerge::ID = 0;<br>
-INITIALIZE_TM_PASS(GlobalMerge, "global-merge", "Merge global variables",<br>
-                   false, false)<br>
+INITIALIZE_PASS_BEGIN(GlobalMerge, "global-merge", "Merge global variables",<br>
+                      false, false)<br>
+INITIALIZE_PASS_END(GlobalMerge, "global-merge", "Merge global variables",<br>
+                    false, false)<br>
<br>
 bool GlobalMerge::doMerge(SmallVectorImpl<GlobalVariable*> &Globals,<br>
                           Module &M, bool isConst, unsigned AddrSpace) const {<br>
-  const TargetLowering *TLI = TM->getSubtargetImpl()->getTargetLowering();<br>
-  const DataLayout *DL = TM->getDataLayout();<br>
-<br>
-  // FIXME: Infer the maximum possible offset depending on the actual users<br>
-  // (these max offsets are different for the users inside Thumb or ARM<br>
-  // functions)<br>
-  unsigned MaxOffset = TLI->getMaximalGlobalOffset();<br>
-<br>
   // FIXME: Find better heuristics<br>
   std::stable_sort(Globals.begin(), Globals.end(),<br>
-                   [DL](const GlobalVariable *GV1, const GlobalVariable *GV2) {<br>
+                   [this](const GlobalVariable *GV1, const GlobalVariable *GV2) {<br>
     Type *Ty1 = cast<PointerType>(GV1->getType())->getElementType();<br>
     Type *Ty2 = cast<PointerType>(GV2->getType())->getElementType();<br>
<br>
@@ -282,9 +284,6 @@ bool GlobalMerge::doInitialization(Modul<br>
<br>
   DenseMap<unsigned, SmallVector<GlobalVariable*, 16> > Globals, ConstGlobals,<br>
                                                         BSSGlobals;<br>
-  const TargetLowering *TLI = TM->getSubtargetImpl()->getTargetLowering();<br>
-  const DataLayout *DL = TM->getDataLayout();<br>
-  unsigned MaxOffset = TLI->getMaximalGlobalOffset();<br>
   bool Changed = false;<br>
   setMustKeepGlobalVariables(M);<br>
<br>
@@ -357,6 +356,6 @@ bool GlobalMerge::doFinalization(Module<br>
   return false;<br>
 }<br>
<br>
-Pass *llvm::createGlobalMergePass(const TargetMachine *TM) {<br>
-  return new GlobalMerge(TM);<br>
+Pass *llvm::createGlobalMergePass(const TargetMachine *TM, unsigned Offset) {<br>
+  return new GlobalMerge(TM, Offset);<br>
 }<br>
<br>
Modified: llvm/trunk/lib/Target/AArch64/AArch64ISelLowering.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/AArch64/AArch64ISelLowering.cpp?rev=230245&r1=230244&r2=230245&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/AArch64/AArch64ISelLowering.cpp?rev=230245&r1=230244&r2=230245&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Target/AArch64/AArch64ISelLowering.cpp (original)<br>
+++ llvm/trunk/lib/Target/AArch64/AArch64ISelLowering.cpp Mon Feb 23 13:28:45 2015<br>
@@ -730,13 +730,6 @@ MVT AArch64TargetLowering::getScalarShif<br>
   return MVT::i64;<br>
 }<br>
<br>
-unsigned AArch64TargetLowering::getMaximalGlobalOffset() const {<br>
-  // FIXME: On AArch64, this depends on the type.<br>
-  // Basically, the addressable offsets are up to 4095 * Ty.getSizeInBytes().<br>
-  // and the offset has to be a multiple of the related size in bytes.<br>
-  return 4095;<br>
-}<br>
-<br>
 FastISel *<br>
 AArch64TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,<br>
                                       const TargetLibraryInfo *libInfo) const {<br>
<br>
Modified: llvm/trunk/lib/Target/AArch64/AArch64ISelLowering.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/AArch64/AArch64ISelLowering.h?rev=230245&r1=230244&r2=230245&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/AArch64/AArch64ISelLowering.h?rev=230245&r1=230244&r2=230245&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Target/AArch64/AArch64ISelLowering.h (original)<br>
+++ llvm/trunk/lib/Target/AArch64/AArch64ISelLowering.h Mon Feb 23 13:28:45 2015<br>
@@ -246,10 +246,6 @@ public:<br>
   /// getFunctionAlignment - Return the Log2 alignment of this function.<br>
   unsigned getFunctionAlignment(const Function *F) const;<br>
<br>
-  /// getMaximalGlobalOffset - Returns the maximal possible offset which can<br>
-  /// be used for loads / stores from the global.<br>
-  unsigned getMaximalGlobalOffset() const override;<br>
-<br>
   /// Returns true if a cast between SrcAS and DestAS is a noop.<br>
   bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const override {<br>
     // Addrspacecasts are always noops.<br>
<br>
Modified: llvm/trunk/lib/Target/AArch64/AArch64TargetMachine.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/AArch64/AArch64TargetMachine.cpp?rev=230245&r1=230244&r2=230245&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/AArch64/AArch64TargetMachine.cpp?rev=230245&r1=230244&r2=230245&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Target/AArch64/AArch64TargetMachine.cpp (original)<br>
+++ llvm/trunk/lib/Target/AArch64/AArch64TargetMachine.cpp Mon Feb 23 13:28:45 2015<br>
@@ -236,8 +236,11 @@ bool AArch64PassConfig::addPreISel() {<br>
   // get a chance to be merged<br>
   if (TM->getOptLevel() != CodeGenOpt::None && EnablePromoteConstant)<br>
     addPass(createAArch64PromoteConstantPass());<br>
+  // FIXME: On AArch64, this depends on the type.<br>
+  // Basically, the addressable offsets are up to 4095 * Ty.getSizeInBytes().<br>
+  // and the offset has to be a multiple of the related size in bytes.<br>
   if (TM->getOptLevel() != CodeGenOpt::None)<br>
-    addPass(createGlobalMergePass(TM));<br>
+    addPass(createGlobalMergePass(TM, 4095));<br>
   if (TM->getOptLevel() != CodeGenOpt::None)<br>
     addPass(createAArch64AddressTypePromotionPass());<br>
<br>
<br>
Modified: llvm/trunk/lib/Target/ARM/ARMISelLowering.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/ARM/ARMISelLowering.cpp?rev=230245&r1=230244&r2=230245&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/ARM/ARMISelLowering.cpp?rev=230245&r1=230244&r2=230245&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Target/ARM/ARMISelLowering.cpp (original)<br>
+++ llvm/trunk/lib/Target/ARM/ARMISelLowering.cpp Mon Feb 23 13:28:45 2015<br>
@@ -1170,12 +1170,6 @@ ARMTargetLowering::createFastISel(Functi<br>
   return ARM::createFastISel(funcInfo, libInfo);<br>
 }<br>
<br>
-/// getMaximalGlobalOffset - Returns the maximal possible offset which can<br>
-/// be used for loads / stores from the global.<br>
-unsigned ARMTargetLowering::getMaximalGlobalOffset() const {<br>
-  return (Subtarget->isThumb1Only() ? 127 : 4095);<br>
-}<br>
-<br>
 Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {<br>
   unsigned NumVals = N->getNumValues();<br>
   if (!NumVals)<br>
<br>
Modified: llvm/trunk/lib/Target/ARM/ARMISelLowering.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/ARM/ARMISelLowering.h?rev=230245&r1=230244&r2=230245&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/ARM/ARMISelLowering.h?rev=230245&r1=230244&r2=230245&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Target/ARM/ARMISelLowering.h (original)<br>
+++ llvm/trunk/lib/Target/ARM/ARMISelLowering.h Mon Feb 23 13:28:45 2015<br>
@@ -353,10 +353,6 @@ namespace llvm {<br>
     /// specified value type.<br>
     const TargetRegisterClass *getRegClassFor(MVT VT) const override;<br>
<br>
-    /// getMaximalGlobalOffset - Returns the maximal possible offset which can<br>
-    /// be used for loads / stores from the global.<br>
-    unsigned getMaximalGlobalOffset() const override;<br>
-<br>
     /// Returns true if a cast between SrcAS and DestAS is a noop.<br>
     bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const override {<br>
       // Addrspacecasts are always noops.<br>
<br>
Modified: llvm/trunk/lib/Target/ARM/ARMTargetMachine.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/ARM/ARMTargetMachine.cpp?rev=230245&r1=230244&r2=230245&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/ARM/ARMTargetMachine.cpp?rev=230245&r1=230244&r2=230245&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Target/ARM/ARMTargetMachine.cpp (original)<br>
+++ llvm/trunk/lib/Target/ARM/ARMTargetMachine.cpp Mon Feb 23 13:28:45 2015<br>
@@ -326,7 +326,12 @@ void ARMPassConfig::addIRPasses() {<br>
<br>
 bool ARMPassConfig::addPreISel() {<br>
   if (TM->getOptLevel() != CodeGenOpt::None)<br>
-    addPass(createGlobalMergePass(TM));<br>
+    // FIXME: This is using the thumb1 only constant value for<br>
+    // maximal global offset for merging globals. We may want<br>
+    // to look into using the old value for non-thumb1 code of<br>
+    // 4095 based on the TargetMachine, but this starts to become<br>
+    // tricky when doing code gen per function.<br>
+    addPass(createGlobalMergePass(TM, 127));<br>
<br></blockquote><div><br></div></div></div></div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>Is it possible to scan all the functions in the module and find the minimum of the MaximalOffsets instead of passing the value to the constructor of GlobalMerge? If all functions in the module are non-thumb1, 4095 is used as the maximal offset (which I think allows a larger number of globals to be merged), otherwise 127 is used.</div></div></div></div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
   return false;<br>
 }<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu" target="_blank">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div></div></div></blockquote></div>