[PATCH] D15777: [GlobalOpt] Globals used only in "main" can more easily be localized

Vaivaswatha Nagaraj via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 25 06:54:19 PST 2015


vaivaswatha created this revision.
vaivaswatha added reviewers: jmolloy, hfinkel.
vaivaswatha added a subscriber: llvm-commits.

This change relaxes the condition for a global, that is only used in "main", to be localized. This relaxation partially undoes the change done in http://reviews.llvm.org/rL253168.  

This is required because the condition that was added earlier, that a store to the global needs to dominate all other references to it in the function is not necessary if the only function that accesses the global is "main". So, similar to the condition prior to the above mentioned change, this change allows "main" as an exception.

http://reviews.llvm.org/D15777

Files:
  lib/Transforms/IPO/GlobalOpt.cpp
  test/Transforms/GlobalOpt/load-store-global.ll

Index: test/Transforms/GlobalOpt/load-store-global.ll
===================================================================
--- test/Transforms/GlobalOpt/load-store-global.ll
+++ test/Transforms/GlobalOpt/load-store-global.ll
@@ -36,3 +36,16 @@
 ; CHECK-NOT: load
 }
 
+; A global used only in "main" can always be localized.
+ at b = internal global i64 13, align 8
+; CHECK-NOT: @b
+
+define i64 @main(i32 %argc, i8** %argv) norecurse {
+; CHECK: %b = alloca
+; CHECK store i64 13
+  %a = load i64, i64* @b
+; CHECK %a = load i64
+  store i64 27, i64* @b
+; CHECK store i64 27
+  ret i64 %a
+}
Index: lib/Transforms/IPO/GlobalOpt.cpp
===================================================================
--- lib/Transforms/IPO/GlobalOpt.cpp
+++ lib/Transforms/IPO/GlobalOpt.cpp
@@ -1883,8 +1883,9 @@
       GV->getType()->getAddressSpace() == 0 &&
       !GV->isExternallyInitialized() &&
       allNonInstructionUsersCanBeMadeInstructions(GV) &&
-      GS.AccessingFunction->doesNotRecurse() &&
-      isPointerValueDeadOnEntryToFunction(GS.AccessingFunction, GV) ) {
+      ((GS.AccessingFunction->doesNotRecurse() &&
+        isPointerValueDeadOnEntryToFunction(GS.AccessingFunction, GV)) ||
+       (GS.AccessingFunction->getName() == "main"))) {
     DEBUG(dbgs() << "LOCALIZING GLOBAL: " << *GV << "\n");
     Instruction &FirstI = const_cast<Instruction&>(*GS.AccessingFunction
                                                    ->getEntryBlock().begin());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D15777.43630.patch
Type: text/x-patch
Size: 1460 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151225/8003c08f/attachment.bin>


More information about the llvm-commits mailing list