comparison lib/IR/Instruction.cpp @ 148:63bd29f05246

merged
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 19:46:37 +0900
parents c2174574ed3a
children
comparison
equal deleted inserted replaced
146:3fc4d5c3e21e 148:63bd29f05246
1 //===-- Instruction.cpp - Implement the Instruction class -----------------===// 1 //===-- Instruction.cpp - Implement the Instruction class -----------------===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // 4 // See https://llvm.org/LICENSE.txt for license information.
5 // This file is distributed under the University of Illinois Open Source 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 // License. See LICENSE.TXT for details.
7 // 6 //
8 //===----------------------------------------------------------------------===// 7 //===----------------------------------------------------------------------===//
9 // 8 //
10 // This file implements the Instruction class for the IR library. 9 // This file implements the Instruction class for the IR library.
11 // 10 //
12 //===----------------------------------------------------------------------===// 11 //===----------------------------------------------------------------------===//
13 12
14 #include "llvm/IR/Instruction.h" 13 #include "llvm/IR/Instruction.h"
14 #include "llvm/IR/IntrinsicInst.h"
15 #include "llvm/ADT/DenseSet.h" 15 #include "llvm/ADT/DenseSet.h"
16 #include "llvm/IR/Constants.h" 16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/Instructions.h" 17 #include "llvm/IR/Instructions.h"
18 #include "llvm/IR/MDBuilder.h" 18 #include "llvm/IR/MDBuilder.h"
19 #include "llvm/IR/Operator.h" 19 #include "llvm/IR/Operator.h"
136 136
137 case Instruction::GetElementPtr: 137 case Instruction::GetElementPtr:
138 cast<GetElementPtrInst>(this)->setIsInBounds(false); 138 cast<GetElementPtrInst>(this)->setIsInBounds(false);
139 break; 139 break;
140 } 140 }
141 } 141 // TODO: FastMathFlags!
142 }
143
142 144
143 bool Instruction::isExact() const { 145 bool Instruction::isExact() const {
144 return cast<PossiblyExactOperator>(this)->isExact(); 146 return cast<PossiblyExactOperator>(this)->isExact();
145 } 147 }
146 148
299 case Unreachable: return "unreachable"; 301 case Unreachable: return "unreachable";
300 case CleanupRet: return "cleanupret"; 302 case CleanupRet: return "cleanupret";
301 case CatchRet: return "catchret"; 303 case CatchRet: return "catchret";
302 case CatchPad: return "catchpad"; 304 case CatchPad: return "catchpad";
303 case CatchSwitch: return "catchswitch"; 305 case CatchSwitch: return "catchswitch";
306 case CallBr: return "callbr";
307
308 // Standard unary operators...
309 case FNeg: return "fneg";
304 310
305 // Standard binary operators... 311 // Standard binary operators...
306 case Add: return "add"; 312 case Add: return "add";
307 case FAdd: return "fadd"; 313 case FAdd: return "fadd";
308 case Sub: return "sub"; 314 case Sub: return "sub";
400 CI->hasIdenticalOperandBundleSchema(*cast<CallInst>(I2)); 406 CI->hasIdenticalOperandBundleSchema(*cast<CallInst>(I2));
401 if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1)) 407 if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1))
402 return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() && 408 return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() &&
403 CI->getAttributes() == cast<InvokeInst>(I2)->getAttributes() && 409 CI->getAttributes() == cast<InvokeInst>(I2)->getAttributes() &&
404 CI->hasIdenticalOperandBundleSchema(*cast<InvokeInst>(I2)); 410 CI->hasIdenticalOperandBundleSchema(*cast<InvokeInst>(I2));
411 if (const CallBrInst *CI = dyn_cast<CallBrInst>(I1))
412 return CI->getCallingConv() == cast<CallBrInst>(I2)->getCallingConv() &&
413 CI->getAttributes() == cast<CallBrInst>(I2)->getAttributes() &&
414 CI->hasIdenticalOperandBundleSchema(*cast<CallBrInst>(I2));
405 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1)) 415 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1))
406 return IVI->getIndices() == cast<InsertValueInst>(I2)->getIndices(); 416 return IVI->getIndices() == cast<InsertValueInst>(I2)->getIndices();
407 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1)) 417 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1))
408 return EVI->getIndices() == cast<ExtractValueInst>(I2)->getIndices(); 418 return EVI->getIndices() == cast<ExtractValueInst>(I2)->getIndices();
409 if (const FenceInst *FI = dyn_cast<FenceInst>(I1)) 419 if (const FenceInst *FI = dyn_cast<FenceInst>(I1))
510 case Instruction::AtomicRMW: 520 case Instruction::AtomicRMW:
511 case Instruction::CatchPad: 521 case Instruction::CatchPad:
512 case Instruction::CatchRet: 522 case Instruction::CatchRet:
513 return true; 523 return true;
514 case Instruction::Call: 524 case Instruction::Call:
515 return !cast<CallInst>(this)->doesNotAccessMemory();
516 case Instruction::Invoke: 525 case Instruction::Invoke:
517 return !cast<InvokeInst>(this)->doesNotAccessMemory(); 526 case Instruction::CallBr:
527 return !cast<CallBase>(this)->doesNotAccessMemory();
518 case Instruction::Store: 528 case Instruction::Store:
519 return !cast<StoreInst>(this)->isUnordered(); 529 return !cast<StoreInst>(this)->isUnordered();
520 } 530 }
521 } 531 }
522 532
530 case Instruction::AtomicRMW: 540 case Instruction::AtomicRMW:
531 case Instruction::CatchPad: 541 case Instruction::CatchPad:
532 case Instruction::CatchRet: 542 case Instruction::CatchRet:
533 return true; 543 return true;
534 case Instruction::Call: 544 case Instruction::Call:
535 return !cast<CallInst>(this)->onlyReadsMemory();
536 case Instruction::Invoke: 545 case Instruction::Invoke:
537 return !cast<InvokeInst>(this)->onlyReadsMemory(); 546 case Instruction::CallBr:
547 return !cast<CallBase>(this)->onlyReadsMemory();
538 case Instruction::Load: 548 case Instruction::Load:
539 return !cast<LoadInst>(this)->isUnordered(); 549 return !cast<LoadInst>(this)->isUnordered();
540 } 550 }
541 } 551 }
542 552
589 return isa<ResumeInst>(this); 599 return isa<ResumeInst>(this);
590 } 600 }
591 601
592 bool Instruction::isSafeToRemove() const { 602 bool Instruction::isSafeToRemove() const {
593 return (!isa<CallInst>(this) || !this->mayHaveSideEffects()) && 603 return (!isa<CallInst>(this) || !this->mayHaveSideEffects()) &&
594 !isa<TerminatorInst>(this); 604 !this->isTerminator();
605 }
606
607 bool Instruction::isLifetimeStartOrEnd() const {
608 auto II = dyn_cast<IntrinsicInst>(this);
609 if (!II)
610 return false;
611 Intrinsic::ID ID = II->getIntrinsicID();
612 return ID == Intrinsic::lifetime_start || ID == Intrinsic::lifetime_end;
613 }
614
615 const Instruction *Instruction::getNextNonDebugInstruction() const {
616 for (const Instruction *I = getNextNode(); I; I = I->getNextNode())
617 if (!isa<DbgInfoIntrinsic>(I))
618 return I;
619 return nullptr;
620 }
621
622 const Instruction *Instruction::getPrevNonDebugInstruction() const {
623 for (const Instruction *I = getPrevNode(); I; I = I->getPrevNode())
624 if (!isa<DbgInfoIntrinsic>(I))
625 return I;
626 return nullptr;
595 } 627 }
596 628
597 bool Instruction::isAssociative() const { 629 bool Instruction::isAssociative() const {
598 unsigned Opcode = getOpcode(); 630 unsigned Opcode = getOpcode();
599 if (isAssociative(Opcode)) 631 if (isAssociative(Opcode))
600 return true; 632 return true;
601 633
602 switch (Opcode) { 634 switch (Opcode) {
603 case FMul: 635 case FMul:
604 case FAdd: 636 case FAdd:
605 return cast<FPMathOperator>(this)->isFast(); 637 return cast<FPMathOperator>(this)->hasAllowReassoc() &&
638 cast<FPMathOperator>(this)->hasNoSignedZeros();
606 default: 639 default:
607 return false; 640 return false;
608 } 641 }
642 }
643
644 unsigned Instruction::getNumSuccessors() const {
645 switch (getOpcode()) {
646 #define HANDLE_TERM_INST(N, OPC, CLASS) \
647 case Instruction::OPC: \
648 return static_cast<const CLASS *>(this)->getNumSuccessors();
649 #include "llvm/IR/Instruction.def"
650 default:
651 break;
652 }
653 llvm_unreachable("not a terminator");
654 }
655
656 BasicBlock *Instruction::getSuccessor(unsigned idx) const {
657 switch (getOpcode()) {
658 #define HANDLE_TERM_INST(N, OPC, CLASS) \
659 case Instruction::OPC: \
660 return static_cast<const CLASS *>(this)->getSuccessor(idx);
661 #include "llvm/IR/Instruction.def"
662 default:
663 break;
664 }
665 llvm_unreachable("not a terminator");
666 }
667
668 void Instruction::setSuccessor(unsigned idx, BasicBlock *B) {
669 switch (getOpcode()) {
670 #define HANDLE_TERM_INST(N, OPC, CLASS) \
671 case Instruction::OPC: \
672 return static_cast<CLASS *>(this)->setSuccessor(idx, B);
673 #include "llvm/IR/Instruction.def"
674 default:
675 break;
676 }
677 llvm_unreachable("not a terminator");
678 }
679
680 void Instruction::replaceSuccessorWith(BasicBlock *OldBB, BasicBlock *NewBB) {
681 for (unsigned Idx = 0, NumSuccessors = Instruction::getNumSuccessors();
682 Idx != NumSuccessors; ++Idx)
683 if (getSuccessor(Idx) == OldBB)
684 setSuccessor(Idx, NewBB);
609 } 685 }
610 686
611 Instruction *Instruction::cloneImpl() const { 687 Instruction *Instruction::cloneImpl() const {
612 llvm_unreachable("Subclass of Instruction failed to implement cloneImpl"); 688 llvm_unreachable("Subclass of Instruction failed to implement cloneImpl");
613 } 689 }
666 New->SubclassOptionalData = SubclassOptionalData; 742 New->SubclassOptionalData = SubclassOptionalData;
667 New->copyMetadata(*this); 743 New->copyMetadata(*this);
668 return New; 744 return New;
669 } 745 }
670 746
671 void Instruction::updateProfWeight(uint64_t S, uint64_t T) {
672 auto *ProfileData = getMetadata(LLVMContext::MD_prof);
673 if (ProfileData == nullptr)
674 return;
675
676 auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
677 if (!ProfDataName || (!ProfDataName->getString().equals("branch_weights") &&
678 !ProfDataName->getString().equals("VP")))
679 return;
680
681 MDBuilder MDB(getContext());
682 SmallVector<Metadata *, 3> Vals;
683 Vals.push_back(ProfileData->getOperand(0));
684 APInt APS(128, S), APT(128, T);
685 if (ProfDataName->getString().equals("branch_weights"))
686 for (unsigned i = 1; i < ProfileData->getNumOperands(); i++) {
687 // Using APInt::div may be expensive, but most cases should fit 64 bits.
688 APInt Val(128,
689 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i))
690 ->getValue()
691 .getZExtValue());
692 Val *= APS;
693 Vals.push_back(MDB.createConstant(
694 ConstantInt::get(Type::getInt64Ty(getContext()),
695 Val.udiv(APT).getLimitedValue())));
696 }
697 else if (ProfDataName->getString().equals("VP"))
698 for (unsigned i = 1; i < ProfileData->getNumOperands(); i += 2) {
699 // The first value is the key of the value profile, which will not change.
700 Vals.push_back(ProfileData->getOperand(i));
701 // Using APInt::div may be expensive, but most cases should fit 64 bits.
702 APInt Val(128,
703 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i + 1))
704 ->getValue()
705 .getZExtValue());
706 Val *= APS;
707 Vals.push_back(MDB.createConstant(
708 ConstantInt::get(Type::getInt64Ty(getContext()),
709 Val.udiv(APT).getLimitedValue())));
710 }
711 setMetadata(LLVMContext::MD_prof, MDNode::get(getContext(), Vals));
712 }
713
714 void Instruction::setProfWeight(uint64_t W) { 747 void Instruction::setProfWeight(uint64_t W) {
715 assert((isa<CallInst>(this) || isa<InvokeInst>(this)) && 748 assert(isa<CallBase>(this) &&
716 "Can only set weights for call and invoke instrucitons"); 749 "Can only set weights for call like instructions");
717 SmallVector<uint32_t, 1> Weights; 750 SmallVector<uint32_t, 1> Weights;
718 Weights.push_back(W); 751 Weights.push_back(W);
719 MDBuilder MDB(getContext()); 752 MDBuilder MDB(getContext());
720 setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights)); 753 setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights));
721 } 754 }