@@ -266,7 +266,7 @@ Function* PPUTranslator::Translate(const ppu_function& info)
266
266
if (!m_ir->GetInsertBlock ()->getTerminator ())
267
267
{
268
268
FlushRegisters ();
269
- CallFunction (m_addr);
269
+ CallFunction (m_addr, nullptr , m_ir-> GetInsertBlock () );
270
270
}
271
271
}
272
272
@@ -354,7 +354,7 @@ Value* PPUTranslator::RotateLeft(Value* arg, Value* n)
354
354
return m_ir->CreateOr (m_ir->CreateShl (arg, m_ir->CreateAnd (n, mask)), m_ir->CreateLShr (arg, m_ir->CreateAnd (m_ir->CreateNeg (n), mask)));
355
355
}
356
356
357
- void PPUTranslator::CallFunction (u64 target, Value* indirect)
357
+ void PPUTranslator::CallFunction (u64 target, Value* indirect, BasicBlock* prev_block )
358
358
{
359
359
const auto type = m_function->getFunctionType ();
360
360
const auto block = m_ir->GetInsertBlock ();
@@ -372,13 +372,19 @@ void PPUTranslator::CallFunction(u64 target, Value* indirect)
372
372
373
373
if (_target >= caddr && _target <= cend)
374
374
{
375
- std::unordered_set<u64> passed_targets{_target} ;
375
+ std::unordered_set<u64> passed_targets;
376
376
377
377
u32 target_last = _target;
378
378
379
379
// Try to follow unconditional branches as long as there is no infinite loop
380
- while (target_last != _target )
380
+ while (target_last != m_addr + base )
381
381
{
382
+ if (passed_targets.empty ())
383
+ {
384
+ passed_targets.emplace (_target);
385
+ passed_targets.emplace (m_addr + base);
386
+ }
387
+
382
388
const ppu_opcode_t op{*ensure (m_info.get_ptr <u32>(target_last))};
383
389
const ppu_itype::type itype = g_ppu_itype.decode (op.opcode );
384
390
@@ -401,11 +407,25 @@ void PPUTranslator::CallFunction(u64 target, Value* indirect)
401
407
402
408
// Odd destination
403
409
}
404
- else if (itype == ppu_itype::BCLR && (op.bo & 0x14 ) == 0x14 && !op.lk )
410
+ else if (itype == ppu_itype::BCLR && (op.bo & 0x14 ) == 0x14 && !op.lk && (prev_block || m_lr) )
405
411
{
406
412
// Special case: empty function
407
413
// In this case the branch can be treated as BCLR because previous CIA does not matter
408
- indirect = RegLoad (m_lr);
414
+ indirect = m_lr;
415
+
416
+ if (!indirect)
417
+ {
418
+ if (block != prev_block)
419
+ {
420
+ // Emit register load in the beginning of the common block
421
+ m_ir->SetInsertPoint (prev_block, prev_block->getFirstInsertionPt ());
422
+ }
423
+
424
+ indirect = RegLoad (m_lr);
425
+
426
+ // Restore current insert point
427
+ m_ir->SetInsertPoint (block);
428
+ }
409
429
}
410
430
411
431
break ;
@@ -629,7 +649,7 @@ Value* PPUTranslator::Trunc(Value* value, Type* type)
629
649
return type != value->getType () ? m_ir->CreateTrunc (value, type) : value;
630
650
}
631
651
632
- void PPUTranslator::UseCondition (MDNode* hint, Value* cond)
652
+ void PPUTranslator::UseCondition (MDNode* hint, Value* cond, BasicBlock* prev_block )
633
653
{
634
654
FlushRegisters ();
635
655
@@ -639,7 +659,7 @@ void PPUTranslator::UseCondition(MDNode* hint, Value* cond)
639
659
const auto next = BasicBlock::Create (m_context, " __next" , m_function);
640
660
m_ir->CreateCondBr (cond, local, next, hint);
641
661
m_ir->SetInsertPoint (next);
642
- CallFunction (m_addr + 4 );
662
+ CallFunction (m_addr + 4 , nullptr , prev_block );
643
663
m_ir->SetInsertPoint (local);
644
664
}
645
665
}
@@ -2024,6 +2044,8 @@ void PPUTranslator::BC(ppu_opcode_t op)
2024
2044
const s32 bt14 = op.bt14 ; // Workaround for VS 16.5
2025
2045
const u64 target = (op.aa ? 0 : m_addr) + bt14;
2026
2046
2047
+ const auto block = m_ir->GetInsertBlock ();
2048
+
2027
2049
if (op.aa && m_reloc)
2028
2050
{
2029
2051
CompilationError (" Branch with absolute address" );
@@ -2034,9 +2056,9 @@ void PPUTranslator::BC(ppu_opcode_t op)
2034
2056
m_ir->CreateStore (GetAddr (+4 ), m_ir->CreateStructGEP (m_thread_type, m_thread, static_cast <uint >(&m_lr - m_locals)));
2035
2057
}
2036
2058
2037
- UseCondition (CheckBranchProbability (op.bo ), CheckBranchCondition (op.bo , op.bi ));
2059
+ UseCondition (CheckBranchProbability (op.bo ), CheckBranchCondition (op.bo , op.bi ), block );
2038
2060
2039
- CallFunction (target);
2061
+ CallFunction (target, nullptr , block );
2040
2062
}
2041
2063
2042
2064
void PPUTranslator::SC (ppu_opcode_t op)
@@ -2074,6 +2096,8 @@ void PPUTranslator::B(ppu_opcode_t op)
2074
2096
const s32 bt24 = op.bt24 ; // Workaround for VS 16.5
2075
2097
const u64 target = (op.aa ? 0 : m_addr) + bt24;
2076
2098
2099
+ const auto block = m_ir->GetInsertBlock ();
2100
+
2077
2101
if (op.aa && m_reloc)
2078
2102
{
2079
2103
CompilationError (" Branch with absolute address" );
@@ -2085,7 +2109,7 @@ void PPUTranslator::B(ppu_opcode_t op)
2085
2109
}
2086
2110
2087
2111
FlushRegisters ();
2088
- CallFunction (target);
2112
+ CallFunction (target, nullptr , block );
2089
2113
}
2090
2114
2091
2115
void PPUTranslator::MCRF (ppu_opcode_t op)
0 commit comments