Skip to content

[CIR][NFCI] Update RValue class to reflect changes in classic CodeGen #142779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 70 additions & 15 deletions clang/lib/CIR/CodeGen/CIRGenValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
#include "clang/AST/CharUnits.h"
#include "clang/AST/Type.h"

#include "llvm/ADT/PointerIntPair.h"

#include "mlir/IR/Value.h"

#include "clang/CIR/MissingFeatures.h"
Expand All @@ -34,28 +32,85 @@ namespace clang::CIRGen {
class RValue {
enum Flavor { Scalar, Complex, Aggregate };

// Stores first value and flavor.
llvm::PointerIntPair<mlir::Value, 2, Flavor> v1;
// Stores second value and volatility.
llvm::PointerIntPair<llvm::PointerUnion<mlir::Value, int *>, 1, bool> v2;
// Stores element type for aggregate values.
mlir::Type elementType;
union {
// Stores first and second value.
struct {
mlir::Value first;
mlir::Value second;
} vals;

// Stores aggregate address.
Address aggregateAddr;
};

unsigned isVolatile : 1;
unsigned flavor : 2;

public:
bool isScalar() const { return v1.getInt() == Scalar; }
bool isAggregate() const { return v1.getInt() == Aggregate; }
RValue() : vals{nullptr, nullptr}, flavor(Scalar) {}

bool isScalar() const { return flavor == Scalar; }
bool isComplex() const { return flavor == Complex; }
bool isAggregate() const { return flavor == Aggregate; }

bool isVolatileQualified() const { return isVolatile; }

/// Return the mlir::Value of this scalar value.
/// Return the value of this scalar value.
mlir::Value getScalarVal() const {
assert(isScalar() && "Not a scalar!");
return v1.getPointer();
return vals.first;
}

/// Return the real/imag components of this complex value.
std::pair<mlir::Value, mlir::Value> getComplexVal() const {
return std::make_pair(vals.first, vals.second);
}

/// Return the value of the address of the aggregate.
Address getAggregateAddress() const {
assert(isAggregate() && "Not an aggregate!");
return aggregateAddr;
}

mlir::Value getAggregatePointer(QualType pointeeType) const {
return getAggregateAddress().getPointer();
}

static RValue getIgnored() {
// FIXME: should we make this a more explicit state?
return get(nullptr);
}

static RValue get(mlir::Value v) {
RValue er;
er.v1.setPointer(v);
er.v1.setInt(Scalar);
er.v2.setInt(false);
er.vals.first = v;
er.flavor = Scalar;
er.isVolatile = false;
return er;
}

static RValue getComplex(mlir::Value v1, mlir::Value v2) {
RValue er;
er.vals = {v1, v2};
er.flavor = Complex;
er.isVolatile = false;
return er;
}
static RValue getComplex(const std::pair<mlir::Value, mlir::Value> &c) {
return getComplex(c.first, c.second);
}
// FIXME: Aggregate rvalues need to retain information about whether they are
// volatile or not. Remove default to find all places that probably get this
// wrong.

/// Convert an Address to an RValue. If the Address is not
/// signed, create an RValue using the unsigned address. Otherwise, resign the
/// address using the provided type.
static RValue getAggregate(Address addr, bool isVolatile = false) {
RValue er;
er.aggregateAddr = addr;
er.flavor = Aggregate;
er.isVolatile = isVolatile;
return er;
}
};
Expand Down
Loading