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

1

2

3

4

5

6

7

8

9

10

11

12

13

21

22using namespace llvm;

23

28

31

32

33

34

35

36

37

38

39

40

41

42

43

44

47 return false;

48

50 if (GEP || GEP->hasOneUse())

51 return false;

52

54 if (!Load || !Load->hasOneUse())

55 return false;

56

57

59 Type *ElemType = Load->getType();

60 if (!ElemType->isPointerTy() || DL.getPointerTypeSizeInBits(ElemType) != 64)

61 return false;

62

63

64 unsigned IndexWidth =

65 DL.getIndexTypeSizeInBits(Load->getPointerOperand()->getType());

67 APInt ConstOffset(IndexWidth, 0);

68 if (GEP->collectOffset(DL, IndexWidth, VarOffsets, ConstOffset) ||

69 !ConstOffset.isZero() || VarOffsets.size() != 1)

70 return false;

71

72

73

74 Info.Index = VarOffsets.front().first;

75 const APInt &Stride = VarOffsets.front().second;

76 if (Stride.ult(DL.getTypeStoreSize(ElemType)))

77 return false;

78

80 Triple TT = M.getTargetTriple();

81

82 bool ShouldDropUnnamedAddr =

83

84

85

86 TT.isAArch64()

87

88

89

90

91 || (TT.isX86() && TT.isOSDarwin());

92

95 for (; Offset.ult(GVSize); Offset += Stride) {

98 if (C)

99 return false;

100

103

104

105

107 return false;

108

109

111 if (!GlovalVarOp || !GlovalVarOp->isConstant())

112 return false;

113

114 if (!GlovalVarOp->hasLocalLinkage() ||

115 !GlovalVarOp->isDSOLocal() ||

116 !GlovalVarOp->isImplicitDSOLocal())

117 return false;

118

119 if (ShouldDropUnnamedAddr)

121

122 Info.Ptrs.push_back(C);

123 }

124

125 if (ShouldDropUnnamedAddr)

126 for (auto *GVOp : GVOps)

128

129 return true;

130}

131

135 Module &M = *Func.getParent();

138

140 M, IntArrayTy, LookupTable.isConstant(), LookupTable.getLinkage(),

141 nullptr, LookupTable.getName() + ".rel", &LookupTable,

142 LookupTable.getThreadLocalMode(), LookupTable.getAddressSpace(),

143 LookupTable.isExternallyInitialized());

144

147

149 Type *IntPtrTy = M.getDataLayout().getIntPtrType(M.getContext());

155 RelLookupTableContents[Idx++] = RelOffset;

156 }

157

163 return RelLookupTable;

164}

165

171

172 Module &M = *LookupTable.getParent();

176

177

180

181

182 Builder.SetInsertPoint(GEP);

184 Value *Offset = Builder.CreateShl(Info.Index, ConstantInt::get(IntTy, 2),

185 "reltable.shift");

186

187

188

189

190 Builder.SetInsertPoint(Load);

192 &M, Intrinsic::load_relative, {Info.Index->getType()});

193

194

195

196 Value *Result = Builder.CreateCall(LoadRelIntrinsic, {RelLookupTable, Offset},

197 "reltable.intrinsic");

198

199

200 Load->replaceAllUsesWith(Result);

201

202 Load->eraseFromParent();

203 GEP->eraseFromParent();

204}

205

206

210 if (F.isDeclaration())

211 continue;

212

213

214 if (!GetTTI(F).shouldBuildRelLookupTables())

215 return false;

216

217

218 break;

219 }

220

222

226 continue;

227

229

230

231 GV.eraseFromParent();

232

234 }

235

237}

238

243

246 };

247

250

253 return PA;

254}

MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL

Analysis containing CSE Info

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

FunctionAnalysisManager FAM

static bool convertToRelativeLookupTables(Module &M, function_ref< TargetTransformInfo &(Function &)> GetTTI)

Definition RelLookupTableConverter.cpp:207

static bool shouldConvertToRelLookupTable(LookupTableInfo &Info, Module &M, GlobalVariable &GV)

Definition RelLookupTableConverter.cpp:29

