LLVM代码生成:具有恒定值的fptoui指令

时间:2014-01-22 12:06:43

标签: llvm

假设我们有代码

%conv = fptoui double -1 to i32

执行%conv后是4294967295.好的。但是当我试图生成这样的代码时,我打电话给

Builder.CreateFPToUI(val, Builder.getInt32Ty())

其中val是ConstantFP,其值为-1,它返回ConstantInt,其值为0而不是4294967295!有人能解释一下原因吗?

UPD: Example.Let说我需要为下一个代码生成IR

a << -1

代码生成就像

一样
Value* one; // for 1 literal
Value* minusOne; // for -1
Value* a; //represents variable a
....
one = llvm::ConstantFP::get(Builder.getDoubleTy(), 1);
....
/* Negation. CreateFSub also doesn't emits instruction but returns ConstantFP with -1 value */
minusOne  = CreateFSub(ConstantFP::getZeroValueForNegation(Builder.getDoubleTy()), one);
....
Value* unsignedOne = Builder.CreateFPToUI(minusOne, Builder.getInt32Ty());
....
Value* shift = Builder.CreateShl(a, unsignedOne, "shl");  

这一切都只是转入

%shl = shl i32 %a, 0

1 个答案:

答案 0 :(得分:0)

来自language reference

  

'fptoui'指令将其浮点操作数转换为最接近的(向零舍入)无符号整数值。 如果该值不适合ty2,则结果未定义

-1不适合任何无符号整数类型...因此您得到未定义的结果。

如果你想知道为什么你会从同一个IR获得两个不同的东西,那么你应该知道它不是相同的IR:IRBuilder::CreateFPToUI调用的结果,正如你所观察到的,是一个值( ConstantInt)而不是指令,因为它是不变的。