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