2015-11-05 06:32:32 +08:00
|
|
|
//===- InstCombineAddSub.cpp ------------------------------------*- C++ -*-===//
|
2010-01-05 15:18:46 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-01-05 15:18:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the visit functions for add, fadd, sub, and fsub.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-01-22 13:25:13 +08:00
|
|
|
#include "InstCombineInternal.h"
|
2017-10-25 05:24:53 +08:00
|
|
|
#include "llvm/ADT/APFloat.h"
|
|
|
|
#include "llvm/ADT/APInt.h"
|
2013-07-15 12:27:47 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2017-10-25 05:24:53 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2010-01-05 15:18:46 +08:00
|
|
|
#include "llvm/Analysis/InstructionSimplify.h"
|
2017-10-25 05:24:53 +08:00
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
|
|
|
#include "llvm/IR/Constant.h"
|
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/InstrTypes.h"
|
|
|
|
#include "llvm/IR/Instruction.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Operator.h"
|
2014-03-04 19:08:18 +08:00
|
|
|
#include "llvm/IR/PatternMatch.h"
|
2017-10-25 05:24:53 +08:00
|
|
|
#include "llvm/IR/Type.h"
|
|
|
|
#include "llvm/IR/Value.h"
|
|
|
|
#include "llvm/Support/AlignOf.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
2017-04-27 00:39:58 +08:00
|
|
|
#include "llvm/Support/KnownBits.h"
|
2017-10-25 05:24:53 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <utility>
|
2015-11-05 06:32:32 +08:00
|
|
|
|
2010-01-05 15:18:46 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace PatternMatch;
|
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "instcombine"
|
|
|
|
|
2012-12-19 07:10:12 +08: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-06 05:20:12 +08:00
|
|
|
/// constructor is merely 4-byte-store-zero (Assuming compiler is able to
|
2012-12-19 07:10:12 +08:00
|
|
|
/// perform write-merging).
|
2013-04-06 05:20:12 +08:00
|
|
|
///
|
2012-12-19 07:10:12 +08:00
|
|
|
class FAddendCoef {
|
|
|
|
public:
|
2014-07-17 14:09:34 +08:00
|
|
|
// The constructor has to initialize a APFloat, which is unnecessary for
|
2012-12-19 07:10:12 +08: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.
|
2017-10-25 05:24:53 +08:00
|
|
|
FAddendCoef() = default;
|
2012-12-19 07:10:12 +08:00
|
|
|
~FAddendCoef();
|
2013-04-06 05:20:12 +08:00
|
|
|
|
2017-10-25 05:24:53 +08: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 &S);
|
|
|
|
|
2012-12-19 07:10:12 +08:00
|
|
|
void set(short C) {
|
|
|
|
assert(!insaneIntVal(C) && "Insane coefficient");
|
|
|
|
IsFp = false; IntVal = C;
|
|
|
|
}
|
2013-04-06 05:20:12 +08:00
|
|
|
|
2012-12-19 07:10:12 +08:00
|
|
|
void set(const APFloat& C);
|
2013-03-26 04:43:41 +08:00
|
|
|
|
2012-12-19 07:10:12 +08:00
|
|
|
void negate();
|
2013-04-06 05:20:12 +08:00
|
|
|
|
2012-12-19 07:10:12 +08:00
|
|
|
bool isZero() const { return isInt() ? !IntVal : getFpVal().isZero(); }
|
|
|
|
Value *getValue(Type *) const;
|
2013-04-06 05:20:12 +08:00
|
|
|
|
2012-12-19 07:10:12 +08: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-06 05:20:12 +08:00
|
|
|
|
2012-12-19 07:10:12 +08:00
|
|
|
private:
|
|
|
|
bool insaneIntVal(int V) { return V > 4 || V < -4; }
|
2017-10-25 05:24:53 +08:00
|
|
|
|
2015-11-05 06:32:32 +08:00
|
|
|
APFloat *getFpValPtr()
|
2017-10-25 05:24:53 +08:00
|
|
|
{ return reinterpret_cast<APFloat *>(&FpValBuf.buffer[0]); }
|
|
|
|
|
2015-11-05 06:32:32 +08:00
|
|
|
const APFloat *getFpValPtr() const
|
2017-10-25 05:24:53 +08:00
|
|
|
{ return reinterpret_cast<const APFloat *>(&FpValBuf.buffer[0]); }
|
2012-12-19 07:10:12 +08:00
|
|
|
|
2015-11-05 06:32:32 +08:00
|
|
|
const APFloat &getFpVal() const {
|
2012-12-19 07:10:12 +08:00
|
|
|
assert(IsFp && BufHasFpVal && "Incorret state");
|
2013-01-15 05:04:40 +08:00
|
|
|
return *getFpValPtr();
|
2012-12-19 07:10:12 +08:00
|
|
|
}
|
|
|
|
|
2015-11-05 06:32:32 +08:00
|
|
|
APFloat &getFpVal() {
|
2013-04-06 05:20:12 +08:00
|
|
|
assert(IsFp && BufHasFpVal && "Incorret state");
|
|
|
|
return *getFpValPtr();
|
|
|
|
}
|
|
|
|
|
2012-12-19 07:10:12 +08:00
|
|
|
bool isInt() const { return !IsFp; }
|
|
|
|
|
2013-03-26 04:43:41 +08:00
|
|
|
// If the coefficient is represented by an integer, promote it to a
|
2013-04-06 05:20:12 +08:00
|
|
|
// floating point.
|
2013-03-26 04:43:41 +08: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-06 05:20:12 +08:00
|
|
|
// from an *SIGNED* integer.
|
2013-03-26 04:43:41 +08:00
|
|
|
APFloat createAPFloatFromInt(const fltSemantics &Sem, int Val);
|
2012-12-19 09:10:17 +08:00
|
|
|
|
2017-10-25 05:24:53 +08:00
|
|
|
bool IsFp = false;
|
2013-04-06 05:20:12 +08:00
|
|
|
|
2012-12-19 07:10:12 +08:00
|
|
|
// True iff FpValBuf contains an instance of APFloat.
|
2017-10-25 05:24:53 +08:00
|
|
|
bool BufHasFpVal = false;
|
2013-04-06 05:20:12 +08:00
|
|
|
|
2012-12-19 07:10:12 +08: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.
|
2017-10-25 05:24:53 +08:00
|
|
|
short IntVal = 0;
|
2012-12-19 09:10:17 +08:00
|
|
|
|
|
|
|
AlignedCharArrayUnion<APFloat> FpValBuf;
|
2012-12-19 07:10:12 +08:00
|
|
|
};
|
2013-04-06 05:20:12 +08:00
|
|
|
|
2012-12-19 07:10:12 +08: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:
|
2017-10-25 05:24:53 +08:00
|
|
|
FAddend() = default;
|
|
|
|
|
|
|
|
void operator+=(const FAddend &T) {
|
|
|
|
assert((Val == T.Val) && "Symbolic-values disagree");
|
|
|
|
Coeff += T.Coeff;
|
|
|
|
}
|
2013-04-06 05:20:12 +08:00
|
|
|
|
2015-11-05 06:32:32 +08:00
|
|
|
Value *getSymVal() const { return Val; }
|
|
|
|
const FAddendCoef &getCoef() const { return Coeff; }
|
2013-04-06 05:20:12 +08:00
|
|
|
|
2014-04-25 13:29:35 +08:00
|
|
|
bool isConstant() const { return Val == nullptr; }
|
2012-12-19 07:10:12 +08:00
|
|
|
bool isZero() const { return Coeff.isZero(); }
|
|
|
|
|
2016-02-19 06:09:30 +08:00
|
|
|
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-06 05:20:12 +08:00
|
|
|
|
2012-12-19 07:10:12 +08:00
|
|
|
void negate() { Coeff.negate(); }
|
2013-04-06 05:20:12 +08:00
|
|
|
|
2012-12-19 07:10:12 +08: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-06 05:20:12 +08:00
|
|
|
|
2012-12-19 07:10:12 +08:00
|
|
|
/// Similar to FAddend::drillDownOneStep() except that the value being
|
|
|
|
/// splitted is the addend itself.
|
|
|
|
unsigned drillAddendDownOneStep(FAddend &Addend0, FAddend &Addend1) const;
|
2013-04-06 05:20:12 +08:00
|
|
|
|
2012-12-19 07:10:12 +08:00
|
|
|
private:
|
|
|
|
void Scale(const FAddendCoef& ScaleAmt) { Coeff *= ScaleAmt; }
|
2013-04-06 05:20:12 +08:00
|
|
|
|
2012-12-19 07:10:12 +08:00
|
|
|
// This addend has the value of "Coeff * Val".
|
2017-10-25 05:24:53 +08:00
|
|
|
Value *Val = nullptr;
|
2012-12-19 07:10:12 +08:00
|
|
|
FAddendCoef Coeff;
|
|
|
|
};
|
2013-04-06 05:20:12 +08:00
|
|
|
|
2012-12-19 07:10:12 +08:00
|
|
|
/// FAddCombine is the class for optimizing an unsafe fadd/fsub along
|
|
|
|
/// with its neighboring at most two instructions.
|
|
|
|
///
|
|
|
|
class FAddCombine {
|
|
|
|
public:
|
2017-10-25 05:24:53 +08:00
|
|
|
FAddCombine(InstCombiner::BuilderTy &B) : Builder(B) {}
|
|
|
|
|
2012-12-19 07:10:12 +08:00
|
|
|
Value *simplify(Instruction *FAdd);
|
2013-04-06 05:20:12 +08:00
|
|
|
|
2012-12-19 07:10:12 +08:00
|
|
|
private:
|
2017-10-25 05:24:53 +08:00
|
|
|
using AddendVect = SmallVector<const FAddend *, 4>;
|
2013-04-06 05:20:12 +08:00
|
|
|
|
2012-12-19 07:10:12 +08:00
|
|
|
Value *simplifyFAdd(AddendVect& V, unsigned InstrQuota);
|
2013-03-15 02:08:26 +08:00
|
|
|
|
2012-12-19 07:10:12 +08:00
|
|
|
/// Convert given addend to a Value
|
|
|
|
Value *createAddendVal(const FAddend &A, bool& NeedNeg);
|
2013-04-06 05:20:12 +08:00
|
|
|
|
2012-12-19 07:10:12 +08:00
|
|
|
/// Return the number of instructions needed to emit the N-ary addition.
|
|
|
|
unsigned calcInstrNumber(const AddendVect& Vect);
|
2017-10-25 05:24:53 +08:00
|
|
|
|
2012-12-19 07:10:12 +08:00
|
|
|
Value *createFSub(Value *Opnd0, Value *Opnd1);
|
|
|
|
Value *createFAdd(Value *Opnd0, Value *Opnd1);
|
|
|
|
Value *createFMul(Value *Opnd0, Value *Opnd1);
|
|
|
|
Value *createFNeg(Value *V);
|
|
|
|
Value *createNaryFAdd(const AddendVect& Opnds, unsigned InstrQuota);
|
2014-01-20 15:44:53 +08:00
|
|
|
void createInstPostProc(Instruction *NewInst, bool NoNumber = false);
|
2013-04-06 05:20:12 +08:00
|
|
|
|
2012-12-19 07:10:12 +08:00
|
|
|
// Debugging stuff are clustered here.
|
|
|
|
#ifndef NDEBUG
|
|
|
|
unsigned CreateInstrNum;
|
|
|
|
void initCreateInstNum() { CreateInstrNum = 0; }
|
|
|
|
void incCreateInstNum() { CreateInstrNum++; }
|
|
|
|
#else
|
|
|
|
void initCreateInstNum() {}
|
|
|
|
void incCreateInstNum() {}
|
|
|
|
#endif
|
2017-10-25 05:24:53 +08:00
|
|
|
|
|
|
|
InstCombiner::BuilderTy &Builder;
|
|
|
|
Instruction *Instr = nullptr;
|
2012-12-19 07:10:12 +08:00
|
|
|
};
|
2015-11-05 06:32:32 +08:00
|
|
|
|
2017-10-25 05:24:53 +08:00
|
|
|
} // end anonymous namespace
|
2012-12-19 07:10:12 +08: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-06 05:20:12 +08:00
|
|
|
IsFp = BufHasFpVal = true;
|
2012-12-19 07:10:12 +08:00
|
|
|
}
|
|
|
|
|
2013-03-26 04:43:41 +08: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-06 05:20:12 +08:00
|
|
|
IsFp = BufHasFpVal = true;
|
2013-03-26 04:43:41 +08: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-19 07:10:12 +08: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-06 05:20:12 +08:00
|
|
|
|
2012-12-19 07:10:12 +08:00
|
|
|
if (isInt()) {
|
|
|
|
const APFloat &T = That.getFpVal();
|
2013-03-26 04:43:41 +08:00
|
|
|
convertToFpType(T.getSemantics());
|
|
|
|
getFpVal().add(T, RndMode);
|
2012-12-19 07:10:12 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-04-06 05:20:12 +08:00
|
|
|
|
2012-12-19 07:10:12 +08:00
|
|
|
APFloat &T = getFpVal();
|
2013-03-26 04:43:41 +08:00
|
|
|
T.add(createAPFloatFromInt(T.getSemantics(), That.IntVal), RndMode);
|
2012-12-19 07:10:12 +08: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-06 05:20:12 +08:00
|
|
|
const fltSemantics &Semantic =
|
2012-12-19 07:10:12 +08:00
|
|
|
isInt() ? That.getFpVal().getSemantics() : getFpVal().getSemantics();
|
|
|
|
|
|
|
|
if (isInt())
|
2013-03-26 04:43:41 +08:00
|
|
|
convertToFpType(Semantic);
|
2012-12-19 07:10:12 +08:00
|
|
|
APFloat &F0 = getFpVal();
|
|
|
|
|
|
|
|
if (That.isInt())
|
2013-03-26 04:43:41 +08:00
|
|
|
F0.multiply(createAPFloatFromInt(Semantic, That.IntVal),
|
|
|
|
APFloat::rmNearestTiesToEven);
|
2012-12-19 07:10:12 +08:00
|
|
|
else
|
|
|
|
F0.multiply(That.getFpVal(), APFloat::rmNearestTiesToEven);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06 05:20:12 +08:00
|
|
|
// A + C <1, A> <C, NULL>
|
2012-12-19 07:10:12 +08:00
|
|
|
// 0 +/- 0 <0, NULL> (corner case)
|
|
|
|
//
|
|
|
|
// Legend: A and B are not constant, C is constant
|
|
|
|
unsigned FAddend::drillValueDownOneStep
|
|
|
|
(Value *Val, FAddend &Addend0, FAddend &Addend1) {
|
2014-04-25 13:29:35 +08:00
|
|
|
Instruction *I = nullptr;
|
|
|
|
if (!Val || !(I = dyn_cast<Instruction>(Val)))
|
2012-12-19 07:10:12 +08: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 13:29:35 +08:00
|
|
|
Opnd0 = nullptr;
|
2012-12-19 07:10:12 +08:00
|
|
|
|
|
|
|
if ((C1 = dyn_cast<ConstantFP>(Opnd1)) && C1->isZero())
|
2014-04-25 13:29:35 +08:00
|
|
|
Opnd1 = nullptr;
|
2012-12-19 07:10:12 +08:00
|
|
|
|
|
|
|
if (Opnd0) {
|
|
|
|
if (!C0)
|
|
|
|
Addend0.set(1, Opnd0);
|
|
|
|
else
|
2014-04-25 13:29:35 +08:00
|
|
|
Addend0.set(C0, nullptr);
|
2012-12-19 07:10:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Opnd1) {
|
|
|
|
FAddend &Addend = Opnd0 ? Addend1 : Addend0;
|
|
|
|
if (!C1)
|
|
|
|
Addend.set(1, Opnd1);
|
|
|
|
else
|
2014-04-25 13:29:35 +08:00
|
|
|
Addend.set(C1, nullptr);
|
2012-12-19 07:10:12 +08:00
|
|
|
if (Opcode == Instruction::FSub)
|
|
|
|
Addend.negate();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Opnd0 || Opnd1)
|
|
|
|
return Opnd0 && Opnd1 ? 2 : 1;
|
|
|
|
|
|
|
|
// Both operands are zero. Weird!
|
2014-04-25 13:29:35 +08:00
|
|
|
Addend0.set(APFloat(C0->getValueAPF().getSemantics()), nullptr);
|
2012-12-19 07:10:12 +08: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-06 05:20:12 +08:00
|
|
|
if (!BreakNum || Coeff.isOne())
|
2012-12-19 07:10:12 +08:00
|
|
|
return BreakNum;
|
|
|
|
|
|
|
|
Addend0.Scale(Coeff);
|
|
|
|
|
|
|
|
if (BreakNum == 2)
|
|
|
|
Addend1.Scale(Coeff);
|
|
|
|
|
|
|
|
return BreakNum;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *FAddCombine::simplify(Instruction *I) {
|
2018-04-15 03:18:28 +08:00
|
|
|
assert(I->hasAllowReassoc() && I->hasNoSignedZeros() &&
|
|
|
|
"Expected 'reassoc'+'nsz' instruction");
|
2012-12-19 07:10:12 +08:00
|
|
|
|
|
|
|
// Currently we are not able to handle vector type.
|
|
|
|
if (I->getType()->isVectorTy())
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2012-12-19 07:10:12 +08:00
|
|
|
|
|
|
|
assert((I->getOpcode() == Instruction::FAdd ||
|
|
|
|
I->getOpcode() == Instruction::FSub) && "Expect add/sub");
|
|
|
|
|
2013-04-06 05:20:12 +08:00
|
|
|
// Save the instruction before calling other member-functions.
|
2012-12-19 07:10:12 +08: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-06 05:20:12 +08:00
|
|
|
if (!Opnd0.isConstant())
|
2012-12-19 07:10:12 +08: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-06 05:20:12 +08:00
|
|
|
InstQuota = ((!isa<Constant>(V0) && V0->hasOneUse()) &&
|
2012-12-19 07:10:12 +08: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 13:29:35 +08:00
|
|
|
return CE.isOne() ? Opnd0.getSymVal() : nullptr;
|
2012-12-19 07:10:12 +08: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;
|
|
|
|
}
|
|
|
|
|
2018-08-12 23:48:26 +08:00
|
|
|
return nullptr;
|
2012-12-19 07:10:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Value *FAddCombine::simplifyFAdd(AddendVect& Addends, unsigned InstrQuota) {
|
|
|
|
unsigned AddendNum = Addends.size();
|
|
|
|
assert(AddendNum <= 4 && "Too many addends");
|
|
|
|
|
2013-04-06 05:20:12 +08:00
|
|
|
// For saving intermediate results;
|
2012-12-19 07:10:12 +08: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 13:29:35 +08:00
|
|
|
const FAddend *ConstAdd = nullptr;
|
2012-12-19 07:10:12 +08:00
|
|
|
|
|
|
|
// Simplified addends are placed <SimpVect>.
|
|
|
|
AddendVect SimpVect;
|
|
|
|
|
|
|
|
// The outer loop works on one symbolic-value at a time. Suppose the input
|
2013-04-06 05:20:12 +08:00
|
|
|
// addends are : <a1, x>, <b1, y>, <a2, x>, <c1, z>, <b2, y>, ...
|
2012-12-19 07:10:12 +08: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 13:29:35 +08:00
|
|
|
Addends[SameSymIdx] = nullptr;
|
2012-12-19 07:10:12 +08: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-06 05:20:12 +08:00
|
|
|
SimpVect.resize(StartIdx);
|
2014-04-25 13:29:35 +08:00
|
|
|
if (Val) {
|
2012-12-19 07:10:12 +08: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 12:27:47 +08:00
|
|
|
assert((NextTmpIdx <= array_lengthof(TmpResult) + 1) &&
|
2012-12-19 07:10:12 +08: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.
|
2017-10-25 05:24:53 +08:00
|
|
|
|
2012-12-19 07:10:12 +08:00
|
|
|
unsigned InstrNeeded = calcInstrNumber(Opnds);
|
|
|
|
if (InstrNeeded > InstrQuota)
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2012-12-19 07:10:12 +08: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 13:29:35 +08:00
|
|
|
Value *LastVal = nullptr;
|
2012-12-19 07:10:12 +08:00
|
|
|
bool LastValNeedNeg = false;
|
|
|
|
|
|
|
|
// Iterate the addends, creating fadd/fsub using adjacent two addends.
|
2016-06-26 20:28:59 +08:00
|
|
|
for (const FAddend *Opnd : Opnds) {
|
2013-04-06 05:20:12 +08:00
|
|
|
bool NeedNeg;
|
2016-06-26 20:28:59 +08:00
|
|
|
Value *V = createAddendVal(*Opnd, NeedNeg);
|
2012-12-19 07:10:12 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-10-25 05:24:53 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
assert(CreateInstrNum == InstrNeeded &&
|
|
|
|
"Inconsistent in instruction numbers");
|
|
|
|
#endif
|
2012-12-19 07:10:12 +08:00
|
|
|
|
|
|
|
return LastVal;
|
|
|
|
}
|
|
|
|
|
2014-12-19 05:11:09 +08:00
|
|
|
Value *FAddCombine::createFSub(Value *Opnd0, Value *Opnd1) {
|
2017-07-08 07:16:26 +08:00
|
|
|
Value *V = Builder.CreateFSub(Opnd0, Opnd1);
|
2013-03-15 02:08:26 +08:00
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V))
|
|
|
|
createInstPostProc(I);
|
2012-12-19 07:10:12 +08:00
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *FAddCombine::createFNeg(Value *V) {
|
2014-12-20 00:44:08 +08:00
|
|
|
Value *Zero = cast<Value>(ConstantFP::getZeroValueForNegation(V->getType()));
|
2014-01-20 15:44:53 +08: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-19 07:10:12 +08:00
|
|
|
}
|
|
|
|
|
2014-12-19 05:11:09 +08:00
|
|
|
Value *FAddCombine::createFAdd(Value *Opnd0, Value *Opnd1) {
|
2017-07-08 07:16:26 +08:00
|
|
|
Value *V = Builder.CreateFAdd(Opnd0, Opnd1);
|
2013-03-15 02:08:26 +08:00
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V))
|
|
|
|
createInstPostProc(I);
|
2012-12-19 07:10:12 +08:00
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *FAddCombine::createFMul(Value *Opnd0, Value *Opnd1) {
|
2017-07-08 07:16:26 +08:00
|
|
|
Value *V = Builder.CreateFMul(Opnd0, Opnd1);
|
2013-03-15 02:08:26 +08:00
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V))
|
|
|
|
createInstPostProc(I);
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2014-12-19 05:11:09 +08:00
|
|
|
void FAddCombine::createInstPostProc(Instruction *NewInstr, bool NoNumber) {
|
2012-12-19 07:10:12 +08:00
|
|
|
NewInstr->setDebugLoc(Instr->getDebugLoc());
|
|
|
|
|
|
|
|
// Keep track of the number of instruction created.
|
2014-01-20 15:44:53 +08:00
|
|
|
if (!NoNumber)
|
|
|
|
incCreateInstNum();
|
2012-12-19 07:10:12 +08: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-06 05:20:12 +08:00
|
|
|
// The number of addends in the form of "(-1)*x".
|
|
|
|
unsigned NegOpndNum = 0;
|
2012-12-19 07:10:12 +08:00
|
|
|
|
|
|
|
// Adjust the number of instructions needed to emit the N-ary add.
|
2016-06-26 20:28:59 +08:00
|
|
|
for (const FAddend *Opnd : Opnds) {
|
2012-12-19 07:10:12 +08:00
|
|
|
if (Opnd->isConstant())
|
|
|
|
continue;
|
|
|
|
|
2017-04-25 01:24:37 +08:00
|
|
|
// The constant check above is really for a few special constant
|
|
|
|
// coefficients.
|
|
|
|
if (isa<UndefValue>(Opnd->getSymVal()))
|
|
|
|
continue;
|
|
|
|
|
2012-12-19 07:10:12 +08:00
|
|
|
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-19 05:11:09 +08:00
|
|
|
Value *FAddCombine::createAddendVal(const FAddend &Opnd, bool &NeedNeg) {
|
2012-12-19 07:10:12 +08: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-19 18:36:52 +08:00
|
|
|
// Checks if any operand is negative and we can convert add to sub.
|
2014-06-27 15:47:35 +08: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 22:47:51 +08:00
|
|
|
static Value *checkForNegativeOperand(BinaryOperator &I,
|
2017-07-08 07:16:26 +08:00
|
|
|
InstCombiner::BuilderTy &Builder) {
|
2014-06-27 15:47:35 +08: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 13:40:22 +08:00
|
|
|
|
2014-06-27 15:47:35 +08: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))) {
|
2017-07-08 07:16:26 +08:00
|
|
|
Value *NewAnd = Builder.CreateAnd(Z, *C1);
|
|
|
|
return Builder.CreateSub(RHS, NewAnd, "sub");
|
2014-06-27 15:47:35 +08:00
|
|
|
} 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))
|
2017-07-08 07:16:26 +08:00
|
|
|
Value *NewOr = Builder.CreateOr(Z, ~(*C1));
|
|
|
|
return Builder.CreateSub(RHS, NewOr, "sub");
|
2014-06-27 15:47:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)) {
|
2017-07-08 07:16:26 +08:00
|
|
|
Value *NewOr = Builder.CreateOr(Z, ~(*C2));
|
|
|
|
return Builder.CreateSub(RHS, NewOr, "sub");
|
2014-06-27 15:47:35 +08:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-03-01 03:05:26 +08:00
|
|
|
/// Wrapping flags may allow combining constants separated by an extend.
|
|
|
|
static Instruction *foldNoWrapAdd(BinaryOperator &Add,
|
|
|
|
InstCombiner::BuilderTy &Builder) {
|
|
|
|
Value *Op0 = Add.getOperand(0), *Op1 = Add.getOperand(1);
|
|
|
|
Type *Ty = Add.getType();
|
|
|
|
Constant *Op1C;
|
|
|
|
if (!match(Op1, m_Constant(Op1C)))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Try this match first because it results in an add in the narrow type.
|
|
|
|
// (zext (X +nuw C2)) + C1 --> zext (X + (C2 + trunc(C1)))
|
|
|
|
Value *X;
|
|
|
|
const APInt *C1, *C2;
|
|
|
|
if (match(Op1, m_APInt(C1)) &&
|
|
|
|
match(Op0, m_OneUse(m_ZExt(m_NUWAdd(m_Value(X), m_APInt(C2))))) &&
|
|
|
|
C1->isNegative() && C1->sge(-C2->sext(C1->getBitWidth()))) {
|
|
|
|
Constant *NewC =
|
|
|
|
ConstantInt::get(X->getType(), *C2 + C1->trunc(C2->getBitWidth()));
|
|
|
|
return new ZExtInst(Builder.CreateNUWAdd(X, NewC), Ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
// More general combining of constants in the wide type.
|
|
|
|
// (sext (X +nsw NarrowC)) + C --> (sext X) + (sext(NarrowC) + C)
|
|
|
|
Constant *NarrowC;
|
|
|
|
if (match(Op0, m_OneUse(m_SExt(m_NSWAdd(m_Value(X), m_Constant(NarrowC)))))) {
|
|
|
|
Constant *WideC = ConstantExpr::getSExt(NarrowC, Ty);
|
|
|
|
Constant *NewC = ConstantExpr::getAdd(WideC, Op1C);
|
|
|
|
Value *WideX = Builder.CreateSExt(X, Ty);
|
|
|
|
return BinaryOperator::CreateAdd(WideX, NewC);
|
|
|
|
}
|
|
|
|
// (zext (X +nuw NarrowC)) + C --> (zext X) + (zext(NarrowC) + C)
|
|
|
|
if (match(Op0, m_OneUse(m_ZExt(m_NUWAdd(m_Value(X), m_Constant(NarrowC)))))) {
|
|
|
|
Constant *WideC = ConstantExpr::getZExt(NarrowC, Ty);
|
|
|
|
Constant *NewC = ConstantExpr::getAdd(WideC, Op1C);
|
|
|
|
Value *WideX = Builder.CreateZExt(X, Ty);
|
|
|
|
return BinaryOperator::CreateAdd(WideX, NewC);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-10-14 00:43:58 +08:00
|
|
|
Instruction *InstCombiner::foldAddWithConstant(BinaryOperator &Add) {
|
2017-05-10 08:07:16 +08:00
|
|
|
Value *Op0 = Add.getOperand(0), *Op1 = Add.getOperand(1);
|
2017-10-14 00:29:38 +08:00
|
|
|
Constant *Op1C;
|
|
|
|
if (!match(Op1, m_Constant(Op1C)))
|
|
|
|
return nullptr;
|
|
|
|
|
2018-03-01 00:36:24 +08:00
|
|
|
if (Instruction *NV = foldBinOpIntoSelectOrPhi(Add))
|
2017-10-14 00:43:58 +08:00
|
|
|
return NV;
|
|
|
|
|
2019-05-31 17:47:04 +08:00
|
|
|
Value *X;
|
|
|
|
Constant *Op00C;
|
|
|
|
|
|
|
|
// add (sub C1, X), C2 --> sub (add C1, C2), X
|
|
|
|
if (match(Op0, m_Sub(m_Constant(Op00C), m_Value(X))))
|
|
|
|
return BinaryOperator::CreateSub(ConstantExpr::getAdd(Op00C, Op1C), X);
|
|
|
|
|
|
|
|
Value *Y;
|
2018-07-29 00:48:44 +08:00
|
|
|
|
|
|
|
// add (sub X, Y), -1 --> add (not Y), X
|
|
|
|
if (match(Op0, m_OneUse(m_Sub(m_Value(X), m_Value(Y)))) &&
|
|
|
|
match(Op1, m_AllOnes()))
|
|
|
|
return BinaryOperator::CreateAdd(Builder.CreateNot(Y), X);
|
|
|
|
|
2017-10-14 04:29:11 +08:00
|
|
|
// zext(bool) + C -> bool ? C + 1 : C
|
|
|
|
if (match(Op0, m_ZExt(m_Value(X))) &&
|
|
|
|
X->getType()->getScalarSizeInBits() == 1)
|
2017-10-14 01:00:47 +08:00
|
|
|
return SelectInst::Create(X, AddOne(Op1C), Op1);
|
2017-10-14 00:29:38 +08:00
|
|
|
|
2017-10-14 04:29:11 +08:00
|
|
|
// ~X + C --> (C-1) - X
|
|
|
|
if (match(Op0, m_Not(m_Value(X))))
|
|
|
|
return BinaryOperator::CreateSub(SubOne(Op1C), X);
|
|
|
|
|
2017-05-10 08:07:16 +08:00
|
|
|
const APInt *C;
|
|
|
|
if (!match(Op1, m_APInt(C)))
|
|
|
|
return nullptr;
|
|
|
|
|
2019-05-08 03:36:41 +08:00
|
|
|
// (X | C2) + C --> (X | C2) ^ C2 iff (C2 == -C)
|
|
|
|
const APInt *C2;
|
|
|
|
if (match(Op0, m_Or(m_Value(), m_APInt(C2))) && *C2 == -*C)
|
|
|
|
return BinaryOperator::CreateXor(Op0, ConstantInt::get(Add.getType(), *C2));
|
|
|
|
|
2017-05-10 08:07:16 +08:00
|
|
|
if (C->isSignMask()) {
|
|
|
|
// If wrapping is not allowed, then the addition must set the sign bit:
|
|
|
|
// X + (signmask) --> X | signmask
|
|
|
|
if (Add.hasNoSignedWrap() || Add.hasNoUnsignedWrap())
|
|
|
|
return BinaryOperator::CreateOr(Op0, Op1);
|
|
|
|
|
|
|
|
// If wrapping is allowed, then the addition flips the sign bit of LHS:
|
|
|
|
// X + (signmask) --> X ^ signmask
|
|
|
|
return BinaryOperator::CreateXor(Op0, Op1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this add the last step in a convoluted sext?
|
|
|
|
// add(zext(xor i16 X, -32768), -32768) --> sext X
|
2017-10-14 01:00:47 +08:00
|
|
|
Type *Ty = Add.getType();
|
2017-05-10 08:07:16 +08:00
|
|
|
if (match(Op0, m_ZExt(m_Xor(m_Value(X), m_APInt(C2)))) &&
|
|
|
|
C2->isMinSignedValue() && C2->sext(Ty->getScalarSizeInBits()) == *C)
|
|
|
|
return CastInst::Create(Instruction::SExt, X, Ty);
|
|
|
|
|
[InstCombine] add (sext i1 X), 1 --> zext (not X)
http://rise4fun.com/Alive/i8Q
A narrow bitwise logic op is obviously better than math for value tracking,
and zext is better than sext. Typically, the 'not' will be folded into an
icmp predicate.
The IR difference would even survive through codegen for x86, so we would see
worse code:
https://godbolt.org/g/C14HMF
one_or_zero(int, int): # @one_or_zero(int, int)
xorl %eax, %eax
cmpl %esi, %edi
setle %al
retq
one_or_zero_alt(int, int): # @one_or_zero_alt(int, int)
xorl %ecx, %ecx
cmpl %esi, %edi
setg %cl
movl $1, %eax
subl %ecx, %eax
retq
llvm-svn: 306243
2017-06-25 22:15:28 +08:00
|
|
|
if (C->isOneValue() && Op0->hasOneUse()) {
|
|
|
|
// add (sext i1 X), 1 --> zext (not X)
|
|
|
|
// TODO: The smallest IR representation is (select X, 0, 1), and that would
|
|
|
|
// not require the one-use check. But we need to remove a transform in
|
|
|
|
// visitSelect and make sure that IR value tracking for select is equal or
|
|
|
|
// better than for these ops.
|
|
|
|
if (match(Op0, m_SExt(m_Value(X))) &&
|
|
|
|
X->getType()->getScalarSizeInBits() == 1)
|
|
|
|
return new ZExtInst(Builder.CreateNot(X), Ty);
|
|
|
|
|
|
|
|
// Shifts and add used to flip and mask off the low bit:
|
|
|
|
// add (ashr (shl i32 X, 31), 31), 1 --> and (not X), 1
|
|
|
|
const APInt *C3;
|
|
|
|
if (match(Op0, m_AShr(m_Shl(m_Value(X), m_APInt(C2)), m_APInt(C3))) &&
|
|
|
|
C2 == C3 && *C2 == Ty->getScalarSizeInBits() - 1) {
|
|
|
|
Value *NotX = Builder.CreateNot(X);
|
|
|
|
return BinaryOperator::CreateAnd(NotX, ConstantInt::get(Ty, 1));
|
|
|
|
}
|
2017-05-10 21:56:52 +08:00
|
|
|
}
|
|
|
|
|
2017-05-10 08:07:16 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-04-27 04:52:28 +08:00
|
|
|
// Matches multiplication expression Op * C where C is a constant. Returns the
|
|
|
|
// constant value in C and the other operand in Op. Returns true if such a
|
|
|
|
// match is found.
|
|
|
|
static bool MatchMul(Value *E, Value *&Op, APInt &C) {
|
|
|
|
const APInt *AI;
|
|
|
|
if (match(E, m_Mul(m_Value(Op), m_APInt(AI)))) {
|
|
|
|
C = *AI;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (match(E, m_Shl(m_Value(Op), m_APInt(AI)))) {
|
|
|
|
C = APInt(AI->getBitWidth(), 1);
|
|
|
|
C <<= *AI;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matches remainder expression Op % C where C is a constant. Returns the
|
|
|
|
// constant value in C and the other operand in Op. Returns the signedness of
|
|
|
|
// the remainder operation in IsSigned. Returns true if such a match is
|
|
|
|
// found.
|
|
|
|
static bool MatchRem(Value *E, Value *&Op, APInt &C, bool &IsSigned) {
|
|
|
|
const APInt *AI;
|
|
|
|
IsSigned = false;
|
|
|
|
if (match(E, m_SRem(m_Value(Op), m_APInt(AI)))) {
|
|
|
|
IsSigned = true;
|
|
|
|
C = *AI;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (match(E, m_URem(m_Value(Op), m_APInt(AI)))) {
|
|
|
|
C = *AI;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (match(E, m_And(m_Value(Op), m_APInt(AI))) && (*AI + 1).isPowerOf2()) {
|
|
|
|
C = *AI + 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matches division expression Op / C with the given signedness as indicated
|
|
|
|
// by IsSigned, where C is a constant. Returns the constant value in C and the
|
|
|
|
// other operand in Op. Returns true if such a match is found.
|
|
|
|
static bool MatchDiv(Value *E, Value *&Op, APInt &C, bool IsSigned) {
|
|
|
|
const APInt *AI;
|
|
|
|
if (IsSigned && match(E, m_SDiv(m_Value(Op), m_APInt(AI)))) {
|
|
|
|
C = *AI;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!IsSigned) {
|
|
|
|
if (match(E, m_UDiv(m_Value(Op), m_APInt(AI)))) {
|
|
|
|
C = *AI;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (match(E, m_LShr(m_Value(Op), m_APInt(AI)))) {
|
|
|
|
C = APInt(AI->getBitWidth(), 1);
|
|
|
|
C <<= *AI;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns whether C0 * C1 with the given signedness overflows.
|
|
|
|
static bool MulWillOverflow(APInt &C0, APInt &C1, bool IsSigned) {
|
|
|
|
bool overflow;
|
|
|
|
if (IsSigned)
|
|
|
|
(void)C0.smul_ov(C1, overflow);
|
|
|
|
else
|
|
|
|
(void)C0.umul_ov(C1, overflow);
|
|
|
|
return overflow;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simplifies X % C0 + (( X / C0 ) % C1) * C0 to X % (C0 * C1), where (C0 * C1)
|
|
|
|
// does not overflow.
|
|
|
|
Value *InstCombiner::SimplifyAddWithRemainder(BinaryOperator &I) {
|
|
|
|
Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
|
|
|
|
Value *X, *MulOpV;
|
|
|
|
APInt C0, MulOpC;
|
|
|
|
bool IsSigned;
|
|
|
|
// Match I = X % C0 + MulOpV * C0
|
|
|
|
if (((MatchRem(LHS, X, C0, IsSigned) && MatchMul(RHS, MulOpV, MulOpC)) ||
|
|
|
|
(MatchRem(RHS, X, C0, IsSigned) && MatchMul(LHS, MulOpV, MulOpC))) &&
|
|
|
|
C0 == MulOpC) {
|
|
|
|
Value *RemOpV;
|
|
|
|
APInt C1;
|
|
|
|
bool Rem2IsSigned;
|
|
|
|
// Match MulOpC = RemOpV % C1
|
|
|
|
if (MatchRem(MulOpV, RemOpV, C1, Rem2IsSigned) &&
|
|
|
|
IsSigned == Rem2IsSigned) {
|
|
|
|
Value *DivOpV;
|
|
|
|
APInt DivOpC;
|
|
|
|
// Match RemOpV = X / C0
|
|
|
|
if (MatchDiv(RemOpV, DivOpV, DivOpC, IsSigned) && X == DivOpV &&
|
|
|
|
C0 == DivOpC && !MulWillOverflow(C0, C1, IsSigned)) {
|
|
|
|
Value *NewDivisor =
|
|
|
|
ConstantInt::get(X->getType()->getContext(), C0 * C1);
|
|
|
|
return IsSigned ? Builder.CreateSRem(X, NewDivisor, "srem")
|
|
|
|
: Builder.CreateURem(X, NewDivisor, "urem");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
[InstCombine] PR37603: low bit mask canonicalization
Summary:
This is [[ https://bugs.llvm.org/show_bug.cgi?id=37603 | PR37603 ]].
https://godbolt.org/g/VCMNpS
https://rise4fun.com/Alive/idM
When doing bit manipulations, it is quite common to calculate some bit mask,
and apply it to some value via `and`.
The typical C code looks like:
```
int mask_signed_add(int nbits) {
return (1 << nbits) - 1;
}
```
which is translated into (with `-O3`)
```
define dso_local i32 @mask_signed_add(int)(i32) local_unnamed_addr #0 {
%2 = shl i32 1, %0
%3 = add nsw i32 %2, -1
ret i32 %3
}
```
But there is a second, less readable variant:
```
int mask_signed_xor(int nbits) {
return ~(-(1 << nbits));
}
```
which is translated into (with `-O3`)
```
define dso_local i32 @mask_signed_xor(int)(i32) local_unnamed_addr #0 {
%2 = shl i32 -1, %0
%3 = xor i32 %2, -1
ret i32 %3
}
```
Since we created such a mask, it is quite likely that we will use it in `and` next.
And then we may get rid of `not` op by folding into `andn`.
But now that i have actually looked:
https://godbolt.org/g/VTUDmU
_some_ backend changes will be needed too.
We clearly loose `bzhi` recognition.
Reviewers: spatel, craig.topper, RKSimon
Reviewed By: spatel
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D47428
llvm-svn: 334127
2018-06-07 03:38:27 +08:00
|
|
|
/// Fold
|
|
|
|
/// (1 << NBits) - 1
|
|
|
|
/// Into:
|
|
|
|
/// ~(-(1 << NBits))
|
|
|
|
/// Because a 'not' is better for bit-tracking analysis and other transforms
|
|
|
|
/// than an 'add'. The new shl is always nsw, and is nuw if old `and` was.
|
|
|
|
static Instruction *canonicalizeLowbitMask(BinaryOperator &I,
|
|
|
|
InstCombiner::BuilderTy &Builder) {
|
|
|
|
Value *NBits;
|
|
|
|
if (!match(&I, m_Add(m_OneUse(m_Shl(m_One(), m_Value(NBits))), m_AllOnes())))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
Constant *MinusOne = Constant::getAllOnesValue(NBits->getType());
|
|
|
|
Value *NotMask = Builder.CreateShl(MinusOne, NBits, "notmask");
|
|
|
|
// Be wary of constant folding.
|
|
|
|
if (auto *BOp = dyn_cast<BinaryOperator>(NotMask)) {
|
|
|
|
// Always NSW. But NUW propagates from `add`.
|
|
|
|
BOp->setHasNoSignedWrap();
|
|
|
|
BOp->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
return BinaryOperator::CreateNot(NotMask, I.getName());
|
|
|
|
}
|
|
|
|
|
2019-03-27 01:50:08 +08:00
|
|
|
static Instruction *foldToUnsignedSaturatedAdd(BinaryOperator &I) {
|
|
|
|
assert(I.getOpcode() == Instruction::Add && "Expecting add instruction");
|
|
|
|
Type *Ty = I.getType();
|
|
|
|
auto getUAddSat = [&]() {
|
|
|
|
return Intrinsic::getDeclaration(I.getModule(), Intrinsic::uadd_sat, Ty);
|
|
|
|
};
|
|
|
|
|
|
|
|
// add (umin X, ~Y), Y --> uaddsat X, Y
|
|
|
|
Value *X, *Y;
|
|
|
|
if (match(&I, m_c_Add(m_c_UMin(m_Value(X), m_Not(m_Value(Y))),
|
|
|
|
m_Deferred(Y))))
|
|
|
|
return CallInst::Create(getUAddSat(), { X, Y });
|
|
|
|
|
|
|
|
// add (umin X, ~C), C --> uaddsat X, C
|
|
|
|
const APInt *C, *NotC;
|
|
|
|
if (match(&I, m_Add(m_UMin(m_Value(X), m_APInt(NotC)), m_APInt(C))) &&
|
|
|
|
*C == ~*NotC)
|
|
|
|
return CallInst::Create(getUAddSat(), { X, ConstantInt::get(Ty, *C) });
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-06-27 15:47:35 +08:00
|
|
|
Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
|
2018-06-22 01:06:36 +08:00
|
|
|
if (Value *V = SimplifyAddInst(I.getOperand(0), I.getOperand(1),
|
|
|
|
I.hasNoSignedWrap(), I.hasNoUnsignedWrap(),
|
|
|
|
SQ.getWithInstruction(&I)))
|
2016-02-02 06:23:39 +08:00
|
|
|
return replaceInstUsesWith(I, V);
|
2010-01-05 15:18:46 +08:00
|
|
|
|
2018-07-13 09:18:07 +08:00
|
|
|
if (SimplifyAssociativeOrCommutative(I))
|
|
|
|
return &I;
|
|
|
|
|
2018-10-03 23:20:58 +08:00
|
|
|
if (Instruction *X = foldVectorBinop(I))
|
2018-06-03 00:27:44 +08:00
|
|
|
return X;
|
|
|
|
|
2017-10-25 05:24:53 +08:00
|
|
|
// (A*B)+(A*C) -> A*(B+C) etc
|
2010-12-22 21:36:08 +08:00
|
|
|
if (Value *V = SimplifyUsingDistributiveLaws(I))
|
2016-02-02 06:23:39 +08:00
|
|
|
return replaceInstUsesWith(I, V);
|
2010-12-22 21:36:08 +08:00
|
|
|
|
2017-10-14 00:43:58 +08:00
|
|
|
if (Instruction *X = foldAddWithConstant(I))
|
2017-05-10 08:07:16 +08:00
|
|
|
return X;
|
2012-12-13 04:57:53 +08:00
|
|
|
|
2019-03-01 03:05:26 +08:00
|
|
|
if (Instruction *X = foldNoWrapAdd(I, Builder))
|
|
|
|
return X;
|
|
|
|
|
2017-05-10 08:07:16 +08:00
|
|
|
// FIXME: This should be moved into the above helper function to allow these
|
2017-10-14 00:29:38 +08:00
|
|
|
// transforms for general constant or constant splat vectors.
|
2018-06-22 01:06:36 +08:00
|
|
|
Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
|
2017-10-14 02:32:53 +08:00
|
|
|
Type *Ty = I.getType();
|
2016-07-17 02:29:26 +08:00
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
|
2014-04-25 13:29:35 +08:00
|
|
|
Value *XorLHS = nullptr; ConstantInt *XorRHS = nullptr;
|
2011-02-10 13:14:58 +08:00
|
|
|
if (match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) {
|
2017-10-14 02:32:53 +08:00
|
|
|
unsigned TySizeBits = Ty->getScalarSizeInBits();
|
2011-02-10 13:14:58 +08:00
|
|
|
const APInt &RHSVal = CI->getValue();
|
2010-01-31 12:29:12 +08: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 15:18:46 +08:00
|
|
|
}
|
2012-12-13 04:57:53 +08:00
|
|
|
|
2010-01-31 12:29:12 +08: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).
llvm-svn: 217342
2014-09-08 02:57:58 +08:00
|
|
|
if (!MaskedValueIsZero(XorLHS, Mask, 0, &I))
|
2010-01-31 12:29:12 +08:00
|
|
|
ExtendAmt = 0;
|
|
|
|
}
|
2012-12-13 04:57:53 +08:00
|
|
|
|
2010-01-31 12:29:12 +08:00
|
|
|
if (ExtendAmt) {
|
2017-10-14 02:32:53 +08:00
|
|
|
Constant *ShAmt = ConstantInt::get(Ty, ExtendAmt);
|
2017-07-08 07:16:26 +08:00
|
|
|
Value *NewShl = Builder.CreateShl(XorLHS, ShAmt, "sext");
|
2010-01-31 12:29:12 +08:00
|
|
|
return BinaryOperator::CreateAShr(NewShl, ShAmt);
|
2010-01-05 15:18:46 +08:00
|
|
|
}
|
2011-12-25 01:31:53 +08: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()) {
|
2017-05-25 00:53:07 +08:00
|
|
|
KnownBits LHSKnown = computeKnownBits(XorLHS, 0, &I);
|
2017-04-27 00:39:58 +08:00
|
|
|
if ((XorRHS->getValue() | LHSKnown.Zero).isAllOnesValue())
|
2011-12-25 01:31:53 +08:00
|
|
|
return BinaryOperator::CreateSub(ConstantExpr::getAdd(XorRHS, CI),
|
|
|
|
XorLHS);
|
|
|
|
}
|
2017-04-21 00:56:25 +08:00
|
|
|
// (X + signmask) + C could have gotten canonicalized to (X^signmask) + C,
|
|
|
|
// transform them into (X + (signmask ^ C))
|
|
|
|
if (XorRHS->getValue().isSignMask())
|
2015-12-21 09:02:28 +08:00
|
|
|
return BinaryOperator::CreateAdd(XorLHS,
|
|
|
|
ConstantExpr::getXor(XorRHS, CI));
|
2010-01-05 15:18:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-14 02:32:53 +08:00
|
|
|
if (Ty->isIntOrIntVectorTy(1))
|
2010-01-05 15:18:46 +08:00
|
|
|
return BinaryOperator::CreateXor(LHS, RHS);
|
|
|
|
|
2011-02-10 13:14:58 +08:00
|
|
|
// X + X --> X << 1
|
2011-02-18 04:55:29 +08:00
|
|
|
if (LHS == RHS) {
|
2017-10-14 02:32:53 +08:00
|
|
|
auto *Shl = BinaryOperator::CreateShl(LHS, ConstantInt::get(Ty, 1));
|
|
|
|
Shl->setHasNoSignedWrap(I.hasNoSignedWrap());
|
|
|
|
Shl->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
|
|
|
|
return Shl;
|
2011-02-17 10:23:02 +08:00
|
|
|
}
|
2010-01-05 15:18:46 +08:00
|
|
|
|
2017-10-14 05:28:50 +08:00
|
|
|
Value *A, *B;
|
|
|
|
if (match(LHS, m_Neg(m_Value(A)))) {
|
|
|
|
// -A + -B --> -(A + B)
|
|
|
|
if (match(RHS, m_Neg(m_Value(B))))
|
|
|
|
return BinaryOperator::CreateNeg(Builder.CreateAdd(A, B));
|
2012-12-13 04:57:53 +08:00
|
|
|
|
2017-10-14 05:28:50 +08:00
|
|
|
// -A + B --> B - A
|
|
|
|
return BinaryOperator::CreateSub(RHS, A);
|
2010-01-05 15:18:46 +08:00
|
|
|
}
|
|
|
|
|
2019-02-25 00:57:45 +08:00
|
|
|
// Canonicalize sext to zext for better value tracking potential.
|
|
|
|
// add A, sext(B) --> sub A, zext(B)
|
|
|
|
if (match(&I, m_c_Add(m_Value(A), m_OneUse(m_SExt(m_Value(B))))) &&
|
|
|
|
B->getType()->isIntOrIntVectorTy(1))
|
|
|
|
return BinaryOperator::CreateSub(A, Builder.CreateZExt(B, Ty));
|
|
|
|
|
2010-01-05 15:18:46 +08:00
|
|
|
// A + -B --> A - B
|
2017-10-14 05:28:50 +08:00
|
|
|
if (match(RHS, m_Neg(m_Value(B))))
|
|
|
|
return BinaryOperator::CreateSub(LHS, B);
|
2010-01-05 15:18:46 +08:00
|
|
|
|
2014-06-19 18:36:52 +08:00
|
|
|
if (Value *V = checkForNegativeOperand(I, Builder))
|
2016-02-02 06:23:39 +08:00
|
|
|
return replaceInstUsesWith(I, V);
|
2014-06-19 18:36:52 +08:00
|
|
|
|
2018-06-26 13:31:18 +08:00
|
|
|
// (A + 1) + ~B --> A - B
|
|
|
|
// ~B + (A + 1) --> A - B
|
2019-07-01 23:55:24 +08:00
|
|
|
// (~B + A) + 1 --> A - B
|
|
|
|
// (A + ~B) + 1 --> A - B
|
|
|
|
if (match(&I, m_c_BinOp(m_Add(m_Value(A), m_One()), m_Not(m_Value(B)))) ||
|
|
|
|
match(&I, m_BinOp(m_c_Add(m_Not(m_Value(B)), m_Value(A)), m_One())))
|
2018-06-26 13:31:18 +08:00
|
|
|
return BinaryOperator::CreateSub(A, B);
|
|
|
|
|
2018-04-27 04:52:28 +08:00
|
|
|
// X % C0 + (( X / C0 ) % C1) * C0 => X % (C0 * C1)
|
|
|
|
if (Value *V = SimplifyAddWithRemainder(I)) return replaceInstUsesWith(I, V);
|
|
|
|
|
2012-09-27 18:14:43 +08:00
|
|
|
// A+B --> A|B iff A and B have no bits set in common.
|
2016-12-19 16:22:17 +08:00
|
|
|
if (haveNoCommonBitsSet(LHS, RHS, DL, &AC, &I, &DT))
|
2015-05-15 07:53:19 +08:00
|
|
|
return BinaryOperator::CreateOr(LHS, RHS);
|
2010-01-05 15:18:46 +08:00
|
|
|
|
2016-07-17 02:29:26 +08:00
|
|
|
// FIXME: We already did a check for ConstantInt RHS above this.
|
|
|
|
// FIXME: Is this pattern covered by another fold? No regression tests fail on
|
|
|
|
// removal.
|
2014-01-19 23:24:22 +08:00
|
|
|
if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) {
|
2010-01-05 15:18:46 +08:00
|
|
|
// (X & FF00) + xx00 -> (X+xx00) & FF00
|
2014-01-19 23:24:22 +08:00
|
|
|
Value *X;
|
|
|
|
ConstantInt *C2;
|
2010-01-05 15:18:46 +08:00
|
|
|
if (LHS->hasOneUse() &&
|
2011-02-10 13:14:58 +08: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-13 04:57:53 +08:00
|
|
|
|
2011-02-10 13:14:58 +08: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.
|
2017-07-08 07:16:26 +08:00
|
|
|
Value *NewAdd = Builder.CreateAdd(X, CRHS, LHS->getName());
|
2011-02-10 13:14:58 +08:00
|
|
|
return BinaryOperator::CreateAnd(NewAdd, C2);
|
2010-01-05 15:18:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 13:14:58 +08:00
|
|
|
if (match(FV, m_Zero()) && match(TV, m_Sub(m_Value(N), m_Specific(A))))
|
2010-01-05 15:18:46 +08:00
|
|
|
// Fold the add into the true select value.
|
|
|
|
return SelectInst::Create(SI->getCondition(), N, A);
|
2012-12-13 04:57:53 +08:00
|
|
|
|
2011-02-10 13:14:58 +08:00
|
|
|
if (match(TV, m_Zero()) && match(FV, m_Sub(m_Value(N), m_Specific(A))))
|
2010-01-05 15:18:46 +08:00
|
|
|
// Fold the add into the false select value.
|
|
|
|
return SelectInst::Create(SI->getCondition(), A, N);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-15 06:23:35 +08:00
|
|
|
if (Instruction *Ext = narrowMathIfNoOverflow(I))
|
2018-09-15 04:40:46 +08:00
|
|
|
return Ext;
|
2016-12-30 08:28:58 +08:00
|
|
|
|
2014-08-12 06:32:02 +08:00
|
|
|
// (add (xor A, B) (and A, B)) --> (or A, B)
|
2017-10-14 04:12:21 +08:00
|
|
|
// (add (and A, B) (xor A, B)) --> (or A, B)
|
[PatternMatch] Stabilize the matching order of commutative matchers
Summary:
Currently, we
1. match `LHS` matcher to the `first` operand of binary operator,
2. and then match `RHS` matcher to the `second` operand of binary operator.
If that does not match, we swap the `LHS` and `RHS` matchers:
1. match `RHS` matcher to the `first` operand of binary operator,
2. and then match `LHS` matcher to the `second` operand of binary operator.
This works ok.
But it complicates writing of commutative matchers, where one would like to match
(`m_Value()`) the value on one side, and use (`m_Specific()`) it on the other side.
This is additionally complicated by the fact that `m_Specific()` stores the `Value *`,
not `Value **`, so it won't work at all out of the box.
The last problem is trivially solved by adding a new `m_c_Specific()` that stores the
`Value **`, not `Value *`. I'm choosing to add a new matcher, not change the existing
one because i guess all the current users are ok with existing behavior,
and this additional pointer indirection may have performance drawbacks.
Also, i'm storing pointer, not reference, because for some mysterious-to-me reason
it did not work with the reference.
The first one appears trivial, too.
Currently, we
1. match `LHS` matcher to the `first` operand of binary operator,
2. and then match `RHS` matcher to the `second` operand of binary operator.
If that does not match, we swap the ~~`LHS` and `RHS` matchers~~ **operands**:
1. match ~~`RHS`~~ **`LHS`** matcher to the ~~`first`~~ **`second`** operand of binary operator,
2. and then match ~~`LHS`~~ **`RHS`** matcher to the ~~`second`~ **`first`** operand of binary operator.
Surprisingly, `$ ninja check-llvm` still passes with this.
But i expect the bots will disagree..
The motivational unittest is included.
I'd like to use this in D45664.
Reviewers: spatel, craig.topper, arsenm, RKSimon
Reviewed By: craig.topper
Subscribers: xbolva00, wdng, llvm-commits
Differential Revision: https://reviews.llvm.org/D45828
llvm-svn: 331085
2018-04-28 05:23:20 +08:00
|
|
|
if (match(&I, m_c_BinOp(m_Xor(m_Value(A), m_Value(B)),
|
|
|
|
m_c_And(m_Deferred(A), m_Deferred(B)))))
|
2017-10-14 04:12:21 +08:00
|
|
|
return BinaryOperator::CreateOr(A, B);
|
2012-04-27 07:29:14 +08:00
|
|
|
|
2014-08-12 06:32:02 +08:00
|
|
|
// (add (or A, B) (and A, B)) --> (add A, B)
|
2017-10-14 04:12:21 +08:00
|
|
|
// (add (and A, B) (or A, B)) --> (add A, B)
|
[PatternMatch] Stabilize the matching order of commutative matchers
Summary:
Currently, we
1. match `LHS` matcher to the `first` operand of binary operator,
2. and then match `RHS` matcher to the `second` operand of binary operator.
If that does not match, we swap the `LHS` and `RHS` matchers:
1. match `RHS` matcher to the `first` operand of binary operator,
2. and then match `LHS` matcher to the `second` operand of binary operator.
This works ok.
But it complicates writing of commutative matchers, where one would like to match
(`m_Value()`) the value on one side, and use (`m_Specific()`) it on the other side.
This is additionally complicated by the fact that `m_Specific()` stores the `Value *`,
not `Value **`, so it won't work at all out of the box.
The last problem is trivially solved by adding a new `m_c_Specific()` that stores the
`Value **`, not `Value *`. I'm choosing to add a new matcher, not change the existing
one because i guess all the current users are ok with existing behavior,
and this additional pointer indirection may have performance drawbacks.
Also, i'm storing pointer, not reference, because for some mysterious-to-me reason
it did not work with the reference.
The first one appears trivial, too.
Currently, we
1. match `LHS` matcher to the `first` operand of binary operator,
2. and then match `RHS` matcher to the `second` operand of binary operator.
If that does not match, we swap the ~~`LHS` and `RHS` matchers~~ **operands**:
1. match ~~`RHS`~~ **`LHS`** matcher to the ~~`first`~~ **`second`** operand of binary operator,
2. and then match ~~`LHS`~~ **`RHS`** matcher to the ~~`second`~ **`first`** operand of binary operator.
Surprisingly, `$ ninja check-llvm` still passes with this.
But i expect the bots will disagree..
The motivational unittest is included.
I'd like to use this in D45664.
Reviewers: spatel, craig.topper, arsenm, RKSimon
Reviewed By: craig.topper
Subscribers: xbolva00, wdng, llvm-commits
Differential Revision: https://reviews.llvm.org/D45828
llvm-svn: 331085
2018-04-28 05:23:20 +08:00
|
|
|
if (match(&I, m_c_BinOp(m_Or(m_Value(A), m_Value(B)),
|
|
|
|
m_c_And(m_Deferred(A), m_Deferred(B))))) {
|
2017-10-14 04:12:21 +08:00
|
|
|
I.setOperand(0, A);
|
|
|
|
I.setOperand(1, B);
|
|
|
|
return &I;
|
2014-08-12 06:32:02 +08:00
|
|
|
}
|
|
|
|
|
2017-05-22 14:25:31 +08:00
|
|
|
// TODO(jingyue): Consider willNotOverflowSignedAdd and
|
2017-05-15 10:44:08 +08:00
|
|
|
// willNotOverflowUnsignedAdd to reduce the number of invocations of
|
2014-06-17 08:42:07 +08:00
|
|
|
// computeKnownBits.
|
2018-07-13 09:18:07 +08:00
|
|
|
bool Changed = false;
|
2017-05-22 14:25:31 +08:00
|
|
|
if (!I.hasNoSignedWrap() && willNotOverflowSignedAdd(LHS, RHS, I)) {
|
2014-06-03 06:01:04 +08:00
|
|
|
Changed = true;
|
|
|
|
I.setHasNoSignedWrap(true);
|
|
|
|
}
|
2017-05-15 10:44:08 +08:00
|
|
|
if (!I.hasNoUnsignedWrap() && willNotOverflowUnsignedAdd(LHS, RHS, I)) {
|
2014-06-17 08:42:07 +08:00
|
|
|
Changed = true;
|
|
|
|
I.setHasNoUnsignedWrap(true);
|
|
|
|
}
|
2014-06-03 06:01:04 +08:00
|
|
|
|
[InstCombine] PR37603: low bit mask canonicalization
Summary:
This is [[ https://bugs.llvm.org/show_bug.cgi?id=37603 | PR37603 ]].
https://godbolt.org/g/VCMNpS
https://rise4fun.com/Alive/idM
When doing bit manipulations, it is quite common to calculate some bit mask,
and apply it to some value via `and`.
The typical C code looks like:
```
int mask_signed_add(int nbits) {
return (1 << nbits) - 1;
}
```
which is translated into (with `-O3`)
```
define dso_local i32 @mask_signed_add(int)(i32) local_unnamed_addr #0 {
%2 = shl i32 1, %0
%3 = add nsw i32 %2, -1
ret i32 %3
}
```
But there is a second, less readable variant:
```
int mask_signed_xor(int nbits) {
return ~(-(1 << nbits));
}
```
which is translated into (with `-O3`)
```
define dso_local i32 @mask_signed_xor(int)(i32) local_unnamed_addr #0 {
%2 = shl i32 -1, %0
%3 = xor i32 %2, -1
ret i32 %3
}
```
Since we created such a mask, it is quite likely that we will use it in `and` next.
And then we may get rid of `not` op by folding into `andn`.
But now that i have actually looked:
https://godbolt.org/g/VTUDmU
_some_ backend changes will be needed too.
We clearly loose `bzhi` recognition.
Reviewers: spatel, craig.topper, RKSimon
Reviewed By: spatel
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D47428
llvm-svn: 334127
2018-06-07 03:38:27 +08:00
|
|
|
if (Instruction *V = canonicalizeLowbitMask(I, Builder))
|
|
|
|
return V;
|
|
|
|
|
2019-03-27 01:50:08 +08:00
|
|
|
if (Instruction *SatAdd = foldToUnsignedSaturatedAdd(I))
|
|
|
|
return SatAdd;
|
|
|
|
|
2014-04-25 13:29:35 +08:00
|
|
|
return Changed ? &I : nullptr;
|
2010-01-05 15:18:46 +08:00
|
|
|
}
|
|
|
|
|
2019-07-26 19:19:18 +08:00
|
|
|
/// Eliminate an op from a linear interpolation (lerp) pattern.
|
|
|
|
static Instruction *factorizeLerp(BinaryOperator &I,
|
|
|
|
InstCombiner::BuilderTy &Builder) {
|
|
|
|
Value *X, *Y, *Z;
|
|
|
|
if (!match(&I, m_c_FAdd(m_OneUse(m_c_FMul(m_Value(Y),
|
|
|
|
m_OneUse(m_FSub(m_FPOne(),
|
|
|
|
m_Value(Z))))),
|
|
|
|
m_OneUse(m_c_FMul(m_Value(X), m_Deferred(Z))))))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// (Y * (1.0 - Z)) + (X * Z) --> Y + Z * (X - Y) [8 commuted variants]
|
|
|
|
Value *XY = Builder.CreateFSubFMF(X, Y, &I);
|
|
|
|
Value *MulZ = Builder.CreateFMulFMF(Z, XY, &I);
|
|
|
|
return BinaryOperator::CreateFAddFMF(Y, MulZ, &I);
|
|
|
|
}
|
|
|
|
|
2018-08-12 23:48:26 +08:00
|
|
|
/// Factor a common operand out of fadd/fsub of fmul/fdiv.
|
|
|
|
static Instruction *factorizeFAddFSub(BinaryOperator &I,
|
|
|
|
InstCombiner::BuilderTy &Builder) {
|
|
|
|
assert((I.getOpcode() == Instruction::FAdd ||
|
|
|
|
I.getOpcode() == Instruction::FSub) && "Expecting fadd/fsub");
|
|
|
|
assert(I.hasAllowReassoc() && I.hasNoSignedZeros() &&
|
|
|
|
"FP factorization requires FMF");
|
2019-07-26 19:19:18 +08:00
|
|
|
|
|
|
|
if (Instruction *Lerp = factorizeLerp(I, Builder))
|
|
|
|
return Lerp;
|
|
|
|
|
2018-08-12 23:48:26 +08:00
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
|
|
|
Value *X, *Y, *Z;
|
|
|
|
bool IsFMul;
|
|
|
|
if ((match(Op0, m_OneUse(m_FMul(m_Value(X), m_Value(Z)))) &&
|
|
|
|
match(Op1, m_OneUse(m_c_FMul(m_Value(Y), m_Specific(Z))))) ||
|
|
|
|
(match(Op0, m_OneUse(m_FMul(m_Value(Z), m_Value(X)))) &&
|
|
|
|
match(Op1, m_OneUse(m_c_FMul(m_Value(Y), m_Specific(Z))))))
|
|
|
|
IsFMul = true;
|
|
|
|
else if (match(Op0, m_OneUse(m_FDiv(m_Value(X), m_Value(Z)))) &&
|
|
|
|
match(Op1, m_OneUse(m_FDiv(m_Value(Y), m_Specific(Z)))))
|
|
|
|
IsFMul = false;
|
|
|
|
else
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// (X * Z) + (Y * Z) --> (X + Y) * Z
|
|
|
|
// (X * Z) - (Y * Z) --> (X - Y) * Z
|
|
|
|
// (X / Z) + (Y / Z) --> (X + Y) / Z
|
|
|
|
// (X / Z) - (Y / Z) --> (X - Y) / Z
|
|
|
|
bool IsFAdd = I.getOpcode() == Instruction::FAdd;
|
|
|
|
Value *XY = IsFAdd ? Builder.CreateFAddFMF(X, Y, &I)
|
|
|
|
: Builder.CreateFSubFMF(X, Y, &I);
|
|
|
|
|
|
|
|
// Bail out if we just created a denormal constant.
|
|
|
|
// TODO: This is copied from a previous implementation. Is it necessary?
|
|
|
|
const APFloat *C;
|
|
|
|
if (match(XY, m_APFloat(C)) && !C->isNormal())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return IsFMul ? BinaryOperator::CreateFMulFMF(XY, Z, &I)
|
|
|
|
: BinaryOperator::CreateFDivFMF(XY, Z, &I);
|
|
|
|
}
|
|
|
|
|
2010-01-05 15:18:46 +08:00
|
|
|
Instruction *InstCombiner::visitFAdd(BinaryOperator &I) {
|
2018-06-22 01:06:36 +08:00
|
|
|
if (Value *V = SimplifyFAddInst(I.getOperand(0), I.getOperand(1),
|
|
|
|
I.getFastMathFlags(),
|
2017-06-09 11:21:29 +08:00
|
|
|
SQ.getWithInstruction(&I)))
|
2016-02-02 06:23:39 +08:00
|
|
|
return replaceInstUsesWith(I, V);
|
2010-01-05 15:18:46 +08:00
|
|
|
|
2018-07-13 09:18:07 +08:00
|
|
|
if (SimplifyAssociativeOrCommutative(I))
|
|
|
|
return &I;
|
|
|
|
|
2018-10-03 23:20:58 +08:00
|
|
|
if (Instruction *X = foldVectorBinop(I))
|
2018-06-03 00:27:44 +08:00
|
|
|
return X;
|
|
|
|
|
2018-03-01 00:36:24 +08:00
|
|
|
if (Instruction *FoldedFAdd = foldBinOpIntoSelectOrPhi(I))
|
|
|
|
return FoldedFAdd;
|
2012-12-15 06:08:26 +08:00
|
|
|
|
2018-04-16 22:13:57 +08:00
|
|
|
// (-X) + Y --> Y - X
|
2019-07-29 21:20:46 +08:00
|
|
|
Value *X, *Y;
|
|
|
|
if (match(&I, m_c_FAdd(m_FNeg(m_Value(X)), m_Value(Y))))
|
|
|
|
return BinaryOperator::CreateFSubFMF(Y, X, &I);
|
2010-01-05 15:18:46 +08:00
|
|
|
|
2019-07-29 21:50:25 +08:00
|
|
|
// Similar to above, but look through fmul/fdiv for the negated term.
|
|
|
|
// (-X * Y) + Z --> Z - (X * Y) [4 commuted variants]
|
|
|
|
Value *Z;
|
|
|
|
if (match(&I, m_c_FAdd(m_OneUse(m_c_FMul(m_FNeg(m_Value(X)), m_Value(Y))),
|
|
|
|
m_Value(Z)))) {
|
|
|
|
Value *XY = Builder.CreateFMulFMF(X, Y, &I);
|
|
|
|
return BinaryOperator::CreateFSubFMF(Z, XY, &I);
|
|
|
|
}
|
|
|
|
// (-X / Y) + Z --> Z - (X / Y) [2 commuted variants]
|
|
|
|
// (X / -Y) + Z --> Z - (X / Y) [2 commuted variants]
|
|
|
|
if (match(&I, m_c_FAdd(m_OneUse(m_FDiv(m_FNeg(m_Value(X)), m_Value(Y))),
|
|
|
|
m_Value(Z))) ||
|
|
|
|
match(&I, m_c_FAdd(m_OneUse(m_FDiv(m_Value(X), m_FNeg(m_Value(Y)))),
|
|
|
|
m_Value(Z)))) {
|
|
|
|
Value *XY = Builder.CreateFDivFMF(X, Y, &I);
|
|
|
|
return BinaryOperator::CreateFSubFMF(Z, XY, &I);
|
|
|
|
}
|
|
|
|
|
2010-03-02 09:11:08 +08:00
|
|
|
// Check for (fadd double (sitofp x), y), see if we can merge this into an
|
2010-01-05 15:18:46 +08:00
|
|
|
// integer add followed by a promotion.
|
2019-07-29 21:20:46 +08:00
|
|
|
Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
|
2010-01-05 15:18:46 +08:00
|
|
|
if (SIToFPInst *LHSConv = dyn_cast<SIToFPInst>(LHS)) {
|
2017-03-21 19:32:15 +08:00
|
|
|
Value *LHSIntVal = LHSConv->getOperand(0);
|
2017-04-22 02:45:25 +08:00
|
|
|
Type *FPType = LHSConv->getType();
|
|
|
|
|
|
|
|
// TODO: This check is overly conservative. In many cases known bits
|
|
|
|
// analysis can tell us that the result of the addition has less significant
|
|
|
|
// bits than the integer type can hold.
|
|
|
|
auto IsValidPromotion = [](Type *FTy, Type *ITy) {
|
2017-04-22 15:24:52 +08:00
|
|
|
Type *FScalarTy = FTy->getScalarType();
|
|
|
|
Type *IScalarTy = ITy->getScalarType();
|
|
|
|
|
2017-04-22 02:45:25 +08:00
|
|
|
// Do we have enough bits in the significand to represent the result of
|
|
|
|
// the integer addition?
|
|
|
|
unsigned MaxRepresentableBits =
|
2017-04-22 15:24:52 +08:00
|
|
|
APFloat::semanticsPrecision(FScalarTy->getFltSemantics());
|
|
|
|
return IScalarTy->getIntegerBitWidth() <= MaxRepresentableBits;
|
2017-04-22 02:45:25 +08:00
|
|
|
};
|
2017-03-21 19:32:15 +08:00
|
|
|
|
2010-03-02 09:11:08 +08:00
|
|
|
// (fadd double (sitofp x), fpcst) --> (sitofp (add int x, intcst))
|
2010-01-05 15:18:46 +08: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.
|
2017-04-22 02:45:25 +08:00
|
|
|
if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS))
|
|
|
|
if (IsValidPromotion(FPType, LHSIntVal->getType())) {
|
|
|
|
Constant *CI =
|
|
|
|
ConstantExpr::getFPToSI(CFP, LHSIntVal->getType());
|
|
|
|
if (LHSConv->hasOneUse() &&
|
|
|
|
ConstantExpr::getSIToFP(CI, I.getType()) == CFP &&
|
2017-05-22 14:25:31 +08:00
|
|
|
willNotOverflowSignedAdd(LHSIntVal, CI, I)) {
|
2017-04-22 02:45:25 +08:00
|
|
|
// Insert the new integer add.
|
2017-07-08 07:16:26 +08:00
|
|
|
Value *NewAdd = Builder.CreateNSWAdd(LHSIntVal, CI, "addconv");
|
2017-04-22 02:45:25 +08:00
|
|
|
return new SIToFPInst(NewAdd, I.getType());
|
|
|
|
}
|
2010-01-05 15:18:46 +08:00
|
|
|
}
|
2012-12-13 04:57:53 +08:00
|
|
|
|
2010-03-02 09:11:08 +08:00
|
|
|
// (fadd double (sitofp x), (sitofp y)) --> (sitofp (add int x, y))
|
2010-01-05 15:18:46 +08:00
|
|
|
if (SIToFPInst *RHSConv = dyn_cast<SIToFPInst>(RHS)) {
|
2017-03-21 19:32:15 +08:00
|
|
|
Value *RHSIntVal = RHSConv->getOperand(0);
|
2017-04-22 02:45:25 +08:00
|
|
|
// It's enough to check LHS types only because we require int types to
|
|
|
|
// be the same for this transform.
|
|
|
|
if (IsValidPromotion(FPType, LHSIntVal->getType())) {
|
|
|
|
// Only do this if x/y have the same type, if at least 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 (LHSIntVal->getType() == RHSIntVal->getType() &&
|
|
|
|
(LHSConv->hasOneUse() || RHSConv->hasOneUse()) &&
|
2017-05-22 14:25:31 +08:00
|
|
|
willNotOverflowSignedAdd(LHSIntVal, RHSIntVal, I)) {
|
2017-04-22 02:45:25 +08:00
|
|
|
// Insert the new integer add.
|
2017-07-08 07:16:26 +08:00
|
|
|
Value *NewAdd = Builder.CreateNSWAdd(LHSIntVal, RHSIntVal, "addconv");
|
2017-04-22 02:45:25 +08:00
|
|
|
return new SIToFPInst(NewAdd, I.getType());
|
|
|
|
}
|
2010-01-05 15:18:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-13 04:57:53 +08:00
|
|
|
|
2017-09-21 01:32:16 +08:00
|
|
|
// Handle specials cases for FAdd with selects feeding the operation
|
|
|
|
if (Value *V = SimplifySelectsFeedingBinaryOp(I, LHS, RHS))
|
|
|
|
return replaceInstUsesWith(I, V);
|
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
llvm-svn: 181216
2013-05-07 00:55:50 +08:00
|
|
|
|
2018-04-15 03:18:28 +08:00
|
|
|
if (I.hasAllowReassoc() && I.hasNoSignedZeros()) {
|
2018-08-12 23:48:26 +08:00
|
|
|
if (Instruction *F = factorizeFAddFSub(I, Builder))
|
|
|
|
return F;
|
2012-12-19 07:10:12 +08:00
|
|
|
if (Value *V = FAddCombine(Builder).simplify(&I))
|
2016-02-02 06:23:39 +08:00
|
|
|
return replaceInstUsesWith(I, V);
|
2012-12-19 07:10:12 +08:00
|
|
|
}
|
|
|
|
|
2018-07-13 09:18:07 +08:00
|
|
|
return nullptr;
|
2010-01-05 15:18:46 +08: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 12:54:35 +08:00
|
|
|
Type *Ty) {
|
2010-01-05 15:18:46 +08: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 13:29:35 +08:00
|
|
|
GEPOperator *GEP1 = nullptr, *GEP2 = nullptr;
|
2012-02-20 22:34:57 +08:00
|
|
|
|
2010-01-05 15:18:46 +08:00
|
|
|
// For now we require one side to be the base pointer "A" or a constant
|
2012-02-20 22:34:57 +08:00
|
|
|
// GEP derived from it.
|
|
|
|
if (GEPOperator *LHSGEP = dyn_cast<GEPOperator>(LHS)) {
|
2010-01-05 15:18:46 +08:00
|
|
|
// (gep X, ...) - X
|
|
|
|
if (LHSGEP->getOperand(0) == RHS) {
|
2012-02-20 22:34:57 +08:00
|
|
|
GEP1 = LHSGEP;
|
2010-01-05 15:18:46 +08:00
|
|
|
Swapped = false;
|
2012-02-20 22:34:57 +08: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 15:18:46 +08:00
|
|
|
Swapped = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-13 04:57:53 +08:00
|
|
|
|
2012-02-20 22:34:57 +08:00
|
|
|
if (GEPOperator *RHSGEP = dyn_cast<GEPOperator>(RHS)) {
|
2010-01-05 15:18:46 +08:00
|
|
|
// X - (gep X, ...)
|
|
|
|
if (RHSGEP->getOperand(0) == LHS) {
|
2012-02-20 22:34:57 +08:00
|
|
|
GEP1 = RHSGEP;
|
2010-01-05 15:18:46 +08:00
|
|
|
Swapped = true;
|
2012-02-20 22:34:57 +08: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 15:18:46 +08:00
|
|
|
Swapped = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-13 04:57:53 +08:00
|
|
|
|
2017-07-28 02:27:11 +08:00
|
|
|
if (!GEP1)
|
|
|
|
// No GEP found.
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2012-12-13 04:57:53 +08:00
|
|
|
|
2017-07-28 02:27:11 +08:00
|
|
|
if (GEP2) {
|
|
|
|
// (gep X, ...) - (gep X, ...)
|
|
|
|
//
|
|
|
|
// Avoid duplicating the arithmetic if there are more than one non-constant
|
|
|
|
// indices between the two GEPs and either GEP has a non-constant index and
|
|
|
|
// multiple users. If zero non-constant index, the result is a constant and
|
|
|
|
// there is no duplication. If one non-constant index, the result is an add
|
|
|
|
// or sub with a constant, which is no larger than the original code, and
|
|
|
|
// there's no duplicated arithmetic, even if either GEP has multiple
|
|
|
|
// users. If more than one non-constant indices combined, as long as the GEP
|
|
|
|
// with at least one non-constant index doesn't have multiple users, there
|
|
|
|
// is no duplication.
|
|
|
|
unsigned NumNonConstantIndices1 = GEP1->countNonConstantIndices();
|
|
|
|
unsigned NumNonConstantIndices2 = GEP2->countNonConstantIndices();
|
|
|
|
if (NumNonConstantIndices1 + NumNonConstantIndices2 > 1 &&
|
|
|
|
((NumNonConstantIndices1 > 0 && !GEP1->hasOneUse()) ||
|
|
|
|
(NumNonConstantIndices2 > 0 && !GEP2->hasOneUse()))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-05 15:18:46 +08:00
|
|
|
// Emit the offset of the GEP and an intptr_t.
|
2012-02-20 22:34:57 +08:00
|
|
|
Value *Result = EmitGEPOffset(GEP1);
|
2012-12-13 04:57:53 +08:00
|
|
|
|
2010-01-05 15:18:46 +08: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 22:34:57 +08:00
|
|
|
if (GEP2) {
|
|
|
|
Value *Offset = EmitGEPOffset(GEP2);
|
2017-07-08 07:16:26 +08:00
|
|
|
Result = Builder.CreateSub(Result, Offset);
|
2010-01-05 15:18:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we have p - gep(p, ...) then we have to negate the result.
|
|
|
|
if (Swapped)
|
2017-07-08 07:16:26 +08:00
|
|
|
Result = Builder.CreateNeg(Result, "diff.neg");
|
2010-01-05 15:18:46 +08:00
|
|
|
|
2017-07-08 07:16:26 +08:00
|
|
|
return Builder.CreateIntCast(Result, Ty, true);
|
2010-01-05 15:18:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InstCombiner::visitSub(BinaryOperator &I) {
|
2018-06-22 01:06:36 +08:00
|
|
|
if (Value *V = SimplifySubInst(I.getOperand(0), I.getOperand(1),
|
|
|
|
I.hasNoSignedWrap(), I.hasNoUnsignedWrap(),
|
|
|
|
SQ.getWithInstruction(&I)))
|
2016-02-02 06:23:39 +08:00
|
|
|
return replaceInstUsesWith(I, V);
|
2010-01-05 15:18:46 +08:00
|
|
|
|
2018-10-03 23:20:58 +08:00
|
|
|
if (Instruction *X = foldVectorBinop(I))
|
2018-06-03 00:27:44 +08:00
|
|
|
return X;
|
|
|
|
|
2010-12-22 21:36:08 +08:00
|
|
|
// (A*B)-(A*C) -> A*(B-C) etc
|
|
|
|
if (Value *V = SimplifyUsingDistributiveLaws(I))
|
2016-02-02 06:23:39 +08:00
|
|
|
return replaceInstUsesWith(I, V);
|
2010-12-22 21:36:08 +08:00
|
|
|
|
2014-07-31 12:49:29 +08:00
|
|
|
// If this is a 'B = x-(-A)', change to B = x+A.
|
2018-06-22 01:06:36 +08:00
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
2010-01-05 15:18:46 +08:00
|
|
|
if (Value *V = dyn_castNegVal(Op1)) {
|
|
|
|
BinaryOperator *Res = BinaryOperator::CreateAdd(Op0, V);
|
2014-07-31 12:49:29 +08: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-23 00:41:23 +08:00
|
|
|
} else {
|
|
|
|
if (cast<Constant>(Op1)->isNotMinSignedValue() && I.hasNoSignedWrap())
|
|
|
|
Res->setHasNoSignedWrap(true);
|
2014-07-31 12:49:29 +08:00
|
|
|
}
|
|
|
|
|
2010-01-05 15:18:46 +08:00
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2017-07-09 15:04:03 +08:00
|
|
|
if (I.getType()->isIntOrIntVectorTy(1))
|
2010-01-05 15:18:46 +08:00
|
|
|
return BinaryOperator::CreateXor(Op0, Op1);
|
2011-02-10 13:14:58 +08:00
|
|
|
|
|
|
|
// Replace (-1 - A) with (~A).
|
|
|
|
if (match(Op0, m_AllOnes()))
|
|
|
|
return BinaryOperator::CreateNot(Op1);
|
2012-12-13 04:57:53 +08:00
|
|
|
|
2018-03-04 01:53:25 +08:00
|
|
|
// (~X) - (~Y) --> Y - X
|
|
|
|
Value *X, *Y;
|
|
|
|
if (match(Op0, m_Not(m_Value(X))) && match(Op1, m_Not(m_Value(Y))))
|
|
|
|
return BinaryOperator::CreateSub(Y, X);
|
|
|
|
|
2018-07-30 02:13:16 +08:00
|
|
|
// (X + -1) - Y --> ~Y + X
|
|
|
|
if (match(Op0, m_OneUse(m_Add(m_Value(X), m_AllOnes()))))
|
|
|
|
return BinaryOperator::CreateAdd(Builder.CreateNot(Op1), X);
|
|
|
|
|
|
|
|
// Y - (X + 1) --> ~X + Y
|
|
|
|
if (match(Op1, m_OneUse(m_Add(m_Value(X), m_One()))))
|
|
|
|
return BinaryOperator::CreateAdd(Builder.CreateNot(X), Op0);
|
|
|
|
|
2019-07-03 17:41:50 +08:00
|
|
|
// Y - ~X --> (X + 1) + Y
|
|
|
|
if (match(Op1, m_OneUse(m_Not(m_Value(X))))) {
|
|
|
|
return BinaryOperator::CreateAdd(
|
|
|
|
Builder.CreateAdd(Op0, ConstantInt::get(I.getType(), 1)), X);
|
|
|
|
}
|
|
|
|
|
2014-01-19 23:24:22 +08:00
|
|
|
if (Constant *C = dyn_cast<Constant>(Op0)) {
|
2018-06-04 00:35:26 +08:00
|
|
|
bool IsNegate = match(C, m_ZeroInt());
|
2017-12-07 05:22:57 +08:00
|
|
|
Value *X;
|
2018-06-04 00:35:26 +08:00
|
|
|
if (match(Op1, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) {
|
|
|
|
// 0 - (zext bool) --> sext bool
|
|
|
|
// C - (zext bool) --> bool ? C - 1 : C
|
|
|
|
if (IsNegate)
|
|
|
|
return CastInst::CreateSExtOrBitCast(X, I.getType());
|
2017-12-07 05:22:57 +08:00
|
|
|
return SelectInst::Create(X, SubOne(C), C);
|
2018-06-04 00:35:26 +08:00
|
|
|
}
|
|
|
|
if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) {
|
|
|
|
// 0 - (sext bool) --> zext bool
|
|
|
|
// C - (sext bool) --> bool ? C + 1 : C
|
|
|
|
if (IsNegate)
|
|
|
|
return CastInst::CreateZExtOrBitCast(X, I.getType());
|
|
|
|
return SelectInst::Create(X, AddOne(C), C);
|
|
|
|
}
|
2017-12-07 05:22:57 +08:00
|
|
|
|
2010-01-05 15:18:46 +08:00
|
|
|
// C - ~X == X + (1+C)
|
|
|
|
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;
|
|
|
|
|
2017-04-15 03:20:12 +08:00
|
|
|
// Try to fold constant sub into PHI values.
|
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(Op1))
|
|
|
|
if (Instruction *R = foldOpIntoPhi(I, PN))
|
|
|
|
return R;
|
|
|
|
|
2014-01-19 23:24:22 +08:00
|
|
|
Constant *C2;
|
2019-05-31 17:47:16 +08:00
|
|
|
|
|
|
|
// C-(C2-X) --> X+(C-C2)
|
|
|
|
if (match(Op1, m_Sub(m_Constant(C2), m_Value(X))))
|
|
|
|
return BinaryOperator::CreateAdd(X, ConstantExpr::getSub(C, C2));
|
|
|
|
|
|
|
|
// C-(X+C2) --> (C-C2)-X
|
2014-01-19 23:24:22 +08:00
|
|
|
if (match(Op1, m_Add(m_Value(X), m_Constant(C2))))
|
2011-02-10 13:14:58 +08:00
|
|
|
return BinaryOperator::CreateSub(ConstantExpr::getSub(C, C2), X);
|
2010-01-05 15:18:46 +08:00
|
|
|
}
|
|
|
|
|
2016-10-15 00:31:54 +08:00
|
|
|
const APInt *Op0C;
|
|
|
|
if (match(Op0, m_APInt(Op0C))) {
|
|
|
|
unsigned BitWidth = I.getType()->getScalarSizeInBits();
|
|
|
|
|
2014-01-19 23:24:22 +08:00
|
|
|
// -(X >>u 31) -> (X >>s 31)
|
|
|
|
// -(X >>s 31) -> (X >>u 31)
|
2017-06-07 15:40:37 +08:00
|
|
|
if (Op0C->isNullValue()) {
|
2014-11-03 13:53:55 +08:00
|
|
|
Value *X;
|
2016-10-15 00:31:54 +08:00
|
|
|
const APInt *ShAmt;
|
|
|
|
if (match(Op1, m_LShr(m_Value(X), m_APInt(ShAmt))) &&
|
|
|
|
*ShAmt == BitWidth - 1) {
|
|
|
|
Value *ShAmtOp = cast<Instruction>(Op1)->getOperand(1);
|
|
|
|
return BinaryOperator::CreateAShr(X, ShAmtOp);
|
|
|
|
}
|
|
|
|
if (match(Op1, m_AShr(m_Value(X), m_APInt(ShAmt))) &&
|
|
|
|
*ShAmt == BitWidth - 1) {
|
|
|
|
Value *ShAmtOp = cast<Instruction>(Op1)->getOperand(1);
|
|
|
|
return BinaryOperator::CreateLShr(X, ShAmtOp);
|
|
|
|
}
|
2018-05-24 01:29:03 +08:00
|
|
|
|
|
|
|
if (Op1->hasOneUse()) {
|
|
|
|
Value *LHS, *RHS;
|
|
|
|
SelectPatternFlavor SPF = matchSelectPattern(Op1, LHS, RHS).Flavor;
|
|
|
|
if (SPF == SPF_ABS || SPF == SPF_NABS) {
|
|
|
|
// This is a negate of an ABS/NABS pattern. Just swap the operands
|
|
|
|
// of the select.
|
2019-08-01 20:31:35 +08:00
|
|
|
cast<SelectInst>(Op1)->swapValues();
|
2018-05-24 01:29:03 +08:00
|
|
|
// Don't swap prof metadata, we didn't change the branch behavior.
|
2019-08-01 20:31:35 +08:00
|
|
|
return replaceInstUsesWith(I, Op1);
|
2018-05-24 01:29:03 +08:00
|
|
|
}
|
|
|
|
}
|
2014-01-19 23:24:22 +08:00
|
|
|
}
|
2015-05-01 06:04:26 +08:00
|
|
|
|
|
|
|
// Turn this into a xor if LHS is 2^n-1 and the remaining bits are known
|
|
|
|
// zero.
|
2017-04-07 05:06:03 +08:00
|
|
|
if (Op0C->isMask()) {
|
2017-05-25 00:53:07 +08:00
|
|
|
KnownBits RHSKnown = computeKnownBits(Op1, 0, &I);
|
2017-04-27 00:39:58 +08:00
|
|
|
if ((*Op0C | RHSKnown.Zero).isAllOnesValue())
|
2016-10-15 00:31:54 +08:00
|
|
|
return BinaryOperator::CreateXor(Op1, Op0);
|
2015-05-01 06:04:26 +08:00
|
|
|
}
|
2014-01-19 23:24:22 +08:00
|
|
|
}
|
|
|
|
|
2014-11-03 13:53:55 +08:00
|
|
|
{
|
2014-10-08 16:37:49 +08:00
|
|
|
Value *Y;
|
2011-02-10 13:14:58 +08:00
|
|
|
// X-(X+Y) == -Y X-(Y+X) == -Y
|
2017-04-11 00:59:40 +08:00
|
|
|
if (match(Op1, m_c_Add(m_Specific(Op0), m_Value(Y))))
|
2011-02-10 13:14:58 +08:00
|
|
|
return BinaryOperator::CreateNeg(Y);
|
2012-12-13 04:57:53 +08:00
|
|
|
|
2011-02-10 13:14:58 +08:00
|
|
|
// (X-Y)-X == -Y
|
|
|
|
if (match(Op0, m_Sub(m_Specific(Op1), m_Value(Y))))
|
|
|
|
return BinaryOperator::CreateNeg(Y);
|
|
|
|
}
|
2012-12-13 04:57:53 +08:00
|
|
|
|
2019-09-04 20:00:33 +08:00
|
|
|
// (sub (or A, B) (and A, B)) --> (xor A, B)
|
|
|
|
{
|
|
|
|
Value *A, *B;
|
|
|
|
if (match(Op1, m_And(m_Value(A), m_Value(B))) &&
|
|
|
|
match(Op0, m_c_Or(m_Specific(A), m_Specific(B))))
|
|
|
|
return BinaryOperator::CreateXor(A, B);
|
|
|
|
}
|
|
|
|
|
[InstCombine] Fold sub (and A, B) (or A, B)) to neg (xor A, B)
Summary:
```
Name: sub(and(x, y), or(x, y)) -> neg(xor(x, y))
%or = or i32 %y, %x
%and = and i32 %x, %y
%sub = sub i32 %and, %or
=>
%sub1 = xor i32 %x, %y
%sub = sub i32 0, %sub1
Optimization: sub(and(x, y), or(x, y)) -> neg(xor(x, y))
Done: 1
Optimization is correct!
```
https://rise4fun.com/Alive/VI6
Found by @lebedev.ri. Also author of the proof.
Reviewers: lebedev.ri, spatel
Reviewed By: lebedev.ri
Subscribers: llvm-commits, lebedev.ri
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D67155
llvm-svn: 370934
2019-09-05 01:30:53 +08:00
|
|
|
// (sub (and A, B) (or A, B)) --> neg (xor A, B)
|
|
|
|
{
|
|
|
|
Value *A, *B;
|
|
|
|
if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
|
|
|
|
match(Op1, m_c_Or(m_Specific(A), m_Specific(B))) &&
|
|
|
|
(Op0->hasOneUse() || Op1->hasOneUse()))
|
|
|
|
return BinaryOperator::CreateNeg(Builder.CreateXor(A, B));
|
|
|
|
}
|
|
|
|
|
2017-07-27 05:54:43 +08:00
|
|
|
// (sub (or A, B), (xor A, B)) --> (and A, B)
|
2014-10-19 16:32:32 +08:00
|
|
|
{
|
2017-04-11 02:09:25 +08:00
|
|
|
Value *A, *B;
|
2014-10-19 16:32:32 +08:00
|
|
|
if (match(Op1, m_Xor(m_Value(A), m_Value(B))) &&
|
2017-04-11 02:09:25 +08:00
|
|
|
match(Op0, m_c_Or(m_Specific(A), m_Specific(B))))
|
2014-10-19 16:32:32 +08:00
|
|
|
return BinaryOperator::CreateAnd(A, B);
|
|
|
|
}
|
|
|
|
|
[InstCombine] sub(xor(x, y), or(x, y)) -> neg(and(x, y))
Summary:
```
Name: sub(xor(x, y), or(x, y)) -> neg(and(x, y))
%or = or i32 %y, %x
%xor = xor i32 %x, %y
%sub = sub i32 %xor, %or
=>
%sub1 = and i32 %x, %y
%sub = sub i32 0, %sub1
Optimization: sub(xor(x, y), or(x, y)) -> neg(and(x, y))
Done: 1
Optimization is correct!
```
https://rise4fun.com/Alive/8OI
Reviewers: lebedev.ri
Reviewed By: lebedev.ri
Subscribers: llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D67188
llvm-svn: 370945
2019-09-05 02:03:21 +08:00
|
|
|
// (sub (xor A, B) (or A, B)) --> neg (and A, B)
|
|
|
|
{
|
|
|
|
Value *A, *B;
|
|
|
|
if (match(Op0, m_Xor(m_Value(A), m_Value(B))) &&
|
|
|
|
match(Op1, m_c_Or(m_Specific(A), m_Specific(B))) &&
|
|
|
|
(Op0->hasOneUse() || Op1->hasOneUse()))
|
|
|
|
return BinaryOperator::CreateNeg(Builder.CreateAnd(A, B));
|
|
|
|
}
|
|
|
|
|
2017-04-11 02:09:25 +08:00
|
|
|
{
|
|
|
|
Value *Y;
|
2014-11-03 13:53:55 +08:00
|
|
|
// ((X | Y) - X) --> (~X & Y)
|
2017-04-11 02:09:25 +08:00
|
|
|
if (match(Op0, m_OneUse(m_c_Or(m_Value(Y), m_Specific(Op1)))))
|
2014-11-03 13:53:55 +08:00
|
|
|
return BinaryOperator::CreateAnd(
|
2017-07-08 07:16:26 +08:00
|
|
|
Y, Builder.CreateNot(Op1, Op1->getName() + ".not"));
|
2014-11-03 13:53:55 +08:00
|
|
|
}
|
|
|
|
|
2011-02-10 13:14:58 +08:00
|
|
|
if (Op1->hasOneUse()) {
|
2014-04-25 13:29:35 +08:00
|
|
|
Value *X = nullptr, *Y = nullptr, *Z = nullptr;
|
|
|
|
Constant *C = nullptr;
|
2011-02-10 13:14:58 +08:00
|
|
|
|
|
|
|
// (X - (Y - Z)) --> (X + (Z - Y)).
|
|
|
|
if (match(Op1, m_Sub(m_Value(Y), m_Value(Z))))
|
|
|
|
return BinaryOperator::CreateAdd(Op0,
|
2017-07-08 07:16:26 +08:00
|
|
|
Builder.CreateSub(Z, Y, Op1->getName()));
|
2011-02-10 13:14:58 +08:00
|
|
|
|
|
|
|
// (X - (X & Y)) --> (X & ~Y)
|
2017-04-11 02:09:25 +08:00
|
|
|
if (match(Op1, m_c_And(m_Value(Y), m_Specific(Op0))))
|
2011-02-10 13:14:58 +08:00
|
|
|
return BinaryOperator::CreateAnd(Op0,
|
2017-07-08 07:16:26 +08:00
|
|
|
Builder.CreateNot(Y, Y->getName() + ".not"));
|
2012-12-13 04:57:53 +08:00
|
|
|
|
2014-07-02 14:07:09 +08:00
|
|
|
// 0 - (X sdiv C) -> (X sdiv -C) provided the negation doesn't overflow.
|
2019-04-09 22:09:06 +08:00
|
|
|
// TODO: This could be extended to match arbitrary vector constants.
|
|
|
|
const APInt *DivC;
|
|
|
|
if (match(Op0, m_Zero()) && match(Op1, m_SDiv(m_Value(X), m_APInt(DivC))) &&
|
|
|
|
!DivC->isMinSignedValue() && *DivC != 1) {
|
|
|
|
Constant *NegDivC = ConstantInt::get(I.getType(), -(*DivC));
|
|
|
|
Instruction *BO = BinaryOperator::CreateSDiv(X, NegDivC);
|
2019-04-08 20:08:03 +08:00
|
|
|
BO->setIsExact(cast<BinaryOperator>(Op1)->isExact());
|
|
|
|
return BO;
|
|
|
|
}
|
2011-02-10 13:14:58 +08:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2016-10-14 23:24:31 +08:00
|
|
|
// Subtracting -1/0 is the same as adding 1/0:
|
|
|
|
// sub [nsw] Op0, sext(bool Y) -> add [nsw] Op0, zext(bool Y)
|
|
|
|
// 'nuw' is dropped in favor of the canonical form.
|
|
|
|
if (match(Op1, m_SExt(m_Value(Y))) &&
|
|
|
|
Y->getType()->getScalarSizeInBits() == 1) {
|
2017-07-08 07:16:26 +08:00
|
|
|
Value *Zext = Builder.CreateZExt(Y, I.getType());
|
2016-10-14 23:24:31 +08:00
|
|
|
BinaryOperator *Add = BinaryOperator::CreateAdd(Op0, Zext);
|
|
|
|
Add->setHasNoSignedWrap(I.hasNoSignedWrap());
|
|
|
|
return Add;
|
|
|
|
}
|
|
|
|
|
2011-02-10 13:14:58 +08:00
|
|
|
// X - A*-B -> X + A*B
|
|
|
|
// X - -A*B -> X + A*B
|
|
|
|
Value *A, *B;
|
2017-04-11 02:09:25 +08:00
|
|
|
if (match(Op1, m_c_Mul(m_Value(A), m_Neg(m_Value(B)))))
|
2017-07-08 07:16:26 +08:00
|
|
|
return BinaryOperator::CreateAdd(Op0, Builder.CreateMul(A, B));
|
2012-12-13 04:57:53 +08:00
|
|
|
|
2019-01-15 19:18:21 +08:00
|
|
|
// X - A*C -> X + A*-C
|
2017-04-11 02:09:25 +08:00
|
|
|
// No need to handle commuted multiply because multiply handling will
|
|
|
|
// ensure constant will be move to the right hand side.
|
2019-01-15 19:18:21 +08:00
|
|
|
if (match(Op1, m_Mul(m_Value(A), m_Constant(C))) && !isa<ConstantExpr>(C)) {
|
|
|
|
Value *NewMul = Builder.CreateMul(A, ConstantExpr::getNeg(C));
|
2011-02-10 13:14:58 +08:00
|
|
|
return BinaryOperator::CreateAdd(Op0, NewMul);
|
2010-01-05 15:18:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-02 17:48:34 +08:00
|
|
|
{
|
|
|
|
// ~A - Min/Max(~A, O) -> Max/Min(A, ~O) - A
|
|
|
|
// ~A - Min/Max(O, ~A) -> Max/Min(A, ~O) - A
|
|
|
|
// Min/Max(~A, O) - ~A -> A - Max/Min(A, ~O)
|
|
|
|
// Min/Max(O, ~A) - ~A -> A - Max/Min(A, ~O)
|
|
|
|
// So long as O here is freely invertible, this will be neutral or a win.
|
|
|
|
Value *LHS, *RHS, *A;
|
|
|
|
Value *NotA = Op0, *MinMax = Op1;
|
|
|
|
SelectPatternFlavor SPF = matchSelectPattern(MinMax, LHS, RHS).Flavor;
|
|
|
|
if (!SelectPatternResult::isMinOrMax(SPF)) {
|
|
|
|
NotA = Op1;
|
|
|
|
MinMax = Op0;
|
|
|
|
SPF = matchSelectPattern(MinMax, LHS, RHS).Flavor;
|
|
|
|
}
|
|
|
|
if (SelectPatternResult::isMinOrMax(SPF) &&
|
|
|
|
match(NotA, m_Not(m_Value(A))) && (NotA == LHS || NotA == RHS)) {
|
|
|
|
if (NotA == LHS)
|
|
|
|
std::swap(LHS, RHS);
|
|
|
|
// LHS is now O above and expected to have at least 2 uses (the min/max)
|
|
|
|
// NotA is epected to have 2 uses from the min/max and 1 from the sub.
|
2019-08-13 20:49:16 +08:00
|
|
|
if (isFreeToInvert(LHS, !LHS->hasNUsesOrMore(3)) &&
|
2018-10-02 17:48:34 +08:00
|
|
|
!NotA->hasNUsesOrMore(4)) {
|
|
|
|
// Note: We don't generate the inverse max/min, just create the not of
|
|
|
|
// it and let other folds do the rest.
|
|
|
|
Value *Not = Builder.CreateNot(MinMax);
|
|
|
|
if (NotA == Op0)
|
|
|
|
return BinaryOperator::CreateSub(Not, A);
|
|
|
|
else
|
|
|
|
return BinaryOperator::CreateSub(A, Not);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-05 15:18:46 +08:00
|
|
|
// Optimize pointer differences into the same array into a size. Consider:
|
|
|
|
// &A[10] - &A[0]: we should compile this to "10".
|
2015-03-10 10:37:25 +08: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()))
|
2016-02-02 06:23:39 +08:00
|
|
|
return replaceInstUsesWith(I, Res);
|
2015-03-10 10:37:25 +08: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()))
|
2016-02-02 06:23:39 +08:00
|
|
|
return replaceInstUsesWith(I, Res);
|
2012-12-13 04:57:53 +08:00
|
|
|
|
2018-06-07 05:58:12 +08:00
|
|
|
// Canonicalize a shifty way to code absolute value to the common pattern.
|
|
|
|
// There are 2 potential commuted variants.
|
|
|
|
// We're relying on the fact that we only do this transform when the shift has
|
|
|
|
// exactly 2 uses and the xor has exactly 1 use (otherwise, we might increase
|
|
|
|
// instructions).
|
|
|
|
Value *A;
|
|
|
|
const APInt *ShAmt;
|
|
|
|
Type *Ty = I.getType();
|
|
|
|
if (match(Op1, m_AShr(m_Value(A), m_APInt(ShAmt))) &&
|
|
|
|
Op1->hasNUses(2) && *ShAmt == Ty->getScalarSizeInBits() - 1 &&
|
|
|
|
match(Op0, m_OneUse(m_c_Xor(m_Specific(A), m_Specific(Op1))))) {
|
|
|
|
// B = ashr i32 A, 31 ; smear the sign bit
|
|
|
|
// sub (xor A, B), B ; flip bits if negative and subtract -1 (add 1)
|
|
|
|
// --> (A < 0) ? -A : A
|
|
|
|
Value *Cmp = Builder.CreateICmpSLT(A, ConstantInt::getNullValue(Ty));
|
|
|
|
// Copy the nuw/nsw flags from the sub to the negate.
|
|
|
|
Value *Neg = Builder.CreateNeg(A, "", I.hasNoUnsignedWrap(),
|
|
|
|
I.hasNoSignedWrap());
|
|
|
|
return SelectInst::Create(Cmp, Neg, A);
|
|
|
|
}
|
|
|
|
|
2018-09-16 02:54:10 +08:00
|
|
|
if (Instruction *Ext = narrowMathIfNoOverflow(I))
|
|
|
|
return Ext;
|
|
|
|
|
2014-08-20 07:36:30 +08:00
|
|
|
bool Changed = false;
|
2017-05-22 14:25:31 +08:00
|
|
|
if (!I.hasNoSignedWrap() && willNotOverflowSignedSub(Op0, Op1, I)) {
|
2014-08-20 07:36:30 +08:00
|
|
|
Changed = true;
|
|
|
|
I.setHasNoSignedWrap(true);
|
|
|
|
}
|
2017-05-22 14:25:31 +08:00
|
|
|
if (!I.hasNoUnsignedWrap() && willNotOverflowUnsignedSub(Op0, Op1, I)) {
|
2014-08-20 15:17:31 +08:00
|
|
|
Changed = true;
|
|
|
|
I.setHasNoUnsignedWrap(true);
|
|
|
|
}
|
2014-08-20 07:36:30 +08:00
|
|
|
|
|
|
|
return Changed ? &I : nullptr;
|
2010-01-05 15:18:46 +08:00
|
|
|
}
|
|
|
|
|
2019-05-21 03:10:30 +08:00
|
|
|
/// This eliminates floating-point negation in either 'fneg(X)' or
|
|
|
|
/// 'fsub(-0.0, X)' form by combining into a constant operand.
|
|
|
|
static Instruction *foldFNegIntoConstant(Instruction &I) {
|
|
|
|
Value *X;
|
|
|
|
Constant *C;
|
|
|
|
|
|
|
|
// Fold negation into constant operand. This is limited with one-use because
|
|
|
|
// fneg is assumed better for analysis and cheaper in codegen than fmul/fdiv.
|
|
|
|
// -(X * C) --> X * (-C)
|
2019-05-21 05:00:42 +08:00
|
|
|
// FIXME: It's arguable whether these should be m_OneUse or not. The current
|
|
|
|
// belief is that the FNeg allows for better reassociation opportunities.
|
2019-05-21 03:10:30 +08:00
|
|
|
if (match(&I, m_FNeg(m_OneUse(m_FMul(m_Value(X), m_Constant(C))))))
|
|
|
|
return BinaryOperator::CreateFMulFMF(X, ConstantExpr::getFNeg(C), &I);
|
|
|
|
// -(X / C) --> X / (-C)
|
|
|
|
if (match(&I, m_FNeg(m_OneUse(m_FDiv(m_Value(X), m_Constant(C))))))
|
|
|
|
return BinaryOperator::CreateFDivFMF(X, ConstantExpr::getFNeg(C), &I);
|
|
|
|
// -(C / X) --> (-C) / X
|
|
|
|
if (match(&I, m_FNeg(m_OneUse(m_FDiv(m_Constant(C), m_Value(X))))))
|
|
|
|
return BinaryOperator::CreateFDivFMF(ConstantExpr::getFNeg(C), X, &I);
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-08-01 00:53:22 +08:00
|
|
|
static Instruction *hoistFNegAboveFMulFDiv(Instruction &I,
|
|
|
|
InstCombiner::BuilderTy &Builder) {
|
|
|
|
Value *FNeg;
|
|
|
|
if (!match(&I, m_FNeg(m_Value(FNeg))))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
Value *X, *Y;
|
|
|
|
if (match(FNeg, m_OneUse(m_FMul(m_Value(X), m_Value(Y)))))
|
|
|
|
return BinaryOperator::CreateFMulFMF(Builder.CreateFNegFMF(X, &I), Y, &I);
|
|
|
|
|
|
|
|
if (match(FNeg, m_OneUse(m_FDiv(m_Value(X), m_Value(Y)))))
|
|
|
|
return BinaryOperator::CreateFDivFMF(Builder.CreateFNegFMF(X, &I), Y, &I);
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-05-11 04:01:04 +08:00
|
|
|
Instruction *InstCombiner::visitFNeg(UnaryOperator &I) {
|
2019-06-12 00:21:21 +08:00
|
|
|
Value *Op = I.getOperand(0);
|
|
|
|
|
|
|
|
if (Value *V = SimplifyFNegInst(Op, I.getFastMathFlags(),
|
2019-05-11 04:01:04 +08:00
|
|
|
SQ.getWithInstruction(&I)))
|
|
|
|
return replaceInstUsesWith(I, V);
|
|
|
|
|
2019-05-21 03:10:30 +08:00
|
|
|
if (Instruction *X = foldFNegIntoConstant(I))
|
|
|
|
return X;
|
|
|
|
|
2019-06-12 00:21:21 +08:00
|
|
|
Value *X, *Y;
|
|
|
|
|
|
|
|
// If we can ignore the sign of zeros: -(X - Y) --> (Y - X)
|
|
|
|
if (I.hasNoSignedZeros() &&
|
|
|
|
match(Op, m_OneUse(m_FSub(m_Value(X), m_Value(Y)))))
|
|
|
|
return BinaryOperator::CreateFSubFMF(Y, X, &I);
|
|
|
|
|
2019-08-01 00:53:22 +08:00
|
|
|
if (Instruction *R = hoistFNegAboveFMulFDiv(I, Builder))
|
|
|
|
return R;
|
|
|
|
|
2019-05-11 04:01:04 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2010-01-05 15:18:46 +08:00
|
|
|
Instruction *InstCombiner::visitFSub(BinaryOperator &I) {
|
2018-06-22 01:06:36 +08:00
|
|
|
if (Value *V = SimplifyFSubInst(I.getOperand(0), I.getOperand(1),
|
|
|
|
I.getFastMathFlags(),
|
2017-06-09 11:21:29 +08:00
|
|
|
SQ.getWithInstruction(&I)))
|
2016-02-02 06:23:39 +08:00
|
|
|
return replaceInstUsesWith(I, V);
|
2012-12-12 08:28:32 +08:00
|
|
|
|
2018-10-03 23:20:58 +08:00
|
|
|
if (Instruction *X = foldVectorBinop(I))
|
2018-06-03 00:27:44 +08:00
|
|
|
return X;
|
|
|
|
|
2018-04-07 01:24:08 +08:00
|
|
|
// Subtraction from -0.0 is the canonical form of fneg.
|
|
|
|
// fsub nsz 0, X ==> fsub nsz -0.0, X
|
2018-06-22 01:06:36 +08:00
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
2018-04-07 01:24:08 +08:00
|
|
|
if (I.hasNoSignedZeros() && match(Op0, m_PosZeroFP()))
|
|
|
|
return BinaryOperator::CreateFNegFMF(Op1, &I);
|
2015-01-01 06:14:05 +08:00
|
|
|
|
2019-05-21 03:10:30 +08:00
|
|
|
if (Instruction *X = foldFNegIntoConstant(I))
|
|
|
|
return X;
|
|
|
|
|
2019-08-01 00:53:22 +08:00
|
|
|
if (Instruction *R = hoistFNegAboveFMulFDiv(I, Builder))
|
|
|
|
return R;
|
|
|
|
|
2018-08-08 22:29:08 +08:00
|
|
|
Value *X, *Y;
|
|
|
|
Constant *C;
|
|
|
|
|
2018-04-07 01:24:08 +08:00
|
|
|
// If Op0 is not -0.0 or we can ignore -0.0: Z - (X - Y) --> Z + (Y - X)
|
2018-04-06 07:21:15 +08:00
|
|
|
// Canonicalize to fadd to make analysis easier.
|
|
|
|
// This can also help codegen because fadd is commutative.
|
2018-04-07 01:24:08 +08:00
|
|
|
// Note that if this fsub was really an fneg, the fadd with -0.0 will get
|
|
|
|
// killed later. We still limit that particular transform with 'hasOneUse'
|
|
|
|
// because an fneg is assumed better/cheaper than a generic fsub.
|
2018-04-06 07:21:15 +08:00
|
|
|
if (I.hasNoSignedZeros() || CannotBeNegativeZero(Op0, SQ.TLI)) {
|
|
|
|
if (match(Op1, m_OneUse(m_FSub(m_Value(X), m_Value(Y))))) {
|
|
|
|
Value *NewSub = Builder.CreateFSubFMF(Y, X, &I);
|
|
|
|
return BinaryOperator::CreateFAddFMF(Op0, NewSub, &I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-20 15:13:13 +08:00
|
|
|
if (isa<Constant>(Op0))
|
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
|
|
|
|
if (Instruction *NV = FoldOpIntoSelect(I, SI))
|
|
|
|
return NV;
|
|
|
|
|
2018-04-06 01:06:45 +08:00
|
|
|
// X - C --> X + (-C)
|
2018-05-31 07:55:12 +08:00
|
|
|
// But don't transform constant expressions because there's an inverse fold
|
|
|
|
// for X + (-Y) --> X - Y.
|
|
|
|
if (match(Op1, m_Constant(C)) && !isa<ConstantExpr>(Op1))
|
2018-04-06 01:06:45 +08:00
|
|
|
return BinaryOperator::CreateFAddFMF(Op0, ConstantExpr::getFNeg(C), &I);
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2018-04-06 01:06:45 +08:00
|
|
|
// X - (-Y) --> X + Y
|
|
|
|
if (match(Op1, m_FNeg(m_Value(Y))))
|
|
|
|
return BinaryOperator::CreateFAddFMF(Op0, Y, &I);
|
2018-02-24 01:07:29 +08:00
|
|
|
|
2018-04-11 23:57:18 +08:00
|
|
|
// Similar to above, but look through a cast of the negated value:
|
|
|
|
// X - (fptrunc(-Y)) --> X + fptrunc(Y)
|
2018-08-09 23:07:13 +08:00
|
|
|
Type *Ty = I.getType();
|
|
|
|
if (match(Op1, m_OneUse(m_FPTrunc(m_FNeg(m_Value(Y))))))
|
|
|
|
return BinaryOperator::CreateFAddFMF(Op0, Builder.CreateFPTrunc(Y, Ty), &I);
|
|
|
|
|
2018-04-11 23:57:18 +08:00
|
|
|
// X - (fpext(-Y)) --> X + fpext(Y)
|
2018-08-09 23:07:13 +08:00
|
|
|
if (match(Op1, m_OneUse(m_FPExt(m_FNeg(m_Value(Y))))))
|
|
|
|
return BinaryOperator::CreateFAddFMF(Op0, Builder.CreateFPExt(Y, Ty), &I);
|
2010-01-05 15:18:46 +08:00
|
|
|
|
2019-07-29 01:10:06 +08:00
|
|
|
// Similar to above, but look through fmul/fdiv of the negated value:
|
|
|
|
// Op0 - (-X * Y) --> Op0 + (X * Y)
|
|
|
|
// Op0 - (Y * -X) --> Op0 + (X * Y)
|
|
|
|
if (match(Op1, m_OneUse(m_c_FMul(m_FNeg(m_Value(X)), m_Value(Y))))) {
|
|
|
|
Value *FMul = Builder.CreateFMulFMF(X, Y, &I);
|
|
|
|
return BinaryOperator::CreateFAddFMF(Op0, FMul, &I);
|
|
|
|
}
|
|
|
|
// Op0 - (-X / Y) --> Op0 + (X / Y)
|
|
|
|
// Op0 - (X / -Y) --> Op0 + (X / Y)
|
|
|
|
if (match(Op1, m_OneUse(m_FDiv(m_FNeg(m_Value(X)), m_Value(Y)))) ||
|
|
|
|
match(Op1, m_OneUse(m_FDiv(m_Value(X), m_FNeg(m_Value(Y)))))) {
|
|
|
|
Value *FDiv = Builder.CreateFDivFMF(X, Y, &I);
|
|
|
|
return BinaryOperator::CreateFAddFMF(Op0, FDiv, &I);
|
|
|
|
}
|
|
|
|
|
2018-08-09 23:07:13 +08:00
|
|
|
// Handle special cases for FSub with selects feeding the operation
|
2017-09-21 01:32:16 +08:00
|
|
|
if (Value *V = SimplifySelectsFeedingBinaryOp(I, Op0, Op1))
|
|
|
|
return replaceInstUsesWith(I, V);
|
|
|
|
|
2018-04-15 03:18:28 +08:00
|
|
|
if (I.hasAllowReassoc() && I.hasNoSignedZeros()) {
|
2018-08-09 00:04:48 +08:00
|
|
|
// (Y - X) - Y --> -X
|
|
|
|
if (match(Op0, m_FSub(m_Specific(Op1), m_Value(X))))
|
|
|
|
return BinaryOperator::CreateFNegFMF(X, &I);
|
|
|
|
|
2018-08-09 00:19:22 +08:00
|
|
|
// Y - (X + Y) --> -X
|
|
|
|
// Y - (Y + X) --> -X
|
|
|
|
if (match(Op1, m_c_FAdd(m_Specific(Op0), m_Value(X))))
|
|
|
|
return BinaryOperator::CreateFNegFMF(X, &I);
|
|
|
|
|
2018-08-10 02:42:12 +08:00
|
|
|
// (X * C) - X --> X * (C - 1.0)
|
|
|
|
if (match(Op0, m_FMul(m_Specific(Op1), m_Constant(C)))) {
|
|
|
|
Constant *CSubOne = ConstantExpr::getFSub(C, ConstantFP::get(Ty, 1.0));
|
|
|
|
return BinaryOperator::CreateFMulFMF(Op1, CSubOne, &I);
|
|
|
|
}
|
|
|
|
// X - (X * C) --> X * (1.0 - C)
|
|
|
|
if (match(Op1, m_FMul(m_Specific(Op0), m_Constant(C)))) {
|
|
|
|
Constant *OneSubC = ConstantExpr::getFSub(ConstantFP::get(Ty, 1.0), C);
|
|
|
|
return BinaryOperator::CreateFMulFMF(Op0, OneSubC, &I);
|
|
|
|
}
|
|
|
|
|
2018-08-12 23:48:26 +08:00
|
|
|
if (Instruction *F = factorizeFAddFSub(I, Builder))
|
|
|
|
return F;
|
|
|
|
|
2018-08-09 00:04:48 +08:00
|
|
|
// TODO: This performs reassociative folds for FP ops. Some fraction of the
|
|
|
|
// functionality has been subsumed by simple pattern matching here and in
|
|
|
|
// InstSimplify. We should let a dedicated reassociation pass handle more
|
|
|
|
// complex pattern matching and remove this from InstCombine.
|
2012-12-19 07:10:12 +08:00
|
|
|
if (Value *V = FAddCombine(Builder).simplify(&I))
|
2016-02-02 06:23:39 +08:00
|
|
|
return replaceInstUsesWith(I, V);
|
2012-12-19 07:10:12 +08:00
|
|
|
}
|
|
|
|
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2010-01-05 15:18:46 +08:00
|
|
|
}
|