2016-10-07 88 views
1

我想檢查數組的堆棧分配是否具有恆定大小或動態大小(在運行時計算)。例如LLVM檢查數組分配是否具有動態大小或常量大小

int myInt; 
scanf("%d", &myInt); 
int buffer[myInt]; //dynamic sized array 

動態大小的數組被轉換成LLVM IR這樣的:

%myInt = alloca i32, align 4 
%saved_stack = alloca i8* 
%call = call i32 (i8*, ...) @__isoc99_scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0), i32* %myInt) 
%0 = load i32, i32* %myInt, align 4 
%1 = zext i32 %0 to i64 
%2 = call i8* @llvm.stacksave() 
store i8* %2, i8** %saved_stack 
%vla = alloca i32, i64 %1, align 16 //allocation 
%3 = load i8*, i8** %saved_stack 
call void @llvm.stackrestore(i8* %3) 

的恆定大小的數組:

int buffer2[123]; 

LLVM IR:

%buffer2 = alloca [123 x i32], align 16 

我怎樣才能確定一個alloca我nstruction分配一個動態大小的數組或一個常數大小的數組?

回答

1

請看「include/llvm/IR/Instructions.h」中的class AllocaInst。它包含一旦你的Value *對數組的大小返回分配的數組

/// Get the number of elements allocated. For a simple allocation of a single 
    /// element, this will return a constant 1 value. 
    const Value *getArraySize() const { return getOperand(0); } 

的大小的方法,你應該能夠分析,如果這是一個常數與否,使用dyn_cast<ConstantInt>。 (這個表達式是grep,它在代碼中被廣泛使用)。