99using namespace HLSLcc ::ControlFlow;
1010using HLSLcc::ForEachOperand;
1111
12- const BasicBlock &ControlFlowGraph::Build (const Instruction * firstInstruction)
12+ const BasicBlock &ControlFlowGraph::Build (const Instruction* firstInstruction, const Instruction* endInstruction )
1313{
1414 using std::for_each;
1515
1616 m_BlockMap.clear ();
1717 m_BlockStorage.clear ();
1818
1919 // Self-registering into m_BlockStorage so it goes out of the scope when ControlFlowGraph does
20- BasicBlock *root = new BasicBlock (Utils::GetNextNonLabelInstruction (firstInstruction), *this , NULL );
20+ BasicBlock *root = new BasicBlock (Utils::GetNextNonLabelInstruction (firstInstruction), *this , NULL , endInstruction );
2121
2222 // Build the reachable set for each block
2323 bool hadChanges;
@@ -58,10 +58,11 @@ BasicBlock *ControlFlowGraph::GetBasicBlockForInstruction(const Instruction *ins
5858
5959// Generate a basic block. Private constructor, can only be constructed from ControlFlowGraph::Build().
6060// Auto-registers itself into ControlFlowGraph
61- BasicBlock::BasicBlock (const Instruction *psFirst, ControlFlowGraph &graph, const Instruction *psPrecedingBlockHead)
61+ BasicBlock::BasicBlock (const Instruction *psFirst, ControlFlowGraph &graph, const Instruction *psPrecedingBlockHead, const Instruction* endInstruction )
6262 : m_Graph(graph)
6363 , m_First(psFirst)
6464 , m_Last(NULL )
65+ , m_End(endInstruction)
6566{
6667 m_UEVar.clear ();
6768 m_VarKill.clear ();
@@ -94,7 +95,7 @@ BasicBlock::BasicBlock(const Instruction *psFirst, ControlFlowGraph &graph, cons
9495void BasicBlock::Build ()
9596{
9697 const Instruction *inst = m_First;
97- while (1 )
98+ while (inst != m_End )
9899 {
99100 // Process sources first
100101 ForEachOperand (inst, inst + 1 , FEO_FLAG_SRC_OPERAND | FEO_FLAG_SUBOPERAND,
@@ -158,7 +159,8 @@ void BasicBlock::Build()
158159 default :
159160 break ;
160161 case OPCODE_RET:
161- blockDone = true ;
162+ // Continue processing, in the case of unreachable code we still need to translate it properly (case 1160309)
163+ // blockDone = true;
162164 break ;
163165 case OPCODE_RETC:
164166 // Basic block is done, start a next one.
@@ -240,7 +242,7 @@ void BasicBlock::Build()
240242 m_Reachable = m_DEDef;
241243
242244 // Tag the end of the basic block
243- m_Last = inst;
245+ m_Last = std::max (m_First, std::min ( inst, m_End - 1 )) ;
244246// printf("Basic Block %d -> %d\n", (int)m_First->id, (int)m_Last->id);
245247}
246248
@@ -256,7 +258,7 @@ BasicBlock * BasicBlock::AddChildBasicBlock(const Instruction *psFirst)
256258 return b;
257259 }
258260 // Otherwise create one. Self-registering and self-connecting
259- return new BasicBlock (psFirst, m_Graph, m_First);
261+ return new BasicBlock (psFirst, m_Graph, m_First, m_End );
260262}
261263
262264bool BasicBlock::RebuildReachable ()
@@ -334,6 +336,7 @@ void BasicBlock::RVarUnion(ReachableVariables &a, const ReachableVariables &b)
334336#if ENABLE_UNIT_TESTS
335337
336338#define UNITY_EXTERNAL_TOOL 1
339+ #include " Projects/PrecompiledHeaders/UnityPrefix.h" // Needed for defines such as ENABLE_CPP_EXCEPTIONS
337340#include " Testing.h" // From Runtime/Testing
338341
339342UNIT_TEST_SUITE (HLSLcc)
@@ -348,7 +351,7 @@ UNIT_TEST_SUITE(HLSLcc)
348351 };
349352
350353 ControlFlowGraph cfg;
351- const BasicBlock &root = cfg.Build (inst);
354+ const BasicBlock &root = cfg.Build (inst, inst + ARRAY_SIZE (inst) );
352355
353356 CHECK_EQUAL (&inst[0 ], root.First ());
354357 CHECK_EQUAL (&inst[1 ], root.Last ());
@@ -403,7 +406,7 @@ UNIT_TEST_SUITE(HLSLcc)
403406 };
404407
405408 ControlFlowGraph cfg;
406- const BasicBlock &root = cfg.Build (inst);
409+ const BasicBlock &root = cfg.Build (inst, inst + ARRAY_SIZE (inst) );
407410
408411 CHECK_EQUAL (root.First (), &inst[0 ]);
409412 CHECK_EQUAL (root.Last (), &inst[2 ]);
@@ -539,7 +542,7 @@ UNIT_TEST_SUITE(HLSLcc)
539542 };
540543
541544 ControlFlowGraph cfg;
542- const BasicBlock &root = cfg.Build (inst);
545+ const BasicBlock &root = cfg.Build (inst, inst + ARRAY_SIZE (inst) );
543546
544547 CHECK_EQUAL (&inst[0 ], root.First ());
545548 CHECK_EQUAL (&inst[4 ], root.Last ());
@@ -699,7 +702,7 @@ UNIT_TEST_SUITE(HLSLcc)
699702 };
700703
701704 ControlFlowGraph cfg;
702- const BasicBlock &root = cfg.Build (inst);
705+ const BasicBlock &root = cfg.Build (inst, inst + ARRAY_SIZE (inst) );
703706
704707 CHECK_EQUAL (&inst[0 ], root.First ());
705708 CHECK_EQUAL (&inst[2 ], root.Last ());
0 commit comments