llvm如何在联合中分配和获取元素?

时间:2018-02-14 04:06:53

标签: llvm llvm-ir

我有以下C代码和函数caller()的IR。任何人都可以解释为什么它为[2 x %struct.C]分配karg1?或者更一般地说,LLVM如何优化联合?我对%karg1.sub的GEP说明感到困惑。

int cmd = 1;
struct T
{
    int tuner;
    char name[32];
    unsigned long rangelow, rangehigh;  
    unsigned int flags;
    unsigned short mode;            
    unsigned short signal;          
};

struct C
{
    char    loadwhat[16];   
    int datasize;
    unsigned char   *data;
};

void f(struct C *kp){
    unsigned char *ud = kp->data;
    if(ud){
    }
}

void caller(){
    union{
        struct T vt;
        struct C vc;
    }karg;
    switch(cmd){
        case 1:
            f(&karg.vc);
    }
}

产生的LLVM IR

; Function Attrs: nounwind uwtable
define void @caller() #0 {
/*why it allocate 2 x %struct.C*/
  %karg1 = alloca [2 x %struct.C], align 8 
  %1 = load i32, i32* @cmd, align 4
  switch i32 %1, label %3 [
    i32 1, label %2
  ]

; <label>:2                                       ; preds = %0
/*How to understand this gep instruction*/
  %karg1.sub = getelementptr inbounds [2 x %struct.C], [2 x %struct.C]* %karg1, i64 0, i64 0
  call void @f(%struct.C* %karg1.sub)
  br label %3

; <label>:3                                       ; preds = %2, %0
  ret void
}

0 个答案:

没有答案