Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6098241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:08:08+00:00 2026-05-23T13:08:08+00:00

I am generating LLVM IR (.ll files) from a source language. This language doesn’t

  • 0

I am generating LLVM IR (.ll files) from a source language. This language doesn’t have any mutable local variables and I don’t use any allocas yet, everything so far is in LLVM registers. It does have immutable local values, though. Currently, they work fine unless the initializer part is a constant or another identifier. For example:

def fun(a: Int, b: Int) = {
  val n = a + b
  n + 2
}

This compiles fine, because a + b compiles to the instruction add i32 %a, %b and instructions can be optionally assigned to local values, so the line becomes: %n = add i32 %a, %b.

On the other hand, I have trouble generating code for the following:

def fun() = {
  val n = 1
  n
}

I could generate %n = bitcast i32 1 to i32 but bitcast doesn’t work with all types and is not really intended for this. Well, I guess in LLVM there is really nothing specifically intended for this, otherwise I wouldn’t have the question.

But is there a good solution without generating tons of different no-op instructions depending on the type of the value? bitcast will not work with tuples for example:

error: invalid cast opcode for cast from '{ i32, i32 }' to '{ i32, i32 }'
%n = bitcast {i32, i32} {i32 1, i32 2} to {i32, i32}

Then again, maybe because there are no ‘copy’ instructions in the IR, I shouldn’t be trying to do this and should be replacing %n with the value everywhere it is used?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:08:08+00:00Added an answer on May 23, 2026 at 1:08 pm

    You have two possibilities:

    1. Generate the code using alloca’s, load and stores (check e.g. clang’s or llvm-gcc’s output at -O0) and then use -mem2reg optimization pass to raise all this stuff to LLVM registers
    2. Use 1 instead of %n everywhere.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Auto generating entities from a legacy database. Many of the tables have non standard
While generating charts using RS 2008 betas, RC0 and such., I never specified any
I'm generating ICalendar (.ics) files. Using the UID and SEQUENCE fields I can update
I'm generating some xml files that needs to conform to an xsd file that
I'm currently generating SQL insert statements from more than one tables, and in the
I am writing a LLVM code generator for the language Timber, the current compiler
Generating normal columnar data in excel file is quite easy but does any one
I'm generating images from an original and storing them in a table. I want
I seem to be generating a lot of questions with this one little iPhone
I have php generating html and I want to create layers. Basically I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.