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