[LLVMdev] Telling the optimizer a value is always null at the start

Carlo Kok ck at remobjects.com
Thu Jul 10 06:06:57 PDT 2014


How do I tell the optimizer that the (dereferenced) value of an i8**
parameter is NULL at the start so that it can eliminate the check?

I have code like:

       void test2(void** ex) {
         printf("go\n"); // does not change *ex
       }

       void call2(void** ex);

       void testeh(void** ex) {
         // I want to tell the optimizer *ex is null so it can eliminate the
first if below if test2 is inlined:
         test2(ex)
         if (*ex) return;
         printf("done")
       }

I tried with llvm.invariant.start/end but that doesn't seem to make any
difference, the icmp/br seems to stay. Can this be done with the current
optimizers?

My usecase is exception handling via an out parameter. The contract is  
that on entry ex always points to a stack position which value holds null.


; ModuleID = 'test.ll'
target datalayout = "e-m:w-p:32:32-i64:64-f80:32-n8:16:32-S32"
target triple = "i686-pc-windows-msvc"

@str1 = linkonce_odr unnamed_addr constant [3 x i8] c"go\00", align 1
@str2 = linkonce_odr unnamed_addr constant [5 x i8] c"done\00", align 1

define void @inlineeh(i8** nocapture readnone %ex) {
     %1 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([3 x
i8]* @str1, i32 0, i32 0))
     ret void
}

declare i32 @printf(i8* nocapture readonly, ...) #0

define void @testeh(i8** nocapture %ex) #0 {
     %1 = bitcast i8** %ex to i8*
     %2 = tail call {}* @llvm.invariant.start(i64 4, i8* %1)
     tail call void @llvm.invariant.end({}* %2, i64 4, i8* %1)
     %3 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([3 x
i8]* @str1, i32 0, i32 0))
     %4 = load i8** %ex, align 4
     %5 = icmp eq i8* %4, null
     br i1 %5, label %6, label %8

; <label>:6                                       ; preds = %0
     %7 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([5 x
i8]* @str2, i32 0, i32 0))
     br label %8

; <label>:8                                       ; preds = %0, %6
     ret void
}

declare void @llvm.invariant.end({}*, i64, i8* nocapture)
declare {}* @llvm.invariant.start(i64, i8* nocapture)




-- 
Carlo Kok
RemObjects Software



More information about the llvm-dev mailing list