Skip to content

Commit 2c0fb96

Browse files
committed
[TypeFinder] Support opaque pointers
We need to explicitly visit a number of types, as these are no longer reachable through the pointer type if opaque pointers are enabled. This is similar to ValueEnumerator changes that have been done previously.
1 parent f282b68 commit 2c0fb96

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

llvm/include/llvm/IR/TypeFinder.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define LLVM_IR_TYPEFINDER_H
1515

1616
#include "llvm/ADT/DenseSet.h"
17+
#include "llvm/IR/Attributes.h"
1718
#include <cstddef>
1819
#include <vector>
1920

@@ -32,6 +33,7 @@ class TypeFinder {
3233
// objects, we keep several helper maps.
3334
DenseSet<const Value*> VisitedConstants;
3435
DenseSet<const MDNode *> VisitedMetadata;
36+
DenseSet<AttributeList> VisitedAttributes;
3537
DenseSet<Type*> VisitedTypes;
3638

3739
std::vector<StructType*> StructTypes;
@@ -74,6 +76,9 @@ class TypeFinder {
7476
/// incorporateMDNode - This method is used to walk the operands of an MDNode
7577
/// to find types hiding within.
7678
void incorporateMDNode(const MDNode *V);
79+
80+
/// Incorporate types referenced by attributes.
81+
void incorporateAttributes(AttributeList AL);
7782
};
7883

7984
} // end namespace llvm

llvm/lib/IR/TypeFinder.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
#include "llvm/IR/DerivedTypes.h"
1919
#include "llvm/IR/Function.h"
2020
#include "llvm/IR/Instruction.h"
21+
#include "llvm/IR/Instructions.h"
2122
#include "llvm/IR/Metadata.h"
2223
#include "llvm/IR/Module.h"
24+
#include "llvm/IR/Operator.h"
2325
#include "llvm/IR/Type.h"
2426
#include "llvm/IR/Use.h"
2527
#include "llvm/IR/User.h"
@@ -34,22 +36,23 @@ void TypeFinder::run(const Module &M, bool onlyNamed) {
3436

3537
// Get types from global variables.
3638
for (const auto &G : M.globals()) {
37-
incorporateType(G.getType());
39+
incorporateType(G.getValueType());
3840
if (G.hasInitializer())
3941
incorporateValue(G.getInitializer());
4042
}
4143

4244
// Get types from aliases.
4345
for (const auto &A : M.aliases()) {
44-
incorporateType(A.getType());
46+
incorporateType(A.getValueType());
4547
if (const Value *Aliasee = A.getAliasee())
4648
incorporateValue(Aliasee);
4749
}
4850

4951
// Get types from functions.
5052
SmallVector<std::pair<unsigned, MDNode *>, 4> MDForInst;
5153
for (const Function &FI : M) {
52-
incorporateType(FI.getType());
54+
incorporateType(FI.getFunctionType());
55+
incorporateAttributes(FI.getAttributes());
5356

5457
for (const Use &U : FI.operands())
5558
incorporateValue(U.get());
@@ -69,6 +72,13 @@ void TypeFinder::run(const Module &M, bool onlyNamed) {
6972
if (&*O && !isa<Instruction>(&*O))
7073
incorporateValue(&*O);
7174

75+
if (auto *GEP = dyn_cast<GetElementPtrInst>(&I))
76+
incorporateType(GEP->getSourceElementType());
77+
if (auto *AI = dyn_cast<AllocaInst>(&I))
78+
incorporateType(AI->getAllocatedType());
79+
if (const auto *CB = dyn_cast<CallBase>(&I))
80+
incorporateAttributes(CB->getAttributes());
81+
7282
// Incorporate types hiding in metadata.
7383
I.getAllMetadataOtherThanDebugLoc(MDForInst);
7484
for (const auto &MD : MDForInst)
@@ -138,6 +148,9 @@ void TypeFinder::incorporateValue(const Value *V) {
138148
if (isa<Instruction>(V))
139149
return;
140150

151+
if (auto *GEP = dyn_cast<GEPOperator>(V))
152+
incorporateType(GEP->getSourceElementType());
153+
141154
// Look in operands for types.
142155
const User *U = cast<User>(V);
143156
for (const auto &I : U->operands())
@@ -173,3 +186,13 @@ void TypeFinder::incorporateMDNode(const MDNode *V) {
173186
}
174187
}
175188
}
189+
190+
void TypeFinder::incorporateAttributes(AttributeList AL) {
191+
if (!VisitedAttributes.insert(AL).second)
192+
return;
193+
194+
for (AttributeSet AS : AL)
195+
for (Attribute A : AS)
196+
if (A.isTypeAttribute())
197+
incorporateType(A.getValueAsType());
198+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
; RUN: opt -S -opaque-pointers < %s | opt -S -opaque-pointers | FileCheck %s
2+
3+
; CHECK: %T1 = type { i8 }
4+
; CHECK: %T2 = type { i8 }
5+
; CHECK: %T3 = type { i8 }
6+
; CHECK: %T4 = type { i8 }
7+
; CHECK: %T5 = type { i8 }
8+
; CHECK: %T6 = type { i8 }
9+
; CHECK: %T7 = type { i8 }
10+
11+
%T1 = type { i8 }
12+
%T2 = type { i8 }
13+
%T3 = type { i8 }
14+
%T4 = type { i8 }
15+
%T5 = type { i8 }
16+
%T6 = type { i8 }
17+
%T7 = type { i8 }
18+
19+
@g = external global %T1
20+
21+
define %T2 @f(ptr %p) {
22+
alloca %T3
23+
getelementptr %T4, ptr %p, i64 1
24+
call void @f(ptr sret(%T5) %p)
25+
store ptr getelementptr (%T6, ptr @g, i64 1), ptr %p
26+
unreachable
27+
}
28+
29+
declare void @f2(ptr sret(%T7))

0 commit comments

Comments
 (0)