[midend-Loop-InductionVarStrengthReduction]增加循环规约变量强度削弱优化
This commit is contained in:
@@ -306,22 +306,74 @@ void LoopCharacteristicsPass::identifyBasicInductionVariables(
|
||||
BasicBlock* header = loop->getHeader();
|
||||
std::vector<std::unique_ptr<InductionVarInfo>> ivs;
|
||||
|
||||
if (DEBUG) {
|
||||
std::cout << " === Identifying Induction Variables for Loop: " << loop->getName() << " ===" << std::endl;
|
||||
std::cout << " Loop header: " << header->getName() << std::endl;
|
||||
std::cout << " Loop blocks: ";
|
||||
for (auto* bb : loop->getBlocks()) {
|
||||
std::cout << bb->getName() << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
// 1. 识别所有BIV
|
||||
for (auto& inst : header->getInstructions()) {
|
||||
auto* phi = dynamic_cast<PhiInst*>(inst.get());
|
||||
if (!phi) continue;
|
||||
if (isBasicInductionVariable(phi, loop)) {
|
||||
ivs.push_back(InductionVarInfo::createBasicBIV(phi, Instruction::Kind::kPhi));
|
||||
if (DEBUG) std::cout << " Found basic induction variable: " << phi->getName() << std::endl;
|
||||
if (DEBUG) {
|
||||
std::cout << " [BIV] Found basic induction variable: " << phi->getName() << std::endl;
|
||||
std::cout << " Incoming values: ";
|
||||
for (auto& [incomingBB, incomingVal] : phi->getIncomingValues()) {
|
||||
std::cout << "{" << incomingBB->getName() << ": " << incomingVal->getName() << "} ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (DEBUG) {
|
||||
std::cout << " Found " << ivs.size() << " basic induction variables" << std::endl;
|
||||
}
|
||||
|
||||
// 2. 递归识别所有派生DIV
|
||||
std::set<Value*> visited;
|
||||
size_t initialSize = ivs.size();
|
||||
for (const auto& biv : ivs) {
|
||||
if (DEBUG) {
|
||||
std::cout << " Searching for derived IVs from BIV: " << biv->div->getName() << std::endl;
|
||||
}
|
||||
findDerivedInductionVars(biv->div, biv->base, loop, ivs, visited);
|
||||
}
|
||||
|
||||
if (DEBUG) {
|
||||
size_t derivedCount = ivs.size() - initialSize;
|
||||
std::cout << " Found " << derivedCount << " derived induction variables" << std::endl;
|
||||
|
||||
// 打印所有归纳变量的详细信息
|
||||
std::cout << " === Final Induction Variables Summary ===" << std::endl;
|
||||
for (size_t i = 0; i < ivs.size(); ++i) {
|
||||
const auto& iv = ivs[i];
|
||||
std::cout << " [" << i << "] " << iv->div->getName()
|
||||
<< " (kind: " << (iv->ivkind == IVKind::kBasic ? "Basic" :
|
||||
iv->ivkind == IVKind::kLinear ? "Linear" : "Complex") << ")" << std::endl;
|
||||
std::cout << " Operation: " << static_cast<int>(iv->Instkind) << std::endl;
|
||||
if (iv->base) {
|
||||
std::cout << " Base: " << iv->base->getName() << std::endl;
|
||||
}
|
||||
if (iv->Multibase.first || iv->Multibase.second) {
|
||||
std::cout << " Multi-base: ";
|
||||
if (iv->Multibase.first) std::cout << iv->Multibase.first->getName() << " ";
|
||||
if (iv->Multibase.second) std::cout << iv->Multibase.second->getName() << " ";
|
||||
std::cout << std::endl;
|
||||
}
|
||||
std::cout << " Factor: " << iv->factor << ", Offset: " << iv->offset << std::endl;
|
||||
std::cout << " Valid: " << (iv->valid ? "Yes" : "No") << std::endl;
|
||||
}
|
||||
std::cout << " =============================================" << std::endl;
|
||||
}
|
||||
|
||||
characteristics->InductionVars = std::move(ivs);
|
||||
}
|
||||
|
||||
@@ -342,52 +394,97 @@ static LinearExpr analyzeLinearExpr(Value* val, Loop* loop, std::vector<std::uni
|
||||
// 只支持单/双BIV线性组合
|
||||
// 见下方详细实现
|
||||
// ----------
|
||||
if (DEBUG >= 2) { // 更详细的调试级别
|
||||
if (auto* inst = dynamic_cast<Instruction*>(val)) {
|
||||
std::cout << " Analyzing linear expression for: " << val->getName()
|
||||
<< " (kind: " << static_cast<int>(inst->getKind()) << ")" << std::endl;
|
||||
} else {
|
||||
std::cout << " Analyzing linear expression for value: " << val->getName() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// 基本变量:常数
|
||||
if (auto* cint = dynamic_cast<ConstantInteger*>(val)) {
|
||||
if (DEBUG >= 2) {
|
||||
std::cout << " -> Constant: " << cint->getInt() << std::endl;
|
||||
}
|
||||
return {nullptr, nullptr, 0, 0, cint->getInt(), true, false};
|
||||
}
|
||||
|
||||
// 基本变量:BIV或派生IV
|
||||
for (auto& iv : ivs) {
|
||||
if (iv->div == val) {
|
||||
if (iv->ivkind == IVKind::kBasic ||
|
||||
iv->ivkind == IVKind::kLinear) {
|
||||
if (DEBUG >= 2) {
|
||||
std::cout << " -> Found " << (iv->ivkind == IVKind::kBasic ? "Basic" : "Linear")
|
||||
<< " IV with base: " << (iv->base ? iv->base->getName() : "null")
|
||||
<< ", factor: " << iv->factor << ", offset: " << iv->offset << std::endl;
|
||||
}
|
||||
return {iv->base, nullptr, iv->factor, 0, iv->offset, true, true};
|
||||
}
|
||||
// 复杂归纳变量
|
||||
if (iv->ivkind == IVKind::kCmplx) {
|
||||
if (DEBUG >= 2) {
|
||||
std::cout << " -> Found Complex IV with multi-base" << std::endl;
|
||||
}
|
||||
return {iv->Multibase.first, iv->Multibase.second, 1, 1, 0, true, false};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 一元负号
|
||||
if (auto* inst = dynamic_cast<Instruction*>(val)) {
|
||||
auto kind = inst->getKind();
|
||||
if (kind == Instruction::Kind::kNeg) {
|
||||
if (DEBUG >= 2) {
|
||||
std::cout << " -> Analyzing negation" << std::endl;
|
||||
}
|
||||
auto expr = analyzeLinearExpr(inst->getOperand(0), loop, ivs);
|
||||
if (!expr.valid) return expr;
|
||||
expr.factor1 = -expr.factor1;
|
||||
expr.factor2 = -expr.factor2;
|
||||
expr.offset = -expr.offset;
|
||||
expr.isSimple = (expr.base2 == nullptr);
|
||||
if (DEBUG >= 2) {
|
||||
std::cout << " -> Negation result: valid=" << expr.valid << ", simple=" << expr.isSimple << std::endl;
|
||||
}
|
||||
return expr;
|
||||
}
|
||||
|
||||
// 二元加减乘
|
||||
if (kind == Instruction::Kind::kAdd || kind == Instruction::Kind::kSub) {
|
||||
if (DEBUG >= 2) {
|
||||
std::cout << " -> Analyzing " << (kind == Instruction::Kind::kAdd ? "addition" : "subtraction") << std::endl;
|
||||
}
|
||||
auto expr0 = analyzeLinearExpr(inst->getOperand(0), loop, ivs);
|
||||
auto expr1 = analyzeLinearExpr(inst->getOperand(1), loop, ivs);
|
||||
if (!expr0.valid || !expr1.valid) return {nullptr, nullptr, 0, 0, 0, false, false};
|
||||
if (!expr0.valid || !expr1.valid) {
|
||||
if (DEBUG >= 2) {
|
||||
std::cout << " -> Failed: operand not linear (expr0.valid=" << expr0.valid << ", expr1.valid=" << expr1.valid << ")" << std::endl;
|
||||
}
|
||||
return {nullptr, nullptr, 0, 0, 0, false, false};
|
||||
}
|
||||
|
||||
// 合并:若BIV相同或有一个是常数
|
||||
// 单BIV+常数
|
||||
if (expr0.base1 && !expr1.base1 && !expr1.base2) {
|
||||
int sign = (kind == Instruction::Kind::kAdd ? 1 : -1);
|
||||
if (DEBUG >= 2) {
|
||||
std::cout << " -> Single BIV + constant pattern" << std::endl;
|
||||
}
|
||||
return {expr0.base1, nullptr, expr0.factor1, 0, expr0.offset + sign * expr1.offset, true, expr0.isSimple};
|
||||
}
|
||||
if (!expr0.base1 && !expr0.base2 && expr1.base1) {
|
||||
int sign = (kind == Instruction::Kind::kAdd ? 1 : -1);
|
||||
int f = sign * expr1.factor1;
|
||||
int off = expr0.offset + sign * expr1.offset;
|
||||
if (DEBUG >= 2) {
|
||||
std::cout << " -> Constant + single BIV pattern" << std::endl;
|
||||
}
|
||||
return {expr1.base1, nullptr, f, 0, off, true, expr1.isSimple};
|
||||
}
|
||||
|
||||
// 双BIV线性组合
|
||||
if (expr0.base1 && expr1.base1 && expr0.base1 != expr1.base1 && !expr0.base2 && !expr1.base2) {
|
||||
int sign = (kind == Instruction::Kind::kAdd ? 1 : -1);
|
||||
@@ -396,31 +493,56 @@ static LinearExpr analyzeLinearExpr(Value* val, Loop* loop, std::vector<std::uni
|
||||
int f1 = expr0.factor1;
|
||||
int f2 = sign * expr1.factor1;
|
||||
int off = expr0.offset + sign * expr1.offset;
|
||||
if (DEBUG >= 2) {
|
||||
std::cout << " -> Double BIV linear combination" << std::endl;
|
||||
}
|
||||
return {base1, base2, f1, f2, off, true, false};
|
||||
}
|
||||
|
||||
// 同BIV合并
|
||||
if (expr0.base1 && expr1.base1 && expr0.base1 == expr1.base1 && !expr0.base2 && !expr1.base2) {
|
||||
int sign = (kind == Instruction::Kind::kAdd ? 1 : -1);
|
||||
int f = expr0.factor1 + sign * expr1.factor1;
|
||||
int off = expr0.offset + sign * expr1.offset;
|
||||
if (DEBUG >= 2) {
|
||||
std::cout << " -> Same BIV combination" << std::endl;
|
||||
}
|
||||
return {expr0.base1, nullptr, f, 0, off, true, true};
|
||||
}
|
||||
}
|
||||
|
||||
// 乘法:BIV*const 或 const*BIV
|
||||
if (kind == Instruction::Kind::kMul) {
|
||||
if (DEBUG >= 2) {
|
||||
std::cout << " -> Analyzing multiplication" << std::endl;
|
||||
}
|
||||
auto expr0 = analyzeLinearExpr(inst->getOperand(0), loop, ivs);
|
||||
auto expr1 = analyzeLinearExpr(inst->getOperand(1), loop, ivs);
|
||||
|
||||
// 只允许一侧为常数
|
||||
if (expr0.base1 && !expr1.base1 && !expr1.base2 && expr1.offset) {
|
||||
if (DEBUG >= 2) {
|
||||
std::cout << " -> BIV * constant pattern" << std::endl;
|
||||
}
|
||||
return {expr0.base1, nullptr, expr0.factor1 * expr1.offset, 0, expr0.offset * expr1.offset, true, true};
|
||||
}
|
||||
if (!expr0.base1 && !expr0.base2 && expr0.offset && expr1.base1) {
|
||||
if (DEBUG >= 2) {
|
||||
std::cout << " -> Constant * BIV pattern" << std::endl;
|
||||
}
|
||||
return {expr1.base1, nullptr, expr1.factor1 * expr0.offset, 0, expr1.offset * expr0.offset, true, true};
|
||||
}
|
||||
// 双BIV乘法不支持
|
||||
if (DEBUG >= 2) {
|
||||
std::cout << " -> Multiplication pattern not supported" << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 其它情况
|
||||
if (DEBUG >= 2) {
|
||||
std::cout << " -> Other case: not linear" << std::endl;
|
||||
}
|
||||
return {nullptr, nullptr, 0, 0, 0, false, false};
|
||||
}
|
||||
|
||||
@@ -506,19 +628,40 @@ void LoopCharacteristicsPass::findDerivedInductionVars(
|
||||
if (visited.count(root)) return;
|
||||
visited.insert(root);
|
||||
|
||||
if (DEBUG) {
|
||||
std::cout << " Analyzing uses of: " << root->getName() << std::endl;
|
||||
}
|
||||
|
||||
for (auto use : root->getUses()) {
|
||||
auto user = use->getUser();
|
||||
Instruction* inst = dynamic_cast<Instruction*>(user);
|
||||
if (!inst) continue;
|
||||
if (!loop->contains(inst->getParent())) continue;
|
||||
if (!loop->contains(inst->getParent())) {
|
||||
if (DEBUG) {
|
||||
std::cout << " Skipping user outside loop: " << inst->getName() << std::endl;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (DEBUG) {
|
||||
std::cout << " Checking instruction: " << inst->getName()
|
||||
<< " (kind: " << static_cast<int>(inst->getKind()) << ")" << std::endl;
|
||||
}
|
||||
|
||||
// 下面是一个例子:假设你有线性归约分析(可用analyzeLinearExpr等递归辅助)
|
||||
auto expr = analyzeLinearExpr(inst, loop, ivs);
|
||||
|
||||
if (!expr.valid) {
|
||||
if (DEBUG) {
|
||||
std::cout << " Linear expression analysis failed for: " << inst->getName() << std::endl;
|
||||
}
|
||||
// 复杂非线性归纳变量,作为kCmplx记录(假如你想追踪)
|
||||
// 这里假设expr.base1、base2都有效才记录double
|
||||
if (expr.base1 && expr.base2) {
|
||||
if (DEBUG) {
|
||||
std::cout << " [DIV-COMPLEX] Creating complex derived IV: " << inst->getName()
|
||||
<< " with bases: " << expr.base1->getName() << ", " << expr.base2->getName() << std::endl;
|
||||
}
|
||||
ivs.push_back(InductionVarInfo::createDoubleDIV(inst, inst->getKind(), expr.base1, expr.base2, 0, expr.offset));
|
||||
}
|
||||
continue;
|
||||
@@ -526,15 +669,30 @@ void LoopCharacteristicsPass::findDerivedInductionVars(
|
||||
|
||||
// 单BIV线性
|
||||
if (expr.base1 && !expr.base2) {
|
||||
if (DEBUG) {
|
||||
std::cout << " [DIV-LINEAR] Creating single-base derived IV: " << inst->getName()
|
||||
<< " with base: " << expr.base1->getName()
|
||||
<< ", factor: " << expr.factor1
|
||||
<< ", offset: " << expr.offset << std::endl;
|
||||
}
|
||||
ivs.push_back(InductionVarInfo::createSingleDIV(inst, inst->getKind(), expr.base1, expr.factor1, expr.offset));
|
||||
findDerivedInductionVars(inst, expr.base1, loop, ivs, visited);
|
||||
}
|
||||
// 双BIV线性
|
||||
else if (expr.base1 && expr.base2) {
|
||||
if (DEBUG) {
|
||||
std::cout << " [DIV-COMPLEX] Creating double-base derived IV: " << inst->getName()
|
||||
<< " with bases: " << expr.base1->getName() << ", " << expr.base2->getName()
|
||||
<< ", offset: " << expr.offset << std::endl;
|
||||
}
|
||||
ivs.push_back(InductionVarInfo::createDoubleDIV(inst, inst->getKind(), expr.base1, expr.base2, 0, expr.offset));
|
||||
// 双BIV情形一般不再递归下游
|
||||
}
|
||||
}
|
||||
|
||||
if (DEBUG) {
|
||||
std::cout << " Finished analyzing uses of: " << root->getName() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// 递归/推进式判定
|
||||
|
||||
Reference in New Issue
Block a user