LLVM: lib/Transforms/Utils/CtorUtils.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

21#include

22

23#define DEBUG_TYPE "ctor_utils"

24

25using namespace llvm;

26

27

29

33 if (!CtorsToRemove.test(I))

35

36

40

41

44 return;

45 }

46

47

53

54

57

59}

60

61

62

63static std::vector<std::pair<uint32_t, Function *>>

66 std::vector<std::pair<uint32_t, Function *>> Result;

68 for (auto &V : CA->operands()) {

70 Result.emplace_back(cast(CS->getOperand(0))->getZExtValue(),

71 dyn_cast(CS->getOperand(1)));

72 }

73 return Result;

74}

75

76

78 GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");

79 if (!GV)

80 return nullptr;

81

82

83

85 return nullptr;

86

87

88

90 if (!CA)

91 return nullptr;

92

93 for (auto &V : CA->operands()) {

94 if (isa(V))

95 continue;

97 if (isa(CS->getOperand(1)))

98 continue;

99

100

102 if (F || F->arg_size() != 0)

103 return nullptr;

104 }

105 return GV;

106}

107

108

109

113 if (!GlobalCtors)

114 return false;

115

116 std::vector<std::pair<uint32_t, Function *>> Ctors =

118 if (Ctors.empty())

119 return false;

120

121 bool MadeChange = false;

122

123 BitVector CtorsToRemove(Ctors.size());

124 std::vector<size_t> CtorsByPriority(Ctors.size());

125 std::iota(CtorsByPriority.begin(), CtorsByPriority.end(), 0);

127 return Ctors[LHS].first < Ctors[RHS].first;

128 });

129 for (unsigned CtorIndex : CtorsByPriority) {

130 const uint32_t Priority = Ctors[CtorIndex].first;

131 Function *F = Ctors[CtorIndex].second;

132 if (F)

133 continue;

134

135 LLVM_DEBUG(dbgs() << "Optimizing Global Constructor: " << *F << "\n");

136

137

138 if (ShouldRemove(Priority, F)) {

139 Ctors[CtorIndex].second = nullptr;

140 CtorsToRemove.set(CtorIndex);

141 MadeChange = true;

142 continue;

143 }

144 }

145

146 if (!MadeChange)

147 return false;

148

150 return true;

151}

This file implements the BitVector class.

This file contains the declarations for the subclasses of Constant, which represent the different fla...

static void removeGlobalCtors(GlobalVariable *GCL, const BitVector &CtorsToRemove)

Given a specified llvm.global_ctors list, remove the listed elements.

static std::vector< std::pair< uint32_t, Function * > > parseGlobalCtors(GlobalVariable *GV)

Given a llvm.global_ctors list that we can understand, return a list of the functions and null termin...

static GlobalVariable * findGlobalCtors(Module &M)

Find the llvm.global_ctors list.

Module.h This file contains the declarations for the Module class.

Type * getElementType() const

bool test(unsigned Idx) const

ConstantArray - Constant Array Declarations.

static Constant * get(ArrayType *T, ArrayRef< Constant * > V)

ArrayType * getType() const

Specialize the getType() method to always return an ArrayType, which reduces the amount of casting ne...

This is an important base class in LLVM.

LinkageTypes getLinkage() const

ThreadLocalMode getThreadLocalMode() const

unsigned getAddressSpace() const

Module * getParent()

Get the module that this global value is contained inside of...

const Constant * getInitializer() const

getInitializer - Return the initializer for this global variable.

void setInitializer(Constant *InitVal)

setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...

bool isConstant() const

If the value is a global constant, its value is immutable throughout the runtime execution of the pro...

bool hasUniqueInitializer() const

hasUniqueInitializer - Whether the global variable has an initializer, and any changes made to the in...

void eraseFromParent()

eraseFromParent - This method unlinks 'this' from the containing module and deletes it.

A Module instance is used to store all the information related to an LLVM module.

void insertGlobalVariable(GlobalVariable *GV)

Insert global variable GV at the end of the global variable list and take ownership.

void push_back(const T &Elt)

This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.

Value * getOperand(unsigned i) const

unsigned getNumOperands() const

Type * getType() const

All values are typed, get the type of this value.

void replaceAllUsesWith(Value *V)

Change all uses of this to point to a new Value.

void takeName(Value *V)

Transfer the name from V to this value.

An efficient, type-erasing, non-owning reference to a callable.

self_iterator getIterator()

This is an optimization pass for GlobalISel generic memory operations.

void stable_sort(R &&Range)

bool optimizeGlobalCtorsList(Module &M, function_ref< bool(uint32_t, Function *)> ShouldRemove)

Call "ShouldRemove" for every entry in M's global_ctor list and remove the entries for which it retur...

raw_ostream & dbgs()

dbgs() - This returns a reference to a raw_ostream for debugging messages.