From d62088e5403f6ed7e61dcf3b5dc63eb5c1b29423 Mon Sep 17 00:00:00 2001 From: Andrew Craik Date: Tue, 15 Oct 2019 11:26:00 -0400 Subject: [PATCH] Enlarge SwitchAnalyzer successor counter The switch analyzer currently uses an int8_t counter to count the number of time a given block is a successor of the swtich/table. This can easily overflow and result in undefined behaviour by the C++ code. This change enlarges the compiler to int32_t to prevent this kind of overflow and improves the assert to ensure a sesnible number is being generated. Signed-off-by: Andrew Craik --- compiler/optimizer/SwitchAnalyzer.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/optimizer/SwitchAnalyzer.cpp b/compiler/optimizer/SwitchAnalyzer.cpp index eadf6ea3f01..f4707183c5a 100644 --- a/compiler/optimizer/SwitchAnalyzer.cpp +++ b/compiler/optimizer/SwitchAnalyzer.cpp @@ -1322,8 +1322,8 @@ int32_t *TR::SwitchAnalyzer::setupFrequencies(TR::Node *node) { if (!_haveProfilingInfo) return 0; - int8_t *targetCounts = (int8_t*) trMemory()->allocateStackMemory(_cfg->getNextNodeNumber() * sizeof(int8_t)); - memset (targetCounts, 0, sizeof(int8_t) * _cfg->getNextNodeNumber()); + int32_t *targetCounts = (int32_t*) trMemory()->allocateStackMemory(_cfg->getNextNodeNumber() * sizeof(int32_t)); + memset (targetCounts, 0, sizeof(int32_t) * _cfg->getNextNodeNumber()); int32_t *frequencies = (int32_t *) trMemory()->allocateStackMemory(node->getCaseIndexUpperBound() * sizeof(int32_t)); memset (frequencies, 0, sizeof(int32_t) * node->getCaseIndexUpperBound()); @@ -1343,7 +1343,7 @@ int32_t *TR::SwitchAnalyzer::setupFrequencies(TR::Node *node) TR::Node *caseNode = node->getChild(i); TR::Block *targetBlock = caseNode->getBranchDestination()->getNode()->getBlock(); int32_t targetCount = targetCounts[targetBlock->getNumber()]; - TR_ASSERT(targetCount != 0, "unreachle successor of switch statement"); + TR_ASSERT_FATAL(targetCount > 0, "Successor block_%d of switch statement has non-sense successsor count", targetBlock->getNumber()); int32_t frequency = targetBlock->getFrequency() / targetCount; frequencies[i] = frequency;