2009-08-21 21 views
10

我一直在尋找最近由llvm-gcc生成的一些LLVM程序集,我注意到了一個我不確定它的目的的反覆陳述。在llvm代碼中出現%「alloca point」行的目的是什麼?

例如,以下C程序:

int main(void) 
{ 
    void (*f)(void) = (0x21332); 
    f(); 
} 

當與編譯 「LLVM-GCC -emit-LLVM -S」 將產生以下代碼(已刪除不相關部分):

define i32 @main() nounwind { 
entry: 
    %retval = alloca i32  ; <i32*> [#uses=1] 
    %f = alloca void()*  ; <void()**> [#uses=2] 
    %"alloca point" = bitcast i32 0 to i32  ; <i32> [#uses=0] 
    store void()* inttoptr (i64 135986 to void()*), void()** %f, align 4 
    %0 = load void()** %f, align 4  ; <void()*> [#uses=1] 
    call void %0() nounwind 
    br label %return 

我感興趣的是該行的目的:

%"alloca point" = bitcast i32 0 to i32  ; <i32> [#uses=0] 

似乎並沒有做任何事情作爲變量它指定的是不會再次使用,並且bitcast本身是毫無意義的。我所能想到的是,它的插入實際上是爲了後來的代碼生成/分析的目的,指示代碼的有趣部分。

+0

我也是在這條線上好奇;我今天晚上看了一些代碼生成的東西時碰到了它。它似乎是alloca邊界,但我不知道爲什麼。 – Albinofrenchy 2009-08-22 05:06:03

回答

8

來自llvm-gcc源代碼:gcc/llvm-convert.cpp,它只是用作幫助值*,它會被一個死的指令消除通道刪除。

// Create a dummy instruction in the entry block as a marker to insert new 
// alloc instructions before. It doesn't matter what this instruction is, 
// it is dead. This allows us to insert allocas in order without having to 
// scan for an insertion point. Use BitCast for int -> int 
+0

是的,這是正確的。在LLVM郵件列表上也詢問了這個問題,並得到了類似的回覆,具體如下: 「它是作爲一個佔位符來插入臨時對象:alloca在bitcast之前,實際代碼生成在它之後開始。前端; instcombine會消除它,基本上所有的東西都會忽略它。「 – 2009-11-02 06:05:57

-1

在互聯網上發現: 在計算堆棧幀大小時,可以在編譯時確定大小的Allocas將在堆棧上分配空間。對於可變大小的分配,目標特定代碼將不得不調整堆棧大小,根據需要調整幀指針和堆棧指針,並將傳出參數的位置調整到堆棧頂部。

聽起來像它那裏,使一些堆棧空間正確工作。

+0

問題不在於alloca本身。 OP正在詢問名爲「alloca point」的指令,該指令似乎是無操作的。 – 2009-10-23 04:53:58