Literal for `i1` in llvm - llvm

tell me please if there exist some literal representing true and false in LLVM ? I mean type i1. For example for i32 we have all possible integer literals: 1,2,3.....

For i1 you're naturally having 2 values: 0 and 1.

Related

LLVM - fastest way to count how many bits of an integer are on

I have a register %v1 with type i256.
I want to know how many bits are equal to 1.
For example, if %v1 equals 110....001, I want the result to be 3.
Is there a quick way to achieve this?
LLVM has an intrinsic for this called ctpop (short for count population.)
Syntax:
This is an overloaded intrinsic. You can use llvm.ctpop on any integer
bit width, or on any vector with integer elements. Not all targets
support all bit widths or vector types, however.
declare i8 #llvm.ctpop.i8(i8 <src>)
declare i16 #llvm.ctpop.i16(i16 <src>)
declare i32 #llvm.ctpop.i32(i32 <src>)
declare i64 #llvm.ctpop.i64(i64 <src>)
declare i256 #llvm.ctpop.i256(i256 <src>)
declare <2 x i32> #llvm.ctlz.v2i32(<2 x i32> <src>, i1 <is_zero_undef>)
Overview:
The ‘llvm.ctpop’ family of intrinsics counts the number of bits set in a value.
Arguments:
The only argument is the value to be counted. The argument may be of any integer type,
or a vector with integer elements. The return type must match the argument type.
Semantics:
The ‘llvm.ctpop’ intrinsic counts the 1’s in a variable, or within each element of a
vector.
I believe something like the following should work.
declare i256 #llvm.ctpop.i256(i256)
%popcount = call i256 #llvm.ctpop.i256(i256 %v1)

Converting i1 type to integer value

For the following branch instruction
br i1 %cmp, label %if.then, label %if.end, !dbg !35
Since llvm is SSA, I can directly access the operand 0, to determine whether the comparison is true or not. The type evaluates to i1 but I am having trouble extracting the value (true or false)
BranchInst &I;
Value *val = I.getOperand(0);
Type yields to i1 type but when I tried to cast to
ConstantInt *cint = dyn_cast<ConstantInt>(val) the casting does not seem to work? how do I go about it
Answering my own question
BranchInst &I;
Module* module;
IRBuilder<> irbuilder(&I);
Value* value = irbuilder.CreateIntCast(I.getCondition(),
Type::getInt32Ty(module->getContext()), false);
This should convert i1 to i32.
You want a different kind of cast — cast<> casts within your code, while what you want is a cast within the generated code, CastInst. Boolean is a one-bit integer type, so what you want probably is a zero extension. CastInst::Create(CastInst::ZExt, I.getOperand(0), … should do.

What's the difference between `ConstantInt::getTrue (LLVMContext &Context)` and `ConstantInt::getTrue (Type *Ty)`?

I found 2 APIs in llvm::ConstantInt document:
static ConstantInt *ConstantInt::getTrue (LLVMContext &Context): https://llvm.org/doxygen/classllvm_1_1ConstantInt.html#a82dbbd8e3688b0bc1eedb338864d0d0c
static Constant *ConstantInt::getTrue (Type *Ty): https://llvm.org/doxygen/classllvm_1_1ConstantInt.html#a1d728e83e9e0fa85b0b58b33ec9c3197
But I don't know what's the difference between these 2 APIs ?
The former constructs a ConstantInt which has i1 type, while the latter allows you to specify the type yourself. This might be useful to construct i8 0, i16 1 and like that.

Why use two '!' operators on a boolean value? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Double Negation in C++ code
When I scanned the Webkit source code, I found a strange use of the boolean "not" operator !:
BOOL enabled;
if (SUCCEEDED(sharedPreferences->continuousSpellCheckingEnabled(&enabled)))
continuousSpellCheckingEnabled = !!enabled;
if (SUCCEEDED(sharedPreferences->grammarCheckingEnabled(&enabled)))
grammarCheckingEnabled = !!enabled;
Why don't they use enabled directly instead of !!enabled?
It's a C++ trick to convert anything to 1 or 0.
For example, 42 is logically true, but is not 1, so applying !! to it, you turn it into 1.
It's meant to force a value to a boolean. So if the value is evaluating to something, you'll get true.
It forces a true value to be exactly 1.
In a boolean context (if (x), while (x), x ? a : b, for (; x; )), a value which is 0 means false, while any other value means true.
If your function accepts a truth value, but you need exactly 1 there, !! is fine.
In other words, !!x is the same as x ? 1 : 0.
Most probably continuousSpellCheckingEnabled is of type bool. BOOL is defined as int, so:
continuousSpellCheckingEnabled = enabled;
issues a warning, while:
continuousSpellCheckingEnabled = !!enabled;
does not.
why don't they directly use "enabled"
Double negation only cancels out for boolean values (0 and 1) so !!(0) == 0 for non-boolean values the value will be converted to boolean !!(100) == 1

LLVM String Value objects: How can I retrieve the String from a Value?

When building the IR from an existing AST, my AST has some string values (at compile-time they are built from std::string) and I want to set them safely as llvm::Value to use as a part of an expression.
In this case, I don't need to bind the string at run-time, because string values are only meant to resolve stuff as variables, functions or classes at compile-time (the language doesn't support a native string type).
Whats the best way to keep my string content as llvm::Value and still be able to retrieve it at later stages of compilation (when the nesting expressions are built)?
More concretely, if I set the llvm::Value with:
llvm::Value* v = llvm::ConstantArray::get(llvmContext, myString.c_str());
How do I safely retrieve the string value? Is llvm::ConstantArray the appropriate way to wrap strings?
Yes, ConstantArray is what you should use here. In order to retrieve the value later just use ConstantArray::getAsCString(). If you have assertions turned on, it will assert if something will went wrong (e.g. you will try to grab string from the array w/o zero terminator).
Running http://llvm.org/demo/ on the C code char *x = "asdf"; gives:
#.str = private unnamed_addr constant [5 x i8] c"asdf\00"
#x = global i8* getelementptr inbounds ([5 x i8]* #.str, i64 0, i64 0), align 8
Basically, to get the address of a string, you have to build a global containing it. You can switch http://llvm.org/demo/ to output C++ API calls if you have trouble figuring out how to do that.