|
|
@@ -1314,6 +1314,30 @@ namespace {
|
|
|
currentBunkerSliceBlocks.blocks = currentBunkerSlice;
|
|
|
currentBunkerSliceBlocks.phiBlock = currentBunkerPhiBlock;
|
|
|
bunkerSliceBlocksList.push_back(currentBunkerSliceBlocks);
|
|
|
+ // 为最后一个phi块添加返回指令
|
|
|
+ IRBuilder<> PhiBuilder(currentBunkerPhiBlock);
|
|
|
+ if (fusedFunc->getReturnType()->isVoidTy()) {
|
|
|
+ PhiBuilder.CreateRetVoid();
|
|
|
+ } else {
|
|
|
+ // 创建默认返回值
|
|
|
+ Value* defaultRetVal;
|
|
|
+ Type* returnType = fusedFunc->getReturnType();
|
|
|
+
|
|
|
+ if (StructType* STy = dyn_cast<StructType>(returnType)) {
|
|
|
+ // 如果是结构体返回类型,创建一个零初始化的结构体
|
|
|
+ defaultRetVal = ConstantAggregateZero::get(STy);
|
|
|
+ } else if (returnType->isPointerTy()) {
|
|
|
+ defaultRetVal = ConstantPointerNull::get(cast<PointerType>(returnType));
|
|
|
+ } else if (returnType->isIntegerTy()) {
|
|
|
+ defaultRetVal = ConstantInt::get(returnType, 0);
|
|
|
+ } else if (returnType->isFloatingPointTy()) {
|
|
|
+ defaultRetVal = ConstantFP::get(returnType, 0.0);
|
|
|
+ } else {
|
|
|
+ defaultRetVal = UndefValue::get(returnType);
|
|
|
+ }
|
|
|
+
|
|
|
+ PhiBuilder.CreateRet(defaultRetVal);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// // 添加新的日志打印代码
|
|
|
@@ -1391,12 +1415,18 @@ namespace {
|
|
|
if (PHINode* PHI = dyn_cast<PHINode>(&I)) {
|
|
|
for (unsigned i = 0; i < PHI->getNumIncomingValues(); i++) {
|
|
|
BasicBlock* incomingBlock = PHI->getIncomingBlock(i);
|
|
|
+ LOG_DEBUG("performCodeFusion",
|
|
|
+ "Processing PHI incoming block {0} in block {1}",
|
|
|
+ getNodeId(incomingBlock),
|
|
|
+ getNodeId(clonedBB));
|
|
|
if (reverseTargetBlockMap.count(incomingBlock)) {
|
|
|
- BasicBlock* originalBlock = reverseTargetBlockMap[incomingBlock];
|
|
|
- if (targetBlockMap.count(originalBlock)) {
|
|
|
- BasicBlock* mappedBlock = targetBlockMap[originalBlock];
|
|
|
- PHI->setIncomingBlock(i, mappedBlock);
|
|
|
- }
|
|
|
+ BasicBlock* mappedBlock = reverseTargetBlockMap[incomingBlock];
|
|
|
+ PHI->setIncomingBlock(i, mappedBlock);
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ LOG_WARNING("performCodeFusion",
|
|
|
+ "Incoming block {0} not found in reverseTargetBlockMap",
|
|
|
+ getNodeId(incomingBlock));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -1404,6 +1434,25 @@ namespace {
|
|
|
|
|
|
// 处理终结指令
|
|
|
if (BranchInst* BI = dyn_cast<BranchInst>(clonedBB->getTerminator())) {
|
|
|
+ // 检查是否是条件分支
|
|
|
+ if (BI->isConditional()) {
|
|
|
+ // 获取并映射条件值
|
|
|
+ Value* originalCond = BI->getCondition();
|
|
|
+ Value* mappedCond = MapValue(originalCond, targetVMap);
|
|
|
+
|
|
|
+ if (mappedCond) {
|
|
|
+ if (mappedCond->getType()->isIntegerTy(1)) {
|
|
|
+ // 更新条件值
|
|
|
+ BI->setCondition(mappedCond);
|
|
|
+ } else {
|
|
|
+ LOG_ERROR("performCodeFusion",
|
|
|
+ "Invalid condition type in block {0}: expected i1, got {1}",
|
|
|
+ getNodeId(clonedBB),
|
|
|
+ getReadableTypeName(mappedCond->getType()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// 检查是否是分片的最后一个基本块
|
|
|
bool isLastInSlice = (clonedBB == sliceBlocks.blocks.back());
|
|
|
|
|
|
@@ -1498,12 +1547,18 @@ namespace {
|
|
|
if (PHINode* PHI = dyn_cast<PHINode>(&I)) {
|
|
|
for (unsigned i = 0; i < PHI->getNumIncomingValues(); i++) {
|
|
|
BasicBlock* incomingBlock = PHI->getIncomingBlock(i);
|
|
|
+ LOG_DEBUG("performCodeFusion",
|
|
|
+ "Processing PHI incoming block {0} in block {1}",
|
|
|
+ getNodeId(incomingBlock),
|
|
|
+ getNodeId(clonedBB));
|
|
|
if (reverseBunkerBlockMap.count(incomingBlock)) {
|
|
|
- BasicBlock* originalBlock = reverseBunkerBlockMap[incomingBlock];
|
|
|
- if (bunkerBlockMap.count(originalBlock)) {
|
|
|
- BasicBlock* mappedBlock = bunkerBlockMap[originalBlock];
|
|
|
- PHI->setIncomingBlock(i, mappedBlock);
|
|
|
- }
|
|
|
+ BasicBlock* mappedBlock = reverseBunkerBlockMap[incomingBlock];
|
|
|
+ PHI->setIncomingBlock(i, mappedBlock);
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ LOG_WARNING("performCodeFusion",
|
|
|
+ "Incoming block {0} not found in reverseTargetBlockMap",
|
|
|
+ getNodeId(incomingBlock));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -1511,6 +1566,25 @@ namespace {
|
|
|
|
|
|
// 处理终结指令
|
|
|
if (BranchInst* BI = dyn_cast<BranchInst>(clonedBB->getTerminator())) {
|
|
|
+ // 检查是否是条件分支
|
|
|
+ if (BI->isConditional()) {
|
|
|
+ // 获取并映射条件值
|
|
|
+ Value* originalCond = BI->getCondition();
|
|
|
+ Value* mappedCond = MapValue(originalCond, bunkerVMap);
|
|
|
+
|
|
|
+ if (mappedCond) {
|
|
|
+ if (mappedCond->getType()->isIntegerTy(1)) {
|
|
|
+ // 更新条件值
|
|
|
+ BI->setCondition(mappedCond);
|
|
|
+ } else {
|
|
|
+ LOG_ERROR("performCodeFusion",
|
|
|
+ "Invalid condition type in block {0}: expected i1, got {1}",
|
|
|
+ getNodeId(clonedBB),
|
|
|
+ getReadableTypeName(mappedCond->getType()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// 检查是否是分片的最后一个基本块
|
|
|
bool isLastInSlice = (clonedBB == sliceBlocks.blocks.back());
|
|
|
|
|
|
@@ -1622,83 +1696,123 @@ namespace {
|
|
|
|
|
|
// 处理 bunker 函数的返回指令
|
|
|
for (auto& pair : bunkerBlockMap) {
|
|
|
- BasicBlock* BB = pair.second;
|
|
|
+ BasicBlock* BB = pair.first;
|
|
|
if (!BB || BB->empty()) continue;
|
|
|
+
|
|
|
+ std::vector<Instruction*> toDelete; // 存储需要删除的指令
|
|
|
+ bool foundReturn = false;
|
|
|
|
|
|
- if (ReturnInst* RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
|
|
|
- IRBuilder<> Builder(RI);
|
|
|
- Value* bunkerRetVal = RI->getReturnValue();
|
|
|
-
|
|
|
- // 创建结构体实例并正确初始化
|
|
|
- Value* structVal = UndefValue::get(STy);
|
|
|
- Type* targetType = STy->getElementType(0);
|
|
|
- Type* bunkerType = STy->getElementType(1);
|
|
|
-
|
|
|
- // 设置target返回值位置为默认值
|
|
|
- LOG_DEBUG("performCodeFusion", "Setting default target return value");
|
|
|
- Value* defaultTargetVal;
|
|
|
- if (targetType->isPointerTy()) {
|
|
|
- defaultTargetVal = ConstantPointerNull::get(cast<PointerType>(targetType));
|
|
|
- } else if (targetType->isIntegerTy()) {
|
|
|
- defaultTargetVal = ConstantInt::get(targetType, 0);
|
|
|
- } else if (targetType->isFloatingPointTy()) {
|
|
|
- defaultTargetVal = ConstantFP::get(targetType, 0.0);
|
|
|
- } else {
|
|
|
- defaultTargetVal = Constant::getNullValue(targetType);
|
|
|
- }
|
|
|
+ for (BasicBlock::iterator I = BB->begin(); I != BB->end();) {
|
|
|
+ Instruction* Inst = &*I;
|
|
|
+ ++I;
|
|
|
+ if (ReturnInst* RI = dyn_cast<ReturnInst>(Inst)){
|
|
|
+ // 添加额外的安全检查
|
|
|
+ if (!RI->getParent()) continue;
|
|
|
+ foundReturn = true;
|
|
|
+ IRBuilder<> Builder(RI);
|
|
|
+ Value* bunkerRetVal = RI->getReturnValue();
|
|
|
+
|
|
|
+ // 创建结构体实例并正确初始化
|
|
|
+ Value* structVal = UndefValue::get(STy);
|
|
|
+ Type* targetType = STy->getElementType(0);
|
|
|
+ Type* bunkerType = STy->getElementType(1);
|
|
|
+
|
|
|
+ // 设置target返回值位置为默认值
|
|
|
+ LOG_DEBUG("performCodeFusion", "Setting default target return value");
|
|
|
+ Value* defaultTargetVal;
|
|
|
+ if (targetType->isPointerTy()) {
|
|
|
+ defaultTargetVal = ConstantPointerNull::get(cast<PointerType>(targetType));
|
|
|
+ } else if (targetType->isIntegerTy()) {
|
|
|
+ defaultTargetVal = ConstantInt::get(targetType, 0);
|
|
|
+ } else if (targetType->isFloatingPointTy()) {
|
|
|
+ defaultTargetVal = ConstantFP::get(targetType, 0.0);
|
|
|
+ } else {
|
|
|
+ defaultTargetVal = Constant::getNullValue(targetType);
|
|
|
+ }
|
|
|
|
|
|
- Value* mappedBunkerRetVal = MapValue(bunkerRetVal, bunkerVMap);
|
|
|
- structVal = Builder.CreateInsertValue(structVal, defaultTargetVal, 0, "struct.target");
|
|
|
- structVal = Builder.CreateInsertValue(structVal, mappedBunkerRetVal, 1, "struct.bunker");
|
|
|
- ReturnInst* NewRI = ReturnInst::Create(M.getContext(), structVal, RI);
|
|
|
- ReplaceInstWithInst(RI, NewRI);
|
|
|
-
|
|
|
+ // Value* mappedBunkerRetVal = MapValue(bunkerRetVal, bunkerVMap);
|
|
|
+ structVal = Builder.CreateInsertValue(structVal, defaultTargetVal, 0, "struct.target");
|
|
|
+ structVal = Builder.CreateInsertValue(structVal, bunkerRetVal, 1, "struct.bunker");
|
|
|
+ ReturnInst* NewRI = ReturnInst::Create(M.getContext(), structVal, RI);
|
|
|
+ ReplaceInstWithInst(RI, NewRI);
|
|
|
+
|
|
|
+ LOG_DEBUG("performCodeFusion",
|
|
|
+ "Created struct return for bunker block: {0} with types: target={1}, bunker={2}",
|
|
|
+ getNodeId(BB),
|
|
|
+ getReadableTypeName(targetType),
|
|
|
+ getReadableTypeName(bunkerType));
|
|
|
+ }else if (foundReturn) {
|
|
|
+ // 将返回指令之后的所有指令标记为待删除
|
|
|
+ toDelete.push_back(Inst);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (Instruction* I : toDelete) {
|
|
|
LOG_DEBUG("performCodeFusion",
|
|
|
- "Created struct return for bunker block: {0} with types: target={1}, bunker={2}",
|
|
|
+ "Removing extra instruction after return in block {0}: {1}",
|
|
|
getNodeId(BB),
|
|
|
- getReadableTypeName(targetType),
|
|
|
- getReadableTypeName(bunkerType));
|
|
|
+ I->getOpcodeName());
|
|
|
+ I->eraseFromParent();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 处理 target 函数的返回指令
|
|
|
for (auto& pair : targetBlockMap) {
|
|
|
- BasicBlock* BB = pair.second;
|
|
|
+ BasicBlock* BB = pair.first;
|
|
|
if (!BB || BB->empty()) continue;
|
|
|
-
|
|
|
- if (ReturnInst* RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
|
|
|
- IRBuilder<> Builder(RI);
|
|
|
- Value* targetRetVal = RI->getReturnValue();
|
|
|
-
|
|
|
- // 创建结构体实例并正确初始化
|
|
|
- Value* structVal = ConstantAggregateZero::get(STy);
|
|
|
- Type* targetType = STy->getElementType(0);
|
|
|
- Type* bunkerType = STy->getElementType(1);
|
|
|
-
|
|
|
- // 设置bunker返回值位置为默认值
|
|
|
- Value* defaultBunkerVal;
|
|
|
- if (bunkerType->isPointerTy()) {
|
|
|
- defaultBunkerVal = ConstantPointerNull::get(cast<PointerType>(bunkerType));
|
|
|
- } else if (bunkerType->isIntegerTy()) {
|
|
|
- defaultBunkerVal = ConstantInt::get(bunkerType, 0);
|
|
|
- } else if (bunkerType->isFloatingPointTy()) {
|
|
|
- defaultBunkerVal = ConstantFP::get(bunkerType, 0.0);
|
|
|
- } else {
|
|
|
- defaultBunkerVal = Constant::getNullValue(bunkerType);
|
|
|
- }
|
|
|
|
|
|
- Value* mappedTargetRetVal = MapValue(targetRetVal, targetVMap);
|
|
|
+ std::vector<Instruction*> toDelete; // 存储需要删除的指令
|
|
|
+ bool foundReturn = false;
|
|
|
+
|
|
|
+ for (BasicBlock::iterator I = BB->begin(); I != BB->end();) {
|
|
|
+ Instruction* Inst = &*I;
|
|
|
+ ++I;
|
|
|
+ if (ReturnInst* RI = dyn_cast<ReturnInst>(Inst)){
|
|
|
+ // 添加额外的安全检查
|
|
|
+ if (!RI->getParent()) continue;
|
|
|
+ foundReturn = true;
|
|
|
+ IRBuilder<> Builder(RI);
|
|
|
+ Value* targetRetVal = RI->getReturnValue();
|
|
|
+
|
|
|
+ // 创建结构体实例并正确初始化
|
|
|
+ Value* structVal = ConstantAggregateZero::get(STy);
|
|
|
+ Type* targetType = STy->getElementType(0);
|
|
|
+ Type* bunkerType = STy->getElementType(1);
|
|
|
+
|
|
|
+ // 设置bunker返回值位置为默认值
|
|
|
+ Value* defaultBunkerVal;
|
|
|
+ if (bunkerType->isPointerTy()) {
|
|
|
+ defaultBunkerVal = ConstantPointerNull::get(cast<PointerType>(bunkerType));
|
|
|
+ } else if (bunkerType->isIntegerTy()) {
|
|
|
+ defaultBunkerVal = ConstantInt::get(bunkerType, 0);
|
|
|
+ } else if (bunkerType->isFloatingPointTy()) {
|
|
|
+ defaultBunkerVal = ConstantFP::get(bunkerType, 0.0);
|
|
|
+ } else {
|
|
|
+ defaultBunkerVal = Constant::getNullValue(bunkerType);
|
|
|
+ }
|
|
|
|
|
|
- structVal = Builder.CreateInsertValue(structVal, mappedTargetRetVal, 0, "struct.target");
|
|
|
- structVal = Builder.CreateInsertValue(structVal, defaultBunkerVal, 1, "struct.bunker");
|
|
|
- ReturnInst* NewRI = ReturnInst::Create(M.getContext(), structVal, RI);
|
|
|
- ReplaceInstWithInst(RI, NewRI);
|
|
|
-
|
|
|
+ // Value* mappedTargetRetVal = MapValue(targetRetVal, targetVMap);
|
|
|
+
|
|
|
+ structVal = Builder.CreateInsertValue(structVal, targetRetVal, 0, "struct.target");
|
|
|
+ structVal = Builder.CreateInsertValue(structVal, defaultBunkerVal, 1, "struct.bunker");
|
|
|
+ ReturnInst* NewRI = ReturnInst::Create(M.getContext(), structVal, RI);
|
|
|
+ ReplaceInstWithInst(RI, NewRI);
|
|
|
+
|
|
|
+ LOG_DEBUG("performCodeFusion",
|
|
|
+ "Created struct return for target block: {0} with types: target={1}, bunker={2}",
|
|
|
+ getNodeId(BB),
|
|
|
+ getReadableTypeName(targetType),
|
|
|
+ getReadableTypeName(bunkerType));
|
|
|
+ }else if (foundReturn) {
|
|
|
+ // 将返回指令之后的所有指令标记为待删除
|
|
|
+ toDelete.push_back(Inst);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (Instruction* I : toDelete) {
|
|
|
LOG_DEBUG("performCodeFusion",
|
|
|
- "Created struct return for target block: {0} with types: target={1}, bunker={2}",
|
|
|
+ "Removing extra instruction after return in block {0}: {1}",
|
|
|
getNodeId(BB),
|
|
|
- getReadableTypeName(targetType),
|
|
|
- getReadableTypeName(bunkerType));
|
|
|
+ I->getOpcodeName());
|
|
|
+ I->eraseFromParent();
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
@@ -1709,77 +1823,147 @@ namespace {
|
|
|
|
|
|
// 处理 bunker 函数的返回指令
|
|
|
for (auto& pair : bunkerBlockMap) {
|
|
|
- BasicBlock* BB = pair.second;
|
|
|
- if (!BB || BB->empty()) continue;
|
|
|
+ BasicBlock* BB = pair.first;
|
|
|
+
|
|
|
+ LOG_DEBUG("performCodeFusion", "Processing bunker block: {0}", getNodeId(BB));
|
|
|
|
|
|
- if (ReturnInst* RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
|
|
|
- IRBuilder<> Builder(RI);
|
|
|
- Value* bunkerRetVal = RI->getReturnValue();
|
|
|
-
|
|
|
- // 先重映射返回值
|
|
|
- if (bunkerRetVal) {
|
|
|
- bunkerRetVal = MapValue(bunkerRetVal, bunkerVMap);
|
|
|
- }
|
|
|
-
|
|
|
- if (!bunkerRetVal) {
|
|
|
- if (returnType->isIntegerTy()) {
|
|
|
- bunkerRetVal = ConstantInt::get(returnType, 0);
|
|
|
- } else if (returnType->isPointerTy()) {
|
|
|
- bunkerRetVal = ConstantPointerNull::get(cast<PointerType>(returnType));
|
|
|
- } else if (returnType->isFloatingPointTy()) {
|
|
|
- bunkerRetVal = ConstantFP::get(returnType, 0.0);
|
|
|
- } else {
|
|
|
- bunkerRetVal = UndefValue::get(returnType);
|
|
|
+ if (!BB || BB->empty()) continue;
|
|
|
+
|
|
|
+ std::vector<Instruction*> toDelete; // 存储需要删除的指令
|
|
|
+ bool foundReturn = false;
|
|
|
+
|
|
|
+ for (BasicBlock::iterator I = BB->begin(); I != BB->end();) {
|
|
|
+ Instruction* Inst = &*I;
|
|
|
+ ++I;
|
|
|
+
|
|
|
+ // 打印每条指令的信息
|
|
|
+ std::string instStr;
|
|
|
+ raw_string_ostream rso(instStr);
|
|
|
+ Inst->print(rso);
|
|
|
+ LOG_DEBUG("performCodeFusion",
|
|
|
+ "Block {0} - Processing instruction: {1} (opcode: {2})",
|
|
|
+ getNodeId(BB),
|
|
|
+ instStr,
|
|
|
+ Inst->getOpcodeName());
|
|
|
+
|
|
|
+ if (ReturnInst* RI = dyn_cast<ReturnInst>(Inst)){
|
|
|
+ // 添加额外的安全检查
|
|
|
+ if (!RI->getParent()) continue;
|
|
|
+ foundReturn = true;
|
|
|
+ IRBuilder<> Builder(RI);
|
|
|
+ Value* bunkerRetVal = RI->getReturnValue();
|
|
|
+ // 先重映射返回值
|
|
|
+ // if (bunkerRetVal) {
|
|
|
+ // LOG_DEBUG("performCodeFusion", "Mapping bunker return value");
|
|
|
+ // bunkerRetVal = MapValue(bunkerRetVal, bunkerVMap);
|
|
|
+ // }
|
|
|
+
|
|
|
+ if (!bunkerRetVal) {
|
|
|
+ LOG_DEBUG("performCodeFusion", "Setting default bunker return value");
|
|
|
+ if (returnType->isIntegerTy()) {
|
|
|
+ bunkerRetVal = ConstantInt::get(returnType, 0);
|
|
|
+ } else if (returnType->isPointerTy()) {
|
|
|
+ bunkerRetVal = ConstantPointerNull::get(cast<PointerType>(returnType));
|
|
|
+ } else if (returnType->isFloatingPointTy()) {
|
|
|
+ bunkerRetVal = ConstantFP::get(returnType, 0.0);
|
|
|
+ } else {
|
|
|
+ bunkerRetVal = UndefValue::get(returnType);
|
|
|
+ }
|
|
|
+ } else if (bunkerRetVal->getType() != returnType) {
|
|
|
+ LOG_DEBUG("performCodeFusion", "Casting bunker return value to {0}", getReadableTypeName(returnType));
|
|
|
+ bunkerRetVal = Builder.CreateBitCast(bunkerRetVal, returnType);
|
|
|
}
|
|
|
- } else if (bunkerRetVal->getType() != returnType) {
|
|
|
- bunkerRetVal = Builder.CreateBitCast(bunkerRetVal, returnType);
|
|
|
+
|
|
|
+ ReturnInst* NewRI = ReturnInst::Create(M.getContext(), bunkerRetVal);
|
|
|
+ ReplaceInstWithInst(RI, NewRI);
|
|
|
+
|
|
|
+ LOG_DEBUG("performCodeFusion",
|
|
|
+ "Updated bunker return in block {0} with value type {1}",
|
|
|
+ getNodeId(BB),
|
|
|
+ getReadableTypeName(bunkerRetVal->getType()));
|
|
|
+ }else if (foundReturn) {
|
|
|
+ // 将返回指令之后的所有指令标记为待删除
|
|
|
+ toDelete.push_back(Inst);
|
|
|
}
|
|
|
-
|
|
|
- ReturnInst* NewRI = ReturnInst::Create(M.getContext(), bunkerRetVal);
|
|
|
- ReplaceInstWithInst(RI, NewRI);
|
|
|
-
|
|
|
+ }
|
|
|
+ for (Instruction* I : toDelete) {
|
|
|
LOG_DEBUG("performCodeFusion",
|
|
|
- "Updated bunker return in block {0} with value type {1}",
|
|
|
+ "Removing extra instruction after return in block {0}: {1}",
|
|
|
getNodeId(BB),
|
|
|
- getReadableTypeName(bunkerRetVal->getType()));
|
|
|
+ I->getOpcodeName());
|
|
|
+ I->eraseFromParent();
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
// 处理 target 函数的返回指令
|
|
|
for (auto& pair : targetBlockMap) {
|
|
|
- BasicBlock* BB = pair.second;
|
|
|
+ BasicBlock* BB = pair.first;
|
|
|
+
|
|
|
+ LOG_DEBUG("performCodeFusion", "Processing target block: {0}", getNodeId(BB));
|
|
|
+
|
|
|
if (!BB || BB->empty()) continue;
|
|
|
-
|
|
|
- if (ReturnInst* RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
|
|
|
- IRBuilder<> Builder(RI);
|
|
|
- Value* targetRetVal = RI->getReturnValue();
|
|
|
-
|
|
|
- // 先重映射返回值
|
|
|
- if (targetRetVal) {
|
|
|
- targetRetVal = MapValue(targetRetVal, targetVMap);
|
|
|
- }
|
|
|
-
|
|
|
- if (!targetRetVal) {
|
|
|
- if (returnType->isIntegerTy()) {
|
|
|
- targetRetVal = ConstantInt::get(returnType, 0);
|
|
|
- } else if (returnType->isPointerTy()) {
|
|
|
- targetRetVal = ConstantPointerNull::get(cast<PointerType>(returnType));
|
|
|
- } else if (returnType->isFloatingPointTy()) {
|
|
|
- targetRetVal = ConstantFP::get(returnType, 0.0);
|
|
|
- } else {
|
|
|
- targetRetVal = UndefValue::get(returnType);
|
|
|
+
|
|
|
+ std::vector<Instruction*> toDelete; // 存储需要删除的指令
|
|
|
+ bool foundReturn = false;
|
|
|
+
|
|
|
+ for (BasicBlock::iterator I = BB->begin(); I != BB->end();) {
|
|
|
+ Instruction* Inst = &*I;
|
|
|
+ ++I;
|
|
|
+
|
|
|
+ // 打印每条指令的信息
|
|
|
+ std::string instStr;
|
|
|
+ raw_string_ostream rso(instStr);
|
|
|
+ Inst->print(rso);
|
|
|
+ LOG_DEBUG("performCodeFusion",
|
|
|
+ "Block {0} - Processing instruction: {1} (opcode: {2})",
|
|
|
+ getNodeId(BB),
|
|
|
+ instStr,
|
|
|
+ Inst->getOpcodeName());
|
|
|
+
|
|
|
+ if (ReturnInst* RI = dyn_cast<ReturnInst>(Inst)){
|
|
|
+ // 添加额外的安全检查
|
|
|
+ if (!RI->getParent()) continue;
|
|
|
+ foundReturn = true;
|
|
|
+ IRBuilder<> Builder(RI);
|
|
|
+ Value* targetRetVal = RI->getReturnValue();
|
|
|
+ // 先重映射返回值
|
|
|
+ // if (targetRetVal) {
|
|
|
+ // LOG_DEBUG("performCodeFusion", "Mapping target return value");
|
|
|
+ // targetRetVal = MapValue(targetRetVal, targetVMap);
|
|
|
+ // }
|
|
|
+
|
|
|
+ if (!targetRetVal) {
|
|
|
+ LOG_DEBUG("performCodeFusion", "Setting default target return value");
|
|
|
+ if (returnType->isIntegerTy()) {
|
|
|
+ targetRetVal = ConstantInt::get(returnType, 0);
|
|
|
+ } else if (returnType->isPointerTy()) {
|
|
|
+ targetRetVal = ConstantPointerNull::get(cast<PointerType>(returnType));
|
|
|
+ } else if (returnType->isFloatingPointTy()) {
|
|
|
+ targetRetVal = ConstantFP::get(returnType, 0.0);
|
|
|
+ } else {
|
|
|
+ targetRetVal = UndefValue::get(returnType);
|
|
|
+ }
|
|
|
+ } else if (targetRetVal->getType() != returnType) {
|
|
|
+ LOG_DEBUG("performCodeFusion", "Casting target return value to {0}", getReadableTypeName(returnType));
|
|
|
+ targetRetVal = Builder.CreateBitCast(targetRetVal, returnType);
|
|
|
}
|
|
|
- } else if (targetRetVal->getType() != returnType) {
|
|
|
- targetRetVal = Builder.CreateBitCast(targetRetVal, returnType);
|
|
|
+
|
|
|
+ ReturnInst* NewRI = ReturnInst::Create(M.getContext(), targetRetVal);
|
|
|
+ ReplaceInstWithInst(RI, NewRI);
|
|
|
+ LOG_DEBUG("performCodeFusion",
|
|
|
+ "Updated target return in block {0} with value type {1}",
|
|
|
+ getNodeId(BB),
|
|
|
+ getReadableTypeName(targetRetVal->getType()));
|
|
|
+ }else if (foundReturn) {
|
|
|
+ // 将返回指令之后的所有指令标记为待删除
|
|
|
+ toDelete.push_back(Inst);
|
|
|
}
|
|
|
-
|
|
|
- ReturnInst* NewRI = ReturnInst::Create(M.getContext(), targetRetVal);
|
|
|
- ReplaceInstWithInst(RI, NewRI);
|
|
|
-
|
|
|
+ }
|
|
|
+ for (Instruction* I : toDelete) {
|
|
|
LOG_DEBUG("performCodeFusion",
|
|
|
- "Updated target return in block {0} with value type {1}",
|
|
|
+ "Removing extra instruction after return in block {0}: {1}",
|
|
|
getNodeId(BB),
|
|
|
- getReadableTypeName(targetRetVal->getType()));
|
|
|
+ I->getOpcodeName());
|
|
|
+ I->eraseFromParent();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -1790,34 +1974,79 @@ namespace {
|
|
|
|
|
|
// 处理 void 返回类型的情况
|
|
|
for (auto& pair : bunkerBlockMap) {
|
|
|
- BasicBlock* BB = pair.second;
|
|
|
+ BasicBlock* BB = pair.first;
|
|
|
if (!BB || BB->empty()) continue;
|
|
|
+
|
|
|
+ std::vector<Instruction*> toDelete; // 存储需要删除的指令
|
|
|
+ bool foundReturn = false;
|
|
|
|
|
|
- if (ReturnInst* RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
|
|
|
+ for (BasicBlock::iterator I = BB->begin(); I != BB->end();) {
|
|
|
+ Instruction* Inst = &*I;
|
|
|
+ ++I;
|
|
|
+ if (ReturnInst* RI = dyn_cast<ReturnInst>(Inst)){
|
|
|
+ // 添加额外的安全检查
|
|
|
+ if (!RI->getParent()) continue;
|
|
|
+ foundReturn = true;
|
|
|
+ LOG_DEBUG("performCodeFusion",
|
|
|
+ "Converting bunker return instruction to void return in block: {0}",
|
|
|
+ getNodeId(BB));
|
|
|
+
|
|
|
+ ReturnInst* NewRI = ReturnInst::Create(M.getContext(), nullptr, RI);
|
|
|
+ ReplaceInstWithInst(RI, NewRI);
|
|
|
+ }else if (foundReturn) {
|
|
|
+ // 将返回指令之后的所有指令标记为待删除
|
|
|
+ toDelete.push_back(Inst);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (Instruction* I : toDelete) {
|
|
|
LOG_DEBUG("performCodeFusion",
|
|
|
- "Converting bunker return instruction to void return in block: {0}",
|
|
|
- getNodeId(BB));
|
|
|
-
|
|
|
- ReturnInst* NewRI = ReturnInst::Create(M.getContext(), nullptr, RI);
|
|
|
- ReplaceInstWithInst(RI, NewRI);
|
|
|
+ "Removing extra instruction after return in block {0}: {1}",
|
|
|
+ getNodeId(BB),
|
|
|
+ I->getOpcodeName());
|
|
|
+ I->eraseFromParent();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
for (auto& pair : targetBlockMap) {
|
|
|
- BasicBlock* BB = pair.second;
|
|
|
+ BasicBlock* BB = pair.first;
|
|
|
if (!BB || BB->empty()) continue;
|
|
|
+
|
|
|
+ std::vector<Instruction*> toDelete; // 存储需要删除的指令
|
|
|
+ bool foundReturn = false;
|
|
|
|
|
|
- if (ReturnInst* RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
|
|
|
+ for (BasicBlock::iterator I = BB->begin(); I != BB->end();) {
|
|
|
+ Instruction* Inst = &*I;
|
|
|
+ ++I;
|
|
|
+ if (ReturnInst* RI = dyn_cast<ReturnInst>(Inst)){
|
|
|
+ // 添加额外的安全检查
|
|
|
+ if (!RI->getParent()) continue;
|
|
|
+ foundReturn = true;
|
|
|
+ LOG_DEBUG("performCodeFusion",
|
|
|
+ "Converting target return instruction to void return in block: {0}",
|
|
|
+ BB->getName().str());
|
|
|
+
|
|
|
+ ReturnInst* NewRI = ReturnInst::Create(M.getContext(), nullptr, RI);
|
|
|
+ ReplaceInstWithInst(RI, NewRI);
|
|
|
+ }else if (foundReturn) {
|
|
|
+ // 将返回指令之后的所有指令标记为待删除
|
|
|
+ toDelete.push_back(Inst);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (Instruction* I : toDelete) {
|
|
|
LOG_DEBUG("performCodeFusion",
|
|
|
- "Converting target return instruction to void return in block: {0}",
|
|
|
- BB->getName().str());
|
|
|
-
|
|
|
- ReturnInst* NewRI = ReturnInst::Create(M.getContext(), nullptr, RI);
|
|
|
- ReplaceInstWithInst(RI, NewRI);
|
|
|
+ "Removing extra instruction after return in block {0}: {1}",
|
|
|
+ getNodeId(BB),
|
|
|
+ I->getOpcodeName());
|
|
|
+ I->eraseFromParent();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // 添加新的日志打印代码
|
|
|
+ LOG_INFO("performCodeFusion", "Listing all instructions after return repairing in function: {0}",
|
|
|
+ fusedFunc->getName().str());
|
|
|
+ dumpControlFlowGraph(*fusedFunc);
|
|
|
+
|
|
|
// 更新调用点
|
|
|
std::vector<CallInst*> bunkerCalls;
|
|
|
std::vector<CallInst*> targetCalls;
|
|
|
@@ -1848,12 +2077,10 @@ namespace {
|
|
|
targetFunc->eraseFromParent();
|
|
|
|
|
|
|
|
|
- // // 添加新的日志打印代码
|
|
|
- // if (fusedFunc->getName() == "fused_createDynamicArray") {
|
|
|
- // LOG_INFO("performCodeFusion", "Listing all instructions after block cloning in function: {0}",
|
|
|
- // fusedFunc->getName().str());
|
|
|
- // dumpControlFlowGraph(*fusedFunc);
|
|
|
- // }
|
|
|
+ // 添加新的日志打印代码
|
|
|
+ LOG_INFO("performCodeFusion", "Listing all instructions after update calls in function: {0}",
|
|
|
+ fusedFunc->getName().str());
|
|
|
+ dumpControlFlowGraph(*fusedFunc);
|
|
|
|
|
|
|
|
|
LOG_INFO("performCodeFusion",
|