Skip to content

Commit 1c0c67a

Browse files
author
Hal Finkel
committed
Check for all known bits on ret in InstCombine
From a combination of @llvm.assume calls (and perhaps through other means, such as range metadata), it is possible that all bits of a return value might be known. Previously, InstCombine did not check for this (which is understandable given assumptions of constant propagation), but means that we'd miss simple cases where assumptions are involved. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217346 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 3ef1aae commit 1c0c67a

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

lib/Transforms/InstCombine/InstCombine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner
232232
Instruction *visitStoreInst(StoreInst &SI);
233233
Instruction *visitBranchInst(BranchInst &BI);
234234
Instruction *visitSwitchInst(SwitchInst &SI);
235+
Instruction *visitReturnInst(ReturnInst &RI);
235236
Instruction *visitInsertValueInst(InsertValueInst &IV);
236237
Instruction *visitInsertElementInst(InsertElementInst &IE);
237238
Instruction *visitExtractElementInst(ExtractElementInst &EI);

lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,7 +2004,25 @@ Instruction *InstCombiner::visitFree(CallInst &FI) {
20042004
return nullptr;
20052005
}
20062006

2007+
Instruction *InstCombiner::visitReturnInst(ReturnInst &RI) {
2008+
if (RI.getNumOperands() == 0) // ret void
2009+
return nullptr;
2010+
2011+
Value *ResultOp = RI.getOperand(0);
2012+
Type *VTy = ResultOp->getType();
2013+
if (!VTy->isIntegerTy())
2014+
return nullptr;
20072015

2016+
// There might be assume intrinsics dominating this return that completely
2017+
// determine the value. If so, constant fold it.
2018+
unsigned BitWidth = VTy->getPrimitiveSizeInBits();
2019+
APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
2020+
computeKnownBits(ResultOp, KnownZero, KnownOne, 0, &RI);
2021+
if ((KnownZero|KnownOne).isAllOnesValue())
2022+
RI.setOperand(0, Constant::getIntegerValue(VTy, KnownOne));
2023+
2024+
return nullptr;
2025+
}
20082026

20092027
Instruction *InstCombiner::visitBranchInst(BranchInst &BI) {
20102028
// Change br (not X), label True, label False to: br X, label False, True

test/Transforms/InstCombine/assume.ll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ entry:
4343
; Function Attrs: nounwind
4444
declare void @llvm.assume(i1) #1
4545

46+
define i32 @simple(i32 %a) #1 {
47+
entry:
48+
49+
; CHECK-LABEL: @simple
50+
; CHECK: call void @llvm.assume
51+
; CHECK: ret i32 4
52+
53+
%cmp = icmp eq i32 %a, 4
54+
tail call void @llvm.assume(i1 %cmp)
55+
ret i32 %a
56+
}
57+
4658
; Function Attrs: nounwind uwtable
4759
define i32 @can1(i1 %a, i1 %b, i1 %c) {
4860
entry:

0 commit comments

Comments
 (0)