1 //===----- TypePromotion.cpp ----------------------------------------------===// 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 /// \file 10 /// This is an opcode based type promotion pass for small types that would 11 /// otherwise be promoted during legalisation. This works around the limitations 12 /// of selection dag for cyclic regions. The search begins from icmp 13 /// instructions operands where a tree, consisting of non-wrapping or safe 14 /// wrapping instructions, is built, checked and promoted if possible. 15 /// 16 //===----------------------------------------------------------------------===// 17 18 #include "llvm/ADT/SetVector.h" 19 #include "llvm/ADT/StringRef.h" 20 #include "llvm/Analysis/TargetTransformInfo.h" 21 #include "llvm/CodeGen/Passes.h" 22 #include "llvm/CodeGen/TargetLowering.h" 23 #include "llvm/CodeGen/TargetPassConfig.h" 24 #include "llvm/CodeGen/TargetSubtargetInfo.h" 25 #include "llvm/IR/Attributes.h" 26 #include "llvm/IR/BasicBlock.h" 27 #include "llvm/IR/Constants.h" 28 #include "llvm/IR/IRBuilder.h" 29 #include "llvm/IR/InstrTypes.h" 30 #include "llvm/IR/Instruction.h" 31 #include "llvm/IR/Instructions.h" 32 #include "llvm/IR/Type.h" 33 #include "llvm/IR/Value.h" 34 #include "llvm/InitializePasses.h" 35 #include "llvm/Pass.h" 36 #include "llvm/Support/Casting.h" 37 #include "llvm/Support/CommandLine.h" 38 #include "llvm/Target/TargetMachine.h" 39 40 #define DEBUG_TYPE "type-promotion" 41 #define PASS_NAME "Type Promotion" 42 43 using namespace llvm; 44 45 static cl::opt<bool> DisablePromotion("disable-type-promotion", cl::Hidden, 46 cl::init(false), 47 cl::desc("Disable type promotion pass")); 48 49 // The goal of this pass is to enable more efficient code generation for 50 // operations on narrow types (i.e. types with < 32-bits) and this is a 51 // motivating IR code example: 52 // 53 // define hidden i32 @cmp(i8 zeroext) { 54 // %2 = add i8 %0, -49 55 // %3 = icmp ult i8 %2, 3 56 // .. 57 // } 58 // 59 // The issue here is that i8 is type-legalized to i32 because i8 is not a 60 // legal type. Thus, arithmetic is done in integer-precision, but then the 61 // byte value is masked out as follows: 62 // 63 // t19: i32 = add t4, Constant:i32<-49> 64 // t24: i32 = and t19, Constant:i32<255> 65 // 66 // Consequently, we generate code like this: 67 // 68 // subs r0, #49 69 // uxtb r1, r0 70 // cmp r1, #3 71 // 72 // This shows that masking out the byte value results in generation of 73 // the UXTB instruction. This is not optimal as r0 already contains the byte 74 // value we need, and so instead we can just generate: 75 // 76 // sub.w r1, r0, #49 77 // cmp r1, #3 78 // 79 // We achieve this by type promoting the IR to i32 like so for this example: 80 // 81 // define i32 @cmp(i8 zeroext %c) { 82 // %0 = zext i8 %c to i32 83 // %c.off = add i32 %0, -49 84 // %1 = icmp ult i32 %c.off, 3 85 // .. 86 // } 87 // 88 // For this to be valid and legal, we need to prove that the i32 add is 89 // producing the same value as the i8 addition, and that e.g. no overflow 90 // happens. 91 // 92 // A brief sketch of the algorithm and some terminology. 93 // We pattern match interesting IR patterns: 94 // - which have "sources": instructions producing narrow values (i8, i16), and 95 // - they have "sinks": instructions consuming these narrow values. 96 // 97 // We collect all instruction connecting sources and sinks in a worklist, so 98 // that we can mutate these instruction and perform type promotion when it is 99 // legal to do so. 100 101 namespace { 102 class IRPromoter { 103 LLVMContext &Ctx; 104 unsigned PromotedWidth = 0; 105 SetVector<Value *> &Visited; 106 SetVector<Value *> &Sources; 107 SetVector<Instruction *> &Sinks; 108 SmallPtrSetImpl<Instruction *> &SafeWrap; 109 IntegerType *ExtTy = nullptr; 110 SmallPtrSet<Value *, 8> NewInsts; 111 SmallPtrSet<Instruction *, 4> InstsToRemove; 112 DenseMap<Value *, SmallVector<Type *, 4>> TruncTysMap; 113 SmallPtrSet<Value *, 8> Promoted; 114 115 void ReplaceAllUsersOfWith(Value *From, Value *To); 116 void ExtendSources(); 117 void ConvertTruncs(); 118 void PromoteTree(); 119 void TruncateSinks(); 120 void Cleanup(); 121 122 public: 123 IRPromoter(LLVMContext &C, unsigned Width, 124 SetVector<Value *> &visited, SetVector<Value *> &sources, 125 SetVector<Instruction *> &sinks, 126 SmallPtrSetImpl<Instruction *> &wrap) 127 : Ctx(C), PromotedWidth(Width), Visited(visited), 128 Sources(sources), Sinks(sinks), SafeWrap(wrap) { 129 ExtTy = IntegerType::get(Ctx, PromotedWidth); 130 } 131 132 void Mutate(); 133 }; 134 135 class TypePromotion : public FunctionPass { 136 unsigned TypeSize = 0; 137 LLVMContext *Ctx = nullptr; 138 unsigned RegisterBitWidth = 0; 139 SmallPtrSet<Value *, 16> AllVisited; 140 SmallPtrSet<Instruction *, 8> SafeToPromote; 141 SmallPtrSet<Instruction *, 4> SafeWrap; 142 143 // Does V have the same size result type as TypeSize. 144 bool EqualTypeSize(Value *V); 145 // Does V have the same size, or narrower, result type as TypeSize. 146 bool LessOrEqualTypeSize(Value *V); 147 // Does V have a result type that is wider than TypeSize. 148 bool GreaterThanTypeSize(Value *V); 149 // Does V have a result type that is narrower than TypeSize. 150 bool LessThanTypeSize(Value *V); 151 // Should V be a leaf in the promote tree? 152 bool isSource(Value *V); 153 // Should V be a root in the promotion tree? 154 bool isSink(Value *V); 155 // Should we change the result type of V? It will result in the users of V 156 // being visited. 157 bool shouldPromote(Value *V); 158 // Is I an add or a sub, which isn't marked as nuw, but where a wrapping 159 // result won't affect the computation? 160 bool isSafeWrap(Instruction *I); 161 // Can V have its integer type promoted, or can the type be ignored. 162 bool isSupportedType(Value *V); 163 // Is V an instruction with a supported opcode or another value that we can 164 // handle, such as constants and basic blocks. 165 bool isSupportedValue(Value *V); 166 // Is V an instruction thats result can trivially promoted, or has safe 167 // wrapping. 168 bool isLegalToPromote(Value *V); 169 bool TryToPromote(Value *V, unsigned PromotedWidth); 170 171 public: 172 static char ID; 173 174 TypePromotion() : FunctionPass(ID) {} 175 176 void getAnalysisUsage(AnalysisUsage &AU) const override { 177 AU.addRequired<TargetTransformInfoWrapperPass>(); 178 AU.addRequired<TargetPassConfig>(); 179 AU.setPreservesCFG(); 180 } 181 182 StringRef getPassName() const override { return PASS_NAME; } 183 184 bool runOnFunction(Function &F) override; 185 }; 186 187 } // namespace 188 189 static bool GenerateSignBits(Instruction *I) { 190 unsigned Opc = I->getOpcode(); 191 return Opc == Instruction::AShr || Opc == Instruction::SDiv || 192 Opc == Instruction::SRem || Opc == Instruction::SExt; 193 } 194 195 bool TypePromotion::EqualTypeSize(Value *V) { 196 return V->getType()->getScalarSizeInBits() == TypeSize; 197 } 198 199 bool TypePromotion::LessOrEqualTypeSize(Value *V) { 200 return V->getType()->getScalarSizeInBits() <= TypeSize; 201 } 202 203 bool TypePromotion::GreaterThanTypeSize(Value *V) { 204 return V->getType()->getScalarSizeInBits() > TypeSize; 205 } 206 207 bool TypePromotion::LessThanTypeSize(Value *V) { 208 return V->getType()->getScalarSizeInBits() < TypeSize; 209 } 210 211 /// Return true if the given value is a source in the use-def chain, producing 212 /// a narrow 'TypeSize' value. These values will be zext to start the promotion 213 /// of the tree to i32. We guarantee that these won't populate the upper bits 214 /// of the register. ZExt on the loads will be free, and the same for call 215 /// return values because we only accept ones that guarantee a zeroext ret val. 216 /// Many arguments will have the zeroext attribute too, so those would be free 217 /// too. 218 bool TypePromotion::isSource(Value *V) { 219 if (!isa<IntegerType>(V->getType())) 220 return false; 221 222 // TODO Allow zext to be sources. 223 if (isa<Argument>(V)) 224 return true; 225 else if (isa<LoadInst>(V)) 226 return true; 227 else if (isa<BitCastInst>(V)) 228 return true; 229 else if (auto *Call = dyn_cast<CallInst>(V)) 230 return Call->hasRetAttr(Attribute::AttrKind::ZExt); 231 else if (auto *Trunc = dyn_cast<TruncInst>(V)) 232 return EqualTypeSize(Trunc); 233 return false; 234 } 235 236 /// Return true if V will require any promoted values to be truncated for the 237 /// the IR to remain valid. We can't mutate the value type of these 238 /// instructions. 239 bool TypePromotion::isSink(Value *V) { 240 // TODO The truncate also isn't actually necessary because we would already 241 // proved that the data value is kept within the range of the original data 242 // type. We currently remove any truncs inserted for handling zext sinks. 243 244 // Sinks are: 245 // - points where the value in the register is being observed, such as an 246 // icmp, switch or store. 247 // - points where value types have to match, such as calls and returns. 248 // - zext are included to ease the transformation and are generally removed 249 // later on. 250 if (auto *Store = dyn_cast<StoreInst>(V)) 251 return LessOrEqualTypeSize(Store->getValueOperand()); 252 if (auto *Return = dyn_cast<ReturnInst>(V)) 253 return LessOrEqualTypeSize(Return->getReturnValue()); 254 if (auto *ZExt = dyn_cast<ZExtInst>(V)) 255 return GreaterThanTypeSize(ZExt); 256 if (auto *Switch = dyn_cast<SwitchInst>(V)) 257 return LessThanTypeSize(Switch->getCondition()); 258 if (auto *ICmp = dyn_cast<ICmpInst>(V)) 259 return ICmp->isSigned() || LessThanTypeSize(ICmp->getOperand(0)); 260 261 return isa<CallInst>(V); 262 } 263 264 /// Return whether this instruction can safely wrap. 265 bool TypePromotion::isSafeWrap(Instruction *I) { 266 // We can support a potentially wrapping instruction (I) if: 267 // - It is only used by an unsigned icmp. 268 // - The icmp uses a constant. 269 // - The wrapping value (I) is decreasing, i.e would underflow - wrapping 270 // around zero to become a larger number than before. 271 // - The wrapping instruction (I) also uses a constant. 272 // 273 // We can then use the two constants to calculate whether the result would 274 // wrap in respect to itself in the original bitwidth. If it doesn't wrap, 275 // just underflows the range, the icmp would give the same result whether the 276 // result has been truncated or not. We calculate this by: 277 // - Zero extending both constants, if needed, to RegisterBitWidth. 278 // - Take the absolute value of I's constant, adding this to the icmp const. 279 // - Check that this value is not out of range for small type. If it is, it 280 // means that it has underflowed enough to wrap around the icmp constant. 281 // 282 // For example: 283 // 284 // %sub = sub i8 %a, 2 285 // %cmp = icmp ule i8 %sub, 254 286 // 287 // If %a = 0, %sub = -2 == FE == 254 288 // But if this is evalulated as a i32 289 // %sub = -2 == FF FF FF FE == 4294967294 290 // So the unsigned compares (i8 and i32) would not yield the same result. 291 // 292 // Another way to look at it is: 293 // %a - 2 <= 254 294 // %a + 2 <= 254 + 2 295 // %a <= 256 296 // And we can't represent 256 in the i8 format, so we don't support it. 297 // 298 // Whereas: 299 // 300 // %sub i8 %a, 1 301 // %cmp = icmp ule i8 %sub, 254 302 // 303 // If %a = 0, %sub = -1 == FF == 255 304 // As i32: 305 // %sub = -1 == FF FF FF FF == 4294967295 306 // 307 // In this case, the unsigned compare results would be the same and this 308 // would also be true for ult, uge and ugt: 309 // - (255 < 254) == (0xFFFFFFFF < 254) == false 310 // - (255 <= 254) == (0xFFFFFFFF <= 254) == false 311 // - (255 > 254) == (0xFFFFFFFF > 254) == true 312 // - (255 >= 254) == (0xFFFFFFFF >= 254) == true 313 // 314 // To demonstrate why we can't handle increasing values: 315 // 316 // %add = add i8 %a, 2 317 // %cmp = icmp ult i8 %add, 127 318 // 319 // If %a = 254, %add = 256 == (i8 1) 320 // As i32: 321 // %add = 256 322 // 323 // (1 < 127) != (256 < 127) 324 325 unsigned Opc = I->getOpcode(); 326 if (Opc != Instruction::Add && Opc != Instruction::Sub) 327 return false; 328 329 if (!I->hasOneUse() || !isa<ICmpInst>(*I->user_begin()) || 330 !isa<ConstantInt>(I->getOperand(1))) 331 return false; 332 333 // Don't support an icmp that deals with sign bits. 334 auto *CI = cast<ICmpInst>(*I->user_begin()); 335 if (CI->isSigned() || CI->isEquality()) 336 return false; 337 338 ConstantInt *ICmpConstant = nullptr; 339 if (auto *Const = dyn_cast<ConstantInt>(CI->getOperand(0))) 340 ICmpConstant = Const; 341 else if (auto *Const = dyn_cast<ConstantInt>(CI->getOperand(1))) 342 ICmpConstant = Const; 343 else 344 return false; 345 346 const APInt &ICmpConst = ICmpConstant->getValue(); 347 APInt OverflowConst = cast<ConstantInt>(I->getOperand(1))->getValue(); 348 if (Opc == Instruction::Sub) 349 OverflowConst = -OverflowConst; 350 if (!OverflowConst.isNonPositive()) 351 return false; 352 353 // Using C1 = OverflowConst and C2 = ICmpConst, we can either prove that: 354 // zext(x) + sext(C1) <u zext(C2) if C1 < 0 and C1 >s C2 355 // zext(x) + sext(C1) <u sext(C2) if C1 < 0 and C1 <=s C2 356 if (OverflowConst.sgt(ICmpConst)) { 357 LLVM_DEBUG(dbgs() << "IR Promotion: Allowing safe overflow for sext " 358 << "const of " << *I << "\n"); 359 SafeWrap.insert(I); 360 return true; 361 } else { 362 LLVM_DEBUG(dbgs() << "IR Promotion: Allowing safe overflow for sext " 363 << "const of " << *I << " and " << *CI << "\n"); 364 SafeWrap.insert(I); 365 SafeWrap.insert(CI); 366 return true; 367 } 368 return false; 369 } 370 371 bool TypePromotion::shouldPromote(Value *V) { 372 if (!isa<IntegerType>(V->getType()) || isSink(V)) 373 return false; 374 375 if (isSource(V)) 376 return true; 377 378 auto *I = dyn_cast<Instruction>(V); 379 if (!I) 380 return false; 381 382 if (isa<ICmpInst>(I)) 383 return false; 384 385 return true; 386 } 387 388 /// Return whether we can safely mutate V's type to ExtTy without having to be 389 /// concerned with zero extending or truncation. 390 static bool isPromotedResultSafe(Instruction *I) { 391 if (GenerateSignBits(I)) 392 return false; 393 394 if (!isa<OverflowingBinaryOperator>(I)) 395 return true; 396 397 return I->hasNoUnsignedWrap(); 398 } 399 400 void IRPromoter::ReplaceAllUsersOfWith(Value *From, Value *To) { 401 SmallVector<Instruction *, 4> Users; 402 Instruction *InstTo = dyn_cast<Instruction>(To); 403 bool ReplacedAll = true; 404 405 LLVM_DEBUG(dbgs() << "IR Promotion: Replacing " << *From << " with " << *To 406 << "\n"); 407 408 for (Use &U : From->uses()) { 409 auto *User = cast<Instruction>(U.getUser()); 410 if (InstTo && User->isIdenticalTo(InstTo)) { 411 ReplacedAll = false; 412 continue; 413 } 414 Users.push_back(User); 415 } 416 417 for (auto *U : Users) 418 U->replaceUsesOfWith(From, To); 419 420 if (ReplacedAll) 421 if (auto *I = dyn_cast<Instruction>(From)) 422 InstsToRemove.insert(I); 423 } 424 425 void IRPromoter::ExtendSources() { 426 IRBuilder<> Builder{Ctx}; 427 428 auto InsertZExt = [&](Value *V, Instruction *InsertPt) { 429 assert(V->getType() != ExtTy && "zext already extends to i32"); 430 LLVM_DEBUG(dbgs() << "IR Promotion: Inserting ZExt for " << *V << "\n"); 431 Builder.SetInsertPoint(InsertPt); 432 if (auto *I = dyn_cast<Instruction>(V)) 433 Builder.SetCurrentDebugLocation(I->getDebugLoc()); 434 435 Value *ZExt = Builder.CreateZExt(V, ExtTy); 436 if (auto *I = dyn_cast<Instruction>(ZExt)) { 437 if (isa<Argument>(V)) 438 I->moveBefore(InsertPt); 439 else 440 I->moveAfter(InsertPt); 441 NewInsts.insert(I); 442 } 443 444 ReplaceAllUsersOfWith(V, ZExt); 445 }; 446 447 // Now, insert extending instructions between the sources and their users. 448 LLVM_DEBUG(dbgs() << "IR Promotion: Promoting sources:\n"); 449 for (auto *V : Sources) { 450 LLVM_DEBUG(dbgs() << " - " << *V << "\n"); 451 if (auto *I = dyn_cast<Instruction>(V)) 452 InsertZExt(I, I); 453 else if (auto *Arg = dyn_cast<Argument>(V)) { 454 BasicBlock &BB = Arg->getParent()->front(); 455 InsertZExt(Arg, &*BB.getFirstInsertionPt()); 456 } else { 457 llvm_unreachable("unhandled source that needs extending"); 458 } 459 Promoted.insert(V); 460 } 461 } 462 463 void IRPromoter::PromoteTree() { 464 LLVM_DEBUG(dbgs() << "IR Promotion: Mutating the tree..\n"); 465 466 // Mutate the types of the instructions within the tree. Here we handle 467 // constant operands. 468 for (auto *V : Visited) { 469 if (Sources.count(V)) 470 continue; 471 472 auto *I = cast<Instruction>(V); 473 if (Sinks.count(I)) 474 continue; 475 476 for (unsigned i = 0, e = I->getNumOperands(); i < e; ++i) { 477 Value *Op = I->getOperand(i); 478 if ((Op->getType() == ExtTy) || !isa<IntegerType>(Op->getType())) 479 continue; 480 481 if (auto *Const = dyn_cast<ConstantInt>(Op)) { 482 // For subtract, we don't need to sext the constant. We only put it in 483 // SafeWrap because SafeWrap.size() is used elsewhere. 484 // For cmp, we need to sign extend a constant appearing in either 485 // operand. For add, we should only sign extend the RHS. 486 Constant *NewConst = (SafeWrap.contains(I) && 487 (I->getOpcode() == Instruction::ICmp || i == 1) && 488 I->getOpcode() != Instruction::Sub) 489 ? ConstantExpr::getSExt(Const, ExtTy) 490 : ConstantExpr::getZExt(Const, ExtTy); 491 I->setOperand(i, NewConst); 492 } else if (isa<UndefValue>(Op)) 493 I->setOperand(i, ConstantInt::get(ExtTy, 0)); 494 } 495 496 // Mutate the result type, unless this is an icmp or switch. 497 if (!isa<ICmpInst>(I) && !isa<SwitchInst>(I)) { 498 I->mutateType(ExtTy); 499 Promoted.insert(I); 500 } 501 } 502 } 503 504 void IRPromoter::TruncateSinks() { 505 LLVM_DEBUG(dbgs() << "IR Promotion: Fixing up the sinks:\n"); 506 507 IRBuilder<> Builder{Ctx}; 508 509 auto InsertTrunc = [&](Value *V, Type *TruncTy) -> Instruction * { 510 if (!isa<Instruction>(V) || !isa<IntegerType>(V->getType())) 511 return nullptr; 512 513 if ((!Promoted.count(V) && !NewInsts.count(V)) || Sources.count(V)) 514 return nullptr; 515 516 LLVM_DEBUG(dbgs() << "IR Promotion: Creating " << *TruncTy << " Trunc for " 517 << *V << "\n"); 518 Builder.SetInsertPoint(cast<Instruction>(V)); 519 auto *Trunc = dyn_cast<Instruction>(Builder.CreateTrunc(V, TruncTy)); 520 if (Trunc) 521 NewInsts.insert(Trunc); 522 return Trunc; 523 }; 524 525 // Fix up any stores or returns that use the results of the promoted 526 // chain. 527 for (auto *I : Sinks) { 528 LLVM_DEBUG(dbgs() << "IR Promotion: For Sink: " << *I << "\n"); 529 530 // Handle calls separately as we need to iterate over arg operands. 531 if (auto *Call = dyn_cast<CallInst>(I)) { 532 for (unsigned i = 0; i < Call->arg_size(); ++i) { 533 Value *Arg = Call->getArgOperand(i); 534 Type *Ty = TruncTysMap[Call][i]; 535 if (Instruction *Trunc = InsertTrunc(Arg, Ty)) { 536 Trunc->moveBefore(Call); 537 Call->setArgOperand(i, Trunc); 538 } 539 } 540 continue; 541 } 542 543 // Special case switches because we need to truncate the condition. 544 if (auto *Switch = dyn_cast<SwitchInst>(I)) { 545 Type *Ty = TruncTysMap[Switch][0]; 546 if (Instruction *Trunc = InsertTrunc(Switch->getCondition(), Ty)) { 547 Trunc->moveBefore(Switch); 548 Switch->setCondition(Trunc); 549 } 550 continue; 551 } 552 553 // Don't insert a trunc for a zext which can still legally promote. 554 if (auto ZExt = dyn_cast<ZExtInst>(I)) 555 if (ZExt->getType()->getScalarSizeInBits() > PromotedWidth) 556 continue; 557 558 // Now handle the others. 559 for (unsigned i = 0; i < I->getNumOperands(); ++i) { 560 Type *Ty = TruncTysMap[I][i]; 561 if (Instruction *Trunc = InsertTrunc(I->getOperand(i), Ty)) { 562 Trunc->moveBefore(I); 563 I->setOperand(i, Trunc); 564 } 565 } 566 } 567 } 568 569 void IRPromoter::Cleanup() { 570 LLVM_DEBUG(dbgs() << "IR Promotion: Cleanup..\n"); 571 // Some zexts will now have become redundant, along with their trunc 572 // operands, so remove them. 573 // Some zexts need to be replaced with truncate if src bitwidth is larger. 574 for (auto *V : Visited) { 575 if (!isa<ZExtInst>(V)) 576 continue; 577 578 auto ZExt = cast<ZExtInst>(V); 579 if (ZExt->getDestTy() != ExtTy) 580 continue; 581 582 Value *Src = ZExt->getOperand(0); 583 if (ZExt->getSrcTy() == ZExt->getDestTy()) { 584 LLVM_DEBUG(dbgs() << "IR Promotion: Removing unnecessary cast: " << *ZExt 585 << "\n"); 586 ReplaceAllUsersOfWith(ZExt, Src); 587 continue; 588 } else if (ZExt->getSrcTy()->getScalarSizeInBits() > PromotedWidth) { 589 IRBuilder<> Builder{ZExt}; 590 Value *Trunc = Builder.CreateTrunc(Src, ZExt->getDestTy()); 591 ReplaceAllUsersOfWith(ZExt, Trunc); 592 continue; 593 } 594 595 // We've inserted a trunc for a zext sink, but we already know that the 596 // input is in range, negating the need for the trunc. 597 if (NewInsts.count(Src) && isa<TruncInst>(Src)) { 598 auto *Trunc = cast<TruncInst>(Src); 599 assert(Trunc->getOperand(0)->getType() == ExtTy && 600 "expected inserted trunc to be operating on i32"); 601 ReplaceAllUsersOfWith(ZExt, Trunc->getOperand(0)); 602 } 603 } 604 605 for (auto *I : InstsToRemove) { 606 LLVM_DEBUG(dbgs() << "IR Promotion: Removing " << *I << "\n"); 607 I->dropAllReferences(); 608 I->eraseFromParent(); 609 } 610 } 611 612 void IRPromoter::ConvertTruncs() { 613 LLVM_DEBUG(dbgs() << "IR Promotion: Converting truncs..\n"); 614 IRBuilder<> Builder{Ctx}; 615 616 for (auto *V : Visited) { 617 if (!isa<TruncInst>(V) || Sources.count(V)) 618 continue; 619 620 auto *Trunc = cast<TruncInst>(V); 621 Builder.SetInsertPoint(Trunc); 622 IntegerType *SrcTy = cast<IntegerType>(Trunc->getOperand(0)->getType()); 623 IntegerType *DestTy = cast<IntegerType>(TruncTysMap[Trunc][0]); 624 625 unsigned NumBits = DestTy->getScalarSizeInBits(); 626 ConstantInt *Mask = 627 ConstantInt::get(SrcTy, APInt::getMaxValue(NumBits).getZExtValue()); 628 Value *Masked = Builder.CreateAnd(Trunc->getOperand(0), Mask); 629 630 if (auto *I = dyn_cast<Instruction>(Masked)) 631 NewInsts.insert(I); 632 633 ReplaceAllUsersOfWith(Trunc, Masked); 634 } 635 } 636 637 void IRPromoter::Mutate() { 638 LLVM_DEBUG(dbgs() << "IR Promotion: Promoting use-def chains to " 639 << PromotedWidth << "-bits\n"); 640 641 // Cache original types of the values that will likely need truncating 642 for (auto *I : Sinks) { 643 if (auto *Call = dyn_cast<CallInst>(I)) { 644 for (Value *Arg : Call->args()) 645 TruncTysMap[Call].push_back(Arg->getType()); 646 } else if (auto *Switch = dyn_cast<SwitchInst>(I)) 647 TruncTysMap[I].push_back(Switch->getCondition()->getType()); 648 else { 649 for (unsigned i = 0; i < I->getNumOperands(); ++i) 650 TruncTysMap[I].push_back(I->getOperand(i)->getType()); 651 } 652 } 653 for (auto *V : Visited) { 654 if (!isa<TruncInst>(V) || Sources.count(V)) 655 continue; 656 auto *Trunc = cast<TruncInst>(V); 657 TruncTysMap[Trunc].push_back(Trunc->getDestTy()); 658 } 659 660 // Insert zext instructions between sources and their users. 661 ExtendSources(); 662 663 // Promote visited instructions, mutating their types in place. 664 PromoteTree(); 665 666 // Convert any truncs, that aren't sources, into AND masks. 667 ConvertTruncs(); 668 669 // Insert trunc instructions for use by calls, stores etc... 670 TruncateSinks(); 671 672 // Finally, remove unecessary zexts and truncs, delete old instructions and 673 // clear the data structures. 674 Cleanup(); 675 676 LLVM_DEBUG(dbgs() << "IR Promotion: Mutation complete\n"); 677 } 678 679 /// We disallow booleans to make life easier when dealing with icmps but allow 680 /// any other integer that fits in a scalar register. Void types are accepted 681 /// so we can handle switches. 682 bool TypePromotion::isSupportedType(Value *V) { 683 Type *Ty = V->getType(); 684 685 // Allow voids and pointers, these won't be promoted. 686 if (Ty->isVoidTy() || Ty->isPointerTy()) 687 return true; 688 689 if (!isa<IntegerType>(Ty) || cast<IntegerType>(Ty)->getBitWidth() == 1 || 690 cast<IntegerType>(Ty)->getBitWidth() > RegisterBitWidth) 691 return false; 692 693 return LessOrEqualTypeSize(V); 694 } 695 696 /// We accept most instructions, as well as Arguments and ConstantInsts. We 697 /// Disallow casts other than zext and truncs and only allow calls if their 698 /// return value is zeroext. We don't allow opcodes that can introduce sign 699 /// bits. 700 bool TypePromotion::isSupportedValue(Value *V) { 701 if (auto *I = dyn_cast<Instruction>(V)) { 702 switch (I->getOpcode()) { 703 default: 704 return isa<BinaryOperator>(I) && isSupportedType(I) && 705 !GenerateSignBits(I); 706 case Instruction::GetElementPtr: 707 case Instruction::Store: 708 case Instruction::Br: 709 case Instruction::Switch: 710 return true; 711 case Instruction::PHI: 712 case Instruction::Select: 713 case Instruction::Ret: 714 case Instruction::Load: 715 case Instruction::Trunc: 716 case Instruction::BitCast: 717 return isSupportedType(I); 718 case Instruction::ZExt: 719 return isSupportedType(I->getOperand(0)); 720 case Instruction::ICmp: 721 // Now that we allow small types than TypeSize, only allow icmp of 722 // TypeSize because they will require a trunc to be legalised. 723 // TODO: Allow icmp of smaller types, and calculate at the end 724 // whether the transform would be beneficial. 725 if (isa<PointerType>(I->getOperand(0)->getType())) 726 return true; 727 return EqualTypeSize(I->getOperand(0)); 728 case Instruction::Call: { 729 // Special cases for calls as we need to check for zeroext 730 // TODO We should accept calls even if they don't have zeroext, as they 731 // can still be sinks. 732 auto *Call = cast<CallInst>(I); 733 return isSupportedType(Call) && 734 Call->hasRetAttr(Attribute::AttrKind::ZExt); 735 } 736 } 737 } else if (isa<Constant>(V) && !isa<ConstantExpr>(V)) { 738 return isSupportedType(V); 739 } else if (isa<Argument>(V)) 740 return isSupportedType(V); 741 742 return isa<BasicBlock>(V); 743 } 744 745 /// Check that the type of V would be promoted and that the original type is 746 /// smaller than the targeted promoted type. Check that we're not trying to 747 /// promote something larger than our base 'TypeSize' type. 748 bool TypePromotion::isLegalToPromote(Value *V) { 749 auto *I = dyn_cast<Instruction>(V); 750 if (!I) 751 return true; 752 753 if (SafeToPromote.count(I)) 754 return true; 755 756 if (isPromotedResultSafe(I) || isSafeWrap(I)) { 757 SafeToPromote.insert(I); 758 return true; 759 } 760 return false; 761 } 762 763 bool TypePromotion::TryToPromote(Value *V, unsigned PromotedWidth) { 764 Type *OrigTy = V->getType(); 765 TypeSize = OrigTy->getPrimitiveSizeInBits().getFixedSize(); 766 SafeToPromote.clear(); 767 SafeWrap.clear(); 768 769 if (!isSupportedValue(V) || !shouldPromote(V) || !isLegalToPromote(V)) 770 return false; 771 772 LLVM_DEBUG(dbgs() << "IR Promotion: TryToPromote: " << *V << ", from " 773 << TypeSize << " bits to " << PromotedWidth << "\n"); 774 775 SetVector<Value *> WorkList; 776 SetVector<Value *> Sources; 777 SetVector<Instruction *> Sinks; 778 SetVector<Value *> CurrentVisited; 779 WorkList.insert(V); 780 781 // Return true if V was added to the worklist as a supported instruction, 782 // if it was already visited, or if we don't need to explore it (e.g. 783 // pointer values and GEPs), and false otherwise. 784 auto AddLegalInst = [&](Value *V) { 785 if (CurrentVisited.count(V)) 786 return true; 787 788 // Ignore GEPs because they don't need promoting and the constant indices 789 // will prevent the transformation. 790 if (isa<GetElementPtrInst>(V)) 791 return true; 792 793 if (!isSupportedValue(V) || (shouldPromote(V) && !isLegalToPromote(V))) { 794 LLVM_DEBUG(dbgs() << "IR Promotion: Can't handle: " << *V << "\n"); 795 return false; 796 } 797 798 WorkList.insert(V); 799 return true; 800 }; 801 802 // Iterate through, and add to, a tree of operands and users in the use-def. 803 while (!WorkList.empty()) { 804 Value *V = WorkList.pop_back_val(); 805 if (CurrentVisited.count(V)) 806 continue; 807 808 // Ignore non-instructions, other than arguments. 809 if (!isa<Instruction>(V) && !isSource(V)) 810 continue; 811 812 // If we've already visited this value from somewhere, bail now because 813 // the tree has already been explored. 814 // TODO: This could limit the transform, ie if we try to promote something 815 // from an i8 and fail first, before trying an i16. 816 if (AllVisited.count(V)) 817 return false; 818 819 CurrentVisited.insert(V); 820 AllVisited.insert(V); 821 822 // Calls can be both sources and sinks. 823 if (isSink(V)) 824 Sinks.insert(cast<Instruction>(V)); 825 826 if (isSource(V)) 827 Sources.insert(V); 828 829 if (!isSink(V) && !isSource(V)) { 830 if (auto *I = dyn_cast<Instruction>(V)) { 831 // Visit operands of any instruction visited. 832 for (auto &U : I->operands()) { 833 if (!AddLegalInst(U)) 834 return false; 835 } 836 } 837 } 838 839 // Don't visit users of a node which isn't going to be mutated unless its a 840 // source. 841 if (isSource(V) || shouldPromote(V)) { 842 for (Use &U : V->uses()) { 843 if (!AddLegalInst(U.getUser())) 844 return false; 845 } 846 } 847 } 848 849 LLVM_DEBUG({ 850 dbgs() << "IR Promotion: Visited nodes:\n"; 851 for (auto *I : CurrentVisited) 852 I->dump(); 853 }); 854 855 unsigned ToPromote = 0; 856 unsigned NonFreeArgs = 0; 857 SmallPtrSet<BasicBlock *, 4> Blocks; 858 for (auto *V : CurrentVisited) { 859 if (auto *I = dyn_cast<Instruction>(V)) 860 Blocks.insert(I->getParent()); 861 862 if (Sources.count(V)) { 863 if (auto *Arg = dyn_cast<Argument>(V)) 864 if (!Arg->hasZExtAttr() && !Arg->hasSExtAttr()) 865 ++NonFreeArgs; 866 continue; 867 } 868 869 if (Sinks.count(cast<Instruction>(V))) 870 continue; 871 ++ToPromote; 872 } 873 874 // DAG optimizations should be able to handle these cases better, especially 875 // for function arguments. 876 if (ToPromote < 2 || (Blocks.size() == 1 && (NonFreeArgs > SafeWrap.size()))) 877 return false; 878 879 IRPromoter Promoter(*Ctx, PromotedWidth, CurrentVisited, Sources, Sinks, 880 SafeWrap); 881 Promoter.Mutate(); 882 return true; 883 } 884 885 bool TypePromotion::runOnFunction(Function &F) { 886 if (skipFunction(F) || DisablePromotion) 887 return false; 888 889 LLVM_DEBUG(dbgs() << "IR Promotion: Running on " << F.getName() << "\n"); 890 891 auto *TPC = getAnalysisIfAvailable<TargetPassConfig>(); 892 if (!TPC) 893 return false; 894 895 AllVisited.clear(); 896 SafeToPromote.clear(); 897 SafeWrap.clear(); 898 bool MadeChange = false; 899 const DataLayout &DL = F.getParent()->getDataLayout(); 900 const TargetMachine &TM = TPC->getTM<TargetMachine>(); 901 const TargetSubtargetInfo *SubtargetInfo = TM.getSubtargetImpl(F); 902 const TargetLowering *TLI = SubtargetInfo->getTargetLowering(); 903 const TargetTransformInfo &TII = 904 getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); 905 RegisterBitWidth = 906 TII.getRegisterBitWidth(TargetTransformInfo::RGK_Scalar).getFixedSize(); 907 Ctx = &F.getParent()->getContext(); 908 909 // Search up from icmps to try to promote their operands. 910 for (BasicBlock &BB : F) { 911 for (Instruction &I : BB) { 912 if (AllVisited.count(&I)) 913 continue; 914 915 if (!isa<ICmpInst>(&I)) 916 continue; 917 918 auto *ICmp = cast<ICmpInst>(&I); 919 // Skip signed or pointer compares 920 if (ICmp->isSigned() || !isa<IntegerType>(ICmp->getOperand(0)->getType())) 921 continue; 922 923 LLVM_DEBUG(dbgs() << "IR Promotion: Searching from: " << *ICmp << "\n"); 924 925 for (auto &Op : ICmp->operands()) { 926 if (auto *I = dyn_cast<Instruction>(Op)) { 927 EVT SrcVT = TLI->getValueType(DL, I->getType()); 928 if (SrcVT.isSimple() && TLI->isTypeLegal(SrcVT.getSimpleVT())) 929 break; 930 931 if (TLI->getTypeAction(*Ctx, SrcVT) != 932 TargetLowering::TypePromoteInteger) 933 break; 934 EVT PromotedVT = TLI->getTypeToTransformTo(*Ctx, SrcVT); 935 if (RegisterBitWidth < PromotedVT.getFixedSizeInBits()) { 936 LLVM_DEBUG(dbgs() << "IR Promotion: Couldn't find target register " 937 << "for promoted type\n"); 938 break; 939 } 940 941 MadeChange |= TryToPromote(I, PromotedVT.getFixedSizeInBits()); 942 break; 943 } 944 } 945 } 946 } 947 948 AllVisited.clear(); 949 SafeToPromote.clear(); 950 SafeWrap.clear(); 951 952 return MadeChange; 953 } 954 955 INITIALIZE_PASS_BEGIN(TypePromotion, DEBUG_TYPE, PASS_NAME, false, false) 956 INITIALIZE_PASS_END(TypePromotion, DEBUG_TYPE, PASS_NAME, false, false) 957 958 char TypePromotion::ID = 0; 959 960 FunctionPass *llvm::createTypePromotionPass() { return new TypePromotion(); } 961