[llvm-dev] c++ programs type name changed in llvm ir after linking

Xiaokang Fan via llvm-dev llvm-dev at lists.llvm.org
Wed May 25 22:46:37 PDT 2016


Hi All,

Recently I am using clang++ (3.8.0) to compile some c++ programs and find
that some type names will be changed after linking different modules. Here
is a simple example:

I have two files: main.cpp and b.cpp

main.cpp
#include <iostream>
using namespace std;

class A {
  public:
    virtual void f() {
      cout << "A:\tf\n";
    }
};

int main ()
{
  A *p = new A;
  p->f();
  return 0;
}

b.cpp
#include <iostream>
using namespace std;

class X {
public:
    virtual int g();
};

int X::g()
{
    cout << "X:\tg\n";
    return 0;
}

I use the following commands to compile these two files separately and then
link them tegother:
clang++ -c -emit-llvm -o b.bc b.cpp
clang++ -c -emit-llvm -o main.bc main.cpp
llvm-link -o whole.bc b.bc main.bc
opt -mem2reg -o whole.mem2reg.bc whole.bc
llvm-dis -o whole.mem2reg.ll whole.mem2reg.bc

In the file whole.mem2reg.ll, type "class.A" has been replaced by "class.X"
in function main, the constructor of class A and the virtual member
function f of class A:

; Function Attrs: norecurse uwtable
define i32 @main() #3 {
  %1 = call noalias i8* @_Znwm(i64 8) #6
  %2 = bitcast i8* %1 to %class.X*
  call void @_ZN1AC2Ev(%class.X* %2) #2
  %3 = bitcast %class.X* %2 to void (%class.X*)***
  %4 = load void (%class.X*)**, void (%class.X*)*** %3, align 8
  %5 = getelementptr inbounds void (%class.X*)*, void (%class.X*)** %4, i64
0
  %6 = load void (%class.X*)*, void (%class.X*)** %5, align 8
  call void %6(%class.X* %2)
  ret i32 0
}

; Function Attrs: inlinehint nounwind uwtable
define linkonce_odr void @_ZN1AC2Ev(%class.X* %this) unnamed_addr #5 comdat
align 2 {
  %1 = bitcast %class.X* %this to i32 (...)***
  store i32 (...)** bitcast (i8** getelementptr inbounds ([3 x i8*], [3 x
i8*]* @_ZTV1A, i64 0, i64 2) to i32 (...)**), i32 (...)*** %1, align 8
  ret void
}

; Function Attrs: uwtable
define linkonce_odr void @_ZN1A1fEv(%class.X* %this) unnamed_addr #0 comdat
align 2 {
  %1 = call dereferenceable(272) %"class.std::basic_ostream"*
@_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(%"class.std::basic_ostream"*
dereferenceable(272) @_ZSt4cout, i8* getelementptr inbounds ([6 x i8], [6 x
i8]* @.str.4, i32 0, i32 0))
  ret void
}

Does anyone know why llvm replace the type name and how to stop llvm from
doing this?

Thanks

-- 
Xiaokang Fan

PhD Student
School of Computer Science and Engineering
The University of New South Wales
Sydney 2052 Australia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160526/afac3bb6/attachment.html>


More information about the llvm-dev mailing list