2010-01-05 07:18:46 +00:00
|
|
|
//===- InstCombineAddSub.cpp ----------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the visit functions for add, fadd, sub, and fsub.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "InstCombine.h"
|
2013-07-15 04:27:47 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2010-01-05 07:18:46 +00:00
|
|
|
#include "llvm/Analysis/InstructionSimplify.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2014-03-04 10:40:04 +00:00
|
|
|
#include "llvm/IR/GetElementPtrTypeIterator.h"
|
2014-03-04 11:08:18 +00:00
|
|
|
#include "llvm/IR/PatternMatch.h"
|
2010-01-05 07:18:46 +00:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace PatternMatch;
|
|
|
|
|
2014-04-22 02:55:47 +00:00
|
|
|
#define DEBUG_TYPE "instcombine"
|
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// Class representing coefficient of floating-point addend.
|
|
|
|
/// This class needs to be highly efficient, which is especially true for
|
|
|
|
/// the constructor. As of I write this comment, the cost of the default
|
2013-04-05 21:20:12 +00:00
|
|
|
/// constructor is merely 4-byte-store-zero (Assuming compiler is able to
|
2012-12-18 23:10:12 +00:00
|
|
|
/// perform write-merging).
|
2013-04-05 21:20:12 +00:00
|
|
|
///
|
2012-12-18 23:10:12 +00:00
|
|
|
class FAddendCoef {
|
|
|
|
public:
|
2014-07-17 06:09:34 +00:00
|
|
|
// The constructor has to initialize a APFloat, which is unnecessary for
|
2012-12-18 23:10:12 +00:00
|
|
|
// most addends which have coefficient either 1 or -1. So, the constructor
|
|
|
|
// is expensive. In order to avoid the cost of the constructor, we should
|
|
|
|
// reuse some instances whenever possible. The pre-created instances
|
|
|
|
// FAddCombine::Add[0-5] embodies this idea.
|
|
|
|
//
|
|
|
|
FAddendCoef() : IsFp(false), BufHasFpVal(false), IntVal(0) {}
|
|
|
|
~FAddendCoef();
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
void set(short C) {
|
|
|
|
assert(!insaneIntVal(C) && "Insane coefficient");
|
|
|
|
IsFp = false; IntVal = C;
|
|
|
|
}
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
void set(const APFloat& C);
|
2013-03-25 20:43:41 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
void negate();
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
bool isZero() const { return isInt() ? !IntVal : getFpVal().isZero(); }
|
|
|
|
Value *getValue(Type *) const;
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
// If possible, don't define operator+/operator- etc because these
|
|
|
|
// operators inevitably call FAddendCoef's constructor which is not cheap.
|
|
|
|
void operator=(const FAddendCoef &A);
|
|
|
|
void operator+=(const FAddendCoef &A);
|
|
|
|
void operator-=(const FAddendCoef &A);
|
|
|
|
void operator*=(const FAddendCoef &S);
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
bool isOne() const { return isInt() && IntVal == 1; }
|
|
|
|
bool isTwo() const { return isInt() && IntVal == 2; }
|
|
|
|
bool isMinusOne() const { return isInt() && IntVal == -1; }
|
|
|
|
bool isMinusTwo() const { return isInt() && IntVal == -2; }
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
private:
|
|
|
|
bool insaneIntVal(int V) { return V > 4 || V < -4; }
|
|
|
|
APFloat *getFpValPtr(void)
|
2012-12-19 01:10:17 +00:00
|
|
|
{ return reinterpret_cast<APFloat*>(&FpValBuf.buffer[0]); }
|
2013-01-14 21:04:40 +00:00
|
|
|
const APFloat *getFpValPtr(void) const
|
|
|
|
{ return reinterpret_cast<const APFloat*>(&FpValBuf.buffer[0]); }
|
2012-12-18 23:10:12 +00:00
|
|
|
|
|
|
|
const APFloat &getFpVal(void) const {
|
|
|
|
assert(IsFp && BufHasFpVal && "Incorret state");
|
2013-01-14 21:04:40 +00:00
|
|
|
return *getFpValPtr();
|
2012-12-18 23:10:12 +00:00
|
|
|
}
|
|
|
|
|
2013-04-05 21:20:12 +00:00
|
|
|
APFloat &getFpVal(void) {
|
|
|
|
assert(IsFp && BufHasFpVal && "Incorret state");
|
|
|
|
return *getFpValPtr();
|
|
|
|
}
|
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
bool isInt() const { return !IsFp; }
|
|
|
|
|
2013-03-25 20:43:41 +00:00
|
|
|
// If the coefficient is represented by an integer, promote it to a
|
2013-04-05 21:20:12 +00:00
|
|
|
// floating point.
|
2013-03-25 20:43:41 +00:00
|
|
|
void convertToFpType(const fltSemantics &Sem);
|
|
|
|
|
|
|
|
// Construct an APFloat from a signed integer.
|
|
|
|
// TODO: We should get rid of this function when APFloat can be constructed
|
2013-04-05 21:20:12 +00:00
|
|
|
// from an *SIGNED* integer.
|
2013-03-25 20:43:41 +00:00
|
|
|
APFloat createAPFloatFromInt(const fltSemantics &Sem, int Val);
|
2012-12-18 23:10:12 +00:00
|
|
|
private:
|
2012-12-19 01:10:17 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
bool IsFp;
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
// True iff FpValBuf contains an instance of APFloat.
|
|
|
|
bool BufHasFpVal;
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
// The integer coefficient of an individual addend is either 1 or -1,
|
|
|
|
// and we try to simplify at most 4 addends from neighboring at most
|
|
|
|
// two instructions. So the range of <IntVal> falls in [-4, 4]. APInt
|
|
|
|
// is overkill of this end.
|
|
|
|
short IntVal;
|
2012-12-19 01:10:17 +00:00
|
|
|
|
|
|
|
AlignedCharArrayUnion<APFloat> FpValBuf;
|
2012-12-18 23:10:12 +00:00
|
|
|
};
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
/// FAddend is used to represent floating-point addend. An addend is
|
|
|
|
/// represented as <C, V>, where the V is a symbolic value, and C is a
|
|
|
|
/// constant coefficient. A constant addend is represented as <C, 0>.
|
|
|
|
///
|
|
|
|
class FAddend {
|
|
|
|
public:
|
2014-04-25 05:29:35 +00:00
|
|
|
FAddend() { Val = nullptr; }
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
Value *getSymVal (void) const { return Val; }
|
|
|
|
const FAddendCoef &getCoef(void) const { return Coeff; }
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2014-04-25 05:29:35 +00:00
|
|
|
bool isConstant() const { return Val == nullptr; }
|
2012-12-18 23:10:12 +00:00
|
|
|
bool isZero() const { return Coeff.isZero(); }
|
|
|
|
|
|
|
|
void set(short Coefficient, Value *V) { Coeff.set(Coefficient), Val = V; }
|
|
|
|
void set(const APFloat& Coefficient, Value *V)
|
|
|
|
{ Coeff.set(Coefficient); Val = V; }
|
|
|
|
void set(const ConstantFP* Coefficient, Value *V)
|
|
|
|
{ Coeff.set(Coefficient->getValueAPF()); Val = V; }
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
void negate() { Coeff.negate(); }
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
/// Drill down the U-D chain one step to find the definition of V, and
|
|
|
|
/// try to break the definition into one or two addends.
|
|
|
|
static unsigned drillValueDownOneStep(Value* V, FAddend &A0, FAddend &A1);
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
/// Similar to FAddend::drillDownOneStep() except that the value being
|
|
|
|
/// splitted is the addend itself.
|
|
|
|
unsigned drillAddendDownOneStep(FAddend &Addend0, FAddend &Addend1) const;
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
void operator+=(const FAddend &T) {
|
|
|
|
assert((Val == T.Val) && "Symbolic-values disagree");
|
|
|
|
Coeff += T.Coeff;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void Scale(const FAddendCoef& ScaleAmt) { Coeff *= ScaleAmt; }
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
// This addend has the value of "Coeff * Val".
|
|
|
|
Value *Val;
|
|
|
|
FAddendCoef Coeff;
|
|
|
|
};
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
/// FAddCombine is the class for optimizing an unsafe fadd/fsub along
|
|
|
|
/// with its neighboring at most two instructions.
|
|
|
|
///
|
|
|
|
class FAddCombine {
|
|
|
|
public:
|
2014-04-25 05:29:35 +00:00
|
|
|
FAddCombine(InstCombiner::BuilderTy *B) : Builder(B), Instr(nullptr) {}
|
2012-12-18 23:10:12 +00:00
|
|
|
Value *simplify(Instruction *FAdd);
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
private:
|
|
|
|
typedef SmallVector<const FAddend*, 4> AddendVect;
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
Value *simplifyFAdd(AddendVect& V, unsigned InstrQuota);
|
2013-03-14 18:08:26 +00:00
|
|
|
|
|
|
|
Value *performFactorization(Instruction *I);
|
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
/// Convert given addend to a Value
|
|
|
|
Value *createAddendVal(const FAddend &A, bool& NeedNeg);
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
/// Return the number of instructions needed to emit the N-ary addition.
|
|
|
|
unsigned calcInstrNumber(const AddendVect& Vect);
|
|
|
|
Value *createFSub(Value *Opnd0, Value *Opnd1);
|
|
|
|
Value *createFAdd(Value *Opnd0, Value *Opnd1);
|
|
|
|
Value *createFMul(Value *Opnd0, Value *Opnd1);
|
2013-03-14 18:08:26 +00:00
|
|
|
Value *createFDiv(Value *Opnd0, Value *Opnd1);
|
2012-12-18 23:10:12 +00:00
|
|
|
Value *createFNeg(Value *V);
|
|
|
|
Value *createNaryFAdd(const AddendVect& Opnds, unsigned InstrQuota);
|
2014-01-20 07:44:53 +00:00
|
|
|
void createInstPostProc(Instruction *NewInst, bool NoNumber = false);
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
InstCombiner::BuilderTy *Builder;
|
|
|
|
Instruction *Instr;
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
private:
|
|
|
|
// Debugging stuff are clustered here.
|
|
|
|
#ifndef NDEBUG
|
|
|
|
unsigned CreateInstrNum;
|
|
|
|
void initCreateInstNum() { CreateInstrNum = 0; }
|
|
|
|
void incCreateInstNum() { CreateInstrNum++; }
|
|
|
|
#else
|
|
|
|
void initCreateInstNum() {}
|
|
|
|
void incCreateInstNum() {}
|
|
|
|
#endif
|
|
|
|
};
|
2013-04-05 21:20:12 +00:00
|
|
|
}
|
2012-12-18 23:10:12 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Implementation of
|
|
|
|
// {FAddendCoef, FAddend, FAddition, FAddCombine}.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
FAddendCoef::~FAddendCoef() {
|
|
|
|
if (BufHasFpVal)
|
|
|
|
getFpValPtr()->~APFloat();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FAddendCoef::set(const APFloat& C) {
|
|
|
|
APFloat *P = getFpValPtr();
|
|
|
|
|
|
|
|
if (isInt()) {
|
|
|
|
// As the buffer is meanless byte stream, we cannot call
|
|
|
|
// APFloat::operator=().
|
|
|
|
new(P) APFloat(C);
|
|
|
|
} else
|
|
|
|
*P = C;
|
|
|
|
|
2013-04-05 21:20:12 +00:00
|
|
|
IsFp = BufHasFpVal = true;
|
2012-12-18 23:10:12 +00:00
|
|
|
}
|
|
|
|
|
2013-03-25 20:43:41 +00:00
|
|
|
void FAddendCoef::convertToFpType(const fltSemantics &Sem) {
|
|
|
|
if (!isInt())
|
|
|
|
return;
|
|
|
|
|
|
|
|
APFloat *P = getFpValPtr();
|
|
|
|
if (IntVal > 0)
|
|
|
|
new(P) APFloat(Sem, IntVal);
|
|
|
|
else {
|
|
|
|
new(P) APFloat(Sem, 0 - IntVal);
|
|
|
|
P->changeSign();
|
|
|
|
}
|
2013-04-05 21:20:12 +00:00
|
|
|
IsFp = BufHasFpVal = true;
|
2013-03-25 20:43:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
APFloat FAddendCoef::createAPFloatFromInt(const fltSemantics &Sem, int Val) {
|
|
|
|
if (Val >= 0)
|
|
|
|
return APFloat(Sem, Val);
|
|
|
|
|
|
|
|
APFloat T(Sem, 0 - Val);
|
|
|
|
T.changeSign();
|
|
|
|
|
|
|
|
return T;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FAddendCoef::operator=(const FAddendCoef &That) {
|
2012-12-18 23:10:12 +00:00
|
|
|
if (That.isInt())
|
|
|
|
set(That.IntVal);
|
|
|
|
else
|
|
|
|
set(That.getFpVal());
|
|
|
|
}
|
|
|
|
|
|
|
|
void FAddendCoef::operator+=(const FAddendCoef &That) {
|
|
|
|
enum APFloat::roundingMode RndMode = APFloat::rmNearestTiesToEven;
|
|
|
|
if (isInt() == That.isInt()) {
|
|
|
|
if (isInt())
|
|
|
|
IntVal += That.IntVal;
|
|
|
|
else
|
|
|
|
getFpVal().add(That.getFpVal(), RndMode);
|
|
|
|
return;
|
|
|
|
}
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
if (isInt()) {
|
|
|
|
const APFloat &T = That.getFpVal();
|
2013-03-25 20:43:41 +00:00
|
|
|
convertToFpType(T.getSemantics());
|
|
|
|
getFpVal().add(T, RndMode);
|
2012-12-18 23:10:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
APFloat &T = getFpVal();
|
2013-03-25 20:43:41 +00:00
|
|
|
T.add(createAPFloatFromInt(T.getSemantics(), That.IntVal), RndMode);
|
2012-12-18 23:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FAddendCoef::operator-=(const FAddendCoef &That) {
|
|
|
|
enum APFloat::roundingMode RndMode = APFloat::rmNearestTiesToEven;
|
|
|
|
if (isInt() == That.isInt()) {
|
|
|
|
if (isInt())
|
|
|
|
IntVal -= That.IntVal;
|
|
|
|
else
|
|
|
|
getFpVal().subtract(That.getFpVal(), RndMode);
|
|
|
|
return;
|
|
|
|
}
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
if (isInt()) {
|
|
|
|
const APFloat &T = That.getFpVal();
|
2013-03-25 20:43:41 +00:00
|
|
|
convertToFpType(T.getSemantics());
|
|
|
|
getFpVal().subtract(T, RndMode);
|
2012-12-18 23:10:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
APFloat &T = getFpVal();
|
2013-03-25 20:43:41 +00:00
|
|
|
T.subtract(createAPFloatFromInt(T.getSemantics(), IntVal), RndMode);
|
2012-12-18 23:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FAddendCoef::operator*=(const FAddendCoef &That) {
|
|
|
|
if (That.isOne())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (That.isMinusOne()) {
|
|
|
|
negate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isInt() && That.isInt()) {
|
|
|
|
int Res = IntVal * (int)That.IntVal;
|
|
|
|
assert(!insaneIntVal(Res) && "Insane int value");
|
|
|
|
IntVal = Res;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-05 21:20:12 +00:00
|
|
|
const fltSemantics &Semantic =
|
2012-12-18 23:10:12 +00:00
|
|
|
isInt() ? That.getFpVal().getSemantics() : getFpVal().getSemantics();
|
|
|
|
|
|
|
|
if (isInt())
|
2013-03-25 20:43:41 +00:00
|
|
|
convertToFpType(Semantic);
|
2012-12-18 23:10:12 +00:00
|
|
|
APFloat &F0 = getFpVal();
|
|
|
|
|
|
|
|
if (That.isInt())
|
2013-03-25 20:43:41 +00:00
|
|
|
F0.multiply(createAPFloatFromInt(Semantic, That.IntVal),
|
|
|
|
APFloat::rmNearestTiesToEven);
|
2012-12-18 23:10:12 +00:00
|
|
|
else
|
|
|
|
F0.multiply(That.getFpVal(), APFloat::rmNearestTiesToEven);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FAddendCoef::negate() {
|
|
|
|
if (isInt())
|
|
|
|
IntVal = 0 - IntVal;
|
|
|
|
else
|
|
|
|
getFpVal().changeSign();
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *FAddendCoef::getValue(Type *Ty) const {
|
|
|
|
return isInt() ?
|
|
|
|
ConstantFP::get(Ty, float(IntVal)) :
|
|
|
|
ConstantFP::get(Ty->getContext(), getFpVal());
|
|
|
|
}
|
|
|
|
|
|
|
|
// The definition of <Val> Addends
|
|
|
|
// =========================================
|
|
|
|
// A + B <1, A>, <1,B>
|
|
|
|
// A - B <1, A>, <1,B>
|
|
|
|
// 0 - B <-1, B>
|
|
|
|
// C * A, <C, A>
|
2013-04-05 21:20:12 +00:00
|
|
|
// A + C <1, A> <C, NULL>
|
2012-12-18 23:10:12 +00:00
|
|
|
// 0 +/- 0 <0, NULL> (corner case)
|
|
|
|
//
|
|
|
|
// Legend: A and B are not constant, C is constant
|
2013-04-05 21:20:12 +00:00
|
|
|
//
|
2012-12-18 23:10:12 +00:00
|
|
|
unsigned FAddend::drillValueDownOneStep
|
|
|
|
(Value *Val, FAddend &Addend0, FAddend &Addend1) {
|
2014-04-25 05:29:35 +00:00
|
|
|
Instruction *I = nullptr;
|
|
|
|
if (!Val || !(I = dyn_cast<Instruction>(Val)))
|
2012-12-18 23:10:12 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
unsigned Opcode = I->getOpcode();
|
|
|
|
|
|
|
|
if (Opcode == Instruction::FAdd || Opcode == Instruction::FSub) {
|
|
|
|
ConstantFP *C0, *C1;
|
|
|
|
Value *Opnd0 = I->getOperand(0);
|
|
|
|
Value *Opnd1 = I->getOperand(1);
|
|
|
|
if ((C0 = dyn_cast<ConstantFP>(Opnd0)) && C0->isZero())
|
2014-04-25 05:29:35 +00:00
|
|
|
Opnd0 = nullptr;
|
2012-12-18 23:10:12 +00:00
|
|
|
|
|
|
|
if ((C1 = dyn_cast<ConstantFP>(Opnd1)) && C1->isZero())
|
2014-04-25 05:29:35 +00:00
|
|
|
Opnd1 = nullptr;
|
2012-12-18 23:10:12 +00:00
|
|
|
|
|
|
|
if (Opnd0) {
|
|
|
|
if (!C0)
|
|
|
|
Addend0.set(1, Opnd0);
|
|
|
|
else
|
2014-04-25 05:29:35 +00:00
|
|
|
Addend0.set(C0, nullptr);
|
2012-12-18 23:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Opnd1) {
|
|
|
|
FAddend &Addend = Opnd0 ? Addend1 : Addend0;
|
|
|
|
if (!C1)
|
|
|
|
Addend.set(1, Opnd1);
|
|
|
|
else
|
2014-04-25 05:29:35 +00:00
|
|
|
Addend.set(C1, nullptr);
|
2012-12-18 23:10:12 +00:00
|
|
|
if (Opcode == Instruction::FSub)
|
|
|
|
Addend.negate();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Opnd0 || Opnd1)
|
|
|
|
return Opnd0 && Opnd1 ? 2 : 1;
|
|
|
|
|
|
|
|
// Both operands are zero. Weird!
|
2014-04-25 05:29:35 +00:00
|
|
|
Addend0.set(APFloat(C0->getValueAPF().getSemantics()), nullptr);
|
2012-12-18 23:10:12 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (I->getOpcode() == Instruction::FMul) {
|
|
|
|
Value *V0 = I->getOperand(0);
|
|
|
|
Value *V1 = I->getOperand(1);
|
|
|
|
if (ConstantFP *C = dyn_cast<ConstantFP>(V0)) {
|
|
|
|
Addend0.set(C, V1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ConstantFP *C = dyn_cast<ConstantFP>(V1)) {
|
|
|
|
Addend0.set(C, V0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to break *this* addend into two addends. e.g. Suppose this addend is
|
|
|
|
// <2.3, V>, and V = X + Y, by calling this function, we obtain two addends,
|
|
|
|
// i.e. <2.3, X> and <2.3, Y>.
|
|
|
|
//
|
|
|
|
unsigned FAddend::drillAddendDownOneStep
|
|
|
|
(FAddend &Addend0, FAddend &Addend1) const {
|
|
|
|
if (isConstant())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
unsigned BreakNum = FAddend::drillValueDownOneStep(Val, Addend0, Addend1);
|
2013-04-05 21:20:12 +00:00
|
|
|
if (!BreakNum || Coeff.isOne())
|
2012-12-18 23:10:12 +00:00
|
|
|
return BreakNum;
|
|
|
|
|
|
|
|
Addend0.Scale(Coeff);
|
|
|
|
|
|
|
|
if (BreakNum == 2)
|
|
|
|
Addend1.Scale(Coeff);
|
|
|
|
|
|
|
|
return BreakNum;
|
|
|
|
}
|
|
|
|
|
2013-03-14 18:08:26 +00:00
|
|
|
// Try to perform following optimization on the input instruction I. Return the
|
|
|
|
// simplified expression if was successful; otherwise, return 0.
|
|
|
|
//
|
|
|
|
// Instruction "I" is Simplified into
|
|
|
|
// -------------------------------------------------------
|
|
|
|
// (x * y) +/- (x * z) x * (y +/- z)
|
|
|
|
// (y / x) +/- (z / x) (y +/- z) / x
|
|
|
|
//
|
|
|
|
Value *FAddCombine::performFactorization(Instruction *I) {
|
|
|
|
assert((I->getOpcode() == Instruction::FAdd ||
|
|
|
|
I->getOpcode() == Instruction::FSub) && "Expect add/sub");
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2013-03-14 18:08:26 +00:00
|
|
|
Instruction *I0 = dyn_cast<Instruction>(I->getOperand(0));
|
|
|
|
Instruction *I1 = dyn_cast<Instruction>(I->getOperand(1));
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2013-03-14 18:08:26 +00:00
|
|
|
if (!I0 || !I1 || I0->getOpcode() != I1->getOpcode())
|
2014-04-25 05:29:35 +00:00
|
|
|
return nullptr;
|
2013-03-14 18:08:26 +00:00
|
|
|
|
|
|
|
bool isMpy = false;
|
|
|
|
if (I0->getOpcode() == Instruction::FMul)
|
|
|
|
isMpy = true;
|
|
|
|
else if (I0->getOpcode() != Instruction::FDiv)
|
2014-04-25 05:29:35 +00:00
|
|
|
return nullptr;
|
2013-03-14 18:08:26 +00:00
|
|
|
|
|
|
|
Value *Opnd0_0 = I0->getOperand(0);
|
|
|
|
Value *Opnd0_1 = I0->getOperand(1);
|
|
|
|
Value *Opnd1_0 = I1->getOperand(0);
|
|
|
|
Value *Opnd1_1 = I1->getOperand(1);
|
|
|
|
|
2013-04-05 21:20:12 +00:00
|
|
|
// Input Instr I Factor AddSub0 AddSub1
|
2013-03-14 18:08:26 +00:00
|
|
|
// ----------------------------------------------
|
|
|
|
// (x*y) +/- (x*z) x y z
|
|
|
|
// (y/x) +/- (z/x) x y z
|
|
|
|
//
|
2014-04-25 05:29:35 +00:00
|
|
|
Value *Factor = nullptr;
|
|
|
|
Value *AddSub0 = nullptr, *AddSub1 = nullptr;
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2013-03-14 18:08:26 +00:00
|
|
|
if (isMpy) {
|
|
|
|
if (Opnd0_0 == Opnd1_0 || Opnd0_0 == Opnd1_1)
|
|
|
|
Factor = Opnd0_0;
|
|
|
|
else if (Opnd0_1 == Opnd1_0 || Opnd0_1 == Opnd1_1)
|
|
|
|
Factor = Opnd0_1;
|
|
|
|
|
|
|
|
if (Factor) {
|
|
|
|
AddSub0 = (Factor == Opnd0_0) ? Opnd0_1 : Opnd0_0;
|
|
|
|
AddSub1 = (Factor == Opnd1_0) ? Opnd1_1 : Opnd1_0;
|
|
|
|
}
|
|
|
|
} else if (Opnd0_1 == Opnd1_1) {
|
|
|
|
Factor = Opnd0_1;
|
|
|
|
AddSub0 = Opnd0_0;
|
|
|
|
AddSub1 = Opnd1_0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Factor)
|
2014-04-25 05:29:35 +00:00
|
|
|
return nullptr;
|
2013-03-14 18:08:26 +00:00
|
|
|
|
2014-01-20 07:44:53 +00:00
|
|
|
FastMathFlags Flags;
|
|
|
|
Flags.setUnsafeAlgebra();
|
|
|
|
if (I0) Flags &= I->getFastMathFlags();
|
|
|
|
if (I1) Flags &= I->getFastMathFlags();
|
|
|
|
|
2013-03-14 18:08:26 +00:00
|
|
|
// Create expression "NewAddSub = AddSub0 +/- AddsSub1"
|
|
|
|
Value *NewAddSub = (I->getOpcode() == Instruction::FAdd) ?
|
|
|
|
createFAdd(AddSub0, AddSub1) :
|
|
|
|
createFSub(AddSub0, AddSub1);
|
|
|
|
if (ConstantFP *CFP = dyn_cast<ConstantFP>(NewAddSub)) {
|
|
|
|
const APFloat &F = CFP->getValueAPF();
|
2013-06-26 23:17:31 +00:00
|
|
|
if (!F.isNormal())
|
2014-04-25 05:29:35 +00:00
|
|
|
return nullptr;
|
2014-01-20 07:44:53 +00:00
|
|
|
} else if (Instruction *II = dyn_cast<Instruction>(NewAddSub))
|
|
|
|
II->setFastMathFlags(Flags);
|
2013-03-14 18:08:26 +00:00
|
|
|
|
2014-01-20 07:44:53 +00:00
|
|
|
if (isMpy) {
|
|
|
|
Value *RI = createFMul(Factor, NewAddSub);
|
|
|
|
if (Instruction *II = dyn_cast<Instruction>(RI))
|
|
|
|
II->setFastMathFlags(Flags);
|
|
|
|
return RI;
|
|
|
|
}
|
2013-04-05 21:20:12 +00:00
|
|
|
|
2014-01-20 07:44:53 +00:00
|
|
|
Value *RI = createFDiv(NewAddSub, Factor);
|
|
|
|
if (Instruction *II = dyn_cast<Instruction>(RI))
|
|
|
|
II->setFastMathFlags(Flags);
|
|
|
|
return RI;
|
2013-03-14 18:08:26 +00:00
|
|
|
}
|
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
Value *FAddCombine::simplify(Instruction *I) {
|
|
|
|
assert(I->hasUnsafeAlgebra() && "Should be in unsafe mode");
|
|
|
|
|
|
|
|
// Currently we are not able to handle vector type.
|
|
|
|
if (I->getType()->isVectorTy())
|
2014-04-25 05:29:35 +00:00
|
|
|
return nullptr;
|
2012-12-18 23:10:12 +00:00
|
|
|
|
|
|
|
assert((I->getOpcode() == Instruction::FAdd ||
|
|
|
|
I->getOpcode() == Instruction::FSub) && "Expect add/sub");
|
|
|
|
|
2013-04-05 21:20:12 +00:00
|
|
|
// Save the instruction before calling other member-functions.
|
2012-12-18 23:10:12 +00:00
|
|
|
Instr = I;
|
|
|
|
|
|
|
|
FAddend Opnd0, Opnd1, Opnd0_0, Opnd0_1, Opnd1_0, Opnd1_1;
|
|
|
|
|
|
|
|
unsigned OpndNum = FAddend::drillValueDownOneStep(I, Opnd0, Opnd1);
|
|
|
|
|
|
|
|
// Step 1: Expand the 1st addend into Opnd0_0 and Opnd0_1.
|
|
|
|
unsigned Opnd0_ExpNum = 0;
|
|
|
|
unsigned Opnd1_ExpNum = 0;
|
|
|
|
|
2013-04-05 21:20:12 +00:00
|
|
|
if (!Opnd0.isConstant())
|
2012-12-18 23:10:12 +00:00
|
|
|
Opnd0_ExpNum = Opnd0.drillAddendDownOneStep(Opnd0_0, Opnd0_1);
|
|
|
|
|
|
|
|
// Step 2: Expand the 2nd addend into Opnd1_0 and Opnd1_1.
|
|
|
|
if (OpndNum == 2 && !Opnd1.isConstant())
|
|
|
|
Opnd1_ExpNum = Opnd1.drillAddendDownOneStep(Opnd1_0, Opnd1_1);
|
|
|
|
|
|
|
|
// Step 3: Try to optimize Opnd0_0 + Opnd0_1 + Opnd1_0 + Opnd1_1
|
|
|
|
if (Opnd0_ExpNum && Opnd1_ExpNum) {
|
|
|
|
AddendVect AllOpnds;
|
|
|
|
AllOpnds.push_back(&Opnd0_0);
|
|
|
|
AllOpnds.push_back(&Opnd1_0);
|
|
|
|
if (Opnd0_ExpNum == 2)
|
|
|
|
AllOpnds.push_back(&Opnd0_1);
|
|
|
|
if (Opnd1_ExpNum == 2)
|
|
|
|
AllOpnds.push_back(&Opnd1_1);
|
|
|
|
|
|
|
|
// Compute instruction quota. We should save at least one instruction.
|
|
|
|
unsigned InstQuota = 0;
|
|
|
|
|
|
|
|
Value *V0 = I->getOperand(0);
|
|
|
|
Value *V1 = I->getOperand(1);
|
2013-04-05 21:20:12 +00:00
|
|
|
InstQuota = ((!isa<Constant>(V0) && V0->hasOneUse()) &&
|
2012-12-18 23:10:12 +00:00
|
|
|
(!isa<Constant>(V1) && V1->hasOneUse())) ? 2 : 1;
|
|
|
|
|
|
|
|
if (Value *R = simplifyFAdd(AllOpnds, InstQuota))
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (OpndNum != 2) {
|
|
|
|
// The input instruction is : "I=0.0 +/- V". If the "V" were able to be
|
|
|
|
// splitted into two addends, say "V = X - Y", the instruction would have
|
|
|
|
// been optimized into "I = Y - X" in the previous steps.
|
|
|
|
//
|
|
|
|
const FAddendCoef &CE = Opnd0.getCoef();
|
2014-04-25 05:29:35 +00:00
|
|
|
return CE.isOne() ? Opnd0.getSymVal() : nullptr;
|
2012-12-18 23:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// step 4: Try to optimize Opnd0 + Opnd1_0 [+ Opnd1_1]
|
|
|
|
if (Opnd1_ExpNum) {
|
|
|
|
AddendVect AllOpnds;
|
|
|
|
AllOpnds.push_back(&Opnd0);
|
|
|
|
AllOpnds.push_back(&Opnd1_0);
|
|
|
|
if (Opnd1_ExpNum == 2)
|
|
|
|
AllOpnds.push_back(&Opnd1_1);
|
|
|
|
|
|
|
|
if (Value *R = simplifyFAdd(AllOpnds, 1))
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
|
|
|
// step 5: Try to optimize Opnd1 + Opnd0_0 [+ Opnd0_1]
|
|
|
|
if (Opnd0_ExpNum) {
|
|
|
|
AddendVect AllOpnds;
|
|
|
|
AllOpnds.push_back(&Opnd1);
|
|
|
|
AllOpnds.push_back(&Opnd0_0);
|
|
|
|
if (Opnd0_ExpNum == 2)
|
|
|
|
AllOpnds.push_back(&Opnd0_1);
|
|
|
|
|
|
|
|
if (Value *R = simplifyFAdd(AllOpnds, 1))
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
2013-04-05 21:20:12 +00:00
|
|
|
// step 6: Try factorization as the last resort,
|
2013-03-14 18:08:26 +00:00
|
|
|
return performFactorization(I);
|
2012-12-18 23:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Value *FAddCombine::simplifyFAdd(AddendVect& Addends, unsigned InstrQuota) {
|
|
|
|
|
|
|
|
unsigned AddendNum = Addends.size();
|
|
|
|
assert(AddendNum <= 4 && "Too many addends");
|
|
|
|
|
2013-04-05 21:20:12 +00:00
|
|
|
// For saving intermediate results;
|
2012-12-18 23:10:12 +00:00
|
|
|
unsigned NextTmpIdx = 0;
|
|
|
|
FAddend TmpResult[3];
|
|
|
|
|
|
|
|
// Points to the constant addend of the resulting simplified expression.
|
|
|
|
// If the resulting expr has constant-addend, this constant-addend is
|
|
|
|
// desirable to reside at the top of the resulting expression tree. Placing
|
|
|
|
// constant close to supper-expr(s) will potentially reveal some optimization
|
|
|
|
// opportunities in super-expr(s).
|
|
|
|
//
|
2014-04-25 05:29:35 +00:00
|
|
|
const FAddend *ConstAdd = nullptr;
|
2012-12-18 23:10:12 +00:00
|
|
|
|
|
|
|
// Simplified addends are placed <SimpVect>.
|
|
|
|
AddendVect SimpVect;
|
|
|
|
|
|
|
|
// The outer loop works on one symbolic-value at a time. Suppose the input
|
2013-04-05 21:20:12 +00:00
|
|
|
// addends are : <a1, x>, <b1, y>, <a2, x>, <c1, z>, <b2, y>, ...
|
2012-12-18 23:10:12 +00:00
|
|
|
// The symbolic-values will be processed in this order: x, y, z.
|
|
|
|
//
|
|
|
|
for (unsigned SymIdx = 0; SymIdx < AddendNum; SymIdx++) {
|
|
|
|
|
|
|
|
const FAddend *ThisAddend = Addends[SymIdx];
|
|
|
|
if (!ThisAddend) {
|
|
|
|
// This addend was processed before.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *Val = ThisAddend->getSymVal();
|
|
|
|
unsigned StartIdx = SimpVect.size();
|
|
|
|
SimpVect.push_back(ThisAddend);
|
|
|
|
|
|
|
|
// The inner loop collects addends sharing same symbolic-value, and these
|
|
|
|
// addends will be later on folded into a single addend. Following above
|
|
|
|
// example, if the symbolic value "y" is being processed, the inner loop
|
|
|
|
// will collect two addends "<b1,y>" and "<b2,Y>". These two addends will
|
|
|
|
// be later on folded into "<b1+b2, y>".
|
|
|
|
//
|
|
|
|
for (unsigned SameSymIdx = SymIdx + 1;
|
|
|
|
SameSymIdx < AddendNum; SameSymIdx++) {
|
|
|
|
const FAddend *T = Addends[SameSymIdx];
|
|
|
|
if (T && T->getSymVal() == Val) {
|
|
|
|
// Set null such that next iteration of the outer loop will not process
|
|
|
|
// this addend again.
|
2014-04-25 05:29:35 +00:00
|
|
|
Addends[SameSymIdx] = nullptr;
|
2012-12-18 23:10:12 +00:00
|
|
|
SimpVect.push_back(T);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If multiple addends share same symbolic value, fold them together.
|
|
|
|
if (StartIdx + 1 != SimpVect.size()) {
|
|
|
|
FAddend &R = TmpResult[NextTmpIdx ++];
|
|
|
|
R = *SimpVect[StartIdx];
|
|
|
|
for (unsigned Idx = StartIdx + 1; Idx < SimpVect.size(); Idx++)
|
|
|
|
R += *SimpVect[Idx];
|
|
|
|
|
|
|
|
// Pop all addends being folded and push the resulting folded addend.
|
2013-04-05 21:20:12 +00:00
|
|
|
SimpVect.resize(StartIdx);
|
2014-04-25 05:29:35 +00:00
|
|
|
if (Val) {
|
2012-12-18 23:10:12 +00:00
|
|
|
if (!R.isZero()) {
|
|
|
|
SimpVect.push_back(&R);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Don't push constant addend at this time. It will be the last element
|
|
|
|
// of <SimpVect>.
|
|
|
|
ConstAdd = &R;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-15 04:27:47 +00:00
|
|
|
assert((NextTmpIdx <= array_lengthof(TmpResult) + 1) &&
|
2012-12-18 23:10:12 +00:00
|
|
|
"out-of-bound access");
|
|
|
|
|
|
|
|
if (ConstAdd)
|
|
|
|
SimpVect.push_back(ConstAdd);
|
|
|
|
|
|
|
|
Value *Result;
|
|
|
|
if (!SimpVect.empty())
|
|
|
|
Result = createNaryFAdd(SimpVect, InstrQuota);
|
|
|
|
else {
|
|
|
|
// The addition is folded to 0.0.
|
|
|
|
Result = ConstantFP::get(Instr->getType(), 0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *FAddCombine::createNaryFAdd
|
|
|
|
(const AddendVect &Opnds, unsigned InstrQuota) {
|
|
|
|
assert(!Opnds.empty() && "Expect at least one addend");
|
|
|
|
|
|
|
|
// Step 1: Check if the # of instructions needed exceeds the quota.
|
2013-04-05 21:20:12 +00:00
|
|
|
//
|
2012-12-18 23:10:12 +00:00
|
|
|
unsigned InstrNeeded = calcInstrNumber(Opnds);
|
|
|
|
if (InstrNeeded > InstrQuota)
|
2014-04-25 05:29:35 +00:00
|
|
|
return nullptr;
|
2012-12-18 23:10:12 +00:00
|
|
|
|
|
|
|
initCreateInstNum();
|
|
|
|
|
|
|
|
// step 2: Emit the N-ary addition.
|
|
|
|
// Note that at most three instructions are involved in Fadd-InstCombine: the
|
|
|
|
// addition in question, and at most two neighboring instructions.
|
|
|
|
// The resulting optimized addition should have at least one less instruction
|
|
|
|
// than the original addition expression tree. This implies that the resulting
|
|
|
|
// N-ary addition has at most two instructions, and we don't need to worry
|
|
|
|
// about tree-height when constructing the N-ary addition.
|
|
|
|
|
2014-04-25 05:29:35 +00:00
|
|
|
Value *LastVal = nullptr;
|
2012-12-18 23:10:12 +00:00
|
|
|
bool LastValNeedNeg = false;
|
|
|
|
|
|
|
|
// Iterate the addends, creating fadd/fsub using adjacent two addends.
|
|
|
|
for (AddendVect::const_iterator I = Opnds.begin(), E = Opnds.end();
|
|
|
|
I != E; I++) {
|
2013-04-05 21:20:12 +00:00
|
|
|
bool NeedNeg;
|
2012-12-18 23:10:12 +00:00
|
|
|
Value *V = createAddendVal(**I, NeedNeg);
|
|
|
|
if (!LastVal) {
|
|
|
|
LastVal = V;
|
|
|
|
LastValNeedNeg = NeedNeg;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LastValNeedNeg == NeedNeg) {
|
|
|
|
LastVal = createFAdd(LastVal, V);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LastValNeedNeg)
|
|
|
|
LastVal = createFSub(V, LastVal);
|
|
|
|
else
|
|
|
|
LastVal = createFSub(LastVal, V);
|
|
|
|
|
|
|
|
LastValNeedNeg = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LastValNeedNeg) {
|
|
|
|
LastVal = createFNeg(LastVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2013-04-05 21:20:12 +00:00
|
|
|
assert(CreateInstrNum == InstrNeeded &&
|
2012-12-18 23:10:12 +00:00
|
|
|
"Inconsistent in instruction numbers");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return LastVal;
|
|
|
|
}
|
|
|
|
|
2014-12-18 21:11:09 +00:00
|
|
|
Value *FAddCombine::createFSub(Value *Opnd0, Value *Opnd1) {
|
2012-12-18 23:10:12 +00:00
|
|
|
Value *V = Builder->CreateFSub(Opnd0, Opnd1);
|
2013-03-14 18:08:26 +00:00
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V))
|
|
|
|
createInstPostProc(I);
|
2012-12-18 23:10:12 +00:00
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *FAddCombine::createFNeg(Value *V) {
|
2014-12-19 16:44:08 +00:00
|
|
|
Value *Zero = cast<Value>(ConstantFP::getZeroValueForNegation(V->getType()));
|
2014-01-20 07:44:53 +00:00
|
|
|
Value *NewV = createFSub(Zero, V);
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(NewV))
|
|
|
|
createInstPostProc(I, true); // fneg's don't receive instruction numbers.
|
|
|
|
return NewV;
|
2012-12-18 23:10:12 +00:00
|
|
|
}
|
|
|
|
|
2014-12-18 21:11:09 +00:00
|
|
|
Value *FAddCombine::createFAdd(Value *Opnd0, Value *Opnd1) {
|
2012-12-18 23:10:12 +00:00
|
|
|
Value *V = Builder->CreateFAdd(Opnd0, Opnd1);
|
2013-03-14 18:08:26 +00:00
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V))
|
|
|
|
createInstPostProc(I);
|
2012-12-18 23:10:12 +00:00
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *FAddCombine::createFMul(Value *Opnd0, Value *Opnd1) {
|
|
|
|
Value *V = Builder->CreateFMul(Opnd0, Opnd1);
|
2013-03-14 18:08:26 +00:00
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V))
|
|
|
|
createInstPostProc(I);
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *FAddCombine::createFDiv(Value *Opnd0, Value *Opnd1) {
|
|
|
|
Value *V = Builder->CreateFDiv(Opnd0, Opnd1);
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V))
|
|
|
|
createInstPostProc(I);
|
2012-12-18 23:10:12 +00:00
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2014-12-18 21:11:09 +00:00
|
|
|
void FAddCombine::createInstPostProc(Instruction *NewInstr, bool NoNumber) {
|
2012-12-18 23:10:12 +00:00
|
|
|
NewInstr->setDebugLoc(Instr->getDebugLoc());
|
|
|
|
|
|
|
|
// Keep track of the number of instruction created.
|
2014-01-20 07:44:53 +00:00
|
|
|
if (!NoNumber)
|
|
|
|
incCreateInstNum();
|
2012-12-18 23:10:12 +00:00
|
|
|
|
|
|
|
// Propagate fast-math flags
|
|
|
|
NewInstr->setFastMathFlags(Instr->getFastMathFlags());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the number of instruction needed to emit the N-ary addition.
|
|
|
|
// NOTE: Keep this function in sync with createAddendVal().
|
|
|
|
unsigned FAddCombine::calcInstrNumber(const AddendVect &Opnds) {
|
|
|
|
unsigned OpndNum = Opnds.size();
|
|
|
|
unsigned InstrNeeded = OpndNum - 1;
|
|
|
|
|
2013-04-05 21:20:12 +00:00
|
|
|
// The number of addends in the form of "(-1)*x".
|
|
|
|
unsigned NegOpndNum = 0;
|
2012-12-18 23:10:12 +00:00
|
|
|
|
|
|
|
// Adjust the number of instructions needed to emit the N-ary add.
|
|
|
|
for (AddendVect::const_iterator I = Opnds.begin(), E = Opnds.end();
|
|
|
|
I != E; I++) {
|
|
|
|
const FAddend *Opnd = *I;
|
|
|
|
if (Opnd->isConstant())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const FAddendCoef &CE = Opnd->getCoef();
|
|
|
|
if (CE.isMinusOne() || CE.isMinusTwo())
|
|
|
|
NegOpndNum++;
|
|
|
|
|
|
|
|
// Let the addend be "c * x". If "c == +/-1", the value of the addend
|
|
|
|
// is immediately available; otherwise, it needs exactly one instruction
|
|
|
|
// to evaluate the value.
|
|
|
|
if (!CE.isMinusOne() && !CE.isOne())
|
|
|
|
InstrNeeded++;
|
|
|
|
}
|
|
|
|
if (NegOpndNum == OpndNum)
|
|
|
|
InstrNeeded++;
|
|
|
|
return InstrNeeded;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Input Addend Value NeedNeg(output)
|
|
|
|
// ================================================================
|
|
|
|
// Constant C C false
|
|
|
|
// <+/-1, V> V coefficient is -1
|
|
|
|
// <2/-2, V> "fadd V, V" coefficient is -2
|
|
|
|
// <C, V> "fmul V, C" false
|
|
|
|
//
|
|
|
|
// NOTE: Keep this function in sync with FAddCombine::calcInstrNumber.
|
2014-12-18 21:11:09 +00:00
|
|
|
Value *FAddCombine::createAddendVal(const FAddend &Opnd, bool &NeedNeg) {
|
2012-12-18 23:10:12 +00:00
|
|
|
const FAddendCoef &Coeff = Opnd.getCoef();
|
|
|
|
|
|
|
|
if (Opnd.isConstant()) {
|
|
|
|
NeedNeg = false;
|
|
|
|
return Coeff.getValue(Instr->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *OpndVal = Opnd.getSymVal();
|
|
|
|
|
|
|
|
if (Coeff.isMinusOne() || Coeff.isOne()) {
|
|
|
|
NeedNeg = Coeff.isMinusOne();
|
|
|
|
return OpndVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Coeff.isTwo() || Coeff.isMinusTwo()) {
|
|
|
|
NeedNeg = Coeff.isMinusTwo();
|
|
|
|
return createFAdd(OpndVal, OpndVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
NeedNeg = false;
|
|
|
|
return createFMul(OpndVal, Coeff.getValue(Instr->getType()));
|
|
|
|
}
|
|
|
|
|
2014-06-04 15:39:14 +00:00
|
|
|
// If one of the operands only has one non-zero bit, and if the other
|
|
|
|
// operand has a known-zero bit in a more significant place than it (not
|
|
|
|
// including the sign bit) the ripple may go up to and fill the zero, but
|
|
|
|
// won't change the sign. For example, (X & ~4) + 1.
|
|
|
|
static bool checkRippleForAdd(const APInt &Op0KnownZero,
|
|
|
|
const APInt &Op1KnownZero) {
|
|
|
|
APInt Op1MaybeOne = ~Op1KnownZero;
|
|
|
|
// Make sure that one of the operand has at most one bit set to 1.
|
|
|
|
if (Op1MaybeOne.countPopulation() != 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Find the most significant known 0 other than the sign bit.
|
|
|
|
int BitWidth = Op0KnownZero.getBitWidth();
|
|
|
|
APInt Op0KnownZeroTemp(Op0KnownZero);
|
|
|
|
Op0KnownZeroTemp.clearBit(BitWidth - 1);
|
|
|
|
int Op0ZeroPosition = BitWidth - Op0KnownZeroTemp.countLeadingZeros() - 1;
|
|
|
|
|
|
|
|
int Op1OnePosition = BitWidth - Op1MaybeOne.countLeadingZeros() - 1;
|
|
|
|
assert(Op1OnePosition >= 0);
|
|
|
|
|
|
|
|
// This also covers the case of no known zero, since in that case
|
|
|
|
// Op0ZeroPosition is -1.
|
|
|
|
return Op0ZeroPosition >= Op1OnePosition;
|
|
|
|
}
|
2010-01-05 07:18:46 +00:00
|
|
|
|
|
|
|
/// WillNotOverflowSignedAdd - Return true if we can prove that:
|
|
|
|
/// (sext (add LHS, RHS)) === (add (sext LHS), (sext RHS))
|
|
|
|
/// This basically requires proving that the add in the original type would not
|
|
|
|
/// overflow to change the sign bit or have a carry out.
|
Make use of @llvm.assume in ValueTracking (computeKnownBits, etc.)
This change, which allows @llvm.assume to be used from within computeKnownBits
(and other associated functions in ValueTracking), adds some (optional)
parameters to computeKnownBits and friends. These functions now (optionally)
take a "context" instruction pointer, an AssumptionTracker pointer, and also a
DomTree pointer, and most of the changes are just to pass this new information
when it is easily available from InstSimplify, InstCombine, etc.
As explained below, the significant conceptual change is that known properties
of a value might depend on the control-flow location of the use (because we
care that the @llvm.assume dominates the use because assumptions have
control-flow dependencies). This means that, when we ask if bits are known in a
value, we might get different answers for different uses.
The significant changes are all in ValueTracking. Two main changes: First, as
with the rest of the code, new parameters need to be passed around. To make
this easier, I grouped them into a structure, and I made internal static
versions of the relevant functions that take this structure as a parameter. The
new code does as you might expect, it looks for @llvm.assume calls that make
use of the value we're trying to learn something about (often indirectly),
attempts to pattern match that expression, and uses the result if successful.
By making use of the AssumptionTracker, the process of finding @llvm.assume
calls is not expensive.
Part of the structure being passed around inside ValueTracking is a set of
already-considered @llvm.assume calls. This is to prevent a query using, for
example, the assume(a == b), to recurse on itself. The context and DT params
are used to find applicable assumptions. An assumption needs to dominate the
context instruction, or come after it deterministically. In this latter case we
only handle the specific case where both the assumption and the context
instruction are in the same block, and we need to exclude assumptions from
being used to simplify their own ephemeral values (those which contribute only
to the assumption) because otherwise the assumption would prove its feeding
comparison trivial and would be removed.
This commit adds the plumbing and the logic for a simple masked-bit propagation
(just enough to write a regression test). Future commits add more patterns
(and, correspondingly, more regression tests).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217342 91177308-0d34-0410-b5e6-96231b3b80d8
2014-09-07 18:57:58 +00:00
|
|
|
bool InstCombiner::WillNotOverflowSignedAdd(Value *LHS, Value *RHS,
|
|
|
|
Instruction *CxtI) {
|
2010-01-05 07:18:46 +00:00
|
|
|
// There are different heuristics we can use for this. Here are some simple
|
|
|
|
// ones.
|
2012-12-12 20:57:53 +00:00
|
|
|
|
2014-06-02 22:01:04 +00:00
|
|
|
// If LHS and RHS each have at least two sign bits, the addition will look
|
|
|
|
// like
|
|
|
|
//
|
|
|
|
// XX..... +
|
|
|
|
// YY.....
|
|
|
|
//
|
|
|
|
// If the carry into the most significant position is 0, X and Y can't both
|
|
|
|
// be 1 and therefore the carry out of the addition is also 0.
|
|
|
|
//
|
|
|
|
// If the carry into the most significant position is 1, X and Y can't both
|
|
|
|
// be 0 and therefore the carry out of the addition is also 1.
|
|
|
|
//
|
|
|
|
// Since the carry into the most significant position is always equal to
|
|
|
|
// the carry out of the addition, there is no signed overflow.
|
Make use of @llvm.assume in ValueTracking (computeKnownBits, etc.)
This change, which allows @llvm.assume to be used from within computeKnownBits
(and other associated functions in ValueTracking), adds some (optional)
parameters to computeKnownBits and friends. These functions now (optionally)
take a "context" instruction pointer, an AssumptionTracker pointer, and also a
DomTree pointer, and most of the changes are just to pass this new information
when it is easily available from InstSimplify, InstCombine, etc.
As explained below, the significant conceptual change is that known properties
of a value might depend on the control-flow location of the use (because we
care that the @llvm.assume dominates the use because assumptions have
control-flow dependencies). This means that, when we ask if bits are known in a
value, we might get different answers for different uses.
The significant changes are all in ValueTracking. Two main changes: First, as
with the rest of the code, new parameters need to be passed around. To make
this easier, I grouped them into a structure, and I made internal static
versions of the relevant functions that take this structure as a parameter. The
new code does as you might expect, it looks for @llvm.assume calls that make
use of the value we're trying to learn something about (often indirectly),
attempts to pattern match that expression, and uses the result if successful.
By making use of the AssumptionTracker, the process of finding @llvm.assume
calls is not expensive.
Part of the structure being passed around inside ValueTracking is a set of
already-considered @llvm.assume calls. This is to prevent a query using, for
example, the assume(a == b), to recurse on itself. The context and DT params
are used to find applicable assumptions. An assumption needs to dominate the
context instruction, or come after it deterministically. In this latter case we
only handle the specific case where both the assumption and the context
instruction are in the same block, and we need to exclude assumptions from
being used to simplify their own ephemeral values (those which contribute only
to the assumption) because otherwise the assumption would prove its feeding
comparison trivial and would be removed.
This commit adds the plumbing and the logic for a simple masked-bit propagation
(just enough to write a regression test). Future commits add more patterns
(and, correspondingly, more regression tests).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217342 91177308-0d34-0410-b5e6-96231b3b80d8
2014-09-07 18:57:58 +00:00
|
|
|
if (ComputeNumSignBits(LHS, 0, CxtI) > 1 &&
|
|
|
|
ComputeNumSignBits(RHS, 0, CxtI) > 1)
|
2010-01-05 07:18:46 +00:00
|
|
|
return true;
|
2012-12-12 20:57:53 +00:00
|
|
|
|
2014-12-26 09:10:14 +00:00
|
|
|
unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
|
|
|
|
APInt LHSKnownZero(BitWidth, 0);
|
|
|
|
APInt LHSKnownOne(BitWidth, 0);
|
|
|
|
computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, 0, CxtI);
|
|
|
|
|
|
|
|
APInt RHSKnownZero(BitWidth, 0);
|
|
|
|
APInt RHSKnownOne(BitWidth, 0);
|
|
|
|
computeKnownBits(RHS, RHSKnownZero, RHSKnownOne, 0, CxtI);
|
|
|
|
|
|
|
|
// Addition of two 2's compliment numbers having opposite signs will never
|
|
|
|
// overflow.
|
|
|
|
if ((LHSKnownOne[BitWidth - 1] && RHSKnownZero[BitWidth - 1]) ||
|
|
|
|
(LHSKnownZero[BitWidth - 1] && RHSKnownOne[BitWidth - 1]))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Check if carry bit of addition will not cause overflow.
|
|
|
|
if (checkRippleForAdd(LHSKnownZero, RHSKnownZero))
|
|
|
|
return true;
|
|
|
|
if (checkRippleForAdd(RHSKnownZero, LHSKnownZero))
|
|
|
|
return true;
|
|
|
|
|
2010-01-05 07:18:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-19 23:36:30 +00:00
|
|
|
/// \brief Return true if we can prove that:
|
|
|
|
/// (sub LHS, RHS) === (sub nsw LHS, RHS)
|
|
|
|
/// This basically requires proving that the add in the original type would not
|
|
|
|
/// overflow to change the sign bit or have a carry out.
|
|
|
|
/// TODO: Handle this for Vectors.
|
Make use of @llvm.assume in ValueTracking (computeKnownBits, etc.)
This change, which allows @llvm.assume to be used from within computeKnownBits
(and other associated functions in ValueTracking), adds some (optional)
parameters to computeKnownBits and friends. These functions now (optionally)
take a "context" instruction pointer, an AssumptionTracker pointer, and also a
DomTree pointer, and most of the changes are just to pass this new information
when it is easily available from InstSimplify, InstCombine, etc.
As explained below, the significant conceptual change is that known properties
of a value might depend on the control-flow location of the use (because we
care that the @llvm.assume dominates the use because assumptions have
control-flow dependencies). This means that, when we ask if bits are known in a
value, we might get different answers for different uses.
The significant changes are all in ValueTracking. Two main changes: First, as
with the rest of the code, new parameters need to be passed around. To make
this easier, I grouped them into a structure, and I made internal static
versions of the relevant functions that take this structure as a parameter. The
new code does as you might expect, it looks for @llvm.assume calls that make
use of the value we're trying to learn something about (often indirectly),
attempts to pattern match that expression, and uses the result if successful.
By making use of the AssumptionTracker, the process of finding @llvm.assume
calls is not expensive.
Part of the structure being passed around inside ValueTracking is a set of
already-considered @llvm.assume calls. This is to prevent a query using, for
example, the assume(a == b), to recurse on itself. The context and DT params
are used to find applicable assumptions. An assumption needs to dominate the
context instruction, or come after it deterministically. In this latter case we
only handle the specific case where both the assumption and the context
instruction are in the same block, and we need to exclude assumptions from
being used to simplify their own ephemeral values (those which contribute only
to the assumption) because otherwise the assumption would prove its feeding
comparison trivial and would be removed.
This commit adds the plumbing and the logic for a simple masked-bit propagation
(just enough to write a regression test). Future commits add more patterns
(and, correspondingly, more regression tests).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217342 91177308-0d34-0410-b5e6-96231b3b80d8
2014-09-07 18:57:58 +00:00
|
|
|
bool InstCombiner::WillNotOverflowSignedSub(Value *LHS, Value *RHS,
|
|
|
|
Instruction *CxtI) {
|
2014-08-19 23:36:30 +00:00
|
|
|
// If LHS and RHS each have at least two sign bits, the subtraction
|
|
|
|
// cannot overflow.
|
Make use of @llvm.assume in ValueTracking (computeKnownBits, etc.)
This change, which allows @llvm.assume to be used from within computeKnownBits
(and other associated functions in ValueTracking), adds some (optional)
parameters to computeKnownBits and friends. These functions now (optionally)
take a "context" instruction pointer, an AssumptionTracker pointer, and also a
DomTree pointer, and most of the changes are just to pass this new information
when it is easily available from InstSimplify, InstCombine, etc.
As explained below, the significant conceptual change is that known properties
of a value might depend on the control-flow location of the use (because we
care that the @llvm.assume dominates the use because assumptions have
control-flow dependencies). This means that, when we ask if bits are known in a
value, we might get different answers for different uses.
The significant changes are all in ValueTracking. Two main changes: First, as
with the rest of the code, new parameters need to be passed around. To make
this easier, I grouped them into a structure, and I made internal static
versions of the relevant functions that take this structure as a parameter. The
new code does as you might expect, it looks for @llvm.assume calls that make
use of the value we're trying to learn something about (often indirectly),
attempts to pattern match that expression, and uses the result if successful.
By making use of the AssumptionTracker, the process of finding @llvm.assume
calls is not expensive.
Part of the structure being passed around inside ValueTracking is a set of
already-considered @llvm.assume calls. This is to prevent a query using, for
example, the assume(a == b), to recurse on itself. The context and DT params
are used to find applicable assumptions. An assumption needs to dominate the
context instruction, or come after it deterministically. In this latter case we
only handle the specific case where both the assumption and the context
instruction are in the same block, and we need to exclude assumptions from
being used to simplify their own ephemeral values (those which contribute only
to the assumption) because otherwise the assumption would prove its feeding
comparison trivial and would be removed.
This commit adds the plumbing and the logic for a simple masked-bit propagation
(just enough to write a regression test). Future commits add more patterns
(and, correspondingly, more regression tests).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217342 91177308-0d34-0410-b5e6-96231b3b80d8
2014-09-07 18:57:58 +00:00
|
|
|
if (ComputeNumSignBits(LHS, 0, CxtI) > 1 &&
|
|
|
|
ComputeNumSignBits(RHS, 0, CxtI) > 1)
|
2014-08-19 23:36:30 +00:00
|
|
|
return true;
|
|
|
|
|
2014-12-26 09:10:14 +00:00
|
|
|
unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
|
|
|
|
APInt LHSKnownZero(BitWidth, 0);
|
|
|
|
APInt LHSKnownOne(BitWidth, 0);
|
|
|
|
computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, 0, CxtI);
|
2014-08-19 23:36:30 +00:00
|
|
|
|
2014-12-26 09:10:14 +00:00
|
|
|
APInt RHSKnownZero(BitWidth, 0);
|
|
|
|
APInt RHSKnownOne(BitWidth, 0);
|
|
|
|
computeKnownBits(RHS, RHSKnownZero, RHSKnownOne, 0, CxtI);
|
2014-08-19 23:36:30 +00:00
|
|
|
|
2014-12-26 09:10:14 +00:00
|
|
|
// Subtraction of two 2's compliment numbers having identical signs will
|
|
|
|
// never overflow.
|
|
|
|
if ((LHSKnownOne[BitWidth - 1] && RHSKnownOne[BitWidth - 1]) ||
|
|
|
|
(LHSKnownZero[BitWidth - 1] && RHSKnownZero[BitWidth - 1]))
|
|
|
|
return true;
|
2014-08-19 23:36:30 +00:00
|
|
|
|
2014-12-26 09:10:14 +00:00
|
|
|
// TODO: implement logic similar to checkRippleForAdd
|
2014-08-19 23:36:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-20 07:17:31 +00:00
|
|
|
/// \brief Return true if we can prove that:
|
|
|
|
/// (sub LHS, RHS) === (sub nuw LHS, RHS)
|
Make use of @llvm.assume in ValueTracking (computeKnownBits, etc.)
This change, which allows @llvm.assume to be used from within computeKnownBits
(and other associated functions in ValueTracking), adds some (optional)
parameters to computeKnownBits and friends. These functions now (optionally)
take a "context" instruction pointer, an AssumptionTracker pointer, and also a
DomTree pointer, and most of the changes are just to pass this new information
when it is easily available from InstSimplify, InstCombine, etc.
As explained below, the significant conceptual change is that known properties
of a value might depend on the control-flow location of the use (because we
care that the @llvm.assume dominates the use because assumptions have
control-flow dependencies). This means that, when we ask if bits are known in a
value, we might get different answers for different uses.
The significant changes are all in ValueTracking. Two main changes: First, as
with the rest of the code, new parameters need to be passed around. To make
this easier, I grouped them into a structure, and I made internal static
versions of the relevant functions that take this structure as a parameter. The
new code does as you might expect, it looks for @llvm.assume calls that make
use of the value we're trying to learn something about (often indirectly),
attempts to pattern match that expression, and uses the result if successful.
By making use of the AssumptionTracker, the process of finding @llvm.assume
calls is not expensive.
Part of the structure being passed around inside ValueTracking is a set of
already-considered @llvm.assume calls. This is to prevent a query using, for
example, the assume(a == b), to recurse on itself. The context and DT params
are used to find applicable assumptions. An assumption needs to dominate the
context instruction, or come after it deterministically. In this latter case we
only handle the specific case where both the assumption and the context
instruction are in the same block, and we need to exclude assumptions from
being used to simplify their own ephemeral values (those which contribute only
to the assumption) because otherwise the assumption would prove its feeding
comparison trivial and would be removed.
This commit adds the plumbing and the logic for a simple masked-bit propagation
(just enough to write a regression test). Future commits add more patterns
(and, correspondingly, more regression tests).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217342 91177308-0d34-0410-b5e6-96231b3b80d8
2014-09-07 18:57:58 +00:00
|
|
|
bool InstCombiner::WillNotOverflowUnsignedSub(Value *LHS, Value *RHS,
|
|
|
|
Instruction *CxtI) {
|
2014-08-20 07:17:31 +00:00
|
|
|
// If the LHS is negative and the RHS is non-negative, no unsigned wrap.
|
|
|
|
bool LHSKnownNonNegative, LHSKnownNegative;
|
|
|
|
bool RHSKnownNonNegative, RHSKnownNegative;
|
2014-12-26 09:10:14 +00:00
|
|
|
ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, /*Depth=*/0, CxtI);
|
|
|
|
ComputeSignBit(RHS, RHSKnownNonNegative, RHSKnownNegative, /*Depth=*/0, CxtI);
|
2014-08-20 07:17:31 +00:00
|
|
|
if (LHSKnownNegative && RHSKnownNonNegative)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-19 10:36:52 +00:00
|
|
|
// Checks if any operand is negative and we can convert add to sub.
|
2014-06-27 07:47:35 +00:00
|
|
|
// This function checks for following negative patterns
|
|
|
|
// ADD(XOR(OR(Z, NOT(C)), C)), 1) == NEG(AND(Z, C))
|
|
|
|
// ADD(XOR(AND(Z, C), C), 1) == NEG(OR(Z, ~C))
|
|
|
|
// XOR(AND(Z, C), (C + 1)) == NEG(OR(Z, ~C)) if C is even
|
2014-07-07 14:47:51 +00:00
|
|
|
static Value *checkForNegativeOperand(BinaryOperator &I,
|
|
|
|
InstCombiner::BuilderTy *Builder) {
|
2014-06-27 07:47:35 +00:00
|
|
|
Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
|
|
|
|
|
|
|
|
// This function creates 2 instructions to replace ADD, we need at least one
|
|
|
|
// of LHS or RHS to have one use to ensure benefit in transform.
|
|
|
|
if (!LHS->hasOneUse() && !RHS->hasOneUse())
|
|
|
|
return nullptr;
|
2014-06-26 05:40:22 +00:00
|
|
|
|
2014-06-27 07:47:35 +00:00
|
|
|
Value *X = nullptr, *Y = nullptr, *Z = nullptr;
|
|
|
|
const APInt *C1 = nullptr, *C2 = nullptr;
|
|
|
|
|
|
|
|
// if ONE is on other side, swap
|
|
|
|
if (match(RHS, m_Add(m_Value(X), m_One())))
|
|
|
|
std::swap(LHS, RHS);
|
|
|
|
|
|
|
|
if (match(LHS, m_Add(m_Value(X), m_One()))) {
|
|
|
|
// if XOR on other side, swap
|
|
|
|
if (match(RHS, m_Xor(m_Value(Y), m_APInt(C1))))
|
|
|
|
std::swap(X, RHS);
|
|
|
|
|
|
|
|
if (match(X, m_Xor(m_Value(Y), m_APInt(C1)))) {
|
|
|
|
// X = XOR(Y, C1), Y = OR(Z, C2), C2 = NOT(C1) ==> X == NOT(AND(Z, C1))
|
|
|
|
// ADD(ADD(X, 1), RHS) == ADD(X, ADD(RHS, 1)) == SUB(RHS, AND(Z, C1))
|
|
|
|
if (match(Y, m_Or(m_Value(Z), m_APInt(C2))) && (*C2 == ~(*C1))) {
|
|
|
|
Value *NewAnd = Builder->CreateAnd(Z, *C1);
|
|
|
|
return Builder->CreateSub(RHS, NewAnd, "sub");
|
|
|
|
} else if (match(Y, m_And(m_Value(Z), m_APInt(C2))) && (*C1 == *C2)) {
|
|
|
|
// X = XOR(Y, C1), Y = AND(Z, C2), C2 == C1 ==> X == NOT(OR(Z, ~C1))
|
|
|
|
// ADD(ADD(X, 1), RHS) == ADD(X, ADD(RHS, 1)) == SUB(RHS, OR(Z, ~C1))
|
|
|
|
Value *NewOr = Builder->CreateOr(Z, ~(*C1));
|
|
|
|
return Builder->CreateSub(RHS, NewOr, "sub");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore LHS and RHS
|
|
|
|
LHS = I.getOperand(0);
|
|
|
|
RHS = I.getOperand(1);
|
|
|
|
|
|
|
|
// if XOR is on other side, swap
|
|
|
|
if (match(RHS, m_Xor(m_Value(Y), m_APInt(C1))))
|
|
|
|
std::swap(LHS, RHS);
|
|
|
|
|
|
|
|
// C2 is ODD
|
|
|
|
// LHS = XOR(Y, C1), Y = AND(Z, C2), C1 == (C2 + 1) => LHS == NEG(OR(Z, ~C2))
|
|
|
|
// ADD(LHS, RHS) == SUB(RHS, OR(Z, ~C2))
|
|
|
|
if (match(LHS, m_Xor(m_Value(Y), m_APInt(C1))))
|
|
|
|
if (C1->countTrailingZeros() == 0)
|
|
|
|
if (match(Y, m_And(m_Value(Z), m_APInt(C2))) && *C1 == (*C2 + 1)) {
|
|
|
|
Value *NewOr = Builder->CreateOr(Z, ~(*C2));
|
|
|
|
return Builder->CreateSub(RHS, NewOr, "sub");
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
|
2014-06-26 05:40:22 +00:00
|
|
|
bool Changed = SimplifyAssociativeOrCommutative(I);
|
|
|
|
Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
|
2010-01-05 07:18:46 +00:00
|
|
|
|
2014-06-26 05:40:22 +00:00
|
|
|
if (Value *V = SimplifyVectorOp(I))
|
|
|
|
return ReplaceInstUsesWith(I, V);
|
2014-05-11 08:46:12 +00:00
|
|
|
|
2014-06-26 05:40:22 +00:00
|
|
|
if (Value *V = SimplifyAddInst(LHS, RHS, I.hasNoSignedWrap(),
|
2015-01-04 12:03:27 +00:00
|
|
|
I.hasNoUnsignedWrap(), DL, TLI, DT, AC))
|
2014-06-26 05:40:22 +00:00
|
|
|
return ReplaceInstUsesWith(I, V);
|
2010-01-05 07:18:46 +00:00
|
|
|
|
2014-06-26 05:40:22 +00:00
|
|
|
// (A*B)+(A*C) -> A*(B+C) etc
|
2010-12-22 13:36:08 +00:00
|
|
|
if (Value *V = SimplifyUsingDistributiveLaws(I))
|
|
|
|
return ReplaceInstUsesWith(I, V);
|
|
|
|
|
2011-02-10 05:14:58 +00:00
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
|
|
|
|
// X + (signbit) --> X ^ signbit
|
|
|
|
const APInt &Val = CI->getValue();
|
|
|
|
if (Val.isSignBit())
|
|
|
|
return BinaryOperator::CreateXor(LHS, RHS);
|
2012-12-12 20:57:53 +00:00
|
|
|
|
2011-02-10 05:14:58 +00:00
|
|
|
// See if SimplifyDemandedBits can simplify this. This handles stuff like
|
|
|
|
// (X & 254)+1 -> (X&254)|1
|
|
|
|
if (SimplifyDemandedInstructionBits(I))
|
|
|
|
return &I;
|
|
|
|
|
|
|
|
// zext(bool) + C -> bool ? C + 1 : C
|
|
|
|
if (ZExtInst *ZI = dyn_cast<ZExtInst>(LHS))
|
|
|
|
if (ZI->getSrcTy()->isIntegerTy(1))
|
|
|
|
return SelectInst::Create(ZI->getOperand(0), AddOne(CI), CI);
|
2012-12-12 20:57:53 +00:00
|
|
|
|
2014-04-25 05:29:35 +00:00
|
|
|
Value *XorLHS = nullptr; ConstantInt *XorRHS = nullptr;
|
2011-02-10 05:14:58 +00:00
|
|
|
if (match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) {
|
2010-01-05 07:18:46 +00:00
|
|
|
uint32_t TySizeBits = I.getType()->getScalarSizeInBits();
|
2011-02-10 05:14:58 +00:00
|
|
|
const APInt &RHSVal = CI->getValue();
|
2010-01-31 04:29:12 +00:00
|
|
|
unsigned ExtendAmt = 0;
|
|
|
|
// If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext.
|
|
|
|
// If we have ADD(XOR(AND(X, 0xFF), 0xF..F80), 0x80), it's a sext.
|
|
|
|
if (XorRHS->getValue() == -RHSVal) {
|
|
|
|
if (RHSVal.isPowerOf2())
|
|
|
|
ExtendAmt = TySizeBits - RHSVal.logBase2() - 1;
|
|
|
|
else if (XorRHS->getValue().isPowerOf2())
|
|
|
|
ExtendAmt = TySizeBits - XorRHS->getValue().logBase2() - 1;
|
2010-01-05 07:18:46 +00:00
|
|
|
}
|
2012-12-12 20:57:53 +00:00
|
|
|
|
2010-01-31 04:29:12 +00:00
|
|
|
if (ExtendAmt) {
|
|
|
|
APInt Mask = APInt::getHighBitsSet(TySizeBits, ExtendAmt);
|
Make use of @llvm.assume in ValueTracking (computeKnownBits, etc.)
This change, which allows @llvm.assume to be used from within computeKnownBits
(and other associated functions in ValueTracking), adds some (optional)
parameters to computeKnownBits and friends. These functions now (optionally)
take a "context" instruction pointer, an AssumptionTracker pointer, and also a
DomTree pointer, and most of the changes are just to pass this new information
when it is easily available from InstSimplify, InstCombine, etc.
As explained below, the significant conceptual change is that known properties
of a value might depend on the control-flow location of the use (because we
care that the @llvm.assume dominates the use because assumptions have
control-flow dependencies). This means that, when we ask if bits are known in a
value, we might get different answers for different uses.
The significant changes are all in ValueTracking. Two main changes: First, as
with the rest of the code, new parameters need to be passed around. To make
this easier, I grouped them into a structure, and I made internal static
versions of the relevant functions that take this structure as a parameter. The
new code does as you might expect, it looks for @llvm.assume calls that make
use of the value we're trying to learn something about (often indirectly),
attempts to pattern match that expression, and uses the result if successful.
By making use of the AssumptionTracker, the process of finding @llvm.assume
calls is not expensive.
Part of the structure being passed around inside ValueTracking is a set of
already-considered @llvm.assume calls. This is to prevent a query using, for
example, the assume(a == b), to recurse on itself. The context and DT params
are used to find applicable assumptions. An assumption needs to dominate the
context instruction, or come after it deterministically. In this latter case we
only handle the specific case where both the assumption and the context
instruction are in the same block, and we need to exclude assumptions from
being used to simplify their own ephemeral values (those which contribute only
to the assumption) because otherwise the assumption would prove its feeding
comparison trivial and would be removed.
This commit adds the plumbing and the logic for a simple masked-bit propagation
(just enough to write a regression test). Future commits add more patterns
(and, correspondingly, more regression tests).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217342 91177308-0d34-0410-b5e6-96231b3b80d8
2014-09-07 18:57:58 +00:00
|
|
|
if (!MaskedValueIsZero(XorLHS, Mask, 0, &I))
|
2010-01-31 04:29:12 +00:00
|
|
|
ExtendAmt = 0;
|
|
|
|
}
|
2012-12-12 20:57:53 +00:00
|
|
|
|
2010-01-31 04:29:12 +00:00
|
|
|
if (ExtendAmt) {
|
|
|
|
Constant *ShAmt = ConstantInt::get(I.getType(), ExtendAmt);
|
|
|
|
Value *NewShl = Builder->CreateShl(XorLHS, ShAmt, "sext");
|
|
|
|
return BinaryOperator::CreateAShr(NewShl, ShAmt);
|
2010-01-05 07:18:46 +00:00
|
|
|
}
|
2011-12-24 17:31:53 +00:00
|
|
|
|
|
|
|
// If this is a xor that was canonicalized from a sub, turn it back into
|
|
|
|
// a sub and fuse this add with it.
|
|
|
|
if (LHS->hasOneUse() && (XorRHS->getValue()+1).isPowerOf2()) {
|
|
|
|
IntegerType *IT = cast<IntegerType>(I.getType());
|
|
|
|
APInt LHSKnownOne(IT->getBitWidth(), 0);
|
|
|
|
APInt LHSKnownZero(IT->getBitWidth(), 0);
|
Make use of @llvm.assume in ValueTracking (computeKnownBits, etc.)
This change, which allows @llvm.assume to be used from within computeKnownBits
(and other associated functions in ValueTracking), adds some (optional)
parameters to computeKnownBits and friends. These functions now (optionally)
take a "context" instruction pointer, an AssumptionTracker pointer, and also a
DomTree pointer, and most of the changes are just to pass this new information
when it is easily available from InstSimplify, InstCombine, etc.
As explained below, the significant conceptual change is that known properties
of a value might depend on the control-flow location of the use (because we
care that the @llvm.assume dominates the use because assumptions have
control-flow dependencies). This means that, when we ask if bits are known in a
value, we might get different answers for different uses.
The significant changes are all in ValueTracking. Two main changes: First, as
with the rest of the code, new parameters need to be passed around. To make
this easier, I grouped them into a structure, and I made internal static
versions of the relevant functions that take this structure as a parameter. The
new code does as you might expect, it looks for @llvm.assume calls that make
use of the value we're trying to learn something about (often indirectly),
attempts to pattern match that expression, and uses the result if successful.
By making use of the AssumptionTracker, the process of finding @llvm.assume
calls is not expensive.
Part of the structure being passed around inside ValueTracking is a set of
already-considered @llvm.assume calls. This is to prevent a query using, for
example, the assume(a == b), to recurse on itself. The context and DT params
are used to find applicable assumptions. An assumption needs to dominate the
context instruction, or come after it deterministically. In this latter case we
only handle the specific case where both the assumption and the context
instruction are in the same block, and we need to exclude assumptions from
being used to simplify their own ephemeral values (those which contribute only
to the assumption) because otherwise the assumption would prove its feeding
comparison trivial and would be removed.
This commit adds the plumbing and the logic for a simple masked-bit propagation
(just enough to write a regression test). Future commits add more patterns
(and, correspondingly, more regression tests).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217342 91177308-0d34-0410-b5e6-96231b3b80d8
2014-09-07 18:57:58 +00:00
|
|
|
computeKnownBits(XorLHS, LHSKnownZero, LHSKnownOne, 0, &I);
|
2011-12-24 17:31:53 +00:00
|
|
|
if ((XorRHS->getValue() | LHSKnownZero).isAllOnesValue())
|
|
|
|
return BinaryOperator::CreateSub(ConstantExpr::getAdd(XorRHS, CI),
|
|
|
|
XorLHS);
|
|
|
|
}
|
2013-05-06 21:21:31 +00:00
|
|
|
// (X + signbit) + C could have gotten canonicalized to (X ^ signbit) + C,
|
|
|
|
// transform them into (X + (signbit ^ C))
|
|
|
|
if (XorRHS->getValue().isSignBit())
|
|
|
|
return BinaryOperator::CreateAdd(XorLHS,
|
|
|
|
ConstantExpr::getXor(XorRHS, CI));
|
2010-01-05 07:18:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-10 05:14:58 +00:00
|
|
|
if (isa<Constant>(RHS) && isa<PHINode>(LHS))
|
|
|
|
if (Instruction *NV = FoldOpIntoPhi(I))
|
|
|
|
return NV;
|
|
|
|
|
2014-01-19 15:24:22 +00:00
|
|
|
if (I.getType()->getScalarType()->isIntegerTy(1))
|
2010-01-05 07:18:46 +00:00
|
|
|
return BinaryOperator::CreateXor(LHS, RHS);
|
|
|
|
|
2011-02-10 05:14:58 +00:00
|
|
|
// X + X --> X << 1
|
2011-02-17 20:55:29 +00:00
|
|
|
if (LHS == RHS) {
|
2011-02-17 02:23:02 +00:00
|
|
|
BinaryOperator *New =
|
|
|
|
BinaryOperator::CreateShl(LHS, ConstantInt::get(I.getType(), 1));
|
|
|
|
New->setHasNoSignedWrap(I.hasNoSignedWrap());
|
|
|
|
New->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
|
|
|
|
return New;
|
|
|
|
}
|
2010-01-05 07:18:46 +00:00
|
|
|
|
|
|
|
// -A + B --> B - A
|
|
|
|
// -A + -B --> -(A + B)
|
|
|
|
if (Value *LHSV = dyn_castNegVal(LHS)) {
|
2012-06-08 22:30:05 +00:00
|
|
|
if (!isa<Constant>(RHS))
|
|
|
|
if (Value *RHSV = dyn_castNegVal(RHS)) {
|
|
|
|
Value *NewAdd = Builder->CreateAdd(LHSV, RHSV, "sum");
|
|
|
|
return BinaryOperator::CreateNeg(NewAdd);
|
|
|
|
}
|
2012-12-12 20:57:53 +00:00
|
|
|
|
2010-01-05 07:18:46 +00:00
|
|
|
return BinaryOperator::CreateSub(RHS, LHSV);
|
|
|
|
}
|
|
|
|
|
|
|
|
// A + -B --> A - B
|
|
|
|
if (!isa<Constant>(RHS))
|
|
|
|
if (Value *V = dyn_castNegVal(RHS))
|
|
|
|
return BinaryOperator::CreateSub(LHS, V);
|
|
|
|
|
2014-06-19 10:36:52 +00:00
|
|
|
if (Value *V = checkForNegativeOperand(I, Builder))
|
|
|
|
return ReplaceInstUsesWith(I, V);
|
|
|
|
|
2012-09-27 10:14:43 +00:00
|
|
|
// A+B --> A|B iff A and B have no bits set in common.
|
2011-07-18 04:54:35 +00:00
|
|
|
if (IntegerType *IT = dyn_cast<IntegerType>(I.getType())) {
|
2010-01-05 07:18:46 +00:00
|
|
|
APInt LHSKnownOne(IT->getBitWidth(), 0);
|
|
|
|
APInt LHSKnownZero(IT->getBitWidth(), 0);
|
Make use of @llvm.assume in ValueTracking (computeKnownBits, etc.)
This change, which allows @llvm.assume to be used from within computeKnownBits
(and other associated functions in ValueTracking), adds some (optional)
parameters to computeKnownBits and friends. These functions now (optionally)
take a "context" instruction pointer, an AssumptionTracker pointer, and also a
DomTree pointer, and most of the changes are just to pass this new information
when it is easily available from InstSimplify, InstCombine, etc.
As explained below, the significant conceptual change is that known properties
of a value might depend on the control-flow location of the use (because we
care that the @llvm.assume dominates the use because assumptions have
control-flow dependencies). This means that, when we ask if bits are known in a
value, we might get different answers for different uses.
The significant changes are all in ValueTracking. Two main changes: First, as
with the rest of the code, new parameters need to be passed around. To make
this easier, I grouped them into a structure, and I made internal static
versions of the relevant functions that take this structure as a parameter. The
new code does as you might expect, it looks for @llvm.assume calls that make
use of the value we're trying to learn something about (often indirectly),
attempts to pattern match that expression, and uses the result if successful.
By making use of the AssumptionTracker, the process of finding @llvm.assume
calls is not expensive.
Part of the structure being passed around inside ValueTracking is a set of
already-considered @llvm.assume calls. This is to prevent a query using, for
example, the assume(a == b), to recurse on itself. The context and DT params
are used to find applicable assumptions. An assumption needs to dominate the
context instruction, or come after it deterministically. In this latter case we
only handle the specific case where both the assumption and the context
instruction are in the same block, and we need to exclude assumptions from
being used to simplify their own ephemeral values (those which contribute only
to the assumption) because otherwise the assumption would prove its feeding
comparison trivial and would be removed.
This commit adds the plumbing and the logic for a simple masked-bit propagation
(just enough to write a regression test). Future commits add more patterns
(and, correspondingly, more regression tests).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217342 91177308-0d34-0410-b5e6-96231b3b80d8
2014-09-07 18:57:58 +00:00
|
|
|
computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, 0, &I);
|
2010-01-05 07:18:46 +00:00
|
|
|
if (LHSKnownZero != 0) {
|
|
|
|
APInt RHSKnownOne(IT->getBitWidth(), 0);
|
|
|
|
APInt RHSKnownZero(IT->getBitWidth(), 0);
|
Make use of @llvm.assume in ValueTracking (computeKnownBits, etc.)
This change, which allows @llvm.assume to be used from within computeKnownBits
(and other associated functions in ValueTracking), adds some (optional)
parameters to computeKnownBits and friends. These functions now (optionally)
take a "context" instruction pointer, an AssumptionTracker pointer, and also a
DomTree pointer, and most of the changes are just to pass this new information
when it is easily available from InstSimplify, InstCombine, etc.
As explained below, the significant conceptual change is that known properties
of a value might depend on the control-flow location of the use (because we
care that the @llvm.assume dominates the use because assumptions have
control-flow dependencies). This means that, when we ask if bits are known in a
value, we might get different answers for different uses.
The significant changes are all in ValueTracking. Two main changes: First, as
with the rest of the code, new parameters need to be passed around. To make
this easier, I grouped them into a structure, and I made internal static
versions of the relevant functions that take this structure as a parameter. The
new code does as you might expect, it looks for @llvm.assume calls that make
use of the value we're trying to learn something about (often indirectly),
attempts to pattern match that expression, and uses the result if successful.
By making use of the AssumptionTracker, the process of finding @llvm.assume
calls is not expensive.
Part of the structure being passed around inside ValueTracking is a set of
already-considered @llvm.assume calls. This is to prevent a query using, for
example, the assume(a == b), to recurse on itself. The context and DT params
are used to find applicable assumptions. An assumption needs to dominate the
context instruction, or come after it deterministically. In this latter case we
only handle the specific case where both the assumption and the context
instruction are in the same block, and we need to exclude assumptions from
being used to simplify their own ephemeral values (those which contribute only
to the assumption) because otherwise the assumption would prove its feeding
comparison trivial and would be removed.
This commit adds the plumbing and the logic for a simple masked-bit propagation
(just enough to write a regression test). Future commits add more patterns
(and, correspondingly, more regression tests).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217342 91177308-0d34-0410-b5e6-96231b3b80d8
2014-09-07 18:57:58 +00:00
|
|
|
computeKnownBits(RHS, RHSKnownZero, RHSKnownOne, 0, &I);
|
2012-12-12 20:57:53 +00:00
|
|
|
|
2010-01-05 07:18:46 +00:00
|
|
|
// No bits in common -> bitwise or.
|
|
|
|
if ((LHSKnownZero|RHSKnownZero).isAllOnesValue())
|
|
|
|
return BinaryOperator::CreateOr(LHS, RHS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-19 15:24:22 +00:00
|
|
|
if (Constant *CRHS = dyn_cast<Constant>(RHS)) {
|
|
|
|
Value *X;
|
|
|
|
if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X
|
2010-01-05 07:18:46 +00:00
|
|
|
return BinaryOperator::CreateSub(SubOne(CRHS), X);
|
2014-01-19 15:24:22 +00:00
|
|
|
}
|
2010-01-05 07:18:46 +00:00
|
|
|
|
2014-01-19 15:24:22 +00:00
|
|
|
if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) {
|
2010-01-05 07:18:46 +00:00
|
|
|
// (X & FF00) + xx00 -> (X+xx00) & FF00
|
2014-01-19 15:24:22 +00:00
|
|
|
Value *X;
|
|
|
|
ConstantInt *C2;
|
2010-01-05 07:18:46 +00:00
|
|
|
if (LHS->hasOneUse() &&
|
2011-02-10 05:14:58 +00:00
|
|
|
match(LHS, m_And(m_Value(X), m_ConstantInt(C2))) &&
|
|
|
|
CRHS->getValue() == (CRHS->getValue() & C2->getValue())) {
|
|
|
|
// See if all bits from the first bit set in the Add RHS up are included
|
|
|
|
// in the mask. First, get the rightmost bit.
|
|
|
|
const APInt &AddRHSV = CRHS->getValue();
|
2012-12-12 20:57:53 +00:00
|
|
|
|
2011-02-10 05:14:58 +00:00
|
|
|
// Form a mask of all bits from the lowest bit added through the top.
|
|
|
|
APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1));
|
|
|
|
|
|
|
|
// See if the and mask includes all of these bits.
|
|
|
|
APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue());
|
|
|
|
|
|
|
|
if (AddRHSHighBits == AddRHSHighBitsAnd) {
|
|
|
|
// Okay, the xform is safe. Insert the new add pronto.
|
|
|
|
Value *NewAdd = Builder->CreateAdd(X, CRHS, LHS->getName());
|
|
|
|
return BinaryOperator::CreateAnd(NewAdd, C2);
|
2010-01-05 07:18:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to fold constant add into select arguments.
|
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(LHS))
|
|
|
|
if (Instruction *R = FoldOpIntoSelect(I, SI))
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add (select X 0 (sub n A)) A --> select X A n
|
|
|
|
{
|
|
|
|
SelectInst *SI = dyn_cast<SelectInst>(LHS);
|
|
|
|
Value *A = RHS;
|
|
|
|
if (!SI) {
|
|
|
|
SI = dyn_cast<SelectInst>(RHS);
|
|
|
|
A = LHS;
|
|
|
|
}
|
|
|
|
if (SI && SI->hasOneUse()) {
|
|
|
|
Value *TV = SI->getTrueValue();
|
|
|
|
Value *FV = SI->getFalseValue();
|
|
|
|
Value *N;
|
|
|
|
|
|
|
|
// Can we fold the add into the argument of the select?
|
|
|
|
// We check both true and false select arguments for a matching subtract.
|
2011-02-10 05:14:58 +00:00
|
|
|
if (match(FV, m_Zero()) && match(TV, m_Sub(m_Value(N), m_Specific(A))))
|
2010-01-05 07:18:46 +00:00
|
|
|
// Fold the add into the true select value.
|
|
|
|
return SelectInst::Create(SI->getCondition(), N, A);
|
2012-12-12 20:57:53 +00:00
|
|
|
|
2011-02-10 05:14:58 +00:00
|
|
|
if (match(TV, m_Zero()) && match(FV, m_Sub(m_Value(N), m_Specific(A))))
|
2010-01-05 07:18:46 +00:00
|
|
|
// Fold the add into the false select value.
|
|
|
|
return SelectInst::Create(SI->getCondition(), A, N);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for (add (sext x), y), see if we can merge this into an
|
|
|
|
// integer add followed by a sext.
|
|
|
|
if (SExtInst *LHSConv = dyn_cast<SExtInst>(LHS)) {
|
|
|
|
// (add (sext x), cst) --> (sext (add x, cst'))
|
|
|
|
if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) {
|
2012-12-12 20:57:53 +00:00
|
|
|
Constant *CI =
|
2010-01-05 07:18:46 +00:00
|
|
|
ConstantExpr::getTrunc(RHSC, LHSConv->getOperand(0)->getType());
|
|
|
|
if (LHSConv->hasOneUse() &&
|
|
|
|
ConstantExpr::getSExt(CI, I.getType()) == RHSC &&
|
Make use of @llvm.assume in ValueTracking (computeKnownBits, etc.)
This change, which allows @llvm.assume to be used from within computeKnownBits
(and other associated functions in ValueTracking), adds some (optional)
parameters to computeKnownBits and friends. These functions now (optionally)
take a "context" instruction pointer, an AssumptionTracker pointer, and also a
DomTree pointer, and most of the changes are just to pass this new information
when it is easily available from InstSimplify, InstCombine, etc.
As explained below, the significant conceptual change is that known properties
of a value might depend on the control-flow location of the use (because we
care that the @llvm.assume dominates the use because assumptions have
control-flow dependencies). This means that, when we ask if bits are known in a
value, we might get different answers for different uses.
The significant changes are all in ValueTracking. Two main changes: First, as
with the rest of the code, new parameters need to be passed around. To make
this easier, I grouped them into a structure, and I made internal static
versions of the relevant functions that take this structure as a parameter. The
new code does as you might expect, it looks for @llvm.assume calls that make
use of the value we're trying to learn something about (often indirectly),
attempts to pattern match that expression, and uses the result if successful.
By making use of the AssumptionTracker, the process of finding @llvm.assume
calls is not expensive.
Part of the structure being passed around inside ValueTracking is a set of
already-considered @llvm.assume calls. This is to prevent a query using, for
example, the assume(a == b), to recurse on itself. The context and DT params
are used to find applicable assumptions. An assumption needs to dominate the
context instruction, or come after it deterministically. In this latter case we
only handle the specific case where both the assumption and the context
instruction are in the same block, and we need to exclude assumptions from
being used to simplify their own ephemeral values (those which contribute only
to the assumption) because otherwise the assumption would prove its feeding
comparison trivial and would be removed.
This commit adds the plumbing and the logic for a simple masked-bit propagation
(just enough to write a regression test). Future commits add more patterns
(and, correspondingly, more regression tests).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217342 91177308-0d34-0410-b5e6-96231b3b80d8
2014-09-07 18:57:58 +00:00
|
|
|
WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI, &I)) {
|
2010-01-05 07:18:46 +00:00
|
|
|
// Insert the new, smaller add.
|
2012-12-12 20:57:53 +00:00
|
|
|
Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0),
|
2010-01-05 07:18:46 +00:00
|
|
|
CI, "addconv");
|
|
|
|
return new SExtInst(NewAdd, I.getType());
|
|
|
|
}
|
|
|
|
}
|
2012-12-12 20:57:53 +00:00
|
|
|
|
2010-01-05 07:18:46 +00:00
|
|
|
// (add (sext x), (sext y)) --> (sext (add int x, y))
|
|
|
|
if (SExtInst *RHSConv = dyn_cast<SExtInst>(RHS)) {
|
|
|
|
// Only do this if x/y have the same type, if at last one of them has a
|
|
|
|
// single use (so we don't increase the number of sexts), and if the
|
|
|
|
// integer add will not overflow.
|
|
|
|
if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&&
|
|
|
|
(LHSConv->hasOneUse() || RHSConv->hasOneUse()) &&
|
|
|
|
WillNotOverflowSignedAdd(LHSConv->getOperand(0),
|
Make use of @llvm.assume in ValueTracking (computeKnownBits, etc.)
This change, which allows @llvm.assume to be used from within computeKnownBits
(and other associated functions in ValueTracking), adds some (optional)
parameters to computeKnownBits and friends. These functions now (optionally)
take a "context" instruction pointer, an AssumptionTracker pointer, and also a
DomTree pointer, and most of the changes are just to pass this new information
when it is easily available from InstSimplify, InstCombine, etc.
As explained below, the significant conceptual change is that known properties
of a value might depend on the control-flow location of the use (because we
care that the @llvm.assume dominates the use because assumptions have
control-flow dependencies). This means that, when we ask if bits are known in a
value, we might get different answers for different uses.
The significant changes are all in ValueTracking. Two main changes: First, as
with the rest of the code, new parameters need to be passed around. To make
this easier, I grouped them into a structure, and I made internal static
versions of the relevant functions that take this structure as a parameter. The
new code does as you might expect, it looks for @llvm.assume calls that make
use of the value we're trying to learn something about (often indirectly),
attempts to pattern match that expression, and uses the result if successful.
By making use of the AssumptionTracker, the process of finding @llvm.assume
calls is not expensive.
Part of the structure being passed around inside ValueTracking is a set of
already-considered @llvm.assume calls. This is to prevent a query using, for
example, the assume(a == b), to recurse on itself. The context and DT params
are used to find applicable assumptions. An assumption needs to dominate the
context instruction, or come after it deterministically. In this latter case we
only handle the specific case where both the assumption and the context
instruction are in the same block, and we need to exclude assumptions from
being used to simplify their own ephemeral values (those which contribute only
to the assumption) because otherwise the assumption would prove its feeding
comparison trivial and would be removed.
This commit adds the plumbing and the logic for a simple masked-bit propagation
(just enough to write a regression test). Future commits add more patterns
(and, correspondingly, more regression tests).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217342 91177308-0d34-0410-b5e6-96231b3b80d8
2014-09-07 18:57:58 +00:00
|
|
|
RHSConv->getOperand(0), &I)) {
|
2010-01-05 07:18:46 +00:00
|
|
|
// Insert the new integer add.
|
2012-12-12 20:57:53 +00:00
|
|
|
Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0),
|
2010-01-05 20:56:24 +00:00
|
|
|
RHSConv->getOperand(0), "addconv");
|
2010-01-05 07:18:46 +00:00
|
|
|
return new SExtInst(NewAdd, I.getType());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-11 22:32:02 +00:00
|
|
|
// (add (xor A, B) (and A, B)) --> (or A, B)
|
2012-04-26 23:29:14 +00:00
|
|
|
{
|
2014-04-25 05:29:35 +00:00
|
|
|
Value *A = nullptr, *B = nullptr;
|
2012-04-26 23:29:14 +00:00
|
|
|
if (match(RHS, m_Xor(m_Value(A), m_Value(B))) &&
|
|
|
|
(match(LHS, m_And(m_Specific(A), m_Specific(B))) ||
|
|
|
|
match(LHS, m_And(m_Specific(B), m_Specific(A)))))
|
|
|
|
return BinaryOperator::CreateOr(A, B);
|
|
|
|
|
|
|
|
if (match(LHS, m_Xor(m_Value(A), m_Value(B))) &&
|
|
|
|
(match(RHS, m_And(m_Specific(A), m_Specific(B))) ||
|
|
|
|
match(RHS, m_And(m_Specific(B), m_Specific(A)))))
|
|
|
|
return BinaryOperator::CreateOr(A, B);
|
|
|
|
}
|
|
|
|
|
2014-08-11 22:32:02 +00:00
|
|
|
// (add (or A, B) (and A, B)) --> (add A, B)
|
|
|
|
{
|
|
|
|
Value *A = nullptr, *B = nullptr;
|
|
|
|
if (match(RHS, m_Or(m_Value(A), m_Value(B))) &&
|
|
|
|
(match(LHS, m_And(m_Specific(A), m_Specific(B))) ||
|
|
|
|
match(LHS, m_And(m_Specific(B), m_Specific(A))))) {
|
|
|
|
auto *New = BinaryOperator::CreateAdd(A, B);
|
|
|
|
New->setHasNoSignedWrap(I.hasNoSignedWrap());
|
|
|
|
New->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
|
|
|
|
return New;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match(LHS, m_Or(m_Value(A), m_Value(B))) &&
|
|
|
|
(match(RHS, m_And(m_Specific(A), m_Specific(B))) ||
|
|
|
|
match(RHS, m_And(m_Specific(B), m_Specific(A))))) {
|
|
|
|
auto *New = BinaryOperator::CreateAdd(A, B);
|
|
|
|
New->setHasNoSignedWrap(I.hasNoSignedWrap());
|
|
|
|
New->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
|
|
|
|
return New;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-17 00:42:07 +00:00
|
|
|
// TODO(jingyue): Consider WillNotOverflowSignedAdd and
|
|
|
|
// WillNotOverflowUnsignedAdd to reduce the number of invocations of
|
|
|
|
// computeKnownBits.
|
Make use of @llvm.assume in ValueTracking (computeKnownBits, etc.)
This change, which allows @llvm.assume to be used from within computeKnownBits
(and other associated functions in ValueTracking), adds some (optional)
parameters to computeKnownBits and friends. These functions now (optionally)
take a "context" instruction pointer, an AssumptionTracker pointer, and also a
DomTree pointer, and most of the changes are just to pass this new information
when it is easily available from InstSimplify, InstCombine, etc.
As explained below, the significant conceptual change is that known properties
of a value might depend on the control-flow location of the use (because we
care that the @llvm.assume dominates the use because assumptions have
control-flow dependencies). This means that, when we ask if bits are known in a
value, we might get different answers for different uses.
The significant changes are all in ValueTracking. Two main changes: First, as
with the rest of the code, new parameters need to be passed around. To make
this easier, I grouped them into a structure, and I made internal static
versions of the relevant functions that take this structure as a parameter. The
new code does as you might expect, it looks for @llvm.assume calls that make
use of the value we're trying to learn something about (often indirectly),
attempts to pattern match that expression, and uses the result if successful.
By making use of the AssumptionTracker, the process of finding @llvm.assume
calls is not expensive.
Part of the structure being passed around inside ValueTracking is a set of
already-considered @llvm.assume calls. This is to prevent a query using, for
example, the assume(a == b), to recurse on itself. The context and DT params
are used to find applicable assumptions. An assumption needs to dominate the
context instruction, or come after it deterministically. In this latter case we
only handle the specific case where both the assumption and the context
instruction are in the same block, and we need to exclude assumptions from
being used to simplify their own ephemeral values (those which contribute only
to the assumption) because otherwise the assumption would prove its feeding
comparison trivial and would be removed.
This commit adds the plumbing and the logic for a simple masked-bit propagation
(just enough to write a regression test). Future commits add more patterns
(and, correspondingly, more regression tests).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217342 91177308-0d34-0410-b5e6-96231b3b80d8
2014-09-07 18:57:58 +00:00
|
|
|
if (!I.hasNoSignedWrap() && WillNotOverflowSignedAdd(LHS, RHS, &I)) {
|
2014-06-02 22:01:04 +00:00
|
|
|
Changed = true;
|
|
|
|
I.setHasNoSignedWrap(true);
|
|
|
|
}
|
2015-01-07 00:39:50 +00:00
|
|
|
if (!I.hasNoUnsignedWrap() &&
|
|
|
|
computeOverflowForUnsignedAdd(LHS, RHS, &I) ==
|
|
|
|
OverflowResult::NeverOverflows) {
|
2014-06-17 00:42:07 +00:00
|
|
|
Changed = true;
|
|
|
|
I.setHasNoUnsignedWrap(true);
|
|
|
|
}
|
2014-06-02 22:01:04 +00:00
|
|
|
|
2014-04-25 05:29:35 +00:00
|
|
|
return Changed ? &I : nullptr;
|
2010-01-05 07:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InstCombiner::visitFAdd(BinaryOperator &I) {
|
2010-11-13 15:10:37 +00:00
|
|
|
bool Changed = SimplifyAssociativeOrCommutative(I);
|
2010-01-05 07:18:46 +00:00
|
|
|
Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
|
|
|
|
|
2014-05-11 08:46:12 +00:00
|
|
|
if (Value *V = SimplifyVectorOp(I))
|
|
|
|
return ReplaceInstUsesWith(I, V);
|
|
|
|
|
2015-01-04 12:03:27 +00:00
|
|
|
if (Value *V =
|
|
|
|
SimplifyFAddInst(LHS, RHS, I.getFastMathFlags(), DL, TLI, DT, AC))
|
2012-12-12 00:28:32 +00:00
|
|
|
return ReplaceInstUsesWith(I, V);
|
2010-01-05 07:18:46 +00:00
|
|
|
|
2013-07-20 07:13:13 +00:00
|
|
|
if (isa<Constant>(RHS)) {
|
|
|
|
if (isa<PHINode>(LHS))
|
|
|
|
if (Instruction *NV = FoldOpIntoPhi(I))
|
|
|
|
return NV;
|
|
|
|
|
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(LHS))
|
|
|
|
if (Instruction *NV = FoldOpIntoSelect(I, SI))
|
|
|
|
return NV;
|
|
|
|
}
|
2012-12-14 22:08:26 +00:00
|
|
|
|
2010-01-05 07:18:46 +00:00
|
|
|
// -A + B --> B - A
|
|
|
|
// -A + -B --> -(A + B)
|
2014-01-16 21:26:02 +00:00
|
|
|
if (Value *LHSV = dyn_castFNegVal(LHS)) {
|
|
|
|
Instruction *RI = BinaryOperator::CreateFSub(RHS, LHSV);
|
|
|
|
RI->copyFastMathFlags(&I);
|
|
|
|
return RI;
|
|
|
|
}
|
2010-01-05 07:18:46 +00:00
|
|
|
|
|
|
|
// A + -B --> A - B
|
|
|
|
if (!isa<Constant>(RHS))
|
2014-01-16 21:26:02 +00:00
|
|
|
if (Value *V = dyn_castFNegVal(RHS)) {
|
|
|
|
Instruction *RI = BinaryOperator::CreateFSub(LHS, V);
|
|
|
|
RI->copyFastMathFlags(&I);
|
|
|
|
return RI;
|
|
|
|
}
|
2010-01-05 07:18:46 +00:00
|
|
|
|
2010-03-02 01:11:08 +00:00
|
|
|
// Check for (fadd double (sitofp x), y), see if we can merge this into an
|
2010-01-05 07:18:46 +00:00
|
|
|
// integer add followed by a promotion.
|
|
|
|
if (SIToFPInst *LHSConv = dyn_cast<SIToFPInst>(LHS)) {
|
2010-03-02 01:11:08 +00:00
|
|
|
// (fadd double (sitofp x), fpcst) --> (sitofp (add int x, intcst))
|
2010-01-05 07:18:46 +00:00
|
|
|
// ... if the constant fits in the integer value. This is useful for things
|
|
|
|
// like (double)(x & 1234) + 4.0 -> (double)((X & 1234)+4) which no longer
|
|
|
|
// requires a constant pool load, and generally allows the add to be better
|
|
|
|
// instcombined.
|
|
|
|
if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) {
|
2012-12-12 20:57:53 +00:00
|
|
|
Constant *CI =
|
2010-01-05 07:18:46 +00:00
|
|
|
ConstantExpr::getFPToSI(CFP, LHSConv->getOperand(0)->getType());
|
|
|
|
if (LHSConv->hasOneUse() &&
|
|
|
|
ConstantExpr::getSIToFP(CI, I.getType()) == CFP &&
|
Make use of @llvm.assume in ValueTracking (computeKnownBits, etc.)
This change, which allows @llvm.assume to be used from within computeKnownBits
(and other associated functions in ValueTracking), adds some (optional)
parameters to computeKnownBits and friends. These functions now (optionally)
take a "context" instruction pointer, an AssumptionTracker pointer, and also a
DomTree pointer, and most of the changes are just to pass this new information
when it is easily available from InstSimplify, InstCombine, etc.
As explained below, the significant conceptual change is that known properties
of a value might depend on the control-flow location of the use (because we
care that the @llvm.assume dominates the use because assumptions have
control-flow dependencies). This means that, when we ask if bits are known in a
value, we might get different answers for different uses.
The significant changes are all in ValueTracking. Two main changes: First, as
with the rest of the code, new parameters need to be passed around. To make
this easier, I grouped them into a structure, and I made internal static
versions of the relevant functions that take this structure as a parameter. The
new code does as you might expect, it looks for @llvm.assume calls that make
use of the value we're trying to learn something about (often indirectly),
attempts to pattern match that expression, and uses the result if successful.
By making use of the AssumptionTracker, the process of finding @llvm.assume
calls is not expensive.
Part of the structure being passed around inside ValueTracking is a set of
already-considered @llvm.assume calls. This is to prevent a query using, for
example, the assume(a == b), to recurse on itself. The context and DT params
are used to find applicable assumptions. An assumption needs to dominate the
context instruction, or come after it deterministically. In this latter case we
only handle the specific case where both the assumption and the context
instruction are in the same block, and we need to exclude assumptions from
being used to simplify their own ephemeral values (those which contribute only
to the assumption) because otherwise the assumption would prove its feeding
comparison trivial and would be removed.
This commit adds the plumbing and the logic for a simple masked-bit propagation
(just enough to write a regression test). Future commits add more patterns
(and, correspondingly, more regression tests).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217342 91177308-0d34-0410-b5e6-96231b3b80d8
2014-09-07 18:57:58 +00:00
|
|
|
WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI, &I)) {
|
2010-01-05 07:18:46 +00:00
|
|
|
// Insert the new integer add.
|
|
|
|
Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0),
|
|
|
|
CI, "addconv");
|
|
|
|
return new SIToFPInst(NewAdd, I.getType());
|
|
|
|
}
|
|
|
|
}
|
2012-12-12 20:57:53 +00:00
|
|
|
|
2010-03-02 01:11:08 +00:00
|
|
|
// (fadd double (sitofp x), (sitofp y)) --> (sitofp (add int x, y))
|
2010-01-05 07:18:46 +00:00
|
|
|
if (SIToFPInst *RHSConv = dyn_cast<SIToFPInst>(RHS)) {
|
|
|
|
// Only do this if x/y have the same type, if at last one of them has a
|
|
|
|
// single use (so we don't increase the number of int->fp conversions),
|
|
|
|
// and if the integer add will not overflow.
|
|
|
|
if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&&
|
|
|
|
(LHSConv->hasOneUse() || RHSConv->hasOneUse()) &&
|
|
|
|
WillNotOverflowSignedAdd(LHSConv->getOperand(0),
|
Make use of @llvm.assume in ValueTracking (computeKnownBits, etc.)
This change, which allows @llvm.assume to be used from within computeKnownBits
(and other associated functions in ValueTracking), adds some (optional)
parameters to computeKnownBits and friends. These functions now (optionally)
take a "context" instruction pointer, an AssumptionTracker pointer, and also a
DomTree pointer, and most of the changes are just to pass this new information
when it is easily available from InstSimplify, InstCombine, etc.
As explained below, the significant conceptual change is that known properties
of a value might depend on the control-flow location of the use (because we
care that the @llvm.assume dominates the use because assumptions have
control-flow dependencies). This means that, when we ask if bits are known in a
value, we might get different answers for different uses.
The significant changes are all in ValueTracking. Two main changes: First, as
with the rest of the code, new parameters need to be passed around. To make
this easier, I grouped them into a structure, and I made internal static
versions of the relevant functions that take this structure as a parameter. The
new code does as you might expect, it looks for @llvm.assume calls that make
use of the value we're trying to learn something about (often indirectly),
attempts to pattern match that expression, and uses the result if successful.
By making use of the AssumptionTracker, the process of finding @llvm.assume
calls is not expensive.
Part of the structure being passed around inside ValueTracking is a set of
already-considered @llvm.assume calls. This is to prevent a query using, for
example, the assume(a == b), to recurse on itself. The context and DT params
are used to find applicable assumptions. An assumption needs to dominate the
context instruction, or come after it deterministically. In this latter case we
only handle the specific case where both the assumption and the context
instruction are in the same block, and we need to exclude assumptions from
being used to simplify their own ephemeral values (those which contribute only
to the assumption) because otherwise the assumption would prove its feeding
comparison trivial and would be removed.
This commit adds the plumbing and the logic for a simple masked-bit propagation
(just enough to write a regression test). Future commits add more patterns
(and, correspondingly, more regression tests).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217342 91177308-0d34-0410-b5e6-96231b3b80d8
2014-09-07 18:57:58 +00:00
|
|
|
RHSConv->getOperand(0), &I)) {
|
2010-01-05 07:18:46 +00:00
|
|
|
// Insert the new integer add.
|
2012-12-12 20:57:53 +00:00
|
|
|
Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0),
|
2010-01-05 07:18:46 +00:00
|
|
|
RHSConv->getOperand(0),"addconv");
|
|
|
|
return new SIToFPInst(NewAdd, I.getType());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-12 20:57:53 +00:00
|
|
|
|
Provide InstCombines for the following 3 cases:
A * (1 - (uitofp i1 C)) -> select C, 0, A
B * (uitofp i1 C) -> select C, B, 0
select C, 0, A + select C, B, 0 -> select C, B, A
These come up in code that has been hand-optimized from a select to a linear blend,
on platforms where that may have mattered. We want to undo such changes
with the following transform:
A*(1 - uitofp i1 C) + B*(uitofp i1 C) -> select C, A, B
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@181216 91177308-0d34-0410-b5e6-96231b3b80d8
2013-05-06 16:55:50 +00:00
|
|
|
// select C, 0, B + select C, A, 0 -> select C, A, B
|
|
|
|
{
|
|
|
|
Value *A1, *B1, *C1, *A2, *B2, *C2;
|
|
|
|
if (match(LHS, m_Select(m_Value(C1), m_Value(A1), m_Value(B1))) &&
|
|
|
|
match(RHS, m_Select(m_Value(C2), m_Value(A2), m_Value(B2)))) {
|
|
|
|
if (C1 == C2) {
|
2014-04-25 05:29:35 +00:00
|
|
|
Constant *Z1=nullptr, *Z2=nullptr;
|
Provide InstCombines for the following 3 cases:
A * (1 - (uitofp i1 C)) -> select C, 0, A
B * (uitofp i1 C) -> select C, B, 0
select C, 0, A + select C, B, 0 -> select C, B, A
These come up in code that has been hand-optimized from a select to a linear blend,
on platforms where that may have mattered. We want to undo such changes
with the following transform:
A*(1 - uitofp i1 C) + B*(uitofp i1 C) -> select C, A, B
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@181216 91177308-0d34-0410-b5e6-96231b3b80d8
2013-05-06 16:55:50 +00:00
|
|
|
Value *A, *B, *C=C1;
|
|
|
|
if (match(A1, m_AnyZero()) && match(B2, m_AnyZero())) {
|
|
|
|
Z1 = dyn_cast<Constant>(A1); A = A2;
|
|
|
|
Z2 = dyn_cast<Constant>(B2); B = B1;
|
|
|
|
} else if (match(B1, m_AnyZero()) && match(A2, m_AnyZero())) {
|
|
|
|
Z1 = dyn_cast<Constant>(B1); B = B2;
|
2014-11-03 05:53:55 +00:00
|
|
|
Z2 = dyn_cast<Constant>(A2); A = A1;
|
Provide InstCombines for the following 3 cases:
A * (1 - (uitofp i1 C)) -> select C, 0, A
B * (uitofp i1 C) -> select C, B, 0
select C, 0, A + select C, B, 0 -> select C, B, A
These come up in code that has been hand-optimized from a select to a linear blend,
on platforms where that may have mattered. We want to undo such changes
with the following transform:
A*(1 - uitofp i1 C) + B*(uitofp i1 C) -> select C, A, B
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@181216 91177308-0d34-0410-b5e6-96231b3b80d8
2013-05-06 16:55:50 +00:00
|
|
|
}
|
2014-11-03 05:53:55 +00:00
|
|
|
|
|
|
|
if (Z1 && Z2 &&
|
|
|
|
(I.hasNoSignedZeros() ||
|
Provide InstCombines for the following 3 cases:
A * (1 - (uitofp i1 C)) -> select C, 0, A
B * (uitofp i1 C) -> select C, B, 0
select C, 0, A + select C, B, 0 -> select C, B, A
These come up in code that has been hand-optimized from a select to a linear blend,
on platforms where that may have mattered. We want to undo such changes
with the following transform:
A*(1 - uitofp i1 C) + B*(uitofp i1 C) -> select C, A, B
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@181216 91177308-0d34-0410-b5e6-96231b3b80d8
2013-05-06 16:55:50 +00:00
|
|
|
(Z1->isNegativeZeroValue() && Z2->isNegativeZeroValue()))) {
|
|
|
|
return SelectInst::Create(C, A, B);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
if (I.hasUnsafeAlgebra()) {
|
|
|
|
if (Value *V = FAddCombine(Builder).simplify(&I))
|
|
|
|
return ReplaceInstUsesWith(I, V);
|
|
|
|
}
|
|
|
|
|
2014-04-25 05:29:35 +00:00
|
|
|
return Changed ? &I : nullptr;
|
2010-01-05 07:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Optimize pointer differences into the same array into a size. Consider:
|
|
|
|
/// &A[10] - &A[0]: we should compile this to "10". LHS/RHS are the pointer
|
|
|
|
/// operands to the ptrtoint instructions for the LHS/RHS of the subtract.
|
|
|
|
///
|
|
|
|
Value *InstCombiner::OptimizePointerDifference(Value *LHS, Value *RHS,
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *Ty) {
|
2014-02-21 00:06:31 +00:00
|
|
|
assert(DL && "Must have target data info for this");
|
2012-12-12 20:57:53 +00:00
|
|
|
|
2010-01-05 07:18:46 +00:00
|
|
|
// If LHS is a gep based on RHS or RHS is a gep based on LHS, we can optimize
|
|
|
|
// this.
|
|
|
|
bool Swapped = false;
|
2014-04-25 05:29:35 +00:00
|
|
|
GEPOperator *GEP1 = nullptr, *GEP2 = nullptr;
|
2012-02-20 14:34:57 +00:00
|
|
|
|
2010-01-05 07:18:46 +00:00
|
|
|
// For now we require one side to be the base pointer "A" or a constant
|
2012-02-20 14:34:57 +00:00
|
|
|
// GEP derived from it.
|
|
|
|
if (GEPOperator *LHSGEP = dyn_cast<GEPOperator>(LHS)) {
|
2010-01-05 07:18:46 +00:00
|
|
|
// (gep X, ...) - X
|
|
|
|
if (LHSGEP->getOperand(0) == RHS) {
|
2012-02-20 14:34:57 +00:00
|
|
|
GEP1 = LHSGEP;
|
2010-01-05 07:18:46 +00:00
|
|
|
Swapped = false;
|
2012-02-20 14:34:57 +00:00
|
|
|
} else if (GEPOperator *RHSGEP = dyn_cast<GEPOperator>(RHS)) {
|
|
|
|
// (gep X, ...) - (gep X, ...)
|
|
|
|
if (LHSGEP->getOperand(0)->stripPointerCasts() ==
|
|
|
|
RHSGEP->getOperand(0)->stripPointerCasts()) {
|
|
|
|
GEP2 = RHSGEP;
|
|
|
|
GEP1 = LHSGEP;
|
2010-01-05 07:18:46 +00:00
|
|
|
Swapped = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-12 20:57:53 +00:00
|
|
|
|
2012-02-20 14:34:57 +00:00
|
|
|
if (GEPOperator *RHSGEP = dyn_cast<GEPOperator>(RHS)) {
|
2010-01-05 07:18:46 +00:00
|
|
|
// X - (gep X, ...)
|
|
|
|
if (RHSGEP->getOperand(0) == LHS) {
|
2012-02-20 14:34:57 +00:00
|
|
|
GEP1 = RHSGEP;
|
2010-01-05 07:18:46 +00:00
|
|
|
Swapped = true;
|
2012-02-20 14:34:57 +00:00
|
|
|
} else if (GEPOperator *LHSGEP = dyn_cast<GEPOperator>(LHS)) {
|
|
|
|
// (gep X, ...) - (gep X, ...)
|
|
|
|
if (RHSGEP->getOperand(0)->stripPointerCasts() ==
|
|
|
|
LHSGEP->getOperand(0)->stripPointerCasts()) {
|
|
|
|
GEP2 = LHSGEP;
|
|
|
|
GEP1 = RHSGEP;
|
2010-01-05 07:18:46 +00:00
|
|
|
Swapped = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-12 20:57:53 +00:00
|
|
|
|
2012-02-20 14:34:57 +00:00
|
|
|
// Avoid duplicating the arithmetic if GEP2 has non-constant indices and
|
|
|
|
// multiple users.
|
2014-04-25 05:29:35 +00:00
|
|
|
if (!GEP1 ||
|
|
|
|
(GEP2 && !GEP2->hasAllConstantIndices() && !GEP2->hasOneUse()))
|
|
|
|
return nullptr;
|
2012-12-12 20:57:53 +00:00
|
|
|
|
2010-01-05 07:18:46 +00:00
|
|
|
// Emit the offset of the GEP and an intptr_t.
|
2012-02-20 14:34:57 +00:00
|
|
|
Value *Result = EmitGEPOffset(GEP1);
|
2012-12-12 20:57:53 +00:00
|
|
|
|
2010-01-05 07:18:46 +00:00
|
|
|
// If we had a constant expression GEP on the other side offsetting the
|
|
|
|
// pointer, subtract it from the offset we have.
|
2012-02-20 14:34:57 +00:00
|
|
|
if (GEP2) {
|
|
|
|
Value *Offset = EmitGEPOffset(GEP2);
|
|
|
|
Result = Builder->CreateSub(Result, Offset);
|
2010-01-05 07:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we have p - gep(p, ...) then we have to negate the result.
|
|
|
|
if (Swapped)
|
|
|
|
Result = Builder->CreateNeg(Result, "diff.neg");
|
|
|
|
|
|
|
|
return Builder->CreateIntCast(Result, Ty, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InstCombiner::visitSub(BinaryOperator &I) {
|
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
|
|
|
|
2014-05-11 08:46:12 +00:00
|
|
|
if (Value *V = SimplifyVectorOp(I))
|
|
|
|
return ReplaceInstUsesWith(I, V);
|
|
|
|
|
2010-12-15 14:07:39 +00:00
|
|
|
if (Value *V = SimplifySubInst(Op0, Op1, I.hasNoSignedWrap(),
|
2015-01-04 12:03:27 +00:00
|
|
|
I.hasNoUnsignedWrap(), DL, TLI, DT, AC))
|
2010-12-15 14:07:39 +00:00
|
|
|
return ReplaceInstUsesWith(I, V);
|
2010-01-05 07:18:46 +00:00
|
|
|
|
2010-12-22 13:36:08 +00:00
|
|
|
// (A*B)-(A*C) -> A*(B-C) etc
|
|
|
|
if (Value *V = SimplifyUsingDistributiveLaws(I))
|
|
|
|
return ReplaceInstUsesWith(I, V);
|
|
|
|
|
2014-07-31 04:49:29 +00:00
|
|
|
// If this is a 'B = x-(-A)', change to B = x+A.
|
2010-01-05 07:18:46 +00:00
|
|
|
if (Value *V = dyn_castNegVal(Op1)) {
|
|
|
|
BinaryOperator *Res = BinaryOperator::CreateAdd(Op0, V);
|
2014-07-31 04:49:29 +00:00
|
|
|
|
|
|
|
if (const auto *BO = dyn_cast<BinaryOperator>(Op1)) {
|
|
|
|
assert(BO->getOpcode() == Instruction::Sub &&
|
|
|
|
"Expected a subtraction operator!");
|
|
|
|
if (BO->hasNoSignedWrap() && I.hasNoSignedWrap())
|
|
|
|
Res->setHasNoSignedWrap(true);
|
2014-08-22 16:41:23 +00:00
|
|
|
} else {
|
|
|
|
if (cast<Constant>(Op1)->isNotMinSignedValue() && I.hasNoSignedWrap())
|
|
|
|
Res->setHasNoSignedWrap(true);
|
2014-07-31 04:49:29 +00:00
|
|
|
}
|
|
|
|
|
2010-01-05 07:18:46 +00:00
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2010-02-15 16:12:20 +00:00
|
|
|
if (I.getType()->isIntegerTy(1))
|
2010-01-05 07:18:46 +00:00
|
|
|
return BinaryOperator::CreateXor(Op0, Op1);
|
2011-02-10 05:14:58 +00:00
|
|
|
|
|
|
|
// Replace (-1 - A) with (~A).
|
|
|
|
if (match(Op0, m_AllOnes()))
|
|
|
|
return BinaryOperator::CreateNot(Op1);
|
2012-12-12 20:57:53 +00:00
|
|
|
|
2014-01-19 15:24:22 +00:00
|
|
|
if (Constant *C = dyn_cast<Constant>(Op0)) {
|
2010-01-05 07:18:46 +00:00
|
|
|
// C - ~X == X + (1+C)
|
2014-04-25 05:29:35 +00:00
|
|
|
Value *X = nullptr;
|
2010-01-05 07:18:46 +00:00
|
|
|
if (match(Op1, m_Not(m_Value(X))))
|
|
|
|
return BinaryOperator::CreateAdd(X, AddOne(C));
|
|
|
|
|
|
|
|
// Try to fold constant sub into select arguments.
|
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
|
|
|
|
if (Instruction *R = FoldOpIntoSelect(I, SI))
|
|
|
|
return R;
|
|
|
|
|
2011-02-10 05:14:58 +00:00
|
|
|
// C-(X+C2) --> (C-C2)-X
|
2014-01-19 15:24:22 +00:00
|
|
|
Constant *C2;
|
|
|
|
if (match(Op1, m_Add(m_Value(X), m_Constant(C2))))
|
2011-02-10 05:14:58 +00:00
|
|
|
return BinaryOperator::CreateSub(ConstantExpr::getSub(C, C2), X);
|
2011-12-24 17:31:38 +00:00
|
|
|
|
|
|
|
if (SimplifyDemandedInstructionBits(I))
|
|
|
|
return &I;
|
2013-01-21 21:57:20 +00:00
|
|
|
|
|
|
|
// Fold (sub 0, (zext bool to B)) --> (sext bool to B)
|
2014-01-19 15:24:22 +00:00
|
|
|
if (C->isNullValue() && match(Op1, m_ZExt(m_Value(X))))
|
|
|
|
if (X->getType()->getScalarType()->isIntegerTy(1))
|
2013-01-21 21:57:20 +00:00
|
|
|
return CastInst::CreateSExtOrBitCast(X, Op1->getType());
|
|
|
|
|
|
|
|
// Fold (sub 0, (sext bool to B)) --> (zext bool to B)
|
2014-01-19 15:24:22 +00:00
|
|
|
if (C->isNullValue() && match(Op1, m_SExt(m_Value(X))))
|
|
|
|
if (X->getType()->getScalarType()->isIntegerTy(1))
|
2013-01-21 21:57:20 +00:00
|
|
|
return CastInst::CreateZExtOrBitCast(X, Op1->getType());
|
2010-01-05 07:18:46 +00:00
|
|
|
}
|
|
|
|
|
2014-01-19 15:24:22 +00:00
|
|
|
if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) {
|
|
|
|
// -(X >>u 31) -> (X >>s 31)
|
|
|
|
// -(X >>s 31) -> (X >>u 31)
|
|
|
|
if (C->isZero()) {
|
2014-11-03 05:53:55 +00:00
|
|
|
Value *X;
|
2014-10-08 08:37:49 +00:00
|
|
|
ConstantInt *CI;
|
2014-01-19 15:24:22 +00:00
|
|
|
if (match(Op1, m_LShr(m_Value(X), m_ConstantInt(CI))) &&
|
|
|
|
// Verify we are shifting out everything but the sign bit.
|
2014-10-08 08:37:49 +00:00
|
|
|
CI->getValue() == I.getType()->getPrimitiveSizeInBits() - 1)
|
2014-01-19 15:24:22 +00:00
|
|
|
return BinaryOperator::CreateAShr(X, CI);
|
|
|
|
|
|
|
|
if (match(Op1, m_AShr(m_Value(X), m_ConstantInt(CI))) &&
|
|
|
|
// Verify we are shifting out everything but the sign bit.
|
2014-10-08 08:37:49 +00:00
|
|
|
CI->getValue() == I.getType()->getPrimitiveSizeInBits() - 1)
|
2014-01-19 15:24:22 +00:00
|
|
|
return BinaryOperator::CreateLShr(X, CI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-12 20:57:53 +00:00
|
|
|
|
2014-11-03 05:53:55 +00:00
|
|
|
{
|
2014-10-08 08:37:49 +00:00
|
|
|
Value *Y;
|
2011-02-10 05:14:58 +00:00
|
|
|
// X-(X+Y) == -Y X-(Y+X) == -Y
|
|
|
|
if (match(Op1, m_Add(m_Specific(Op0), m_Value(Y))) ||
|
|
|
|
match(Op1, m_Add(m_Value(Y), m_Specific(Op0))))
|
|
|
|
return BinaryOperator::CreateNeg(Y);
|
2012-12-12 20:57:53 +00:00
|
|
|
|
2011-02-10 05:14:58 +00:00
|
|
|
// (X-Y)-X == -Y
|
|
|
|
if (match(Op0, m_Sub(m_Specific(Op1), m_Value(Y))))
|
|
|
|
return BinaryOperator::CreateNeg(Y);
|
|
|
|
}
|
2012-12-12 20:57:53 +00:00
|
|
|
|
2014-10-19 08:32:32 +00:00
|
|
|
// (sub (or A, B) (xor A, B)) --> (and A, B)
|
|
|
|
{
|
|
|
|
Value *A = nullptr, *B = nullptr;
|
|
|
|
if (match(Op1, m_Xor(m_Value(A), m_Value(B))) &&
|
|
|
|
(match(Op0, m_Or(m_Specific(A), m_Specific(B))) ||
|
|
|
|
match(Op0, m_Or(m_Specific(B), m_Specific(A)))))
|
|
|
|
return BinaryOperator::CreateAnd(A, B);
|
|
|
|
}
|
|
|
|
|
2014-11-03 05:53:55 +00:00
|
|
|
if (Op0->hasOneUse()) {
|
|
|
|
Value *Y = nullptr;
|
|
|
|
// ((X | Y) - X) --> (~X & Y)
|
|
|
|
if (match(Op0, m_Or(m_Value(Y), m_Specific(Op1))) ||
|
|
|
|
match(Op0, m_Or(m_Specific(Op1), m_Value(Y))))
|
|
|
|
return BinaryOperator::CreateAnd(
|
|
|
|
Y, Builder->CreateNot(Op1, Op1->getName() + ".not"));
|
|
|
|
}
|
|
|
|
|
2011-02-10 05:14:58 +00:00
|
|
|
if (Op1->hasOneUse()) {
|
2014-04-25 05:29:35 +00:00
|
|
|
Value *X = nullptr, *Y = nullptr, *Z = nullptr;
|
|
|
|
Constant *C = nullptr;
|
|
|
|
Constant *CI = nullptr;
|
2011-02-10 05:14:58 +00:00
|
|
|
|
|
|
|
// (X - (Y - Z)) --> (X + (Z - Y)).
|
|
|
|
if (match(Op1, m_Sub(m_Value(Y), m_Value(Z))))
|
|
|
|
return BinaryOperator::CreateAdd(Op0,
|
|
|
|
Builder->CreateSub(Z, Y, Op1->getName()));
|
|
|
|
|
|
|
|
// (X - (X & Y)) --> (X & ~Y)
|
|
|
|
//
|
|
|
|
if (match(Op1, m_And(m_Value(Y), m_Specific(Op0))) ||
|
|
|
|
match(Op1, m_And(m_Specific(Op0), m_Value(Y))))
|
|
|
|
return BinaryOperator::CreateAnd(Op0,
|
|
|
|
Builder->CreateNot(Y, Y->getName() + ".not"));
|
2012-12-12 20:57:53 +00:00
|
|
|
|
2014-07-02 06:07:09 +00:00
|
|
|
// 0 - (X sdiv C) -> (X sdiv -C) provided the negation doesn't overflow.
|
|
|
|
if (match(Op1, m_SDiv(m_Value(X), m_Constant(C))) && match(Op0, m_Zero()) &&
|
2014-08-22 16:41:23 +00:00
|
|
|
C->isNotMinSignedValue() && !C->isOneValue())
|
2011-02-10 05:14:58 +00:00
|
|
|
return BinaryOperator::CreateSDiv(X, ConstantExpr::getNeg(C));
|
|
|
|
|
|
|
|
// 0 - (X << Y) -> (-X << Y) when X is freely negatable.
|
|
|
|
if (match(Op1, m_Shl(m_Value(X), m_Value(Y))) && match(Op0, m_Zero()))
|
|
|
|
if (Value *XNeg = dyn_castNegVal(X))
|
|
|
|
return BinaryOperator::CreateShl(XNeg, Y);
|
|
|
|
|
|
|
|
// X - A*-B -> X + A*B
|
|
|
|
// X - -A*B -> X + A*B
|
|
|
|
Value *A, *B;
|
|
|
|
if (match(Op1, m_Mul(m_Value(A), m_Neg(m_Value(B)))) ||
|
|
|
|
match(Op1, m_Mul(m_Neg(m_Value(A)), m_Value(B))))
|
|
|
|
return BinaryOperator::CreateAdd(Op0, Builder->CreateMul(A, B));
|
2012-12-12 20:57:53 +00:00
|
|
|
|
2011-02-10 05:14:58 +00:00
|
|
|
// X - A*CI -> X + A*-CI
|
|
|
|
// X - CI*A -> X + A*-CI
|
2014-01-19 15:24:22 +00:00
|
|
|
if (match(Op1, m_Mul(m_Value(A), m_Constant(CI))) ||
|
|
|
|
match(Op1, m_Mul(m_Constant(CI), m_Value(A)))) {
|
2011-02-10 05:14:58 +00:00
|
|
|
Value *NewMul = Builder->CreateMul(A, ConstantExpr::getNeg(CI));
|
|
|
|
return BinaryOperator::CreateAdd(Op0, NewMul);
|
2010-01-05 07:18:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optimize pointer differences into the same array into a size. Consider:
|
|
|
|
// &A[10] - &A[0]: we should compile this to "10".
|
2014-02-21 00:06:31 +00:00
|
|
|
if (DL) {
|
2010-01-05 07:18:46 +00:00
|
|
|
Value *LHSOp, *RHSOp;
|
|
|
|
if (match(Op0, m_PtrToInt(m_Value(LHSOp))) &&
|
|
|
|
match(Op1, m_PtrToInt(m_Value(RHSOp))))
|
|
|
|
if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType()))
|
|
|
|
return ReplaceInstUsesWith(I, Res);
|
2012-12-12 20:57:53 +00:00
|
|
|
|
2010-01-05 07:18:46 +00:00
|
|
|
// trunc(p)-trunc(q) -> trunc(p-q)
|
|
|
|
if (match(Op0, m_Trunc(m_PtrToInt(m_Value(LHSOp)))) &&
|
|
|
|
match(Op1, m_Trunc(m_PtrToInt(m_Value(RHSOp)))))
|
|
|
|
if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType()))
|
|
|
|
return ReplaceInstUsesWith(I, Res);
|
2014-06-27 07:47:35 +00:00
|
|
|
}
|
2012-12-12 20:57:53 +00:00
|
|
|
|
2014-08-19 23:36:30 +00:00
|
|
|
bool Changed = false;
|
Make use of @llvm.assume in ValueTracking (computeKnownBits, etc.)
This change, which allows @llvm.assume to be used from within computeKnownBits
(and other associated functions in ValueTracking), adds some (optional)
parameters to computeKnownBits and friends. These functions now (optionally)
take a "context" instruction pointer, an AssumptionTracker pointer, and also a
DomTree pointer, and most of the changes are just to pass this new information
when it is easily available from InstSimplify, InstCombine, etc.
As explained below, the significant conceptual change is that known properties
of a value might depend on the control-flow location of the use (because we
care that the @llvm.assume dominates the use because assumptions have
control-flow dependencies). This means that, when we ask if bits are known in a
value, we might get different answers for different uses.
The significant changes are all in ValueTracking. Two main changes: First, as
with the rest of the code, new parameters need to be passed around. To make
this easier, I grouped them into a structure, and I made internal static
versions of the relevant functions that take this structure as a parameter. The
new code does as you might expect, it looks for @llvm.assume calls that make
use of the value we're trying to learn something about (often indirectly),
attempts to pattern match that expression, and uses the result if successful.
By making use of the AssumptionTracker, the process of finding @llvm.assume
calls is not expensive.
Part of the structure being passed around inside ValueTracking is a set of
already-considered @llvm.assume calls. This is to prevent a query using, for
example, the assume(a == b), to recurse on itself. The context and DT params
are used to find applicable assumptions. An assumption needs to dominate the
context instruction, or come after it deterministically. In this latter case we
only handle the specific case where both the assumption and the context
instruction are in the same block, and we need to exclude assumptions from
being used to simplify their own ephemeral values (those which contribute only
to the assumption) because otherwise the assumption would prove its feeding
comparison trivial and would be removed.
This commit adds the plumbing and the logic for a simple masked-bit propagation
(just enough to write a regression test). Future commits add more patterns
(and, correspondingly, more regression tests).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217342 91177308-0d34-0410-b5e6-96231b3b80d8
2014-09-07 18:57:58 +00:00
|
|
|
if (!I.hasNoSignedWrap() && WillNotOverflowSignedSub(Op0, Op1, &I)) {
|
2014-08-19 23:36:30 +00:00
|
|
|
Changed = true;
|
|
|
|
I.setHasNoSignedWrap(true);
|
|
|
|
}
|
Make use of @llvm.assume in ValueTracking (computeKnownBits, etc.)
This change, which allows @llvm.assume to be used from within computeKnownBits
(and other associated functions in ValueTracking), adds some (optional)
parameters to computeKnownBits and friends. These functions now (optionally)
take a "context" instruction pointer, an AssumptionTracker pointer, and also a
DomTree pointer, and most of the changes are just to pass this new information
when it is easily available from InstSimplify, InstCombine, etc.
As explained below, the significant conceptual change is that known properties
of a value might depend on the control-flow location of the use (because we
care that the @llvm.assume dominates the use because assumptions have
control-flow dependencies). This means that, when we ask if bits are known in a
value, we might get different answers for different uses.
The significant changes are all in ValueTracking. Two main changes: First, as
with the rest of the code, new parameters need to be passed around. To make
this easier, I grouped them into a structure, and I made internal static
versions of the relevant functions that take this structure as a parameter. The
new code does as you might expect, it looks for @llvm.assume calls that make
use of the value we're trying to learn something about (often indirectly),
attempts to pattern match that expression, and uses the result if successful.
By making use of the AssumptionTracker, the process of finding @llvm.assume
calls is not expensive.
Part of the structure being passed around inside ValueTracking is a set of
already-considered @llvm.assume calls. This is to prevent a query using, for
example, the assume(a == b), to recurse on itself. The context and DT params
are used to find applicable assumptions. An assumption needs to dominate the
context instruction, or come after it deterministically. In this latter case we
only handle the specific case where both the assumption and the context
instruction are in the same block, and we need to exclude assumptions from
being used to simplify their own ephemeral values (those which contribute only
to the assumption) because otherwise the assumption would prove its feeding
comparison trivial and would be removed.
This commit adds the plumbing and the logic for a simple masked-bit propagation
(just enough to write a regression test). Future commits add more patterns
(and, correspondingly, more regression tests).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217342 91177308-0d34-0410-b5e6-96231b3b80d8
2014-09-07 18:57:58 +00:00
|
|
|
if (!I.hasNoUnsignedWrap() && WillNotOverflowUnsignedSub(Op0, Op1, &I)) {
|
2014-08-20 07:17:31 +00:00
|
|
|
Changed = true;
|
|
|
|
I.setHasNoUnsignedWrap(true);
|
|
|
|
}
|
2014-08-19 23:36:30 +00:00
|
|
|
|
|
|
|
return Changed ? &I : nullptr;
|
2010-01-05 07:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InstCombiner::visitFSub(BinaryOperator &I) {
|
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
|
|
|
|
2014-05-11 08:46:12 +00:00
|
|
|
if (Value *V = SimplifyVectorOp(I))
|
|
|
|
return ReplaceInstUsesWith(I, V);
|
|
|
|
|
2015-01-04 12:03:27 +00:00
|
|
|
if (Value *V =
|
|
|
|
SimplifyFSubInst(Op0, Op1, I.getFastMathFlags(), DL, TLI, DT, AC))
|
2012-12-12 00:28:32 +00:00
|
|
|
return ReplaceInstUsesWith(I, V);
|
|
|
|
|
2014-12-31 22:14:05 +00:00
|
|
|
// fsub nsz 0, X ==> fsub nsz -0.0, X
|
|
|
|
if (I.getFastMathFlags().noSignedZeros() && match(Op0, m_Zero())) {
|
|
|
|
// Subtraction from -0.0 is the canonical form of fneg.
|
|
|
|
Instruction *NewI = BinaryOperator::CreateFNeg(Op1);
|
|
|
|
NewI->copyFastMathFlags(&I);
|
|
|
|
return NewI;
|
|
|
|
}
|
|
|
|
|
2013-07-20 07:13:13 +00:00
|
|
|
if (isa<Constant>(Op0))
|
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
|
|
|
|
if (Instruction *NV = FoldOpIntoSelect(I, SI))
|
|
|
|
return NV;
|
|
|
|
|
2013-07-26 21:40:29 +00:00
|
|
|
// If this is a 'B = x-(-A)', change to B = x+A, potentially looking
|
|
|
|
// through FP extensions/truncations along the way.
|
2013-07-30 23:53:17 +00:00
|
|
|
if (Value *V = dyn_castFNegVal(Op1)) {
|
|
|
|
Instruction *NewI = BinaryOperator::CreateFAdd(Op0, V);
|
|
|
|
NewI->copyFastMathFlags(&I);
|
|
|
|
return NewI;
|
|
|
|
}
|
2013-07-26 21:40:29 +00:00
|
|
|
if (FPTruncInst *FPTI = dyn_cast<FPTruncInst>(Op1)) {
|
|
|
|
if (Value *V = dyn_castFNegVal(FPTI->getOperand(0))) {
|
|
|
|
Value *NewTrunc = Builder->CreateFPTrunc(V, I.getType());
|
2013-07-30 23:53:17 +00:00
|
|
|
Instruction *NewI = BinaryOperator::CreateFAdd(Op0, NewTrunc);
|
|
|
|
NewI->copyFastMathFlags(&I);
|
|
|
|
return NewI;
|
2013-07-26 21:40:29 +00:00
|
|
|
}
|
|
|
|
} else if (FPExtInst *FPEI = dyn_cast<FPExtInst>(Op1)) {
|
|
|
|
if (Value *V = dyn_castFNegVal(FPEI->getOperand(0))) {
|
2013-07-26 22:06:21 +00:00
|
|
|
Value *NewExt = Builder->CreateFPExt(V, I.getType());
|
2013-07-30 23:53:17 +00:00
|
|
|
Instruction *NewI = BinaryOperator::CreateFAdd(Op0, NewExt);
|
|
|
|
NewI->copyFastMathFlags(&I);
|
|
|
|
return NewI;
|
2013-07-26 21:40:29 +00:00
|
|
|
}
|
|
|
|
}
|
2010-01-05 07:18:46 +00:00
|
|
|
|
2012-12-18 23:10:12 +00:00
|
|
|
if (I.hasUnsafeAlgebra()) {
|
|
|
|
if (Value *V = FAddCombine(Builder).simplify(&I))
|
|
|
|
return ReplaceInstUsesWith(I, V);
|
|
|
|
}
|
|
|
|
|
2014-04-25 05:29:35 +00:00
|
|
|
return nullptr;
|
2010-01-05 07:18:46 +00:00
|
|
|
}
|