@@ -266,7 +266,7 @@ Function* PPUTranslator::Translate(const ppu_function& info)
266266 if (!m_ir->GetInsertBlock ()->getTerminator ())
267267 {
268268 FlushRegisters ();
269- CallFunction (m_addr);
269+ CallFunction (m_addr, nullptr , m_ir-> GetInsertBlock () );
270270 }
271271 }
272272
@@ -354,7 +354,7 @@ Value* PPUTranslator::RotateLeft(Value* arg, Value* n)
354354 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)));
355355}
356356
357- void PPUTranslator::CallFunction (u64 target, Value* indirect)
357+ void PPUTranslator::CallFunction (u64 target, Value* indirect, BasicBlock* prev_block )
358358{
359359 const auto type = m_function->getFunctionType ();
360360 const auto block = m_ir->GetInsertBlock ();
@@ -372,13 +372,19 @@ void PPUTranslator::CallFunction(u64 target, Value* indirect)
372372
373373 if (_target >= caddr && _target <= cend)
374374 {
375- std::unordered_set<u64 > passed_targets{_target} ;
375+ std::unordered_set<u64 > passed_targets;
376376
377377 u32 target_last = _target;
378378
379379 // 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 )
381381 {
382+ if (passed_targets.empty ())
383+ {
384+ passed_targets.emplace (_target);
385+ passed_targets.emplace (m_addr + base);
386+ }
387+
382388 const ppu_opcode_t op{*ensure (m_info.get_ptr <u32 >(target_last))};
383389 const ppu_itype::type itype = g_ppu_itype.decode (op.opcode );
384390
@@ -401,11 +407,25 @@ void PPUTranslator::CallFunction(u64 target, Value* indirect)
401407
402408 // Odd destination
403409 }
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) )
405411 {
406412 // Special case: empty function
407413 // 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+ }
409429 }
410430
411431 break ;
@@ -629,7 +649,7 @@ Value* PPUTranslator::Trunc(Value* value, Type* type)
629649 return type != value->getType () ? m_ir->CreateTrunc (value, type) : value;
630650}
631651
632- void PPUTranslator::UseCondition (MDNode* hint, Value* cond)
652+ void PPUTranslator::UseCondition (MDNode* hint, Value* cond, BasicBlock* prev_block )
633653{
634654 FlushRegisters ();
635655
@@ -639,7 +659,7 @@ void PPUTranslator::UseCondition(MDNode* hint, Value* cond)
639659 const auto next = BasicBlock::Create (m_context, " __next" , m_function);
640660 m_ir->CreateCondBr (cond, local, next, hint);
641661 m_ir->SetInsertPoint (next);
642- CallFunction (m_addr + 4 );
662+ CallFunction (m_addr + 4 , nullptr , prev_block );
643663 m_ir->SetInsertPoint (local);
644664 }
645665}
@@ -2024,19 +2044,22 @@ void PPUTranslator::BC(ppu_opcode_t op)
20242044 const s32 bt14 = op.bt14 ; // Workaround for VS 16.5
20252045 const u64 target = (op.aa ? 0 : m_addr) + bt14;
20262046
2047+ const auto block = m_ir->GetInsertBlock ();
2048+
20272049 if (op.aa && m_reloc)
20282050 {
20292051 CompilationError (" Branch with absolute address" );
20302052 }
20312053
20322054 if (op.lk )
20332055 {
2034- m_ir->CreateStore (GetAddr (+4 ), m_ir->CreateStructGEP (m_thread_type, m_thread, static_cast <uint>(&m_lr - m_locals)));
2056+ m_lr = GetAddr (+4 );
2057+ m_ir->CreateStore (m_lr, m_ir->CreateStructGEP (m_thread_type, m_thread, static_cast <uint>(&m_lr - m_locals)));
20352058 }
20362059
2037- UseCondition (CheckBranchProbability (op.bo ), CheckBranchCondition (op.bo , op.bi ));
2060+ UseCondition (CheckBranchProbability (op.bo ), CheckBranchCondition (op.bo , op.bi ), block );
20382061
2039- CallFunction (target);
2062+ CallFunction (target, nullptr , block );
20402063}
20412064
20422065void PPUTranslator::SC (ppu_opcode_t op)
@@ -2074,6 +2097,8 @@ void PPUTranslator::B(ppu_opcode_t op)
20742097 const s32 bt24 = op.bt24 ; // Workaround for VS 16.5
20752098 const u64 target = (op.aa ? 0 : m_addr) + bt24;
20762099
2100+ const auto block = m_ir->GetInsertBlock ();
2101+
20772102 if (op.aa && m_reloc)
20782103 {
20792104 CompilationError (" Branch with absolute address" );
@@ -2085,7 +2110,7 @@ void PPUTranslator::B(ppu_opcode_t op)
20852110 }
20862111
20872112 FlushRegisters ();
2088- CallFunction (target);
2113+ CallFunction (target, nullptr , block );
20892114}
20902115
20912116void PPUTranslator::MCRF (ppu_opcode_t op)
@@ -2103,7 +2128,8 @@ void PPUTranslator::BCLR(ppu_opcode_t op)
21032128
21042129 if (op.lk )
21052130 {
2106- m_ir->CreateStore (GetAddr (+4 ), m_ir->CreateStructGEP (m_thread_type, m_thread, static_cast <uint>(&m_lr - m_locals)));
2131+ m_lr = GetAddr (+4 );
2132+ m_ir->CreateStore (m_lr, m_ir->CreateStructGEP (m_thread_type, m_thread, static_cast <uint>(&m_lr - m_locals)));
21072133 }
21082134
21092135 UseCondition (CheckBranchProbability (op.bo ), CheckBranchCondition (op.bo , op.bi ));
@@ -2166,7 +2192,8 @@ void PPUTranslator::BCCTR(ppu_opcode_t op)
21662192
21672193 if (op.lk )
21682194 {
2169- m_ir->CreateStore (GetAddr (+4 ), m_ir->CreateStructGEP (m_thread_type, m_thread, static_cast <uint>(&m_lr - m_locals)));
2195+ m_lr = GetAddr (+4 );
2196+ m_ir->CreateStore (m_lr, m_ir->CreateStructGEP (m_thread_type, m_thread, static_cast <uint>(&m_lr - m_locals)));
21702197 }
21712198
21722199 UseCondition (CheckBranchProbability (op.bo | 0x4 ), CheckBranchCondition (op.bo | 0x4 , op.bi ));
0 commit comments