Skip to content

Commit 8b354cc

Browse files
committed
[ConstantFolding] check applicability of AllOnes constant creation first
The getAllOnesValue can only handle things that are bitcast from a ConstantInt, while here we bitcast through a pointer, so we may see more complex objects (like Array or Struct). Differential Revision: https://reviews.llvm.org/D83870
1 parent 6187eeb commit 8b354cc

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,12 @@ Constant *llvm::ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy,
342342
// pointers legally).
343343
if (C->isNullValue() && !DestTy->isX86_MMXTy())
344344
return Constant::getNullValue(DestTy);
345-
if (C->isAllOnesValue() && !DestTy->isX86_MMXTy() &&
346-
!DestTy->isPtrOrPtrVectorTy()) // Don't get ones for ptr types!
345+
if (C->isAllOnesValue() &&
346+
(DestTy->isIntegerTy() || DestTy->isFloatingPointTy() ||
347+
DestTy->isVectorTy()) &&
348+
!DestTy->isX86_MMXTy() && !DestTy->isPtrOrPtrVectorTy())
349+
// Get ones when the input is trivial, but
350+
// only for supported types inside getAllOnesValue.
347351
return Constant::getAllOnesValue(DestTy);
348352

349353
// If the type sizes are the same and a cast is legal, just directly
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
; RUN: opt -early-cse -S -o - %s | FileCheck %s
2+
target datalayout = "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64-ni:2"
3+
target triple = "armv7-unknown-linux-gnueabi"
4+
5+
%struct.anon = type { i32 }
6+
7+
@onesstruct = private constant %struct.anon { i32 -1 }, align 4
8+
9+
define i32 @allones_struct() {
10+
; CHECK-LABEL: @allones_struct()
11+
; CHECK-NEXT: %1 = load [1 x i32], [1 x i32]* bitcast (%struct.anon* @onesstruct to [1 x i32]*), align 4
12+
; CHECK-NEXT: %2 = extractvalue [1 x i32] %1, 0
13+
; CHECK-NEXT: ret i32 %2
14+
%1 = load [1 x i32], [1 x i32]* bitcast (%struct.anon* @onesstruct to [1 x i32]*), align 4
15+
%2 = extractvalue [1 x i32] %1, 0
16+
ret i32 %2
17+
}
18+
19+
define i32 @allones_int() {
20+
; CHECK-LABEL: @allones_int()
21+
; CHECK-NEXT: ret i32 -1
22+
%1 = load i32, i32* bitcast (%struct.anon* @onesstruct to i32*), align 4
23+
ret i32 %1
24+
}
25+
26+
define i32* @allones_ptr() {
27+
; CHECK-LABEL: @allones_ptr()
28+
; CHECK-NEXT: ret i32* inttoptr (i32 -1 to i32*)
29+
%1 = load i32*, i32** bitcast (%struct.anon* @onesstruct to i32**), align 4
30+
ret i32* %1
31+
}
32+
33+
define i32 addrspace(1)* @allones_ptr1() {
34+
; CHECK-LABEL: @allones_ptr1()
35+
; CHECK-NEXT: ret i32 addrspace(1)* inttoptr (i32 -1 to i32 addrspace(1)*)
36+
%1 = load i32 addrspace(1)*, i32 addrspace(1)** bitcast (%struct.anon* @onesstruct to i32 addrspace(1)**), align 4
37+
ret i32 addrspace(1)* %1
38+
}
39+
40+
define i32 addrspace(2)* @allones_ptr2() {
41+
; CHECK-LABEL: @allones_ptr2()
42+
; CHECK-NEXT: %1 = load i32 addrspace(2)*, i32 addrspace(2)** bitcast (%struct.anon* @onesstruct to i32 addrspace(2)**), align 4
43+
; CHECK-NEXT: ret i32 addrspace(2)* %1
44+
%1 = load i32 addrspace(2)*, i32 addrspace(2)** bitcast (%struct.anon* @onesstruct to i32 addrspace(2)**), align 4
45+
ret i32 addrspace(2)* %1
46+
}

0 commit comments

Comments
 (0)