Skip to content

Commit d469133

Browse files
author
Robert Lougher
committed
[Evaluator] Walk initial elements when handling load through bitcast
When evaluating a store through a bitcast, the evaluator tries to move the bitcast from the pointer onto the stored value. If the cast is invalid, it tries to "introspect" the type to get a valid cast by obtaining a pointer to the initial element (if the type is nested, this may require walking several initial elements). In some situations it is possible to get a bitcast on a load (e.g. with unions, where the bitcast may not be the same type as the store). However, equivalent logic to the store to introspect the type is missing. This patch add this logic. Note, when developing the patch I was unhappy with adding similar logic directly to the load case as it could get out of step. Instead, I have abstracted the "introspection" into a helper function, with the specifics being handled by a passed-in lambda function. Differential Revision: https://reviews.llvm.org/D60793 llvm-svn: 359205
1 parent ba55767 commit d469133

File tree

3 files changed

+158
-38
lines changed

3 files changed

+158
-38
lines changed

llvm/lib/Transforms/Utils/Evaluator.cpp

Lines changed: 65 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,34 @@ static bool isSimpleEnoughPointerToCommit(Constant *C) {
174174
return false;
175175
}
176176

177+
/// Apply 'Func' to Ptr. If this returns nullptr, introspect the pointer's
178+
/// type and walk down through the initial elements to obtain additional
179+
/// pointers to try. Returns the first non-null return value from Func, or
180+
/// nullptr if the type can't be introspected further.
181+
static Constant *
182+
evaluateBitcastFromPtr(Constant *Ptr, const DataLayout &DL,
183+
const TargetLibraryInfo *TLI,
184+
std::function<Constant *(Constant *)> Func) {
185+
Constant *Val;
186+
while (!(Val = Func(Ptr))) {
187+
// If Ty is a struct, we can convert the pointer to the struct
188+
// into a pointer to its first member.
189+
// FIXME: This could be extended to support arrays as well.
190+
Type *Ty = cast<PointerType>(Ptr->getType())->getElementType();
191+
if (!isa<StructType>(Ty))
192+
break;
193+
194+
IntegerType *IdxTy = IntegerType::get(Ty->getContext(), 32);
195+
Constant *IdxZero = ConstantInt::get(IdxTy, 0, false);
196+
Constant *const IdxList[] = {IdxZero, IdxZero};
197+
198+
Ptr = ConstantExpr::getGetElementPtr(Ty, Ptr, IdxList);
199+
if (auto *FoldedPtr = ConstantFoldConstant(Ptr, DL, TLI))
200+
Ptr = FoldedPtr;
201+
}
202+
return Val;
203+
}
204+
177205
static Constant *getInitializer(Constant *C) {
178206
auto *GV = dyn_cast<GlobalVariable>(C);
179207
return GV && GV->hasDefinitiveInitializer() ? GV->getInitializer() : nullptr;
@@ -184,8 +212,14 @@ static Constant *getInitializer(Constant *C) {
184212
Constant *Evaluator::ComputeLoadResult(Constant *P) {
185213
// If this memory location has been recently stored, use the stored value: it
186214
// is the most up-to-date.
187-
DenseMap<Constant*, Constant*>::const_iterator I = MutatedMemory.find(P);
188-
if (I != MutatedMemory.end()) return I->second;
215+
auto findMemLoc = [this](Constant *Ptr) {
216+
DenseMap<Constant *, Constant *>::const_iterator I =
217+
MutatedMemory.find(Ptr);
218+
return I != MutatedMemory.end() ? I->second : nullptr;
219+
};
220+
221+
if (Constant *Val = findMemLoc(P))
222+
return Val;
189223

190224
// Access it.
191225
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(P)) {
@@ -203,13 +237,17 @@ Constant *Evaluator::ComputeLoadResult(Constant *P) {
203237
break;
204238
// Handle a constantexpr bitcast.
205239
case Instruction::BitCast:
206-
Constant *Val = getVal(CE->getOperand(0));
207-
auto MM = MutatedMemory.find(Val);
208-
auto *I = (MM != MutatedMemory.end()) ? MM->second
209-
: getInitializer(CE->getOperand(0));
210-
if (I)
240+
// We're evaluating a load through a pointer that was bitcast to a
241+
// different type. See if the "from" pointer has recently been stored.
242+
// If it hasn't, we may still be able to find a stored pointer by
243+
// introspecting the type.
244+
Constant *Val =
245+
evaluateBitcastFromPtr(CE->getOperand(0), DL, TLI, findMemLoc);
246+
if (!Val)
247+
Val = getInitializer(CE->getOperand(0));
248+
if (Val)
211249
return ConstantFoldLoadThroughBitcast(
212-
I, P->getType()->getPointerElementType(), DL);
250+
Val, P->getType()->getPointerElementType(), DL);
213251
break;
214252
}
215253
}
@@ -329,37 +367,26 @@ bool Evaluator::EvaluateBlock(BasicBlock::iterator CurInst,
329367
<< "Attempting to resolve bitcast on constant ptr.\n");
330368
// If we're evaluating a store through a bitcast, then we need
331369
// to pull the bitcast off the pointer type and push it onto the
332-
// stored value.
333-
Ptr = CE->getOperand(0);
334-
335-
Type *NewTy = cast<PointerType>(Ptr->getType())->getElementType();
336-
337-
// In order to push the bitcast onto the stored value, a bitcast
338-
// from NewTy to Val's type must be legal. If it's not, we can try
339-
// introspecting NewTy to find a legal conversion.
340-
Constant *NewVal;
341-
while (!(NewVal = ConstantFoldLoadThroughBitcast(Val, NewTy, DL))) {
342-
// If NewTy is a struct, we can convert the pointer to the struct
343-
// into a pointer to its first member.
344-
// FIXME: This could be extended to support arrays as well.
345-
if (StructType *STy = dyn_cast<StructType>(NewTy)) {
346-
347-
IntegerType *IdxTy = IntegerType::get(NewTy->getContext(), 32);
348-
Constant *IdxZero = ConstantInt::get(IdxTy, 0, false);
349-
Constant * const IdxList[] = {IdxZero, IdxZero};
350-
351-
Ptr = ConstantExpr::getGetElementPtr(NewTy, Ptr, IdxList);
352-
if (auto *FoldedPtr = ConstantFoldConstant(Ptr, DL, TLI))
353-
Ptr = FoldedPtr;
354-
NewTy = STy->getTypeAtIndex(0U);
355-
356-
// If we can't improve the situation by introspecting NewTy,
357-
// we have to give up.
358-
} else {
359-
LLVM_DEBUG(dbgs() << "Failed to bitcast constant ptr, can not "
360-
"evaluate.\n");
361-
return false;
370+
// stored value. In order to push the bitcast onto the stored value,
371+
// a bitcast from the pointer's element type to Val's type must be
372+
// legal. If it's not, we can try introspecting the type to find a
373+
// legal conversion.
374+
375+
auto castValTy = [&](Constant *P) -> Constant * {
376+
Type *Ty = cast<PointerType>(P->getType())->getElementType();
377+
if (Constant *FV = ConstantFoldLoadThroughBitcast(Val, Ty, DL)) {
378+
Ptr = P;
379+
return FV;
362380
}
381+
return nullptr;
382+
};
383+
384+
Constant *NewVal =
385+
evaluateBitcastFromPtr(CE->getOperand(0), DL, TLI, castValTy);
386+
if (!NewVal) {
387+
LLVM_DEBUG(dbgs() << "Failed to bitcast constant ptr, can not "
388+
"evaluate.\n");
389+
return false;
363390
}
364391

365392
Val = NewVal;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
; RUN: opt < %s -globalopt -S | FileCheck %s
2+
3+
; Test the evaluation of a store and a load via a bitcast, and check
4+
; that globals are constant folded to the correct value.
5+
6+
; CHECK: @u = dso_local local_unnamed_addr global %union.A { i8* inttoptr (i64 12345 to i8*) }, align 8
7+
; CHECK: @l = dso_local local_unnamed_addr global i64 12345, align 8
8+
9+
; Test derived from:
10+
;
11+
; union A {
12+
; A(long long ll) : l(ll) {}
13+
; void *p;
14+
; long long l;
15+
; } u(12345);
16+
;
17+
; long long l = u.l;
18+
19+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
20+
target triple = "x86_64-unknown-linux-gnu"
21+
22+
%union.A = type { i8* }
23+
24+
$_ZN1AC2Ex = comdat any
25+
26+
@u = dso_local global %union.A zeroinitializer, align 8
27+
@l = dso_local global i64 0, align 8
28+
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @_GLOBAL__sub_I_test.cpp, i8* null }]
29+
30+
define internal void @__cxx_global_var_init() section ".text.startup" {
31+
call void @_ZN1AC2Ex(%union.A* @u, i64 12345)
32+
ret void
33+
}
34+
35+
define linkonce_odr dso_local void @_ZN1AC2Ex(%union.A* %this, i64 %ll) unnamed_addr comdat align 2 {
36+
%l = bitcast %union.A* %this to i64*
37+
store i64 %ll, i64* %l, align 8
38+
ret void
39+
}
40+
41+
define internal void @__cxx_global_var_init.1() section ".text.startup" {
42+
%1 = load i64, i64* bitcast (%union.A* @u to i64*), align 8
43+
store i64 %1, i64* @l, align 8
44+
ret void
45+
}
46+
47+
define internal void @_GLOBAL__sub_I_test.cpp() section ".text.startup" {
48+
call void @__cxx_global_var_init()
49+
call void @__cxx_global_var_init.1()
50+
ret void
51+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
; RUN: opt < %s -globalopt -S | FileCheck %s
2+
3+
; Test the evaluation of a load via a bitcast and a store via a GEP.
4+
; Check that globals are constant folded to the correct value.
5+
6+
; CHECK: @u = dso_local local_unnamed_addr global %union.A { i8* inttoptr (i64 12345 to i8*) }, align 8
7+
; CHECK: @l = dso_local local_unnamed_addr global i64 12345, align 8
8+
9+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
10+
target triple = "x86_64-unknown-linux-gnu"
11+
12+
%union.A = type { i8* }
13+
14+
$_ZN1AC2Ex = comdat any
15+
16+
@u = dso_local global %union.A zeroinitializer, align 8
17+
@l = dso_local global i64 0, align 8
18+
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @_GLOBAL__sub_I_test.cpp, i8* null }]
19+
20+
define internal void @__cxx_global_var_init() section ".text.startup" {
21+
call void @_ZN1AC2Ex(%union.A* @u, i64 12345)
22+
ret void
23+
}
24+
25+
define linkonce_odr dso_local void @_ZN1AC2Ex(%union.A* %this, i64 %ll) unnamed_addr comdat align 2 {
26+
%l = inttoptr i64 %ll to i8*
27+
%p = getelementptr inbounds %union.A, %union.A* %this, i64 0, i32 0
28+
store i8* %l, i8** %p
29+
ret void
30+
}
31+
32+
define internal void @__cxx_global_var_init.1() section ".text.startup" {
33+
%1 = load i64, i64* bitcast (%union.A* @u to i64*), align 8
34+
store i64 %1, i64* @l, align 8
35+
ret void
36+
}
37+
38+
define internal void @_GLOBAL__sub_I_test.cpp() section ".text.startup" {
39+
call void @__cxx_global_var_init()
40+
call void @__cxx_global_var_init.1()
41+
ret void
42+
}

0 commit comments

Comments
 (0)