2018-07-23 20:27:47 +08:00
|
|
|
//===----- ARMCodeGenPrepare.cpp ------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
/// \file
|
|
|
|
/// This pass inserts intrinsics to handle small types that would otherwise be
|
|
|
|
/// promoted during legalization. Here we can manually promote types or insert
|
|
|
|
/// intrinsics which can handle narrow types that aren't supported by the
|
|
|
|
/// register classes.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ARM.h"
|
|
|
|
#include "ARMSubtarget.h"
|
|
|
|
#include "ARMTargetMachine.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/CodeGen/TargetPassConfig.h"
|
|
|
|
#include "llvm/IR/Attributes.h"
|
|
|
|
#include "llvm/IR/BasicBlock.h"
|
|
|
|
#include "llvm/IR/IRBuilder.h"
|
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/InstrTypes.h"
|
|
|
|
#include "llvm/IR/Instruction.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/Intrinsics.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
|
|
|
#include "llvm/IR/Value.h"
|
|
|
|
#include "llvm/IR/Verifier.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "arm-codegenprepare"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
2018-09-24 19:40:07 +08:00
|
|
|
DisableCGP("arm-disable-cgp", cl::Hidden, cl::init(true),
|
2018-07-23 20:27:47 +08:00
|
|
|
cl::desc("Disable ARM specific CodeGenPrepare pass"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
EnableDSP("arm-enable-scalar-dsp", cl::Hidden, cl::init(false),
|
|
|
|
cl::desc("Use DSP instructions for scalar operations"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
EnableDSPWithImms("arm-enable-scalar-dsp-imms", cl::Hidden, cl::init(false),
|
|
|
|
cl::desc("Use DSP instructions for scalar operations\
|
|
|
|
with immediate operands"));
|
|
|
|
|
2018-08-17 15:34:01 +08:00
|
|
|
// The goal of this pass is to enable more efficient code generation for
|
|
|
|
// operations on narrow types (i.e. types with < 32-bits) and this is a
|
|
|
|
// motivating IR code example:
|
|
|
|
//
|
|
|
|
// define hidden i32 @cmp(i8 zeroext) {
|
|
|
|
// %2 = add i8 %0, -49
|
|
|
|
// %3 = icmp ult i8 %2, 3
|
|
|
|
// ..
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// The issue here is that i8 is type-legalized to i32 because i8 is not a
|
|
|
|
// legal type. Thus, arithmetic is done in integer-precision, but then the
|
|
|
|
// byte value is masked out as follows:
|
|
|
|
//
|
|
|
|
// t19: i32 = add t4, Constant:i32<-49>
|
|
|
|
// t24: i32 = and t19, Constant:i32<255>
|
|
|
|
//
|
|
|
|
// Consequently, we generate code like this:
|
|
|
|
//
|
|
|
|
// subs r0, #49
|
|
|
|
// uxtb r1, r0
|
|
|
|
// cmp r1, #3
|
|
|
|
//
|
|
|
|
// This shows that masking out the byte value results in generation of
|
|
|
|
// the UXTB instruction. This is not optimal as r0 already contains the byte
|
|
|
|
// value we need, and so instead we can just generate:
|
|
|
|
//
|
|
|
|
// sub.w r1, r0, #49
|
|
|
|
// cmp r1, #3
|
|
|
|
//
|
|
|
|
// We achieve this by type promoting the IR to i32 like so for this example:
|
|
|
|
//
|
|
|
|
// define i32 @cmp(i8 zeroext %c) {
|
|
|
|
// %0 = zext i8 %c to i32
|
|
|
|
// %c.off = add i32 %0, -49
|
|
|
|
// %1 = icmp ult i32 %c.off, 3
|
|
|
|
// ..
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// For this to be valid and legal, we need to prove that the i32 add is
|
|
|
|
// producing the same value as the i8 addition, and that e.g. no overflow
|
|
|
|
// happens.
|
|
|
|
//
|
|
|
|
// A brief sketch of the algorithm and some terminology.
|
|
|
|
// We pattern match interesting IR patterns:
|
|
|
|
// - which have "sources": instructions producing narrow values (i8, i16), and
|
|
|
|
// - they have "sinks": instructions consuming these narrow values.
|
|
|
|
//
|
|
|
|
// We collect all instruction connecting sources and sinks in a worklist, so
|
|
|
|
// that we can mutate these instruction and perform type promotion when it is
|
|
|
|
// legal to do so.
|
2018-07-23 20:27:47 +08:00
|
|
|
|
2018-08-17 15:34:01 +08:00
|
|
|
namespace {
|
2018-07-23 20:27:47 +08:00
|
|
|
class IRPromoter {
|
|
|
|
SmallPtrSet<Value*, 8> NewInsts;
|
|
|
|
SmallVector<Instruction*, 4> InstsToRemove;
|
2018-11-01 23:23:42 +08:00
|
|
|
DenseMap<Value*, Type*> TruncTysMap;
|
|
|
|
SmallPtrSet<Value*, 8> Promoted;
|
2018-07-23 20:27:47 +08:00
|
|
|
Module *M = nullptr;
|
|
|
|
LLVMContext &Ctx;
|
2018-11-05 18:58:37 +08:00
|
|
|
IntegerType *ExtTy = nullptr;
|
|
|
|
IntegerType *OrigTy = nullptr;
|
2018-11-01 23:23:42 +08:00
|
|
|
|
|
|
|
void PrepareConstants(SmallPtrSetImpl<Value*> &Visited,
|
|
|
|
SmallPtrSetImpl<Instruction*> &SafeToPromote);
|
|
|
|
void ExtendSources(SmallPtrSetImpl<Value*> &Sources);
|
|
|
|
void PromoteTree(SmallPtrSetImpl<Value*> &Visited,
|
|
|
|
SmallPtrSetImpl<Value*> &Sources,
|
|
|
|
SmallPtrSetImpl<Instruction*> &Sinks,
|
|
|
|
SmallPtrSetImpl<Instruction*> &SafeToPromote);
|
|
|
|
void TruncateSinks(SmallPtrSetImpl<Value*> &Sources,
|
|
|
|
SmallPtrSetImpl<Instruction*> &Sinks);
|
2018-11-05 18:58:37 +08:00
|
|
|
void Cleanup(SmallPtrSetImpl<Instruction*> &Sinks);
|
2018-07-23 20:27:47 +08:00
|
|
|
|
|
|
|
public:
|
2018-11-01 23:23:42 +08:00
|
|
|
IRPromoter(Module *M) : M(M), Ctx(M->getContext()),
|
|
|
|
ExtTy(Type::getInt32Ty(Ctx)) { }
|
2018-07-23 20:27:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
void Mutate(Type *OrigTy,
|
|
|
|
SmallPtrSetImpl<Value*> &Visited,
|
2018-08-17 15:34:01 +08:00
|
|
|
SmallPtrSetImpl<Value*> &Sources,
|
2018-11-01 23:23:42 +08:00
|
|
|
SmallPtrSetImpl<Instruction*> &Sinks,
|
|
|
|
SmallPtrSetImpl<Instruction*> &SafeToPromote);
|
2018-07-23 20:27:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class ARMCodeGenPrepare : public FunctionPass {
|
|
|
|
const ARMSubtarget *ST = nullptr;
|
|
|
|
IRPromoter *Promoter = nullptr;
|
|
|
|
std::set<Value*> AllVisited;
|
2018-11-01 23:23:42 +08:00
|
|
|
SmallPtrSet<Instruction*, 8> SafeToPromote;
|
2018-07-23 20:27:47 +08:00
|
|
|
|
2018-11-01 23:23:42 +08:00
|
|
|
bool isSafeOverflow(Instruction *I);
|
2018-07-23 20:27:47 +08:00
|
|
|
bool isSupportedValue(Value *V);
|
|
|
|
bool isLegalToPromote(Value *V);
|
|
|
|
bool TryToPromote(Value *V);
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
2018-08-10 21:57:13 +08:00
|
|
|
static unsigned TypeSize;
|
|
|
|
Type *OrigTy = nullptr;
|
2018-07-23 20:27:47 +08:00
|
|
|
|
|
|
|
ARMCodeGenPrepare() : FunctionPass(ID) {}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.addRequired<TargetPassConfig>();
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef getPassName() const override { return "ARM IR optimizations"; }
|
|
|
|
|
|
|
|
bool doInitialization(Module &M) override;
|
|
|
|
bool runOnFunction(Function &F) override;
|
2018-07-24 01:00:45 +08:00
|
|
|
bool doFinalization(Module &M) override;
|
2018-07-23 20:27:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-17 15:34:01 +08:00
|
|
|
static bool generateSignBits(Value *V) {
|
2018-07-23 20:27:47 +08:00
|
|
|
if (!isa<Instruction>(V))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned Opc = cast<Instruction>(V)->getOpcode();
|
|
|
|
return Opc == Instruction::AShr || Opc == Instruction::SDiv ||
|
|
|
|
Opc == Instruction::SRem;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Some instructions can use 8- and 16-bit operands, and we don't need to
|
|
|
|
/// promote anything larger. We disallow booleans to make life easier when
|
|
|
|
/// dealing with icmps but allow any other integer that is <= 16 bits. Void
|
|
|
|
/// types are accepted so we can handle switches.
|
|
|
|
static bool isSupportedType(Value *V) {
|
2018-08-10 21:57:13 +08:00
|
|
|
Type *Ty = V->getType();
|
2018-08-15 15:52:35 +08:00
|
|
|
|
|
|
|
// Allow voids and pointers, these won't be promoted.
|
|
|
|
if (Ty->isVoidTy() || Ty->isPointerTy())
|
2018-07-23 20:27:47 +08:00
|
|
|
return true;
|
|
|
|
|
2018-08-10 21:57:13 +08:00
|
|
|
if (auto *Ld = dyn_cast<LoadInst>(V))
|
|
|
|
Ty = cast<PointerType>(Ld->getPointerOperandType())->getElementType();
|
2018-07-23 20:27:47 +08:00
|
|
|
|
2018-08-10 21:57:13 +08:00
|
|
|
const IntegerType *IntTy = dyn_cast<IntegerType>(Ty);
|
2018-09-13 23:14:12 +08:00
|
|
|
if (!IntTy)
|
2018-07-23 20:27:47 +08:00
|
|
|
return false;
|
|
|
|
|
2018-08-10 21:57:13 +08:00
|
|
|
return IntTy->getBitWidth() == ARMCodeGenPrepare::TypeSize;
|
|
|
|
}
|
2018-07-23 20:27:47 +08:00
|
|
|
|
2018-08-17 15:34:01 +08:00
|
|
|
/// Return true if the given value is a source in the use-def chain, producing
|
2018-08-10 21:57:13 +08:00
|
|
|
/// a narrow (i8, i16) value. These values will be zext to start the promotion
|
|
|
|
/// of the tree to i32. We guarantee that these won't populate the upper bits
|
|
|
|
/// of the register. ZExt on the loads will be free, and the same for call
|
|
|
|
/// return values because we only accept ones that guarantee a zeroext ret val.
|
|
|
|
/// Many arguments will have the zeroext attribute too, so those would be free
|
|
|
|
/// too.
|
|
|
|
static bool isSource(Value *V) {
|
2018-08-15 15:52:35 +08:00
|
|
|
if (!isa<IntegerType>(V->getType()))
|
|
|
|
return false;
|
2018-09-18 08:11:55 +08:00
|
|
|
// TODO Allow zext to be sources.
|
|
|
|
if (isa<Argument>(V))
|
2018-09-12 17:11:48 +08:00
|
|
|
return true;
|
2018-09-18 08:11:55 +08:00
|
|
|
else if (isa<LoadInst>(V))
|
|
|
|
return true;
|
|
|
|
else if (isa<BitCastInst>(V))
|
|
|
|
return true;
|
|
|
|
else if (auto *Call = dyn_cast<CallInst>(V))
|
|
|
|
return Call->hasRetAttr(Attribute::AttrKind::ZExt);
|
|
|
|
else if (auto *Trunc = dyn_cast<TruncInst>(V))
|
|
|
|
return isSupportedType(Trunc);
|
2018-08-10 21:57:13 +08:00
|
|
|
return false;
|
2018-07-23 20:27:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return true if V will require any promoted values to be truncated for the
|
2018-08-10 21:57:13 +08:00
|
|
|
/// the IR to remain valid. We can't mutate the value type of these
|
|
|
|
/// instructions.
|
2018-07-23 20:27:47 +08:00
|
|
|
static bool isSink(Value *V) {
|
2018-08-10 21:57:13 +08:00
|
|
|
// TODO The truncate also isn't actually necessary because we would already
|
|
|
|
// proved that the data value is kept within the range of the original data
|
|
|
|
// type.
|
2018-07-23 20:27:47 +08:00
|
|
|
auto UsesNarrowValue = [](Value *V) {
|
2018-08-10 21:57:13 +08:00
|
|
|
return V->getType()->getScalarSizeInBits() == ARMCodeGenPrepare::TypeSize;
|
2018-07-23 20:27:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (auto *Store = dyn_cast<StoreInst>(V))
|
|
|
|
return UsesNarrowValue(Store->getValueOperand());
|
|
|
|
if (auto *Return = dyn_cast<ReturnInst>(V))
|
|
|
|
return UsesNarrowValue(Return->getReturnValue());
|
2018-08-10 21:57:13 +08:00
|
|
|
if (auto *Trunc = dyn_cast<TruncInst>(V))
|
|
|
|
return UsesNarrowValue(Trunc->getOperand(0));
|
2018-08-16 19:54:09 +08:00
|
|
|
if (auto *ZExt = dyn_cast<ZExtInst>(V))
|
|
|
|
return UsesNarrowValue(ZExt->getOperand(0));
|
2018-08-16 18:05:39 +08:00
|
|
|
if (auto *ICmp = dyn_cast<ICmpInst>(V))
|
|
|
|
return ICmp->isSigned();
|
2018-07-23 20:27:47 +08:00
|
|
|
|
|
|
|
return isa<CallInst>(V);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return whether the instruction can be promoted within any modifications to
|
2018-11-01 23:23:42 +08:00
|
|
|
/// its operands or result.
|
|
|
|
bool ARMCodeGenPrepare::isSafeOverflow(Instruction *I) {
|
2018-08-10 21:57:13 +08:00
|
|
|
// FIXME Do we need NSW too?
|
2018-07-23 20:27:47 +08:00
|
|
|
if (isa<OverflowingBinaryOperator>(I) && I->hasNoUnsignedWrap())
|
|
|
|
return true;
|
|
|
|
|
2018-09-26 18:56:00 +08:00
|
|
|
// We can support a, potentially, overflowing instruction (I) if:
|
|
|
|
// - It is only used by an unsigned icmp.
|
|
|
|
// - The icmp uses a constant.
|
|
|
|
// - The overflowing value (I) is decreasing, i.e would underflow - wrapping
|
|
|
|
// around zero to become a larger number than before.
|
|
|
|
// - The underflowing instruction (I) also uses a constant.
|
|
|
|
//
|
|
|
|
// We can then use the two constants to calculate whether the result would
|
|
|
|
// wrap in respect to itself in the original bitwidth. If it doesn't wrap,
|
|
|
|
// just underflows the range, the icmp would give the same result whether the
|
|
|
|
// result has been truncated or not. We calculate this by:
|
|
|
|
// - Zero extending both constants, if needed, to 32-bits.
|
|
|
|
// - Take the absolute value of I's constant, adding this to the icmp const.
|
|
|
|
// - Check that this value is not out of range for small type. If it is, it
|
|
|
|
// means that it has underflowed enough to wrap around the icmp constant.
|
|
|
|
//
|
|
|
|
// For example:
|
|
|
|
//
|
|
|
|
// %sub = sub i8 %a, 2
|
|
|
|
// %cmp = icmp ule i8 %sub, 254
|
|
|
|
//
|
|
|
|
// If %a = 0, %sub = -2 == FE == 254
|
|
|
|
// But if this is evalulated as a i32
|
|
|
|
// %sub = -2 == FF FF FF FE == 4294967294
|
|
|
|
// So the unsigned compares (i8 and i32) would not yield the same result.
|
|
|
|
//
|
|
|
|
// Another way to look at it is:
|
|
|
|
// %a - 2 <= 254
|
|
|
|
// %a + 2 <= 254 + 2
|
|
|
|
// %a <= 256
|
|
|
|
// And we can't represent 256 in the i8 format, so we don't support it.
|
|
|
|
//
|
|
|
|
// Whereas:
|
|
|
|
//
|
|
|
|
// %sub i8 %a, 1
|
|
|
|
// %cmp = icmp ule i8 %sub, 254
|
|
|
|
//
|
|
|
|
// If %a = 0, %sub = -1 == FF == 255
|
|
|
|
// As i32:
|
|
|
|
// %sub = -1 == FF FF FF FF == 4294967295
|
|
|
|
//
|
|
|
|
// In this case, the unsigned compare results would be the same and this
|
|
|
|
// would also be true for ult, uge and ugt:
|
|
|
|
// - (255 < 254) == (0xFFFFFFFF < 254) == false
|
|
|
|
// - (255 <= 254) == (0xFFFFFFFF <= 254) == false
|
|
|
|
// - (255 > 254) == (0xFFFFFFFF > 254) == true
|
|
|
|
// - (255 >= 254) == (0xFFFFFFFF >= 254) == true
|
|
|
|
//
|
|
|
|
// To demonstrate why we can't handle increasing values:
|
|
|
|
//
|
|
|
|
// %add = add i8 %a, 2
|
|
|
|
// %cmp = icmp ult i8 %add, 127
|
|
|
|
//
|
|
|
|
// If %a = 254, %add = 256 == (i8 1)
|
|
|
|
// As i32:
|
|
|
|
// %add = 256
|
|
|
|
//
|
|
|
|
// (1 < 127) != (256 < 127)
|
|
|
|
|
2018-07-23 20:27:47 +08:00
|
|
|
unsigned Opc = I->getOpcode();
|
2018-09-26 18:56:00 +08:00
|
|
|
if (Opc != Instruction::Add && Opc != Instruction::Sub)
|
|
|
|
return false;
|
2018-07-23 20:27:47 +08:00
|
|
|
|
2018-09-26 18:56:00 +08:00
|
|
|
if (!I->hasOneUse() ||
|
|
|
|
!isa<ICmpInst>(*I->user_begin()) ||
|
|
|
|
!isa<ConstantInt>(I->getOperand(1)))
|
|
|
|
return false;
|
2018-07-23 20:27:47 +08:00
|
|
|
|
2018-09-26 18:56:00 +08:00
|
|
|
ConstantInt *OverflowConst = cast<ConstantInt>(I->getOperand(1));
|
|
|
|
bool NegImm = OverflowConst->isNegative();
|
|
|
|
bool IsDecreasing = ((Opc == Instruction::Sub) && !NegImm) ||
|
|
|
|
((Opc == Instruction::Add) && NegImm);
|
|
|
|
if (!IsDecreasing)
|
|
|
|
return false;
|
2018-09-17 21:48:25 +08:00
|
|
|
|
2018-09-26 18:56:00 +08:00
|
|
|
// Don't support an icmp that deals with sign bits.
|
|
|
|
auto *CI = cast<ICmpInst>(*I->user_begin());
|
|
|
|
if (CI->isSigned() || CI->isEquality())
|
|
|
|
return false;
|
2018-09-17 21:48:25 +08:00
|
|
|
|
2018-09-26 18:56:00 +08:00
|
|
|
ConstantInt *ICmpConst = nullptr;
|
|
|
|
if (auto *Const = dyn_cast<ConstantInt>(CI->getOperand(0)))
|
|
|
|
ICmpConst = Const;
|
|
|
|
else if (auto *Const = dyn_cast<ConstantInt>(CI->getOperand(1)))
|
|
|
|
ICmpConst = Const;
|
|
|
|
else
|
|
|
|
return false;
|
2018-07-23 20:27:47 +08:00
|
|
|
|
2018-09-26 18:56:00 +08:00
|
|
|
// Now check that the result can't wrap on itself.
|
|
|
|
APInt Total = ICmpConst->getValue().getBitWidth() < 32 ?
|
|
|
|
ICmpConst->getValue().zext(32) : ICmpConst->getValue();
|
2018-07-23 20:27:47 +08:00
|
|
|
|
2018-09-26 18:56:00 +08:00
|
|
|
Total += OverflowConst->getValue().getBitWidth() < 32 ?
|
|
|
|
OverflowConst->getValue().abs().zext(32) : OverflowConst->getValue().abs();
|
|
|
|
|
|
|
|
APInt Max = APInt::getAllOnesValue(ARMCodeGenPrepare::TypeSize);
|
|
|
|
|
|
|
|
if (Total.getBitWidth() > Max.getBitWidth()) {
|
|
|
|
if (Total.ugt(Max.zext(Total.getBitWidth())))
|
|
|
|
return false;
|
|
|
|
} else if (Max.getBitWidth() > Total.getBitWidth()) {
|
|
|
|
if (Total.zext(Max.getBitWidth()).ugt(Max))
|
|
|
|
return false;
|
|
|
|
} else if (Total.ugt(Max))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM CGP: Allowing safe overflow for " << *I << "\n");
|
|
|
|
return true;
|
2018-07-23 20:27:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool shouldPromote(Value *V) {
|
2018-09-13 23:14:12 +08:00
|
|
|
if (!isa<IntegerType>(V->getType()) || isSink(V))
|
2018-07-23 20:27:47 +08:00
|
|
|
return false;
|
|
|
|
|
2018-08-10 21:57:13 +08:00
|
|
|
if (isSource(V))
|
|
|
|
return true;
|
2018-07-23 20:27:47 +08:00
|
|
|
|
2018-08-10 21:57:13 +08:00
|
|
|
auto *I = dyn_cast<Instruction>(V);
|
|
|
|
if (!I)
|
2018-07-23 20:27:47 +08:00
|
|
|
return false;
|
|
|
|
|
2018-08-10 21:57:13 +08:00
|
|
|
if (isa<ICmpInst>(I))
|
|
|
|
return false;
|
2018-07-23 20:27:47 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return whether we can safely mutate V's type to ExtTy without having to be
|
|
|
|
/// concerned with zero extending or truncation.
|
|
|
|
static bool isPromotedResultSafe(Value *V) {
|
|
|
|
if (!isa<Instruction>(V))
|
|
|
|
return true;
|
|
|
|
|
2018-08-17 15:34:01 +08:00
|
|
|
if (generateSignBits(V))
|
2018-07-23 20:27:47 +08:00
|
|
|
return false;
|
|
|
|
|
2018-11-05 18:58:37 +08:00
|
|
|
return !isa<OverflowingBinaryOperator>(V);
|
2018-07-23 20:27:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the intrinsic for the instruction that can perform the same
|
|
|
|
/// operation but on a narrow type. This is using the parallel dsp intrinsics
|
|
|
|
/// on scalar values.
|
2018-08-10 21:57:13 +08:00
|
|
|
static Intrinsic::ID getNarrowIntrinsic(Instruction *I) {
|
2018-07-23 20:27:47 +08:00
|
|
|
// Whether we use the signed or unsigned versions of these intrinsics
|
|
|
|
// doesn't matter because we're not using the GE bits that they set in
|
|
|
|
// the APSR.
|
|
|
|
switch(I->getOpcode()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case Instruction::Add:
|
2018-08-10 21:57:13 +08:00
|
|
|
return ARMCodeGenPrepare::TypeSize == 16 ? Intrinsic::arm_uadd16 :
|
2018-07-23 20:27:47 +08:00
|
|
|
Intrinsic::arm_uadd8;
|
|
|
|
case Instruction::Sub:
|
2018-08-10 21:57:13 +08:00
|
|
|
return ARMCodeGenPrepare::TypeSize == 16 ? Intrinsic::arm_usub16 :
|
2018-07-23 20:27:47 +08:00
|
|
|
Intrinsic::arm_usub8;
|
|
|
|
}
|
|
|
|
llvm_unreachable("unhandled opcode for narrow intrinsic");
|
|
|
|
}
|
|
|
|
|
2018-11-01 23:23:42 +08:00
|
|
|
static void ReplaceAllUsersOfWith(Value *From, Value *To) {
|
|
|
|
SmallVector<Instruction*, 4> Users;
|
|
|
|
Instruction *InstTo = dyn_cast<Instruction>(To);
|
|
|
|
for (Use &U : From->uses()) {
|
|
|
|
auto *User = cast<Instruction>(U.getUser());
|
|
|
|
if (InstTo && User->isIdenticalTo(InstTo))
|
|
|
|
continue;
|
|
|
|
Users.push_back(User);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto *U : Users)
|
|
|
|
U->replaceUsesOfWith(From, To);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IRPromoter::PrepareConstants(SmallPtrSetImpl<Value*> &Visited,
|
|
|
|
SmallPtrSetImpl<Instruction*> &SafeToPromote) {
|
2018-07-23 20:27:47 +08:00
|
|
|
IRBuilder<> Builder{Ctx};
|
2018-11-01 23:23:42 +08:00
|
|
|
// First step is to prepare the instructions for mutation. Most constants
|
|
|
|
// just need to be zero extended into their new type, but complications arise
|
|
|
|
// because:
|
|
|
|
// - For nuw binary operators, negative immediates would need sign extending;
|
|
|
|
// however, instead we'll change them to positive and zext them. We can do
|
|
|
|
// this because:
|
|
|
|
// > The operators that can wrap are: add, sub, mul and shl.
|
|
|
|
// > shl interprets its second operand as unsigned and if the first operand
|
|
|
|
// is an immediate, it will need zext to be nuw.
|
|
|
|
// > I'm assuming mul cannot be nuw while using a negative immediate...
|
|
|
|
// > Which leaves the nuw add and sub to be handled; as with shl, if an
|
|
|
|
// immediate is used as operand 0, it will need zext to be nuw.
|
|
|
|
// - We also allow add and sub to safely overflow in certain circumstances
|
|
|
|
// and only when the value (operand 0) is being decreased.
|
|
|
|
//
|
|
|
|
// For adds and subs, that are either nuw or safely wrap and use a negative
|
|
|
|
// immediate as operand 1, we create an equivalent instruction using a
|
|
|
|
// positive immediate. That positive immediate can then be zext along with
|
|
|
|
// all the other immediates later.
|
|
|
|
for (auto *V : Visited) {
|
|
|
|
if (!isa<Instruction>(V))
|
|
|
|
continue;
|
2018-07-23 20:27:47 +08:00
|
|
|
|
2018-11-01 23:23:42 +08:00
|
|
|
auto *I = cast<Instruction>(V);
|
|
|
|
if (SafeToPromote.count(I)) {
|
2018-08-16 18:05:39 +08:00
|
|
|
|
2018-11-01 23:23:42 +08:00
|
|
|
if (!isa<OverflowingBinaryOperator>(I))
|
2018-07-23 20:27:47 +08:00
|
|
|
continue;
|
|
|
|
|
2018-11-01 23:23:42 +08:00
|
|
|
if (auto *Const = dyn_cast<ConstantInt>(I->getOperand(1))) {
|
|
|
|
if (!Const->isNegative())
|
|
|
|
break;
|
|
|
|
|
|
|
|
unsigned Opc = I->getOpcode();
|
|
|
|
assert((Opc == Instruction::Add || Opc == Instruction::Sub) &&
|
|
|
|
"expected only an add or sub to use a negative imm");
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM CGP: Adjusting " << *I << "\n");
|
|
|
|
auto *NewConst = ConstantInt::get(Ctx, Const->getValue().abs());
|
|
|
|
Builder.SetInsertPoint(I);
|
|
|
|
Value *NewVal = Opc == Instruction::Sub ?
|
|
|
|
Builder.CreateAdd(I->getOperand(0), NewConst) :
|
|
|
|
Builder.CreateSub(I->getOperand(0), NewConst);
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM CGP: New equivalent: " << *NewVal << "\n");
|
|
|
|
|
|
|
|
if (auto *NewInst = dyn_cast<Instruction>(NewVal)) {
|
|
|
|
NewInst->copyIRFlags(I);
|
|
|
|
NewInsts.insert(NewInst);
|
|
|
|
}
|
|
|
|
InstsToRemove.push_back(I);
|
|
|
|
I->replaceAllUsesWith(NewVal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto *I : NewInsts)
|
|
|
|
Visited.insert(I);
|
|
|
|
}
|
2018-07-23 20:27:47 +08:00
|
|
|
|
2018-11-01 23:23:42 +08:00
|
|
|
void IRPromoter::ExtendSources(SmallPtrSetImpl<Value*> &Sources) {
|
|
|
|
IRBuilder<> Builder{Ctx};
|
2018-07-23 20:27:47 +08:00
|
|
|
|
|
|
|
auto InsertZExt = [&](Value *V, Instruction *InsertPt) {
|
2018-11-05 18:58:37 +08:00
|
|
|
assert(V->getType() != ExtTy && "zext already extends to i32");
|
2018-07-23 20:27:47 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "ARM CGP: Inserting ZExt for " << *V << "\n");
|
|
|
|
Builder.SetInsertPoint(InsertPt);
|
|
|
|
if (auto *I = dyn_cast<Instruction>(V))
|
|
|
|
Builder.SetCurrentDebugLocation(I->getDebugLoc());
|
2018-11-05 18:58:37 +08:00
|
|
|
|
|
|
|
Value *ZExt = Builder.CreateZExt(V, ExtTy);
|
|
|
|
if (auto *I = dyn_cast<Instruction>(ZExt)) {
|
|
|
|
if (isa<Argument>(V))
|
|
|
|
I->moveBefore(InsertPt);
|
|
|
|
else
|
|
|
|
I->moveAfter(InsertPt);
|
|
|
|
NewInsts.insert(I);
|
|
|
|
}
|
2018-07-23 20:27:47 +08:00
|
|
|
ReplaceAllUsersOfWith(V, ZExt);
|
2018-08-16 18:05:39 +08:00
|
|
|
TruncTysMap[ZExt] = TruncTysMap[V];
|
2018-07-23 20:27:47 +08:00
|
|
|
};
|
|
|
|
|
2018-11-01 23:23:42 +08:00
|
|
|
// Now, insert extending instructions between the sources and their users.
|
2018-08-17 15:34:01 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "ARM CGP: Promoting sources:\n");
|
|
|
|
for (auto V : Sources) {
|
2018-07-23 20:27:47 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " - " << *V << "\n");
|
2018-08-10 21:57:13 +08:00
|
|
|
if (auto *I = dyn_cast<Instruction>(V))
|
2018-07-23 20:27:47 +08:00
|
|
|
InsertZExt(I, I);
|
|
|
|
else if (auto *Arg = dyn_cast<Argument>(V)) {
|
|
|
|
BasicBlock &BB = Arg->getParent()->front();
|
|
|
|
InsertZExt(Arg, &*BB.getFirstInsertionPt());
|
|
|
|
} else {
|
2018-08-17 15:34:01 +08:00
|
|
|
llvm_unreachable("unhandled source that needs extending");
|
2018-07-23 20:27:47 +08:00
|
|
|
}
|
|
|
|
Promoted.insert(V);
|
|
|
|
}
|
2018-11-01 23:23:42 +08:00
|
|
|
}
|
2018-07-23 20:27:47 +08:00
|
|
|
|
2018-11-01 23:23:42 +08:00
|
|
|
void IRPromoter::PromoteTree(SmallPtrSetImpl<Value*> &Visited,
|
|
|
|
SmallPtrSetImpl<Value*> &Sources,
|
|
|
|
SmallPtrSetImpl<Instruction*> &Sinks,
|
|
|
|
SmallPtrSetImpl<Instruction*> &SafeToPromote) {
|
2018-07-23 20:27:47 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "ARM CGP: Mutating the tree..\n");
|
2018-11-01 23:23:42 +08:00
|
|
|
|
|
|
|
IRBuilder<> Builder{Ctx};
|
|
|
|
|
|
|
|
// Mutate the types of the instructions within the tree. Here we handle
|
2018-07-23 20:27:47 +08:00
|
|
|
// constant operands.
|
|
|
|
for (auto *V : Visited) {
|
2018-08-17 15:34:01 +08:00
|
|
|
if (Sources.count(V))
|
2018-07-23 20:27:47 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
auto *I = cast<Instruction>(V);
|
2018-08-17 15:34:01 +08:00
|
|
|
if (Sinks.count(I))
|
2018-07-23 20:27:47 +08:00
|
|
|
continue;
|
|
|
|
|
2018-08-15 15:52:35 +08:00
|
|
|
for (unsigned i = 0, e = I->getNumOperands(); i < e; ++i) {
|
|
|
|
Value *Op = I->getOperand(i);
|
|
|
|
if ((Op->getType() == ExtTy) || !isa<IntegerType>(Op->getType()))
|
2018-07-23 20:27:47 +08:00
|
|
|
continue;
|
|
|
|
|
2018-11-01 23:23:42 +08:00
|
|
|
if (auto *Const = dyn_cast<ConstantInt>(Op)) {
|
|
|
|
Constant *NewConst = ConstantExpr::getZExt(Const, ExtTy);
|
|
|
|
I->setOperand(i, NewConst);
|
|
|
|
} else if (isa<UndefValue>(Op))
|
2018-08-15 15:52:35 +08:00
|
|
|
I->setOperand(i, UndefValue::get(ExtTy));
|
2018-07-23 20:27:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldPromote(I)) {
|
|
|
|
I->mutateType(ExtTy);
|
|
|
|
Promoted.insert(I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 23:23:42 +08:00
|
|
|
// Finally, any instructions that should be promoted but haven't yet been,
|
|
|
|
// need to be handled using intrinsics.
|
2018-07-23 20:27:47 +08:00
|
|
|
for (auto *V : Visited) {
|
2018-11-01 23:23:42 +08:00
|
|
|
auto *I = dyn_cast<Instruction>(V);
|
|
|
|
if (!I)
|
2018-07-23 20:27:47 +08:00
|
|
|
continue;
|
2018-08-10 21:57:13 +08:00
|
|
|
|
2018-11-01 23:23:42 +08:00
|
|
|
if (Sources.count(I) || Sinks.count(I))
|
2018-07-23 20:27:47 +08:00
|
|
|
continue;
|
|
|
|
|
2018-11-01 23:23:42 +08:00
|
|
|
if (!shouldPromote(I) || SafeToPromote.count(I) || NewInsts.count(I))
|
|
|
|
continue;
|
|
|
|
|
2018-09-26 18:56:00 +08:00
|
|
|
assert(EnableDSP && "DSP intrinisc insertion not enabled!");
|
|
|
|
|
2018-07-23 20:27:47 +08:00
|
|
|
// Replace unsafe instructions with appropriate intrinsic calls.
|
2018-11-01 23:23:42 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "ARM CGP: Inserting DSP intrinsic for "
|
|
|
|
<< *I << "\n");
|
|
|
|
Function *DSPInst =
|
|
|
|
Intrinsic::getDeclaration(M, getNarrowIntrinsic(I));
|
|
|
|
Builder.SetInsertPoint(I);
|
|
|
|
Builder.SetCurrentDebugLocation(I->getDebugLoc());
|
|
|
|
Value *Args[] = { I->getOperand(0), I->getOperand(1) };
|
|
|
|
CallInst *Call = Builder.CreateCall(DSPInst, Args);
|
|
|
|
ReplaceAllUsersOfWith(I, Call);
|
|
|
|
InstsToRemove.push_back(I);
|
|
|
|
NewInsts.insert(Call);
|
|
|
|
TruncTysMap[Call] = OrigTy;
|
2018-07-23 20:27:47 +08:00
|
|
|
}
|
2018-11-01 23:23:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void IRPromoter::TruncateSinks(SmallPtrSetImpl<Value*> &Sources,
|
|
|
|
SmallPtrSetImpl<Instruction*> &Sinks) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM CGP: Fixing up the sinks:\n");
|
|
|
|
|
|
|
|
IRBuilder<> Builder{Ctx};
|
2018-07-23 20:27:47 +08:00
|
|
|
|
2018-08-16 18:05:39 +08:00
|
|
|
auto InsertTrunc = [&](Value *V) -> Instruction* {
|
|
|
|
if (!isa<Instruction>(V) || !isa<IntegerType>(V->getType()))
|
|
|
|
return nullptr;
|
|
|
|
|
2018-08-16 19:54:09 +08:00
|
|
|
if ((!Promoted.count(V) && !NewInsts.count(V)) || !TruncTysMap.count(V) ||
|
2018-08-17 15:34:01 +08:00
|
|
|
Sources.count(V))
|
2018-08-16 18:05:39 +08:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
Type *TruncTy = TruncTysMap[V];
|
|
|
|
if (TruncTy == ExtTy)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM CGP: Creating " << *TruncTy << " Trunc for "
|
|
|
|
<< *V << "\n");
|
|
|
|
Builder.SetInsertPoint(cast<Instruction>(V));
|
2018-11-02 00:44:45 +08:00
|
|
|
auto *Trunc = dyn_cast<Instruction>(Builder.CreateTrunc(V, TruncTy));
|
|
|
|
if (Trunc)
|
|
|
|
NewInsts.insert(Trunc);
|
2018-08-16 18:05:39 +08:00
|
|
|
return Trunc;
|
|
|
|
};
|
|
|
|
|
2018-07-23 20:27:47 +08:00
|
|
|
// Fix up any stores or returns that use the results of the promoted
|
|
|
|
// chain.
|
2018-08-17 15:34:01 +08:00
|
|
|
for (auto I : Sinks) {
|
2018-07-23 20:27:47 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " - " << *I << "\n");
|
2018-08-16 18:05:39 +08:00
|
|
|
|
|
|
|
// Handle calls separately as we need to iterate over arg operands.
|
|
|
|
if (auto *Call = dyn_cast<CallInst>(I)) {
|
|
|
|
for (unsigned i = 0; i < Call->getNumArgOperands(); ++i) {
|
|
|
|
Value *Arg = Call->getArgOperand(i);
|
|
|
|
if (Instruction *Trunc = InsertTrunc(Arg)) {
|
|
|
|
Trunc->moveBefore(Call);
|
|
|
|
Call->setArgOperand(i, Trunc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
2018-07-23 20:27:47 +08:00
|
|
|
}
|
|
|
|
|
2018-08-16 18:05:39 +08:00
|
|
|
// Now handle the others.
|
2018-08-15 16:23:03 +08:00
|
|
|
for (unsigned i = 0; i < I->getNumOperands(); ++i) {
|
2018-08-16 18:05:39 +08:00
|
|
|
if (Instruction *Trunc = InsertTrunc(I->getOperand(i))) {
|
|
|
|
Trunc->moveBefore(I);
|
|
|
|
I->setOperand(i, Trunc);
|
2018-07-23 20:27:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-05 18:58:37 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void IRPromoter::Cleanup(SmallPtrSetImpl<Instruction*> &Sinks) {
|
|
|
|
// Some zext sinks will now have become redundant, along with their trunc
|
|
|
|
// operands, so remove them.
|
|
|
|
for (auto I : Sinks) {
|
|
|
|
if (auto *ZExt = dyn_cast<ZExtInst>(I)) {
|
|
|
|
if (ZExt->getDestTy() != ExtTy)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Value *Src = ZExt->getOperand(0);
|
|
|
|
if (ZExt->getSrcTy() == ZExt->getDestTy()) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM CGP: Removing unnecessary zext\n");
|
|
|
|
ReplaceAllUsersOfWith(ZExt, Src);
|
|
|
|
InstsToRemove.push_back(ZExt);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For any truncs that we insert to handle zexts, we can replace the
|
|
|
|
// result of the zext with the input to the trunc.
|
|
|
|
if (NewInsts.count(Src) && isa<TruncInst>(Src)) {
|
|
|
|
auto *Trunc = cast<TruncInst>(Src);
|
|
|
|
assert(Trunc->getOperand(0)->getType() == ExtTy &&
|
|
|
|
"expected inserted trunc to be operating on i32");
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM CGP: Replacing zext with trunc operand: "
|
|
|
|
<< *Trunc->getOperand(0));
|
|
|
|
ReplaceAllUsersOfWith(ZExt, Trunc->getOperand(0));
|
|
|
|
InstsToRemove.push_back(ZExt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto *I : InstsToRemove) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM CGP: Removing " << *I << "\n");
|
|
|
|
I->dropAllReferences();
|
|
|
|
I->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
InstsToRemove.clear();
|
|
|
|
NewInsts.clear();
|
|
|
|
TruncTysMap.clear();
|
|
|
|
Promoted.clear();
|
2018-11-01 23:23:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void IRPromoter::Mutate(Type *OrigTy,
|
|
|
|
SmallPtrSetImpl<Value*> &Visited,
|
|
|
|
SmallPtrSetImpl<Value*> &Sources,
|
|
|
|
SmallPtrSetImpl<Instruction*> &Sinks,
|
|
|
|
SmallPtrSetImpl<Instruction*> &SafeToPromote) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM CGP: Promoting use-def chains to from "
|
|
|
|
<< ARMCodeGenPrepare::TypeSize << " to 32-bits\n");
|
2018-11-05 18:58:37 +08:00
|
|
|
|
|
|
|
assert(isa<IntegerType>(OrigTy) && "expected integer type");
|
|
|
|
this->OrigTy = cast<IntegerType>(OrigTy);
|
|
|
|
assert(OrigTy->getPrimitiveSizeInBits() < ExtTy->getPrimitiveSizeInBits() &&
|
|
|
|
"original type not smaller than extended type");
|
2018-11-01 23:23:42 +08:00
|
|
|
|
|
|
|
// Cache original types.
|
|
|
|
for (auto *V : Visited)
|
|
|
|
TruncTysMap[V] = V->getType();
|
|
|
|
|
|
|
|
// Convert adds and subs using negative immediates to equivalent instructions
|
|
|
|
// that use positive constants.
|
|
|
|
PrepareConstants(Visited, SafeToPromote);
|
|
|
|
|
|
|
|
// Insert zext instructions between sources and their users.
|
|
|
|
ExtendSources(Sources);
|
|
|
|
|
|
|
|
// Promote visited instructions, mutating their types in place. Also insert
|
|
|
|
// DSP intrinsics, if enabled, for adds and subs which would be unsafe to
|
|
|
|
// promote.
|
|
|
|
PromoteTree(Visited, Sources, Sinks, SafeToPromote);
|
|
|
|
|
2018-11-05 18:58:37 +08:00
|
|
|
// Insert trunc instructions for use by calls, stores etc...
|
2018-11-01 23:23:42 +08:00
|
|
|
TruncateSinks(Sources, Sinks);
|
|
|
|
|
2018-11-05 18:58:37 +08:00
|
|
|
// Finally, remove unecessary zexts and truncs, delete old instructions and
|
|
|
|
// clear the data structures.
|
|
|
|
Cleanup(Sinks);
|
|
|
|
|
2018-08-16 19:54:09 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "ARM CGP: Mutation complete:\n");
|
2018-09-13 23:14:12 +08:00
|
|
|
LLVM_DEBUG(dbgs();
|
|
|
|
for (auto *V : Sources)
|
|
|
|
V->dump();
|
|
|
|
for (auto *I : NewInsts)
|
|
|
|
I->dump();
|
|
|
|
for (auto *V : Visited) {
|
|
|
|
if (!Sources.count(V))
|
|
|
|
V->dump();
|
|
|
|
});
|
2018-07-23 20:27:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// We accept most instructions, as well as Arguments and ConstantInsts. We
|
|
|
|
/// Disallow casts other than zext and truncs and only allow calls if their
|
|
|
|
/// return value is zeroext. We don't allow opcodes that can introduce sign
|
|
|
|
/// bits.
|
|
|
|
bool ARMCodeGenPrepare::isSupportedValue(Value *V) {
|
2018-09-18 08:11:55 +08:00
|
|
|
if (isa<ICmpInst>(V))
|
|
|
|
return true;
|
2018-07-23 20:27:47 +08:00
|
|
|
|
2018-09-18 08:11:55 +08:00
|
|
|
// Memory instructions
|
|
|
|
if (isa<StoreInst>(V) || isa<GetElementPtrInst>(V))
|
|
|
|
return true;
|
2018-07-23 20:27:47 +08:00
|
|
|
|
2018-09-18 08:11:55 +08:00
|
|
|
// Branches and targets.
|
|
|
|
if( isa<BranchInst>(V) || isa<SwitchInst>(V) || isa<BasicBlock>(V))
|
2018-07-23 20:27:47 +08:00
|
|
|
return true;
|
|
|
|
|
2018-09-18 08:11:55 +08:00
|
|
|
// Non-instruction values that we can handle.
|
|
|
|
if ((isa<Constant>(V) && !isa<ConstantExpr>(V)) || isa<Argument>(V))
|
|
|
|
return isSupportedType(V);
|
|
|
|
|
|
|
|
if (isa<PHINode>(V) || isa<SelectInst>(V) || isa<ReturnInst>(V) ||
|
|
|
|
isa<LoadInst>(V))
|
|
|
|
return isSupportedType(V);
|
|
|
|
|
|
|
|
// Truncs can be either sources or sinks.
|
|
|
|
if (auto *Trunc = dyn_cast<TruncInst>(V))
|
|
|
|
return isSupportedType(Trunc) || isSupportedType(Trunc->getOperand(0));
|
|
|
|
|
|
|
|
if (isa<CastInst>(V) && !isa<SExtInst>(V))
|
|
|
|
return isSupportedType(cast<CastInst>(V)->getOperand(0));
|
2018-08-16 19:54:09 +08:00
|
|
|
|
2018-07-23 20:27:47 +08:00
|
|
|
// Special cases for calls as we need to check for zeroext
|
|
|
|
// TODO We should accept calls even if they don't have zeroext, as they can
|
2018-08-17 15:34:01 +08:00
|
|
|
// still be sinks.
|
2018-07-23 20:27:47 +08:00
|
|
|
if (auto *Call = dyn_cast<CallInst>(V))
|
2018-08-10 21:57:13 +08:00
|
|
|
return isSupportedType(Call) &&
|
|
|
|
Call->hasRetAttr(Attribute::AttrKind::ZExt);
|
|
|
|
|
2018-09-18 08:11:55 +08:00
|
|
|
if (!isa<BinaryOperator>(V))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!isSupportedType(V))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (generateSignBits(V)) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM CGP: No, instruction can generate sign bits.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2018-07-23 20:27:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Check that the type of V would be promoted and that the original type is
|
|
|
|
/// smaller than the targeted promoted type. Check that we're not trying to
|
|
|
|
/// promote something larger than our base 'TypeSize' type.
|
|
|
|
bool ARMCodeGenPrepare::isLegalToPromote(Value *V) {
|
|
|
|
|
2018-08-10 21:57:13 +08:00
|
|
|
auto *I = dyn_cast<Instruction>(V);
|
|
|
|
if (!I)
|
2018-11-01 23:23:42 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if (SafeToPromote.count(I))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (isPromotedResultSafe(V) || isSafeOverflow(I)) {
|
|
|
|
SafeToPromote.insert(I);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (I->getOpcode() != Instruction::Add && I->getOpcode() != Instruction::Sub)
|
2018-08-10 21:57:13 +08:00
|
|
|
return false;
|
2018-07-23 20:27:47 +08:00
|
|
|
|
2018-08-10 21:57:13 +08:00
|
|
|
// If promotion is not safe, can we use a DSP instruction to natively
|
|
|
|
// handle the narrow type?
|
|
|
|
if (!ST->hasDSP() || !EnableDSP || !isSupportedType(I))
|
2018-07-23 20:27:47 +08:00
|
|
|
return false;
|
|
|
|
|
2018-08-10 21:57:13 +08:00
|
|
|
if (ST->isThumb() && !ST->hasThumb2())
|
|
|
|
return false;
|
2018-07-23 20:27:47 +08:00
|
|
|
|
2018-08-10 21:57:13 +08:00
|
|
|
// TODO
|
|
|
|
// Would it be profitable? For Thumb code, these parallel DSP instructions
|
|
|
|
// are only Thumb-2, so we wouldn't be able to dual issue on Cortex-M33. For
|
|
|
|
// Cortex-A, specifically Cortex-A72, the latency is double and throughput is
|
|
|
|
// halved. They also do not take immediates as operands.
|
|
|
|
for (auto &Op : I->operands()) {
|
|
|
|
if (isa<Constant>(Op)) {
|
|
|
|
if (!EnableDSPWithImms)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-11-01 23:23:42 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "ARM CGP: Will use an intrinsic for: " << *I << "\n");
|
2018-08-10 21:57:13 +08:00
|
|
|
return true;
|
2018-07-23 20:27:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ARMCodeGenPrepare::TryToPromote(Value *V) {
|
|
|
|
OrigTy = V->getType();
|
|
|
|
TypeSize = OrigTy->getPrimitiveSizeInBits();
|
2018-08-15 21:29:50 +08:00
|
|
|
if (TypeSize > 16 || TypeSize < 8)
|
2018-08-10 21:57:13 +08:00
|
|
|
return false;
|
2018-07-23 20:27:47 +08:00
|
|
|
|
2018-11-01 23:23:42 +08:00
|
|
|
SafeToPromote.clear();
|
|
|
|
|
2018-07-23 20:27:47 +08:00
|
|
|
if (!isSupportedValue(V) || !shouldPromote(V) || !isLegalToPromote(V))
|
|
|
|
return false;
|
|
|
|
|
2018-08-10 21:57:13 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "ARM CGP: TryToPromote: " << *V << ", TypeSize = "
|
|
|
|
<< TypeSize << "\n");
|
2018-07-23 20:27:47 +08:00
|
|
|
|
|
|
|
SetVector<Value*> WorkList;
|
2018-08-17 15:34:01 +08:00
|
|
|
SmallPtrSet<Value*, 8> Sources;
|
|
|
|
SmallPtrSet<Instruction*, 4> Sinks;
|
2018-07-23 20:27:47 +08:00
|
|
|
SmallPtrSet<Value*, 16> CurrentVisited;
|
2018-11-01 23:23:42 +08:00
|
|
|
WorkList.insert(V);
|
2018-07-23 20:27:47 +08:00
|
|
|
|
2018-08-17 15:34:01 +08:00
|
|
|
// Return true if V was added to the worklist as a supported instruction,
|
|
|
|
// if it was already visited, or if we don't need to explore it (e.g.
|
|
|
|
// pointer values and GEPs), and false otherwise.
|
2018-07-23 20:27:47 +08:00
|
|
|
auto AddLegalInst = [&](Value *V) {
|
|
|
|
if (CurrentVisited.count(V))
|
|
|
|
return true;
|
|
|
|
|
2018-08-16 20:24:40 +08:00
|
|
|
// Ignore GEPs because they don't need promoting and the constant indices
|
|
|
|
// will prevent the transformation.
|
|
|
|
if (isa<GetElementPtrInst>(V))
|
|
|
|
return true;
|
|
|
|
|
2018-07-23 20:27:47 +08:00
|
|
|
if (!isSupportedValue(V) || (shouldPromote(V) && !isLegalToPromote(V))) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM CGP: Can't handle: " << *V << "\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkList.insert(V);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Iterate through, and add to, a tree of operands and users in the use-def.
|
|
|
|
while (!WorkList.empty()) {
|
|
|
|
Value *V = WorkList.back();
|
|
|
|
WorkList.pop_back();
|
|
|
|
if (CurrentVisited.count(V))
|
|
|
|
continue;
|
|
|
|
|
2018-08-15 15:52:35 +08:00
|
|
|
// Ignore non-instructions, other than arguments.
|
2018-07-23 20:27:47 +08:00
|
|
|
if (!isa<Instruction>(V) && !isSource(V))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If we've already visited this value from somewhere, bail now because
|
|
|
|
// the tree has already been explored.
|
|
|
|
// TODO: This could limit the transform, ie if we try to promote something
|
|
|
|
// from an i8 and fail first, before trying an i16.
|
2018-09-13 23:14:12 +08:00
|
|
|
if (AllVisited.count(V))
|
2018-07-23 20:27:47 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
CurrentVisited.insert(V);
|
|
|
|
AllVisited.insert(V);
|
|
|
|
|
|
|
|
// Calls can be both sources and sinks.
|
|
|
|
if (isSink(V))
|
2018-08-17 15:34:01 +08:00
|
|
|
Sinks.insert(cast<Instruction>(V));
|
2018-07-23 20:27:47 +08:00
|
|
|
if (isSource(V))
|
2018-08-17 15:34:01 +08:00
|
|
|
Sources.insert(V);
|
2018-07-23 20:27:47 +08:00
|
|
|
else if (auto *I = dyn_cast<Instruction>(V)) {
|
|
|
|
// Visit operands of any instruction visited.
|
|
|
|
for (auto &U : I->operands()) {
|
|
|
|
if (!AddLegalInst(U))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't visit users of a node which isn't going to be mutated unless its a
|
|
|
|
// source.
|
|
|
|
if (isSource(V) || shouldPromote(V)) {
|
|
|
|
for (Use &U : V->uses()) {
|
|
|
|
if (!AddLegalInst(U.getUser()))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM CGP: Visited nodes:\n";
|
|
|
|
for (auto *I : CurrentVisited)
|
|
|
|
I->dump();
|
|
|
|
);
|
2018-08-15 15:52:35 +08:00
|
|
|
unsigned ToPromote = 0;
|
|
|
|
for (auto *V : CurrentVisited) {
|
2018-08-17 15:34:01 +08:00
|
|
|
if (Sources.count(V))
|
2018-08-15 15:52:35 +08:00
|
|
|
continue;
|
2018-08-17 15:34:01 +08:00
|
|
|
if (Sinks.count(cast<Instruction>(V)))
|
2018-08-15 15:52:35 +08:00
|
|
|
continue;
|
|
|
|
++ToPromote;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ToPromote < 2)
|
|
|
|
return false;
|
2018-07-23 20:27:47 +08:00
|
|
|
|
2018-11-01 23:23:42 +08:00
|
|
|
Promoter->Mutate(OrigTy, CurrentVisited, Sources, Sinks, SafeToPromote);
|
2018-07-23 20:27:47 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ARMCodeGenPrepare::doInitialization(Module &M) {
|
|
|
|
Promoter = new IRPromoter(&M);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ARMCodeGenPrepare::runOnFunction(Function &F) {
|
|
|
|
if (skipFunction(F) || DisableCGP)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto *TPC = &getAnalysis<TargetPassConfig>();
|
|
|
|
if (!TPC)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const TargetMachine &TM = TPC->getTM<TargetMachine>();
|
|
|
|
ST = &TM.getSubtarget<ARMSubtarget>(F);
|
|
|
|
bool MadeChange = false;
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM CGP: Running on " << F.getName() << "\n");
|
|
|
|
|
|
|
|
// Search up from icmps to try to promote their operands.
|
|
|
|
for (BasicBlock &BB : F) {
|
|
|
|
auto &Insts = BB.getInstList();
|
|
|
|
for (auto &I : Insts) {
|
|
|
|
if (AllVisited.count(&I))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (isa<ICmpInst>(I)) {
|
|
|
|
auto &CI = cast<ICmpInst>(I);
|
|
|
|
|
|
|
|
// Skip signed or pointer compares
|
|
|
|
if (CI.isSigned() || !isa<IntegerType>(CI.getOperand(0)->getType()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (auto &Op : CI.operands()) {
|
2018-08-10 21:57:13 +08:00
|
|
|
if (auto *I = dyn_cast<Instruction>(Op))
|
|
|
|
MadeChange |= TryToPromote(I);
|
2018-07-23 20:27:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LLVM_DEBUG(if (verifyFunction(F, &dbgs())) {
|
2018-11-05 18:58:37 +08:00
|
|
|
dbgs() << F;
|
2018-07-23 20:27:47 +08:00
|
|
|
report_fatal_error("Broken function after type promotion");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (MadeChange)
|
|
|
|
LLVM_DEBUG(dbgs() << "After ARMCodeGenPrepare: " << F << "\n");
|
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
2018-07-24 01:00:45 +08:00
|
|
|
bool ARMCodeGenPrepare::doFinalization(Module &M) {
|
|
|
|
delete Promoter;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-23 20:27:47 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(ARMCodeGenPrepare, DEBUG_TYPE,
|
|
|
|
"ARM IR optimizations", false, false)
|
|
|
|
INITIALIZE_PASS_END(ARMCodeGenPrepare, DEBUG_TYPE, "ARM IR optimizations",
|
|
|
|
false, false)
|
|
|
|
|
|
|
|
char ARMCodeGenPrepare::ID = 0;
|
2018-08-10 21:57:13 +08:00
|
|
|
unsigned ARMCodeGenPrepare::TypeSize = 0;
|
2018-07-23 20:27:47 +08:00
|
|
|
|
|
|
|
FunctionPass *llvm::createARMCodeGenPreparePass() {
|
|
|
|
return new ARMCodeGenPrepare();
|
|
|
|
}
|