Skip to content

Commit

Permalink
[IR] Don't assume all functions are 4 byte aligned
Browse files Browse the repository at this point in the history
In some cases different alignments for function might be used to save
space e.g. thumb mode with -Oz will try to use 2 byte function
alignment. Similar patch that fixed this in other areas exists here
https://reviews.llvm.org/D46110

Differential Revision: https://reviews.llvm.org/D55115

llvm-svn: 348215
  • Loading branch information
rs-arm committed Dec 4, 2018
1 parent d97feef commit f5d1b64
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
7 changes: 3 additions & 4 deletions llvm/lib/IR/ConstantFold.cpp
Expand Up @@ -27,6 +27,7 @@
#include "llvm/IR/GlobalAlias.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/Support/ErrorHandling.h"
Expand Down Expand Up @@ -1077,10 +1078,8 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
isa<GlobalValue>(CE1->getOperand(0))) {
GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));

// Functions are at least 4-byte aligned.
unsigned GVAlign = GV->getAlignment();
if (isa<Function>(GV))
GVAlign = std::max(GVAlign, 4U);
unsigned GVAlign =
GV->getPointerAlignment(GV->getParent()->getDataLayout());

if (GVAlign > 1) {
unsigned DstWidth = CI2->getType()->getBitWidth();
Expand Down
27 changes: 27 additions & 0 deletions llvm/test/Analysis/ConstantFolding/func-and-folding.ll
@@ -0,0 +1,27 @@
; RUN: opt < %s -constprop -S -o - | FileCheck %s

; Function Attrs: minsize norecurse nounwind optsize readnone
define dso_local void @foo1() #0 {
entry:
ret void
}

; Function Attrs: minsize norecurse nounwind optsize readnone
define dso_local void @foo2() align 4 {
entry:
ret void
}

; Function Attrs: minsize nounwind optsize
define dso_local i32 @main() local_unnamed_addr #1 {
entry:
; CHECK: ptrtoint
%call = tail call i32 bitcast (i32 (...)* @process to i32 (i32)*)(i32 and (i32 ptrtoint (void ()* @foo1 to i32), i32 2)) #3
; CHECK-NEXT: ptrtoint
%call2 = tail call i32 bitcast (i32 (...)* @process to i32 (i32)*)(i32 and (i32 ptrtoint (void ()* @foo2 to i32), i32 2)) #3
ret i32 0
}

; Function Attrs: minsize optsize
declare dso_local i32 @process(...) local_unnamed_addr #2

16 changes: 0 additions & 16 deletions llvm/test/Assembler/2004-03-07-FunctionAddressAlignment.ll

This file was deleted.

0 comments on commit f5d1b64

Please sign in to comment.