[llvm] r366952 - IR: Teach GlobalIndirectSymbol::getBaseObject() to handle more kinds of expressions.

Peter Collingbourne via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 24 15:23:05 PDT 2019


Author: pcc
Date: Wed Jul 24 15:23:05 2019
New Revision: 366952

URL: http://llvm.org/viewvc/llvm-project?rev=366952&view=rev
Log:
IR: Teach GlobalIndirectSymbol::getBaseObject() to handle more kinds of expressions.

For aliases, any expression that lowers at the MC level to global_object or
global_object+constant is valid at the object file level. getBaseObject()
should return a result if the aliasee ends up being of that form even if
the IR used to produce it is somewhat unconventional.

Note that this is different from what stripInBoundsOffsets() and that family
of functions is doing. Those functions are concerned about semantic properties
of IR, whereas here we only care about the lowering result.

Therefore reimplement getBaseObject() in a way that matches the lowering
result. This fixes a crash when producing a summary for aliases such as
that in the included test case.

Differential Revision: https://reviews.llvm.org/D65115

Added:
    llvm/trunk/test/Bitcode/thinlto-alias3.ll
Modified:
    llvm/trunk/include/llvm/IR/GlobalIndirectSymbol.h
    llvm/trunk/lib/IR/Globals.cpp
    llvm/trunk/test/Linker/comdat8.ll

Modified: llvm/trunk/include/llvm/IR/GlobalIndirectSymbol.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/GlobalIndirectSymbol.h?rev=366952&r1=366951&r2=366952&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/GlobalIndirectSymbol.h (original)
+++ llvm/trunk/include/llvm/IR/GlobalIndirectSymbol.h Wed Jul 24 15:23:05 2019
@@ -54,9 +54,7 @@ public:
           static_cast<const GlobalIndirectSymbol *>(this)->getIndirectSymbol());
   }
 
-  const GlobalObject *getBaseObject() const {
-    return dyn_cast<GlobalObject>(getIndirectSymbol()->stripInBoundsOffsets());
-  }
+  const GlobalObject *getBaseObject() const;
   GlobalObject *getBaseObject() {
     return const_cast<GlobalObject *>(
               static_cast<const GlobalIndirectSymbol *>(this)->getBaseObject());

Modified: llvm/trunk/lib/IR/Globals.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Globals.cpp?rev=366952&r1=366951&r2=366952&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Globals.cpp (original)
+++ llvm/trunk/lib/IR/Globals.cpp Wed Jul 24 15:23:05 2019
@@ -427,6 +427,43 @@ GlobalIndirectSymbol::GlobalIndirectSymb
     Op<0>() = Symbol;
 }
 
+static const GlobalObject *
+findBaseObject(const Constant *C, DenseSet<const GlobalAlias *> &Aliases) {
+  if (auto *GO = dyn_cast<GlobalObject>(C))
+    return GO;
+  if (auto *GA = dyn_cast<GlobalAlias>(C))
+    if (Aliases.insert(GA).second)
+      return findBaseObject(GA->getOperand(0), Aliases);
+  if (auto *CE = dyn_cast<ConstantExpr>(C)) {
+    switch (CE->getOpcode()) {
+    case Instruction::Add: {
+      auto *LHS = findBaseObject(CE->getOperand(0), Aliases);
+      auto *RHS = findBaseObject(CE->getOperand(1), Aliases);
+      if (LHS && RHS)
+        return nullptr;
+      return LHS ? LHS : RHS;
+    }
+    case Instruction::Sub: {
+      if (findBaseObject(CE->getOperand(1), Aliases))
+        return nullptr;
+      return findBaseObject(CE->getOperand(0), Aliases);
+    }
+    case Instruction::IntToPtr:
+    case Instruction::PtrToInt:
+    case Instruction::BitCast:
+    case Instruction::GetElementPtr:
+      return findBaseObject(CE->getOperand(0), Aliases);
+    default:
+      break;
+    }
+  }
+  return nullptr;
+}
+
+const GlobalObject *GlobalIndirectSymbol::getBaseObject() const {
+  DenseSet<const GlobalAlias *> Aliases;
+  return findBaseObject(getOperand(0), Aliases);
+}
 
 //===----------------------------------------------------------------------===//
 // GlobalAlias Implementation

Added: llvm/trunk/test/Bitcode/thinlto-alias3.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Bitcode/thinlto-alias3.ll?rev=366952&view=auto
==============================================================================
--- llvm/trunk/test/Bitcode/thinlto-alias3.ll (added)
+++ llvm/trunk/test/Bitcode/thinlto-alias3.ll Wed Jul 24 15:23:05 2019
@@ -0,0 +1,11 @@
+; Test that inttoptr, add and ptrtoint don't cause problems in alias summaries.
+; RUN: opt -module-summary %s -o - | llvm-dis | FileCheck %s
+
+; CHECK: ^1 = gv: (name: "a", {{.*}} aliasee: ^2
+; CHECK: ^2 = gv: (name: "b",
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+ at a = alias i32, i32* inttoptr (i64 add (i64 ptrtoint (i32* @b to i64), i64 1297036692682702848) to i32*)
+ at b = global i32 1

Modified: llvm/trunk/test/Linker/comdat8.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Linker/comdat8.ll?rev=366952&r1=366951&r2=366952&view=diff
==============================================================================
--- llvm/trunk/test/Linker/comdat8.ll (original)
+++ llvm/trunk/test/Linker/comdat8.ll Wed Jul 24 15:23:05 2019
@@ -2,7 +2,7 @@
 
 $c1 = comdat largest
 
- at some_name = private unnamed_addr constant i32 42, comdat($c1)
- at c1 = alias i8, inttoptr (i32 ptrtoint (i32* @some_name to i32) to i8*)
+ at some_name = unnamed_addr constant i32 42, comdat($c1)
+ at c1 = alias i8, inttoptr (i32 1 to i8*)
 
 ; CHECK: COMDAT key involves incomputable alias size.




More information about the llvm-commits mailing list