Skip to content

Commit

Permalink
Merge pull request #575 from hvdijk/update-types-in-constants
Browse files Browse the repository at this point in the history
[AlignModuleStructsPass] Update types in constants
  • Loading branch information
hvdijk authored Oct 24, 2024
2 parents 6c02bae + 745fc40 commit 18e5871
Showing 1 changed file with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,30 @@ Type *getNewType(Value *v, const StructReplacementMap &typeMap) {
return getNewType(v->getType(), typeMap);
}

bool replaceConstantsForRemapping(Constant *C,
const StructReplacementMap &typeMap,
const ValueToValueMapTy &valMap) {
if (valMap.find(C) != valMap.end()) {
return false;
}
if (getNewType(C->getType(), typeMap)) {
compiler::utils::replaceConstantExpressionWithInstruction(C);
return true;
}
if (auto *GEP = dyn_cast<GEPOperator>(C)) {
if (getNewType(GEP->getSourceElementType(), typeMap)) {
compiler::utils::replaceConstantExpressionWithInstruction(C);
return true;
}
}
for (auto *Op : C->operand_values()) {
if (replaceConstantsForRemapping(cast<Constant>(Op), typeMap, valMap)) {
return true;
}
}
return false;
}

/// @brief Performs the update on the LLVM module to replace all the Values
/// using the old struct type with Values using our padded variant.
///
Expand All @@ -378,6 +402,19 @@ void replaceModuleTypes(const StructReplacementMap &typeMap, Module &module) {
replaceGlobalVariable(global, typeMap, valMap);
}

// Replace constants if they are using types that need to be remapped.
for (auto &func : module.functions()) {
for (BasicBlock &block : func) {
for (Instruction &inst : block) {
for (auto *op : inst.operand_values()) {
if (auto *c = dyn_cast<Constant>(op)) {
replaceConstantsForRemapping(c, typeMap, valMap);
}
}
}
}
}

// Identify all functions which use a struct type and need to be cloned,
// this is to avoid unnecessary work in the clone.
SmallVector<Function *, 4> funcs;
Expand Down

0 comments on commit 18e5871

Please sign in to comment.