我有一个简单的C程序。
int
1.main(int argc, char **argv) {
2. unsigned buffer[4] = { 0, 0, 0, 0 };
3. return buffer[argc];
4. }
IR代码如下
; Function Attrs: norecurse nounwind readnone uwtable
define i32 @main(i32 %argc, i8** nocapture readnone %argv) #0 !dbg !6 {
%buffer = alloca [4 x i32], align 16
tail call void @llvm.dbg.value(metadata i32 %argc, i64 0, metadata !14, metadata !21), !dbg !22
tail call void @llvm.dbg.value(metadata i8** %argv, i64 0, metadata !15, metadata !21), !dbg !23
%1 = bitcast [4 x i32]* %buffer to i8*, !dbg !24
call void @llvm.lifetime.start(i64 16, i8* %1) #3, !dbg !24
tail call void @llvm.dbg.declare(metadata [4 x i32]* %buffer, metadata !16, metadata !21), !dbg !25
call void @llvm.memset.p0i8.i64(i8* %1, i8 0, i64 16, i32 16, i1 false), !dbg !26
%2 = sext i32 %argc to i64, !dbg !28
%3 = getelementptr inbounds [4 x i32], [4 x i32]* %buffer, i64 0, i64 %2, !dbg !28
%4 = load i32, i32* %3, align 4, !dbg !28, !tbaa !29
call void @llvm.lifetime.end(i64 16, i8* %1) #3, !dbg !33
ret i32 %4, !dbg !34
}
我想比较第3行的访问索引是否是有效索引。为了进行比较,我需要提取为argc存储的值。下面是我为了获得argc的vvalue而编写的一段代码
auto gep = llvm::dyn_cast<llvm::GetElementPtrInst>(inst);
auto operand2 = gep->getOperand(2);
outs() << "operand 2 "<<*operand2<<"\n";
auto newOperand =operand2.getOperand(0);
outs()<<"New operand "<<*newOperand<<"\n";
输出: -
操作数2%2 = sext i32%argc到i64,!dbg!28
新操作数i32%argc
如何获取%argc的值?
答案 0 :(得分:0)
getOperand
函数的返回值是Value*
变量的argc
对象(代码中的变量newOperand
)。您可以将该值传递给您可能要在IR中注入的任何新指令(例如CreateICmpEQ
),以便将argc
的值与某个常量值进行比较。