讓我們從GetElementPtrInst的文檔開始,因爲IRBuilder正在爲其構造函數提供包裝。如果我們想要添加這條指令,我通常會直接去創建。
GetElementPtrInst::Create(ptr, IdxList, name, insertpoint)
- PTR:這是一個值*傳遞到GetElementPtr(GEP)初始PTR值。在你的情況下,%arr。
- IdxList:這是一個值列表,它是傳遞給GEP的偏移序列。你的例子有0和%一些值。
- 姓名:這是IR的名稱。如果你想要「%arrayidx」,你可以提供「arrayidx」。
- 插入點:如果沒有IRBuilder,則必須指定插入指令的位置(在另一條指令之前或在基本塊的末尾)。
把這些碎片拼湊起來,我們有下面的代碼序列:
Value* arr = ...; // This is the instruction producing %arr
Value* someValue = ...; // This is the instruction producing %some value
// We need an array of index values
// Note - we need a type for constants, so use someValue's type
Value* indexList[2] = {ConstantInt::get(someValue->getType(), 0), someValue};
GetElementPtrInst* gepInst = GetElementPtrInst::Create(arr, ArrayRef<Value*>(indexList, 2), "arrayIdx", <some location to insert>);
現在,你問有關使用IRBuilder,其中有一個非常類似的function:
IRBuilder::CreateGEP(ptr, idxList, name)
如果你想要使用IRBuilder,那麼可以用類似的IRBuilder的調用替換代碼片段的最後一行。
謝謝Brian。真的很好的解釋。 – techcomp 2014-11-07 10:13:20