From: Peter Collingbourne Date: Wed, 7 Jun 2017 15:49:14 +0000 (+0000) Subject: LowerTypeTests: Generate simpler IR for br(llvm.type.test, then, else). X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=619317ee76fc0870d47da550288870a2b146595e;p=llvm 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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@304921 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/IPO/LowerTypeTests.cpp b/lib/Transforms/IPO/LowerTypeTests.cpp index 7bec50d9d25..15a13b8155f 100644 --- a/lib/Transforms/IPO/LowerTypeTests.cpp +++ b/lib/Transforms/IPO/LowerTypeTests.cpp @@ -608,8 +608,25 @@ Value *LowerTypeTestsModule::lowerTypeTestCall(Metadata *TypeId, CallInst *CI, if (TIL.TheKind == TypeTestResolution::AllOnes) return OffsetInRange; - TerminatorInst *Term = SplitBlockAndInsertIfThen(OffsetInRange, CI, false); - IRBuilder<> ThenB(Term); + // See if the intrinsic is used in the following common pattern: + // 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(*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 // appropriate bit from the bitset. diff --git a/test/Transforms/LowerTypeTests/simplify.ll b/test/Transforms/LowerTypeTests/simplify.ll new file mode 100644 index 00000000000..cb5ad4a10bf --- /dev/null +++ b/test/Transforms/LowerTypeTests/simplify.ll @@ -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 +}