llvm.org GIT mirror llvm / 8b0a8c8
Implement aliases. This fixes PR1017 and it's dependent bugs. CFE part will follow. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36435 91177308-0d34-0410-b5e6-96231b3b80d8 Anton Korobeynikov 13 years ago
22 changed file(s) with 552 addition(s) and 52 deletion(s). Raw diff Collapse all Expand all
942942 target triple specified, i.e. a platform-independent module).
943943
944944
945 string
946 The data layout string describing the endianness,
947 pointer size, and type alignments for which the module was written
948 (blank means no data layout specified, i.e. a platform-independent
949 module).
945 string
946 The data layout string describing the endianness,
947 pointer size, and type alignments for which the module was written
948 (blank means no data layout specified, i.e. a platform-independent
949 module).
950950
951951
952952 llist(string)
957957
958958 string
959959 The inline asm block for this module.
960
961
962 zlist(alias)
963 A zero terminated list of aliases occurring in the
964 module.
960965
961966
962967
11221127 bit(5-30) 11231128 Type slot number of type for the function. 11241129 1125 1130 11261131 bit(31) 11271132 Indicates whether an extension word follows. 11281133 11781183 into the SectionID llist in the 11791184 Module Global Info block. If this value is 11801185 0 or not present, the function has an empty section string. 1186 1187 1188 1189 1190
1191 1192 1193 1195
1196

Aliases are written using 3 uint32_vbr 1197 that encode information about alias itself and aliasee.

1198 1199

The table below provides the bit layout of 1200 the first uint32_vbr which describes alias itself.

1201 1202
1203
1204
1205 Type
1206 Description
1207
1208
1209 bit(0-1)
1210 Alias linkage. 0 - External linkage, 1 - Internal
1211 linkage, 2 - Weak linkage.
1212
1213
1214 bit(2-31)
1215 Type slot number of type for the alias itself.
1216
1217
1218
1219 1220

The next two uint32_vbr's describes the 1221 aliasee.

1222 1223
1224
1225
1226 Type
1227 Description
1228
1229
1230 uint32_vbr
1231 Type slot number of type for the aliasee.
1232
1233
1234 uint32_vbr
1235 Slot number of the aliasee.
11811236
11821237
11831238
497497

It is illegal for a function declaration

498498 to have any linkage type other than "externally visible", dllimport,
499499 or extern_weak.

500
500

Aliases can have only external, internal and weak

501 linkages.
501502
502503
503504
601602
602603

Global variables define regions of memory allocated at compilation time

