llvm.org GIT mirror llvm / cde25b4
Clarify that llvm.used can contain aliases. Also add a check for llvm.used in the verifier and simplify clients now that they can assume they have a ConstantArray. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@180019 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 7 years ago
14 changed file(s) with 75 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
28672867 The '``llvm.used``' Global Variable
28682868 -----------------------------------
28692869
2870 The ``@llvm.used`` global is an array with i8\* element type which has
2871 :ref:`appending linkage `. This array contains a list of
2872 pointers to global variables and functions which may optionally have a
2870 The ``@llvm.used`` global is an array which has
2871 :ref:`appending linkage `. This array contains a list of
2872 pointers to global variables, functions and aliases which may optionally have a
28732873 pointer cast formed of bitcast or getelementptr. For example, a legal
28742874 use of it is:
28752875
28832883 i8* bitcast (i32* @Y to i8*)
28842884 ], section "llvm.metadata"
28852885
2886 If a global variable appears in the ``@llvm.used`` list, then the
2887 compiler, assembler, and linker are required to treat the symbol as if
2888 there is a reference to the global that it cannot see. For example, if a
2889 variable has internal linkage and no references other than that from the
2890 ``@llvm.used`` list, it cannot be deleted. This is commonly used to
2891 represent references from inline asms and other things the compiler
2892 cannot "see", and corresponds to "``attribute((used))``" in GNU C.
2886 If a symbol appears in the ``@llvm.used`` list, then the compiler, assembler,
2887 and linker are required to treat the symbol as if there is a reference to the
2888 symbol that it cannot see. For example, if a variable has internal linkage and
2889 no references other than that from the ``@llvm.used`` list, it cannot be
2890 deleted. This is commonly used to represent references from inline asms and
2891 other things the compiler cannot "see", and corresponds to
2892 "``attribute((used))``" in GNU C.
28932893
28942894 On some targets, the code generator must emit a directive to the
28952895 assembler or object file to prevent the assembler and linker from
2424 class BlockAddress;
2525 class GCStrategy;
2626 class Constant;
27 class ConstantArray;
2728 class GCMetadataPrinter;
2829 class GlobalValue;
2930 class GlobalVariable;
479480 void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
480481 const MachineBasicBlock *MBB,
481482 unsigned uid) const;
482 void EmitLLVMUsedList(const Constant *List);
483 void EmitLLVMUsedList(const ConstantArray *InitList);
483484 void EmitXXStructorList(const Constant *List, bool isCtor);
484485 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);
485486 };
12121212 bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
12131213 if (GV->getName() == "llvm.used") {
12141214 if (MAI->hasNoDeadStrip()) // No need to emit this at all.
1215 EmitLLVMUsedList(GV->getInitializer());
1215 EmitLLVMUsedList(cast(GV->getInitializer()));
12161216 return true;
12171217 }
12181218
12551255 /// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
12561256 /// global in the specified llvm.used list for which emitUsedDirectiveFor
12571257 /// is true, as being used with this directive.
1258 void AsmPrinter::EmitLLVMUsedList(const Constant *List) {
1258 void AsmPrinter::EmitLLVMUsedList(const ConstantArray *InitList) {
12591259 // Should be an array of 'i8*'.
1260 const ConstantArray *InitList = dyn_cast(List);
1261 if (InitList == 0) return;
1262
12631260 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
12641261 const GlobalValue *GV =
12651262 dyn_cast(InitList->getOperand(i)->stripPointerCasts());
325325 if (!GV || !GV->hasInitializer()) return;
326326
327327 // Should be an array of 'i8*'.
328 const ConstantArray *InitList = dyn_cast(GV->getInitializer());
329 if (InitList == 0) return;
328 const ConstantArray *InitList = cast(GV->getInitializer());
330329
331330 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
332331 if (const Function *F =
445445 STy->getTypeAtIndex(0u)->isIntegerTy(32) &&
446446 STy->getTypeAtIndex(1) == FuncPtrTy,
447447 "wrong type for intrinsic global variable", &GV);
448 }
449 }
450
451 if (GV.hasName() && (GV.getName() == "llvm.used")) {
452 Assert1(!GV.hasInitializer() || GV.hasAppendingLinkage(),
453 "invalid linkage for intrinsic global variable", &GV);
454 Type *GVType = GV.getType()->getElementType();
455 if (ArrayType *ATy = dyn_cast(GVType)) {
456 PointerType *PTy = dyn_cast(ATy->getElementType());
457 Assert1(PTy, "wrong type for intrinsic global variable", &GV);
458 if (GV.hasInitializer()) {
459 Constant *Init = GV.getInitializer();
460 ConstantArray *InitArray = dyn_cast(Init);
461 Assert1(InitArray, "wrong initalizer for intrinsic global variable",
462 Init);
463 for (unsigned i = 0, e = InitArray->getNumOperands(); i != e; ++i) {
464 Value *V = Init->getOperand(i)->stripPointerCasts();
465 // stripPointerCasts strips aliases, so we only need to check for
466 // variables and functions.
467 Assert1(isa(V) || isa(V),
468 "invalid llvm.used member", V);
469 }
470 }
448471 }
449472 }
450473
6565 static void FindUsedValues(GlobalVariable *LLVMUsed,
6666 SmallPtrSet &UsedValues) {
6767 if (LLVMUsed == 0) return;
68 ConstantArray *Inits = dyn_cast(LLVMUsed->getInitializer());
69 if (Inits == 0) return;
70
68 ConstantArray *Inits = cast(LLVMUsed->getInitializer());
69
7170 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
7271 if (GlobalValue *GV =
7372 dyn_cast(Inits->getOperand(i)->stripPointerCasts()))
194194 SmallPtrSet &UsedValues) {
195195 if (LLVMUsed == 0) return;
196196 UsedValues.insert(LLVMUsed);
197
198 ConstantArray *Inits = dyn_cast(LLVMUsed->getInitializer());
199 if (Inits == 0) return;
200
197
198 ConstantArray *Inits = cast(LLVMUsed->getInitializer());
199
201200 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
202201 if (GlobalValue *GV =
203202 dyn_cast(Inits->getOperand(i)->stripPointerCasts()))
199199 if (!GV || !GV->hasInitializer()) return;
200200
201201 // Should be an array of 'i8*'.
202 const ConstantArray *InitList = dyn_cast(GV->getInitializer());
203 if (InitList == 0) return;
204
202 const ConstantArray *InitList = cast(GV->getInitializer());
203
205204 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
206205 if (const GlobalVariable *G =
207206 dyn_cast(InitList->getOperand(i)->stripPointerCasts()))
0 ; RUN: llvm-as < %s | llvm-dis > %t1.ll
11 ; RUN: llvm-as %t1.ll -o - | llvm-dis > %t2.ll
22 ; RUN: diff %t1.ll %t2.ll
3
4 @llvm.used = appending global [1 x i8*] [i8* bitcast (i32* @foo1 to i8*)], section "llvm.metadata"
35
46 @bar = external global i32
57 @foo1 = alias i32* @bar
0 ; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s
1
2 @llvm.used = appending global [1 x i8*] zeroinitializer, section "llvm.metadata"
3
4 ; CHECK: wrong initalizer for intrinsic global variable
5 ; CHECK-NEXT: [1 x i8*] zeroinitializer
0 ; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s
1
2 @a = global i8 42
3 @llvm.used = appending global [2 x i8*] [i8* @a, i8* null], section "llvm.metadata"
4
5 ; CHECK: invalid llvm.used member
6 ; CHECK-NEXT: i8* null
0 ; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s
1
2 @llvm.used = appending global [1 x i32] [i32 0], section "llvm.metadata"
3
4 ; CHECK: wrong type for intrinsic global variable
5 ; CHECK-NEXT: [1 x i32]* @llvm.used
0 ; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s
1 @llvm.used = appending global i32 0, section "llvm.metadata"
2
3 ; CHECK: Only global arrays can have appending linkage!
4 ; CHEKC-NEXT: i32* @llvm.used
0 ; RUN: llvm-as < %s -o /dev/null
1
2 @a = global i32 42
3 @llvm.used = appending global [1 x i32*] [i32* @a], section "llvm.metadata"