Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 43f41cc

Browse files
committed
Merging r195118:
------------------------------------------------------------------------ r195118 | chandlerc | 2013-11-19 01:03:18 -0800 (Tue, 19 Nov 2013) | 22 lines Fix an issue where SROA computed different results based on the relative order of slices of the alloca which have exactly the same size and other properties. This was found by a perniciously unstable sort implementation used to flush out buggy uses of the algorithm. The fundamental idea is that findCommonType should return the best common type it can find across all of the slices in the range. There were two bugs here previously: 1) We would accept an integer type smaller than a byte-width multiple, and if there were different bit-width integer types, we would accept the first one. This caused an actual failure in the testcase updated here when the sort order changed. 2) If we found a bad combination of types or a non-load, non-store use before an integer typed load or store we would bail, but if we found the integere typed load or store, we would use it. The correct behavior is to always use an integer typed operation which covers the partition if one exists. While a clever debugging sort algorithm found problem #1 in our existing test cases, I have no useful test case ideas for #2. I spotted in by inspection when looking at this code. ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_34@195217 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent d20d4e5 commit 43f41cc

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

lib/Transforms/Scalar/SROA.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,7 @@ static Type *findCommonType(AllocaSlices::const_iterator B,
938938
AllocaSlices::const_iterator E,
939939
uint64_t EndOffset) {
940940
Type *Ty = 0;
941+
bool IgnoreNonIntegralTypes = false;
941942
for (AllocaSlices::const_iterator I = B; I != E; ++I) {
942943
Use *U = I->getUse();
943944
if (isa<IntrinsicInst>(*U->getUser()))
@@ -946,29 +947,37 @@ static Type *findCommonType(AllocaSlices::const_iterator B,
946947
continue;
947948

948949
Type *UserTy = 0;
949-
if (LoadInst *LI = dyn_cast<LoadInst>(U->getUser()))
950+
if (LoadInst *LI = dyn_cast<LoadInst>(U->getUser())) {
950951
UserTy = LI->getType();
951-
else if (StoreInst *SI = dyn_cast<StoreInst>(U->getUser()))
952+
} else if (StoreInst *SI = dyn_cast<StoreInst>(U->getUser())) {
952953
UserTy = SI->getValueOperand()->getType();
953-
else
954-
return 0; // Bail if we have weird uses.
954+
} else {
955+
IgnoreNonIntegralTypes = true; // Give up on anything but an iN type.
956+
continue;
957+
}
955958

956959
if (IntegerType *ITy = dyn_cast<IntegerType>(UserTy)) {
957960
// If the type is larger than the partition, skip it. We only encounter
958961
// this for split integer operations where we want to use the type of the
959-
// entity causing the split.
960-
if (ITy->getBitWidth() / 8 > (EndOffset - B->beginOffset()))
962+
// entity causing the split. Also skip if the type is not a byte width
963+
// multiple.
964+
if (ITy->getBitWidth() % 8 != 0 ||
965+
ITy->getBitWidth() / 8 > (EndOffset - B->beginOffset()))
961966
continue;
962967

963968
// If we have found an integer type use covering the alloca, use that
964-
// regardless of the other types, as integers are often used for a
965-
// "bucket
966-
// of bits" type.
969+
// regardless of the other types, as integers are often used for
970+
// a "bucket of bits" type.
971+
//
972+
// NB: This *must* be the only return from inside the loop so that the
973+
// order of slices doesn't impact the computed type.
967974
return ITy;
975+
} else if (IgnoreNonIntegralTypes) {
976+
continue;
968977
}
969978

970979
if (Ty && Ty != UserTy)
971-
return 0;
980+
IgnoreNonIntegralTypes = true; // Give up on anything but an iN type.
972981

973982
Ty = UserTy;
974983
}

test/Transforms/SROA/basictest.ll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,6 @@ entry:
11811181
store i1 %x, i1* %b.i1, align 8
11821182
%b.i8 = bitcast <{ i1 }>* %b to i8*
11831183
%foo = load i8* %b.i8, align 1
1184-
; CHECK-NEXT: {{.*}} = zext i1 %x to i8
11851184
; CHECK-NEXT: %[[ext:.*]] = zext i1 %x to i8
11861185
; CHECK-NEXT: store i8 %[[ext]], i8* %[[a]], align 8
11871186
; CHECK-NEXT: {{.*}} = load i8* %[[a]], align 8

0 commit comments

Comments
 (0)