LowerTypeTests: Generate simpler IR for br(llvm.type.test, then, else).

This makes it so that the code quality for CFI checks when compiling
with -O2 and linking with --lto-O0 is similar to that of the rest of
the code.

Reduces the size of a chrome binary built with -O2/--lto-O0 by
about 750KB.

Differential Revision: https://reviews.llvm.org/D33925

llvm-svn: 304921
This commit is contained in:
Peter Collingbourne 2017-06-07 15:49:14 +00:00
parent cf531ca50c
commit aaae7eed5c
2 changed files with 56 additions and 2 deletions

View File

@ -608,8 +608,25 @@ Value *LowerTypeTestsModule::lowerTypeTestCall(Metadata *TypeId, CallInst *CI,
if (TIL.TheKind == TypeTestResolution::AllOnes) if (TIL.TheKind == TypeTestResolution::AllOnes)
return OffsetInRange; return OffsetInRange;
TerminatorInst *Term = SplitBlockAndInsertIfThen(OffsetInRange, CI, false); // See if the intrinsic is used in the following common pattern:
IRBuilder<> ThenB(Term); // br(llvm.type.test(...), thenbb, elsebb)
// where nothing happens between the type test and the br.
// If so, create slightly simpler IR.
if (CI->hasOneUse())
if (auto *Br = dyn_cast<BranchInst>(*CI->user_begin()))
if (CI->getNextNode() == Br) {
BasicBlock *Then = InitialBB->splitBasicBlock(CI->getIterator());
BasicBlock *Else = Br->getSuccessor(1);
BranchInst *NewBr = BranchInst::Create(Then, Else, OffsetInRange);
NewBr->setMetadata(LLVMContext::MD_prof,
Br->getMetadata(LLVMContext::MD_prof));
ReplaceInstWithInst(InitialBB->getTerminator(), NewBr);
IRBuilder<> ThenB(CI);
return createBitSetTest(ThenB, TIL, BitOffset);
}
IRBuilder<> ThenB(SplitBlockAndInsertIfThen(OffsetInRange, CI, false));
// Now that we know that the offset is in range and aligned, load the // Now that we know that the offset is in range and aligned, load the
// appropriate bit from the bitset. // appropriate bit from the bitset.

View File

@ -0,0 +1,37 @@
; RUN: opt -S -lowertypetests -lowertypetests-summary-action=import -lowertypetests-read-summary=%S/Inputs/import.yaml < %s | FileCheck %s
target datalayout = "e-p:64:64"
declare i1 @llvm.type.test(i8* %ptr, metadata %bitset) nounwind readnone
; CHECK: define i1 @bytearray7(i8* [[p:%.*]])
define i1 @bytearray7(i8* %p) {
; CHECK-NEXT: [[pi:%.*]] = ptrtoint i8* [[p]] to i64
; CHECK-NEXT: [[sub:%.*]] = sub i64 [[pi]], ptrtoint (i8* @__typeid_bytearray7_global_addr to i64)
; CHECK-NEXT: [[lshr:%.*]] = lshr i64 [[sub]], zext (i8 ptrtoint (i8* @__typeid_bytearray7_align to i8) to i64)
; CHECK-NEXT: [[shl:%.*]] = shl i64 [[sub]], zext (i8 sub (i8 64, i8 ptrtoint (i8* @__typeid_bytearray7_align to i8)) to i64)
; CHECK-NEXT: [[or:%.*]] = or i64 [[lshr]], [[shl]]
; CHECK-NEXT: [[ule:%.*]] = icmp ule i64 [[or]], ptrtoint (i8* @__typeid_bytearray7_size_m1 to i64)
; CHECK-NEXT: br i1 [[ule]], label %[[t1:.*]], label %[[f:.*]]
; CHECK: [[t1]]:
; CHECK-NEXT: [[gep:%.*]] = getelementptr i8, i8* @__typeid_bytearray7_byte_array, i64 [[or]]
; CHECK-NEXT: [[load:%.*]] = load i8, i8* [[gep]]
; CHECK-NEXT: [[and:%.*]] = and i8 [[load]], ptrtoint (i8* @__typeid_bytearray7_bit_mask to i8)
; CHECK-NEXT: [[ne:%.*]] = icmp ne i8 [[and]], 0
; CHECK-NEXT: br i1 [[ne]], label %[[t:.*]], label %[[f:.*]]
; CHECK: [[t]]:
; CHECK-NEXT: ret i1 true
; CHECK: [[f]]:
; CHECK-NEXT: ret i1 false
%x = call i1 @llvm.type.test(i8* %p, metadata !"bytearray7")
br i1 %x, label %t, label %f
t:
ret i1 true
f:
ret i1 false
}