Skip to content

Commit

Permalink
Special case aliases in GlobalValue::getAlignment.
Browse files Browse the repository at this point in the history
An alias has the address of what it points to, so it also has the same
alignment.

This allows a few optimizations to see past aliases for free.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@208103 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
espindola committed May 6, 2014
1 parent bfc3f30 commit b889448
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
4 changes: 1 addition & 3 deletions include/llvm/IR/GlobalValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ class GlobalValue : public Constant {
removeDeadConstantUsers(); // remove any dead constants using this.
}

unsigned getAlignment() const {
return (1u << Alignment) >> 1;
}
unsigned getAlignment() const;
void setAlignment(unsigned Align);

bool hasUnnamedAddr() const { return UnnamedAddr; }
Expand Down
7 changes: 7 additions & 0 deletions lib/IR/Globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
setDLLStorageClass(Src->getDLLStorageClass());
}

unsigned GlobalValue::getAlignment() const {
if (auto *GA = dyn_cast<GlobalAlias>(this))
return GA->getAliasedGlobal()->getAlignment();

return (1u << Alignment) >> 1;
}

void GlobalValue::setAlignment(unsigned Align) {
assert((!isa<GlobalAlias>(this)) &&
"GlobalAlias should not have an alignment!");
Expand Down
1 change: 0 additions & 1 deletion lib/IR/Verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,6 @@ void Verifier::visitGlobalAlias(const GlobalAlias &GA) {
"Alias and aliasee types should match!", &GA);
Assert1(!GA.hasUnnamedAddr(), "Alias cannot have unnamed_addr!", &GA);
Assert1(!GA.hasSection(), "Alias cannot have a section!", &GA);
Assert1(!GA.getAlignment(), "Alias connot have an alignment", &GA);

const Constant *Aliasee = GA.getAliasee();
const GlobalValue *GV = dyn_cast<GlobalValue>(Aliasee);
Expand Down
14 changes: 14 additions & 0 deletions test/CodeGen/AArch64/global-alignment.ll
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
@var32 = global [3 x i32] zeroinitializer
@var64 = global [3 x i64] zeroinitializer
@var32_align64 = global [3 x i32] zeroinitializer, align 8
@alias = alias [3 x i32]* @var32_align64

define i64 @test_align32() {
; CHECK-LABEL: test_align32:
Expand Down Expand Up @@ -47,6 +48,19 @@ define i64 @test_var32_align64() {
ret i64 %val
}

define i64 @test_var32_alias() {
; CHECK-LABEL: test_var32_alias:
%addr = bitcast [3 x i32]* @alias to i64*

; Test that we can find the alignment for aliases.
%val = load i64* %addr
; CHECK: adrp x[[HIBITS:[0-9]+]], alias
; CHECK-NOT: add x[[HIBITS]]
; CHECK: ldr x0, [x[[HIBITS]], {{#?}}:lo12:alias]

ret i64 %val
}

@yet_another_var = external global {i32, i32}

define i64 @test_yet_another_var() {
Expand Down

0 comments on commit b889448

Please sign in to comment.