Skip to content

Commit 6b7770b

Browse files
committed
Add expectations for list index in range branches
1 parent cdfacf7 commit 6b7770b

File tree

1 file changed

+14
-3
lines changed
  • src/engine/internal/llvm/instructions

1 file changed

+14
-3
lines changed

src/engine/internal/llvm/instructions/lists.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,21 +364,32 @@ LLVMInstruction *Lists::buildListContainsItem(LLVMInstruction *ins)
364364

365365
llvm::Value *Lists::getIndex(const LLVMListPtr &listPtr, llvm::Value *indexDouble)
366366
{
367+
llvm::Function *expectIntrinsic = llvm::Intrinsic::getDeclaration(m_utils.module(), llvm::Intrinsic::expect, m_builder.getInt64Ty());
368+
367369
llvm::Value *zero = llvm::ConstantFP::get(m_utils.llvmCtx(), llvm::APFloat(0.0));
368370
llvm::Value *isNegative = m_builder.CreateFCmpOLT(indexDouble, zero, "listIndex.isNegative");
369371
llvm::Value *intMax = llvm::ConstantInt::get(m_builder.getInt64Ty(), INT64_MAX);
370372
llvm::Value *intIndex = m_builder.CreateFPToUI(indexDouble, m_builder.getInt64Ty(), "listIndex.int");
371-
return m_builder.CreateSelect(isNegative, intMax, intIndex);
373+
374+
// Tell the optimizer that negative indices are uncommon
375+
llvm::Value *index = m_builder.CreateSelect(isNegative, intMax, intIndex);
376+
return m_builder.CreateCall(expectIntrinsic, { index, intIndex });
372377
}
373378

374379
llvm::Value *Lists::createSizeRangeCheck(const LLVMListPtr &listPtr, llvm::Value *indexInt, const std::string &name, bool includeSize)
375380
{
381+
llvm::Function *expectIntrinsic = llvm::Intrinsic::getDeclaration(m_utils.module(), llvm::Intrinsic::expect, m_builder.getInt1Ty());
382+
376383
llvm::Value *size = m_utils.getListSize(listPtr);
384+
llvm::Value *inRange;
377385

378386
if (includeSize)
379-
return m_builder.CreateICmpULE(indexInt, size, name);
387+
inRange = m_builder.CreateICmpULE(indexInt, size, name);
380388
else
381-
return m_builder.CreateICmpULT(indexInt, size, name);
389+
inRange = m_builder.CreateICmpULT(indexInt, size, name);
390+
391+
// Tell the optimizer that indices in range are more common
392+
return m_builder.CreateCall(expectIntrinsic, { inRange, m_builder.getInt1(true) });
382393
}
383394

384395
void Lists::createListTypeUpdate(const LLVMListPtr &listPtr, const LLVMRegister *newValue, Compiler::StaticType newValueType)

0 commit comments

Comments
 (0)