static GlobalVariable * createRelLookupTable(LookupTableInfo &Info, Function &Func, GlobalVariable &LookupTable)

Definition RelLookupTableConverter.cpp:132

static void convertToRelLookupTable(LookupTableInfo &Info, GlobalVariable &LookupTable)

Definition RelLookupTableConverter.cpp:166

This file implements relative lookup table converter that converts lookup tables to relative lookup t...

This pass exposes codegen information to IR-level passes.

Class for arbitrary precision integers.

bool isZero() const

Determine if this value is zero, i.e. all bits are clear.

bool ult(const APInt &RHS) const

Unsigned less than comparison.

PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)

Get the result of an analysis pass for a given IR unit.

static LLVM_ABI ArrayType * get(Type *ElementType, uint64_t NumElements)

This static method is the primary way to construct an ArrayType.

LLVM Basic Block Representation.

const Function * getParent() const

Return the enclosing method, or null if none.

Represents analyses that only rely on functions' control flow.

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

static LLVM_ABI Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)

static LLVM_ABI Constant * getPtrToInt(Constant *C, Type *Ty, bool OnlyIfReduced=false)

static LLVM_ABI Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)

This is an important base class in LLVM.

A parsed version of the target data layout string in and methods for querying it.

an instruction for type-safe pointer arithmetic to access elements of arrays and structs

bool isImplicitDSOLocal() const

void setUnnamedAddr(UnnamedAddr Val)

bool hasLocalLinkage() const

Type * getValueType() const

const Constant * getInitializer() const

getInitializer - Return the initializer for this global variable.

LLVM_ABI void setInitializer(Constant *InitVal)

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

bool hasInitializer() const

Definitions have initializers, declarations don't.

bool isConstant() const

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

void setAlignment(Align Align)

Sets the alignment attribute of the GlobalVariable.

This provides a uniform API for creating instructions and inserting them into a basic block: either a...

Class to represent integer types.

An instruction for reading from memory.

std::pair< KeyT, ValueT > & front()

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

A set of analyses that are preserved following a run of a transformation pass.

static PreservedAnalyses all()

Construct a special preserved set that preserves all passes.

PreservedAnalyses & preserveSet()

Mark an analysis set as preserved.

PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)

Definition RelLookupTableConverter.cpp:239

void push_back(const T &Elt)

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

Analysis pass providing the TargetTransformInfo.

This pass provides access to the codegen interfaces that are needed for IR-level transformations.

Target - Wrapper for Target specific information.

Triple - Helper class for working with autoconf configuration names.

The instances of the Type class are immutable: once they are created, they are never changed.

static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)

bool isPointerTy() const

True if this is an instance of PointerType.

User * getUser() const

Returns the User that contains this Use.

LLVM Value Representation.

bool hasOneUse() const

Return true if there is exactly one use of this value.

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

@ C

The default llvm calling convention, compatible with C.

LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})

Look up the Function declaration of the intrinsic id in the Module M.

This is an optimization pass for GlobalISel generic memory operations.

decltype(auto) dyn_cast(const From &Val)

dyn_cast - Return the argument parameter cast to the specified type.

LLVM_ABI bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset, const DataLayout &DL, DSOLocalEquivalent **DSOEquiv=nullptr)

If this constant is a constant offset from a global, return the global and the constant.

iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)

Make a range that does early increment to allow mutation of the underlying range without disrupting i...

InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy

Provide the FunctionAnalysisManager to Module proxy.

LLVM_ABI Constant * ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset, const DataLayout &DL)

Extract value of C at the given Offset reinterpreted as Ty.

@ Sub

Subtraction of integers.

decltype(auto) cast(const From &Val)

cast - Return the argument parameter cast to the specified type.

AnalysisManager< Function > FunctionAnalysisManager

Convenience typedef for the Function analysis manager.

AnalysisManager< Module > ModuleAnalysisManager

Convenience typedef for the Module analysis manager.

SmallVector< Constant * > Ptrs

Definition RelLookupTableConverter.cpp:26

Value * Index

Definition RelLookupTableConverter.cpp:25

This struct is a compact representation of a valid (non-zero power of two) alignment.

A MapVector that performs no allocations if smaller than a certain size.