Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit dc022ba

Browse files
committed
[IFUNC] Introduce GlobalIndirectSymbol as a base class for alias and ifunc
This patch is a part of http://reviews.llvm.org/D15525 GlobalIndirectSymbol class contains common implementation for both aliases and ifuncs. This patch should be NFC change that just prepare common code for ifunc support. Differential Revision: http://reviews.llvm.org/D18433 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@265016 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 8fef4bc commit dc022ba

File tree

4 files changed

+96
-27
lines changed

4 files changed

+96
-27
lines changed

include/llvm/IR/GlobalAlias.h

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717

1818
#include "llvm/ADT/Twine.h"
1919
#include "llvm/ADT/ilist_node.h"
20-
#include "llvm/IR/GlobalValue.h"
21-
#include "llvm/IR/OperandTraits.h"
20+
#include "llvm/IR/GlobalIndirectSymbol.h"
2221

2322
namespace llvm {
2423

2524
class Module;
2625
template <typename ValueSubClass> class SymbolTableListTraits;
2726

28-
class GlobalAlias : public GlobalValue, public ilist_node<GlobalAlias> {
27+
class GlobalAlias : public GlobalIndirectSymbol,
28+
public ilist_node<GlobalAlias> {
2929
friend class SymbolTableListTraits<GlobalAlias>;
3030
void operator=(const GlobalAlias &) = delete;
3131
GlobalAlias(const GlobalAlias &) = delete;
@@ -36,11 +36,6 @@ class GlobalAlias : public GlobalValue, public ilist_node<GlobalAlias> {
3636
const Twine &Name, Constant *Aliasee, Module *Parent);
3737

3838
public:
39-
// allocate space for exactly one operand
40-
void *operator new(size_t s) {
41-
return User::operator new(s, 1);
42-
}
43-
4439
/// If a parent module is specified, the alias is automatically inserted into
4540
/// the end of the specified module's alias list.
4641
static GlobalAlias *create(Type *Ty, unsigned AddressSpace,
@@ -64,9 +59,6 @@ class GlobalAlias : public GlobalValue, public ilist_node<GlobalAlias> {
6459
// Linkage, Type, Parent and AddressSpace taken from the Aliasee.
6560
static GlobalAlias *create(const Twine &Name, GlobalValue *Aliasee);
6661

67-
/// Provide fast operand accessors
68-
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
69-
7062
/// removeFromParent - This method unlinks 'this' from the containing module,
7163
/// but does not delete it.
7264
///
@@ -77,13 +69,13 @@ class GlobalAlias : public GlobalValue, public ilist_node<GlobalAlias> {
7769
///
7870
void eraseFromParent() override;
7971

80-
/// These methods retrive and set alias target.
72+
/// These methods retrieve and set alias target.
8173
void setAliasee(Constant *Aliasee);
8274
const Constant *getAliasee() const {
83-
return const_cast<GlobalAlias *>(this)->getAliasee();
75+
return getIndirectSymbol();
8476
}
8577
Constant *getAliasee() {
86-
return getOperand(0);
78+
return getIndirectSymbol();
8779
}
8880

8981
const GlobalObject *getBaseObject() const {
@@ -112,13 +104,6 @@ class GlobalAlias : public GlobalValue, public ilist_node<GlobalAlias> {
112104
}
113105
};
114106

115-
template <>
116-
struct OperandTraits<GlobalAlias> :
117-
public FixedNumOperandTraits<GlobalAlias, 1> {
118-
};
119-
120-
DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalAlias, Constant)
121-
122107
} // End llvm namespace
123108

124109
#endif
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//===- llvm/GlobalIndirectSymbol.h - GlobalIndirectSymbol class -*- C++ -*-===//
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+
// This file contains the declaration of the GlobalIndirectSymbol class, which
11+
// is a base class for GlobalAlias and GlobalIFunc. It contains all common code
12+
// for aliases and ifuncs.
13+
//
14+
//===----------------------------------------------------------------------===//
15+
16+
#ifndef LLVM_IR_GLOBALINDIRECTSYMBOL_H
17+
#define LLVM_IR_GLOBALINDIRECTSYMBOL_H
18+
19+
#include "llvm/IR/GlobalValue.h"
20+
#include "llvm/IR/OperandTraits.h"
21+
22+
namespace llvm {
23+
24+
class GlobalIndirectSymbol : public GlobalValue {
25+
void operator=(const GlobalIndirectSymbol &) = delete;
26+
GlobalIndirectSymbol(const GlobalIndirectSymbol &) = delete;
27+
28+
protected:
29+
GlobalIndirectSymbol(Type *Ty, ValueTy VTy, unsigned AddressSpace,
30+
LinkageTypes Linkage, const Twine &Name, Constant *Symbol);
31+
32+
public:
33+
// allocate space for exactly one operand
34+
void *operator new(size_t s) {
35+
return User::operator new(s, 1);
36+
}
37+
38+
/// Provide fast operand accessors
39+
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
40+
41+
/// These methods set and retrieve indirect symbol.
42+
void setIndirectSymbol(Constant *Symbol) {
43+
setOperand(0, Symbol);
44+
}
45+
const Constant *getIndirectSymbol() const {
46+
return const_cast<GlobalIndirectSymbol *>(this)->getIndirectSymbol();
47+
}
48+
Constant *getIndirectSymbol() {
49+
return getOperand(0);
50+
}
51+
52+
// Methods for support type inquiry through isa, cast, and dyn_cast:
53+
static inline bool classof(const Value *V) {
54+
return V->getValueID() == Value::GlobalAliasVal;
55+
}
56+
};
57+
58+
template <>
59+
struct OperandTraits<GlobalIndirectSymbol> :
60+
public FixedNumOperandTraits<GlobalIndirectSymbol, 1> {
61+
};
62+
63+
DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalIndirectSymbol, Constant)
64+
65+
} // End llvm namespace
66+
67+
#endif

include/llvm/IR/Value.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class ConstantData;
3131
class DataLayout;
3232
class Function;
3333
class GlobalAlias;
34+
class GlobalIndirectSymbol;
3435
class GlobalObject;
3536
class GlobalValue;
3637
class GlobalVariable;
@@ -742,9 +743,15 @@ template <> struct isa_impl<GlobalAlias, Value> {
742743
}
743744
};
744745

746+
template <> struct isa_impl<GlobalIndirectSymbol, Value> {
747+
static inline bool doit(const Value &Val) {
748+
return isa<GlobalAlias>(Val);
749+
}
750+
};
751+
745752
template <> struct isa_impl<GlobalValue, Value> {
746753
static inline bool doit(const Value &Val) {
747-
return isa<GlobalObject>(Val) || isa<GlobalAlias>(Val);
754+
return isa<GlobalObject>(Val) || isa<GlobalIndirectSymbol>(Val);
748755
}
749756
};
750757

lib/IR/Globals.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,17 +293,27 @@ void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
293293
}
294294

295295

296+
//===----------------------------------------------------------------------===//
297+
// GlobalIndirectSymbol Implementation
298+
//===----------------------------------------------------------------------===//
299+
300+
GlobalIndirectSymbol::GlobalIndirectSymbol(Type *Ty, ValueTy VTy,
301+
unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name,
302+
Constant *Symbol)
303+
: GlobalValue(Ty, VTy, &Op<0>(), 1, Linkage, Name, AddressSpace) {
304+
Op<0>() = Symbol;
305+
}
306+
307+
296308
//===----------------------------------------------------------------------===//
297309
// GlobalAlias Implementation
298310
//===----------------------------------------------------------------------===//
299311

300312
GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
301313
const Twine &Name, Constant *Aliasee,
302314
Module *ParentModule)
303-
: GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name,
304-
AddressSpace) {
305-
Op<0>() = Aliasee;
306-
315+
: GlobalIndirectSymbol(Ty, Value::GlobalAliasVal, AddressSpace, Link, Name,
316+
Aliasee) {
307317
if (ParentModule)
308318
ParentModule->getAliasList().push_back(this);
309319
}
@@ -352,5 +362,5 @@ void GlobalAlias::eraseFromParent() {
352362
void GlobalAlias::setAliasee(Constant *Aliasee) {
353363
assert((!Aliasee || Aliasee->getType() == getType()) &&
354364
"Alias and aliasee types should match!");
355-
setOperand(0, Aliasee);
365+
setIndirectSymbol(Aliasee);
356366
}

0 commit comments

Comments
 (0)