1 //===- ARMTargetTransformInfo.cpp - ARM specific TTI ----------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "ARMTargetTransformInfo.h" 10 #include "ARMSubtarget.h" 11 #include "MCTargetDesc/ARMAddressingModes.h" 12 #include "llvm/ADT/APInt.h" 13 #include "llvm/ADT/SmallVector.h" 14 #include "llvm/Analysis/LoopInfo.h" 15 #include "llvm/CodeGen/CostTable.h" 16 #include "llvm/CodeGen/ISDOpcodes.h" 17 #include "llvm/CodeGen/ValueTypes.h" 18 #include "llvm/IR/BasicBlock.h" 19 #include "llvm/IR/DataLayout.h" 20 #include "llvm/IR/DerivedTypes.h" 21 #include "llvm/IR/Instruction.h" 22 #include "llvm/IR/Instructions.h" 23 #include "llvm/IR/IntrinsicInst.h" 24 #include "llvm/IR/IntrinsicsARM.h" 25 #include "llvm/IR/PatternMatch.h" 26 #include "llvm/IR/Type.h" 27 #include "llvm/MC/SubtargetFeature.h" 28 #include "llvm/Support/Casting.h" 29 #include "llvm/Support/MachineValueType.h" 30 #include "llvm/Target/TargetMachine.h" 31 #include <algorithm> 32 #include <cassert> 33 #include <cstdint> 34 #include <utility> 35 36 using namespace llvm; 37 38 #define DEBUG_TYPE "armtti" 39 40 static cl::opt<bool> EnableMaskedLoadStores( 41 "enable-arm-maskedldst", cl::Hidden, cl::init(true), 42 cl::desc("Enable the generation of masked loads and stores")); 43 44 static cl::opt<bool> DisableLowOverheadLoops( 45 "disable-arm-loloops", cl::Hidden, cl::init(false), 46 cl::desc("Disable the generation of low-overhead loops")); 47 48 extern cl::opt<bool> DisableTailPredication; 49 50 extern cl::opt<bool> EnableMaskedGatherScatters; 51 52 bool ARMTTIImpl::areInlineCompatible(const Function *Caller, 53 const Function *Callee) const { 54 const TargetMachine &TM = getTLI()->getTargetMachine(); 55 const FeatureBitset &CallerBits = 56 TM.getSubtargetImpl(*Caller)->getFeatureBits(); 57 const FeatureBitset &CalleeBits = 58 TM.getSubtargetImpl(*Callee)->getFeatureBits(); 59 60 // To inline a callee, all features not in the allowed list must match exactly. 61 bool MatchExact = (CallerBits & ~InlineFeaturesAllowed) == 62 (CalleeBits & ~InlineFeaturesAllowed); 63 // For features in the allowed list, the callee's features must be a subset of 64 // the callers'. 65 bool MatchSubset = ((CallerBits & CalleeBits) & InlineFeaturesAllowed) == 66 (CalleeBits & InlineFeaturesAllowed); 67 return MatchExact && MatchSubset; 68 } 69 70 bool ARMTTIImpl::shouldFavorBackedgeIndex(const Loop *L) const { 71 if (L->getHeader()->getParent()->hasOptSize()) 72 return false; 73 if (ST->hasMVEIntegerOps()) 74 return false; 75 return ST->isMClass() && ST->isThumb2() && L->getNumBlocks() == 1; 76 } 77 78 bool ARMTTIImpl::shouldFavorPostInc() const { 79 if (ST->hasMVEIntegerOps()) 80 return true; 81 return false; 82 } 83 84 int ARMTTIImpl::getIntImmCost(const APInt &Imm, Type *Ty, 85 TTI::TargetCostKind CostKind) { 86 assert(Ty->isIntegerTy()); 87 88 unsigned Bits = Ty->getPrimitiveSizeInBits(); 89 if (Bits == 0 || Imm.getActiveBits() >= 64) 90 return 4; 91 92 int64_t SImmVal = Imm.getSExtValue(); 93 uint64_t ZImmVal = Imm.getZExtValue(); 94 if (!ST->isThumb()) { 95 if ((SImmVal >= 0 && SImmVal < 65536) || 96 (ARM_AM::getSOImmVal(ZImmVal) != -1) || 97 (ARM_AM::getSOImmVal(~ZImmVal) != -1)) 98 return 1; 99 return ST->hasV6T2Ops() ? 2 : 3; 100 } 101 if (ST->isThumb2()) { 102 if ((SImmVal >= 0 && SImmVal < 65536) || 103 (ARM_AM::getT2SOImmVal(ZImmVal) != -1) || 104 (ARM_AM::getT2SOImmVal(~ZImmVal) != -1)) 105 return 1; 106 return ST->hasV6T2Ops() ? 2 : 3; 107 } 108 // Thumb1, any i8 imm cost 1. 109 if (Bits == 8 || (SImmVal >= 0 && SImmVal < 256)) 110 return 1; 111 if ((~SImmVal < 256) || ARM_AM::isThumbImmShiftedVal(ZImmVal)) 112 return 2; 113 // Load from constantpool. 114 return 3; 115 } 116 117 // Constants smaller than 256 fit in the immediate field of 118 // Thumb1 instructions so we return a zero cost and 1 otherwise. 119 int ARMTTIImpl::getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx, 120 const APInt &Imm, Type *Ty) { 121 if (Imm.isNonNegative() && Imm.getLimitedValue() < 256) 122 return 0; 123 124 return 1; 125 } 126 127 int ARMTTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx, const APInt &Imm, 128 Type *Ty, TTI::TargetCostKind CostKind) { 129 // Division by a constant can be turned into multiplication, but only if we 130 // know it's constant. So it's not so much that the immediate is cheap (it's 131 // not), but that the alternative is worse. 132 // FIXME: this is probably unneeded with GlobalISel. 133 if ((Opcode == Instruction::SDiv || Opcode == Instruction::UDiv || 134 Opcode == Instruction::SRem || Opcode == Instruction::URem) && 135 Idx == 1) 136 return 0; 137 138 if (Opcode == Instruction::And) { 139 // UXTB/UXTH 140 if (Imm == 255 || Imm == 65535) 141 return 0; 142 // Conversion to BIC is free, and means we can use ~Imm instead. 143 return std::min(getIntImmCost(Imm, Ty, CostKind), 144 getIntImmCost(~Imm, Ty, CostKind)); 145 } 146 147 if (Opcode == Instruction::Add) 148 // Conversion to SUB is free, and means we can use -Imm instead. 149 return std::min(getIntImmCost(Imm, Ty, CostKind), 150 getIntImmCost(-Imm, Ty, CostKind)); 151 152 if (Opcode == Instruction::ICmp && Imm.isNegative() && 153 Ty->getIntegerBitWidth() == 32) { 154 int64_t NegImm = -Imm.getSExtValue(); 155 if (ST->isThumb2() && NegImm < 1<<12) 156 // icmp X, #-C -> cmn X, #C 157 return 0; 158 if (ST->isThumb() && NegImm < 1<<8) 159 // icmp X, #-C -> adds X, #C 160 return 0; 161 } 162 163 // xor a, -1 can always be folded to MVN 164 if (Opcode == Instruction::Xor && Imm.isAllOnesValue()) 165 return 0; 166 167 return getIntImmCost(Imm, Ty, CostKind); 168 } 169 170 int ARMTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src, 171 TTI::TargetCostKind CostKind, 172 const Instruction *I) { 173 int ISD = TLI->InstructionOpcodeToISD(Opcode); 174 assert(ISD && "Invalid opcode"); 175 176 // TODO: Allow non-throughput costs that aren't binary. 177 auto AdjustCost = [&CostKind](int Cost) { 178 if (CostKind != TTI::TCK_RecipThroughput) 179 return Cost == 0 ? 0 : 1; 180 return Cost; 181 }; 182 183 // Single to/from double precision conversions. 184 static const CostTblEntry NEONFltDblTbl[] = { 185 // Vector fptrunc/fpext conversions. 186 { ISD::FP_ROUND, MVT::v2f64, 2 }, 187 { ISD::FP_EXTEND, MVT::v2f32, 2 }, 188 { ISD::FP_EXTEND, MVT::v4f32, 4 } 189 }; 190 191 if (Src->isVectorTy() && ST->hasNEON() && (ISD == ISD::FP_ROUND || 192 ISD == ISD::FP_EXTEND)) { 193 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src); 194 if (const auto *Entry = CostTableLookup(NEONFltDblTbl, ISD, LT.second)) 195 return AdjustCost(LT.first * Entry->Cost); 196 } 197 198 EVT SrcTy = TLI->getValueType(DL, Src); 199 EVT DstTy = TLI->getValueType(DL, Dst); 200 201 if (!SrcTy.isSimple() || !DstTy.isSimple()) 202 return AdjustCost(BaseT::getCastInstrCost(Opcode, Dst, Src, CostKind, I)); 203 204 // The extend of a load is free 205 if (I && isa<LoadInst>(I->getOperand(0))) { 206 static const TypeConversionCostTblEntry LoadConversionTbl[] = { 207 {ISD::SIGN_EXTEND, MVT::i32, MVT::i16, 0}, 208 {ISD::ZERO_EXTEND, MVT::i32, MVT::i16, 0}, 209 {ISD::SIGN_EXTEND, MVT::i32, MVT::i8, 0}, 210 {ISD::ZERO_EXTEND, MVT::i32, MVT::i8, 0}, 211 {ISD::SIGN_EXTEND, MVT::i16, MVT::i8, 0}, 212 {ISD::ZERO_EXTEND, MVT::i16, MVT::i8, 0}, 213 {ISD::SIGN_EXTEND, MVT::i64, MVT::i32, 1}, 214 {ISD::ZERO_EXTEND, MVT::i64, MVT::i32, 1}, 215 {ISD::SIGN_EXTEND, MVT::i64, MVT::i16, 1}, 216 {ISD::ZERO_EXTEND, MVT::i64, MVT::i16, 1}, 217 {ISD::SIGN_EXTEND, MVT::i64, MVT::i8, 1}, 218 {ISD::ZERO_EXTEND, MVT::i64, MVT::i8, 1}, 219 }; 220 if (const auto *Entry = ConvertCostTableLookup( 221 LoadConversionTbl, ISD, DstTy.getSimpleVT(), SrcTy.getSimpleVT())) 222 return AdjustCost(Entry->Cost); 223 224 static const TypeConversionCostTblEntry MVELoadConversionTbl[] = { 225 {ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i16, 0}, 226 {ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i16, 0}, 227 {ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i8, 0}, 228 {ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i8, 0}, 229 {ISD::SIGN_EXTEND, MVT::v8i16, MVT::v8i8, 0}, 230 {ISD::ZERO_EXTEND, MVT::v8i16, MVT::v8i8, 0}, 231 }; 232 if (SrcTy.isVector() && ST->hasMVEIntegerOps()) { 233 if (const auto *Entry = 234 ConvertCostTableLookup(MVELoadConversionTbl, ISD, 235 DstTy.getSimpleVT(), SrcTy.getSimpleVT())) 236 return AdjustCost(Entry->Cost); 237 } 238 } 239 240 // NEON vector operations that can extend their inputs. 241 if ((ISD == ISD::SIGN_EXTEND || ISD == ISD::ZERO_EXTEND) && 242 I && I->hasOneUse() && ST->hasNEON() && SrcTy.isVector()) { 243 static const TypeConversionCostTblEntry NEONDoubleWidthTbl[] = { 244 // vaddl 245 { ISD::ADD, MVT::v4i32, MVT::v4i16, 0 }, 246 { ISD::ADD, MVT::v8i16, MVT::v8i8, 0 }, 247 // vsubl 248 { ISD::SUB, MVT::v4i32, MVT::v4i16, 0 }, 249 { ISD::SUB, MVT::v8i16, MVT::v8i8, 0 }, 250 // vmull 251 { ISD::MUL, MVT::v4i32, MVT::v4i16, 0 }, 252 { ISD::MUL, MVT::v8i16, MVT::v8i8, 0 }, 253 // vshll 254 { ISD::SHL, MVT::v4i32, MVT::v4i16, 0 }, 255 { ISD::SHL, MVT::v8i16, MVT::v8i8, 0 }, 256 }; 257 258 auto *User = cast<Instruction>(*I->user_begin()); 259 int UserISD = TLI->InstructionOpcodeToISD(User->getOpcode()); 260 if (auto *Entry = ConvertCostTableLookup(NEONDoubleWidthTbl, UserISD, 261 DstTy.getSimpleVT(), 262 SrcTy.getSimpleVT())) { 263 return AdjustCost(Entry->Cost); 264 } 265 } 266 267 // Some arithmetic, load and store operations have specific instructions 268 // to cast up/down their types automatically at no extra cost. 269 // TODO: Get these tables to know at least what the related operations are. 270 static const TypeConversionCostTblEntry NEONVectorConversionTbl[] = { 271 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i16, 1 }, 272 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i16, 1 }, 273 { ISD::SIGN_EXTEND, MVT::v2i64, MVT::v2i32, 1 }, 274 { ISD::ZERO_EXTEND, MVT::v2i64, MVT::v2i32, 1 }, 275 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 0 }, 276 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i32, 1 }, 277 278 // The number of vmovl instructions for the extension. 279 { ISD::SIGN_EXTEND, MVT::v8i16, MVT::v8i8, 1 }, 280 { ISD::ZERO_EXTEND, MVT::v8i16, MVT::v8i8, 1 }, 281 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i8, 2 }, 282 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i8, 2 }, 283 { ISD::SIGN_EXTEND, MVT::v2i64, MVT::v2i8, 3 }, 284 { ISD::ZERO_EXTEND, MVT::v2i64, MVT::v2i8, 3 }, 285 { ISD::SIGN_EXTEND, MVT::v2i64, MVT::v2i16, 2 }, 286 { ISD::ZERO_EXTEND, MVT::v2i64, MVT::v2i16, 2 }, 287 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 3 }, 288 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 }, 289 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 3 }, 290 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 3 }, 291 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i8, 7 }, 292 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i8, 7 }, 293 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i16, 6 }, 294 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i16, 6 }, 295 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 6 }, 296 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 6 }, 297 298 // Operations that we legalize using splitting. 299 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i32, 6 }, 300 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 3 }, 301 302 // Vector float <-> i32 conversions. 303 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 }, 304 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 }, 305 306 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i8, 3 }, 307 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i8, 3 }, 308 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i16, 2 }, 309 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i16, 2 }, 310 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i32, 1 }, 311 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i32, 1 }, 312 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i1, 3 }, 313 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i1, 3 }, 314 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 }, 315 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 }, 316 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 }, 317 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 }, 318 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i16, 4 }, 319 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 4 }, 320 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i32, 2 }, 321 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 2 }, 322 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i16, 8 }, 323 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i16, 8 }, 324 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i32, 4 }, 325 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i32, 4 }, 326 327 { ISD::FP_TO_SINT, MVT::v4i32, MVT::v4f32, 1 }, 328 { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f32, 1 }, 329 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 3 }, 330 { ISD::FP_TO_UINT, MVT::v4i8, MVT::v4f32, 3 }, 331 { ISD::FP_TO_SINT, MVT::v4i16, MVT::v4f32, 2 }, 332 { ISD::FP_TO_UINT, MVT::v4i16, MVT::v4f32, 2 }, 333 334 // Vector double <-> i32 conversions. 335 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 }, 336 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 }, 337 338 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i8, 4 }, 339 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i8, 4 }, 340 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i16, 3 }, 341 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i16, 3 }, 342 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 }, 343 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 }, 344 345 { ISD::FP_TO_SINT, MVT::v2i32, MVT::v2f64, 2 }, 346 { ISD::FP_TO_UINT, MVT::v2i32, MVT::v2f64, 2 }, 347 { ISD::FP_TO_SINT, MVT::v8i16, MVT::v8f32, 4 }, 348 { ISD::FP_TO_UINT, MVT::v8i16, MVT::v8f32, 4 }, 349 { ISD::FP_TO_SINT, MVT::v16i16, MVT::v16f32, 8 }, 350 { ISD::FP_TO_UINT, MVT::v16i16, MVT::v16f32, 8 } 351 }; 352 353 if (SrcTy.isVector() && ST->hasNEON()) { 354 if (const auto *Entry = ConvertCostTableLookup(NEONVectorConversionTbl, ISD, 355 DstTy.getSimpleVT(), 356 SrcTy.getSimpleVT())) 357 return AdjustCost(Entry->Cost); 358 } 359 360 // Scalar float to integer conversions. 361 static const TypeConversionCostTblEntry NEONFloatConversionTbl[] = { 362 { ISD::FP_TO_SINT, MVT::i1, MVT::f32, 2 }, 363 { ISD::FP_TO_UINT, MVT::i1, MVT::f32, 2 }, 364 { ISD::FP_TO_SINT, MVT::i1, MVT::f64, 2 }, 365 { ISD::FP_TO_UINT, MVT::i1, MVT::f64, 2 }, 366 { ISD::FP_TO_SINT, MVT::i8, MVT::f32, 2 }, 367 { ISD::FP_TO_UINT, MVT::i8, MVT::f32, 2 }, 368 { ISD::FP_TO_SINT, MVT::i8, MVT::f64, 2 }, 369 { ISD::FP_TO_UINT, MVT::i8, MVT::f64, 2 }, 370 { ISD::FP_TO_SINT, MVT::i16, MVT::f32, 2 }, 371 { ISD::FP_TO_UINT, MVT::i16, MVT::f32, 2 }, 372 { ISD::FP_TO_SINT, MVT::i16, MVT::f64, 2 }, 373 { ISD::FP_TO_UINT, MVT::i16, MVT::f64, 2 }, 374 { ISD::FP_TO_SINT, MVT::i32, MVT::f32, 2 }, 375 { ISD::FP_TO_UINT, MVT::i32, MVT::f32, 2 }, 376 { ISD::FP_TO_SINT, MVT::i32, MVT::f64, 2 }, 377 { ISD::FP_TO_UINT, MVT::i32, MVT::f64, 2 }, 378 { ISD::FP_TO_SINT, MVT::i64, MVT::f32, 10 }, 379 { ISD::FP_TO_UINT, MVT::i64, MVT::f32, 10 }, 380 { ISD::FP_TO_SINT, MVT::i64, MVT::f64, 10 }, 381 { ISD::FP_TO_UINT, MVT::i64, MVT::f64, 10 } 382 }; 383 if (SrcTy.isFloatingPoint() && ST->hasNEON()) { 384 if (const auto *Entry = ConvertCostTableLookup(NEONFloatConversionTbl, ISD, 385 DstTy.getSimpleVT(), 386 SrcTy.getSimpleVT())) 387 return AdjustCost(Entry->Cost); 388 } 389 390 // Scalar integer to float conversions. 391 static const TypeConversionCostTblEntry NEONIntegerConversionTbl[] = { 392 { ISD::SINT_TO_FP, MVT::f32, MVT::i1, 2 }, 393 { ISD::UINT_TO_FP, MVT::f32, MVT::i1, 2 }, 394 { ISD::SINT_TO_FP, MVT::f64, MVT::i1, 2 }, 395 { ISD::UINT_TO_FP, MVT::f64, MVT::i1, 2 }, 396 { ISD::SINT_TO_FP, MVT::f32, MVT::i8, 2 }, 397 { ISD::UINT_TO_FP, MVT::f32, MVT::i8, 2 }, 398 { ISD::SINT_TO_FP, MVT::f64, MVT::i8, 2 }, 399 { ISD::UINT_TO_FP, MVT::f64, MVT::i8, 2 }, 400 { ISD::SINT_TO_FP, MVT::f32, MVT::i16, 2 }, 401 { ISD::UINT_TO_FP, MVT::f32, MVT::i16, 2 }, 402 { ISD::SINT_TO_FP, MVT::f64, MVT::i16, 2 }, 403 { ISD::UINT_TO_FP, MVT::f64, MVT::i16, 2 }, 404 { ISD::SINT_TO_FP, MVT::f32, MVT::i32, 2 }, 405 { ISD::UINT_TO_FP, MVT::f32, MVT::i32, 2 }, 406 { ISD::SINT_TO_FP, MVT::f64, MVT::i32, 2 }, 407 { ISD::UINT_TO_FP, MVT::f64, MVT::i32, 2 }, 408 { ISD::SINT_TO_FP, MVT::f32, MVT::i64, 10 }, 409 { ISD::UINT_TO_FP, MVT::f32, MVT::i64, 10 }, 410 { ISD::SINT_TO_FP, MVT::f64, MVT::i64, 10 }, 411 { ISD::UINT_TO_FP, MVT::f64, MVT::i64, 10 } 412 }; 413 414 if (SrcTy.isInteger() && ST->hasNEON()) { 415 if (const auto *Entry = ConvertCostTableLookup(NEONIntegerConversionTbl, 416 ISD, DstTy.getSimpleVT(), 417 SrcTy.getSimpleVT())) 418 return AdjustCost(Entry->Cost); 419 } 420 421 // MVE extend costs, taken from codegen tests. i8->i16 or i16->i32 is one 422 // instruction, i8->i32 is two. i64 zexts are an VAND with a constant, sext 423 // are linearised so take more. 424 static const TypeConversionCostTblEntry MVEVectorConversionTbl[] = { 425 { ISD::SIGN_EXTEND, MVT::v8i16, MVT::v8i8, 1 }, 426 { ISD::ZERO_EXTEND, MVT::v8i16, MVT::v8i8, 1 }, 427 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i8, 2 }, 428 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i8, 2 }, 429 { ISD::SIGN_EXTEND, MVT::v2i64, MVT::v2i8, 10 }, 430 { ISD::ZERO_EXTEND, MVT::v2i64, MVT::v2i8, 2 }, 431 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i16, 1 }, 432 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i16, 1 }, 433 { ISD::SIGN_EXTEND, MVT::v2i64, MVT::v2i16, 10 }, 434 { ISD::ZERO_EXTEND, MVT::v2i64, MVT::v2i16, 2 }, 435 { ISD::SIGN_EXTEND, MVT::v2i64, MVT::v2i32, 8 }, 436 { ISD::ZERO_EXTEND, MVT::v2i64, MVT::v2i32, 2 }, 437 }; 438 439 if (SrcTy.isVector() && ST->hasMVEIntegerOps()) { 440 if (const auto *Entry = ConvertCostTableLookup(MVEVectorConversionTbl, 441 ISD, DstTy.getSimpleVT(), 442 SrcTy.getSimpleVT())) 443 return AdjustCost(Entry->Cost * ST->getMVEVectorCostFactor()); 444 } 445 446 // Scalar integer conversion costs. 447 static const TypeConversionCostTblEntry ARMIntegerConversionTbl[] = { 448 // i16 -> i64 requires two dependent operations. 449 { ISD::SIGN_EXTEND, MVT::i64, MVT::i16, 2 }, 450 451 // Truncates on i64 are assumed to be free. 452 { ISD::TRUNCATE, MVT::i32, MVT::i64, 0 }, 453 { ISD::TRUNCATE, MVT::i16, MVT::i64, 0 }, 454 { ISD::TRUNCATE, MVT::i8, MVT::i64, 0 }, 455 { ISD::TRUNCATE, MVT::i1, MVT::i64, 0 } 456 }; 457 458 if (SrcTy.isInteger()) { 459 if (const auto *Entry = ConvertCostTableLookup(ARMIntegerConversionTbl, ISD, 460 DstTy.getSimpleVT(), 461 SrcTy.getSimpleVT())) 462 return AdjustCost(Entry->Cost); 463 } 464 465 int BaseCost = ST->hasMVEIntegerOps() && Src->isVectorTy() 466 ? ST->getMVEVectorCostFactor() 467 : 1; 468 return AdjustCost( 469 BaseCost * BaseT::getCastInstrCost(Opcode, Dst, Src, CostKind, I)); 470 } 471 472 int ARMTTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy, 473 unsigned Index) { 474 // Penalize inserting into an D-subregister. We end up with a three times 475 // lower estimated throughput on swift. 476 if (ST->hasSlowLoadDSubregister() && Opcode == Instruction::InsertElement && 477 ValTy->isVectorTy() && ValTy->getScalarSizeInBits() <= 32) 478 return 3; 479 480 if (ST->hasNEON() && (Opcode == Instruction::InsertElement || 481 Opcode == Instruction::ExtractElement)) { 482 // Cross-class copies are expensive on many microarchitectures, 483 // so assume they are expensive by default. 484 if (cast<VectorType>(ValTy)->getElementType()->isIntegerTy()) 485 return 3; 486 487 // Even if it's not a cross class copy, this likely leads to mixing 488 // of NEON and VFP code and should be therefore penalized. 489 if (ValTy->isVectorTy() && 490 ValTy->getScalarSizeInBits() <= 32) 491 return std::max(BaseT::getVectorInstrCost(Opcode, ValTy, Index), 2U); 492 } 493 494 if (ST->hasMVEIntegerOps() && (Opcode == Instruction::InsertElement || 495 Opcode == Instruction::ExtractElement)) { 496 // We say MVE moves costs at least the MVEVectorCostFactor, even though 497 // they are scalar instructions. This helps prevent mixing scalar and 498 // vector, to prevent vectorising where we end up just scalarising the 499 // result anyway. 500 return std::max(BaseT::getVectorInstrCost(Opcode, ValTy, Index), 501 ST->getMVEVectorCostFactor()) * 502 cast<FixedVectorType>(ValTy)->getNumElements() / 2; 503 } 504 505 return BaseT::getVectorInstrCost(Opcode, ValTy, Index); 506 } 507 508 int ARMTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy, 509 TTI::TargetCostKind CostKind, 510 const Instruction *I) { 511 // TODO: Handle other cost kinds. 512 if (CostKind != TTI::TCK_RecipThroughput) 513 return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, CostKind, I); 514 515 int ISD = TLI->InstructionOpcodeToISD(Opcode); 516 // On NEON a vector select gets lowered to vbsl. 517 if (ST->hasNEON() && ValTy->isVectorTy() && ISD == ISD::SELECT) { 518 // Lowering of some vector selects is currently far from perfect. 519 static const TypeConversionCostTblEntry NEONVectorSelectTbl[] = { 520 { ISD::SELECT, MVT::v4i1, MVT::v4i64, 4*4 + 1*2 + 1 }, 521 { ISD::SELECT, MVT::v8i1, MVT::v8i64, 50 }, 522 { ISD::SELECT, MVT::v16i1, MVT::v16i64, 100 } 523 }; 524 525 EVT SelCondTy = TLI->getValueType(DL, CondTy); 526 EVT SelValTy = TLI->getValueType(DL, ValTy); 527 if (SelCondTy.isSimple() && SelValTy.isSimple()) { 528 if (const auto *Entry = ConvertCostTableLookup(NEONVectorSelectTbl, ISD, 529 SelCondTy.getSimpleVT(), 530 SelValTy.getSimpleVT())) 531 return Entry->Cost; 532 } 533 534 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy); 535 return LT.first; 536 } 537 538 int BaseCost = ST->hasMVEIntegerOps() && ValTy->isVectorTy() 539 ? ST->getMVEVectorCostFactor() 540 : 1; 541 return BaseCost * BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, CostKind, 542 I); 543 } 544 545 int ARMTTIImpl::getAddressComputationCost(Type *Ty, ScalarEvolution *SE, 546 const SCEV *Ptr) { 547 // Address computations in vectorized code with non-consecutive addresses will 548 // likely result in more instructions compared to scalar code where the 549 // computation can more often be merged into the index mode. The resulting 550 // extra micro-ops can significantly decrease throughput. 551 unsigned NumVectorInstToHideOverhead = 10; 552 int MaxMergeDistance = 64; 553 554 if (ST->hasNEON()) { 555 if (Ty->isVectorTy() && SE && 556 !BaseT::isConstantStridedAccessLessThan(SE, Ptr, MaxMergeDistance + 1)) 557 return NumVectorInstToHideOverhead; 558 559 // In many cases the address computation is not merged into the instruction 560 // addressing mode. 561 return 1; 562 } 563 return BaseT::getAddressComputationCost(Ty, SE, Ptr); 564 } 565 566 bool ARMTTIImpl::isProfitableLSRChainElement(Instruction *I) { 567 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { 568 // If a VCTP is part of a chain, it's already profitable and shouldn't be 569 // optimized, else LSR may block tail-predication. 570 switch (II->getIntrinsicID()) { 571 case Intrinsic::arm_mve_vctp8: 572 case Intrinsic::arm_mve_vctp16: 573 case Intrinsic::arm_mve_vctp32: 574 case Intrinsic::arm_mve_vctp64: 575 return true; 576 default: 577 break; 578 } 579 } 580 return false; 581 } 582 583 bool ARMTTIImpl::isLegalMaskedLoad(Type *DataTy, Align Alignment) { 584 if (!EnableMaskedLoadStores || !ST->hasMVEIntegerOps()) 585 return false; 586 587 if (auto *VecTy = dyn_cast<FixedVectorType>(DataTy)) { 588 // Don't support v2i1 yet. 589 if (VecTy->getNumElements() == 2) 590 return false; 591 592 // We don't support extending fp types. 593 unsigned VecWidth = DataTy->getPrimitiveSizeInBits(); 594 if (VecWidth != 128 && VecTy->getElementType()->isFloatingPointTy()) 595 return false; 596 } 597 598 unsigned EltWidth = DataTy->getScalarSizeInBits(); 599 return (EltWidth == 32 && Alignment >= 4) || 600 (EltWidth == 16 && Alignment >= 2) || (EltWidth == 8); 601 } 602 603 bool ARMTTIImpl::isLegalMaskedGather(Type *Ty, Align Alignment) { 604 if (!EnableMaskedGatherScatters || !ST->hasMVEIntegerOps()) 605 return false; 606 607 // This method is called in 2 places: 608 // - from the vectorizer with a scalar type, in which case we need to get 609 // this as good as we can with the limited info we have (and rely on the cost 610 // model for the rest). 611 // - from the masked intrinsic lowering pass with the actual vector type. 612 // For MVE, we have a custom lowering pass that will already have custom 613 // legalised any gathers that we can to MVE intrinsics, and want to expand all 614 // the rest. The pass runs before the masked intrinsic lowering pass, so if we 615 // are here, we know we want to expand. 616 if (isa<VectorType>(Ty)) 617 return false; 618 619 unsigned EltWidth = Ty->getScalarSizeInBits(); 620 return ((EltWidth == 32 && Alignment >= 4) || 621 (EltWidth == 16 && Alignment >= 2) || EltWidth == 8); 622 } 623 624 int ARMTTIImpl::getMemcpyCost(const Instruction *I) { 625 const MemCpyInst *MI = dyn_cast<MemCpyInst>(I); 626 assert(MI && "MemcpyInst expected"); 627 ConstantInt *C = dyn_cast<ConstantInt>(MI->getLength()); 628 629 // To model the cost of a library call, we assume 1 for the call, and 630 // 3 for the argument setup. 631 const unsigned LibCallCost = 4; 632 633 // If 'size' is not a constant, a library call will be generated. 634 if (!C) 635 return LibCallCost; 636 637 const unsigned Size = C->getValue().getZExtValue(); 638 const Align DstAlign = *MI->getDestAlign(); 639 const Align SrcAlign = *MI->getSourceAlign(); 640 const Function *F = I->getParent()->getParent(); 641 const unsigned Limit = TLI->getMaxStoresPerMemmove(F->hasMinSize()); 642 std::vector<EVT> MemOps; 643 644 // MemOps will be poplulated with a list of data types that needs to be 645 // loaded and stored. That's why we multiply the number of elements by 2 to 646 // get the cost for this memcpy. 647 if (getTLI()->findOptimalMemOpLowering( 648 MemOps, Limit, 649 MemOp::Copy(Size, /*DstAlignCanChange*/ false, DstAlign, SrcAlign, 650 /*IsVolatile*/ true), 651 MI->getDestAddressSpace(), MI->getSourceAddressSpace(), 652 F->getAttributes())) 653 return MemOps.size() * 2; 654 655 // If we can't find an optimal memop lowering, return the default cost 656 return LibCallCost; 657 } 658 659 int ARMTTIImpl::getShuffleCost(TTI::ShuffleKind Kind, VectorType *Tp, 660 int Index, VectorType *SubTp) { 661 if (ST->hasNEON()) { 662 if (Kind == TTI::SK_Broadcast) { 663 static const CostTblEntry NEONDupTbl[] = { 664 // VDUP handles these cases. 665 {ISD::VECTOR_SHUFFLE, MVT::v2i32, 1}, 666 {ISD::VECTOR_SHUFFLE, MVT::v2f32, 1}, 667 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1}, 668 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1}, 669 {ISD::VECTOR_SHUFFLE, MVT::v4i16, 1}, 670 {ISD::VECTOR_SHUFFLE, MVT::v8i8, 1}, 671 672 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 1}, 673 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 1}, 674 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 1}, 675 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 1}}; 676 677 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp); 678 679 if (const auto *Entry = 680 CostTableLookup(NEONDupTbl, ISD::VECTOR_SHUFFLE, LT.second)) 681 return LT.first * Entry->Cost; 682 } 683 if (Kind == TTI::SK_Reverse) { 684 static const CostTblEntry NEONShuffleTbl[] = { 685 // Reverse shuffle cost one instruction if we are shuffling within a 686 // double word (vrev) or two if we shuffle a quad word (vrev, vext). 687 {ISD::VECTOR_SHUFFLE, MVT::v2i32, 1}, 688 {ISD::VECTOR_SHUFFLE, MVT::v2f32, 1}, 689 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1}, 690 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1}, 691 {ISD::VECTOR_SHUFFLE, MVT::v4i16, 1}, 692 {ISD::VECTOR_SHUFFLE, MVT::v8i8, 1}, 693 694 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2}, 695 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2}, 696 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 2}, 697 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 2}}; 698 699 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp); 700 701 if (const auto *Entry = 702 CostTableLookup(NEONShuffleTbl, ISD::VECTOR_SHUFFLE, LT.second)) 703 return LT.first * Entry->Cost; 704 } 705 if (Kind == TTI::SK_Select) { 706 static const CostTblEntry NEONSelShuffleTbl[] = { 707 // Select shuffle cost table for ARM. Cost is the number of 708 // instructions 709 // required to create the shuffled vector. 710 711 {ISD::VECTOR_SHUFFLE, MVT::v2f32, 1}, 712 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1}, 713 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1}, 714 {ISD::VECTOR_SHUFFLE, MVT::v2i32, 1}, 715 716 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2}, 717 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2}, 718 {ISD::VECTOR_SHUFFLE, MVT::v4i16, 2}, 719 720 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 16}, 721 722 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 32}}; 723 724 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp); 725 if (const auto *Entry = CostTableLookup(NEONSelShuffleTbl, 726 ISD::VECTOR_SHUFFLE, LT.second)) 727 return LT.first * Entry->Cost; 728 } 729 } 730 if (ST->hasMVEIntegerOps()) { 731 if (Kind == TTI::SK_Broadcast) { 732 static const CostTblEntry MVEDupTbl[] = { 733 // VDUP handles these cases. 734 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 1}, 735 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 1}, 736 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 1}, 737 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 1}, 738 {ISD::VECTOR_SHUFFLE, MVT::v8f16, 1}}; 739 740 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp); 741 742 if (const auto *Entry = CostTableLookup(MVEDupTbl, ISD::VECTOR_SHUFFLE, 743 LT.second)) 744 return LT.first * Entry->Cost * ST->getMVEVectorCostFactor(); 745 } 746 } 747 int BaseCost = ST->hasMVEIntegerOps() && Tp->isVectorTy() 748 ? ST->getMVEVectorCostFactor() 749 : 1; 750 return BaseCost * BaseT::getShuffleCost(Kind, Tp, Index, SubTp); 751 } 752 753 int ARMTTIImpl::getArithmeticInstrCost(unsigned Opcode, Type *Ty, 754 TTI::TargetCostKind CostKind, 755 TTI::OperandValueKind Op1Info, 756 TTI::OperandValueKind Op2Info, 757 TTI::OperandValueProperties Opd1PropInfo, 758 TTI::OperandValueProperties Opd2PropInfo, 759 ArrayRef<const Value *> Args, 760 const Instruction *CxtI) { 761 // TODO: Handle more cost kinds. 762 if (CostKind != TTI::TCK_RecipThroughput) 763 return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Op1Info, 764 Op2Info, Opd1PropInfo, 765 Opd2PropInfo, Args, CxtI); 766 767 int ISDOpcode = TLI->InstructionOpcodeToISD(Opcode); 768 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty); 769 770 if (ST->hasNEON()) { 771 const unsigned FunctionCallDivCost = 20; 772 const unsigned ReciprocalDivCost = 10; 773 static const CostTblEntry CostTbl[] = { 774 // Division. 775 // These costs are somewhat random. Choose a cost of 20 to indicate that 776 // vectorizing devision (added function call) is going to be very expensive. 777 // Double registers types. 778 { ISD::SDIV, MVT::v1i64, 1 * FunctionCallDivCost}, 779 { ISD::UDIV, MVT::v1i64, 1 * FunctionCallDivCost}, 780 { ISD::SREM, MVT::v1i64, 1 * FunctionCallDivCost}, 781 { ISD::UREM, MVT::v1i64, 1 * FunctionCallDivCost}, 782 { ISD::SDIV, MVT::v2i32, 2 * FunctionCallDivCost}, 783 { ISD::UDIV, MVT::v2i32, 2 * FunctionCallDivCost}, 784 { ISD::SREM, MVT::v2i32, 2 * FunctionCallDivCost}, 785 { ISD::UREM, MVT::v2i32, 2 * FunctionCallDivCost}, 786 { ISD::SDIV, MVT::v4i16, ReciprocalDivCost}, 787 { ISD::UDIV, MVT::v4i16, ReciprocalDivCost}, 788 { ISD::SREM, MVT::v4i16, 4 * FunctionCallDivCost}, 789 { ISD::UREM, MVT::v4i16, 4 * FunctionCallDivCost}, 790 { ISD::SDIV, MVT::v8i8, ReciprocalDivCost}, 791 { ISD::UDIV, MVT::v8i8, ReciprocalDivCost}, 792 { ISD::SREM, MVT::v8i8, 8 * FunctionCallDivCost}, 793 { ISD::UREM, MVT::v8i8, 8 * FunctionCallDivCost}, 794 // Quad register types. 795 { ISD::SDIV, MVT::v2i64, 2 * FunctionCallDivCost}, 796 { ISD::UDIV, MVT::v2i64, 2 * FunctionCallDivCost}, 797 { ISD::SREM, MVT::v2i64, 2 * FunctionCallDivCost}, 798 { ISD::UREM, MVT::v2i64, 2 * FunctionCallDivCost}, 799 { ISD::SDIV, MVT::v4i32, 4 * FunctionCallDivCost}, 800 { ISD::UDIV, MVT::v4i32, 4 * FunctionCallDivCost}, 801 { ISD::SREM, MVT::v4i32, 4 * FunctionCallDivCost}, 802 { ISD::UREM, MVT::v4i32, 4 * FunctionCallDivCost}, 803 { ISD::SDIV, MVT::v8i16, 8 * FunctionCallDivCost}, 804 { ISD::UDIV, MVT::v8i16, 8 * FunctionCallDivCost}, 805 { ISD::SREM, MVT::v8i16, 8 * FunctionCallDivCost}, 806 { ISD::UREM, MVT::v8i16, 8 * FunctionCallDivCost}, 807 { ISD::SDIV, MVT::v16i8, 16 * FunctionCallDivCost}, 808 { ISD::UDIV, MVT::v16i8, 16 * FunctionCallDivCost}, 809 { ISD::SREM, MVT::v16i8, 16 * FunctionCallDivCost}, 810 { ISD::UREM, MVT::v16i8, 16 * FunctionCallDivCost}, 811 // Multiplication. 812 }; 813 814 if (const auto *Entry = CostTableLookup(CostTbl, ISDOpcode, LT.second)) 815 return LT.first * Entry->Cost; 816 817 int Cost = BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Op1Info, 818 Op2Info, 819 Opd1PropInfo, Opd2PropInfo); 820 821 // This is somewhat of a hack. The problem that we are facing is that SROA 822 // creates a sequence of shift, and, or instructions to construct values. 823 // These sequences are recognized by the ISel and have zero-cost. Not so for 824 // the vectorized code. Because we have support for v2i64 but not i64 those 825 // sequences look particularly beneficial to vectorize. 826 // To work around this we increase the cost of v2i64 operations to make them 827 // seem less beneficial. 828 if (LT.second == MVT::v2i64 && 829 Op2Info == TargetTransformInfo::OK_UniformConstantValue) 830 Cost += 4; 831 832 return Cost; 833 } 834 835 // If this operation is a shift on arm/thumb2, it might well be folded into 836 // the following instruction, hence having a cost of 0. 837 auto LooksLikeAFreeShift = [&]() { 838 if (ST->isThumb1Only() || Ty->isVectorTy()) 839 return false; 840 841 if (!CxtI || !CxtI->hasOneUse() || !CxtI->isShift()) 842 return false; 843 if (Op2Info != TargetTransformInfo::OK_UniformConstantValue) 844 return false; 845 846 // Folded into a ADC/ADD/AND/BIC/CMP/EOR/MVN/ORR/ORN/RSB/SBC/SUB 847 switch (cast<Instruction>(CxtI->user_back())->getOpcode()) { 848 case Instruction::Add: 849 case Instruction::Sub: 850 case Instruction::And: 851 case Instruction::Xor: 852 case Instruction::Or: 853 case Instruction::ICmp: 854 return true; 855 default: 856 return false; 857 } 858 }; 859 if (LooksLikeAFreeShift()) 860 return 0; 861 862 int BaseCost = ST->hasMVEIntegerOps() && Ty->isVectorTy() 863 ? ST->getMVEVectorCostFactor() 864 : 1; 865 866 // The rest of this mostly follows what is done in BaseT::getArithmeticInstrCost, 867 // without treating floats as more expensive that scalars or increasing the 868 // costs for custom operations. The results is also multiplied by the 869 // MVEVectorCostFactor where appropriate. 870 if (TLI->isOperationLegalOrCustomOrPromote(ISDOpcode, LT.second)) 871 return LT.first * BaseCost; 872 873 // Else this is expand, assume that we need to scalarize this op. 874 if (auto *VTy = dyn_cast<FixedVectorType>(Ty)) { 875 unsigned Num = VTy->getNumElements(); 876 unsigned Cost = getArithmeticInstrCost(Opcode, Ty->getScalarType(), 877 CostKind); 878 // Return the cost of multiple scalar invocation plus the cost of 879 // inserting and extracting the values. 880 return BaseT::getScalarizationOverhead(VTy, Args) + Num * Cost; 881 } 882 883 return BaseCost; 884 } 885 886 int ARMTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src, 887 MaybeAlign Alignment, unsigned AddressSpace, 888 TTI::TargetCostKind CostKind, 889 const Instruction *I) { 890 // TODO: Handle other cost kinds. 891 if (CostKind != TTI::TCK_RecipThroughput) 892 return 1; 893 894 // Type legalization can't handle structs 895 if (TLI->getValueType(DL, Src, true) == MVT::Other) 896 return BaseT::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace, 897 CostKind); 898 899 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src); 900 901 if (ST->hasNEON() && Src->isVectorTy() && 902 (Alignment && *Alignment != Align(16)) && 903 cast<VectorType>(Src)->getElementType()->isDoubleTy()) { 904 // Unaligned loads/stores are extremely inefficient. 905 // We need 4 uops for vst.1/vld.1 vs 1uop for vldr/vstr. 906 return LT.first * 4; 907 } 908 int BaseCost = ST->hasMVEIntegerOps() && Src->isVectorTy() 909 ? ST->getMVEVectorCostFactor() 910 : 1; 911 return BaseCost * LT.first; 912 } 913 914 int ARMTTIImpl::getInterleavedMemoryOpCost( 915 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices, 916 Align Alignment, unsigned AddressSpace, TTI::TargetCostKind CostKind, 917 bool UseMaskForCond, bool UseMaskForGaps) { 918 assert(Factor >= 2 && "Invalid interleave factor"); 919 assert(isa<VectorType>(VecTy) && "Expect a vector type"); 920 921 // vldN/vstN doesn't support vector types of i64/f64 element. 922 bool EltIs64Bits = DL.getTypeSizeInBits(VecTy->getScalarType()) == 64; 923 924 if (Factor <= TLI->getMaxSupportedInterleaveFactor() && !EltIs64Bits && 925 !UseMaskForCond && !UseMaskForGaps) { 926 unsigned NumElts = cast<FixedVectorType>(VecTy)->getNumElements(); 927 auto *SubVecTy = 928 FixedVectorType::get(VecTy->getScalarType(), NumElts / Factor); 929 930 // vldN/vstN only support legal vector types of size 64 or 128 in bits. 931 // Accesses having vector types that are a multiple of 128 bits can be 932 // matched to more than one vldN/vstN instruction. 933 int BaseCost = ST->hasMVEIntegerOps() ? ST->getMVEVectorCostFactor() : 1; 934 if (NumElts % Factor == 0 && 935 TLI->isLegalInterleavedAccessType(Factor, SubVecTy, DL)) 936 return Factor * BaseCost * TLI->getNumInterleavedAccesses(SubVecTy, DL); 937 938 // Some smaller than legal interleaved patterns are cheap as we can make 939 // use of the vmovn or vrev patterns to interleave a standard load. This is 940 // true for v4i8, v8i8 and v4i16 at least (but not for v4f16 as it is 941 // promoted differently). The cost of 2 here is then a load and vrev or 942 // vmovn. 943 if (ST->hasMVEIntegerOps() && Factor == 2 && NumElts / Factor > 2 && 944 VecTy->isIntOrIntVectorTy() && DL.getTypeSizeInBits(SubVecTy) <= 64) 945 return 2 * BaseCost; 946 } 947 948 return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices, 949 Alignment, AddressSpace, CostKind, 950 UseMaskForCond, UseMaskForGaps); 951 } 952 953 unsigned ARMTTIImpl::getGatherScatterOpCost(unsigned Opcode, Type *DataTy, 954 const Value *Ptr, bool VariableMask, 955 Align Alignment, 956 TTI::TargetCostKind CostKind, 957 const Instruction *I) { 958 using namespace PatternMatch; 959 if (!ST->hasMVEIntegerOps() || !EnableMaskedGatherScatters) 960 return BaseT::getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask, 961 Alignment, CostKind, I); 962 963 assert(DataTy->isVectorTy() && "Can't do gather/scatters on scalar!"); 964 auto *VTy = cast<FixedVectorType>(DataTy); 965 966 // TODO: Splitting, once we do that. 967 968 unsigned NumElems = VTy->getNumElements(); 969 unsigned EltSize = VTy->getScalarSizeInBits(); 970 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, DataTy); 971 972 // For now, it is assumed that for the MVE gather instructions the loads are 973 // all effectively serialised. This means the cost is the scalar cost 974 // multiplied by the number of elements being loaded. This is possibly very 975 // conservative, but even so we still end up vectorising loops because the 976 // cost per iteration for many loops is lower than for scalar loops. 977 unsigned VectorCost = NumElems * LT.first; 978 // The scalarization cost should be a lot higher. We use the number of vector 979 // elements plus the scalarization overhead. 980 unsigned ScalarCost = 981 NumElems * LT.first + BaseT::getScalarizationOverhead(VTy, {}); 982 983 if (Alignment < EltSize / 8) 984 return ScalarCost; 985 986 unsigned ExtSize = EltSize; 987 // Check whether there's a single user that asks for an extended type 988 if (I != nullptr) { 989 // Dependent of the caller of this function, a gather instruction will 990 // either have opcode Instruction::Load or be a call to the masked_gather 991 // intrinsic 992 if ((I->getOpcode() == Instruction::Load || 993 match(I, m_Intrinsic<Intrinsic::masked_gather>())) && 994 I->hasOneUse()) { 995 const User *Us = *I->users().begin(); 996 if (isa<ZExtInst>(Us) || isa<SExtInst>(Us)) { 997 // only allow valid type combinations 998 unsigned TypeSize = 999 cast<Instruction>(Us)->getType()->getScalarSizeInBits(); 1000 if (((TypeSize == 32 && (EltSize == 8 || EltSize == 16)) || 1001 (TypeSize == 16 && EltSize == 8)) && 1002 TypeSize * NumElems == 128) { 1003 ExtSize = TypeSize; 1004 } 1005 } 1006 } 1007 // Check whether the input data needs to be truncated 1008 TruncInst *T; 1009 if ((I->getOpcode() == Instruction::Store || 1010 match(I, m_Intrinsic<Intrinsic::masked_scatter>())) && 1011 (T = dyn_cast<TruncInst>(I->getOperand(0)))) { 1012 // Only allow valid type combinations 1013 unsigned TypeSize = T->getOperand(0)->getType()->getScalarSizeInBits(); 1014 if (((EltSize == 16 && TypeSize == 32) || 1015 (EltSize == 8 && (TypeSize == 32 || TypeSize == 16))) && 1016 TypeSize * NumElems == 128) 1017 ExtSize = TypeSize; 1018 } 1019 } 1020 1021 if (ExtSize * NumElems != 128 || NumElems < 4) 1022 return ScalarCost; 1023 1024 // Any (aligned) i32 gather will not need to be scalarised. 1025 if (ExtSize == 32) 1026 return VectorCost; 1027 // For smaller types, we need to ensure that the gep's inputs are correctly 1028 // extended from a small enough value. Other sizes (including i64) are 1029 // scalarized for now. 1030 if (ExtSize != 8 && ExtSize != 16) 1031 return ScalarCost; 1032 1033 if (const auto *BC = dyn_cast<BitCastInst>(Ptr)) 1034 Ptr = BC->getOperand(0); 1035 if (const auto *GEP = dyn_cast<GetElementPtrInst>(Ptr)) { 1036 if (GEP->getNumOperands() != 2) 1037 return ScalarCost; 1038 unsigned Scale = DL.getTypeAllocSize(GEP->getResultElementType()); 1039 // Scale needs to be correct (which is only relevant for i16s). 1040 if (Scale != 1 && Scale * 8 != ExtSize) 1041 return ScalarCost; 1042 // And we need to zext (not sext) the indexes from a small enough type. 1043 if (const auto *ZExt = dyn_cast<ZExtInst>(GEP->getOperand(1))) { 1044 if (ZExt->getOperand(0)->getType()->getScalarSizeInBits() <= ExtSize) 1045 return VectorCost; 1046 } 1047 return ScalarCost; 1048 } 1049 return ScalarCost; 1050 } 1051 1052 bool ARMTTIImpl::isLoweredToCall(const Function *F) { 1053 if (!F->isIntrinsic()) 1054 BaseT::isLoweredToCall(F); 1055 1056 // Assume all Arm-specific intrinsics map to an instruction. 1057 if (F->getName().startswith("llvm.arm")) 1058 return false; 1059 1060 switch (F->getIntrinsicID()) { 1061 default: break; 1062 case Intrinsic::powi: 1063 case Intrinsic::sin: 1064 case Intrinsic::cos: 1065 case Intrinsic::pow: 1066 case Intrinsic::log: 1067 case Intrinsic::log10: 1068 case Intrinsic::log2: 1069 case Intrinsic::exp: 1070 case Intrinsic::exp2: 1071 return true; 1072 case Intrinsic::sqrt: 1073 case Intrinsic::fabs: 1074 case Intrinsic::copysign: 1075 case Intrinsic::floor: 1076 case Intrinsic::ceil: 1077 case Intrinsic::trunc: 1078 case Intrinsic::rint: 1079 case Intrinsic::nearbyint: 1080 case Intrinsic::round: 1081 case Intrinsic::canonicalize: 1082 case Intrinsic::lround: 1083 case Intrinsic::llround: 1084 case Intrinsic::lrint: 1085 case Intrinsic::llrint: 1086 if (F->getReturnType()->isDoubleTy() && !ST->hasFP64()) 1087 return true; 1088 if (F->getReturnType()->isHalfTy() && !ST->hasFullFP16()) 1089 return true; 1090 // Some operations can be handled by vector instructions and assume 1091 // unsupported vectors will be expanded into supported scalar ones. 1092 // TODO Handle scalar operations properly. 1093 return !ST->hasFPARMv8Base() && !ST->hasVFP2Base(); 1094 case Intrinsic::masked_store: 1095 case Intrinsic::masked_load: 1096 case Intrinsic::masked_gather: 1097 case Intrinsic::masked_scatter: 1098 return !ST->hasMVEIntegerOps(); 1099 case Intrinsic::sadd_with_overflow: 1100 case Intrinsic::uadd_with_overflow: 1101 case Intrinsic::ssub_with_overflow: 1102 case Intrinsic::usub_with_overflow: 1103 case Intrinsic::sadd_sat: 1104 case Intrinsic::uadd_sat: 1105 case Intrinsic::ssub_sat: 1106 case Intrinsic::usub_sat: 1107 return false; 1108 } 1109 1110 return BaseT::isLoweredToCall(F); 1111 } 1112 1113 bool ARMTTIImpl::isHardwareLoopProfitable(Loop *L, ScalarEvolution &SE, 1114 AssumptionCache &AC, 1115 TargetLibraryInfo *LibInfo, 1116 HardwareLoopInfo &HWLoopInfo) { 1117 // Low-overhead branches are only supported in the 'low-overhead branch' 1118 // extension of v8.1-m. 1119 if (!ST->hasLOB() || DisableLowOverheadLoops) { 1120 LLVM_DEBUG(dbgs() << "ARMHWLoops: Disabled\n"); 1121 return false; 1122 } 1123 1124 if (!SE.hasLoopInvariantBackedgeTakenCount(L)) { 1125 LLVM_DEBUG(dbgs() << "ARMHWLoops: No BETC\n"); 1126 return false; 1127 } 1128 1129 const SCEV *BackedgeTakenCount = SE.getBackedgeTakenCount(L); 1130 if (isa<SCEVCouldNotCompute>(BackedgeTakenCount)) { 1131 LLVM_DEBUG(dbgs() << "ARMHWLoops: Uncomputable BETC\n"); 1132 return false; 1133 } 1134 1135 const SCEV *TripCountSCEV = 1136 SE.getAddExpr(BackedgeTakenCount, 1137 SE.getOne(BackedgeTakenCount->getType())); 1138 1139 // We need to store the trip count in LR, a 32-bit register. 1140 if (SE.getUnsignedRangeMax(TripCountSCEV).getBitWidth() > 32) { 1141 LLVM_DEBUG(dbgs() << "ARMHWLoops: Trip count does not fit into 32bits\n"); 1142 return false; 1143 } 1144 1145 // Making a call will trash LR and clear LO_BRANCH_INFO, so there's little 1146 // point in generating a hardware loop if that's going to happen. 1147 auto MaybeCall = [this](Instruction &I) { 1148 const ARMTargetLowering *TLI = getTLI(); 1149 unsigned ISD = TLI->InstructionOpcodeToISD(I.getOpcode()); 1150 EVT VT = TLI->getValueType(DL, I.getType(), true); 1151 if (TLI->getOperationAction(ISD, VT) == TargetLowering::LibCall) 1152 return true; 1153 1154 // Check if an intrinsic will be lowered to a call and assume that any 1155 // other CallInst will generate a bl. 1156 if (auto *Call = dyn_cast<CallInst>(&I)) { 1157 if (isa<IntrinsicInst>(Call)) { 1158 if (const Function *F = Call->getCalledFunction()) 1159 return isLoweredToCall(F); 1160 } 1161 return true; 1162 } 1163 1164 // FPv5 provides conversions between integer, double-precision, 1165 // single-precision, and half-precision formats. 1166 switch (I.getOpcode()) { 1167 default: 1168 break; 1169 case Instruction::FPToSI: 1170 case Instruction::FPToUI: 1171 case Instruction::SIToFP: 1172 case Instruction::UIToFP: 1173 case Instruction::FPTrunc: 1174 case Instruction::FPExt: 1175 return !ST->hasFPARMv8Base(); 1176 } 1177 1178 // FIXME: Unfortunately the approach of checking the Operation Action does 1179 // not catch all cases of Legalization that use library calls. Our 1180 // Legalization step categorizes some transformations into library calls as 1181 // Custom, Expand or even Legal when doing type legalization. So for now 1182 // we have to special case for instance the SDIV of 64bit integers and the 1183 // use of floating point emulation. 1184 if (VT.isInteger() && VT.getSizeInBits() >= 64) { 1185 switch (ISD) { 1186 default: 1187 break; 1188 case ISD::SDIV: 1189 case ISD::UDIV: 1190 case ISD::SREM: 1191 case ISD::UREM: 1192 case ISD::SDIVREM: 1193 case ISD::UDIVREM: 1194 return true; 1195 } 1196 } 1197 1198 // Assume all other non-float operations are supported. 1199 if (!VT.isFloatingPoint()) 1200 return false; 1201 1202 // We'll need a library call to handle most floats when using soft. 1203 if (TLI->useSoftFloat()) { 1204 switch (I.getOpcode()) { 1205 default: 1206 return true; 1207 case Instruction::Alloca: 1208 case Instruction::Load: 1209 case Instruction::Store: 1210 case Instruction::Select: 1211 case Instruction::PHI: 1212 return false; 1213 } 1214 } 1215 1216 // We'll need a libcall to perform double precision operations on a single 1217 // precision only FPU. 1218 if (I.getType()->isDoubleTy() && !ST->hasFP64()) 1219 return true; 1220 1221 // Likewise for half precision arithmetic. 1222 if (I.getType()->isHalfTy() && !ST->hasFullFP16()) 1223 return true; 1224 1225 return false; 1226 }; 1227 1228 auto IsHardwareLoopIntrinsic = [](Instruction &I) { 1229 if (auto *Call = dyn_cast<IntrinsicInst>(&I)) { 1230 switch (Call->getIntrinsicID()) { 1231 default: 1232 break; 1233 case Intrinsic::set_loop_iterations: 1234 case Intrinsic::test_set_loop_iterations: 1235 case Intrinsic::loop_decrement: 1236 case Intrinsic::loop_decrement_reg: 1237 return true; 1238 } 1239 } 1240 return false; 1241 }; 1242 1243 // Scan the instructions to see if there's any that we know will turn into a 1244 // call or if this loop is already a low-overhead loop. 1245 auto ScanLoop = [&](Loop *L) { 1246 for (auto *BB : L->getBlocks()) { 1247 for (auto &I : *BB) { 1248 if (MaybeCall(I) || IsHardwareLoopIntrinsic(I)) { 1249 LLVM_DEBUG(dbgs() << "ARMHWLoops: Bad instruction: " << I << "\n"); 1250 return false; 1251 } 1252 } 1253 } 1254 return true; 1255 }; 1256 1257 // Visit inner loops. 1258 for (auto Inner : *L) 1259 if (!ScanLoop(Inner)) 1260 return false; 1261 1262 if (!ScanLoop(L)) 1263 return false; 1264 1265 // TODO: Check whether the trip count calculation is expensive. If L is the 1266 // inner loop but we know it has a low trip count, calculating that trip 1267 // count (in the parent loop) may be detrimental. 1268 1269 LLVMContext &C = L->getHeader()->getContext(); 1270 HWLoopInfo.CounterInReg = true; 1271 HWLoopInfo.IsNestingLegal = false; 1272 HWLoopInfo.PerformEntryTest = true; 1273 HWLoopInfo.CountType = Type::getInt32Ty(C); 1274 HWLoopInfo.LoopDecrement = ConstantInt::get(HWLoopInfo.CountType, 1); 1275 return true; 1276 } 1277 1278 static bool canTailPredicateInstruction(Instruction &I, int &ICmpCount) { 1279 // We don't allow icmp's, and because we only look at single block loops, 1280 // we simply count the icmps, i.e. there should only be 1 for the backedge. 1281 if (isa<ICmpInst>(&I) && ++ICmpCount > 1) 1282 return false; 1283 1284 if (isa<FCmpInst>(&I)) 1285 return false; 1286 1287 // We could allow extending/narrowing FP loads/stores, but codegen is 1288 // too inefficient so reject this for now. 1289 if (isa<FPExtInst>(&I) || isa<FPTruncInst>(&I)) 1290 return false; 1291 1292 // Extends have to be extending-loads 1293 if (isa<SExtInst>(&I) || isa<ZExtInst>(&I) ) 1294 if (!I.getOperand(0)->hasOneUse() || !isa<LoadInst>(I.getOperand(0))) 1295 return false; 1296 1297 // Truncs have to be narrowing-stores 1298 if (isa<TruncInst>(&I) ) 1299 if (!I.hasOneUse() || !isa<StoreInst>(*I.user_begin())) 1300 return false; 1301 1302 return true; 1303 } 1304 1305 // To set up a tail-predicated loop, we need to know the total number of 1306 // elements processed by that loop. Thus, we need to determine the element 1307 // size and: 1308 // 1) it should be uniform for all operations in the vector loop, so we 1309 // e.g. don't want any widening/narrowing operations. 1310 // 2) it should be smaller than i64s because we don't have vector operations 1311 // that work on i64s. 1312 // 3) we don't want elements to be reversed or shuffled, to make sure the 1313 // tail-predication masks/predicates the right lanes. 1314 // 1315 static bool canTailPredicateLoop(Loop *L, LoopInfo *LI, ScalarEvolution &SE, 1316 const DataLayout &DL, 1317 const LoopAccessInfo *LAI) { 1318 PredicatedScalarEvolution PSE = LAI->getPSE(); 1319 int ICmpCount = 0; 1320 int Stride = 0; 1321 1322 LLVM_DEBUG(dbgs() << "tail-predication: checking allowed instructions\n"); 1323 SmallVector<Instruction *, 16> LoadStores; 1324 for (BasicBlock *BB : L->blocks()) { 1325 for (Instruction &I : BB->instructionsWithoutDebug()) { 1326 if (isa<PHINode>(&I)) 1327 continue; 1328 if (!canTailPredicateInstruction(I, ICmpCount)) { 1329 LLVM_DEBUG(dbgs() << "Instruction not allowed: "; I.dump()); 1330 return false; 1331 } 1332 1333 Type *T = I.getType(); 1334 if (T->isPointerTy()) 1335 T = T->getPointerElementType(); 1336 1337 if (T->getScalarSizeInBits() > 32) { 1338 LLVM_DEBUG(dbgs() << "Unsupported Type: "; T->dump()); 1339 return false; 1340 } 1341 1342 if (isa<StoreInst>(I) || isa<LoadInst>(I)) { 1343 Value *Ptr = isa<LoadInst>(I) ? I.getOperand(0) : I.getOperand(1); 1344 int64_t NextStride = getPtrStride(PSE, Ptr, L); 1345 // TODO: for now only allow consecutive strides of 1. We could support 1346 // other strides as long as it is uniform, but let's keep it simple for 1347 // now. 1348 if (Stride == 0 && NextStride == 1) { 1349 Stride = NextStride; 1350 continue; 1351 } 1352 if (Stride != NextStride) { 1353 LLVM_DEBUG(dbgs() << "Different strides found, can't " 1354 "tail-predicate\n."); 1355 return false; 1356 } 1357 } 1358 } 1359 } 1360 1361 LLVM_DEBUG(dbgs() << "tail-predication: all instructions allowed!\n"); 1362 return true; 1363 } 1364 1365 bool ARMTTIImpl::preferPredicateOverEpilogue(Loop *L, LoopInfo *LI, 1366 ScalarEvolution &SE, 1367 AssumptionCache &AC, 1368 TargetLibraryInfo *TLI, 1369 DominatorTree *DT, 1370 const LoopAccessInfo *LAI) { 1371 if (DisableTailPredication) 1372 return false; 1373 1374 // Creating a predicated vector loop is the first step for generating a 1375 // tail-predicated hardware loop, for which we need the MVE masked 1376 // load/stores instructions: 1377 if (!ST->hasMVEIntegerOps()) 1378 return false; 1379 1380 // For now, restrict this to single block loops. 1381 if (L->getNumBlocks() > 1) { 1382 LLVM_DEBUG(dbgs() << "preferPredicateOverEpilogue: not a single block " 1383 "loop.\n"); 1384 return false; 1385 } 1386 1387 assert(L->empty() && "preferPredicateOverEpilogue: inner-loop expected"); 1388 1389 HardwareLoopInfo HWLoopInfo(L); 1390 if (!HWLoopInfo.canAnalyze(*LI)) { 1391 LLVM_DEBUG(dbgs() << "preferPredicateOverEpilogue: hardware-loop is not " 1392 "analyzable.\n"); 1393 return false; 1394 } 1395 1396 // This checks if we have the low-overhead branch architecture 1397 // extension, and if we will create a hardware-loop: 1398 if (!isHardwareLoopProfitable(L, SE, AC, TLI, HWLoopInfo)) { 1399 LLVM_DEBUG(dbgs() << "preferPredicateOverEpilogue: hardware-loop is not " 1400 "profitable.\n"); 1401 return false; 1402 } 1403 1404 if (!HWLoopInfo.isHardwareLoopCandidate(SE, *LI, *DT)) { 1405 LLVM_DEBUG(dbgs() << "preferPredicateOverEpilogue: hardware-loop is not " 1406 "a candidate.\n"); 1407 return false; 1408 } 1409 1410 return canTailPredicateLoop(L, LI, SE, DL, LAI); 1411 } 1412 1413 bool ARMTTIImpl::emitGetActiveLaneMask() const { 1414 if (!ST->hasMVEIntegerOps() || DisableTailPredication) 1415 return false; 1416 1417 // Intrinsic @llvm.get.active.lane.mask is supported. 1418 // It is used in the MVETailPredication pass, which requires the number of 1419 // elements processed by this vector loop to setup the tail-predicated 1420 // loop. 1421 return true; 1422 } 1423 void ARMTTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE, 1424 TTI::UnrollingPreferences &UP) { 1425 // Only currently enable these preferences for M-Class cores. 1426 if (!ST->isMClass()) 1427 return BasicTTIImplBase::getUnrollingPreferences(L, SE, UP); 1428 1429 // Disable loop unrolling for Oz and Os. 1430 UP.OptSizeThreshold = 0; 1431 UP.PartialOptSizeThreshold = 0; 1432 if (L->getHeader()->getParent()->hasOptSize()) 1433 return; 1434 1435 // Only enable on Thumb-2 targets. 1436 if (!ST->isThumb2()) 1437 return; 1438 1439 SmallVector<BasicBlock*, 4> ExitingBlocks; 1440 L->getExitingBlocks(ExitingBlocks); 1441 LLVM_DEBUG(dbgs() << "Loop has:\n" 1442 << "Blocks: " << L->getNumBlocks() << "\n" 1443 << "Exit blocks: " << ExitingBlocks.size() << "\n"); 1444 1445 // Only allow another exit other than the latch. This acts as an early exit 1446 // as it mirrors the profitability calculation of the runtime unroller. 1447 if (ExitingBlocks.size() > 2) 1448 return; 1449 1450 // Limit the CFG of the loop body for targets with a branch predictor. 1451 // Allowing 4 blocks permits if-then-else diamonds in the body. 1452 if (ST->hasBranchPredictor() && L->getNumBlocks() > 4) 1453 return; 1454 1455 // Scan the loop: don't unroll loops with calls as this could prevent 1456 // inlining. 1457 unsigned Cost = 0; 1458 for (auto *BB : L->getBlocks()) { 1459 for (auto &I : *BB) { 1460 // Don't unroll vectorised loop. MVE does not benefit from it as much as 1461 // scalar code. 1462 if (I.getType()->isVectorTy()) 1463 return; 1464 1465 if (isa<CallInst>(I) || isa<InvokeInst>(I)) { 1466 if (const Function *F = cast<CallBase>(I).getCalledFunction()) { 1467 if (!isLoweredToCall(F)) 1468 continue; 1469 } 1470 return; 1471 } 1472 1473 SmallVector<const Value*, 4> Operands(I.value_op_begin(), 1474 I.value_op_end()); 1475 Cost += getUserCost(&I, Operands, TargetTransformInfo::TCK_CodeSize); 1476 } 1477 } 1478 1479 LLVM_DEBUG(dbgs() << "Cost of loop: " << Cost << "\n"); 1480 1481 UP.Partial = true; 1482 UP.Runtime = true; 1483 UP.UpperBound = true; 1484 UP.UnrollRemainder = true; 1485 UP.DefaultUnrollRuntimeCount = 4; 1486 UP.UnrollAndJam = true; 1487 UP.UnrollAndJamInnerLoopThreshold = 60; 1488 1489 // Force unrolling small loops can be very useful because of the branch 1490 // taken cost of the backedge. 1491 if (Cost < 12) 1492 UP.Force = true; 1493 } 1494 1495 bool ARMTTIImpl::useReductionIntrinsic(unsigned Opcode, Type *Ty, 1496 TTI::ReductionFlags Flags) const { 1497 return ST->hasMVEIntegerOps(); 1498 } 1499