1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
//===- Target/DirectX/PointerTypeAnalisis.cpp - PointerType analysis ------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Analysis pass to assign types to opaque pointers.
//
//===----------------------------------------------------------------------===//
#include "PointerTypeAnalysis.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
using namespace llvm;
using namespace llvm::dxil;
namespace {
Type *classifyFunctionType(const Function &F, PointerTypeMap &Map);
// Classifies the type of the value passed in by walking the value's users to
// find a typed instruction to materialize a type from.
Type *classifyPointerType(const Value *V, PointerTypeMap &Map) {
assert(V->getType()->isPointerTy() &&
"classifyPointerType called with non-pointer");
// A CallInst will trigger this case, and we want to classify its Function
// operand as a Function rather than a generic Value.
if (const Function *F = dyn_cast<Function>(V))
return classifyFunctionType(*F, Map);
// There can potentially be dead constants hanging off of the globals we do
// not want to deal with. So we remove them here.
if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
GV->removeDeadConstantUsers();
auto It = Map.find(V);
if (It != Map.end())
return It->second;
Type *PointeeTy = nullptr;
if (auto *GEP = dyn_cast<GEPOperator>(V)) {
if (!GEP->getResultElementType()->isPointerTy())
PointeeTy = GEP->getResultElementType();
} else if (auto *Inst = dyn_cast<AllocaInst>(V)) {
PointeeTy = Inst->getAllocatedType();
} else if (auto *GV = dyn_cast<GlobalVariable>(V)) {
PointeeTy = GV->getValueType();
}
for (const auto *User : V->users()) {
Type *NewPointeeTy = nullptr;
if (const auto *Inst = dyn_cast<LoadInst>(User)) {
NewPointeeTy = Inst->getType();
} else if (const auto *Inst = dyn_cast<StoreInst>(User)) {
NewPointeeTy = Inst->getValueOperand()->getType();
// When store value is ptr type, cannot get more type info.
if (NewPointeeTy->isPointerTy())
continue;
} else if (const auto *GEP = dyn_cast<GEPOperator>(User)) {
NewPointeeTy = GEP->getSourceElementType();
}
if (NewPointeeTy) {
// HLSL doesn't support pointers, so it is unlikely to get more than one
// or two levels of indirection in the IR. Because of this, recursion is
// pretty safe.
if (NewPointeeTy->isPointerTy()) {
PointeeTy = classifyPointerType(User, Map);
break;
}
if (!PointeeTy)
PointeeTy = NewPointeeTy;
else if (PointeeTy != NewPointeeTy)
PointeeTy = Type::getInt8Ty(V->getContext());
}
}
// If we were unable to determine the pointee type, set to i8
if (!PointeeTy)
PointeeTy = Type::getInt8Ty(V->getContext());
auto *TypedPtrTy =
TypedPointerType::get(PointeeTy, V->getType()->getPointerAddressSpace());
Map[V] = TypedPtrTy;
return TypedPtrTy;
}
// This function constructs a function type accepting typed pointers. It only
// handles function arguments and return types, and assigns the function type to
// the function's value in the type map.
Type *classifyFunctionType(const Function &F, PointerTypeMap &Map) {
auto It = Map.find(&F);
if (It != Map.end())
return It->second;
SmallVector<Type *, 8> NewArgs;
Type *RetTy = F.getReturnType();
LLVMContext &Ctx = F.getContext();
if (RetTy->isPointerTy()) {
RetTy = nullptr;
for (const auto &B : F) {
const auto *RetInst = dyn_cast_or_null<ReturnInst>(B.getTerminator());
if (!RetInst)
continue;
Type *NewRetTy = classifyPointerType(RetInst->getReturnValue(), Map);
if (!RetTy)
RetTy = NewRetTy;
else if (RetTy != NewRetTy)
RetTy = TypedPointerType::get(
Type::getInt8Ty(Ctx), F.getReturnType()->getPointerAddressSpace());
}
// For function decl.
if (!RetTy)
RetTy = TypedPointerType::get(
Type::getInt8Ty(Ctx), F.getReturnType()->getPointerAddressSpace());
}
for (auto &A : F.args()) {
Type *ArgTy = A.getType();
if (ArgTy->isPointerTy())
ArgTy = classifyPointerType(&A, Map);
NewArgs.push_back(ArgTy);
}
auto *TypedPtrTy =
TypedPointerType::get(FunctionType::get(RetTy, NewArgs, false), 0);
Map[&F] = TypedPtrTy;
return TypedPtrTy;
}
} // anonymous namespace
static Type *classifyConstantWithOpaquePtr(const Constant *C,
PointerTypeMap &Map) {
// FIXME: support ConstantPointerNull which could map to more than one
// TypedPointerType.
// See https://github.com/llvm/llvm-project/issues/57942.
if (isa<ConstantPointerNull>(C))
return TypedPointerType::get(Type::getInt8Ty(C->getContext()),
C->getType()->getPointerAddressSpace());
// Skip ConstantData which cannot have opaque ptr.
if (isa<ConstantData>(C))
return C->getType();
auto It = Map.find(C);
if (It != Map.end())
return It->second;
if (const auto *F = dyn_cast<Function>(C))
return classifyFunctionType(*F, Map);
Type *Ty = C->getType();
Type *TargetTy = nullptr;
if (auto *CS = dyn_cast<ConstantStruct>(C)) {
SmallVector<Type *> EltTys;
for (unsigned int I = 0; I < CS->getNumOperands(); ++I) {
const Constant *Elt = C->getAggregateElement(I);
Type *EltTy = classifyConstantWithOpaquePtr(Elt, Map);
EltTys.emplace_back(EltTy);
}
TargetTy = StructType::get(C->getContext(), EltTys);
} else if (auto *CA = dyn_cast<ConstantAggregate>(C)) {
Type *TargetEltTy = nullptr;
for (auto &Elt : CA->operands()) {
Type *EltTy = classifyConstantWithOpaquePtr(cast<Constant>(&Elt), Map);
assert(TargetEltTy == EltTy || TargetEltTy == nullptr);
TargetEltTy = EltTy;
}
if (auto *AT = dyn_cast<ArrayType>(Ty)) {
TargetTy = ArrayType::get(TargetEltTy, AT->getNumElements());
} else {
// Not struct, not array, must be vector here.
auto *VT = cast<VectorType>(Ty);
TargetTy = VectorType::get(TargetEltTy, VT);
}
}
// Must have a target ty when map.
assert(TargetTy && "PointerTypeAnalyisis failed to identify target type");
// Same type, no need to map.
if (TargetTy == Ty)
return Ty;
Map[C] = TargetTy;
return TargetTy;
}
static void classifyGlobalCtorPointerType(const GlobalVariable &GV,
PointerTypeMap &Map) {
const auto *CA = cast<ConstantArray>(GV.getInitializer());
// Type for global ctor should be array of { i32, void ()*, i8* }.
Type *CtorArrayTy = classifyConstantWithOpaquePtr(CA, Map);
// Map the global type.
Map[&GV] = TypedPointerType::get(CtorArrayTy,
GV.getType()->getPointerAddressSpace());
}
PointerTypeMap PointerTypeAnalysis::run(const Module &M) {
PointerTypeMap Map;
for (auto &G : M.globals()) {
if (G.getType()->isPointerTy())
classifyPointerType(&G, Map);
if (G.getName() == "llvm.global_ctors")
classifyGlobalCtorPointerType(G, Map);
}
for (auto &F : M) {
classifyFunctionType(F, Map);
for (const auto &B : F) {
for (const auto &I : B) {
if (I.getType()->isPointerTy())
classifyPointerType(&I, Map);
for (const auto &O : I.operands())
if (O.get()->getType()->isPointerTy())
classifyPointerType(O.get(), Map);
}
}
}
return Map;
}
|