@@ -364,21 +364,32 @@ LLVMInstruction *Lists::buildListContainsItem(LLVMInstruction *ins)
364364
365365llvm::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
374379llvm::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
384395void Lists::createListTypeUpdate (const LLVMListPtr &listPtr, const LLVMRegister *newValue, Compiler::StaticType newValueType)
0 commit comments