603604 instead of run-time. Global variables may optionally be initialized, may have
604 an explicit section to be placed in, and may
605 have an optional explicit alignment specified. A variable may be defined as
606 "thread_local", which means that it will not be shared by threads (each thread
607 will have a separated copy of the variable).
608 A variable may be defined as a global "constant," which indicates that the
609 contents of the variable will never be modified (enabling better
605 an explicit section to be placed in, and may have an optional explicit alignment
606 specified. A variable may be defined as "thread_local", which means that it
607 will not be shared by threads (each thread will have a separated copy of the
608 variable). A variable may be defined as a global "constant," which indicates
609 that the contents of the variable will never be modified (enabling better
610610 optimization, allowing the global data to be placed in the read-only section of
611611 an executable, etc). Note that variables that need runtime initialization
612612 cannot be marked "constant" as there is a store to the variable.

696696 a power of 2.

697697
698698
699
700
701
702
703 Aliases
704
705
706

Aliases act as "second name" for the aliasee value (which can be either

707 function or global variable). Aliases may have an
708 optional linkage type, and an
709 optional visibility style.

710
711
Syntax:
712
713

                  
                
714 @<Name> = [Linkage] [Visibility] alias <AliaseeTy> @<Aliasee>
715
716
717
718
719
699720
700721
701722
2323 class Constant;
2424 class ConstantArray;
2525 class GlobalVariable;
26 class GlobalAlias;
2627 class MachineConstantPoolEntry;
2728 class MachineConstantPoolValue;
2829 class Mangler;
140140 /// removeFromParent - This method unlinks 'this' from the containing module,
141141 /// but does not delete it.
142142 ///
143 void removeFromParent();
143 virtual void removeFromParent();
144144
145145 /// eraseFromParent - This method unlinks 'this' from the containing module
146146 /// and deletes it.
147147 ///
148 void eraseFromParent();
148 virtual void eraseFromParent();
149149
150150
151151 /// Get the underlying elements of the Function... the basic block list is
120120 /// value is outside of the current translation unit...
121121 virtual bool isDeclaration() const = 0;
122122
123 /// removeFromParent - This method unlinks 'this' from the containing module,
124 /// but does not delete it.
125 ///
126 virtual void removeFromParent() = 0;
127
128 /// eraseFromParent - This method unlinks 'this' from the containing module
129 /// and deletes it.
130 ///
131 virtual void eraseFromParent() = 0;
132
123133 /// getParent - Get the module that this global value is contained inside
124134 /// of...
125135 inline Module *getParent() { return Parent; }
135145 static inline bool classof(const GlobalValue *) { return true; }
136146 static inline bool classof(const Value *V) {
137147 return V->getValueID() == Value::FunctionVal ||
138 V->getValueID() == Value::GlobalVariableVal;
148 V->getValueID() == Value::GlobalVariableVal ||
149 V->getValueID() == Value::GlobalAliasVal;
139150 }
140151 };
141152
106106 /// removeFromParent - This method unlinks 'this' from the containing module,
107107 /// but does not delete it.
108108 ///
109 void removeFromParent();
109 virtual void removeFromParent();
110110
111111 /// eraseFromParent - This method unlinks 'this' from the containing module
112112 /// and deletes it.
113113 ///
114 void eraseFromParent();
114 virtual void eraseFromParent();
115115
116116 /// Override Constant's implementation of this method so we can
117117 /// replace constant initializers.
1515
1616 #include "llvm/Function.h"
1717 #include "llvm/GlobalVariable.h"
18 #include "llvm/GlobalAlias.h"
1819 #include "llvm/Support/DataTypes.h"
1920 #include
2021
2122 namespace llvm {
2223
23 class GlobalVariable;
2424 class GlobalValueRefMap; // Used by ConstantVals.cpp
2525 class FunctionType;
2626
4242 static inline ValueSymbolTable *getSymTab(Module *M);
4343 static int getListOffset();
4444 };
45 template<> struct ilist_traits
46 : public SymbolTableListTraits {
47 // createSentinel is used to create a node that marks the end of the list.
48 static GlobalAlias *createSentinel();
49 static void destroySentinel(GlobalAlias *GA) { delete GA; }
50 static iplist &getList(Module *M);
51 static inline ValueSymbolTable *getSymTab(Module *M);
52 static int getListOffset();
53 };
4554
4655 /// A Module instance is used to store all the information related to an
4756 /// LLVM module. Modules are the top level container of all other LLVM
6271 typedef iplist GlobalListType;
6372 /// The type for the list of functions.
6473 typedef iplist FunctionListType;
74 /// The type for the list of aliases.
75 typedef iplist AliasListType;
6576
6677 /// The type for the list of dependent libraries.
6778 typedef std::vector LibraryListType;
7687 /// The Function constant iterator
7788 typedef FunctionListType::const_iterator const_iterator;
7889
90 /// The Global Alias iterators.
91 typedef AliasListType::iterator alias_iterator;
92 /// The Global Alias constant iterator
93 typedef AliasListType::const_iterator const_alias_iterator;
94
7995 /// The Library list iterator.
8096 typedef LibraryListType::const_iterator lib_iterator;
8197
91107 private:
92108 GlobalListType GlobalList; ///< The Global Variables in the module
93109 FunctionListType FunctionList; ///< The Functions in the module
110 AliasListType AliasList; ///< The Aliases in the module
94111 LibraryListType LibraryList; ///< The Libraries needed by the module
95112 std::string GlobalScopeAsm; ///< Inline Asm at global scope.
96113 ValueSymbolTable *ValSymTab; ///< Symbol table for values
174191 /// getOrInsertFunction - Look up the specified function in the module symbol
175192 /// table. If it does not exist, add a prototype for the function and return
176193 /// it. This function guarantees to return a constant of pointer to the
177 /// specified function type or a ConstantExpr BitCast of that type if the
178 /// named /// function has a different type. This version of the method
179 /// takes a null terminated list of function arguments, which makes it
180 /// easier for clients to use.
194 /// specified function type or a ConstantExpr BitCast of that type if the
195 /// named function has a different type. This version of the method takes a
196 /// null terminated list of function arguments, which makes it easier for
197 /// clients to use.
181198 Constant *getOrInsertFunction(const std::string &Name, const Type *RetTy,...)
182199 END_WITH_NULL;
183200
204221 GlobalVariable *getNamedGlobal(const std::string &Name) const {
205222 return getGlobalVariable(Name, true);
206223 }
224
225 /// @}
226 /// @name Global Variable Accessors
227 /// @{
228 public:
229 /// getNamedGlobal - Return the first global alias in the module with the
230 /// specified name, of arbitrary type. This method returns null if a global
231 /// with the specified name is not found.
232 GlobalAlias *getNamedAlias(const std::string &Name) const;
207233
208234 /// @}
209235 /// @name Type Accessors
234260 const FunctionListType &getFunctionList() const { return FunctionList; }
235261 /// Get the Module's list of functions.
236262 FunctionListType &getFunctionList() { return FunctionList; }
263 /// Get the Module's list of aliases (constant).
264 const AliasListType &getAliasList() const { return AliasList; }
265 /// Get the Module's list of aliases.
266 AliasListType &getAliasList() { return AliasList; }
237267 /// Get the symbol table of global variable and function identifiers
238268 const ValueSymbolTable &getValueSymbolTable() const { return *ValSymTab; }
239269 /// Get the Module's symbol table of global variable and function identifiers.
240270 ValueSymbolTable &getValueSymbolTable() { return *ValSymTab; }
241271 /// Get the symbol table of types
242 const TypeSymbolTable &getTypeSymbolTable() const { return *TypeSymTab; }
272 const TypeSymbolTable &getTypeSymbolTable() const { return *TypeSymTab; }
243273 /// Get the Module's symbol table of types
244 TypeSymbolTable &getTypeSymbolTable() { return *TypeSymTab; }
274 TypeSymbolTable &getTypeSymbolTable() { return *TypeSymTab; }
245275
246276 /// @}
247277 /// @name Global Variable Iteration
271301 /// Get a constant iterator to the last function.
272302 const_iterator end () const { return FunctionList.end(); }
273303 /// Determine how many functions are in the Module's list of functions.
274 size_t size() const { return FunctionList.size(); }
304 size_t size() const { return FunctionList.size(); }
275305 /// Determine if the list of functions is empty.
276306 bool empty() const { return FunctionList.empty(); }
277307
282312 /// @brief Get a constant iterator to beginning of dependent library list.
283313 inline lib_iterator lib_begin() const { return LibraryList.begin(); }
284314 /// @brief Get a constant iterator to end of dependent library list.
285 inline lib_iterator lib_end() const { return LibraryList.end(); }
315 inline lib_iterator lib_end() const { return LibraryList.end(); }
286316 /// @brief Returns the number of items in the list of libraries.
287 inline size_t lib_size() const { return LibraryList.size(); }
317 inline size_t lib_size() const { return LibraryList.size(); }
288318 /// @brief Add a library to the list of dependent libraries
289319 void addLibrary(const std::string& Lib);
290320 /// @brief Remove a library from the list of dependent libraries
291321 void removeLibrary(const std::string& Lib);
292322 /// @brief Get all the libraries
293323 inline const LibraryListType& getLibraries() const { return LibraryList; }
324
325 /// @}
326 /// @name Alias Iteration
327 /// @{
328 public:
329 /// Get an iterator to the first alias.
330 alias_iterator alias_begin() { return AliasList.begin(); }
331 /// Get a constant iterator to the first alias.
332 const_alias_iterator alias_begin() const { return AliasList.begin(); }
333 /// Get an iterator to the last alias.
334 alias_iterator alias_end () { return AliasList.end(); }
335 /// Get a constant iterator to the last alias.
336 const_alias_iterator alias_end () const { return AliasList.end(); }
337 /// Determine how many functions are in the Module's list of aliases.
338 size_t alias_size () const { return AliasList.size(); }
339 /// Determine if the list of aliases is empty.
340 bool alias_empty() const { return AliasList.empty(); }
294341
295342 /// @}
296343 /// @name Utility functions for printing and dumping Module objects
323370 Module *Obj = 0;
324371 return unsigned(reinterpret_cast(&Obj->GlobalList));
325372 }
373 static unsigned getAliasListOffset() {
374 Module *Obj = 0;
375 return unsigned(reinterpret_cast(&Obj->AliasList));
376 }
326377 };
327378
328379 /// An iostream inserter for modules.
341392 return M ? &M->getValueSymbolTable() : 0;
342393 }
343394
395 inline ValueSymbolTable *
396 ilist_traits::getSymTab(Module *M) {
397 return M ? &M->getValueSymbolTable() : 0;
398 }
399
344400 inline int
345401 ilist_traits::getListOffset() {
346402 return Module::getFunctionListOffset();
351407 return Module::getGlobalVariableListOffset();
352408 }
353409
410 inline int
411 ilist_traits::getListOffset() {
412 return Module::getAliasListOffset();
413 }
414
354415 } // End llvm namespace
355416
356417 #endif
2929 class GlobalValue;
3030 class Function;
3131 class GlobalVariable;
32 class GlobalAlias;
3233 class InlineAsm;
3334 class ValueSymbolTable;
3435 class TypeSymbolTable;
159160 ArgumentVal, // This is an instance of Argument
160161 BasicBlockVal, // This is an instance of BasicBlock
161162 FunctionVal, // This is an instance of Function
163 GlobalAliasVal, // This is an instance of GlobalAlias
162164 GlobalVariableVal, // This is an instance of GlobalVariable
163165 UndefValueVal, // This is an instance of UndefValue
164166 ConstantExprVal, // This is an instance of ConstantExpr
247249 template <> inline bool isa_impl(const Value &Val) {
248250 return Val.getValueID() == Value::GlobalVariableVal;
249251 }
252 template <> inline bool isa_impl(const Value &Val) {
253 return Val.getValueID() == Value::GlobalAliasVal;
254 }
250255 template <> inline bool isa_impl(const Value &Val) {
251 return isa(Val) || isa(Val);
256 return isa(Val) || isa(Val) || isa(Val);
252257 }
253258
254259 } // End llvm namespace
3434 friend class SymbolTableListTraits;
3535 friend class SymbolTableListTraits;
3636 friend class SymbolTableListTraits;
37 friend class SymbolTableListTraits;
3738 /// @name Types
3839 /// @{
3940 public:
221221 volatile { return VOLATILE; }
222222 align { return ALIGN; }
223223 section { return SECTION; }
224 alias { return ALIAS; }
224225 module { return MODULE; }
225226 asm { return ASM_TOK; }
226227 sideeffect { return SIDEEFFECT; }
10031003 %type OptSideEffect // 'sideeffect' or not.
10041004 %type GVInternalLinkage GVExternalLinkage
10051005 %type FunctionDefineLinkage FunctionDeclareLinkage
1006 %type AliasLinkage
10061007 %type GVVisibilityStyle
10071008
10081009 // ValueRef - Unresolved reference to a definition or BB
10341035
10351036 %token LOCALVAR GLOBALVAR LABELSTR STRINGCONSTANT ATSTRINGCONSTANT
10361037 %type LocalName OptLocalName OptLocalAssign
1037 %type GlobalName OptGlobalAssign
1038 %type GlobalName OptGlobalAssign GlobalAssign
10381039 %type OptAlign OptCAlign
10391040 %type OptSection SectionString
10401041
10411042 %token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
1042 %token DECLARE DEFINE GLOBAL CONSTANT SECTION VOLATILE THREAD_LOCAL
1043 %token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
10431044 %token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
10441045 %token DLLIMPORT DLLEXPORT EXTERN_WEAK
10451046 %token OPAQUE EXTERNAL TARGET TRIPLE ALIGN
11351136
11361137 GlobalName : GLOBALVAR | ATSTRINGCONSTANT;
11371138
1138 OptGlobalAssign : GlobalName '=' {
1139 $$ = $1;
1140 CHECK_FOR_ERROR
1141 }
1139 OptGlobalAssign : GlobalAssign
11421140 | /*empty*/ {
11431141 $$ = 0;
11441142 CHECK_FOR_ERROR
11451143 };
1144
1145 GlobalAssign : GlobalName '=' {
1146 $$ = $1;
1147 CHECK_FOR_ERROR
1148 }
11461149
11471150 GVInternalLinkage
11481151 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
11601163
11611164 GVVisibilityStyle
11621165 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1166 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
11631167 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
11641168 ;
11651169
11691173 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
11701174 ;
11711175
1172 FunctionDefineLinkage
1176 FunctionDefineLinkage
11731177 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
11741178 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
11751179 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
11761180 | WEAK { $$ = GlobalValue::WeakLinkage; }
11771181 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
11781182 ;
1183
1184 AliasLinkage
1185 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1186 | WEAK { $$ = GlobalValue::WeakLinkage; }
1187 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1188 ;
11791189
11801190 OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
11811191 CCC_TOK { $$ = CallingConv::C; } |
20292039 } GlobalVarAttributes {
20302040 CurGV = 0;
20312041 CHECK_FOR_ERROR
2042 }
2043 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage ResultTypes SymbolicValueRef {
2044 std::string Name($1);
2045 if (Name.empty())
2046 GEN_ERROR("Alias name cannot be empty")
2047 const PointerType *PFTy = 0;
2048 const FunctionType *Ty = 0;
2049 Value* V = 0;
2050 const Type* VTy = 0;
2051 if (!(PFTy = dyn_cast($5->get())) ||
2052 !(Ty = dyn_cast(PFTy->getElementType()))) {
2053 VTy = $5->get();
2054 V = getExistingVal(VTy, $6);
2055 } else {
2056 VTy = PFTy;
2057 V = getExistingVal(PFTy, $6);
2058 }
2059 if (V == 0)
2060 GEN_ERROR(std::string("Invalid aliasee for alias: ") + $1);
2061 GlobalValue* Aliasee;
2062 if (Aliasee = dyn_cast(V)) {
2063 GlobalAlias* GA = new GlobalAlias(VTy, $4, Name, Aliasee, CurModule.CurrentModule);
2064 GA->setVisibility($2);
2065 InsertValue(GA, CurModule.Values);
2066 } else
2067 GEN_ERROR("Aliases can be created only to global values");
2068 CHECK_FOR_ERROR
2069 delete $5;
20322070 }
20332071 | TARGET TargetDefinition {
20342072 CHECK_FOR_ERROR
347347 /// with this method. The ValueTable argument must be one of ModuleValues
348348 /// or FunctionValues data members of this class.
349349 unsigned BytecodeReader::insertValue(Value *Val, unsigned type,
350 ValueTable &ValueTab) {
350 ValueTable &ValueTab) {
351351 if (ValueTab.size() <= type)
352352 ValueTab.resize(type+1);
353353
18541854 case 1: Func->setLinkage(Function::DLLImportLinkage); break;
18551855 case 2: Func->setLinkage(Function::ExternalWeakLinkage); break;
18561856 default: assert(0 && "Unsupported external linkage");
1857 }
1857 }
18581858 }
18591859
18601860 Func->setCallingConv(CC-1);
19181918 I->first->setSection(SectionNames[I->second-1]);
19191919 }
19201920
1921 if (At != BlockEnd) {
1922 // Read aliases...
1923 unsigned VarType = read_vbr_uint();
1924 while (VarType != Type::VoidTyID) { // List is terminated by Void
1925 unsigned TypeSlotNo = VarType >> 2;
1926 unsigned EncodedLinkage = VarType & 3;
1927 unsigned AliaseeTypeSlotNo, AliaseeSlotNo;
1928
1929 AliaseeTypeSlotNo = read_vbr_uint();
1930 AliaseeSlotNo = read_vbr_uint();
1931
1932 const Type *Ty = getType(TypeSlotNo);
1933 if (!Ty)
1934 error("Alias has no type! SlotNo=" + utostr(TypeSlotNo));
1935
1936 if (!isa(Ty))
1937 error("Alias not a pointer type! Ty= " + Ty->getDescription());
1938
1939 Value* V = getValue(AliaseeTypeSlotNo, AliaseeSlotNo, false);
1940 if (!V)
1941 error("Invalid aliasee! TypeSlotNo=" + utostr(AliaseeTypeSlotNo) +
1942 " SlotNo=" + utostr(AliaseeSlotNo));
1943 if (!isa(V))
1944 error("Aliasee is not global value! SlotNo=" + utostr(AliaseeSlotNo));
1945
1946 GlobalValue::LinkageTypes Linkage;
1947 switch (EncodedLinkage) {
1948 case 0:
1949 Linkage = GlobalValue::ExternalLinkage;
1950 break;
1951 case 1:
1952 Linkage = GlobalValue::InternalLinkage;
1953 break;
1954 case 2:
1955 Linkage = GlobalValue::WeakLinkage;
1956 break;
1957 default:
1958 assert(0 && "Unsupported encoded alias linkage");
1959 }
1960
1961 GlobalAlias *GA = new GlobalAlias(Ty, Linkage, "",
1962 dyn_cast(V), TheModule);
1963 insertValue(GA, TypeSlotNo, ModuleValues);
1964 VarType = read_vbr_uint();
1965 }
1966 }
1967
19211968 // This is for future proofing... in the future extra fields may be added that
19221969 // we don't understand, so we transparently ignore them.
19231970 //
10871087 output_vbr((unsigned)SectionNames.size());
10881088 for (unsigned i = 0, e = SectionNames.size(); i != e; ++i)
10891089 output(SectionNames[i]);
1090
1090
10911091 // Output the inline asm string.
10921092 output(M->getModuleInlineAsm());
1093
1094 // Output aliases
1095 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1096 I != E; ++I) {
1097 unsigned Slot = Table.getTypeSlot(I->getType());
1098 assert(((Slot << 2) >> 2) == Slot && "Slot # too big!");
1099 unsigned aliasLinkage = 0;
1100 switch (I->getLinkage()) {
1101 case GlobalValue::ExternalLinkage:
1102 aliasLinkage = 0;
1103 break;
1104 case GlobalValue::InternalLinkage:
1105 aliasLinkage = 1;
1106 break;
1107 case GlobalValue::WeakLinkage:
1108 aliasLinkage = 2;
1109 break;
1110 default:
1111 assert(0 && "Invalid alias linkage");
1112 }
1113 output_vbr((Slot << 2) | aliasLinkage);
1114 output_vbr(Table.getTypeSlot(I->getAliasee()->getType()));
1115 output_vbr(Table.getSlot(I->getAliasee()));
1116 }
1117 output_typeid(Table.getTypeSlot(Type::VoidTy));
10931118 }
10941119
10951120 void BytecodeWriter::outputInstructions(const Function *F) {
110110
111111 bool AsmPrinter::doFinalization(Module &M) {
112112 if (TAI->getWeakRefDirective()) {
113 if (ExtWeakSymbols.begin() != ExtWeakSymbols.end())
113 if (!ExtWeakSymbols.empty())
114114 SwitchToDataSection("");
115115
116116 for (std::set::iterator i = ExtWeakSymbols.begin(),
118118 const GlobalValue *GV = *i;
119119 std::string Name = Mang->getValueName(GV);
120120 O << TAI->getWeakRefDirective() << Name << "\n";
121 }
122 }
123
124 if (TAI->getSetDirective()) {
125 if (M.alias_size())
126 SwitchToTextSection(TAI->getTextSection());
127
128 O << "\n";
129 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
130 I!=E; ++I) {
131 const GlobalValue *Aliasee = I->getAliasee();
132 assert(Aliasee && "Aliasee cannot be null!");
133 std::string Target = Mang->getValueName(Aliasee);
134 std::string Name = Mang->getValueName(I);
135
136 // Aliases with external weak linkage was emitted already
137 if (I->hasExternalLinkage())
138 O << "\t.globl\t" << Name << "\n";
139 else if (I->hasWeakLinkage())
140 O << TAI->getWeakRefDirective() << Name << "\n";
141 else if (!I->hasInternalLinkage())
142 assert(0 && "Invalid alias linkage");
143
144 O << TAI->getSetDirective() << Name << ", " << Target << "\n";
121145 }
122146 }
123147
107107 ReadOnlySection = "\t.section\t.rodata\n";
108108 PrivateGlobalPrefix = ".L";
109109 WeakRefDirective = "\t.weak\t";
110 SetDirective = "\t.set\t";
110111 DwarfRequiresFrameSection = false;
111112 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
112113 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
136137 AbsoluteSectionOffsets = true;
137138 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
138139 WeakRefDirective = "\t.weak\t";
140 SetDirective = "\t.set\t";
139141 DwarfRequiresFrameSection = false;
140142 DwarfSectionOffsetDirective = "\t.secrel32\t";
141143 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
6161 GlobalIsNeeded(I);
6262 }
6363
64 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) {
64 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
65 I != E; ++I) {
6566 Changed |= RemoveUnusedGlobalValue(*I);
6667 // Externally visible & appending globals are needed, if they have an
6768 // initializer.
7071 GlobalIsNeeded(I);
7172 }
7273
74
75 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
76 I != E; ++I) {
77 Changed |= RemoveUnusedGlobalValue(*I);
78 // Aliases are always needed even if they are not used.
79 GlobalIsNeeded(I);
80 }
7381
7482 // Now that all globals which are needed are in the AliveGlobals set, we loop
7583 // through the program, deleting those which are not alive.
134142 // referenced by the initializer to the alive set.
135143 if (GV->hasInitializer())
136144 MarkUsedGlobalsAsNeeded(GV->getInitializer());
145 } else if (GlobalAlias *GA = dyn_cast(G)) {
146 // If this is a global alias we also need it's aliasee
147 GlobalIsNeeded(const_cast(GA->getAliasee()));
137148 } else {
138149 // Otherwise this must be a function object. We have to scan the body of
139150 // the function looking for constants and global values which are used as
165165 return new SlotMachine(BB->getParent());
166166 } else if (const GlobalVariable *GV = dyn_cast(V)){
167167 return new SlotMachine(GV->getParent());
168 } else if (const GlobalAlias *GA = dyn_cast(V)){
169 return new SlotMachine(GA->getParent());
168170 } else if (const Function *Func = dyn_cast(V)) {
169171 return new SlotMachine(Func);
170172 }
682684 fillTypeNameTable(M, TypeNames);
683685 }
684686
685 inline void write(const Module *M) { printModule(M); }
686 inline void write(const GlobalVariable *G) { printGlobal(G); }
687 inline void write(const Function *F) { printFunction(F); }
688 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
687 inline void write(const Module *M) { printModule(M); }
688 inline void write(const GlobalVariable *G) { printGlobal(G); }
689 inline void write(const GlobalAlias *G) { printAlias(G); }
690 inline void write(const Function *F) { printFunction(F); }
691 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
689692 inline void write(const Instruction *I) { printInstruction(*I); }
690 inline void write(const Type *Ty) { printType(Ty); }
693 inline void write(const Type *Ty) { printType(Ty); }
691694
692695 void writeOperand(const Value *Op, bool PrintType);
693696
697700 void printModule(const Module *M);
698701 void printTypeSymbolTable(const TypeSymbolTable &ST);
699702 void printGlobal(const GlobalVariable *GV);
703 void printAlias(const GlobalAlias *GV);
700704 void printFunction(const Function *F);
701705 void printArgument(const Argument *FA, uint16_t ParamAttrs);
702706 void printBasicBlock(const BasicBlock *BB);
847851 // Output all of the functions.
848852 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
849853 printFunction(I);
854
855 // Output all aliases
856 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
857 I != E; ++I)
858 printAlias(I);
850859 }
851860
852861 void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
887896 assert(C && "GlobalVar initializer isn't constant?");
888897 writeOperand(GV->getInitializer(), false);
889898 }
890
899
891900 if (GV->hasSection())
892901 Out << ", section \"" << GV->getSection() << '"';
893902 if (GV->getAlignment())
894903 Out << ", align " << GV->getAlignment();
904
905 printInfoComment(*GV);
906 Out << "\n";
907 }
908
909 void AssemblyWriter::printAlias(const GlobalAlias *GA) {
910 Out << getLLVMName(GA->getName(), GlobalPrefix) << " = ";
911 switch (GA->getVisibility()) {
912 default: assert(0 && "Invalid visibility style!");
913 case GlobalValue::DefaultVisibility: break;
914 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
915 }
916
917 Out << "alias ";
918
919 switch (GA->getLinkage()) {
920 case GlobalValue::WeakLinkage: Out << "weak "; break;
921 case GlobalValue::InternalLinkage: Out << "internal "; break;
922 case GlobalValue::ExternalLinkage: break;
923 default:
924 assert(0 && "Invalid alias linkage");
925 }
895926
896 printInfoComment(*GV);
927 const GlobalValue *Aliasee = GA->getAliasee();
928 assert(Aliasee && "Aliasee cannot be null");
929
930 if (const GlobalVariable *GV = dyn_cast(Aliasee)) {
931 printType(GV->getType());
932 Out << " " << getLLVMName(GV->getName(), GlobalPrefix);
933 } else if (const Function *F = dyn_cast(Aliasee)) {
934 printType(F->getFunctionType());
935 Out << "* ";
936
937 if (!F->getName().empty())
938 Out << getLLVMName(F->getName(), GlobalPrefix);
939 else
940 Out << "@\"\"";
941 } else
942 assert(0 && "Unsupported aliasee");
943
944 printInfoComment(*GA);
897945 Out << "\n";
898946 }
899947
13351383 W.write(this);
13361384 }
13371385
1386 void GlobalAlias::print(std::ostream &o) const {
1387 SlotMachine SlotTable(getParent());
1388 AssemblyWriter W(o, SlotTable, getParent(), 0);
1389 W.write(this);
1390 }
1391
13381392 void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
13391393 SlotMachine SlotTable(getParent());
13401394 AssemblyWriter W(o, SlotTable, getParent(), AAW);
15371591
15381592 SC_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
15391593 DestSlot << " [");
1540 // G = Global, F = Function, o = other
1541 SC_DEBUG((isa(V) ? 'G' : 'F') << "]\n");
1594 // G = Global, F = Function, A = Alias, o = other
1595 SC_DEBUG((isa(V) ? 'G' :
1596 (isa ? 'F' :
1597 (isa ? 'A' : 'o'))) << "]\n");
15421598 }
15431599
15441600
1212 //===----------------------------------------------------------------------===//
1313
1414 #include "llvm/GlobalVariable.h"
15 #include "llvm/GlobalAlias.h"
1516 #include "llvm/DerivedTypes.h"
1617 #include "llvm/Module.h"
1718 #include "llvm/Support/LeakDetector.h"
7576 assert(0 && "You can't GV->destroyConstant()!");
7677 abort();
7778 }
79
7880 //===----------------------------------------------------------------------===//
7981 // GlobalVariable Implementation
8082 //===----------------------------------------------------------------------===//
119121 Before->getParent()->getGlobalList().insert(Before, this);
120122 }
121123
122
123124 void GlobalVariable::setParent(Module *parent) {
124125 if (getParent())
125126 LeakDetector::addGarbageObject(this);
155156 // Okay, preconditions out of the way, replace the constant initializer.
156157 this->setOperand(0, cast(To));
157158 }
159
160 //===----------------------------------------------------------------------===//
161 // GlobalAlias Implementation
162 //===----------------------------------------------------------------------===//
163
164 GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link,
165 const std::string &Name, const GlobalValue* aliasee,
166 Module *ParentModule)
167 : GlobalValue(Ty, Value::GlobalAliasVal, 0, 0,
168 Link, Name), Aliasee(aliasee) {
169 LeakDetector::addGarbageObject(this);
170
171 if (ParentModule)
172 ParentModule->getAliasList().push_back(this);
173 }
174
175 void GlobalAlias::setParent(Module *parent) {
176 if (getParent())
177 LeakDetector::addGarbageObject(this);
178 Parent = parent;
179 if (getParent())
180 LeakDetector::removeGarbageObject(this);
181 }
182
183 void GlobalAlias::removeFromParent() {
184 getParent()->getAliasList().remove(this);
185 }
186
187 void GlobalAlias::eraseFromParent() {
188 getParent()->getAliasList().erase(this);
189 }
190
191 bool GlobalAlias::isDeclaration() const {
192 return (Aliasee && Aliasee->isDeclaration());
193 }
194
195 void GlobalAlias::setAliasee(const GlobalValue *GV)
196 {
197 // FIXME: Some checks?
198 Aliasee = GV;
199 }
200
4444 LeakDetector::removeGarbageObject(Ret);
4545 return Ret;
4646 }
47 GlobalAlias *ilist_traits::createSentinel() {
48 GlobalAlias *Ret = new GlobalAlias(Type::Int32Ty, GlobalValue::ExternalLinkage);
49 // This should not be garbage monitored.
50 LeakDetector::removeGarbageObject(Ret);
51 return Ret;
52 }
4753
4854 iplist &ilist_traits::getList(Module *M) {
4955 return M->getFunctionList();
5056 }
5157 iplist &ilist_traits::getList(Module *M) {
5258 return M->getGlobalList();
59 }
60 iplist &ilist_traits::getList(Module *M) {
61 return M->getAliasList();
5362 }
5463
5564 // Explicit instantiations of SymbolTableListTraits since some of the methods
5665 // are not in the public header file.
5766 template class SymbolTableListTraits;
5867 template class SymbolTableListTraits;
68 template class SymbolTableListTraits;
5969
6070 //===----------------------------------------------------------------------===//
6171 // Primitive Module methods.
7181 dropAllReferences();
7282 GlobalList.clear();
7383 FunctionList.clear();
84 AliasList.clear();
7485 LibraryList.clear();
7586 delete ValSymTab;
7687 delete TypeSymTab;
211222 }
212223
213224 //===----------------------------------------------------------------------===//
225 // Methods for easy access to the global variables in the module.
226 //
227
228 // getNamedAlias - Look up the specified global in the module symbol table.
229 // If it does not exist, return null.
230 //
231 GlobalAlias *Module::getNamedAlias(const std::string &Name) const {
232 const ValueSymbolTable &SymTab = getValueSymbolTable();
233 return dyn_cast_or_null(SymTab.lookup(Name));
234 }
235
236 //===----------------------------------------------------------------------===//
214237 // Methods for easy access to the types in the module.
215238 //
216239
140140 I != E; ++I)
141141 visitGlobalVariable(*I);
142142
143 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
144 I != E; ++I)
145 visitGlobalAlias(*I);
146
143147 // If the module is broken, abort at this time.
144148 return abortIfBroken();
145149 }
178182 void verifyTypeSymbolTable(TypeSymbolTable &ST);
179183 void visitGlobalValue(GlobalValue &GV);
180184 void visitGlobalVariable(GlobalVariable &GV);
185 void visitGlobalAlias(GlobalAlias &GA);
181186 void visitFunction(Function &F);
182187 void visitBasicBlock(BasicBlock &BB);
183188 void visitTruncInst(TruncInst &I);
276281 Assert1(!GV.isDeclaration() ||
277282 GV.hasExternalLinkage() ||
278283 GV.hasDLLImportLinkage() ||
279 GV.hasExternalWeakLinkage(),
284 GV.hasExternalWeakLinkage() ||
285 (isa(GV) &&
286 (GV.hasInternalLinkage() || GV.hasWeakLinkage())),
280287 "Global is external, but doesn't have external or dllimport or weak linkage!",
281288 &GV);
282289
300307 "variable type!", &GV);
301308
302309 visitGlobalValue(GV);
310 }
311
312 void Verifier::visitGlobalAlias(GlobalAlias &GA) {
313 Assert1(!GA.getName().empty(),
314 "Alias name cannot be empty!", &GA);
315 Assert1(GA.hasExternalLinkage() || GA.hasInternalLinkage() ||
316 GA.hasWeakLinkage(),
317 "Alias should have external or external weak linkage!", &GA);
318
319 visitGlobalValue(GA);
303320 }
304321
305322 void Verifier::verifyTypeSymbolTable(TypeSymbolTable &ST) {
0 ; RUN: llvm-as < %s | \
1 ; RUN: llc -mtriple=i686-pc-linux-gnu -o %t -f
2 ; RUN: grep -c set %t | grep 4
3 ; RUN: grep -c globl %t | grep 3
4 ; RUN: grep -c weak %t | grep 1
5
6 @bar = external global i32
7 @foo1 = alias i32* @bar
8 @foo2 = alias i32* @bar
9
10 %FunTy = type i32()
11
12 declare i32 @foo_f()
13 @bar_f = alias weak %FunTy* @foo_f
14
15 @bar_i = alias internal i32* @bar
16
17 define i32 @test() {
18 entry:
19 %tmp = load i32* @foo1
20 %tmp1 = load i32* @foo2
21 %tmp0 = load i32* @bar_i
22 %tmp2 = call i32 @foo_f()
23 %tmp3 = add i32 %tmp, %tmp2
24 %tmp4 = call %FunTy* @bar_f()
25 %tmp5 = add i32 %tmp3, %tmp4
26 %tmp6 = add i32 %tmp1, %tmp5
27 %tmp7 = add i32 %tmp6, %tmp0
28 ret i32 %tmp7
29 }
0 ; RUN: llvm-as < %s | llvm-dis > %t1.ll
1 ; RUN: llvm-as %t1.ll -o - | llvm-dis > %t2.ll
2 ; RUN: diff %t1.ll %t2.ll
3
4 @bar = external global i32
5 @foo1 = alias i32* @bar
6 @foo2 = alias i32* @bar
7
8 %FunTy = type i32()
9
10 declare i32 @foo_f()
11 @bar_f = alias weak %FunTy* @foo_f
12
13 @bar_i = alias internal i32* @bar
14
15 define i32 @test() {
16 entry:
17 %tmp = load i32* @foo1
18 %tmp1 = load i32* @foo2
19 %tmp0 = load i32* @bar_i
20 %tmp2 = call i32 @foo_f()
21 %tmp3 = add i32 %tmp, %tmp2
22 %tmp4 = call %FunTy* @bar_f()
23 %tmp5 = add i32 %tmp3, %tmp4
24 %tmp6 = add i32 %tmp1, %tmp5
25 %tmp7 = add i32 %tmp6, %tmp0
26 ret i32 %tmp7
27 }