adding new data types to llvm

Hello .

I would like to add new custom data type to llvm C parser, I use LLVM/Clang version 3.1.
Adding new type instructions from site are out of date (
Could you please provide me with guidance?
Thanks in advance,

Edvard Ghazaryan <> writes:

I would like to add new custom data type to llvm C parser, I use LLVM/Clang
version 3.1.
Adding new type instructions from site are out of date (http://
Could you please provide me with guidance?

Check out include/clang/AST/Type.h in the clang sources. Pick an existing
type with similar semantics to what you want to add (pointer type, scalar
type, etc), and then derive a new class either from Type, or from the type you
want to specialize.

Hello .

I’m trying to implement LoopPass.
Here is simple code :

class LoopParser: public llvm::LoopPass
static char ID;

virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const

virtual bool runOnLoop(llvm::Loop* IncomingLoop,
llvm::LPPassManager& LPM_Ref)
{ return false; }

LoopParser() : llvmLoopPass(ID)

char LoopParser::ID = 0;
static llvm::RegisterPass XX(“LoopParser”, "TODO ", false, false);

when I’am trying to load a get the error message : undefined symbol: _ZTIN4llvm8LoopPassE
How can I fix?

Thanks in advance,

Hi Edvard, _ZTIN4llvm8LoopPassE is "typeinfo for llvm::LoopPass". LLVM is
built without typeinfo, so you will need to build your pass with -fno-rtti.

Ciao, Duncan.

Thanks Duncan.


Hello .

I’m trying to implement FunctionPass for detecting loops in llvm IR.
How can I get for loop from llvm::Loop object.?
Is there any example?

Thanks in advance,

I think the LoopInfo analysis already locates loops. I think one way to do this is to use the PostDominator pass to compute control-dependences for the loop and then find which basic block(s) are dependences for the loop entry basic block. The condition variable should be an argument to the terminator instruction of the basic block on which the loop entry block depends. – John T.

Hello .

I ran opt with -indvars options , got wrong result.
This is my example: :
int test(int a)
for (int i = 2; i < a; ++i) {
a += ;
return a;
clang -O3 -emit-llvm -S -o test.ll
opt -indvards -S test.ll -o indvars.ll > /dev/null

indvars.ll :

; ModuleID = ‘test.ll’
target datalayout = “e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32-S128”
target triple = “i386-pc-linux-gnu”

define i32 @_Z4testi(i32 %a) nounwind readnone {
%cmp1 = icmp sgt i32 %a, 2
br i1 %cmp1, label %for.body.preheader, label %for.end

for.body.preheader: ; preds = %entry
br label %for.body

for.body: ; preds = %for.body.preheader, %for.body
%a.addr.03 = phi i32 [ %add, %for.body ], [ %a, %for.body.preheader ] //should start at 0 , I think there is no Canonicalize Induction Variable
%i.02 = phi i32 [ %inc, %for.body ], [ 2, %for.body.preheader ]
%add = add nsw i32 %a.addr.03, %i.02
%inc = add nsw i32 %i.02, 1
%cmp = icmp slt i32 %inc, %add
br i1 %cmp, label %for.body, label %for.end.loopexit

for.end.loopexit: ; preds = %for.body
%add.lcssa = phi i32 [ %add, %for.body ]
br label %for.end

for.end: ; preds = %for.end.loopexit, %entry
%a.addr.0.lcssa = phi i32 [ %a, %entry ], [ %add.lcssa, %for.end.loopexit ]
ret i32 %a.addr.0.lcssa

Someone can explain why?

Thanks in advance,

Hello .

opt -indvars pass does not generate canonical induction variable.

(NULL == loop->getCanonicalInductionVariable():wink:

PLEASE explain why? and how can I fix it?

Thanks in advance,

This may not solve your problem, but I've found that (e.g. for
unrolling) the loop rotate pass often needs to be run.

I’m not sure I understand the question. Here are some tips.

Loop::getLoopLatch() give you a canonical “latch” block if it exists. BasicBlock::getTerminator() gives you the branch. This is done throughout the code, so you can see the pattern.

Loop::getExitingBlocks() gives you all the branches that may leave the loop.

Or may want to do your own search for the “last” exit by following block predecessors from the loop header.


-indvars runs the “Induction Variable Simplification” pass. It determines whether any induction variable users can be simplified. If not, then it doesn’t rewrite the loop. We used to have an option for rewriting induction variables, but that was removed in 3.1 because it was untested.

Loop::getCanonicalInductionVariable() is also untested and should be removed. There is still some SCEVExpander code that uses it because of projects and out-of-tree work. I’d like to figure out a graceful way to remove all of this so that external users can migrate.

The way to “fix” it depends on what you really want. If you want induction variable analysis, then use SCEV. If you want the loop’s induction variables transformed into a more optimal form, that is highly target specific and done in LoopStrengthReduce. If you have some other reason you want to rewrite induction variables, then you and anyone else who’s interested need to maintain your own pass to do that.

Sorry for the confusion.