Skip to content

Commit

Permalink
Get rid of the getPointeeAlignment helper function from
Browse files Browse the repository at this point in the history
InstCombineLoadStoreAlloca.cpp, which had many issues.
(At least two bugs were noted on llvm-commits, and it was overly conservative.)
Instead, use getOrEnforceKnownAlignment.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@168629 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
eefriedman committed Nov 26, 2012
1 parent 1243922 commit 0c617e6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
26 changes: 4 additions & 22 deletions lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,26 +150,6 @@ isOnlyCopiedFromConstantGlobal(AllocaInst *AI,
return 0;
}

/// getPointeeAlignment - Compute the minimum alignment of the value pointed
/// to by the given pointer.
static unsigned getPointeeAlignment(Value *V, const DataLayout &TD) {
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
if (CE->getOpcode() == Instruction::BitCast ||
(CE->getOpcode() == Instruction::GetElementPtr &&
cast<GEPOperator>(CE)->hasAllZeroIndices()))
return getPointeeAlignment(CE->getOperand(0), TD);

if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
if (!GV->isDeclaration())
return TD.getPreferredAlignment(GV);

if (PointerType *PT = dyn_cast<PointerType>(V->getType()))
if (PT->getElementType()->isSized())
return TD.getABITypeAlignment(PT->getElementType());

return 0;
}

Instruction *InstCombiner::visitAllocaInst(AllocaInst &AI) {
// Ensure that the alloca array size argument has type intptr_t, so that
// any casting is exposed early.
Expand Down Expand Up @@ -265,7 +245,7 @@ Instruction *InstCombiner::visitAllocaInst(AllocaInst &AI) {
}
}

if (TD) {
if (AI.getAlignment()) {
// Check to see if this allocation is only modified by a memcpy/memmove from
// a constant global whose alignment is equal to or exceeds that of the
// allocation. If this is the case, we can change all users to use
Expand All @@ -274,7 +254,9 @@ Instruction *InstCombiner::visitAllocaInst(AllocaInst &AI) {
// is only subsequently read.
SmallVector<Instruction *, 4> ToDelete;
if (MemTransferInst *Copy = isOnlyCopiedFromConstantGlobal(&AI, ToDelete)) {
if (AI.getAlignment() <= getPointeeAlignment(Copy->getSource(), *TD)) {
unsigned SourceAlign = getOrEnforceKnownAlignment(Copy->getSource(),
AI.getAlignment(), TD);
if (AI.getAlignment() <= SourceAlign) {
DEBUG(dbgs() << "Found alloca equal to global: " << AI << '\n');
DEBUG(dbgs() << " memcpy = " << *Copy << '\n');
for (unsigned i = 0, e = ToDelete.size(); i != e; ++i)
Expand Down
10 changes: 10 additions & 0 deletions test/Transforms/InstCombine/memcpy-from-global.ll
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,13 @@ define void @test8() {
; CHECK: bar
ret void
}

define void @test9() {
%A = alloca %U, align 4
%a = bitcast %U* %A to i8*
call void @llvm.memcpy.p0i8.p0i8.i64(i8* %a, i8* bitcast (%U* getelementptr ([2 x %U]* @H, i64 0, i32 1) to i8*), i64 20, i32 4, i1 false)
call void @bar(i8* %a) readonly
; CHECK: @test9
; CHECK-NEXT: call void @bar(i8* bitcast (%U* getelementptr inbounds ([2 x %U]* @H, i64 0, i64 1) to i8*))
ret void
}

0 comments on commit 0c617e6

Please sign in to comment.