InstrProf: Simplify counting a file's regions when writing coverage (NFC)
[oota-llvm.git] / lib / IR / Mangler.cpp
1 //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Unified name mangler for assembly backends.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/IR/Mangler.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/IR/DataLayout.h"
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/Support/raw_ostream.h"
21 using namespace llvm;
22
23 static void getNameWithPrefixx(raw_ostream &OS, const Twine &GVName,
24                               Mangler::ManglerPrefixTy PrefixTy,
25                               const DataLayout &DL, bool UseAt) {
26   SmallString<256> TmpData;
27   StringRef Name = GVName.toStringRef(TmpData);
28   assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
29
30   // No need to do anything special if the global has the special "do not
31   // mangle" flag in the name.
32   if (Name[0] == '\1') {
33     OS << Name.substr(1);
34     return;
35   }
36
37   if (PrefixTy == Mangler::Private)
38     OS << DL.getPrivateGlobalPrefix();
39   else if (PrefixTy == Mangler::LinkerPrivate)
40     OS << DL.getLinkerPrivateGlobalPrefix();
41
42   if (UseAt) {
43     OS << '@';
44   } else {
45     char Prefix = DL.getGlobalPrefix();
46     if (Prefix != '\0')
47       OS << Prefix;
48   }
49
50   // If this is a simple string that doesn't need escaping, just append it.
51   OS << Name;
52 }
53
54 void Mangler::getNameWithPrefix(raw_ostream &OS, const Twine &GVName,
55                                 ManglerPrefixTy PrefixTy) const {
56   return getNameWithPrefixx(OS, GVName, PrefixTy, *DL, false);
57 }
58
59 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
60                                 const Twine &GVName,
61                                 ManglerPrefixTy PrefixTy) const {
62   raw_svector_ostream OS(OutName);
63   return getNameWithPrefix(OS, GVName, PrefixTy);
64 }
65
66 /// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
67 /// a suffix on their name indicating the number of words of arguments they
68 /// take.
69 static void AddFastCallStdCallSuffix(raw_ostream &OS, const Function *F,
70                                      const DataLayout &TD) {
71   // Calculate arguments size total.
72   unsigned ArgWords = 0;
73   for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
74        AI != AE; ++AI) {
75     // Skip arguments in registers to handle typical fastcall lowering.
76     if (F->getAttributes().hasAttribute(AI->getArgNo() + 1, Attribute::InReg))
77       continue;
78     Type *Ty = AI->getType();
79     // 'Dereference' type in case of byval or inalloca parameter attribute.
80     if (AI->hasByValOrInAllocaAttr())
81       Ty = cast<PointerType>(Ty)->getElementType();
82     // Size should be aligned to DWORD boundary
83     ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
84   }
85
86   OS << '@' << ArgWords;
87 }
88
89 void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
90                                 bool CannotUsePrivateLabel) const {
91   ManglerPrefixTy PrefixTy = Mangler::Default;
92   if (GV->hasPrivateLinkage()) {
93     if (CannotUsePrivateLabel)
94       PrefixTy = Mangler::LinkerPrivate;
95     else
96       PrefixTy = Mangler::Private;
97   }
98
99   if (!GV->hasName()) {
100     // Get the ID for the global, assigning a new one if we haven't got one
101     // already.
102     unsigned &ID = AnonGlobalIDs[GV];
103     if (ID == 0)
104       ID = NextAnonGlobalID++;
105
106     // Must mangle the global into a unique ID.
107     getNameWithPrefix(OS, "__unnamed_" + Twine(ID), PrefixTy);
108     return;
109   }
110
111   StringRef Name = GV->getName();
112
113   bool UseAt = false;
114   const Function *MSFunc = nullptr;
115   CallingConv::ID CC;
116   if (Name[0] != '\1' && DL->hasMicrosoftFastStdCallMangling()) {
117     if ((MSFunc = dyn_cast<Function>(GV))) {
118       CC = MSFunc->getCallingConv();
119       // fastcall functions need to start with @ instead of _.
120       if (CC == CallingConv::X86_FastCall)
121         UseAt = true;
122     }
123   }
124
125   getNameWithPrefixx(OS, Name, PrefixTy, *DL, UseAt);
126
127   if (!MSFunc)
128     return;
129
130   // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
131   // add it.
132   // fastcall and stdcall functions usually need @42 at the end to specify
133   // the argument info.
134   FunctionType *FT = MSFunc->getFunctionType();
135   if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
136       // "Pure" variadic functions do not receive @0 suffix.
137       (!FT->isVarArg() || FT->getNumParams() == 0 ||
138        (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
139     AddFastCallStdCallSuffix(OS, MSFunc, *DL);
140 }
141
142 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
143                                 const GlobalValue *GV,
144                                 bool CannotUsePrivateLabel) const {
145   raw_svector_ostream OS(OutName);
146   getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
147 }