[PATCH] D55115: [IR] Don't assume all functions are 4 byte aligned
Ranjeet Singh via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Nov 30 03:38:10 PST 2018
rs created this revision.
rs added reviewers: petpav01, olista01.
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
Repository:
rL LLVM
https://reviews.llvm.org/D55115
Files:
lib/IR/ConstantFold.cpp
test/Analysis/ConstantFolding/func-and-folding.ll
test/Assembler/2004-03-07-FunctionAddressAlignment.ll
Index: test/Assembler/2004-03-07-FunctionAddressAlignment.ll
===================================================================
--- test/Assembler/2004-03-07-FunctionAddressAlignment.ll
+++ test/Assembler/2004-03-07-FunctionAddressAlignment.ll
@@ -3,11 +3,11 @@
; All of these should be eliminable
-define i32 @foo() {
+define i32 @foo() align 4 {
ret i32 and (i32 ptrtoint (i32()* @foo to i32), i32 1)
}
-define i32 @foo2() {
+define i32 @foo2() align 4 {
ret i32 and (i32 1, i32 ptrtoint (i32()* @foo2 to i32))
}
Index: test/Analysis/ConstantFolding/func-and-folding.ll
===================================================================
--- /dev/null
+++ test/Analysis/ConstantFolding/func-and-folding.ll
@@ -0,0 +1,35 @@
+; 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() #0 {
+entry:
+ ret void
+}
+
+; Function Attrs: minsize norecurse nounwind optsize readnone
+define dso_local void @foo3() 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
+; CHECK-NOT: ptrtoint
+ %call3 = tail call i32 bitcast (i32 (...)* @process to i32 (i32)*)(i32 and (i32 ptrtoint (void ()* @foo3 to i32), i32 2)) #3
+ ret i32 0
+}
+
+; Function Attrs: minsize optsize
+declare dso_local i32 @process(...) local_unnamed_addr #2
+
Index: lib/IR/ConstantFold.cpp
===================================================================
--- lib/IR/ConstantFold.cpp
+++ lib/IR/ConstantFold.cpp
@@ -1077,10 +1077,11 @@
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);
+
+ // If alignment isn't specified on functions don't assume it.
+ if (isa<Function>(GV) && GVAlign == 0)
+ return nullptr;
if (GVAlign > 1) {
unsigned DstWidth = CI2->getType()->getBitWidth();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D55115.176077.patch
Type: text/x-patch
Size: 2575 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181130/b9a20eb8/attachment.bin>
More information about the llvm-commits
mailing list