[Attributor][FIX] Do not apply h2s for arbitrary mallocs

H2S did apply to mallocs of non-constant sizes if the uses were OK. This
is now forbidden through reording of the "good" and "bad" cases in the
conditional.

llvm-svn: 374698
This commit is contained in:
Johannes Doerfert 2019-10-13 03:54:08 +00:00
parent 9daf51910b
commit d20f80780e
2 changed files with 30 additions and 19 deletions

View File

@ -3620,30 +3620,36 @@ ChangeStatus AAHeapToStackImpl::updateImpl(Attributor &A) {
};
auto MallocCallocCheck = [&](Instruction &I) {
if (isMallocLikeFn(&I, TLI)) {
if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(0)))
if (!Size->getValue().sle(MaxHeapToStackSize))
return true;
} else if (isCallocLikeFn(&I, TLI)) {
bool Overflow = false;
if (auto *Num = dyn_cast<ConstantInt>(I.getOperand(0)))
if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(1)))
if (!(Size->getValue().umul_ov(Num->getValue(), Overflow))
.sle(MaxHeapToStackSize))
if (!Overflow)
return true;
} else {
if (BadMallocCalls.count(&I))
return true;
bool IsMalloc = isMallocLikeFn(&I, TLI);
bool IsCalloc = !IsMalloc && isCallocLikeFn(&I, TLI);
if (!IsMalloc && !IsCalloc) {
BadMallocCalls.insert(&I);
return true;
}
if (BadMallocCalls.count(&I))
return true;
if (IsMalloc) {
if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(0)))
if (Size->getValue().sle(MaxHeapToStackSize))
if (UsesCheck(I)) {
MallocCalls.insert(&I);
return true;
}
} else if (IsCalloc) {
bool Overflow = false;
if (auto *Num = dyn_cast<ConstantInt>(I.getOperand(0)))
if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(1)))
if ((Size->getValue().umul_ov(Num->getValue(), Overflow))
.sle(MaxHeapToStackSize))
if (!Overflow && UsesCheck(I)) {
MallocCalls.insert(&I);
return true;
}
}
if (UsesCheck(I))
MallocCalls.insert(&I);
else
BadMallocCalls.insert(&I);
BadMallocCalls.insert(&I);
return true;
};

View File

@ -316,3 +316,8 @@ define void @test14() {
; CHECK: tail call void @free(i8* noalias %1)
ret void
}
define void @test15(i64 %S) {
%1 = tail call noalias i8* @malloc(i64 %S)
ret void
}