1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2022 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#include "EvaluateFreeze.hpp"
#include "Compiler/CodeGenContextWrapper.hpp"
#include "Compiler/MetaDataUtilsWrapper.h"
#include "Compiler/IGCPassSupport.h"
#include "common/LLVMWarningsPush.hpp"
#include <llvm/IR/IRBuilder.h>
#include <llvm/Pass.h>
#include "common/LLVMWarningsPop.hpp"
#include <vector>
using namespace llvm;
class EvaluateFreeze : public FunctionPass {
public:
static char ID;
EvaluateFreeze() : FunctionPass(ID) {
IGC::initializeEvaluateFreezePass(*PassRegistry::getPassRegistry());
}
bool runOnFunction(Function&) override;
void getAnalysisUsage(AnalysisUsage& AU) const override {
AU.setPreservesCFG();
}
private:
#if LLVM_VERSION_MAJOR >= 10
void evaluateInBasicBlock(
BasicBlock *,
std::vector<llvm::FreezeInst *> &RemList);
void evaluateFreezeInstUndef(FreezeInst*) const;
void evaluateFreezeInstNotUndef(FreezeInst*) const;
#endif // LLVM_VERSION_MAJOR >= 10
};
char EvaluateFreeze::ID = 0;
#define PASS_FLAG "igc-evaluate-freeze"
#define PASS_DESC "Evaluate Freeze Instructions"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
namespace IGC {
IGC_INITIALIZE_PASS_BEGIN(EvaluateFreeze, PASS_FLAG, PASS_DESC, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_END(EvaluateFreeze, PASS_FLAG, PASS_DESC, PASS_CFG_ONLY, PASS_ANALYSIS)
}
llvm::FunctionPass* IGC::createEvaluateFreezePass()
{
return new EvaluateFreeze();
}
bool EvaluateFreeze::runOnFunction(Function& F) {
#if LLVM_VERSION_MAJOR >= 10
std::vector<llvm::FreezeInst *> RemList;
for (auto& BB : F)
evaluateInBasicBlock(&BB, RemList);
for (FreezeInst *FI : RemList)
FI->eraseFromParent();
return !RemList.empty();
#else // LLVM_VERSION_MAJOR >= 10
return false;
#endif // LLVM_VERSION_MAJOR >= 10
}
#if LLVM_VERSION_MAJOR >= 10
// A freeze is idempotent on anything that is not %poison or %undef
// For those we return an arbitrary value. We actually make it deterministic
// for all cases just for sanity sake.
//
// %freeze_dst = freeze i32 %valid
// => replace all uses of %freeze_dst with %valid
//
// %freeze_dst = freeze T %undef
// where T is:
// iX = replace with constant some magic int (i1 is 0)
// fX = replace with constant 0.0
//
void EvaluateFreeze::evaluateInBasicBlock(
BasicBlock* BB,
std::vector<llvm::FreezeInst *> &RemList)
{
for (auto BI = BB->begin(), BE = BB->end(); BI != BE; /* EMPTY */) {
Instruction* I = &(*BI++);
if (FreezeInst *FI = dyn_cast<FreezeInst>(I)) {
if (isa<UndefValue>(FI->getOperand(0)))
evaluateFreezeInstUndef(FI);
else
evaluateFreezeInstNotUndef(FI);
RemList.push_back(FI);
}
}
}
void EvaluateFreeze::evaluateFreezeInstUndef(FreezeInst* FI) const
{
// Undef or poison value
// Replace all uses with a fixed value.
Value *Op = FI->getOperand(0);
Type *OpTy = Op->getType();
if (OpTy->isIntegerTy()) {
IntegerType *IT = (IntegerType *)OpTy;
//
// Give the folks debugging raw assembly a fighting chance
uint64_t MagicVal =
IT->getBitWidth() >= 64 ? 0xF4EE7E00F4EE7E00ull :
IT->getBitWidth() >= 32 ? 0xF4EE7E00ull :
IT->getBitWidth() >= 16 ? 0xF47Eull :
0x0; // branches get false
ConstantInt *CI = ConstantInt::get(IT, MagicVal, false);
FI->replaceAllUsesWith(CI);
} else if (OpTy->isFloatingPointTy()) {
Constant *C = Constant::getNullValue(OpTy);
FI->replaceAllUsesWith(C);
} else {
IGC_ASSERT_MESSAGE(0, "unsupported type in freeze");
}
}
void EvaluateFreeze::evaluateFreezeInstNotUndef(FreezeInst* FI) const
{
// Not an undef value ==> just propagate the operand as the result
llvm::Value *Op = FI->getOperand(0);
FI->replaceAllUsesWith(Op);
}
#endif // LLVM_VERSION_MAJOR >= 10
|