[SLP] Report the correct operand to getArithmeticInstrCost() when duplicated scalars (#174442)
Before, we were selecting the wrong operand in cases when Scalars
contained duplicate values. Stems from #135797.
Using:
`opt -passes=slp-vectorizer -mtriple=riscv64 -mattr=+v t.ll`
```
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
target triple = "riscv64"
define void @foo(ptr noalias %A, ptr noalias %B) {
entry:
%0 = load i32, ptr %B
%add = add nsw i32 %0, 1
store i32 %add, ptr %A
%arrayidx.1 = getelementptr inbounds nuw i8, ptr %B, i64 4
%1 = load i32, ptr %arrayidx.1
%add.1 = add nsw i32 %1, 1
%arrayidx2.1 = getelementptr inbounds nuw i8, ptr %A, i64 4
store i32 %add.1, ptr %arrayidx2.1
%arrayidx.2 = getelementptr inbounds nuw i8, ptr %B, i64 8
%2 = load i32, ptr %arrayidx.2
%add.2 = add nsw i32 %2, 1
%arrayidx2.2 = getelementptr inbounds nuw i8, ptr %A, i64 8
store i32 %add.2, ptr %arrayidx2.2
%arrayidx.3 = getelementptr inbounds nuw i8, ptr %B, i64 12
%arrayidx2.3 = getelementptr inbounds nuw i8, ptr %A, i64 12
store i32 %add, ptr %arrayidx2.3
%arrayidx.4 = getelementptr inbounds nuw i8, ptr %B, i64 16
%4 = load i32, ptr %arrayidx.4
%add.4 = add nsw i32 %4, 1
%arrayidx2.4 = getelementptr inbounds nuw i8, ptr %A, i64 16
store i32 %add.4, ptr %arrayidx2.4
%arrayidx.5 = getelementptr inbounds nuw i8, ptr %B, i64 20
%5 = load i32, ptr %arrayidx.5
%add.5 = add nsw i32 %5, 1
%arrayidx2.5 = getelementptr inbounds nuw i8, ptr %A, i64 20
store i32 %add.5, ptr %arrayidx2.5
%arrayidx.6 = getelementptr inbounds nuw i8, ptr %B, i64 24
%6 = load i32, ptr %arrayidx.6
%add.6 = add nsw i32 %6, 1
%arrayidx2.6 = getelementptr inbounds nuw i8, ptr %A, i64 24
store i32 %add.6, ptr %arrayidx2.6
%arrayidx.7 = getelementptr inbounds nuw i8, ptr %B, i64 28
%7 = load i32, ptr %arrayidx.7
%add.7 = add nsw i32 %7, 1
%arrayidx2.7 = getelementptr inbounds nuw i8, ptr %A, i64 28
store i32 %add.7, ptr %arrayidx2.7
ret void
}
```
The following trace is produced, note the wrong operand is used for `Idx
> 2`
Before:
```
GetScalarCost(), Idx=0
UniqueValues[Idx]: %add = add nsw i32 %0, 1
Op1: %0 = load i32, ptr %B, align 4
GetScalarCost(), Idx=1
UniqueValues[Idx]: %add.1 = add nsw i32 %1, 1
Op1: %1 = load i32, ptr %arrayidx.1, align 4
GetScalarCost(), Idx=2
UniqueValues[Idx]: %add.2 = add nsw i32 %2, 1
Op1: %2 = load i32, ptr %arrayidx.2, align 4
GetScalarCost(), Idx=3
UniqueValues[Idx]: %add.4 = add nsw i32 %3, 1
Op1: %0 = load i32, ptr %B, align 4
GetScalarCost(), Idx=4
UniqueValues[Idx]: %add.5 = add nsw i32 %4, 1
Op1: %3 = load i32, ptr %arrayidx.4, align 4
GetScalarCost(), Idx=5
UniqueValues[Idx]: %add.6 = add nsw i32 %5, 1
Op1: %4 = load i32, ptr %arrayidx.5, align 4
GetScalarCost(), Idx=6
UniqueValues[Idx]: %add.7 = add nsw i32 %6, 1
Op1: %5 = load i32, ptr %arrayidx.6, align 4
```
After:
```
GetScalarCost(), Idx=0
UniqueValues[Idx]: %add = add nsw i32 %0, 1
Op1: %0 = load i32, ptr %B, align 4
GetScalarCost(), Idx=1
UniqueValues[Idx]: %add.1 = add nsw i32 %1, 1
Op1: %1 = load i32, ptr %arrayidx.1, align 4
GetScalarCost(), Idx=2
UniqueValues[Idx]: %add.2 = add nsw i32 %2, 1
Op1: %2 = load i32, ptr %arrayidx.2, align 4
GetScalarCost(), Idx=3
UniqueValues[Idx]: %add.4 = add nsw i32 %3, 1
Op1: %3 = load i32, ptr %arrayidx.4, align 4
GetScalarCost(), Idx=4
UniqueValues[Idx]: %add.5 = add nsw i32 %4, 1
Op1: %4 = load i32, ptr %arrayidx.5, align 4
GetScalarCost(), Idx=5
UniqueValues[Idx]: %add.6 = add nsw i32 %5, 1
Op1: %5 = load i32, ptr %arrayidx.6, align 4
GetScalarCost(), Idx=6
UniqueValues[Idx]: %add.7 = add nsw i32 %6, 1
Op1: %6 = load i32, ptr %arrayidx.7, align 4
```Welcome to the LLVM project!
This repository contains the source code for LLVM, a toolkit for the construction of highly optimized compilers, optimizers, and run-time environments.
The LLVM project has multiple components. The core of the project is itself called “LLVM”. This contains all of the tools, libraries, and header files needed to process intermediate representations and convert them into object files. Tools include an assembler, disassembler, bitcode analyzer, and bitcode optimizer.
C-like languages use the Clang frontend. This component compiles C, C++, Objective-C, and Objective-C++ code into LLVM bitcode -- and from there into object files, using LLVM.
Other components include: the libc++ C++ standard library, the LLD linker, and more.
Consult the Getting Started with LLVM page for information on building and running LLVM.
For information on how to contribute to the LLVM project, please take a look at the Contributing to LLVM guide.
Join the LLVM Discourse forums, Discord chat, LLVM Office Hours or Regular sync-ups.
The LLVM project has adopted a code of conduct for participants to all modes of communication within the project.