2002-08-12 21:17:25 +00:00
|
|
|
//===- InstructionCombining.cpp - Combine multiple instructions -----------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-12-14 16:52:21 +00:00
|
|
|
//
|
|
|
|
// InstructionCombining - Combine instructions to form fewer, simple
|
2002-09-02 04:59:56 +00:00
|
|
|
// instructions. This pass does not modify the CFG This pass is where algebraic
|
|
|
|
// simplification happens.
|
2001-12-14 16:52:21 +00:00
|
|
|
//
|
|
|
|
// This pass combines things like:
|
2007-03-18 22:51:34 +00:00
|
|
|
// %Y = add i32 %X, 1
|
|
|
|
// %Z = add i32 %Y, 1
|
2001-12-14 16:52:21 +00:00
|
|
|
// into:
|
2007-03-18 22:51:34 +00:00
|
|
|
// %Z = add i32 %X, 2
|
2001-12-14 16:52:21 +00:00
|
|
|
//
|
|
|
|
// This is a simple worklist driven algorithm.
|
|
|
|
//
|
2003-09-10 05:29:43 +00:00
|
|
|
// This pass guarantees that the following canonicalizations are performed on
|
2003-07-23 21:41:57 +00:00
|
|
|
// the program:
|
|
|
|
// 1. If a binary operator has a constant operand, it is moved to the RHS
|
2003-08-12 21:53:41 +00:00
|
|
|
// 2. Bitwise operators with constant operands are always grouped so that
|
|
|
|
// shifts are performed first, then or's, then and's, then xor's.
|
2006-12-23 06:05:41 +00:00
|
|
|
// 3. Compare instructions are converted from <,>,<=,>= to ==,!= if possible
|
|
|
|
// 4. All cmp instructions on boolean values are replaced with logical ops
|
2003-08-13 04:18:28 +00:00
|
|
|
// 5. add X, X is represented as (X*2) => (X << 1)
|
|
|
|
// 6. Multiplies with a power-of-two constant argument are transformed into
|
|
|
|
// shifts.
|
2004-11-14 19:13:23 +00:00
|
|
|
// ... etc.
|
2003-07-23 21:41:57 +00:00
|
|
|
//
|
2001-12-14 16:52:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-03-13 23:54:27 +00:00
|
|
|
#define DEBUG_TYPE "instcombine"
|
2002-05-07 20:03:00 +00:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2004-10-12 04:52:52 +00:00
|
|
|
#include "llvm/IntrinsicInst.h"
|
2002-02-26 21:46:54 +00:00
|
|
|
#include "llvm/Pass.h"
|
2002-11-04 16:18:53 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2003-06-26 05:06:25 +00:00
|
|
|
#include "llvm/GlobalVariable.h"
|
2007-01-30 23:46:24 +00:00
|
|
|
#include "llvm/Analysis/ConstantFolding.h"
|
2003-11-02 05:57:39 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2004-04-05 01:30:19 +00:00
|
|
|
#include "llvm/Support/CallSite.h"
|
2008-02-03 16:33:09 +00:00
|
|
|
#include "llvm/Support/ConstantRange.h"
|
2004-12-08 23:43:58 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2004-04-05 01:30:19 +00:00
|
|
|
#include "llvm/Support/GetElementPtrTypeIterator.h"
|
2002-04-18 17:39:14 +00:00
|
|
|
#include "llvm/Support/InstVisitor.h"
|
2005-08-02 19:16:58 +00:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2004-07-30 07:50:03 +00:00
|
|
|
#include "llvm/Support/PatternMatch.h"
|
2006-08-27 12:54:02 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2007-03-02 21:28:56 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2007-01-31 04:40:53 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2007-02-15 19:41:52 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2004-12-08 23:43:58 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2002-05-14 15:24:07 +00:00
|
|
|
#include <algorithm>
|
2008-04-20 08:33:11 +00:00
|
|
|
#include <climits>
|
2007-03-26 17:44:01 +00:00
|
|
|
#include <sstream>
|
2003-12-07 01:24:23 +00:00
|
|
|
using namespace llvm;
|
2004-07-30 07:50:03 +00:00
|
|
|
using namespace llvm::PatternMatch;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2006-12-19 21:40:18 +00:00
|
|
|
STATISTIC(NumCombined , "Number of insts combined");
|
|
|
|
STATISTIC(NumConstProp, "Number of constant folds");
|
|
|
|
STATISTIC(NumDeadInst , "Number of dead inst eliminated");
|
|
|
|
STATISTIC(NumDeadStore, "Number of dead stores eliminated");
|
|
|
|
STATISTIC(NumSunkInst , "Number of instructions sunk");
|
2002-10-01 22:38:41 +00:00
|
|
|
|
2006-12-19 21:40:18 +00:00
|
|
|
namespace {
|
2006-06-28 22:08:15 +00:00
|
|
|
class VISIBILITY_HIDDEN InstCombiner
|
|
|
|
: public FunctionPass,
|
|
|
|
public InstVisitor<InstCombiner, Instruction*> {
|
2002-04-18 17:39:14 +00:00
|
|
|
// Worklist of all of the instructions that need to be simplified.
|
2007-03-02 21:28:56 +00:00
|
|
|
std::vector<Instruction*> Worklist;
|
|
|
|
DenseMap<Instruction*, unsigned> WorklistMap;
|
2003-11-02 05:57:39 +00:00
|
|
|
TargetData *TD;
|
2007-03-04 04:27:24 +00:00
|
|
|
bool MustPreserveLCSSA;
|
2007-03-02 21:28:56 +00:00
|
|
|
public:
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2007-05-01 21:15:47 +00:00
|
|
|
InstCombiner() : FunctionPass((intptr_t)&ID) {}
|
|
|
|
|
2007-03-02 21:28:56 +00:00
|
|
|
/// AddToWorkList - Add the specified instruction to the worklist if it
|
|
|
|
/// isn't already in it.
|
|
|
|
void AddToWorkList(Instruction *I) {
|
|
|
|
if (WorklistMap.insert(std::make_pair(I, Worklist.size())))
|
|
|
|
Worklist.push_back(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveFromWorkList - remove I from the worklist if it exists.
|
|
|
|
void RemoveFromWorkList(Instruction *I) {
|
|
|
|
DenseMap<Instruction*, unsigned>::iterator It = WorklistMap.find(I);
|
|
|
|
if (It == WorklistMap.end()) return; // Not in worklist.
|
|
|
|
|
|
|
|
// Don't bother moving everything down, just null out the slot.
|
|
|
|
Worklist[It->second] = 0;
|
|
|
|
|
|
|
|
WorklistMap.erase(It);
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *RemoveOneFromWorkList() {
|
|
|
|
Instruction *I = Worklist.back();
|
|
|
|
Worklist.pop_back();
|
|
|
|
WorklistMap.erase(I);
|
|
|
|
return I;
|
|
|
|
}
|
2002-04-18 17:39:14 +00:00
|
|
|
|
2007-03-02 21:28:56 +00:00
|
|
|
|
2004-02-28 05:22:00 +00:00
|
|
|
/// AddUsersToWorkList - When an instruction is simplified, add all users of
|
|
|
|
/// the instruction to the work lists because they might get more simplified
|
|
|
|
/// now.
|
|
|
|
///
|
2006-02-07 06:56:34 +00:00
|
|
|
void AddUsersToWorkList(Value &I) {
|
2002-06-25 16:13:24 +00:00
|
|
|
for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
|
2002-04-18 17:39:14 +00:00
|
|
|
UI != UE; ++UI)
|
2007-03-02 21:28:56 +00:00
|
|
|
AddToWorkList(cast<Instruction>(*UI));
|
2002-04-18 17:39:14 +00:00
|
|
|
}
|
|
|
|
|
2004-02-28 05:22:00 +00:00
|
|
|
/// AddUsesToWorkList - When an instruction is simplified, add operands to
|
|
|
|
/// the work lists because they might get more simplified now.
|
|
|
|
///
|
|
|
|
void AddUsesToWorkList(Instruction &I) {
|
|
|
|
for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
|
|
|
|
if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i)))
|
2007-03-02 21:28:56 +00:00
|
|
|
AddToWorkList(Op);
|
2004-02-28 05:22:00 +00:00
|
|
|
}
|
add a new SimplifyDemandedVectorElts method, which works similarly to
SimplifyDemandedBits. The idea is that some operations can be simplified if
not all of the computed elements are needed. Some targets (like x86) have a
large number of intrinsics that operate on a single element, but pass other
elts through unmodified. If those other elements are not needed, the
intrinsics can be simplified to scalar operations, and insertelement ops can
be removed.
This turns (f.e.):
ushort %Convert_sse(float %f) {
%tmp = insertelement <4 x float> undef, float %f, uint 0 ; <<4 x float>> [#uses=1]
%tmp10 = insertelement <4 x float> %tmp, float 0.000000e+00, uint 1 ; <<4 x float>> [#uses=1]
%tmp11 = insertelement <4 x float> %tmp10, float 0.000000e+00, uint 2 ; <<4 x float>> [#uses=1]
%tmp12 = insertelement <4 x float> %tmp11, float 0.000000e+00, uint 3 ; <<4 x float>> [#uses=1]
%tmp28 = tail call <4 x float> %llvm.x86.sse.sub.ss( <4 x float> %tmp12, <4 x float> < float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp37 = tail call <4 x float> %llvm.x86.sse.mul.ss( <4 x float> %tmp28, <4 x float> < float 5.000000e-01, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp37, <4 x float> < float 6.553500e+04, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> zeroinitializer ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
into:
ushort %Convert_sse(float %f) {
entry:
%tmp28 = sub float %f, 1.000000e+00 ; <float> [#uses=1]
%tmp37 = mul float %tmp28, 5.000000e-01 ; <float> [#uses=1]
%tmp375 = insertelement <4 x float> undef, float %tmp37, uint 0 ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp375, <4 x float> < float 6.553500e+04, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> < float 0.000000e+00, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
which improves codegen from:
_Convert_sse:
movss LCPI1_0, %xmm0
movss 4(%esp), %xmm1
subss %xmm0, %xmm1
movss LCPI1_1, %xmm0
mulss %xmm0, %xmm1
movss LCPI1_2, %xmm0
minss %xmm0, %xmm1
xorps %xmm0, %xmm0
maxss %xmm0, %xmm1
cvttss2si %xmm1, %eax
andl $65535, %eax
ret
to:
_Convert_sse:
movss 4(%esp), %xmm0
subss LCPI1_0, %xmm0
mulss LCPI1_1, %xmm0
movss LCPI1_2, %xmm1
minss %xmm1, %xmm0
xorps %xmm1, %xmm1
maxss %xmm1, %xmm0
cvttss2si %xmm0, %eax
andl $65535, %eax
ret
This is just a first step, it can be extended in many ways. Testcase here:
Transforms/InstCombine/vec_demanded_elts.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30752 91177308-0d34-0410-b5e6-96231b3b80d8
2006-10-05 06:55:50 +00:00
|
|
|
|
|
|
|
/// AddSoonDeadInstToWorklist - The specified instruction is about to become
|
|
|
|
/// dead. Add all of its operands to the worklist, turning them into
|
|
|
|
/// undef's to reduce the number of uses of those instructions.
|
|
|
|
///
|
|
|
|
/// Return the specified operand before it is turned into an undef.
|
|
|
|
///
|
|
|
|
Value *AddSoonDeadInstToWorklist(Instruction &I, unsigned op) {
|
|
|
|
Value *R = I.getOperand(op);
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
|
|
|
|
if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) {
|
2007-03-02 21:28:56 +00:00
|
|
|
AddToWorkList(Op);
|
add a new SimplifyDemandedVectorElts method, which works similarly to
SimplifyDemandedBits. The idea is that some operations can be simplified if
not all of the computed elements are needed. Some targets (like x86) have a
large number of intrinsics that operate on a single element, but pass other
elts through unmodified. If those other elements are not needed, the
intrinsics can be simplified to scalar operations, and insertelement ops can
be removed.
This turns (f.e.):
ushort %Convert_sse(float %f) {
%tmp = insertelement <4 x float> undef, float %f, uint 0 ; <<4 x float>> [#uses=1]
%tmp10 = insertelement <4 x float> %tmp, float 0.000000e+00, uint 1 ; <<4 x float>> [#uses=1]
%tmp11 = insertelement <4 x float> %tmp10, float 0.000000e+00, uint 2 ; <<4 x float>> [#uses=1]
%tmp12 = insertelement <4 x float> %tmp11, float 0.000000e+00, uint 3 ; <<4 x float>> [#uses=1]
%tmp28 = tail call <4 x float> %llvm.x86.sse.sub.ss( <4 x float> %tmp12, <4 x float> < float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp37 = tail call <4 x float> %llvm.x86.sse.mul.ss( <4 x float> %tmp28, <4 x float> < float 5.000000e-01, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp37, <4 x float> < float 6.553500e+04, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> zeroinitializer ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
into:
ushort %Convert_sse(float %f) {
entry:
%tmp28 = sub float %f, 1.000000e+00 ; <float> [#uses=1]
%tmp37 = mul float %tmp28, 5.000000e-01 ; <float> [#uses=1]
%tmp375 = insertelement <4 x float> undef, float %tmp37, uint 0 ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp375, <4 x float> < float 6.553500e+04, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> < float 0.000000e+00, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
which improves codegen from:
_Convert_sse:
movss LCPI1_0, %xmm0
movss 4(%esp), %xmm1
subss %xmm0, %xmm1
movss LCPI1_1, %xmm0
mulss %xmm0, %xmm1
movss LCPI1_2, %xmm0
minss %xmm0, %xmm1
xorps %xmm0, %xmm0
maxss %xmm0, %xmm1
cvttss2si %xmm1, %eax
andl $65535, %eax
ret
to:
_Convert_sse:
movss 4(%esp), %xmm0
subss LCPI1_0, %xmm0
mulss LCPI1_1, %xmm0
movss LCPI1_2, %xmm1
minss %xmm1, %xmm0
xorps %xmm1, %xmm1
maxss %xmm1, %xmm0
cvttss2si %xmm0, %eax
andl $65535, %eax
ret
This is just a first step, it can be extended in many ways. Testcase here:
Transforms/InstCombine/vec_demanded_elts.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30752 91177308-0d34-0410-b5e6-96231b3b80d8
2006-10-05 06:55:50 +00:00
|
|
|
// Set the operand to undef to drop the use.
|
|
|
|
I.setOperand(i, UndefValue::get(Op->getType()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return R;
|
|
|
|
}
|
2004-02-28 05:22:00 +00:00
|
|
|
|
2002-04-18 17:39:14 +00:00
|
|
|
public:
|
2002-06-25 16:13:24 +00:00
|
|
|
virtual bool runOnFunction(Function &F);
|
2007-03-03 02:04:50 +00:00
|
|
|
|
|
|
|
bool DoOneIteration(Function &F, unsigned ItNum);
|
2002-04-18 17:39:14 +00:00
|
|
|
|
2002-04-28 21:27:06 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2003-11-02 05:57:39 +00:00
|
|
|
AU.addRequired<TargetData>();
|
2006-07-10 19:03:49 +00:00
|
|
|
AU.addPreservedID(LCSSAID);
|
2002-10-21 20:00:28 +00:00
|
|
|
AU.setPreservesCFG();
|
2002-04-28 21:27:06 +00:00
|
|
|
}
|
|
|
|
|
2004-04-05 01:30:19 +00:00
|
|
|
TargetData &getTargetData() const { return *TD; }
|
|
|
|
|
2002-04-18 17:39:14 +00:00
|
|
|
// Visitation implementation - Implement instruction combining for different
|
|
|
|
// instruction types. The semantics are as follows:
|
|
|
|
// Return Value:
|
|
|
|
// null - No change was made
|
2002-08-12 21:17:25 +00:00
|
|
|
// I - Change was made, I is still valid, I may be dead though
|
2002-04-18 17:39:14 +00:00
|
|
|
// otherwise - Change was made, replace I with returned instruction
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2002-06-25 16:13:24 +00:00
|
|
|
Instruction *visitAdd(BinaryOperator &I);
|
|
|
|
Instruction *visitSub(BinaryOperator &I);
|
|
|
|
Instruction *visitMul(BinaryOperator &I);
|
2006-11-02 01:53:59 +00:00
|
|
|
Instruction *visitURem(BinaryOperator &I);
|
|
|
|
Instruction *visitSRem(BinaryOperator &I);
|
|
|
|
Instruction *visitFRem(BinaryOperator &I);
|
|
|
|
Instruction *commonRemTransforms(BinaryOperator &I);
|
|
|
|
Instruction *commonIRemTransforms(BinaryOperator &I);
|
2006-10-26 06:15:43 +00:00
|
|
|
Instruction *commonDivTransforms(BinaryOperator &I);
|
|
|
|
Instruction *commonIDivTransforms(BinaryOperator &I);
|
|
|
|
Instruction *visitUDiv(BinaryOperator &I);
|
|
|
|
Instruction *visitSDiv(BinaryOperator &I);
|
|
|
|
Instruction *visitFDiv(BinaryOperator &I);
|
2002-06-25 16:13:24 +00:00
|
|
|
Instruction *visitAnd(BinaryOperator &I);
|
|
|
|
Instruction *visitOr (BinaryOperator &I);
|
|
|
|
Instruction *visitXor(BinaryOperator &I);
|
2007-02-02 02:16:23 +00:00
|
|
|
Instruction *visitShl(BinaryOperator &I);
|
|
|
|
Instruction *visitAShr(BinaryOperator &I);
|
|
|
|
Instruction *visitLShr(BinaryOperator &I);
|
|
|
|
Instruction *commonShiftTransforms(BinaryOperator &I);
|
2006-12-23 06:05:41 +00:00
|
|
|
Instruction *visitFCmpInst(FCmpInst &I);
|
|
|
|
Instruction *visitICmpInst(ICmpInst &I);
|
|
|
|
Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI);
|
2007-04-03 17:43:25 +00:00
|
|
|
Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
|
|
|
|
Instruction *LHS,
|
|
|
|
ConstantInt *RHS);
|
2007-06-20 23:46:26 +00:00
|
|
|
Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
|
|
|
|
ConstantInt *DivRHS);
|
2005-04-24 06:59:08 +00:00
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
Instruction *FoldGEPICmp(User *GEPLHS, Value *RHS,
|
|
|
|
ICmpInst::Predicate Cond, Instruction &I);
|
2006-10-20 07:07:24 +00:00
|
|
|
Instruction *FoldShiftByConstant(Value *Op0, ConstantInt *Op1,
|
2007-02-02 02:16:23 +00:00
|
|
|
BinaryOperator &I);
|
2006-11-27 01:05:10 +00:00
|
|
|
Instruction *commonCastTransforms(CastInst &CI);
|
|
|
|
Instruction *commonIntCastTransforms(CastInst &CI);
|
2007-04-27 17:44:50 +00:00
|
|
|
Instruction *commonPointerCastTransforms(CastInst &CI);
|
2007-04-11 06:57:46 +00:00
|
|
|
Instruction *visitTrunc(TruncInst &CI);
|
|
|
|
Instruction *visitZExt(ZExtInst &CI);
|
|
|
|
Instruction *visitSExt(SExtInst &CI);
|
2008-01-27 05:29:54 +00:00
|
|
|
Instruction *visitFPTrunc(FPTruncInst &CI);
|
2006-11-27 01:05:10 +00:00
|
|
|
Instruction *visitFPExt(CastInst &CI);
|
|
|
|
Instruction *visitFPToUI(CastInst &CI);
|
|
|
|
Instruction *visitFPToSI(CastInst &CI);
|
|
|
|
Instruction *visitUIToFP(CastInst &CI);
|
|
|
|
Instruction *visitSIToFP(CastInst &CI);
|
|
|
|
Instruction *visitPtrToInt(CastInst &CI);
|
2008-01-08 07:23:51 +00:00
|
|
|
Instruction *visitIntToPtr(IntToPtrInst &CI);
|
2007-04-27 17:44:50 +00:00
|
|
|
Instruction *visitBitCast(BitCastInst &CI);
|
2005-01-19 21:50:18 +00:00
|
|
|
Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI,
|
|
|
|
Instruction *FI);
|
2004-03-12 05:52:32 +00:00
|
|
|
Instruction *visitSelectInst(SelectInst &CI);
|
2003-06-19 17:00:31 +00:00
|
|
|
Instruction *visitCallInst(CallInst &CI);
|
|
|
|
Instruction *visitInvokeInst(InvokeInst &II);
|
2002-06-25 16:13:24 +00:00
|
|
|
Instruction *visitPHINode(PHINode &PN);
|
|
|
|
Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
|
2002-11-04 16:18:53 +00:00
|
|
|
Instruction *visitAllocationInst(AllocationInst &AI);
|
2003-12-07 01:24:23 +00:00
|
|
|
Instruction *visitFreeInst(FreeInst &FI);
|
2003-06-26 05:06:25 +00:00
|
|
|
Instruction *visitLoadInst(LoadInst &LI);
|
2005-01-31 05:36:43 +00:00
|
|
|
Instruction *visitStoreInst(StoreInst &SI);
|
2003-06-04 04:46:00 +00:00
|
|
|
Instruction *visitBranchInst(BranchInst &BI);
|
2004-07-03 00:26:11 +00:00
|
|
|
Instruction *visitSwitchInst(SwitchInst &SI);
|
2006-04-15 01:39:45 +00:00
|
|
|
Instruction *visitInsertElementInst(InsertElementInst &IE);
|
2006-01-13 22:48:06 +00:00
|
|
|
Instruction *visitExtractElementInst(ExtractElementInst &EI);
|
2006-04-10 22:45:52 +00:00
|
|
|
Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI);
|
2002-04-18 17:39:14 +00:00
|
|
|
|
|
|
|
// visitInstruction - Specify what to return for unhandled instructions...
|
2002-06-25 16:13:24 +00:00
|
|
|
Instruction *visitInstruction(Instruction &I) { return 0; }
|
2002-08-09 23:47:40 +00:00
|
|
|
|
2003-06-19 17:00:31 +00:00
|
|
|
private:
|
2003-10-07 22:32:43 +00:00
|
|
|
Instruction *visitCallSite(CallSite CS);
|
2003-06-19 17:00:31 +00:00
|
|
|
bool transformConstExprCastCall(CallSite CS);
|
2007-09-17 10:26:40 +00:00
|
|
|
Instruction *transformCallThroughTrampoline(CallSite CS);
|
2008-03-24 00:21:34 +00:00
|
|
|
Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI,
|
|
|
|
bool DoXform = true);
|
2003-06-19 17:00:31 +00:00
|
|
|
|
2004-04-05 01:30:19 +00:00
|
|
|
public:
|
2002-08-09 23:47:40 +00:00
|
|
|
// InsertNewInstBefore - insert an instruction New before instruction Old
|
|
|
|
// in the program. Add the new instruction to the worklist.
|
|
|
|
//
|
Fold (and (setcc X, C1), (setcc X, C2))
This is important for several reasons:
1. Benchmarks have lots of code that looks like this (perlbmk in particular):
%tmp.2.i = setne int %tmp.0.i, 128 ; <bool> [#uses=1]
%tmp.6343 = seteq int %tmp.0.i, 1 ; <bool> [#uses=1]
%tmp.63 = and bool %tmp.2.i, %tmp.6343 ; <bool> [#uses=1]
we now fold away the setne, a clear improvement.
2. In the more important cases, such as (X >= 10) & (X < 20), we now produce
smaller code: (X-10) < 10.
3. Perhaps the nicest effect of this patch is that it really helps out the
code generators. In particular, for a 'range test' like the above,
instead of generating this on X86 (the difference on PPC is even more
pronounced):
cmp %EAX, 50
setge %CL
cmp %EAX, 100
setl %AL
and %CL, %AL
cmp %CL, 0
we now generate this:
add %EAX, -50
cmp %EAX, 50
Furthermore, this causes setcc's to be folded into branches more often.
These combinations trigger dozens of times in the spec benchmarks, particularly
in 176.gcc, 186.crafty, 253.perlbmk, 254.gap, & 099.go.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16559 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-28 21:48:02 +00:00
|
|
|
Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) {
|
2002-08-23 18:32:43 +00:00
|
|
|
assert(New && New->getParent() == 0 &&
|
|
|
|
"New instruction already inserted into a basic block!");
|
2002-08-09 23:47:40 +00:00
|
|
|
BasicBlock *BB = Old.getParent();
|
|
|
|
BB->getInstList().insert(&Old, New); // Insert inst
|
2007-03-02 21:28:56 +00:00
|
|
|
AddToWorkList(New);
|
2004-02-23 06:38:22 +00:00
|
|
|
return New;
|
2002-08-09 23:47:40 +00:00
|
|
|
}
|
|
|
|
|
2004-09-24 15:21:34 +00:00
|
|
|
/// InsertCastBefore - Insert a cast of V to TY before the instruction POS.
|
|
|
|
/// This also adds the cast to the worklist. Finally, this returns the
|
|
|
|
/// cast.
|
2006-12-12 09:18:51 +00:00
|
|
|
Value *InsertCastBefore(Instruction::CastOps opc, Value *V, const Type *Ty,
|
|
|
|
Instruction &Pos) {
|
2004-09-24 15:21:34 +00:00
|
|
|
if (V->getType() == Ty) return V;
|
2005-04-21 23:48:37 +00:00
|
|
|
|
Lower vperm(x,y, mask) -> shuffle(x,y,mask) if mask is constant. This allows
us to compile oh-so-realistic stuff like this:
vec_vperm(A, B, (vector unsigned char){14});
to:
vspltb v0, v0, 14
instead of:
vspltisb v0, 14
vperm v0, v2, v1, v0
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@27452 91177308-0d34-0410-b5e6-96231b3b80d8
2006-04-06 19:19:17 +00:00
|
|
|
if (Constant *CV = dyn_cast<Constant>(V))
|
2006-12-12 09:18:51 +00:00
|
|
|
return ConstantExpr::getCast(opc, CV, Ty);
|
Lower vperm(x,y, mask) -> shuffle(x,y,mask) if mask is constant. This allows
us to compile oh-so-realistic stuff like this:
vec_vperm(A, B, (vector unsigned char){14});
to:
vspltb v0, v0, 14
instead of:
vspltisb v0, 14
vperm v0, v2, v1, v0
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@27452 91177308-0d34-0410-b5e6-96231b3b80d8
2006-04-06 19:19:17 +00:00
|
|
|
|
2006-12-12 09:18:51 +00:00
|
|
|
Instruction *C = CastInst::create(opc, V, Ty, V->getName(), &Pos);
|
2007-03-02 21:28:56 +00:00
|
|
|
AddToWorkList(C);
|
2004-09-24 15:21:34 +00:00
|
|
|
return C;
|
|
|
|
}
|
2008-01-13 22:23:22 +00:00
|
|
|
|
|
|
|
Value *InsertBitCastBefore(Value *V, const Type *Ty, Instruction &Pos) {
|
|
|
|
return InsertCastBefore(Instruction::BitCast, V, Ty, Pos);
|
|
|
|
}
|
|
|
|
|
2004-09-24 15:21:34 +00:00
|
|
|
|
2002-08-09 23:47:40 +00:00
|
|
|
// ReplaceInstUsesWith - This method is to be used when an instruction is
|
|
|
|
// found to be dead, replacable with another preexisting expression. Here
|
|
|
|
// we add all uses of I to the worklist, replace all uses of I with the new
|
|
|
|
// value, then return I, so that the inst combiner will know that I was
|
|
|
|
// modified.
|
|
|
|
//
|
|
|
|
Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) {
|
2004-02-28 05:22:00 +00:00
|
|
|
AddUsersToWorkList(I); // Add all modified instrs to worklist
|
2004-04-05 02:10:19 +00:00
|
|
|
if (&I != V) {
|
|
|
|
I.replaceAllUsesWith(V);
|
|
|
|
return &I;
|
|
|
|
} else {
|
|
|
|
// If we are replacing the instruction with itself, this must be in a
|
|
|
|
// segment of unreachable code, so just clobber the instruction.
|
2004-10-18 02:59:09 +00:00
|
|
|
I.replaceAllUsesWith(UndefValue::get(I.getType()));
|
2004-04-05 02:10:19 +00:00
|
|
|
return &I;
|
|
|
|
}
|
2002-08-09 23:47:40 +00:00
|
|
|
}
|
2004-02-28 05:22:00 +00:00
|
|
|
|
2006-02-07 06:56:34 +00:00
|
|
|
// UpdateValueUsesWith - This method is to be used when an value is
|
|
|
|
// found to be replacable with another preexisting expression or was
|
|
|
|
// updated. Here we add all uses of I to the worklist, replace all uses of
|
|
|
|
// I with the new value (unless the instruction was just updated), then
|
|
|
|
// return true, so that the inst combiner will know that I was modified.
|
|
|
|
//
|
|
|
|
bool UpdateValueUsesWith(Value *Old, Value *New) {
|
|
|
|
AddUsersToWorkList(*Old); // Add all modified instrs to worklist
|
|
|
|
if (Old != New)
|
|
|
|
Old->replaceAllUsesWith(New);
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(Old))
|
2007-03-02 21:28:56 +00:00
|
|
|
AddToWorkList(I);
|
2006-02-12 08:02:11 +00:00
|
|
|
if (Instruction *I = dyn_cast<Instruction>(New))
|
2007-03-02 21:28:56 +00:00
|
|
|
AddToWorkList(I);
|
2006-02-07 06:56:34 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-02-28 05:22:00 +00:00
|
|
|
// EraseInstFromFunction - When dealing with an instruction that has side
|
|
|
|
// effects or produces a void value, we can't rely on DCE to delete the
|
|
|
|
// instruction. Instead, visit methods should return the value returned by
|
|
|
|
// this function.
|
|
|
|
Instruction *EraseInstFromFunction(Instruction &I) {
|
|
|
|
assert(I.use_empty() && "Cannot erase instruction that is used!");
|
|
|
|
AddUsesToWorkList(I);
|
2007-03-02 21:28:56 +00:00
|
|
|
RemoveFromWorkList(&I);
|
2004-11-18 21:41:39 +00:00
|
|
|
I.eraseFromParent();
|
2004-02-28 05:22:00 +00:00
|
|
|
return 0; // Don't do anything with FI
|
|
|
|
}
|
|
|
|
|
2003-08-13 20:16:26 +00:00
|
|
|
private:
|
2003-07-24 17:35:25 +00:00
|
|
|
/// InsertOperandCastBefore - This inserts a cast of V to DestTy before the
|
|
|
|
/// InsertBefore instruction. This is specialized a bit to avoid inserting
|
|
|
|
/// casts that are known to not do anything...
|
|
|
|
///
|
2006-12-12 09:18:51 +00:00
|
|
|
Value *InsertOperandCastBefore(Instruction::CastOps opcode,
|
|
|
|
Value *V, const Type *DestTy,
|
2003-07-24 17:35:25 +00:00
|
|
|
Instruction *InsertBefore);
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
/// SimplifyCommutative - This performs a few simplifications for
|
|
|
|
/// commutative operators.
|
2003-03-11 00:12:48 +00:00
|
|
|
bool SimplifyCommutative(BinaryOperator &I);
|
2003-09-19 17:17:26 +00:00
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
/// SimplifyCompare - This reorders the operands of a CmpInst to get them in
|
|
|
|
/// most-complex to least-complex order.
|
|
|
|
bool SimplifyCompare(CmpInst &I);
|
|
|
|
|
2007-03-23 21:24:59 +00:00
|
|
|
/// SimplifyDemandedBits - Attempts to replace V with a simpler value based
|
|
|
|
/// on the demanded bits.
|
2007-03-12 17:25:59 +00:00
|
|
|
bool SimplifyDemandedBits(Value *V, APInt DemandedMask,
|
|
|
|
APInt& KnownZero, APInt& KnownOne,
|
|
|
|
unsigned Depth = 0);
|
|
|
|
|
add a new SimplifyDemandedVectorElts method, which works similarly to
SimplifyDemandedBits. The idea is that some operations can be simplified if
not all of the computed elements are needed. Some targets (like x86) have a
large number of intrinsics that operate on a single element, but pass other
elts through unmodified. If those other elements are not needed, the
intrinsics can be simplified to scalar operations, and insertelement ops can
be removed.
This turns (f.e.):
ushort %Convert_sse(float %f) {
%tmp = insertelement <4 x float> undef, float %f, uint 0 ; <<4 x float>> [#uses=1]
%tmp10 = insertelement <4 x float> %tmp, float 0.000000e+00, uint 1 ; <<4 x float>> [#uses=1]
%tmp11 = insertelement <4 x float> %tmp10, float 0.000000e+00, uint 2 ; <<4 x float>> [#uses=1]
%tmp12 = insertelement <4 x float> %tmp11, float 0.000000e+00, uint 3 ; <<4 x float>> [#uses=1]
%tmp28 = tail call <4 x float> %llvm.x86.sse.sub.ss( <4 x float> %tmp12, <4 x float> < float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp37 = tail call <4 x float> %llvm.x86.sse.mul.ss( <4 x float> %tmp28, <4 x float> < float 5.000000e-01, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp37, <4 x float> < float 6.553500e+04, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> zeroinitializer ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
into:
ushort %Convert_sse(float %f) {
entry:
%tmp28 = sub float %f, 1.000000e+00 ; <float> [#uses=1]
%tmp37 = mul float %tmp28, 5.000000e-01 ; <float> [#uses=1]
%tmp375 = insertelement <4 x float> undef, float %tmp37, uint 0 ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp375, <4 x float> < float 6.553500e+04, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> < float 0.000000e+00, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
which improves codegen from:
_Convert_sse:
movss LCPI1_0, %xmm0
movss 4(%esp), %xmm1
subss %xmm0, %xmm1
movss LCPI1_1, %xmm0
mulss %xmm0, %xmm1
movss LCPI1_2, %xmm0
minss %xmm0, %xmm1
xorps %xmm0, %xmm0
maxss %xmm0, %xmm1
cvttss2si %xmm1, %eax
andl $65535, %eax
ret
to:
_Convert_sse:
movss 4(%esp), %xmm0
subss LCPI1_0, %xmm0
mulss LCPI1_1, %xmm0
movss LCPI1_2, %xmm1
minss %xmm1, %xmm0
xorps %xmm1, %xmm1
maxss %xmm1, %xmm0
cvttss2si %xmm0, %eax
andl $65535, %eax
ret
This is just a first step, it can be extended in many ways. Testcase here:
Transforms/InstCombine/vec_demanded_elts.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30752 91177308-0d34-0410-b5e6-96231b3b80d8
2006-10-05 06:55:50 +00:00
|
|
|
Value *SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts,
|
|
|
|
uint64_t &UndefElts, unsigned Depth = 0);
|
|
|
|
|
Fold binary expressions and casts into PHI nodes that have all constant inputs.
This takes something like this:
%A = phi int [ 3, %cond_false.0 ], [ 2, %endif.0.i ], [ 2, %endif.1.i ]
%B = div int %tmp.243, 4
and turns it into:
%A = phi int [ 3/4, %cond_false.0 ], [ 2/4, %endif.0.i ], [ 2/4, %endif.1.i ]
which is later simplified (in this case) into %A = 0.
This triggers thousands of times in spec, for example, 269 times in 176.gcc.
This is tested by InstCombine/add.ll:test23 and set.ll:test18.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16582 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-29 05:07:12 +00:00
|
|
|
// FoldOpIntoPhi - Given a binary operator or cast instruction which has a
|
|
|
|
// PHI node as operand #0, see if we can fold the instruction into the PHI
|
|
|
|
// (which is only possible if all operands to the PHI are constants).
|
|
|
|
Instruction *FoldOpIntoPhi(Instruction &I);
|
|
|
|
|
2004-11-14 19:13:23 +00:00
|
|
|
// FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary"
|
|
|
|
// operator and they all are only used by the PHI, PHI together their
|
|
|
|
// inputs, and do the operation once, to the result of the PHI.
|
|
|
|
Instruction *FoldPHIArgOpIntoPHI(PHINode &PN);
|
2006-11-01 04:51:18 +00:00
|
|
|
Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN);
|
|
|
|
|
|
|
|
|
2007-01-11 12:24:14 +00:00
|
|
|
Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS,
|
|
|
|
ConstantInt *AndRHS, BinaryOperator &TheAnd);
|
Implement add.ll:test29. Codegening:
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus1 (unsigned int x) {
b.i += x;
}
as:
_plus1:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
add r3, r4, r3
rlwimi r3, r4, 0, 0, 25
stw r3, 0(r2)
blr
instead of:
_plus1:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
rlwinm r5, r4, 0, 26, 31
add r3, r5, r3
rlwimi r3, r4, 0, 0, 25
stw r3, 0(r2)
blr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23379 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 04:24:45 +00:00
|
|
|
|
2007-01-11 12:24:14 +00:00
|
|
|
Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask,
|
Implement add.ll:test29. Codegening:
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus1 (unsigned int x) {
b.i += x;
}
as:
_plus1:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
add r3, r4, r3
rlwimi r3, r4, 0, 0, 25
stw r3, 0(r2)
blr
instead of:
_plus1:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
rlwinm r5, r4, 0, 26, 31
add r3, r5, r3
rlwimi r3, r4, 0, 0, 25
stw r3, 0(r2)
blr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23379 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 04:24:45 +00:00
|
|
|
bool isSub, Instruction &I);
|
2004-09-29 17:40:11 +00:00
|
|
|
Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi,
|
2006-12-23 06:05:41 +00:00
|
|
|
bool isSigned, bool Inside, Instruction &IB);
|
2007-04-27 17:44:50 +00:00
|
|
|
Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocationInst &AI);
|
2006-06-15 19:07:26 +00:00
|
|
|
Instruction *MatchBSwap(BinaryOperator &I);
|
2007-04-15 00:07:55 +00:00
|
|
|
bool SimplifyStoreAtEndOfBlock(StoreInst &SI);
|
2008-01-13 23:50:23 +00:00
|
|
|
Instruction *SimplifyMemTransfer(MemIntrinsic *MI);
|
2008-04-30 06:39:11 +00:00
|
|
|
Instruction *SimplifyMemSet(MemSetInst *MI);
|
2008-01-13 23:50:23 +00:00
|
|
|
|
2006-06-15 19:07:26 +00:00
|
|
|
|
2006-12-13 18:21:21 +00:00
|
|
|
Value *EvaluateInDifferentType(Value *V, const Type *Ty, bool isSigned);
|
2008-04-10 18:43:06 +00:00
|
|
|
|
|
|
|
void ComputeMaskedBits(Value *V, const APInt &Mask, APInt& KnownZero,
|
|
|
|
APInt& KnownOne, unsigned Depth = 0);
|
|
|
|
bool MaskedValueIsZero(Value *V, const APInt& Mask, unsigned Depth = 0);
|
|
|
|
bool CanEvaluateInDifferentType(Value *V, const IntegerType *Ty,
|
|
|
|
unsigned CastOpc,
|
|
|
|
int &NumCastsRemoved);
|
|
|
|
unsigned GetOrEnforceKnownAlignment(Value *V,
|
|
|
|
unsigned PrefAlign = 0);
|
2002-04-18 17:39:14 +00:00
|
|
|
};
|
2002-07-23 18:06:35 +00:00
|
|
|
|
2007-05-03 01:11:54 +00:00
|
|
|
char InstCombiner::ID = 0;
|
2006-08-27 22:42:52 +00:00
|
|
|
RegisterPass<InstCombiner> X("instcombine", "Combine redundant instructions");
|
2002-04-18 17:39:14 +00:00
|
|
|
}
|
|
|
|
|
2003-03-10 21:43:22 +00:00
|
|
|
// getComplexity: Assign a complexity or rank value to LLVM Values...
|
2004-10-16 18:11:37 +00:00
|
|
|
// 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst
|
2003-03-10 21:43:22 +00:00
|
|
|
static unsigned getComplexity(Value *V) {
|
|
|
|
if (isa<Instruction>(V)) {
|
|
|
|
if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V))
|
2004-10-16 18:11:37 +00:00
|
|
|
return 3;
|
|
|
|
return 4;
|
2003-03-10 21:43:22 +00:00
|
|
|
}
|
2004-10-16 18:11:37 +00:00
|
|
|
if (isa<Argument>(V)) return 3;
|
|
|
|
return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2;
|
2003-03-10 21:43:22 +00:00
|
|
|
}
|
2002-04-18 17:39:14 +00:00
|
|
|
|
2003-03-11 00:12:48 +00:00
|
|
|
// isOnlyUse - Return true if this instruction will be deleted if we stop using
|
|
|
|
// it.
|
|
|
|
static bool isOnlyUse(Value *V) {
|
2003-10-15 16:48:29 +00:00
|
|
|
return V->hasOneUse() || isa<Constant>(V);
|
2003-03-11 00:12:48 +00:00
|
|
|
}
|
|
|
|
|
2004-02-23 06:38:22 +00:00
|
|
|
// getPromotedType - Return the specified type promoted as it would be to pass
|
|
|
|
// though a va_arg area...
|
|
|
|
static const Type *getPromotedType(const Type *Ty) {
|
For PR1064:
Implement the arbitrary bit-width integer feature. The feature allows
integers of any bitwidth (up to 64) to be defined instead of just 1, 8,
16, 32, and 64 bit integers.
This change does several things:
1. Introduces a new Derived Type, IntegerType, to represent the number of
bits in an integer. The Type classes SubclassData field is used to
store the number of bits. This allows 2^23 bits in an integer type.
2. Removes the five integer Type::TypeID values for the 1, 8, 16, 32 and
64-bit integers. These are replaced with just IntegerType which is not
a primitive any more.
3. Adjust the rest of LLVM to account for this change.
Note that while this incremental change lays the foundation for arbitrary
bit-width integers, LLVM has not yet been converted to actually deal with
them in any significant way. Most optimization passes, for example, will
still only deal with the byte-width integer types. Future increments
will rectify this situation.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33113 91177308-0d34-0410-b5e6-96231b3b80d8
2007-01-12 07:05:14 +00:00
|
|
|
if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) {
|
|
|
|
if (ITy->getBitWidth() < 32)
|
|
|
|
return Type::Int32Ty;
|
2007-05-23 01:17:04 +00:00
|
|
|
}
|
For PR1064:
Implement the arbitrary bit-width integer feature. The feature allows
integers of any bitwidth (up to 64) to be defined instead of just 1, 8,
16, 32, and 64 bit integers.
This change does several things:
1. Introduces a new Derived Type, IntegerType, to represent the number of
bits in an integer. The Type classes SubclassData field is used to
store the number of bits. This allows 2^23 bits in an integer type.
2. Removes the five integer Type::TypeID values for the 1, 8, 16, 32 and
64-bit integers. These are replaced with just IntegerType which is not
a primitive any more.
3. Adjust the rest of LLVM to account for this change.
Note that while this incremental change lays the foundation for arbitrary
bit-width integers, LLVM has not yet been converted to actually deal with
them in any significant way. Most optimization passes, for example, will
still only deal with the byte-width integer types. Future increments
will rectify this situation.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33113 91177308-0d34-0410-b5e6-96231b3b80d8
2007-01-12 07:05:14 +00:00
|
|
|
return Ty;
|
2004-02-23 06:38:22 +00:00
|
|
|
}
|
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
/// getBitCastOperand - If the specified operand is a CastInst or a constant
|
|
|
|
/// expression bitcast, return the operand value, otherwise return null.
|
|
|
|
static Value *getBitCastOperand(Value *V) {
|
|
|
|
if (BitCastInst *I = dyn_cast<BitCastInst>(V))
|
2005-09-13 00:40:14 +00:00
|
|
|
return I->getOperand(0);
|
|
|
|
else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
|
2006-11-27 01:05:10 +00:00
|
|
|
if (CE->getOpcode() == Instruction::BitCast)
|
2005-09-13 00:40:14 +00:00
|
|
|
return CE->getOperand(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
/// This function is a wrapper around CastInst::isEliminableCastPair. It
|
|
|
|
/// simply extracts arguments and returns what that function returns.
|
|
|
|
static Instruction::CastOps
|
|
|
|
isEliminableCastPair(
|
|
|
|
const CastInst *CI, ///< The first cast instruction
|
|
|
|
unsigned opcode, ///< The opcode of the second cast instruction
|
|
|
|
const Type *DstTy, ///< The target type for the second cast instruction
|
|
|
|
TargetData *TD ///< The target data for pointer size
|
|
|
|
) {
|
2006-05-06 09:00:16 +00:00
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
const Type *SrcTy = CI->getOperand(0)->getType(); // A from above
|
|
|
|
const Type *MidTy = CI->getType(); // B from above
|
2006-05-06 09:00:16 +00:00
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
// Get the opcodes of the two Cast instructions
|
|
|
|
Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode());
|
|
|
|
Instruction::CastOps secondOp = Instruction::CastOps(opcode);
|
2006-05-06 09:00:16 +00:00
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
return Instruction::CastOps(
|
|
|
|
CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy,
|
|
|
|
DstTy, TD->getIntPtrType()));
|
2006-05-06 09:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ValueRequiresCast - Return true if the cast from "V to Ty" actually results
|
|
|
|
/// in any code being generated. It does not require codegen if V is simple
|
|
|
|
/// enough or if the cast can be folded into other casts.
|
2006-12-23 06:05:41 +00:00
|
|
|
static bool ValueRequiresCast(Instruction::CastOps opcode, const Value *V,
|
|
|
|
const Type *Ty, TargetData *TD) {
|
2006-05-06 09:00:16 +00:00
|
|
|
if (V->getType() == Ty || isa<Constant>(V)) return false;
|
|
|
|
|
2006-05-25 23:24:33 +00:00
|
|
|
// If this is another cast that can be eliminated, it isn't codegen either.
|
2006-05-06 09:00:16 +00:00
|
|
|
if (const CastInst *CI = dyn_cast<CastInst>(V))
|
2006-12-23 06:05:41 +00:00
|
|
|
if (isEliminableCastPair(CI, opcode, Ty, TD))
|
2006-05-06 09:00:16 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// InsertOperandCastBefore - This inserts a cast of V to DestTy before the
|
|
|
|
/// InsertBefore instruction. This is specialized a bit to avoid inserting
|
|
|
|
/// casts that are known to not do anything...
|
|
|
|
///
|
2006-12-12 09:18:51 +00:00
|
|
|
Value *InstCombiner::InsertOperandCastBefore(Instruction::CastOps opcode,
|
|
|
|
Value *V, const Type *DestTy,
|
2006-05-06 09:00:16 +00:00
|
|
|
Instruction *InsertBefore) {
|
|
|
|
if (V->getType() == DestTy) return V;
|
|
|
|
if (Constant *C = dyn_cast<Constant>(V))
|
2006-12-12 09:18:51 +00:00
|
|
|
return ConstantExpr::getCast(opcode, C, DestTy);
|
2006-05-06 09:00:16 +00:00
|
|
|
|
2006-12-12 09:18:51 +00:00
|
|
|
return InsertCastBefore(opcode, V, DestTy, *InsertBefore);
|
2006-05-06 09:00:16 +00:00
|
|
|
}
|
|
|
|
|
2003-03-10 21:43:22 +00:00
|
|
|
// SimplifyCommutative - This performs a few simplifications for commutative
|
|
|
|
// operators:
|
|
|
|
//
|
|
|
|
// 1. Order operands such that they are listed from right (least complex) to
|
|
|
|
// left (most complex). This puts constants before unary operators before
|
|
|
|
// binary operators.
|
2002-04-18 17:39:14 +00:00
|
|
|
//
|
2003-03-11 00:12:48 +00:00
|
|
|
// 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2))
|
|
|
|
// 3. Transform: (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
|
2003-03-10 21:43:22 +00:00
|
|
|
//
|
2003-03-11 00:12:48 +00:00
|
|
|
bool InstCombiner::SimplifyCommutative(BinaryOperator &I) {
|
2003-03-10 21:43:22 +00:00
|
|
|
bool Changed = false;
|
|
|
|
if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1)))
|
|
|
|
Changed = !I.swapOperands();
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2003-03-10 21:43:22 +00:00
|
|
|
if (!I.isAssociative()) return Changed;
|
|
|
|
Instruction::BinaryOps Opcode = I.getOpcode();
|
2003-03-11 00:12:48 +00:00
|
|
|
if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0)))
|
|
|
|
if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) {
|
|
|
|
if (isa<Constant>(I.getOperand(1))) {
|
2003-05-27 16:40:51 +00:00
|
|
|
Constant *Folded = ConstantExpr::get(I.getOpcode(),
|
|
|
|
cast<Constant>(I.getOperand(1)),
|
|
|
|
cast<Constant>(Op->getOperand(1)));
|
2003-03-11 00:12:48 +00:00
|
|
|
I.setOperand(0, Op->getOperand(0));
|
|
|
|
I.setOperand(1, Folded);
|
|
|
|
return true;
|
|
|
|
} else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1)))
|
|
|
|
if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) &&
|
|
|
|
isOnlyUse(Op) && isOnlyUse(Op1)) {
|
|
|
|
Constant *C1 = cast<Constant>(Op->getOperand(1));
|
|
|
|
Constant *C2 = cast<Constant>(Op1->getOperand(1));
|
|
|
|
|
|
|
|
// Fold (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
|
2003-05-27 16:40:51 +00:00
|
|
|
Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2);
|
2003-03-11 00:12:48 +00:00
|
|
|
Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0),
|
|
|
|
Op1->getOperand(0),
|
|
|
|
Op1->getName(), &I);
|
2007-03-02 21:28:56 +00:00
|
|
|
AddToWorkList(New);
|
2003-03-11 00:12:48 +00:00
|
|
|
I.setOperand(0, New);
|
|
|
|
I.setOperand(1, Folded);
|
|
|
|
return true;
|
2005-04-21 23:48:37 +00:00
|
|
|
}
|
2003-03-10 21:43:22 +00:00
|
|
|
}
|
|
|
|
return Changed;
|
2002-04-18 17:39:14 +00:00
|
|
|
}
|
2001-12-14 16:52:21 +00:00
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
/// SimplifyCompare - For a CmpInst this function just orders the operands
|
|
|
|
/// so that theyare listed from right (least complex) to left (most complex).
|
|
|
|
/// This puts constants before unary operators before binary operators.
|
|
|
|
bool InstCombiner::SimplifyCompare(CmpInst &I) {
|
|
|
|
if (getComplexity(I.getOperand(0)) >= getComplexity(I.getOperand(1)))
|
|
|
|
return false;
|
|
|
|
I.swapOperands();
|
|
|
|
// Compare instructions are not associative so there's nothing else we can do.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2003-03-10 23:06:50 +00:00
|
|
|
// dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction
|
|
|
|
// if the LHS is a constant zero (which is the 'negate' form).
|
2002-05-06 16:49:18 +00:00
|
|
|
//
|
2003-03-10 23:06:50 +00:00
|
|
|
static inline Value *dyn_castNegVal(Value *V) {
|
|
|
|
if (BinaryOperator::isNeg(V))
|
2005-04-24 07:30:14 +00:00
|
|
|
return BinaryOperator::getNegArgument(V);
|
2003-03-10 23:06:50 +00:00
|
|
|
|
2004-12-14 20:08:06 +00:00
|
|
|
// Constants can be considered to be negated values if they can be folded.
|
|
|
|
if (ConstantInt *C = dyn_cast<ConstantInt>(V))
|
|
|
|
return ConstantExpr::getNeg(C);
|
2003-03-10 23:06:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Value *dyn_castNotVal(Value *V) {
|
|
|
|
if (BinaryOperator::isNot(V))
|
2005-04-24 07:30:14 +00:00
|
|
|
return BinaryOperator::getNotArgument(V);
|
2003-03-10 23:06:50 +00:00
|
|
|
|
|
|
|
// Constants can be considered to be not'ed values...
|
2007-01-11 12:24:14 +00:00
|
|
|
if (ConstantInt *C = dyn_cast<ConstantInt>(V))
|
2007-04-02 13:45:30 +00:00
|
|
|
return ConstantInt::get(~C->getValue());
|
2003-03-10 23:06:50 +00:00
|
|
|
return 0;
|
2002-05-06 16:49:18 +00:00
|
|
|
}
|
|
|
|
|
2003-03-11 00:12:48 +00:00
|
|
|
// dyn_castFoldableMul - If this value is a multiply that can be folded into
|
|
|
|
// other computations (because it has a constant operand), return the
|
2004-11-13 19:50:12 +00:00
|
|
|
// non-constant operand of the multiply, and set CST to point to the multiplier.
|
|
|
|
// Otherwise, return null.
|
2003-03-11 00:12:48 +00:00
|
|
|
//
|
2004-11-13 19:50:12 +00:00
|
|
|
static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) {
|
2007-01-15 02:27:26 +00:00
|
|
|
if (V->hasOneUse() && V->getType()->isInteger())
|
2004-11-13 19:50:12 +00:00
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V)) {
|
2003-03-11 00:12:48 +00:00
|
|
|
if (I->getOpcode() == Instruction::Mul)
|
2004-11-15 05:54:07 +00:00
|
|
|
if ((CST = dyn_cast<ConstantInt>(I->getOperand(1))))
|
2003-03-11 00:12:48 +00:00
|
|
|
return I->getOperand(0);
|
2004-11-13 19:50:12 +00:00
|
|
|
if (I->getOpcode() == Instruction::Shl)
|
2004-11-15 05:54:07 +00:00
|
|
|
if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) {
|
2004-11-13 19:50:12 +00:00
|
|
|
// The multiplier is really 1 << CST.
|
2007-03-29 01:57:21 +00:00
|
|
|
uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
|
2007-03-30 09:29:48 +00:00
|
|
|
uint32_t CSTVal = CST->getLimitedValue(BitWidth);
|
2007-03-29 01:57:21 +00:00
|
|
|
CST = ConstantInt::get(APInt(BitWidth, 1).shl(CSTVal));
|
2004-11-13 19:50:12 +00:00
|
|
|
return I->getOperand(0);
|
|
|
|
}
|
|
|
|
}
|
2003-03-11 00:12:48 +00:00
|
|
|
return 0;
|
2003-02-18 19:28:33 +00:00
|
|
|
}
|
2002-08-14 17:51:49 +00:00
|
|
|
|
2005-01-13 20:14:25 +00:00
|
|
|
/// dyn_castGetElementPtr - If this is a getelementptr instruction or constant
|
|
|
|
/// expression, return it.
|
|
|
|
static User *dyn_castGetElementPtr(Value *V) {
|
|
|
|
if (isa<GetElementPtrInst>(V)) return cast<User>(V);
|
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
|
|
|
|
if (CE->getOpcode() == Instruction::GetElementPtr)
|
|
|
|
return cast<User>(V);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-04-10 18:43:06 +00:00
|
|
|
/// getOpcode - If this is an Instruction or a ConstantExpr, return the
|
|
|
|
/// opcode value. Otherwise return UserOp1.
|
|
|
|
static unsigned getOpcode(User *U) {
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(U))
|
|
|
|
return I->getOpcode();
|
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U))
|
|
|
|
return CE->getOpcode();
|
|
|
|
// Use UserOp1 to mean there's no opcode.
|
|
|
|
return Instruction::UserOp1;
|
|
|
|
}
|
|
|
|
|
2007-03-25 05:33:51 +00:00
|
|
|
/// AddOne - Add one to a ConstantInt
|
2004-09-29 17:40:11 +00:00
|
|
|
static ConstantInt *AddOne(ConstantInt *C) {
|
2007-03-25 19:55:33 +00:00
|
|
|
APInt Val(C->getValue());
|
|
|
|
return ConstantInt::get(++Val);
|
Fold (and (setcc X, C1), (setcc X, C2))
This is important for several reasons:
1. Benchmarks have lots of code that looks like this (perlbmk in particular):
%tmp.2.i = setne int %tmp.0.i, 128 ; <bool> [#uses=1]
%tmp.6343 = seteq int %tmp.0.i, 1 ; <bool> [#uses=1]
%tmp.63 = and bool %tmp.2.i, %tmp.6343 ; <bool> [#uses=1]
we now fold away the setne, a clear improvement.
2. In the more important cases, such as (X >= 10) & (X < 20), we now produce
smaller code: (X-10) < 10.
3. Perhaps the nicest effect of this patch is that it really helps out the
code generators. In particular, for a 'range test' like the above,
instead of generating this on X86 (the difference on PPC is even more
pronounced):
cmp %EAX, 50
setge %CL
cmp %EAX, 100
setl %AL
and %CL, %AL
cmp %CL, 0
we now generate this:
add %EAX, -50
cmp %EAX, 50
Furthermore, this causes setcc's to be folded into branches more often.
These combinations trigger dozens of times in the spec benchmarks, particularly
in 176.gcc, 186.crafty, 253.perlbmk, 254.gap, & 099.go.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16559 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-28 21:48:02 +00:00
|
|
|
}
|
2007-03-25 05:33:51 +00:00
|
|
|
/// SubOne - Subtract one from a ConstantInt
|
2004-09-29 17:40:11 +00:00
|
|
|
static ConstantInt *SubOne(ConstantInt *C) {
|
2007-03-25 19:55:33 +00:00
|
|
|
APInt Val(C->getValue());
|
|
|
|
return ConstantInt::get(--Val);
|
2007-03-25 05:33:51 +00:00
|
|
|
}
|
|
|
|
/// Add - Add two ConstantInts together
|
|
|
|
static ConstantInt *Add(ConstantInt *C1, ConstantInt *C2) {
|
|
|
|
return ConstantInt::get(C1->getValue() + C2->getValue());
|
|
|
|
}
|
|
|
|
/// And - Bitwise AND two ConstantInts together
|
|
|
|
static ConstantInt *And(ConstantInt *C1, ConstantInt *C2) {
|
|
|
|
return ConstantInt::get(C1->getValue() & C2->getValue());
|
|
|
|
}
|
|
|
|
/// Subtract - Subtract one ConstantInt from another
|
|
|
|
static ConstantInt *Subtract(ConstantInt *C1, ConstantInt *C2) {
|
|
|
|
return ConstantInt::get(C1->getValue() - C2->getValue());
|
|
|
|
}
|
|
|
|
/// Multiply - Multiply two ConstantInts together
|
|
|
|
static ConstantInt *Multiply(ConstantInt *C1, ConstantInt *C2) {
|
|
|
|
return ConstantInt::get(C1->getValue() * C2->getValue());
|
Fold (and (setcc X, C1), (setcc X, C2))
This is important for several reasons:
1. Benchmarks have lots of code that looks like this (perlbmk in particular):
%tmp.2.i = setne int %tmp.0.i, 128 ; <bool> [#uses=1]
%tmp.6343 = seteq int %tmp.0.i, 1 ; <bool> [#uses=1]
%tmp.63 = and bool %tmp.2.i, %tmp.6343 ; <bool> [#uses=1]
we now fold away the setne, a clear improvement.
2. In the more important cases, such as (X >= 10) & (X < 20), we now produce
smaller code: (X-10) < 10.
3. Perhaps the nicest effect of this patch is that it really helps out the
code generators. In particular, for a 'range test' like the above,
instead of generating this on X86 (the difference on PPC is even more
pronounced):
cmp %EAX, 50
setge %CL
cmp %EAX, 100
setl %AL
and %CL, %AL
cmp %CL, 0
we now generate this:
add %EAX, -50
cmp %EAX, 50
Furthermore, this causes setcc's to be folded into branches more often.
These combinations trigger dozens of times in the spec benchmarks, particularly
in 176.gcc, 186.crafty, 253.perlbmk, 254.gap, & 099.go.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16559 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-28 21:48:02 +00:00
|
|
|
}
|
2008-02-18 22:48:05 +00:00
|
|
|
/// MultiplyOverflows - True if the multiply can not be expressed in an int
|
|
|
|
/// this size.
|
|
|
|
static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign) {
|
|
|
|
uint32_t W = C1->getBitWidth();
|
|
|
|
APInt LHSExt = C1->getValue(), RHSExt = C2->getValue();
|
|
|
|
if (sign) {
|
|
|
|
LHSExt.sext(W * 2);
|
|
|
|
RHSExt.sext(W * 2);
|
|
|
|
} else {
|
|
|
|
LHSExt.zext(W * 2);
|
|
|
|
RHSExt.zext(W * 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
APInt MulExt = LHSExt * RHSExt;
|
|
|
|
|
|
|
|
if (sign) {
|
|
|
|
APInt Min = APInt::getSignedMinValue(W).sext(W * 2);
|
|
|
|
APInt Max = APInt::getSignedMaxValue(W).sext(W * 2);
|
|
|
|
return MulExt.slt(Min) || MulExt.sgt(Max);
|
|
|
|
} else
|
|
|
|
return MulExt.ugt(APInt::getLowBitsSet(W * 2, W));
|
|
|
|
}
|
Fold (and (setcc X, C1), (setcc X, C2))
This is important for several reasons:
1. Benchmarks have lots of code that looks like this (perlbmk in particular):
%tmp.2.i = setne int %tmp.0.i, 128 ; <bool> [#uses=1]
%tmp.6343 = seteq int %tmp.0.i, 1 ; <bool> [#uses=1]
%tmp.63 = and bool %tmp.2.i, %tmp.6343 ; <bool> [#uses=1]
we now fold away the setne, a clear improvement.
2. In the more important cases, such as (X >= 10) & (X < 20), we now produce
smaller code: (X-10) < 10.
3. Perhaps the nicest effect of this patch is that it really helps out the
code generators. In particular, for a 'range test' like the above,
instead of generating this on X86 (the difference on PPC is even more
pronounced):
cmp %EAX, 50
setge %CL
cmp %EAX, 100
setl %AL
and %CL, %AL
cmp %CL, 0
we now generate this:
add %EAX, -50
cmp %EAX, 50
Furthermore, this causes setcc's to be folded into branches more often.
These combinations trigger dozens of times in the spec benchmarks, particularly
in 176.gcc, 186.crafty, 253.perlbmk, 254.gap, & 099.go.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16559 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-28 21:48:02 +00:00
|
|
|
|
2007-03-08 01:46:38 +00:00
|
|
|
/// ComputeMaskedBits - Determine which of the bits specified in Mask are
|
|
|
|
/// known to be either zero or one and return them in the KnownZero/KnownOne
|
|
|
|
/// bit sets. This code only analyzes bits in Mask, in order to short-circuit
|
|
|
|
/// processing.
|
|
|
|
/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
|
|
|
|
/// we cannot optimize based on the assumption that it is zero without changing
|
|
|
|
/// it to be an explicit zero. If we don't change it to zero, other code could
|
|
|
|
/// optimized based on the contradictory assumption that it is non-zero.
|
|
|
|
/// Because instcombine aggressively folds operations with undef args anyway,
|
|
|
|
/// this won't lose us code quality.
|
2008-04-10 18:43:06 +00:00
|
|
|
void InstCombiner::ComputeMaskedBits(Value *V, const APInt &Mask,
|
|
|
|
APInt& KnownZero, APInt& KnownOne,
|
|
|
|
unsigned Depth) {
|
2007-03-13 02:23:10 +00:00
|
|
|
assert(V && "No Value?");
|
|
|
|
assert(Depth <= 6 && "Limit Search Depth");
|
2007-03-08 01:46:38 +00:00
|
|
|
uint32_t BitWidth = Mask.getBitWidth();
|
2008-04-10 18:43:06 +00:00
|
|
|
assert((V->getType()->isInteger() || isa<PointerType>(V->getType())) &&
|
|
|
|
"Not integer or pointer type!");
|
|
|
|
assert((!TD || TD->getTypeSizeInBits(V->getType()) == BitWidth) &&
|
|
|
|
(!isa<IntegerType>(V->getType()) ||
|
|
|
|
V->getType()->getPrimitiveSizeInBits() == BitWidth) &&
|
2007-03-13 02:23:10 +00:00
|
|
|
KnownZero.getBitWidth() == BitWidth &&
|
2007-03-08 01:46:38 +00:00
|
|
|
KnownOne.getBitWidth() == BitWidth &&
|
2007-03-28 02:19:03 +00:00
|
|
|
"V, Mask, KnownOne and KnownZero should have same BitWidth");
|
2007-03-08 01:46:38 +00:00
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
|
|
|
|
// We know all of the bits for a constant!
|
2007-03-13 02:23:10 +00:00
|
|
|
KnownOne = CI->getValue() & Mask;
|
2007-03-08 01:46:38 +00:00
|
|
|
KnownZero = ~KnownOne & Mask;
|
|
|
|
return;
|
|
|
|
}
|
2008-04-10 18:43:06 +00:00
|
|
|
// Null is all-zeros.
|
|
|
|
if (isa<ConstantPointerNull>(V)) {
|
|
|
|
KnownOne.clear();
|
|
|
|
KnownZero = Mask;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// The address of an aligned GlobalValue has trailing zeros.
|
|
|
|
if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
|
|
|
|
unsigned Align = GV->getAlignment();
|
|
|
|
if (Align == 0 && TD && GV->getType()->getElementType()->isSized())
|
|
|
|
Align = TD->getPrefTypeAlignment(GV->getType()->getElementType());
|
|
|
|
if (Align > 0)
|
|
|
|
KnownZero = Mask & APInt::getLowBitsSet(BitWidth,
|
|
|
|
CountTrailingZeros_32(Align));
|
|
|
|
else
|
|
|
|
KnownZero.clear();
|
|
|
|
KnownOne.clear();
|
|
|
|
return;
|
|
|
|
}
|
2007-03-08 01:46:38 +00:00
|
|
|
|
2008-04-28 17:02:21 +00:00
|
|
|
KnownZero.clear(); KnownOne.clear(); // Start out not knowing anything.
|
|
|
|
|
2007-03-08 01:46:38 +00:00
|
|
|
if (Depth == 6 || Mask == 0)
|
|
|
|
return; // Limit search depth.
|
|
|
|
|
2008-04-10 18:43:06 +00:00
|
|
|
User *I = dyn_cast<User>(V);
|
2007-03-08 01:46:38 +00:00
|
|
|
if (!I) return;
|
|
|
|
|
|
|
|
APInt KnownZero2(KnownZero), KnownOne2(KnownOne);
|
2008-04-10 18:43:06 +00:00
|
|
|
switch (getOpcode(I)) {
|
|
|
|
default: break;
|
2007-03-25 02:03:12 +00:00
|
|
|
case Instruction::And: {
|
2007-03-08 01:46:38 +00:00
|
|
|
// If either the LHS or the RHS are Zero, the result is zero.
|
|
|
|
ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
|
2007-03-25 02:03:12 +00:00
|
|
|
APInt Mask2(Mask & ~KnownZero);
|
|
|
|
ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
|
2007-03-08 01:46:38 +00:00
|
|
|
assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
|
|
|
|
assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
|
|
|
|
|
|
|
|
// Output known-1 bits are only known if set in both the LHS & RHS.
|
|
|
|
KnownOne &= KnownOne2;
|
|
|
|
// Output known-0 are known to be clear if zero in either the LHS | RHS.
|
|
|
|
KnownZero |= KnownZero2;
|
|
|
|
return;
|
2007-03-25 02:03:12 +00:00
|
|
|
}
|
|
|
|
case Instruction::Or: {
|
2007-03-08 01:46:38 +00:00
|
|
|
ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
|
2007-03-25 02:03:12 +00:00
|
|
|
APInt Mask2(Mask & ~KnownOne);
|
|
|
|
ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
|
2007-03-08 01:46:38 +00:00
|
|
|
assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
|
|
|
|
assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
|
|
|
|
|
|
|
|
// Output known-0 bits are only known if clear in both the LHS & RHS.
|
|
|
|
KnownZero &= KnownZero2;
|
|
|
|
// Output known-1 are known to be set if set in either the LHS | RHS.
|
|
|
|
KnownOne |= KnownOne2;
|
|
|
|
return;
|
2007-03-25 02:03:12 +00:00
|
|
|
}
|
2007-03-08 01:46:38 +00:00
|
|
|
case Instruction::Xor: {
|
|
|
|
ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
|
|
|
|
ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
|
|
|
|
assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
|
|
|
|
assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
|
|
|
|
|
|
|
|
// Output known-0 bits are known if clear or set in both the LHS & RHS.
|
|
|
|
APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
|
|
|
|
// Output known-1 are known to be set if set in only one of the LHS, RHS.
|
|
|
|
KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
|
|
|
|
KnownZero = KnownZeroOut;
|
|
|
|
return;
|
|
|
|
}
|
2008-04-10 18:43:06 +00:00
|
|
|
case Instruction::Mul: {
|
|
|
|
APInt Mask2 = APInt::getAllOnesValue(BitWidth);
|
|
|
|
ComputeMaskedBits(I->getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
|
|
|
|
ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
|
|
|
|
assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
|
|
|
|
assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
|
|
|
|
|
|
|
|
// If low bits are zero in either operand, output low known-0 bits.
|
2008-04-28 17:02:21 +00:00
|
|
|
// Also compute a conserative estimate for high known-0 bits.
|
2008-04-10 18:43:06 +00:00
|
|
|
// More trickiness is possible, but this is sufficient for the
|
|
|
|
// interesting case of alignment computation.
|
|
|
|
KnownOne.clear();
|
|
|
|
unsigned TrailZ = KnownZero.countTrailingOnes() +
|
|
|
|
KnownZero2.countTrailingOnes();
|
2008-04-28 17:02:21 +00:00
|
|
|
unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
|
|
|
|
KnownZero2.countLeadingOnes() +
|
|
|
|
1, BitWidth) - BitWidth;
|
|
|
|
|
2008-04-10 18:43:06 +00:00
|
|
|
TrailZ = std::min(TrailZ, BitWidth);
|
2008-04-28 17:02:21 +00:00
|
|
|
LeadZ = std::min(LeadZ, BitWidth);
|
|
|
|
KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
|
|
|
|
APInt::getHighBitsSet(BitWidth, LeadZ);
|
2008-04-10 18:43:06 +00:00
|
|
|
KnownZero &= Mask;
|
|
|
|
return;
|
|
|
|
}
|
2008-04-28 17:02:21 +00:00
|
|
|
case Instruction::UDiv: {
|
|
|
|
// For the purposes of computing leading zeros we can conservatively
|
|
|
|
// treat a udiv as a logical right shift by the power of 2 known to
|
|
|
|
// be greater than the denominator.
|
|
|
|
APInt AllOnes = APInt::getAllOnesValue(BitWidth);
|
|
|
|
ComputeMaskedBits(I->getOperand(0),
|
|
|
|
AllOnes, KnownZero2, KnownOne2, Depth+1);
|
|
|
|
unsigned LeadZ = KnownZero2.countLeadingOnes();
|
|
|
|
|
|
|
|
KnownOne2.clear();
|
|
|
|
KnownZero2.clear();
|
|
|
|
ComputeMaskedBits(I->getOperand(1),
|
|
|
|
AllOnes, KnownZero2, KnownOne2, Depth+1);
|
|
|
|
LeadZ = std::min(BitWidth,
|
|
|
|
LeadZ + BitWidth - KnownOne2.countLeadingZeros());
|
|
|
|
|
|
|
|
KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
|
|
|
|
return;
|
|
|
|
}
|
2007-03-08 01:46:38 +00:00
|
|
|
case Instruction::Select:
|
|
|
|
ComputeMaskedBits(I->getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
|
|
|
|
ComputeMaskedBits(I->getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
|
|
|
|
assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
|
|
|
|
assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
|
|
|
|
|
|
|
|
// Only known if known in both the LHS and RHS.
|
|
|
|
KnownOne &= KnownOne2;
|
|
|
|
KnownZero &= KnownZero2;
|
|
|
|
return;
|
|
|
|
case Instruction::FPTrunc:
|
|
|
|
case Instruction::FPExt:
|
|
|
|
case Instruction::FPToUI:
|
|
|
|
case Instruction::FPToSI:
|
|
|
|
case Instruction::SIToFP:
|
|
|
|
case Instruction::UIToFP:
|
2008-04-10 18:43:06 +00:00
|
|
|
return; // Can't work with floating point.
|
|
|
|
case Instruction::PtrToInt:
|
2007-03-08 01:46:38 +00:00
|
|
|
case Instruction::IntToPtr:
|
2008-04-10 18:43:06 +00:00
|
|
|
// We can't handle these if we don't know the pointer size.
|
|
|
|
if (!TD) return;
|
|
|
|
// Fall through and handle them the same as zext/trunc.
|
|
|
|
case Instruction::ZExt:
|
2007-03-13 02:23:10 +00:00
|
|
|
case Instruction::Trunc: {
|
2007-03-08 01:46:38 +00:00
|
|
|
// All these have integer operands
|
2008-04-10 18:43:06 +00:00
|
|
|
const Type *SrcTy = I->getOperand(0)->getType();
|
|
|
|
uint32_t SrcBitWidth = TD ?
|
|
|
|
TD->getTypeSizeInBits(SrcTy) :
|
|
|
|
SrcTy->getPrimitiveSizeInBits();
|
2007-03-28 02:19:03 +00:00
|
|
|
APInt MaskIn(Mask);
|
2008-04-10 18:43:06 +00:00
|
|
|
MaskIn.zextOrTrunc(SrcBitWidth);
|
|
|
|
KnownZero.zextOrTrunc(SrcBitWidth);
|
|
|
|
KnownOne.zextOrTrunc(SrcBitWidth);
|
2007-03-28 02:19:03 +00:00
|
|
|
ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, Depth+1);
|
2008-04-10 18:43:06 +00:00
|
|
|
KnownZero.zextOrTrunc(BitWidth);
|
|
|
|
KnownOne.zextOrTrunc(BitWidth);
|
|
|
|
// Any top bits are known to be zero.
|
|
|
|
if (BitWidth > SrcBitWidth)
|
|
|
|
KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
|
2007-03-08 01:46:38 +00:00
|
|
|
return;
|
2007-03-13 02:23:10 +00:00
|
|
|
}
|
2007-03-08 01:46:38 +00:00
|
|
|
case Instruction::BitCast: {
|
|
|
|
const Type *SrcTy = I->getOperand(0)->getType();
|
2008-04-10 18:43:06 +00:00
|
|
|
if (SrcTy->isInteger() || isa<PointerType>(SrcTy)) {
|
2007-03-08 01:46:38 +00:00
|
|
|
ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Instruction::SExt: {
|
|
|
|
// Compute the bits in the result that are not present in the input.
|
|
|
|
const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType());
|
2007-03-13 02:23:10 +00:00
|
|
|
uint32_t SrcBitWidth = SrcTy->getBitWidth();
|
2007-03-25 04:26:16 +00:00
|
|
|
|
2007-03-28 02:19:03 +00:00
|
|
|
APInt MaskIn(Mask);
|
|
|
|
MaskIn.trunc(SrcBitWidth);
|
|
|
|
KnownZero.trunc(SrcBitWidth);
|
|
|
|
KnownOne.trunc(SrcBitWidth);
|
|
|
|
ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, Depth+1);
|
2007-03-08 01:46:38 +00:00
|
|
|
assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
|
2007-03-13 02:23:10 +00:00
|
|
|
KnownZero.zext(BitWidth);
|
2007-03-23 21:24:59 +00:00
|
|
|
KnownOne.zext(BitWidth);
|
|
|
|
|
|
|
|
// If the sign bit of the input is known set or clear, then we know the
|
|
|
|
// top bits of the result.
|
2007-03-28 02:19:03 +00:00
|
|
|
if (KnownZero[SrcBitWidth-1]) // Input sign bit known zero
|
2007-03-28 17:38:21 +00:00
|
|
|
KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
|
2007-03-28 02:19:03 +00:00
|
|
|
else if (KnownOne[SrcBitWidth-1]) // Input sign bit known set
|
2007-03-28 17:38:21 +00:00
|
|
|
KnownOne |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
|
2007-03-23 21:24:59 +00:00
|
|
|
return;
|
2006-02-07 06:56:34 +00:00
|
|
|
}
|
|
|
|
case Instruction::Shl:
|
2007-03-23 21:24:59 +00:00
|
|
|
// (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
|
2006-10-20 07:07:24 +00:00
|
|
|
if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
|
2007-03-30 09:29:48 +00:00
|
|
|
uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
|
2007-03-25 02:03:12 +00:00
|
|
|
APInt Mask2(Mask.lshr(ShiftAmt));
|
|
|
|
ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, Depth+1);
|
2006-02-11 09:31:47 +00:00
|
|
|
assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
|
2006-10-20 07:07:24 +00:00
|
|
|
KnownZero <<= ShiftAmt;
|
|
|
|
KnownOne <<= ShiftAmt;
|
2007-03-25 19:55:33 +00:00
|
|
|
KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); // low bits known 0
|
2007-03-23 21:24:59 +00:00
|
|
|
return;
|
2006-02-11 09:31:47 +00:00
|
|
|
}
|
2006-02-07 06:56:34 +00:00
|
|
|
break;
|
2006-11-08 06:47:33 +00:00
|
|
|
case Instruction::LShr:
|
2007-03-23 21:24:59 +00:00
|
|
|
// (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
|
2006-11-08 06:47:33 +00:00
|
|
|
if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
|
|
|
|
// Compute the new bits that are at the top now.
|
2007-03-30 09:29:48 +00:00
|
|
|
uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
|
2007-03-23 21:24:59 +00:00
|
|
|
|
2006-11-08 06:47:33 +00:00
|
|
|
// Unsigned shift right.
|
2007-03-25 02:03:12 +00:00
|
|
|
APInt Mask2(Mask.shl(ShiftAmt));
|
|
|
|
ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero,KnownOne,Depth+1);
|
2007-03-23 21:24:59 +00:00
|
|
|
assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
|
|
|
|
KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
|
|
|
|
KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
|
2007-03-28 02:19:03 +00:00
|
|
|
// high bits known zero.
|
|
|
|
KnownZero |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
|
2007-03-23 21:24:59 +00:00
|
|
|
return;
|
2006-11-08 06:47:33 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Instruction::AShr:
|
2007-03-28 02:19:03 +00:00
|
|
|
// (ashr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
|
2006-10-20 07:07:24 +00:00
|
|
|
if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
|
2006-02-11 09:31:47 +00:00
|
|
|
// Compute the new bits that are at the top now.
|
2007-03-30 09:29:48 +00:00
|
|
|
uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
|
2007-03-23 21:24:59 +00:00
|
|
|
|
2006-11-08 06:47:33 +00:00
|
|
|
// Signed shift right.
|
2007-03-25 02:03:12 +00:00
|
|
|
APInt Mask2(Mask.shl(ShiftAmt));
|
|
|
|
ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero,KnownOne,Depth+1);
|
2007-03-23 21:24:59 +00:00
|
|
|
assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
|
|
|
|
KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
|
|
|
|
KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
|
2006-02-11 09:31:47 +00:00
|
|
|
|
2007-03-28 02:19:03 +00:00
|
|
|
APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
|
|
|
|
if (KnownZero[BitWidth-ShiftAmt-1]) // New bits are known zero.
|
2007-03-23 21:24:59 +00:00
|
|
|
KnownZero |= HighBits;
|
2007-03-28 02:19:03 +00:00
|
|
|
else if (KnownOne[BitWidth-ShiftAmt-1]) // New bits are known one.
|
2006-11-08 06:47:33 +00:00
|
|
|
KnownOne |= HighBits;
|
2007-03-23 21:24:59 +00:00
|
|
|
return;
|
2006-02-11 09:31:47 +00:00
|
|
|
}
|
2006-02-07 06:56:34 +00:00
|
|
|
break;
|
2008-04-10 18:43:06 +00:00
|
|
|
case Instruction::Sub: {
|
|
|
|
if (ConstantInt *CLHS = dyn_cast<ConstantInt>(I->getOperand(0))) {
|
|
|
|
// We know that the top bits of C-X are clear if X contains less bits
|
|
|
|
// than C (i.e. no wrap-around can happen). For example, 20-X is
|
|
|
|
// positive if we can prove that X is >= 0 and < 16.
|
|
|
|
if (!CLHS->getValue().isNegative()) {
|
|
|
|
unsigned NLZ = (CLHS->getValue()+1).countLeadingZeros();
|
|
|
|
// NLZ can't be BitWidth with no sign bit
|
|
|
|
APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
|
2008-04-28 17:02:21 +00:00
|
|
|
ComputeMaskedBits(I->getOperand(1), MaskV, KnownZero2, KnownOne2,
|
|
|
|
Depth+1);
|
2008-04-10 18:43:06 +00:00
|
|
|
|
2008-04-28 17:02:21 +00:00
|
|
|
// If all of the MaskV bits are known to be zero, then we know the
|
|
|
|
// output top bits are zero, because we now know that the output is
|
|
|
|
// from [0-C].
|
|
|
|
if ((KnownZero2 & MaskV) == MaskV) {
|
2008-04-10 18:43:06 +00:00
|
|
|
unsigned NLZ2 = CLHS->getValue().countLeadingZeros();
|
|
|
|
// Top bits known zero.
|
|
|
|
KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// fall through
|
2008-03-21 08:32:17 +00:00
|
|
|
case Instruction::Add: {
|
2008-03-21 05:19:58 +00:00
|
|
|
// Output known-0 bits are known if clear or set in both the low clear bits
|
|
|
|
// common to both LHS & RHS. For example, 8+(X<<3) is known to have the
|
|
|
|
// low 3 bits clear.
|
2008-04-28 17:02:21 +00:00
|
|
|
APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
|
|
|
|
ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
|
|
|
|
assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
|
|
|
|
unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
|
|
|
|
|
|
|
|
ComputeMaskedBits(I->getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
|
|
|
|
assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
|
|
|
|
KnownZeroOut = std::min(KnownZeroOut,
|
|
|
|
KnownZero2.countTrailingOnes());
|
|
|
|
|
|
|
|
KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
|
2008-03-21 05:19:58 +00:00
|
|
|
return;
|
2008-03-21 08:32:17 +00:00
|
|
|
}
|
2008-03-06 06:48:30 +00:00
|
|
|
case Instruction::SRem:
|
|
|
|
if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
|
|
|
|
APInt RA = Rem->getValue();
|
|
|
|
if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
|
|
|
|
APInt LowBits = RA.isStrictlyPositive() ? ((RA - 1) | RA) : ~RA;
|
|
|
|
APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
|
|
|
|
ComputeMaskedBits(I->getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
|
|
|
|
|
|
|
|
// The sign of a remainder is equal to the sign of the first
|
|
|
|
// operand (zero being positive).
|
|
|
|
if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
|
|
|
|
KnownZero2 |= ~LowBits;
|
|
|
|
else if (KnownOne2[BitWidth-1])
|
|
|
|
KnownOne2 |= ~LowBits;
|
|
|
|
|
|
|
|
KnownZero |= KnownZero2 & Mask;
|
|
|
|
KnownOne |= KnownOne2 & Mask;
|
|
|
|
|
|
|
|
assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2008-04-28 17:02:21 +00:00
|
|
|
case Instruction::URem: {
|
2008-03-06 06:48:30 +00:00
|
|
|
if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
|
|
|
|
APInt RA = Rem->getValue();
|
|
|
|
if (RA.isStrictlyPositive() && RA.isPowerOf2()) {
|
|
|
|
APInt LowBits = (RA - 1) | RA;
|
|
|
|
APInt Mask2 = LowBits & Mask;
|
|
|
|
KnownZero |= ~LowBits & Mask;
|
|
|
|
ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
|
|
|
|
assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
|
2008-04-28 17:02:21 +00:00
|
|
|
break;
|
2008-03-06 06:48:30 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-28 17:02:21 +00:00
|
|
|
|
|
|
|
// Since the result is less than or equal to either operand, any leading
|
|
|
|
// zero bits in either operand must also exist in the result.
|
|
|
|
APInt AllOnes = APInt::getAllOnesValue(BitWidth);
|
|
|
|
ComputeMaskedBits(I->getOperand(0), AllOnes, KnownZero, KnownOne,
|
|
|
|
Depth+1);
|
|
|
|
ComputeMaskedBits(I->getOperand(1), AllOnes, KnownZero2, KnownOne2,
|
|
|
|
Depth+1);
|
|
|
|
|
|
|
|
uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
|
|
|
|
KnownZero2.countLeadingOnes());
|
|
|
|
KnownOne.clear();
|
|
|
|
KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
|
2008-03-06 06:48:30 +00:00
|
|
|
break;
|
2008-04-28 17:02:21 +00:00
|
|
|
}
|
2008-04-10 18:43:06 +00:00
|
|
|
|
|
|
|
case Instruction::Alloca:
|
|
|
|
case Instruction::Malloc: {
|
|
|
|
AllocationInst *AI = cast<AllocationInst>(V);
|
|
|
|
unsigned Align = AI->getAlignment();
|
|
|
|
if (Align == 0 && TD) {
|
|
|
|
if (isa<AllocaInst>(AI))
|
|
|
|
Align = TD->getPrefTypeAlignment(AI->getType()->getElementType());
|
|
|
|
else if (isa<MallocInst>(AI)) {
|
|
|
|
// Malloc returns maximally aligned memory.
|
|
|
|
Align = TD->getABITypeAlignment(AI->getType()->getElementType());
|
|
|
|
Align =
|
|
|
|
std::max(Align,
|
|
|
|
(unsigned)TD->getABITypeAlignment(Type::DoubleTy));
|
|
|
|
Align =
|
|
|
|
std::max(Align,
|
|
|
|
(unsigned)TD->getABITypeAlignment(Type::Int64Ty));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Align > 0)
|
|
|
|
KnownZero = Mask & APInt::getLowBitsSet(BitWidth,
|
|
|
|
CountTrailingZeros_32(Align));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Instruction::GetElementPtr: {
|
|
|
|
// Analyze all of the subscripts of this getelementptr instruction
|
|
|
|
// to determine if we can prove known low zero bits.
|
|
|
|
APInt LocalMask = APInt::getAllOnesValue(BitWidth);
|
|
|
|
APInt LocalKnownZero(BitWidth, 0), LocalKnownOne(BitWidth, 0);
|
|
|
|
ComputeMaskedBits(I->getOperand(0), LocalMask,
|
|
|
|
LocalKnownZero, LocalKnownOne, Depth+1);
|
|
|
|
unsigned TrailZ = LocalKnownZero.countTrailingOnes();
|
|
|
|
|
|
|
|
gep_type_iterator GTI = gep_type_begin(I);
|
|
|
|
for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
|
|
|
|
Value *Index = I->getOperand(i);
|
|
|
|
if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
|
|
|
|
// Handle struct member offset arithmetic.
|
|
|
|
if (!TD) return;
|
|
|
|
const StructLayout *SL = TD->getStructLayout(STy);
|
|
|
|
unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
|
|
|
|
uint64_t Offset = SL->getElementOffset(Idx);
|
|
|
|
TrailZ = std::min(TrailZ,
|
|
|
|
CountTrailingZeros_64(Offset));
|
|
|
|
} else {
|
|
|
|
// Handle array index arithmetic.
|
|
|
|
const Type *IndexedTy = GTI.getIndexedType();
|
|
|
|
if (!IndexedTy->isSized()) return;
|
|
|
|
unsigned GEPOpiBits = Index->getType()->getPrimitiveSizeInBits();
|
|
|
|
uint64_t TypeSize = TD ? TD->getABITypeSize(IndexedTy) : 1;
|
|
|
|
LocalMask = APInt::getAllOnesValue(GEPOpiBits);
|
|
|
|
LocalKnownZero = LocalKnownOne = APInt(GEPOpiBits, 0);
|
|
|
|
ComputeMaskedBits(Index, LocalMask,
|
|
|
|
LocalKnownZero, LocalKnownOne, Depth+1);
|
|
|
|
TrailZ = std::min(TrailZ,
|
|
|
|
CountTrailingZeros_64(TypeSize) +
|
|
|
|
LocalKnownZero.countTrailingOnes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) & Mask;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Instruction::PHI: {
|
|
|
|
PHINode *P = cast<PHINode>(I);
|
|
|
|
// Handle the case of a simple two-predecessor recurrence PHI.
|
|
|
|
// There's a lot more that could theoretically be done here, but
|
|
|
|
// this is sufficient to catch some interesting cases.
|
|
|
|
if (P->getNumIncomingValues() == 2) {
|
|
|
|
for (unsigned i = 0; i != 2; ++i) {
|
|
|
|
Value *L = P->getIncomingValue(i);
|
|
|
|
Value *R = P->getIncomingValue(!i);
|
|
|
|
User *LU = dyn_cast<User>(L);
|
|
|
|
unsigned Opcode = LU ? getOpcode(LU) : (unsigned)Instruction::UserOp1;
|
|
|
|
// Check for operations that have the property that if
|
|
|
|
// both their operands have low zero bits, the result
|
|
|
|
// will have low zero bits.
|
|
|
|
if (Opcode == Instruction::Add ||
|
|
|
|
Opcode == Instruction::Sub ||
|
|
|
|
Opcode == Instruction::And ||
|
|
|
|
Opcode == Instruction::Or ||
|
|
|
|
Opcode == Instruction::Mul) {
|
|
|
|
Value *LL = LU->getOperand(0);
|
|
|
|
Value *LR = LU->getOperand(1);
|
|
|
|
// Find a recurrence.
|
|
|
|
if (LL == I)
|
|
|
|
L = LR;
|
|
|
|
else if (LR == I)
|
|
|
|
L = LL;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
// Ok, we have a PHI of the form L op= R. Check for low
|
|
|
|
// zero bits.
|
|
|
|
APInt Mask2 = APInt::getAllOnesValue(BitWidth);
|
|
|
|
ComputeMaskedBits(R, Mask2, KnownZero2, KnownOne2, Depth+1);
|
|
|
|
Mask2 = APInt::getLowBitsSet(BitWidth,
|
|
|
|
KnownZero2.countTrailingOnes());
|
|
|
|
KnownOne2.clear();
|
|
|
|
KnownZero2.clear();
|
|
|
|
ComputeMaskedBits(L, Mask2, KnownZero2, KnownOne2, Depth+1);
|
|
|
|
KnownZero = Mask &
|
|
|
|
APInt::getLowBitsSet(BitWidth,
|
|
|
|
KnownZero2.countTrailingOnes());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-04-28 17:02:21 +00:00
|
|
|
case Instruction::Call:
|
|
|
|
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
|
|
|
|
switch (II->getIntrinsicID()) {
|
|
|
|
default: break;
|
|
|
|
case Intrinsic::ctpop:
|
|
|
|
case Intrinsic::ctlz:
|
|
|
|
case Intrinsic::cttz: {
|
|
|
|
unsigned LowBits = Log2_32(BitWidth)+1;
|
|
|
|
KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2006-02-07 06:56:34 +00:00
|
|
|
}
|
2007-03-23 21:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
|
|
|
|
/// this predicate to simplify operations downstream. Mask is known to be zero
|
|
|
|
/// for bits that V cannot have.
|
2008-04-10 18:43:06 +00:00
|
|
|
bool InstCombiner::MaskedValueIsZero(Value *V, const APInt& Mask,
|
|
|
|
unsigned Depth) {
|
2007-03-23 21:24:59 +00:00
|
|
|
APInt KnownZero(Mask.getBitWidth(), 0), KnownOne(Mask.getBitWidth(), 0);
|
|
|
|
ComputeMaskedBits(V, Mask, KnownZero, KnownOne, Depth);
|
|
|
|
assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
|
|
|
|
return (KnownZero & Mask) == Mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ShrinkDemandedConstant - Check to see if the specified operand of the
|
|
|
|
/// specified instruction is a constant integer. If so, check to see if there
|
|
|
|
/// are any bits set in the constant that are not demanded. If so, shrink the
|
|
|
|
/// constant and return true.
|
|
|
|
static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo,
|
|
|
|
APInt Demanded) {
|
|
|
|
assert(I && "No instruction?");
|
|
|
|
assert(OpNo < I->getNumOperands() && "Operand index too large");
|
|
|
|
|
|
|
|
// If the operand is not a constant integer, nothing to do.
|
|
|
|
ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo));
|
|
|
|
if (!OpC) return false;
|
|
|
|
|
|
|
|
// If there are no bits set that aren't demanded, nothing to do.
|
|
|
|
Demanded.zextOrTrunc(OpC->getValue().getBitWidth());
|
|
|
|
if ((~Demanded & OpC->getValue()) == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// This instruction is producing bits that are not demanded. Shrink the RHS.
|
|
|
|
Demanded &= OpC->getValue();
|
|
|
|
I->setOperand(OpNo, ConstantInt::get(Demanded));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a
|
|
|
|
// set of known zero and one bits, compute the maximum and minimum values that
|
|
|
|
// could have the specified known zero and known one bits, returning them in
|
|
|
|
// min/max.
|
|
|
|
static void ComputeSignedMinMaxValuesFromKnownBits(const Type *Ty,
|
|
|
|
const APInt& KnownZero,
|
|
|
|
const APInt& KnownOne,
|
|
|
|
APInt& Min, APInt& Max) {
|
|
|
|
uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth();
|
|
|
|
assert(KnownZero.getBitWidth() == BitWidth &&
|
|
|
|
KnownOne.getBitWidth() == BitWidth &&
|
|
|
|
Min.getBitWidth() == BitWidth && Max.getBitWidth() == BitWidth &&
|
|
|
|
"Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth.");
|
2007-03-25 04:26:16 +00:00
|
|
|
APInt UnknownBits = ~(KnownZero|KnownOne);
|
2007-03-23 21:24:59 +00:00
|
|
|
|
|
|
|
// The minimum value is when all unknown bits are zeros, EXCEPT for the sign
|
|
|
|
// bit if it is unknown.
|
|
|
|
Min = KnownOne;
|
|
|
|
Max = KnownOne|UnknownBits;
|
|
|
|
|
2007-03-28 05:15:57 +00:00
|
|
|
if (UnknownBits[BitWidth-1]) { // Sign bit is unknown
|
2007-04-02 13:45:30 +00:00
|
|
|
Min.set(BitWidth-1);
|
|
|
|
Max.clear(BitWidth-1);
|
2007-03-23 21:24:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and
|
|
|
|
// a set of known zero and one bits, compute the maximum and minimum values that
|
|
|
|
// could have the specified known zero and known one bits, returning them in
|
|
|
|
// min/max.
|
|
|
|
static void ComputeUnsignedMinMaxValuesFromKnownBits(const Type *Ty,
|
2007-08-05 08:47:58 +00:00
|
|
|
const APInt &KnownZero,
|
|
|
|
const APInt &KnownOne,
|
|
|
|
APInt &Min, APInt &Max) {
|
|
|
|
uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); BitWidth = BitWidth;
|
2007-03-23 21:24:59 +00:00
|
|
|
assert(KnownZero.getBitWidth() == BitWidth &&
|
|
|
|
KnownOne.getBitWidth() == BitWidth &&
|
|
|
|
Min.getBitWidth() == BitWidth && Max.getBitWidth() &&
|
|
|
|
"Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth.");
|
2007-03-25 04:26:16 +00:00
|
|
|
APInt UnknownBits = ~(KnownZero|KnownOne);
|
2006-02-11 09:31:47 +00:00
|
|
|
|
2007-03-23 21:24:59 +00:00
|
|
|
// The minimum value is when the unknown bits are all zeros.
|
|
|
|
Min = KnownOne;
|
|
|
|
// The maximum value is when the unknown bits are all ones.
|
|
|
|
Max = KnownOne|UnknownBits;
|
|
|
|
}
|
2006-02-07 06:56:34 +00:00
|
|
|
|
2007-03-12 17:25:59 +00:00
|
|
|
/// SimplifyDemandedBits - This function attempts to replace V with a simpler
|
|
|
|
/// value based on the demanded bits. When this function is called, it is known
|
|
|
|
/// that only the bits set in DemandedMask of the result of V are ever used
|
|
|
|
/// downstream. Consequently, depending on the mask and V, it may be possible
|
|
|
|
/// to replace V with a constant or one of its operands. In such cases, this
|
|
|
|
/// function does the replacement and returns true. In all other cases, it
|
|
|
|
/// returns false after analyzing the expression and setting KnownOne and known
|
|
|
|
/// to be one in the expression. KnownZero contains all the bits that are known
|
|
|
|
/// to be zero in the expression. These are provided to potentially allow the
|
|
|
|
/// caller (which might recursively be SimplifyDemandedBits itself) to simplify
|
|
|
|
/// the expression. KnownOne and KnownZero always follow the invariant that
|
|
|
|
/// KnownOne & KnownZero == 0. That is, a bit can't be both 1 and 0. Note that
|
|
|
|
/// the bits in KnownOne and KnownZero may only be accurate for those bits set
|
|
|
|
/// in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero
|
|
|
|
/// and KnownOne must all be the same.
|
|
|
|
bool InstCombiner::SimplifyDemandedBits(Value *V, APInt DemandedMask,
|
|
|
|
APInt& KnownZero, APInt& KnownOne,
|
|
|
|
unsigned Depth) {
|
|
|
|
assert(V != 0 && "Null pointer of Value???");
|
|
|
|
assert(Depth <= 6 && "Limit Search Depth");
|
|
|
|
uint32_t BitWidth = DemandedMask.getBitWidth();
|
|
|
|
const IntegerType *VTy = cast<IntegerType>(V->getType());
|
|
|
|
assert(VTy->getBitWidth() == BitWidth &&
|
|
|
|
KnownZero.getBitWidth() == BitWidth &&
|
|
|
|
KnownOne.getBitWidth() == BitWidth &&
|
|
|
|
"Value *V, DemandedMask, KnownZero and KnownOne \
|
|
|
|
must have same BitWidth");
|
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
|
|
|
|
// We know all of the bits for a constant!
|
|
|
|
KnownOne = CI->getValue() & DemandedMask;
|
|
|
|
KnownZero = ~KnownOne & DemandedMask;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-03-14 03:21:24 +00:00
|
|
|
KnownZero.clear();
|
|
|
|
KnownOne.clear();
|
2007-03-12 17:25:59 +00:00
|
|
|
if (!V->hasOneUse()) { // Other users may use these bits.
|
|
|
|
if (Depth != 0) { // Not at the root.
|
|
|
|
// Just compute the KnownZero/KnownOne bits to simplify things downstream.
|
|
|
|
ComputeMaskedBits(V, DemandedMask, KnownZero, KnownOne, Depth);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// If this is the root being simplified, allow it to have multiple uses,
|
|
|
|
// just set the DemandedMask to all bits.
|
|
|
|
DemandedMask = APInt::getAllOnesValue(BitWidth);
|
|
|
|
} else if (DemandedMask == 0) { // Not demanding any bits from V.
|
|
|
|
if (V != UndefValue::get(VTy))
|
|
|
|
return UpdateValueUsesWith(V, UndefValue::get(VTy));
|
|
|
|
return false;
|
|
|
|
} else if (Depth == 6) { // Limit search depth.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *I = dyn_cast<Instruction>(V);
|
|
|
|
if (!I) return false; // Only analyze instructions.
|
|
|
|
|
|
|
|
APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
|
|
|
|
APInt &RHSKnownZero = KnownZero, &RHSKnownOne = KnownOne;
|
|
|
|
switch (I->getOpcode()) {
|
2008-04-28 17:02:21 +00:00
|
|
|
default:
|
|
|
|
ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth);
|
|
|
|
break;
|
2007-03-12 17:25:59 +00:00
|
|
|
case Instruction::And:
|
|
|
|
// If either the LHS or the RHS are Zero, the result is zero.
|
|
|
|
if (SimplifyDemandedBits(I->getOperand(1), DemandedMask,
|
|
|
|
RHSKnownZero, RHSKnownOne, Depth+1))
|
|
|
|
return true;
|
|
|
|
assert((RHSKnownZero & RHSKnownOne) == 0 &&
|
|
|
|
"Bits known to be one AND zero?");
|
|
|
|
|
|
|
|
// If something is known zero on the RHS, the bits aren't demanded on the
|
|
|
|
// LHS.
|
|
|
|
if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownZero,
|
|
|
|
LHSKnownZero, LHSKnownOne, Depth+1))
|
|
|
|
return true;
|
|
|
|
assert((LHSKnownZero & LHSKnownOne) == 0 &&
|
|
|
|
"Bits known to be one AND zero?");
|
|
|
|
|
|
|
|
// If all of the demanded bits are known 1 on one side, return the other.
|
|
|
|
// These bits cannot contribute to the result of the 'and'.
|
|
|
|
if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
|
|
|
|
(DemandedMask & ~LHSKnownZero))
|
|
|
|
return UpdateValueUsesWith(I, I->getOperand(0));
|
|
|
|
if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
|
|
|
|
(DemandedMask & ~RHSKnownZero))
|
|
|
|
return UpdateValueUsesWith(I, I->getOperand(1));
|
|
|
|
|
|
|
|
// If all of the demanded bits in the inputs are known zeros, return zero.
|
|
|
|
if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
|
|
|
|
return UpdateValueUsesWith(I, Constant::getNullValue(VTy));
|
|
|
|
|
|
|
|
// If the RHS is a constant, see if we can simplify it.
|
|
|
|
if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero))
|
|
|
|
return UpdateValueUsesWith(I, I);
|
|
|
|
|
|
|
|
// Output known-1 bits are only known if set in both the LHS & RHS.
|
|
|
|
RHSKnownOne &= LHSKnownOne;
|
|
|
|
// Output known-0 are known to be clear if zero in either the LHS | RHS.
|
|
|
|
RHSKnownZero |= LHSKnownZero;
|
|
|
|
break;
|
|
|
|
case Instruction::Or:
|
|
|
|
// If either the LHS or the RHS are One, the result is One.
|
|
|
|
if (SimplifyDemandedBits(I->getOperand(1), DemandedMask,
|
|
|
|
RHSKnownZero, RHSKnownOne, Depth+1))
|
|
|
|
return true;
|
|
|
|
assert((RHSKnownZero & RHSKnownOne) == 0 &&
|
|
|
|
"Bits known to be one AND zero?");
|
|
|
|
// If something is known one on the RHS, the bits aren't demanded on the
|
|
|
|
// LHS.
|
|
|
|
if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownOne,
|
|
|
|
LHSKnownZero, LHSKnownOne, Depth+1))
|
|
|
|
return true;
|
|
|
|
assert((LHSKnownZero & LHSKnownOne) == 0 &&
|
|
|
|
"Bits known to be one AND zero?");
|
|
|
|
|
|
|
|
// If all of the demanded bits are known zero on one side, return the other.
|
|
|
|
// These bits cannot contribute to the result of the 'or'.
|
|
|
|
if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
|
|
|
|
(DemandedMask & ~LHSKnownOne))
|
|
|
|
return UpdateValueUsesWith(I, I->getOperand(0));
|
|
|
|
if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
|
|
|
|
(DemandedMask & ~RHSKnownOne))
|
|
|
|
return UpdateValueUsesWith(I, I->getOperand(1));
|
|
|
|
|
|
|
|
// If all of the potentially set bits on one side are known to be set on
|
|
|
|
// the other side, just use the 'other' side.
|
|
|
|
if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
|
|
|
|
(DemandedMask & (~RHSKnownZero)))
|
|
|
|
return UpdateValueUsesWith(I, I->getOperand(0));
|
|
|
|
if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
|
|
|
|
(DemandedMask & (~LHSKnownZero)))
|
|
|
|
return UpdateValueUsesWith(I, I->getOperand(1));
|
|
|
|
|
|
|
|
// If the RHS is a constant, see if we can simplify it.
|
|
|
|
if (ShrinkDemandedConstant(I, 1, DemandedMask))
|
|
|
|
return UpdateValueUsesWith(I, I);
|
|
|
|
|
|
|
|
// Output known-0 bits are only known if clear in both the LHS & RHS.
|
|
|
|
RHSKnownZero &= LHSKnownZero;
|
|
|
|
// Output known-1 are known to be set if set in either the LHS | RHS.
|
|
|
|
RHSKnownOne |= LHSKnownOne;
|
|
|
|
break;
|
|
|
|
case Instruction::Xor: {
|
|
|
|
if (SimplifyDemandedBits(I->getOperand(1), DemandedMask,
|
|
|
|
RHSKnownZero, RHSKnownOne, Depth+1))
|
|
|
|
return true;
|
|
|
|
assert((RHSKnownZero & RHSKnownOne) == 0 &&
|
|
|
|
"Bits known to be one AND zero?");
|
|
|
|
if (SimplifyDemandedBits(I->getOperand(0), DemandedMask,
|
|
|
|
LHSKnownZero, LHSKnownOne, Depth+1))
|
|
|
|
return true;
|
|
|
|
assert((LHSKnownZero & LHSKnownOne) == 0 &&
|
|
|
|
"Bits known to be one AND zero?");
|
|
|
|
|
|
|
|
// If all of the demanded bits are known zero on one side, return the other.
|
|
|
|
// These bits cannot contribute to the result of the 'xor'.
|
|
|
|
if ((DemandedMask & RHSKnownZero) == DemandedMask)
|
|
|
|
return UpdateValueUsesWith(I, I->getOperand(0));
|
|
|
|
if ((DemandedMask & LHSKnownZero) == DemandedMask)
|
|
|
|
return UpdateValueUsesWith(I, I->getOperand(1));
|
|
|
|
|
|
|
|
// Output known-0 bits are known if clear or set in both the LHS & RHS.
|
|
|
|
APInt KnownZeroOut = (RHSKnownZero & LHSKnownZero) |
|
|
|
|
(RHSKnownOne & LHSKnownOne);
|
|
|
|
// Output known-1 are known to be set if set in only one of the LHS, RHS.
|
|
|
|
APInt KnownOneOut = (RHSKnownZero & LHSKnownOne) |
|
|
|
|
(RHSKnownOne & LHSKnownZero);
|
|
|
|
|
|
|
|
// If all of the demanded bits are known to be zero on one side or the
|
|
|
|
// other, turn this into an *inclusive* or.
|
|
|
|
// e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
|
|
|
|
if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) {
|
|
|
|
Instruction *Or =
|
|
|
|
BinaryOperator::createOr(I->getOperand(0), I->getOperand(1),
|
|
|
|
I->getName());
|
|
|
|
InsertNewInstBefore(Or, *I);
|
|
|
|
return UpdateValueUsesWith(I, Or);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If all of the demanded bits on one side are known, and all of the set
|
|
|
|
// bits on that side are also known to be set on the other side, turn this
|
|
|
|
// into an AND, as we know the bits will be cleared.
|
|
|
|
// e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
|
|
|
|
if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) {
|
|
|
|
// all known
|
|
|
|
if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) {
|
|
|
|
Constant *AndC = ConstantInt::get(~RHSKnownOne & DemandedMask);
|
|
|
|
Instruction *And =
|
|
|
|
BinaryOperator::createAnd(I->getOperand(0), AndC, "tmp");
|
|
|
|
InsertNewInstBefore(And, *I);
|
|
|
|
return UpdateValueUsesWith(I, And);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the RHS is a constant, see if we can simplify it.
|
|
|
|
// FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
|
|
|
|
if (ShrinkDemandedConstant(I, 1, DemandedMask))
|
|
|
|
return UpdateValueUsesWith(I, I);
|
|
|
|
|
|
|
|
RHSKnownZero = KnownZeroOut;
|
|
|
|
RHSKnownOne = KnownOneOut;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Instruction::Select:
|
|
|
|
if (SimplifyDemandedBits(I->getOperand(2), DemandedMask,
|
|
|
|
RHSKnownZero, RHSKnownOne, Depth+1))
|
|
|
|
return true;
|
|
|
|
if (SimplifyDemandedBits(I->getOperand(1), DemandedMask,
|
|
|
|
LHSKnownZero, LHSKnownOne, Depth+1))
|
|
|
|
return true;
|
|
|
|
assert((RHSKnownZero & RHSKnownOne) == 0 &&
|
|
|
|
"Bits known to be one AND zero?");
|
|
|
|
assert((LHSKnownZero & LHSKnownOne) == 0 &&
|
|
|
|
"Bits known to be one AND zero?");
|
|
|
|
|
|
|
|
// If the operands are constants, see if we can simplify them.
|
|
|
|
if (ShrinkDemandedConstant(I, 1, DemandedMask))
|
|
|
|
return UpdateValueUsesWith(I, I);
|
|
|
|
if (ShrinkDemandedConstant(I, 2, DemandedMask))
|
|
|
|
return UpdateValueUsesWith(I, I);
|
|
|
|
|
|
|
|
// Only known if known in both the LHS and RHS.
|
|
|
|
RHSKnownOne &= LHSKnownOne;
|
|
|
|
RHSKnownZero &= LHSKnownZero;
|
|
|
|
break;
|
|
|
|
case Instruction::Trunc: {
|
|
|
|
uint32_t truncBf =
|
|
|
|
cast<IntegerType>(I->getOperand(0)->getType())->getBitWidth();
|
2007-03-29 02:26:30 +00:00
|
|
|
DemandedMask.zext(truncBf);
|
|
|
|
RHSKnownZero.zext(truncBf);
|
|
|
|
RHSKnownOne.zext(truncBf);
|
|
|
|
if (SimplifyDemandedBits(I->getOperand(0), DemandedMask,
|
|
|
|
RHSKnownZero, RHSKnownOne, Depth+1))
|
2007-03-12 17:25:59 +00:00
|
|
|
return true;
|
|
|
|
DemandedMask.trunc(BitWidth);
|
|
|
|
RHSKnownZero.trunc(BitWidth);
|
|
|
|
RHSKnownOne.trunc(BitWidth);
|
|
|
|
assert((RHSKnownZero & RHSKnownOne) == 0 &&
|
|
|
|
"Bits known to be one AND zero?");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Instruction::BitCast:
|
|
|
|
if (!I->getOperand(0)->getType()->isInteger())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (SimplifyDemandedBits(I->getOperand(0), DemandedMask,
|
|
|
|
RHSKnownZero, RHSKnownOne, Depth+1))
|
|
|
|
return true;
|
|
|
|
assert((RHSKnownZero & RHSKnownOne) == 0 &&
|
|
|
|
"Bits known to be one AND zero?");
|
|
|
|
break;
|
|
|
|
case Instruction::ZExt: {
|
|
|
|
// Compute the bits in the result that are not present in the input.
|
|
|
|
const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType());
|
2007-03-25 04:26:16 +00:00
|
|
|
uint32_t SrcBitWidth = SrcTy->getBitWidth();
|
2007-03-12 17:25:59 +00:00
|
|
|
|
2007-03-29 04:45:55 +00:00
|
|
|
DemandedMask.trunc(SrcBitWidth);
|
|
|
|
RHSKnownZero.trunc(SrcBitWidth);
|
|
|
|
RHSKnownOne.trunc(SrcBitWidth);
|
2007-03-29 02:26:30 +00:00
|
|
|
if (SimplifyDemandedBits(I->getOperand(0), DemandedMask,
|
|
|
|
RHSKnownZero, RHSKnownOne, Depth+1))
|
2007-03-12 17:25:59 +00:00
|
|
|
return true;
|
|
|
|
DemandedMask.zext(BitWidth);
|
|
|
|
RHSKnownZero.zext(BitWidth);
|
|
|
|
RHSKnownOne.zext(BitWidth);
|
|
|
|
assert((RHSKnownZero & RHSKnownOne) == 0 &&
|
|
|
|
"Bits known to be one AND zero?");
|
|
|
|
// The top bits are known to be zero.
|
2007-03-29 02:26:30 +00:00
|
|
|
RHSKnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
|
2007-03-12 17:25:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Instruction::SExt: {
|
|
|
|
// Compute the bits in the result that are not present in the input.
|
|
|
|
const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType());
|
2007-03-25 04:26:16 +00:00
|
|
|
uint32_t SrcBitWidth = SrcTy->getBitWidth();
|
2007-03-12 17:25:59 +00:00
|
|
|
|
|
|
|
APInt InputDemandedBits = DemandedMask &
|
2007-03-29 02:26:30 +00:00
|
|
|
APInt::getLowBitsSet(BitWidth, SrcBitWidth);
|
2007-03-12 17:25:59 +00:00
|
|
|
|
2007-03-29 02:26:30 +00:00
|
|
|
APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth));
|
2007-03-12 17:25:59 +00:00
|
|
|
// If any of the sign extended bits are demanded, we know that the sign
|
|
|
|
// bit is demanded.
|
|
|
|
if ((NewBits & DemandedMask) != 0)
|
2007-04-02 13:45:30 +00:00
|
|
|
InputDemandedBits.set(SrcBitWidth-1);
|
2007-03-12 17:25:59 +00:00
|
|
|
|
2007-03-29 04:45:55 +00:00
|
|
|
InputDemandedBits.trunc(SrcBitWidth);
|
|
|
|
RHSKnownZero.trunc(SrcBitWidth);
|
|
|
|
RHSKnownOne.trunc(SrcBitWidth);
|
2007-03-29 02:26:30 +00:00
|
|
|
if (SimplifyDemandedBits(I->getOperand(0), InputDemandedBits,
|
|
|
|
RHSKnownZero, RHSKnownOne, Depth+1))
|
2007-03-12 17:25:59 +00:00
|
|
|
return true;
|
|
|
|
InputDemandedBits.zext(BitWidth);
|
|
|
|
RHSKnownZero.zext(BitWidth);
|
|
|
|
RHSKnownOne.zext(BitWidth);
|
|
|
|
assert((RHSKnownZero & RHSKnownOne) == 0 &&
|
|
|
|
"Bits known to be one AND zero?");
|
|
|
|
|
|
|
|
// If the sign bit of the input is known set or clear, then we know the
|
|
|
|
// top bits of the result.
|
|
|
|
|
|
|
|
// If the input sign bit is known zero, or if the NewBits are not demanded
|
|
|
|
// convert this into a zero extension.
|
2007-03-29 02:26:30 +00:00
|
|
|
if (RHSKnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits)
|
2007-03-12 17:25:59 +00:00
|
|
|
{
|
|
|
|
// Convert to ZExt cast
|
|
|
|
CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName(), I);
|
|
|
|
return UpdateValueUsesWith(I, NewCast);
|
2007-03-29 02:26:30 +00:00
|
|
|
} else if (RHSKnownOne[SrcBitWidth-1]) { // Input sign bit known set
|
2007-03-12 17:25:59 +00:00
|
|
|
RHSKnownOne |= NewBits;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Instruction::Add: {
|
|
|
|
// Figure out what the input bits are. If the top bits of the and result
|
|
|
|
// are not demanded, then the add doesn't demand them from its input
|
|
|
|
// either.
|
2007-03-25 21:11:44 +00:00
|
|
|
uint32_t NLZ = DemandedMask.countLeadingZeros();
|
2007-03-12 17:25:59 +00:00
|
|
|
|
|
|
|
// If there is a constant on the RHS, there are a variety of xformations
|
|
|
|
// we can do.
|
|
|
|
if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
|
|
|
|
// If null, this should be simplified elsewhere. Some of the xforms here
|
|
|
|
// won't work if the RHS is zero.
|
|
|
|
if (RHS->isZero())
|
|
|
|
break;
|
|
|
|
|
|
|
|
// If the top bit of the output is demanded, demand everything from the
|
|
|
|
// input. Otherwise, we demand all the input bits except NLZ top bits.
|
2007-03-29 02:26:30 +00:00
|
|
|
APInt InDemandedBits(APInt::getLowBitsSet(BitWidth, BitWidth - NLZ));
|
2007-03-12 17:25:59 +00:00
|
|
|
|
|
|
|
// Find information about known zero/one bits in the input.
|
|
|
|
if (SimplifyDemandedBits(I->getOperand(0), InDemandedBits,
|
|
|
|
LHSKnownZero, LHSKnownOne, Depth+1))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// If the RHS of the add has bits set that can't affect the input, reduce
|
|
|
|
// the constant.
|
|
|
|
if (ShrinkDemandedConstant(I, 1, InDemandedBits))
|
|
|
|
return UpdateValueUsesWith(I, I);
|
|
|
|
|
|
|
|
// Avoid excess work.
|
|
|
|
if (LHSKnownZero == 0 && LHSKnownOne == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Turn it into OR if input bits are zero.
|
|
|
|
if ((LHSKnownZero & RHS->getValue()) == RHS->getValue()) {
|
|
|
|
Instruction *Or =
|
|
|
|
BinaryOperator::createOr(I->getOperand(0), I->getOperand(1),
|
|
|
|
I->getName());
|
|
|
|
InsertNewInstBefore(Or, *I);
|
|
|
|
return UpdateValueUsesWith(I, Or);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can say something about the output known-zero and known-one bits,
|
|
|
|
// depending on potential carries from the input constant and the
|
|
|
|
// unknowns. For example if the LHS is known to have at most the 0x0F0F0
|
|
|
|
// bits set and the RHS constant is 0x01001, then we know we have a known
|
|
|
|
// one mask of 0x00001 and a known zero mask of 0xE0F0E.
|
|
|
|
|
|
|
|
// To compute this, we first compute the potential carry bits. These are
|
|
|
|
// the bits which may be modified. I'm not aware of a better way to do
|
|
|
|
// this scan.
|
2007-03-31 02:38:39 +00:00
|
|
|
const APInt& RHSVal = RHS->getValue();
|
|
|
|
APInt CarryBits((~LHSKnownZero + RHSVal) ^ (~LHSKnownZero ^ RHSVal));
|
2007-03-12 17:25:59 +00:00
|
|
|
|
|
|
|
// Now that we know which bits have carries, compute the known-1/0 sets.
|
|
|
|
|
|
|
|
// Bits are known one if they are known zero in one operand and one in the
|
|
|
|
// other, and there is no input carry.
|
|
|
|
RHSKnownOne = ((LHSKnownZero & RHSVal) |
|
|
|
|
(LHSKnownOne & ~RHSVal)) & ~CarryBits;
|
|
|
|
|
|
|
|
// Bits are known zero if they are known zero in both operands and there
|
|
|
|
// is no input carry.
|
|
|
|
RHSKnownZero = LHSKnownZero & ~RHSVal & ~CarryBits;
|
|
|
|
} else {
|
|
|
|
// If the high-bits of this ADD are not demanded, then it does not demand
|
|
|
|
// the high bits of its LHS or RHS.
|
2007-03-29 02:26:30 +00:00
|
|
|
if (DemandedMask[BitWidth-1] == 0) {
|
2007-03-12 17:25:59 +00:00
|
|
|
// Right fill the mask of bits for this ADD to demand the most
|
|
|
|
// significant bit and all those below it.
|
2007-03-29 02:26:30 +00:00
|
|
|
APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
|
2007-03-12 17:25:59 +00:00
|
|
|
if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps,
|
|
|
|
LHSKnownZero, LHSKnownOne, Depth+1))
|
|
|
|
return true;
|
|
|
|
if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps,
|
|
|
|
LHSKnownZero, LHSKnownOne, Depth+1))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Instruction::Sub:
|
|
|
|
// If the high-bits of this SUB are not demanded, then it does not demand
|
|
|
|
// the high bits of its LHS or RHS.
|
2007-03-29 02:26:30 +00:00
|
|
|
if (DemandedMask[BitWidth-1] == 0) {
|
2007-03-12 17:25:59 +00:00
|
|
|
// Right fill the mask of bits for this SUB to demand the most
|
|
|
|
// significant bit and all those below it.
|
2007-04-02 08:20:41 +00:00
|
|
|
uint32_t NLZ = DemandedMask.countLeadingZeros();
|
2007-03-29 02:26:30 +00:00
|
|
|
APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
|
2007-03-12 17:25:59 +00:00
|
|
|
if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps,
|
|
|
|
LHSKnownZero, LHSKnownOne, Depth+1))
|
|
|
|
return true;
|
|
|
|
if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps,
|
|
|
|
LHSKnownZero, LHSKnownOne, Depth+1))
|
|
|
|
return true;
|
|
|
|
}
|
2008-04-28 17:02:21 +00:00
|
|
|
// Otherwise just hand the sub off to ComputeMaskedBits to fill in
|
|
|
|
// the known zeros and ones.
|
|
|
|
ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth);
|
2007-03-12 17:25:59 +00:00
|
|
|
break;
|
|
|
|
case Instruction::Shl:
|
|
|
|
if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
|
2007-03-30 09:29:48 +00:00
|
|
|
uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
|
2007-03-29 02:26:30 +00:00
|
|
|
APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
|
|
|
|
if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn,
|
2007-03-12 17:25:59 +00:00
|
|
|
RHSKnownZero, RHSKnownOne, Depth+1))
|
|
|
|
return true;
|
|
|
|
assert((RHSKnownZero & RHSKnownOne) == 0 &&
|
|
|
|
"Bits known to be one AND zero?");
|
|
|
|
RHSKnownZero <<= ShiftAmt;
|
|
|
|
RHSKnownOne <<= ShiftAmt;
|
|
|
|
// low bits known zero.
|
2007-03-14 09:07:33 +00:00
|
|
|
if (ShiftAmt)
|
2007-03-28 15:02:20 +00:00
|
|
|
RHSKnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
|
2007-03-12 17:25:59 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Instruction::LShr:
|
|
|
|
// For a logical shift right
|
|
|
|
if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
|
2007-03-30 09:29:48 +00:00
|
|
|
uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
|
2007-03-12 17:25:59 +00:00
|
|
|
|
|
|
|
// Unsigned shift right.
|
2007-03-29 02:26:30 +00:00
|
|
|
APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
|
|
|
|
if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn,
|
2007-03-12 17:25:59 +00:00
|
|
|
RHSKnownZero, RHSKnownOne, Depth+1))
|
|
|
|
return true;
|
|
|
|
assert((RHSKnownZero & RHSKnownOne) == 0 &&
|
|
|
|
"Bits known to be one AND zero?");
|
|
|
|
RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt);
|
|
|
|
RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt);
|
2007-03-14 09:07:33 +00:00
|
|
|
if (ShiftAmt) {
|
|
|
|
// Compute the new bits that are at the top now.
|
2007-03-29 02:26:30 +00:00
|
|
|
APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
|
2007-03-14 09:07:33 +00:00
|
|
|
RHSKnownZero |= HighBits; // high bits known zero.
|
|
|
|
}
|
2007-03-12 17:25:59 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Instruction::AShr:
|
|
|
|
// If this is an arithmetic shift right and only the low-bit is set, we can
|
|
|
|
// always convert this into a logical shr, even if the shift amount is
|
|
|
|
// variable. The low bit of the shift cannot be an input sign bit unless
|
|
|
|
// the shift amount is >= the size of the datatype, which is undefined.
|
|
|
|
if (DemandedMask == 1) {
|
|
|
|
// Perform the logical shift right.
|
|
|
|
Value *NewVal = BinaryOperator::createLShr(
|
|
|
|
I->getOperand(0), I->getOperand(1), I->getName());
|
|
|
|
InsertNewInstBefore(cast<Instruction>(NewVal), *I);
|
|
|
|
return UpdateValueUsesWith(I, NewVal);
|
|
|
|
}
|
2007-07-15 20:54:51 +00:00
|
|
|
|
|
|
|
// If the sign bit is the only bit demanded by this ashr, then there is no
|
|
|
|
// need to do it, the shift doesn't change the high bit.
|
|
|
|
if (DemandedMask.isSignBit())
|
|
|
|
return UpdateValueUsesWith(I, I->getOperand(0));
|
2007-03-12 17:25:59 +00:00
|
|
|
|
|
|
|
if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
|
2007-03-30 17:20:39 +00:00
|
|
|
uint32_t ShiftAmt = SA->getLimitedValue(BitWidth);
|
2007-03-12 17:25:59 +00:00
|
|
|
|
|
|
|
// Signed shift right.
|
2007-03-29 02:26:30 +00:00
|
|
|
APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
|
2007-06-06 17:08:48 +00:00
|
|
|
// If any of the "high bits" are demanded, we should set the sign bit as
|
|
|
|
// demanded.
|
|
|
|
if (DemandedMask.countLeadingZeros() <= ShiftAmt)
|
|
|
|
DemandedMaskIn.set(BitWidth-1);
|
2007-03-12 17:25:59 +00:00
|
|
|
if (SimplifyDemandedBits(I->getOperand(0),
|
2007-03-29 02:26:30 +00:00
|
|
|
DemandedMaskIn,
|
2007-03-12 17:25:59 +00:00
|
|
|
RHSKnownZero, RHSKnownOne, Depth+1))
|
|
|
|
return true;
|
|
|
|
assert((RHSKnownZero & RHSKnownOne) == 0 &&
|
|
|
|
"Bits known to be one AND zero?");
|
|
|
|
// Compute the new bits that are at the top now.
|
2007-03-29 02:26:30 +00:00
|
|
|
APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
|
2007-03-12 17:25:59 +00:00
|
|
|
RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt);
|
|
|
|
RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt);
|
|
|
|
|
|
|
|
// Handle the sign bits.
|
|
|
|
APInt SignBit(APInt::getSignBit(BitWidth));
|
|
|
|
// Adjust to where it is now in the mask.
|
|
|
|
SignBit = APIntOps::lshr(SignBit, ShiftAmt);
|
|
|
|
|
|
|
|
// If the input sign bit is known to be zero, or if none of the top bits
|
|
|
|
// are demanded, turn this into an unsigned shift right.
|
2007-03-29 02:26:30 +00:00
|
|
|
if (RHSKnownZero[BitWidth-ShiftAmt-1] ||
|
2007-03-12 17:25:59 +00:00
|
|
|
(HighBits & ~DemandedMask) == HighBits) {
|
|
|
|
// Perform the logical shift right.
|
|
|
|
Value *NewVal = BinaryOperator::createLShr(
|
|
|
|
I->getOperand(0), SA, I->getName());
|
|
|
|
InsertNewInstBefore(cast<Instruction>(NewVal), *I);
|
|
|
|
return UpdateValueUsesWith(I, NewVal);
|
|
|
|
} else if ((RHSKnownOne & SignBit) != 0) { // New bits are known one.
|
|
|
|
RHSKnownOne |= HighBits;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2008-03-06 06:48:30 +00:00
|
|
|
case Instruction::SRem:
|
|
|
|
if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
|
|
|
|
APInt RA = Rem->getValue();
|
|
|
|
if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
|
|
|
|
APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) | RA : ~RA;
|
|
|
|
APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
|
|
|
|
if (SimplifyDemandedBits(I->getOperand(0), Mask2,
|
|
|
|
LHSKnownZero, LHSKnownOne, Depth+1))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (LHSKnownZero[BitWidth-1] || ((LHSKnownZero & LowBits) == LowBits))
|
|
|
|
LHSKnownZero |= ~LowBits;
|
|
|
|
else if (LHSKnownOne[BitWidth-1])
|
|
|
|
LHSKnownOne |= ~LowBits;
|
|
|
|
|
|
|
|
KnownZero |= LHSKnownZero & DemandedMask;
|
|
|
|
KnownOne |= LHSKnownOne & DemandedMask;
|
|
|
|
|
|
|
|
assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2008-04-28 17:02:21 +00:00
|
|
|
case Instruction::URem: {
|
2008-03-06 06:48:30 +00:00
|
|
|
if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
|
|
|
|
APInt RA = Rem->getValue();
|
2008-04-28 17:02:21 +00:00
|
|
|
if (RA.isStrictlyPositive() && RA.isPowerOf2()) {
|
2008-03-06 06:48:30 +00:00
|
|
|
APInt LowBits = (RA - 1) | RA;
|
|
|
|
APInt Mask2 = LowBits & DemandedMask;
|
|
|
|
KnownZero |= ~LowBits & DemandedMask;
|
|
|
|
if (SimplifyDemandedBits(I->getOperand(0), Mask2,
|
|
|
|
KnownZero, KnownOne, Depth+1))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
|
2008-04-28 17:02:21 +00:00
|
|
|
break;
|
2008-03-06 06:48:30 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-28 17:02:21 +00:00
|
|
|
|
|
|
|
APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0);
|
|
|
|
APInt AllOnes = APInt::getAllOnesValue(BitWidth);
|
|
|
|
ComputeMaskedBits(I->getOperand(0), AllOnes,
|
|
|
|
KnownZero2, KnownOne2, Depth+1);
|
|
|
|
uint32_t Leaders = KnownZero2.countLeadingOnes();
|
|
|
|
APInt HighZeros = APInt::getHighBitsSet(BitWidth, Leaders);
|
|
|
|
if (SimplifyDemandedBits(I->getOperand(1), ~HighZeros,
|
|
|
|
KnownZero2, KnownOne2, Depth+1))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
Leaders = std::max(Leaders,
|
|
|
|
KnownZero2.countLeadingOnes());
|
|
|
|
KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask;
|
2008-03-06 06:48:30 +00:00
|
|
|
break;
|
2007-03-12 17:25:59 +00:00
|
|
|
}
|
2008-04-28 17:02:21 +00:00
|
|
|
}
|
2007-03-12 17:25:59 +00:00
|
|
|
|
|
|
|
// If the client is only demanding bits that we know, return the known
|
|
|
|
// constant.
|
|
|
|
if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask)
|
|
|
|
return UpdateValueUsesWith(I, ConstantInt::get(RHSKnownOne));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
add a new SimplifyDemandedVectorElts method, which works similarly to
SimplifyDemandedBits. The idea is that some operations can be simplified if
not all of the computed elements are needed. Some targets (like x86) have a
large number of intrinsics that operate on a single element, but pass other
elts through unmodified. If those other elements are not needed, the
intrinsics can be simplified to scalar operations, and insertelement ops can
be removed.
This turns (f.e.):
ushort %Convert_sse(float %f) {
%tmp = insertelement <4 x float> undef, float %f, uint 0 ; <<4 x float>> [#uses=1]
%tmp10 = insertelement <4 x float> %tmp, float 0.000000e+00, uint 1 ; <<4 x float>> [#uses=1]
%tmp11 = insertelement <4 x float> %tmp10, float 0.000000e+00, uint 2 ; <<4 x float>> [#uses=1]
%tmp12 = insertelement <4 x float> %tmp11, float 0.000000e+00, uint 3 ; <<4 x float>> [#uses=1]
%tmp28 = tail call <4 x float> %llvm.x86.sse.sub.ss( <4 x float> %tmp12, <4 x float> < float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp37 = tail call <4 x float> %llvm.x86.sse.mul.ss( <4 x float> %tmp28, <4 x float> < float 5.000000e-01, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp37, <4 x float> < float 6.553500e+04, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> zeroinitializer ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
into:
ushort %Convert_sse(float %f) {
entry:
%tmp28 = sub float %f, 1.000000e+00 ; <float> [#uses=1]
%tmp37 = mul float %tmp28, 5.000000e-01 ; <float> [#uses=1]
%tmp375 = insertelement <4 x float> undef, float %tmp37, uint 0 ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp375, <4 x float> < float 6.553500e+04, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> < float 0.000000e+00, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
which improves codegen from:
_Convert_sse:
movss LCPI1_0, %xmm0
movss 4(%esp), %xmm1
subss %xmm0, %xmm1
movss LCPI1_1, %xmm0
mulss %xmm0, %xmm1
movss LCPI1_2, %xmm0
minss %xmm0, %xmm1
xorps %xmm0, %xmm0
maxss %xmm0, %xmm1
cvttss2si %xmm1, %eax
andl $65535, %eax
ret
to:
_Convert_sse:
movss 4(%esp), %xmm0
subss LCPI1_0, %xmm0
mulss LCPI1_1, %xmm0
movss LCPI1_2, %xmm1
minss %xmm1, %xmm0
xorps %xmm1, %xmm1
maxss %xmm1, %xmm0
cvttss2si %xmm0, %eax
andl $65535, %eax
ret
This is just a first step, it can be extended in many ways. Testcase here:
Transforms/InstCombine/vec_demanded_elts.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30752 91177308-0d34-0410-b5e6-96231b3b80d8
2006-10-05 06:55:50 +00:00
|
|
|
|
|
|
|
/// SimplifyDemandedVectorElts - The specified value producecs a vector with
|
|
|
|
/// 64 or fewer elements. DemandedElts contains the set of elements that are
|
|
|
|
/// actually used by the caller. This method analyzes which elements of the
|
|
|
|
/// operand are undef and returns that information in UndefElts.
|
|
|
|
///
|
|
|
|
/// If the information about demanded elements can be used to simplify the
|
|
|
|
/// operation, the operation is simplified, then the resultant value is
|
|
|
|
/// returned. This returns null if no change was made.
|
|
|
|
Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts,
|
|
|
|
uint64_t &UndefElts,
|
|
|
|
unsigned Depth) {
|
2007-02-15 02:26:10 +00:00
|
|
|
unsigned VWidth = cast<VectorType>(V->getType())->getNumElements();
|
add a new SimplifyDemandedVectorElts method, which works similarly to
SimplifyDemandedBits. The idea is that some operations can be simplified if
not all of the computed elements are needed. Some targets (like x86) have a
large number of intrinsics that operate on a single element, but pass other
elts through unmodified. If those other elements are not needed, the
intrinsics can be simplified to scalar operations, and insertelement ops can
be removed.
This turns (f.e.):
ushort %Convert_sse(float %f) {
%tmp = insertelement <4 x float> undef, float %f, uint 0 ; <<4 x float>> [#uses=1]
%tmp10 = insertelement <4 x float> %tmp, float 0.000000e+00, uint 1 ; <<4 x float>> [#uses=1]
%tmp11 = insertelement <4 x float> %tmp10, float 0.000000e+00, uint 2 ; <<4 x float>> [#uses=1]
%tmp12 = insertelement <4 x float> %tmp11, float 0.000000e+00, uint 3 ; <<4 x float>> [#uses=1]
%tmp28 = tail call <4 x float> %llvm.x86.sse.sub.ss( <4 x float> %tmp12, <4 x float> < float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp37 = tail call <4 x float> %llvm.x86.sse.mul.ss( <4 x float> %tmp28, <4 x float> < float 5.000000e-01, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp37, <4 x float> < float 6.553500e+04, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> zeroinitializer ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
into:
ushort %Convert_sse(float %f) {
entry:
%tmp28 = sub float %f, 1.000000e+00 ; <float> [#uses=1]
%tmp37 = mul float %tmp28, 5.000000e-01 ; <float> [#uses=1]
%tmp375 = insertelement <4 x float> undef, float %tmp37, uint 0 ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp375, <4 x float> < float 6.553500e+04, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> < float 0.000000e+00, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
which improves codegen from:
_Convert_sse:
movss LCPI1_0, %xmm0
movss 4(%esp), %xmm1
subss %xmm0, %xmm1
movss LCPI1_1, %xmm0
mulss %xmm0, %xmm1
movss LCPI1_2, %xmm0
minss %xmm0, %xmm1
xorps %xmm0, %xmm0
maxss %xmm0, %xmm1
cvttss2si %xmm1, %eax
andl $65535, %eax
ret
to:
_Convert_sse:
movss 4(%esp), %xmm0
subss LCPI1_0, %xmm0
mulss LCPI1_1, %xmm0
movss LCPI1_2, %xmm1
minss %xmm1, %xmm0
xorps %xmm1, %xmm1
maxss %xmm1, %xmm0
cvttss2si %xmm0, %eax
andl $65535, %eax
ret
This is just a first step, it can be extended in many ways. Testcase here:
Transforms/InstCombine/vec_demanded_elts.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30752 91177308-0d34-0410-b5e6-96231b3b80d8
2006-10-05 06:55:50 +00:00
|
|
|
assert(VWidth <= 64 && "Vector too wide to analyze!");
|
|
|
|
uint64_t EltMask = ~0ULL >> (64-VWidth);
|
|
|
|
assert(DemandedElts != EltMask && (DemandedElts & ~EltMask) == 0 &&
|
|
|
|
"Invalid DemandedElts!");
|
|
|
|
|
|
|
|
if (isa<UndefValue>(V)) {
|
|
|
|
// If the entire vector is undefined, just return this info.
|
|
|
|
UndefElts = EltMask;
|
|
|
|
return 0;
|
|
|
|
} else if (DemandedElts == 0) { // If nothing is demanded, provide undef.
|
|
|
|
UndefElts = EltMask;
|
|
|
|
return UndefValue::get(V->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
UndefElts = 0;
|
2007-02-15 02:26:10 +00:00
|
|
|
if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) {
|
|
|
|
const Type *EltTy = cast<VectorType>(V->getType())->getElementType();
|
add a new SimplifyDemandedVectorElts method, which works similarly to
SimplifyDemandedBits. The idea is that some operations can be simplified if
not all of the computed elements are needed. Some targets (like x86) have a
large number of intrinsics that operate on a single element, but pass other
elts through unmodified. If those other elements are not needed, the
intrinsics can be simplified to scalar operations, and insertelement ops can
be removed.
This turns (f.e.):
ushort %Convert_sse(float %f) {
%tmp = insertelement <4 x float> undef, float %f, uint 0 ; <<4 x float>> [#uses=1]
%tmp10 = insertelement <4 x float> %tmp, float 0.000000e+00, uint 1 ; <<4 x float>> [#uses=1]
%tmp11 = insertelement <4 x float> %tmp10, float 0.000000e+00, uint 2 ; <<4 x float>> [#uses=1]
%tmp12 = insertelement <4 x float> %tmp11, float 0.000000e+00, uint 3 ; <<4 x float>> [#uses=1]
%tmp28 = tail call <4 x float> %llvm.x86.sse.sub.ss( <4 x float> %tmp12, <4 x float> < float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp37 = tail call <4 x float> %llvm.x86.sse.mul.ss( <4 x float> %tmp28, <4 x float> < float 5.000000e-01, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp37, <4 x float> < float 6.553500e+04, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> zeroinitializer ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
into:
ushort %Convert_sse(float %f) {
entry:
%tmp28 = sub float %f, 1.000000e+00 ; <float> [#uses=1]
%tmp37 = mul float %tmp28, 5.000000e-01 ; <float> [#uses=1]
%tmp375 = insertelement <4 x float> undef, float %tmp37, uint 0 ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp375, <4 x float> < float 6.553500e+04, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> < float 0.000000e+00, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
which improves codegen from:
_Convert_sse:
movss LCPI1_0, %xmm0
movss 4(%esp), %xmm1
subss %xmm0, %xmm1
movss LCPI1_1, %xmm0
mulss %xmm0, %xmm1
movss LCPI1_2, %xmm0
minss %xmm0, %xmm1
xorps %xmm0, %xmm0
maxss %xmm0, %xmm1
cvttss2si %xmm1, %eax
andl $65535, %eax
ret
to:
_Convert_sse:
movss 4(%esp), %xmm0
subss LCPI1_0, %xmm0
mulss LCPI1_1, %xmm0
movss LCPI1_2, %xmm1
minss %xmm1, %xmm0
xorps %xmm1, %xmm1
maxss %xmm1, %xmm0
cvttss2si %xmm0, %eax
andl $65535, %eax
ret
This is just a first step, it can be extended in many ways. Testcase here:
Transforms/InstCombine/vec_demanded_elts.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30752 91177308-0d34-0410-b5e6-96231b3b80d8
2006-10-05 06:55:50 +00:00
|
|
|
Constant *Undef = UndefValue::get(EltTy);
|
|
|
|
|
|
|
|
std::vector<Constant*> Elts;
|
|
|
|
for (unsigned i = 0; i != VWidth; ++i)
|
|
|
|
if (!(DemandedElts & (1ULL << i))) { // If not demanded, set to undef.
|
|
|
|
Elts.push_back(Undef);
|
|
|
|
UndefElts |= (1ULL << i);
|
|
|
|
} else if (isa<UndefValue>(CP->getOperand(i))) { // Already undef.
|
|
|
|
Elts.push_back(Undef);
|
|
|
|
UndefElts |= (1ULL << i);
|
|
|
|
} else { // Otherwise, defined.
|
|
|
|
Elts.push_back(CP->getOperand(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we changed the constant, return it.
|
2007-02-15 02:26:10 +00:00
|
|
|
Constant *NewCP = ConstantVector::get(Elts);
|
add a new SimplifyDemandedVectorElts method, which works similarly to
SimplifyDemandedBits. The idea is that some operations can be simplified if
not all of the computed elements are needed. Some targets (like x86) have a
large number of intrinsics that operate on a single element, but pass other
elts through unmodified. If those other elements are not needed, the
intrinsics can be simplified to scalar operations, and insertelement ops can
be removed.
This turns (f.e.):
ushort %Convert_sse(float %f) {
%tmp = insertelement <4 x float> undef, float %f, uint 0 ; <<4 x float>> [#uses=1]
%tmp10 = insertelement <4 x float> %tmp, float 0.000000e+00, uint 1 ; <<4 x float>> [#uses=1]
%tmp11 = insertelement <4 x float> %tmp10, float 0.000000e+00, uint 2 ; <<4 x float>> [#uses=1]
%tmp12 = insertelement <4 x float> %tmp11, float 0.000000e+00, uint 3 ; <<4 x float>> [#uses=1]
%tmp28 = tail call <4 x float> %llvm.x86.sse.sub.ss( <4 x float> %tmp12, <4 x float> < float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp37 = tail call <4 x float> %llvm.x86.sse.mul.ss( <4 x float> %tmp28, <4 x float> < float 5.000000e-01, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp37, <4 x float> < float 6.553500e+04, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> zeroinitializer ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
into:
ushort %Convert_sse(float %f) {
entry:
%tmp28 = sub float %f, 1.000000e+00 ; <float> [#uses=1]
%tmp37 = mul float %tmp28, 5.000000e-01 ; <float> [#uses=1]
%tmp375 = insertelement <4 x float> undef, float %tmp37, uint 0 ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp375, <4 x float> < float 6.553500e+04, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> < float 0.000000e+00, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
which improves codegen from:
_Convert_sse:
movss LCPI1_0, %xmm0
movss 4(%esp), %xmm1
subss %xmm0, %xmm1
movss LCPI1_1, %xmm0
mulss %xmm0, %xmm1
movss LCPI1_2, %xmm0
minss %xmm0, %xmm1
xorps %xmm0, %xmm0
maxss %xmm0, %xmm1
cvttss2si %xmm1, %eax
andl $65535, %eax
ret
to:
_Convert_sse:
movss 4(%esp), %xmm0
subss LCPI1_0, %xmm0
mulss LCPI1_1, %xmm0
movss LCPI1_2, %xmm1
minss %xmm1, %xmm0
xorps %xmm1, %xmm1
maxss %xmm1, %xmm0
cvttss2si %xmm0, %eax
andl $65535, %eax
ret
This is just a first step, it can be extended in many ways. Testcase here:
Transforms/InstCombine/vec_demanded_elts.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30752 91177308-0d34-0410-b5e6-96231b3b80d8
2006-10-05 06:55:50 +00:00
|
|
|
return NewCP != CP ? NewCP : 0;
|
|
|
|
} else if (isa<ConstantAggregateZero>(V)) {
|
2007-02-15 02:26:10 +00:00
|
|
|
// Simplify the CAZ to a ConstantVector where the non-demanded elements are
|
add a new SimplifyDemandedVectorElts method, which works similarly to
SimplifyDemandedBits. The idea is that some operations can be simplified if
not all of the computed elements are needed. Some targets (like x86) have a
large number of intrinsics that operate on a single element, but pass other
elts through unmodified. If those other elements are not needed, the
intrinsics can be simplified to scalar operations, and insertelement ops can
be removed.
This turns (f.e.):
ushort %Convert_sse(float %f) {
%tmp = insertelement <4 x float> undef, float %f, uint 0 ; <<4 x float>> [#uses=1]
%tmp10 = insertelement <4 x float> %tmp, float 0.000000e+00, uint 1 ; <<4 x float>> [#uses=1]
%tmp11 = insertelement <4 x float> %tmp10, float 0.000000e+00, uint 2 ; <<4 x float>> [#uses=1]
%tmp12 = insertelement <4 x float> %tmp11, float 0.000000e+00, uint 3 ; <<4 x float>> [#uses=1]
%tmp28 = tail call <4 x float> %llvm.x86.sse.sub.ss( <4 x float> %tmp12, <4 x float> < float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp37 = tail call <4 x float> %llvm.x86.sse.mul.ss( <4 x float> %tmp28, <4 x float> < float 5.000000e-01, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp37, <4 x float> < float 6.553500e+04, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> zeroinitializer ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
into:
ushort %Convert_sse(float %f) {
entry:
%tmp28 = sub float %f, 1.000000e+00 ; <float> [#uses=1]
%tmp37 = mul float %tmp28, 5.000000e-01 ; <float> [#uses=1]
%tmp375 = insertelement <4 x float> undef, float %tmp37, uint 0 ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp375, <4 x float> < float 6.553500e+04, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> < float 0.000000e+00, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
which improves codegen from:
_Convert_sse:
movss LCPI1_0, %xmm0
movss 4(%esp), %xmm1
subss %xmm0, %xmm1
movss LCPI1_1, %xmm0
mulss %xmm0, %xmm1
movss LCPI1_2, %xmm0
minss %xmm0, %xmm1
xorps %xmm0, %xmm0
maxss %xmm0, %xmm1
cvttss2si %xmm1, %eax
andl $65535, %eax
ret
to:
_Convert_sse:
movss 4(%esp), %xmm0
subss LCPI1_0, %xmm0
mulss LCPI1_1, %xmm0
movss LCPI1_2, %xmm1
minss %xmm1, %xmm0
xorps %xmm1, %xmm1
maxss %xmm1, %xmm0
cvttss2si %xmm0, %eax
andl $65535, %eax
ret
This is just a first step, it can be extended in many ways. Testcase here:
Transforms/InstCombine/vec_demanded_elts.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30752 91177308-0d34-0410-b5e6-96231b3b80d8
2006-10-05 06:55:50 +00:00
|
|
|
// set to undef.
|
2007-02-15 02:26:10 +00:00
|
|
|
const Type *EltTy = cast<VectorType>(V->getType())->getElementType();
|
add a new SimplifyDemandedVectorElts method, which works similarly to
SimplifyDemandedBits. The idea is that some operations can be simplified if
not all of the computed elements are needed. Some targets (like x86) have a
large number of intrinsics that operate on a single element, but pass other
elts through unmodified. If those other elements are not needed, the
intrinsics can be simplified to scalar operations, and insertelement ops can
be removed.
This turns (f.e.):
ushort %Convert_sse(float %f) {
%tmp = insertelement <4 x float> undef, float %f, uint 0 ; <<4 x float>> [#uses=1]
%tmp10 = insertelement <4 x float> %tmp, float 0.000000e+00, uint 1 ; <<4 x float>> [#uses=1]
%tmp11 = insertelement <4 x float> %tmp10, float 0.000000e+00, uint 2 ; <<4 x float>> [#uses=1]
%tmp12 = insertelement <4 x float> %tmp11, float 0.000000e+00, uint 3 ; <<4 x float>> [#uses=1]
%tmp28 = tail call <4 x float> %llvm.x86.sse.sub.ss( <4 x float> %tmp12, <4 x float> < float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp37 = tail call <4 x float> %llvm.x86.sse.mul.ss( <4 x float> %tmp28, <4 x float> < float 5.000000e-01, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp37, <4 x float> < float 6.553500e+04, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> zeroinitializer ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
into:
ushort %Convert_sse(float %f) {
entry:
%tmp28 = sub float %f, 1.000000e+00 ; <float> [#uses=1]
%tmp37 = mul float %tmp28, 5.000000e-01 ; <float> [#uses=1]
%tmp375 = insertelement <4 x float> undef, float %tmp37, uint 0 ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp375, <4 x float> < float 6.553500e+04, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> < float 0.000000e+00, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
which improves codegen from:
_Convert_sse:
movss LCPI1_0, %xmm0
movss 4(%esp), %xmm1
subss %xmm0, %xmm1
movss LCPI1_1, %xmm0
mulss %xmm0, %xmm1
movss LCPI1_2, %xmm0
minss %xmm0, %xmm1
xorps %xmm0, %xmm0
maxss %xmm0, %xmm1
cvttss2si %xmm1, %eax
andl $65535, %eax
ret
to:
_Convert_sse:
movss 4(%esp), %xmm0
subss LCPI1_0, %xmm0
mulss LCPI1_1, %xmm0
movss LCPI1_2, %xmm1
minss %xmm1, %xmm0
xorps %xmm1, %xmm1
maxss %xmm1, %xmm0
cvttss2si %xmm0, %eax
andl $65535, %eax
ret
This is just a first step, it can be extended in many ways. Testcase here:
Transforms/InstCombine/vec_demanded_elts.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30752 91177308-0d34-0410-b5e6-96231b3b80d8
2006-10-05 06:55:50 +00:00
|
|
|
Constant *Zero = Constant::getNullValue(EltTy);
|
|
|
|
Constant *Undef = UndefValue::get(EltTy);
|
|
|
|
std::vector<Constant*> Elts;
|
|
|
|
for (unsigned i = 0; i != VWidth; ++i)
|
|
|
|
Elts.push_back((DemandedElts & (1ULL << i)) ? Zero : Undef);
|
|
|
|
UndefElts = DemandedElts ^ EltMask;
|
2007-02-15 02:26:10 +00:00
|
|
|
return ConstantVector::get(Elts);
|
add a new SimplifyDemandedVectorElts method, which works similarly to
SimplifyDemandedBits. The idea is that some operations can be simplified if
not all of the computed elements are needed. Some targets (like x86) have a
large number of intrinsics that operate on a single element, but pass other
elts through unmodified. If those other elements are not needed, the
intrinsics can be simplified to scalar operations, and insertelement ops can
be removed.
This turns (f.e.):
ushort %Convert_sse(float %f) {
%tmp = insertelement <4 x float> undef, float %f, uint 0 ; <<4 x float>> [#uses=1]
%tmp10 = insertelement <4 x float> %tmp, float 0.000000e+00, uint 1 ; <<4 x float>> [#uses=1]
%tmp11 = insertelement <4 x float> %tmp10, float 0.000000e+00, uint 2 ; <<4 x float>> [#uses=1]
%tmp12 = insertelement <4 x float> %tmp11, float 0.000000e+00, uint 3 ; <<4 x float>> [#uses=1]
%tmp28 = tail call <4 x float> %llvm.x86.sse.sub.ss( <4 x float> %tmp12, <4 x float> < float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp37 = tail call <4 x float> %llvm.x86.sse.mul.ss( <4 x float> %tmp28, <4 x float> < float 5.000000e-01, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp37, <4 x float> < float 6.553500e+04, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> zeroinitializer ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
into:
ushort %Convert_sse(float %f) {
entry:
%tmp28 = sub float %f, 1.000000e+00 ; <float> [#uses=1]
%tmp37 = mul float %tmp28, 5.000000e-01 ; <float> [#uses=1]
%tmp375 = insertelement <4 x float> undef, float %tmp37, uint 0 ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp375, <4 x float> < float 6.553500e+04, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> < float 0.000000e+00, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
which improves codegen from:
_Convert_sse:
movss LCPI1_0, %xmm0
movss 4(%esp), %xmm1
subss %xmm0, %xmm1
movss LCPI1_1, %xmm0
mulss %xmm0, %xmm1
movss LCPI1_2, %xmm0
minss %xmm0, %xmm1
xorps %xmm0, %xmm0
maxss %xmm0, %xmm1
cvttss2si %xmm1, %eax
andl $65535, %eax
ret
to:
_Convert_sse:
movss 4(%esp), %xmm0
subss LCPI1_0, %xmm0
mulss LCPI1_1, %xmm0
movss LCPI1_2, %xmm1
minss %xmm1, %xmm0
xorps %xmm1, %xmm1
maxss %xmm1, %xmm0
cvttss2si %xmm0, %eax
andl $65535, %eax
ret
This is just a first step, it can be extended in many ways. Testcase here:
Transforms/InstCombine/vec_demanded_elts.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30752 91177308-0d34-0410-b5e6-96231b3b80d8
2006-10-05 06:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!V->hasOneUse()) { // Other users may use these bits.
|
|
|
|
if (Depth != 0) { // Not at the root.
|
|
|
|
// TODO: Just compute the UndefElts information recursively.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} else if (Depth == 10) { // Limit search depth.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *I = dyn_cast<Instruction>(V);
|
|
|
|
if (!I) return false; // Only analyze instructions.
|
|
|
|
|
|
|
|
bool MadeChange = false;
|
|
|
|
uint64_t UndefElts2;
|
|
|
|
Value *TmpV;
|
|
|
|
switch (I->getOpcode()) {
|
|
|
|
default: break;
|
|
|
|
|
|
|
|
case Instruction::InsertElement: {
|
|
|
|
// If this is a variable index, we don't know which element it overwrites.
|
|
|
|
// demand exactly the same input as we produce.
|
2006-10-20 07:07:24 +00:00
|
|
|
ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
|
add a new SimplifyDemandedVectorElts method, which works similarly to
SimplifyDemandedBits. The idea is that some operations can be simplified if
not all of the computed elements are needed. Some targets (like x86) have a
large number of intrinsics that operate on a single element, but pass other
elts through unmodified. If those other elements are not needed, the
intrinsics can be simplified to scalar operations, and insertelement ops can
be removed.
This turns (f.e.):
ushort %Convert_sse(float %f) {
%tmp = insertelement <4 x float> undef, float %f, uint 0 ; <<4 x float>> [#uses=1]
%tmp10 = insertelement <4 x float> %tmp, float 0.000000e+00, uint 1 ; <<4 x float>> [#uses=1]
%tmp11 = insertelement <4 x float> %tmp10, float 0.000000e+00, uint 2 ; <<4 x float>> [#uses=1]
%tmp12 = insertelement <4 x float> %tmp11, float 0.000000e+00, uint 3 ; <<4 x float>> [#uses=1]
%tmp28 = tail call <4 x float> %llvm.x86.sse.sub.ss( <4 x float> %tmp12, <4 x float> < float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp37 = tail call <4 x float> %llvm.x86.sse.mul.ss( <4 x float> %tmp28, <4 x float> < float 5.000000e-01, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp37, <4 x float> < float 6.553500e+04, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> zeroinitializer ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
into:
ushort %Convert_sse(float %f) {
entry:
%tmp28 = sub float %f, 1.000000e+00 ; <float> [#uses=1]
%tmp37 = mul float %tmp28, 5.000000e-01 ; <float> [#uses=1]
%tmp375 = insertelement <4 x float> undef, float %tmp37, uint 0 ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp375, <4 x float> < float 6.553500e+04, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> < float 0.000000e+00, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
which improves codegen from:
_Convert_sse:
movss LCPI1_0, %xmm0
movss 4(%esp), %xmm1
subss %xmm0, %xmm1
movss LCPI1_1, %xmm0
mulss %xmm0, %xmm1
movss LCPI1_2, %xmm0
minss %xmm0, %xmm1
xorps %xmm0, %xmm0
maxss %xmm0, %xmm1
cvttss2si %xmm1, %eax
andl $65535, %eax
ret
to:
_Convert_sse:
movss 4(%esp), %xmm0
subss LCPI1_0, %xmm0
mulss LCPI1_1, %xmm0
movss LCPI1_2, %xmm1
minss %xmm1, %xmm0
xorps %xmm1, %xmm1
maxss %xmm1, %xmm0
cvttss2si %xmm0, %eax
andl $65535, %eax
ret
This is just a first step, it can be extended in many ways. Testcase here:
Transforms/InstCombine/vec_demanded_elts.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30752 91177308-0d34-0410-b5e6-96231b3b80d8
2006-10-05 06:55:50 +00:00
|
|
|
if (Idx == 0) {
|
|
|
|
// Note that we can't propagate undef elt info, because we don't know
|
|
|
|
// which elt is getting updated.
|
|
|
|
TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
|
|
|
|
UndefElts2, Depth+1);
|
|
|
|
if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is inserting an element that isn't demanded, remove this
|
|
|
|
// insertelement.
|
2006-10-20 07:07:24 +00:00
|
|
|
unsigned IdxNo = Idx->getZExtValue();
|
add a new SimplifyDemandedVectorElts method, which works similarly to
SimplifyDemandedBits. The idea is that some operations can be simplified if
not all of the computed elements are needed. Some targets (like x86) have a
large number of intrinsics that operate on a single element, but pass other
elts through unmodified. If those other elements are not needed, the
intrinsics can be simplified to scalar operations, and insertelement ops can
be removed.
This turns (f.e.):
ushort %Convert_sse(float %f) {
%tmp = insertelement <4 x float> undef, float %f, uint 0 ; <<4 x float>> [#uses=1]
%tmp10 = insertelement <4 x float> %tmp, float 0.000000e+00, uint 1 ; <<4 x float>> [#uses=1]
%tmp11 = insertelement <4 x float> %tmp10, float 0.000000e+00, uint 2 ; <<4 x float>> [#uses=1]
%tmp12 = insertelement <4 x float> %tmp11, float 0.000000e+00, uint 3 ; <<4 x float>> [#uses=1]
%tmp28 = tail call <4 x float> %llvm.x86.sse.sub.ss( <4 x float> %tmp12, <4 x float> < float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp37 = tail call <4 x float> %llvm.x86.sse.mul.ss( <4 x float> %tmp28, <4 x float> < float 5.000000e-01, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp37, <4 x float> < float 6.553500e+04, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> zeroinitializer ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
into:
ushort %Convert_sse(float %f) {
entry:
%tmp28 = sub float %f, 1.000000e+00 ; <float> [#uses=1]
%tmp37 = mul float %tmp28, 5.000000e-01 ; <float> [#uses=1]
%tmp375 = insertelement <4 x float> undef, float %tmp37, uint 0 ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp375, <4 x float> < float 6.553500e+04, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> < float 0.000000e+00, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
which improves codegen from:
_Convert_sse:
movss LCPI1_0, %xmm0
movss 4(%esp), %xmm1
subss %xmm0, %xmm1
movss LCPI1_1, %xmm0
mulss %xmm0, %xmm1
movss LCPI1_2, %xmm0
minss %xmm0, %xmm1
xorps %xmm0, %xmm0
maxss %xmm0, %xmm1
cvttss2si %xmm1, %eax
andl $65535, %eax
ret
to:
_Convert_sse:
movss 4(%esp), %xmm0
subss LCPI1_0, %xmm0
mulss LCPI1_1, %xmm0
movss LCPI1_2, %xmm1
minss %xmm1, %xmm0
xorps %xmm1, %xmm1
maxss %xmm1, %xmm0
cvttss2si %xmm0, %eax
andl $65535, %eax
ret
This is just a first step, it can be extended in many ways. Testcase here:
Transforms/InstCombine/vec_demanded_elts.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30752 91177308-0d34-0410-b5e6-96231b3b80d8
2006-10-05 06:55:50 +00:00
|
|
|
if (IdxNo >= VWidth || (DemandedElts & (1ULL << IdxNo)) == 0)
|
|
|
|
return AddSoonDeadInstToWorklist(*I, 0);
|
|
|
|
|
|
|
|
// Otherwise, the element inserted overwrites whatever was there, so the
|
|
|
|
// input demanded set is simpler than the output set.
|
|
|
|
TmpV = SimplifyDemandedVectorElts(I->getOperand(0),
|
|
|
|
DemandedElts & ~(1ULL << IdxNo),
|
|
|
|
UndefElts, Depth+1);
|
|
|
|
if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
|
|
|
|
|
|
|
|
// The inserted element is defined.
|
|
|
|
UndefElts |= 1ULL << IdxNo;
|
|
|
|
break;
|
|
|
|
}
|
Implement InstCombine/vec_demanded_elts.ll:test2. This allows us to turn
unsigned test(float f) {
return _mm_cvtsi128_si32( (__m128i) _mm_set_ss( f*f ));
}
into:
_test:
movss 4(%esp), %xmm0
mulss %xmm0, %xmm0
movd %xmm0, %eax
ret
instead of:
_test:
movss 4(%esp), %xmm0
mulss %xmm0, %xmm0
xorps %xmm1, %xmm1
movss %xmm0, %xmm1
movd %xmm1, %eax
ret
GCC gets:
_test:
subl $28, %esp
movss 32(%esp), %xmm0
mulss %xmm0, %xmm0
xorps %xmm1, %xmm1
movss %xmm0, %xmm1
movaps %xmm1, %xmm0
movd %xmm0, 12(%esp)
movl 12(%esp), %eax
addl $28, %esp
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36020 91177308-0d34-0410-b5e6-96231b3b80d8
2007-04-14 22:29:23 +00:00
|
|
|
case Instruction::BitCast: {
|
2007-07-16 14:29:03 +00:00
|
|
|
// Vector->vector casts only.
|
Implement InstCombine/vec_demanded_elts.ll:test2. This allows us to turn
unsigned test(float f) {
return _mm_cvtsi128_si32( (__m128i) _mm_set_ss( f*f ));
}
into:
_test:
movss 4(%esp), %xmm0
mulss %xmm0, %xmm0
movd %xmm0, %eax
ret
instead of:
_test:
movss 4(%esp), %xmm0
mulss %xmm0, %xmm0
xorps %xmm1, %xmm1
movss %xmm0, %xmm1
movd %xmm1, %eax
ret
GCC gets:
_test:
subl $28, %esp
movss 32(%esp), %xmm0
mulss %xmm0, %xmm0
xorps %xmm1, %xmm1
movss %xmm0, %xmm1
movaps %xmm1, %xmm0
movd %xmm0, 12(%esp)
movl 12(%esp), %eax
addl $28, %esp
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36020 91177308-0d34-0410-b5e6-96231b3b80d8
2007-04-14 22:29:23 +00:00
|
|
|
const VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
|
|
|
|
if (!VTy) break;
|
|
|
|
unsigned InVWidth = VTy->getNumElements();
|
|
|
|
uint64_t InputDemandedElts = 0;
|
|
|
|
unsigned Ratio;
|
|
|
|
|
|
|
|
if (VWidth == InVWidth) {
|
2007-07-16 14:29:03 +00:00
|
|
|
// If we are converting from <4 x i32> -> <4 x f32>, we demand the same
|
Implement InstCombine/vec_demanded_elts.ll:test2. This allows us to turn
unsigned test(float f) {
return _mm_cvtsi128_si32( (__m128i) _mm_set_ss( f*f ));
}
into:
_test:
movss 4(%esp), %xmm0
mulss %xmm0, %xmm0
movd %xmm0, %eax
ret
instead of:
_test:
movss 4(%esp), %xmm0
mulss %xmm0, %xmm0
xorps %xmm1, %xmm1
movss %xmm0, %xmm1
movd %xmm1, %eax
ret
GCC gets:
_test:
subl $28, %esp
movss 32(%esp), %xmm0
mulss %xmm0, %xmm0
xorps %xmm1, %xmm1
movss %xmm0, %xmm1
movaps %xmm1, %xmm0
movd %xmm0, 12(%esp)
movl 12(%esp), %eax
addl $28, %esp
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36020 91177308-0d34-0410-b5e6-96231b3b80d8
2007-04-14 22:29:23 +00:00
|
|
|
// elements as are demanded of us.
|
|
|
|
Ratio = 1;
|
|
|
|
InputDemandedElts = DemandedElts;
|
|
|
|
} else if (VWidth > InVWidth) {
|
|
|
|
// Untested so far.
|
|
|
|
break;
|
|
|
|
|
|
|
|
// If there are more elements in the result than there are in the source,
|
|
|
|
// then an input element is live if any of the corresponding output
|
|
|
|
// elements are live.
|
|
|
|
Ratio = VWidth/InVWidth;
|
|
|
|
for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
|
|
|
|
if (DemandedElts & (1ULL << OutIdx))
|
|
|
|
InputDemandedElts |= 1ULL << (OutIdx/Ratio);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Untested so far.
|
|
|
|
break;
|
|
|
|
|
|
|
|
// If there are more elements in the source than there are in the result,
|
|
|
|
// then an input element is live if the corresponding output element is
|
|
|
|
// live.
|
|
|
|
Ratio = InVWidth/VWidth;
|
|
|
|
for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
|
|
|
|
if (DemandedElts & (1ULL << InIdx/Ratio))
|
|
|
|
InputDemandedElts |= 1ULL << InIdx;
|
|
|
|
}
|
add a new SimplifyDemandedVectorElts method, which works similarly to
SimplifyDemandedBits. The idea is that some operations can be simplified if
not all of the computed elements are needed. Some targets (like x86) have a
large number of intrinsics that operate on a single element, but pass other
elts through unmodified. If those other elements are not needed, the
intrinsics can be simplified to scalar operations, and insertelement ops can
be removed.
This turns (f.e.):
ushort %Convert_sse(float %f) {
%tmp = insertelement <4 x float> undef, float %f, uint 0 ; <<4 x float>> [#uses=1]
%tmp10 = insertelement <4 x float> %tmp, float 0.000000e+00, uint 1 ; <<4 x float>> [#uses=1]
%tmp11 = insertelement <4 x float> %tmp10, float 0.000000e+00, uint 2 ; <<4 x float>> [#uses=1]
%tmp12 = insertelement <4 x float> %tmp11, float 0.000000e+00, uint 3 ; <<4 x float>> [#uses=1]
%tmp28 = tail call <4 x float> %llvm.x86.sse.sub.ss( <4 x float> %tmp12, <4 x float> < float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp37 = tail call <4 x float> %llvm.x86.sse.mul.ss( <4 x float> %tmp28, <4 x float> < float 5.000000e-01, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp37, <4 x float> < float 6.553500e+04, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> zeroinitializer ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
into:
ushort %Convert_sse(float %f) {
entry:
%tmp28 = sub float %f, 1.000000e+00 ; <float> [#uses=1]
%tmp37 = mul float %tmp28, 5.000000e-01 ; <float> [#uses=1]
%tmp375 = insertelement <4 x float> undef, float %tmp37, uint 0 ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp375, <4 x float> < float 6.553500e+04, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> < float 0.000000e+00, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
which improves codegen from:
_Convert_sse:
movss LCPI1_0, %xmm0
movss 4(%esp), %xmm1
subss %xmm0, %xmm1
movss LCPI1_1, %xmm0
mulss %xmm0, %xmm1
movss LCPI1_2, %xmm0
minss %xmm0, %xmm1
xorps %xmm0, %xmm0
maxss %xmm0, %xmm1
cvttss2si %xmm1, %eax
andl $65535, %eax
ret
to:
_Convert_sse:
movss 4(%esp), %xmm0
subss LCPI1_0, %xmm0
mulss LCPI1_1, %xmm0
movss LCPI1_2, %xmm1
minss %xmm1, %xmm0
xorps %xmm1, %xmm1
maxss %xmm1, %xmm0
cvttss2si %xmm0, %eax
andl $65535, %eax
ret
This is just a first step, it can be extended in many ways. Testcase here:
Transforms/InstCombine/vec_demanded_elts.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30752 91177308-0d34-0410-b5e6-96231b3b80d8
2006-10-05 06:55:50 +00:00
|
|
|
|
Implement InstCombine/vec_demanded_elts.ll:test2. This allows us to turn
unsigned test(float f) {
return _mm_cvtsi128_si32( (__m128i) _mm_set_ss( f*f ));
}
into:
_test:
movss 4(%esp), %xmm0
mulss %xmm0, %xmm0
movd %xmm0, %eax
ret
instead of:
_test:
movss 4(%esp), %xmm0
mulss %xmm0, %xmm0
xorps %xmm1, %xmm1
movss %xmm0, %xmm1
movd %xmm1, %eax
ret
GCC gets:
_test:
subl $28, %esp
movss 32(%esp), %xmm0
mulss %xmm0, %xmm0
xorps %xmm1, %xmm1
movss %xmm0, %xmm1
movaps %xmm1, %xmm0
movd %xmm0, 12(%esp)
movl 12(%esp), %eax
addl $28, %esp
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36020 91177308-0d34-0410-b5e6-96231b3b80d8
2007-04-14 22:29:23 +00:00
|
|
|
// div/rem demand all inputs, because they don't want divide by zero.
|
|
|
|
TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts,
|
|
|
|
UndefElts2, Depth+1);
|
|
|
|
if (TmpV) {
|
|
|
|
I->setOperand(0, TmpV);
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
UndefElts = UndefElts2;
|
|
|
|
if (VWidth > InVWidth) {
|
|
|
|
assert(0 && "Unimp");
|
|
|
|
// If there are more elements in the result than there are in the source,
|
|
|
|
// then an output element is undef if the corresponding input element is
|
|
|
|
// undef.
|
|
|
|
for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
|
|
|
|
if (UndefElts2 & (1ULL << (OutIdx/Ratio)))
|
|
|
|
UndefElts |= 1ULL << OutIdx;
|
|
|
|
} else if (VWidth < InVWidth) {
|
|
|
|
assert(0 && "Unimp");
|
|
|
|
// If there are more elements in the source than there are in the result,
|
|
|
|
// then a result element is undef if all of the corresponding input
|
|
|
|
// elements are undef.
|
|
|
|
UndefElts = ~0ULL >> (64-VWidth); // Start out all undef.
|
|
|
|
for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
|
|
|
|
if ((UndefElts2 & (1ULL << InIdx)) == 0) // Not undef?
|
|
|
|
UndefElts &= ~(1ULL << (InIdx/Ratio)); // Clear undef bit.
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
add a new SimplifyDemandedVectorElts method, which works similarly to
SimplifyDemandedBits. The idea is that some operations can be simplified if
not all of the computed elements are needed. Some targets (like x86) have a
large number of intrinsics that operate on a single element, but pass other
elts through unmodified. If those other elements are not needed, the
intrinsics can be simplified to scalar operations, and insertelement ops can
be removed.
This turns (f.e.):
ushort %Convert_sse(float %f) {
%tmp = insertelement <4 x float> undef, float %f, uint 0 ; <<4 x float>> [#uses=1]
%tmp10 = insertelement <4 x float> %tmp, float 0.000000e+00, uint 1 ; <<4 x float>> [#uses=1]
%tmp11 = insertelement <4 x float> %tmp10, float 0.000000e+00, uint 2 ; <<4 x float>> [#uses=1]
%tmp12 = insertelement <4 x float> %tmp11, float 0.000000e+00, uint 3 ; <<4 x float>> [#uses=1]
%tmp28 = tail call <4 x float> %llvm.x86.sse.sub.ss( <4 x float> %tmp12, <4 x float> < float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp37 = tail call <4 x float> %llvm.x86.sse.mul.ss( <4 x float> %tmp28, <4 x float> < float 5.000000e-01, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp37, <4 x float> < float 6.553500e+04, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> zeroinitializer ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
into:
ushort %Convert_sse(float %f) {
entry:
%tmp28 = sub float %f, 1.000000e+00 ; <float> [#uses=1]
%tmp37 = mul float %tmp28, 5.000000e-01 ; <float> [#uses=1]
%tmp375 = insertelement <4 x float> undef, float %tmp37, uint 0 ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp375, <4 x float> < float 6.553500e+04, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> < float 0.000000e+00, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
which improves codegen from:
_Convert_sse:
movss LCPI1_0, %xmm0
movss 4(%esp), %xmm1
subss %xmm0, %xmm1
movss LCPI1_1, %xmm0
mulss %xmm0, %xmm1
movss LCPI1_2, %xmm0
minss %xmm0, %xmm1
xorps %xmm0, %xmm0
maxss %xmm0, %xmm1
cvttss2si %xmm1, %eax
andl $65535, %eax
ret
to:
_Convert_sse:
movss 4(%esp), %xmm0
subss LCPI1_0, %xmm0
mulss LCPI1_1, %xmm0
movss LCPI1_2, %xmm1
minss %xmm1, %xmm0
xorps %xmm1, %xmm1
maxss %xmm1, %xmm0
cvttss2si %xmm0, %eax
andl $65535, %eax
ret
This is just a first step, it can be extended in many ways. Testcase here:
Transforms/InstCombine/vec_demanded_elts.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30752 91177308-0d34-0410-b5e6-96231b3b80d8
2006-10-05 06:55:50 +00:00
|
|
|
case Instruction::And:
|
|
|
|
case Instruction::Or:
|
|
|
|
case Instruction::Xor:
|
|
|
|
case Instruction::Add:
|
|
|
|
case Instruction::Sub:
|
|
|
|
case Instruction::Mul:
|
|
|
|
// div/rem demand all inputs, because they don't want divide by zero.
|
|
|
|
TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
|
|
|
|
UndefElts, Depth+1);
|
|
|
|
if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
|
|
|
|
TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts,
|
|
|
|
UndefElts2, Depth+1);
|
|
|
|
if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
|
|
|
|
|
|
|
|
// Output elements are undefined if both are undefined. Consider things
|
|
|
|
// like undef&0. The result is known zero, not undef.
|
|
|
|
UndefElts &= UndefElts2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Instruction::Call: {
|
|
|
|
IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
|
|
|
|
if (!II) break;
|
|
|
|
switch (II->getIntrinsicID()) {
|
|
|
|
default: break;
|
|
|
|
|
|
|
|
// Binary vector operations that work column-wise. A dest element is a
|
|
|
|
// function of the corresponding input elements from the two inputs.
|
|
|
|
case Intrinsic::x86_sse_sub_ss:
|
|
|
|
case Intrinsic::x86_sse_mul_ss:
|
|
|
|
case Intrinsic::x86_sse_min_ss:
|
|
|
|
case Intrinsic::x86_sse_max_ss:
|
|
|
|
case Intrinsic::x86_sse2_sub_sd:
|
|
|
|
case Intrinsic::x86_sse2_mul_sd:
|
|
|
|
case Intrinsic::x86_sse2_min_sd:
|
|
|
|
case Intrinsic::x86_sse2_max_sd:
|
|
|
|
TmpV = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts,
|
|
|
|
UndefElts, Depth+1);
|
|
|
|
if (TmpV) { II->setOperand(1, TmpV); MadeChange = true; }
|
|
|
|
TmpV = SimplifyDemandedVectorElts(II->getOperand(2), DemandedElts,
|
|
|
|
UndefElts2, Depth+1);
|
|
|
|
if (TmpV) { II->setOperand(2, TmpV); MadeChange = true; }
|
|
|
|
|
|
|
|
// If only the low elt is demanded and this is a scalarizable intrinsic,
|
|
|
|
// scalarize it now.
|
|
|
|
if (DemandedElts == 1) {
|
|
|
|
switch (II->getIntrinsicID()) {
|
|
|
|
default: break;
|
|
|
|
case Intrinsic::x86_sse_sub_ss:
|
|
|
|
case Intrinsic::x86_sse_mul_ss:
|
|
|
|
case Intrinsic::x86_sse2_sub_sd:
|
|
|
|
case Intrinsic::x86_sse2_mul_sd:
|
|
|
|
// TODO: Lower MIN/MAX/ABS/etc
|
|
|
|
Value *LHS = II->getOperand(1);
|
|
|
|
Value *RHS = II->getOperand(2);
|
|
|
|
// Extract the element as scalars.
|
|
|
|
LHS = InsertNewInstBefore(new ExtractElementInst(LHS, 0U,"tmp"), *II);
|
|
|
|
RHS = InsertNewInstBefore(new ExtractElementInst(RHS, 0U,"tmp"), *II);
|
|
|
|
|
|
|
|
switch (II->getIntrinsicID()) {
|
|
|
|
default: assert(0 && "Case stmts out of sync!");
|
|
|
|
case Intrinsic::x86_sse_sub_ss:
|
|
|
|
case Intrinsic::x86_sse2_sub_sd:
|
|
|
|
TmpV = InsertNewInstBefore(BinaryOperator::createSub(LHS, RHS,
|
|
|
|
II->getName()), *II);
|
|
|
|
break;
|
|
|
|
case Intrinsic::x86_sse_mul_ss:
|
|
|
|
case Intrinsic::x86_sse2_mul_sd:
|
|
|
|
TmpV = InsertNewInstBefore(BinaryOperator::createMul(LHS, RHS,
|
|
|
|
II->getName()), *II);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *New =
|
2008-04-06 20:25:17 +00:00
|
|
|
InsertElementInst::Create(UndefValue::get(II->getType()), TmpV, 0U,
|
|
|
|
II->getName());
|
add a new SimplifyDemandedVectorElts method, which works similarly to
SimplifyDemandedBits. The idea is that some operations can be simplified if
not all of the computed elements are needed. Some targets (like x86) have a
large number of intrinsics that operate on a single element, but pass other
elts through unmodified. If those other elements are not needed, the
intrinsics can be simplified to scalar operations, and insertelement ops can
be removed.
This turns (f.e.):
ushort %Convert_sse(float %f) {
%tmp = insertelement <4 x float> undef, float %f, uint 0 ; <<4 x float>> [#uses=1]
%tmp10 = insertelement <4 x float> %tmp, float 0.000000e+00, uint 1 ; <<4 x float>> [#uses=1]
%tmp11 = insertelement <4 x float> %tmp10, float 0.000000e+00, uint 2 ; <<4 x float>> [#uses=1]
%tmp12 = insertelement <4 x float> %tmp11, float 0.000000e+00, uint 3 ; <<4 x float>> [#uses=1]
%tmp28 = tail call <4 x float> %llvm.x86.sse.sub.ss( <4 x float> %tmp12, <4 x float> < float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp37 = tail call <4 x float> %llvm.x86.sse.mul.ss( <4 x float> %tmp28, <4 x float> < float 5.000000e-01, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp37, <4 x float> < float 6.553500e+04, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> zeroinitializer ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
into:
ushort %Convert_sse(float %f) {
entry:
%tmp28 = sub float %f, 1.000000e+00 ; <float> [#uses=1]
%tmp37 = mul float %tmp28, 5.000000e-01 ; <float> [#uses=1]
%tmp375 = insertelement <4 x float> undef, float %tmp37, uint 0 ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp375, <4 x float> < float 6.553500e+04, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> < float 0.000000e+00, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
which improves codegen from:
_Convert_sse:
movss LCPI1_0, %xmm0
movss 4(%esp), %xmm1
subss %xmm0, %xmm1
movss LCPI1_1, %xmm0
mulss %xmm0, %xmm1
movss LCPI1_2, %xmm0
minss %xmm0, %xmm1
xorps %xmm0, %xmm0
maxss %xmm0, %xmm1
cvttss2si %xmm1, %eax
andl $65535, %eax
ret
to:
_Convert_sse:
movss 4(%esp), %xmm0
subss LCPI1_0, %xmm0
mulss LCPI1_1, %xmm0
movss LCPI1_2, %xmm1
minss %xmm1, %xmm0
xorps %xmm1, %xmm1
maxss %xmm1, %xmm0
cvttss2si %xmm0, %eax
andl $65535, %eax
ret
This is just a first step, it can be extended in many ways. Testcase here:
Transforms/InstCombine/vec_demanded_elts.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30752 91177308-0d34-0410-b5e6-96231b3b80d8
2006-10-05 06:55:50 +00:00
|
|
|
InsertNewInstBefore(New, *II);
|
|
|
|
AddSoonDeadInstToWorklist(*II, 0);
|
|
|
|
return New;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output elements are undefined if both are undefined. Consider things
|
|
|
|
// like undef&0. The result is known zero, not undef.
|
|
|
|
UndefElts &= UndefElts2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return MadeChange ? I : 0;
|
|
|
|
}
|
|
|
|
|
2007-09-06 02:40:25 +00:00
|
|
|
/// @returns true if the specified compare predicate is
|
2006-12-23 06:05:41 +00:00
|
|
|
/// true when both operands are equal...
|
2007-09-06 02:40:25 +00:00
|
|
|
/// @brief Determine if the icmp Predicate is true when both operands are equal
|
|
|
|
static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
|
2006-12-23 06:05:41 +00:00
|
|
|
return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
|
|
|
|
pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
|
|
|
|
pred == ICmpInst::ICMP_SLE;
|
|
|
|
}
|
|
|
|
|
2007-09-06 02:40:25 +00:00
|
|
|
/// @returns true if the specified compare instruction is
|
|
|
|
/// true when both operands are equal...
|
|
|
|
/// @brief Determine if the ICmpInst returns true when both operands are equal
|
|
|
|
static bool isTrueWhenEqual(ICmpInst &ICI) {
|
|
|
|
return isTrueWhenEqual(ICI.getPredicate());
|
|
|
|
}
|
|
|
|
|
2003-08-13 19:01:45 +00:00
|
|
|
/// AssociativeOpt - Perform an optimization on an associative operator. This
|
|
|
|
/// function is designed to check a chain of associative operators for a
|
|
|
|
/// potential to apply a certain optimization. Since the optimization may be
|
|
|
|
/// applicable if the expression was reassociated, this checks the chain, then
|
|
|
|
/// reassociates the expression as necessary to expose the optimization
|
|
|
|
/// opportunity. This makes use of a special Functor, which must define
|
|
|
|
/// 'shouldApply' and 'apply' methods.
|
|
|
|
///
|
|
|
|
template<typename Functor>
|
|
|
|
Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) {
|
|
|
|
unsigned Opcode = Root.getOpcode();
|
|
|
|
Value *LHS = Root.getOperand(0);
|
|
|
|
|
|
|
|
// Quick check, see if the immediate LHS matches...
|
|
|
|
if (F.shouldApply(LHS))
|
|
|
|
return F.apply(Root);
|
|
|
|
|
|
|
|
// Otherwise, if the LHS is not of the same opcode as the root, return.
|
|
|
|
Instruction *LHSI = dyn_cast<Instruction>(LHS);
|
2003-10-15 16:48:29 +00:00
|
|
|
while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) {
|
2003-08-13 19:01:45 +00:00
|
|
|
// Should we apply this transform to the RHS?
|
|
|
|
bool ShouldApply = F.shouldApply(LHSI->getOperand(1));
|
|
|
|
|
|
|
|
// If not to the RHS, check to see if we should apply to the LHS...
|
|
|
|
if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) {
|
|
|
|
cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS
|
|
|
|
ShouldApply = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the functor wants to apply the optimization to the RHS of LHSI,
|
|
|
|
// reassociate the expression from ((? op A) op B) to (? op (A op B))
|
|
|
|
if (ShouldApply) {
|
|
|
|
BasicBlock *BB = Root.getParent();
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2003-08-13 19:01:45 +00:00
|
|
|
// Now all of the instructions are in the current basic block, go ahead
|
|
|
|
// and perform the reassociation.
|
|
|
|
Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0));
|
|
|
|
|
|
|
|
// First move the selected RHS to the LHS of the root...
|
|
|
|
Root.setOperand(0, LHSI->getOperand(1));
|
|
|
|
|
|
|
|
// Make what used to be the LHS of the root be the user of the root...
|
|
|
|
Value *ExtraOperand = TmpLHSI->getOperand(1);
|
2004-04-16 18:08:07 +00:00
|
|
|
if (&Root == TmpLHSI) {
|
2004-04-05 02:10:19 +00:00
|
|
|
Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType()));
|
|
|
|
return 0;
|
|
|
|
}
|
2004-04-16 18:08:07 +00:00
|
|
|
Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI
|
2003-08-13 19:01:45 +00:00
|
|
|
TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root
|
2004-04-16 18:08:07 +00:00
|
|
|
TmpLHSI->getParent()->getInstList().remove(TmpLHSI);
|
|
|
|
BasicBlock::iterator ARI = &Root; ++ARI;
|
|
|
|
BB->getInstList().insert(ARI, TmpLHSI); // Move TmpLHSI to after Root
|
|
|
|
ARI = Root;
|
2003-08-13 19:01:45 +00:00
|
|
|
|
|
|
|
// Now propagate the ExtraOperand down the chain of instructions until we
|
|
|
|
// get to LHSI.
|
|
|
|
while (TmpLHSI != LHSI) {
|
|
|
|
Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0));
|
2004-04-16 18:08:07 +00:00
|
|
|
// Move the instruction to immediately before the chain we are
|
|
|
|
// constructing to avoid breaking dominance properties.
|
|
|
|
NextLHSI->getParent()->getInstList().remove(NextLHSI);
|
|
|
|
BB->getInstList().insert(ARI, NextLHSI);
|
|
|
|
ARI = NextLHSI;
|
|
|
|
|
2003-08-13 19:01:45 +00:00
|
|
|
Value *NextOp = NextLHSI->getOperand(1);
|
|
|
|
NextLHSI->setOperand(1, ExtraOperand);
|
|
|
|
TmpLHSI = NextLHSI;
|
|
|
|
ExtraOperand = NextOp;
|
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2003-08-13 19:01:45 +00:00
|
|
|
// Now that the instructions are reassociated, have the functor perform
|
|
|
|
// the transformation...
|
|
|
|
return F.apply(Root);
|
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2003-08-13 19:01:45 +00:00
|
|
|
LHSI = dyn_cast<Instruction>(LHSI->getOperand(0));
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// AddRHS - Implements: X + X --> X << 1
|
|
|
|
struct AddRHS {
|
|
|
|
Value *RHS;
|
|
|
|
AddRHS(Value *rhs) : RHS(rhs) {}
|
|
|
|
bool shouldApply(Value *LHS) const { return LHS == RHS; }
|
|
|
|
Instruction *apply(BinaryOperator &Add) const {
|
2007-02-02 14:08:20 +00:00
|
|
|
return BinaryOperator::createShl(Add.getOperand(0),
|
2007-02-02 02:16:23 +00:00
|
|
|
ConstantInt::get(Add.getType(), 1));
|
2003-08-13 19:01:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2)
|
|
|
|
// iff C1&C2 == 0
|
|
|
|
struct AddMaskingAnd {
|
|
|
|
Constant *C2;
|
|
|
|
AddMaskingAnd(Constant *c) : C2(c) {}
|
|
|
|
bool shouldApply(Value *LHS) const {
|
2004-07-30 07:50:03 +00:00
|
|
|
ConstantInt *C1;
|
2005-04-21 23:48:37 +00:00
|
|
|
return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) &&
|
2004-07-30 07:50:03 +00:00
|
|
|
ConstantExpr::getAnd(C1, C2)->isNullValue();
|
2003-08-13 19:01:45 +00:00
|
|
|
}
|
|
|
|
Instruction *apply(BinaryOperator &Add) const {
|
2004-06-10 02:07:29 +00:00
|
|
|
return BinaryOperator::createOr(Add.getOperand(0), Add.getOperand(1));
|
2003-08-13 19:01:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2005-01-01 16:22:27 +00:00
|
|
|
static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO,
|
2004-04-09 19:05:30 +00:00
|
|
|
InstCombiner *IC) {
|
2006-11-27 01:05:10 +00:00
|
|
|
if (CastInst *CI = dyn_cast<CastInst>(&I)) {
|
2005-01-01 16:22:27 +00:00
|
|
|
if (Constant *SOC = dyn_cast<Constant>(SO))
|
2006-11-27 01:05:10 +00:00
|
|
|
return ConstantExpr::getCast(CI->getOpcode(), SOC, I.getType());
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
return IC->InsertNewInstBefore(CastInst::create(
|
|
|
|
CI->getOpcode(), SO, I.getType(), SO->getName() + ".cast"), I);
|
2005-01-01 16:22:27 +00:00
|
|
|
}
|
|
|
|
|
2004-04-09 19:05:30 +00:00
|
|
|
// Figure out if the constant is the left or the right argument.
|
2005-01-01 16:22:27 +00:00
|
|
|
bool ConstIsRHS = isa<Constant>(I.getOperand(1));
|
|
|
|
Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS));
|
2004-04-09 19:05:30 +00:00
|
|
|
|
|
|
|
if (Constant *SOC = dyn_cast<Constant>(SO)) {
|
|
|
|
if (ConstIsRHS)
|
2005-01-01 16:22:27 +00:00
|
|
|
return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand);
|
|
|
|
return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC);
|
2004-04-09 19:05:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Value *Op0 = SO, *Op1 = ConstOperand;
|
|
|
|
if (!ConstIsRHS)
|
|
|
|
std::swap(Op0, Op1);
|
|
|
|
Instruction *New;
|
2005-01-01 16:22:27 +00:00
|
|
|
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I))
|
|
|
|
New = BinaryOperator::create(BO->getOpcode(), Op0, Op1,SO->getName()+".op");
|
2006-12-23 06:05:41 +00:00
|
|
|
else if (CmpInst *CI = dyn_cast<CmpInst>(&I))
|
|
|
|
New = CmpInst::create(CI->getOpcode(), CI->getPredicate(), Op0, Op1,
|
|
|
|
SO->getName()+".cmp");
|
2004-04-10 19:15:56 +00:00
|
|
|
else {
|
2004-04-09 19:05:30 +00:00
|
|
|
assert(0 && "Unknown binary instruction type!");
|
2004-04-10 19:15:56 +00:00
|
|
|
abort();
|
|
|
|
}
|
2005-01-01 16:22:27 +00:00
|
|
|
return IC->InsertNewInstBefore(New, I);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FoldOpIntoSelect - Given an instruction with a select as one operand and a
|
|
|
|
// constant as the other operand, try to fold the binary operator into the
|
|
|
|
// select arguments. This also works for Cast instructions, which obviously do
|
|
|
|
// not have a second operand.
|
|
|
|
static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI,
|
|
|
|
InstCombiner *IC) {
|
|
|
|
// Don't modify shared select instructions
|
|
|
|
if (!SI->hasOneUse()) return 0;
|
|
|
|
Value *TV = SI->getOperand(1);
|
|
|
|
Value *FV = SI->getOperand(2);
|
|
|
|
|
|
|
|
if (isa<Constant>(TV) || isa<Constant>(FV)) {
|
2005-04-21 05:43:13 +00:00
|
|
|
// Bool selects with constant operands can be folded to logical ops.
|
2007-01-11 18:21:29 +00:00
|
|
|
if (SI->getType() == Type::Int1Ty) return 0;
|
2005-04-21 05:43:13 +00:00
|
|
|
|
2005-01-01 16:22:27 +00:00
|
|
|
Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC);
|
|
|
|
Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC);
|
|
|
|
|
2008-04-06 20:25:17 +00:00
|
|
|
return SelectInst::Create(SI->getCondition(), SelectTrueVal,
|
|
|
|
SelectFalseVal);
|
2005-01-01 16:22:27 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2004-04-09 19:05:30 +00:00
|
|
|
}
|
2003-08-13 19:01:45 +00:00
|
|
|
|
Fold binary expressions and casts into PHI nodes that have all constant inputs.
This takes something like this:
%A = phi int [ 3, %cond_false.0 ], [ 2, %endif.0.i ], [ 2, %endif.1.i ]
%B = div int %tmp.243, 4
and turns it into:
%A = phi int [ 3/4, %cond_false.0 ], [ 2/4, %endif.0.i ], [ 2/4, %endif.1.i ]
which is later simplified (in this case) into %A = 0.
This triggers thousands of times in spec, for example, 269 times in 176.gcc.
This is tested by InstCombine/add.ll:test23 and set.ll:test18.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16582 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-29 05:07:12 +00:00
|
|
|
|
|
|
|
/// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI
|
|
|
|
/// node as operand #0, see if we can fold the instruction into the PHI (which
|
|
|
|
/// is only possible if all operands to the PHI are constants).
|
|
|
|
Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) {
|
|
|
|
PHINode *PN = cast<PHINode>(I.getOperand(0));
|
2004-11-14 19:13:23 +00:00
|
|
|
unsigned NumPHIValues = PN->getNumIncomingValues();
|
2006-09-09 22:02:56 +00:00
|
|
|
if (!PN->hasOneUse() || NumPHIValues == 0) return 0;
|
|
|
|
|
|
|
|
// Check to see if all of the operands of the PHI are constants. If there is
|
|
|
|
// one non-constant value, remember the BB it is. If there is more than one
|
2007-02-24 01:03:45 +00:00
|
|
|
// or if *it* is a PHI, bail out.
|
2006-09-09 22:02:56 +00:00
|
|
|
BasicBlock *NonConstBB = 0;
|
|
|
|
for (unsigned i = 0; i != NumPHIValues; ++i)
|
|
|
|
if (!isa<Constant>(PN->getIncomingValue(i))) {
|
|
|
|
if (NonConstBB) return 0; // More than one non-const value.
|
2007-02-24 01:03:45 +00:00
|
|
|
if (isa<PHINode>(PN->getIncomingValue(i))) return 0; // Itself a phi.
|
2006-09-09 22:02:56 +00:00
|
|
|
NonConstBB = PN->getIncomingBlock(i);
|
|
|
|
|
|
|
|
// If the incoming non-constant value is in I's block, we have an infinite
|
|
|
|
// loop.
|
|
|
|
if (NonConstBB == I.getParent())
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is exactly one non-constant value, we can insert a copy of the
|
|
|
|
// operation in that block. However, if this is a critical edge, we would be
|
|
|
|
// inserting the computation one some other paths (e.g. inside a loop). Only
|
|
|
|
// do this if the pred block is unconditionally branching into the phi block.
|
|
|
|
if (NonConstBB) {
|
|
|
|
BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator());
|
|
|
|
if (!BI || !BI->isUnconditional()) return 0;
|
|
|
|
}
|
Fold binary expressions and casts into PHI nodes that have all constant inputs.
This takes something like this:
%A = phi int [ 3, %cond_false.0 ], [ 2, %endif.0.i ], [ 2, %endif.1.i ]
%B = div int %tmp.243, 4
and turns it into:
%A = phi int [ 3/4, %cond_false.0 ], [ 2/4, %endif.0.i ], [ 2/4, %endif.1.i ]
which is later simplified (in this case) into %A = 0.
This triggers thousands of times in spec, for example, 269 times in 176.gcc.
This is tested by InstCombine/add.ll:test23 and set.ll:test18.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16582 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-29 05:07:12 +00:00
|
|
|
|
|
|
|
// Okay, we can do the transformation: create the new PHI node.
|
2008-04-06 20:25:17 +00:00
|
|
|
PHINode *NewPN = PHINode::Create(I.getType(), "");
|
2005-01-29 00:39:08 +00:00
|
|
|
NewPN->reserveOperandSpace(PN->getNumOperands()/2);
|
Fold binary expressions and casts into PHI nodes that have all constant inputs.
This takes something like this:
%A = phi int [ 3, %cond_false.0 ], [ 2, %endif.0.i ], [ 2, %endif.1.i ]
%B = div int %tmp.243, 4
and turns it into:
%A = phi int [ 3/4, %cond_false.0 ], [ 2/4, %endif.0.i ], [ 2/4, %endif.1.i ]
which is later simplified (in this case) into %A = 0.
This triggers thousands of times in spec, for example, 269 times in 176.gcc.
This is tested by InstCombine/add.ll:test23 and set.ll:test18.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16582 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-29 05:07:12 +00:00
|
|
|
InsertNewInstBefore(NewPN, *PN);
|
2007-02-11 01:23:03 +00:00
|
|
|
NewPN->takeName(PN);
|
Fold binary expressions and casts into PHI nodes that have all constant inputs.
This takes something like this:
%A = phi int [ 3, %cond_false.0 ], [ 2, %endif.0.i ], [ 2, %endif.1.i ]
%B = div int %tmp.243, 4
and turns it into:
%A = phi int [ 3/4, %cond_false.0 ], [ 2/4, %endif.0.i ], [ 2/4, %endif.1.i ]
which is later simplified (in this case) into %A = 0.
This triggers thousands of times in spec, for example, 269 times in 176.gcc.
This is tested by InstCombine/add.ll:test23 and set.ll:test18.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16582 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-29 05:07:12 +00:00
|
|
|
|
|
|
|
// Next, add all of the operands to the PHI.
|
|
|
|
if (I.getNumOperands() == 2) {
|
|
|
|
Constant *C = cast<Constant>(I.getOperand(1));
|
2004-11-14 19:13:23 +00:00
|
|
|
for (unsigned i = 0; i != NumPHIValues; ++i) {
|
2007-08-05 08:47:58 +00:00
|
|
|
Value *InV = 0;
|
2006-09-09 22:02:56 +00:00
|
|
|
if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) {
|
2006-12-23 06:05:41 +00:00
|
|
|
if (CmpInst *CI = dyn_cast<CmpInst>(&I))
|
|
|
|
InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C);
|
|
|
|
else
|
|
|
|
InV = ConstantExpr::get(I.getOpcode(), InC, C);
|
2006-09-09 22:02:56 +00:00
|
|
|
} else {
|
|
|
|
assert(PN->getIncomingBlock(i) == NonConstBB);
|
|
|
|
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I))
|
|
|
|
InV = BinaryOperator::create(BO->getOpcode(),
|
|
|
|
PN->getIncomingValue(i), C, "phitmp",
|
|
|
|
NonConstBB->getTerminator());
|
2006-12-23 06:05:41 +00:00
|
|
|
else if (CmpInst *CI = dyn_cast<CmpInst>(&I))
|
|
|
|
InV = CmpInst::create(CI->getOpcode(),
|
|
|
|
CI->getPredicate(),
|
|
|
|
PN->getIncomingValue(i), C, "phitmp",
|
|
|
|
NonConstBB->getTerminator());
|
2006-09-09 22:02:56 +00:00
|
|
|
else
|
|
|
|
assert(0 && "Unknown binop!");
|
|
|
|
|
2007-03-02 21:28:56 +00:00
|
|
|
AddToWorkList(cast<Instruction>(InV));
|
2006-09-09 22:02:56 +00:00
|
|
|
}
|
|
|
|
NewPN->addIncoming(InV, PN->getIncomingBlock(i));
|
Fold binary expressions and casts into PHI nodes that have all constant inputs.
This takes something like this:
%A = phi int [ 3, %cond_false.0 ], [ 2, %endif.0.i ], [ 2, %endif.1.i ]
%B = div int %tmp.243, 4
and turns it into:
%A = phi int [ 3/4, %cond_false.0 ], [ 2/4, %endif.0.i ], [ 2/4, %endif.1.i ]
which is later simplified (in this case) into %A = 0.
This triggers thousands of times in spec, for example, 269 times in 176.gcc.
This is tested by InstCombine/add.ll:test23 and set.ll:test18.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16582 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-29 05:07:12 +00:00
|
|
|
}
|
2006-11-27 01:05:10 +00:00
|
|
|
} else {
|
|
|
|
CastInst *CI = cast<CastInst>(&I);
|
|
|
|
const Type *RetTy = CI->getType();
|
2004-11-14 19:13:23 +00:00
|
|
|
for (unsigned i = 0; i != NumPHIValues; ++i) {
|
2006-09-09 22:02:56 +00:00
|
|
|
Value *InV;
|
|
|
|
if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) {
|
2006-11-27 01:05:10 +00:00
|
|
|
InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy);
|
2006-09-09 22:02:56 +00:00
|
|
|
} else {
|
|
|
|
assert(PN->getIncomingBlock(i) == NonConstBB);
|
2006-11-27 01:05:10 +00:00
|
|
|
InV = CastInst::create(CI->getOpcode(), PN->getIncomingValue(i),
|
|
|
|
I.getType(), "phitmp",
|
|
|
|
NonConstBB->getTerminator());
|
2007-03-02 21:28:56 +00:00
|
|
|
AddToWorkList(cast<Instruction>(InV));
|
2006-09-09 22:02:56 +00:00
|
|
|
}
|
|
|
|
NewPN->addIncoming(InV, PN->getIncomingBlock(i));
|
Fold binary expressions and casts into PHI nodes that have all constant inputs.
This takes something like this:
%A = phi int [ 3, %cond_false.0 ], [ 2, %endif.0.i ], [ 2, %endif.1.i ]
%B = div int %tmp.243, 4
and turns it into:
%A = phi int [ 3/4, %cond_false.0 ], [ 2/4, %endif.0.i ], [ 2/4, %endif.1.i ]
which is later simplified (in this case) into %A = 0.
This triggers thousands of times in spec, for example, 269 times in 176.gcc.
This is tested by InstCombine/add.ll:test23 and set.ll:test18.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16582 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-29 05:07:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ReplaceInstUsesWith(I, NewPN);
|
|
|
|
}
|
|
|
|
|
2008-01-29 06:52:45 +00:00
|
|
|
|
|
|
|
/// CannotBeNegativeZero - Return true if we can prove that the specified FP
|
|
|
|
/// value is never equal to -0.0.
|
|
|
|
///
|
|
|
|
/// Note that this function will need to be revisited when we support nondefault
|
|
|
|
/// rounding modes!
|
|
|
|
///
|
|
|
|
static bool CannotBeNegativeZero(const Value *V) {
|
|
|
|
if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V))
|
|
|
|
return !CFP->getValueAPF().isNegZero();
|
|
|
|
|
|
|
|
// (add x, 0.0) is guaranteed to return +0.0, not -0.0.
|
|
|
|
if (const Instruction *I = dyn_cast<Instruction>(V)) {
|
|
|
|
if (I->getOpcode() == Instruction::Add &&
|
|
|
|
isa<ConstantFP>(I->getOperand(1)) &&
|
|
|
|
cast<ConstantFP>(I->getOperand(1))->isNullValue())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
|
|
|
|
if (II->getIntrinsicID() == Intrinsic::sqrt)
|
|
|
|
return CannotBeNegativeZero(II->getOperand(1));
|
|
|
|
|
|
|
|
if (const CallInst *CI = dyn_cast<CallInst>(I))
|
|
|
|
if (const Function *F = CI->getCalledFunction()) {
|
|
|
|
if (F->isDeclaration()) {
|
|
|
|
switch (F->getNameLen()) {
|
|
|
|
case 3: // abs(x) != -0.0
|
|
|
|
if (!strcmp(F->getNameStart(), "abs")) return true;
|
|
|
|
break;
|
|
|
|
case 4: // abs[lf](x) != -0.0
|
|
|
|
if (!strcmp(F->getNameStart(), "absf")) return true;
|
|
|
|
if (!strcmp(F->getNameStart(), "absl")) return true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
|
2003-03-10 21:43:22 +00:00
|
|
|
bool Changed = SimplifyCommutative(I);
|
2002-06-25 16:13:24 +00:00
|
|
|
Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
|
2002-05-06 16:49:18 +00:00
|
|
|
|
2004-04-10 22:01:55 +00:00
|
|
|
if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
|
2004-10-16 18:11:37 +00:00
|
|
|
// X + undef -> undef
|
|
|
|
if (isa<UndefValue>(RHS))
|
|
|
|
return ReplaceInstUsesWith(I, RHS);
|
|
|
|
|
2004-04-10 22:01:55 +00:00
|
|
|
// X + 0 --> X
|
2006-12-02 00:13:08 +00:00
|
|
|
if (!I.getType()->isFPOrFPVector()) { // NOTE: -0 + +0 = +0.
|
2005-10-17 17:56:38 +00:00
|
|
|
if (RHSC->isNullValue())
|
|
|
|
return ReplaceInstUsesWith(I, LHS);
|
2005-10-17 20:18:38 +00:00
|
|
|
} else if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
|
2007-09-14 22:26:36 +00:00
|
|
|
if (CFP->isExactlyValue(ConstantFP::getNegativeZero
|
|
|
|
(I.getType())->getValueAPF()))
|
2005-10-17 20:18:38 +00:00
|
|
|
return ReplaceInstUsesWith(I, LHS);
|
2005-10-17 17:56:38 +00:00
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-04-10 22:01:55 +00:00
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) {
|
2006-11-09 05:12:27 +00:00
|
|
|
// X + (signbit) --> X ^ signbit
|
2007-04-01 17:13:37 +00:00
|
|
|
const APInt& Val = CI->getValue();
|
2007-04-02 08:20:41 +00:00
|
|
|
uint32_t BitWidth = Val.getBitWidth();
|
2007-03-23 21:24:59 +00:00
|
|
|
if (Val == APInt::getSignBit(BitWidth))
|
2004-06-10 02:07:29 +00:00
|
|
|
return BinaryOperator::createXor(LHS, RHS);
|
2006-11-09 05:12:27 +00:00
|
|
|
|
|
|
|
// See if SimplifyDemandedBits can simplify this. This handles stuff like
|
|
|
|
// (X & 254)+1 -> (X&254)|1
|
2007-03-23 21:24:59 +00:00
|
|
|
if (!isa<VectorType>(I.getType())) {
|
|
|
|
APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
|
|
|
|
if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth),
|
|
|
|
KnownZero, KnownOne))
|
|
|
|
return &I;
|
|
|
|
}
|
2004-04-10 22:01:55 +00:00
|
|
|
}
|
Fold binary expressions and casts into PHI nodes that have all constant inputs.
This takes something like this:
%A = phi int [ 3, %cond_false.0 ], [ 2, %endif.0.i ], [ 2, %endif.1.i ]
%B = div int %tmp.243, 4
and turns it into:
%A = phi int [ 3/4, %cond_false.0 ], [ 2/4, %endif.0.i ], [ 2/4, %endif.1.i ]
which is later simplified (in this case) into %A = 0.
This triggers thousands of times in spec, for example, 269 times in 176.gcc.
This is tested by InstCombine/add.ll:test23 and set.ll:test18.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16582 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-29 05:07:12 +00:00
|
|
|
|
|
|
|
if (isa<PHINode>(LHS))
|
|
|
|
if (Instruction *NV = FoldOpIntoPhi(I))
|
|
|
|
return NV;
|
2005-09-24 23:43:33 +00:00
|
|
|
|
2006-01-06 17:59:59 +00:00
|
|
|
ConstantInt *XorRHS = 0;
|
|
|
|
Value *XorLHS = 0;
|
Symbolically evaluate constant expressions like &A[123] - &A[4].f.
This occurs in C++ code like:
#include <iostream>
#include <iterator>
int a[] = { 1, 2, 3, 4, 5 };
int main() {
using namespace std;
copy(a, a + sizeof(a)/sizeof(a[0]), ostream_iterator<int>(cout, "\n"));
return 0;
}
Before we would decide the loop trip count is:
sdiv (i32 sub (i32 ptrtoint (i32* getelementptr ([5 x i32]* @a, i32 0, i32 5) to i32), i32 ptrtoint ([5 x i32]* @a to i32)), i32 4)
Now we decide it is "5". Amazing.
This code will need to be refactored, but I'm doing that as a separate
commit.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33665 91177308-0d34-0410-b5e6-96231b3b80d8
2007-01-30 22:32:46 +00:00
|
|
|
if (isa<ConstantInt>(RHSC) &&
|
|
|
|
match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) {
|
2007-04-02 08:20:41 +00:00
|
|
|
uint32_t TySizeBits = I.getType()->getPrimitiveSizeInBits();
|
2007-04-01 17:13:37 +00:00
|
|
|
const APInt& RHSVal = cast<ConstantInt>(RHSC)->getValue();
|
2005-09-24 23:43:33 +00:00
|
|
|
|
2007-04-02 08:20:41 +00:00
|
|
|
uint32_t Size = TySizeBits / 2;
|
2007-03-23 21:24:59 +00:00
|
|
|
APInt C0080Val(APInt(TySizeBits, 1ULL).shl(Size - 1));
|
|
|
|
APInt CFF80Val(-C0080Val);
|
2005-09-24 23:43:33 +00:00
|
|
|
do {
|
|
|
|
if (TySizeBits > Size) {
|
|
|
|
// 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.
|
2007-03-23 21:24:59 +00:00
|
|
|
if ((RHSVal == CFF80Val && XorRHS->getValue() == C0080Val) ||
|
|
|
|
(RHSVal == C0080Val && XorRHS->getValue() == CFF80Val)) {
|
2005-09-24 23:43:33 +00:00
|
|
|
// This is a sign extend if the top bits are known zero.
|
2007-03-29 08:15:12 +00:00
|
|
|
if (!MaskedValueIsZero(XorLHS,
|
|
|
|
APInt::getHighBitsSet(TySizeBits, TySizeBits - Size)))
|
2005-09-24 23:43:33 +00:00
|
|
|
Size = 0; // Not a sign ext, but can't be any others either.
|
2007-03-23 21:24:59 +00:00
|
|
|
break;
|
2005-09-24 23:43:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Size >>= 1;
|
2007-03-23 21:24:59 +00:00
|
|
|
C0080Val = APIntOps::lshr(C0080Val, Size);
|
|
|
|
CFF80Val = APIntOps::ashr(CFF80Val, Size);
|
|
|
|
} while (Size >= 1);
|
2005-09-24 23:43:33 +00:00
|
|
|
|
2007-03-28 01:36:16 +00:00
|
|
|
// FIXME: This shouldn't be necessary. When the backends can handle types
|
|
|
|
// with funny bit widths then this whole cascade of if statements should
|
|
|
|
// be removed. It is just here to get the size of the "middle" type back
|
|
|
|
// up to something that the back ends can handle.
|
|
|
|
const Type *MiddleType = 0;
|
|
|
|
switch (Size) {
|
|
|
|
default: break;
|
|
|
|
case 32: MiddleType = Type::Int32Ty; break;
|
|
|
|
case 16: MiddleType = Type::Int16Ty; break;
|
|
|
|
case 8: MiddleType = Type::Int8Ty; break;
|
|
|
|
}
|
|
|
|
if (MiddleType) {
|
2006-12-12 23:36:14 +00:00
|
|
|
Instruction *NewTrunc = new TruncInst(XorLHS, MiddleType, "sext");
|
2005-09-24 23:43:33 +00:00
|
|
|
InsertNewInstBefore(NewTrunc, I);
|
2007-03-28 01:36:16 +00:00
|
|
|
return new SExtInst(NewTrunc, I.getType(), I.getName());
|
2005-09-24 23:43:33 +00:00
|
|
|
}
|
|
|
|
}
|
2004-04-10 22:01:55 +00:00
|
|
|
}
|
2002-05-06 16:49:18 +00:00
|
|
|
|
2003-08-13 19:01:45 +00:00
|
|
|
// X + X --> X << 1
|
2007-01-15 02:27:26 +00:00
|
|
|
if (I.getType()->isInteger() && I.getType() != Type::Int1Ty) {
|
2003-08-13 19:01:45 +00:00
|
|
|
if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result;
|
2005-04-07 17:14:51 +00:00
|
|
|
|
|
|
|
if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) {
|
|
|
|
if (RHSI->getOpcode() == Instruction::Sub)
|
|
|
|
if (LHS == RHSI->getOperand(1)) // A + (B - A) --> B
|
|
|
|
return ReplaceInstUsesWith(I, RHSI->getOperand(0));
|
|
|
|
}
|
|
|
|
if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
|
|
|
|
if (LHSI->getOpcode() == Instruction::Sub)
|
|
|
|
if (RHS == LHSI->getOperand(1)) // (B - A) + A --> B
|
|
|
|
return ReplaceInstUsesWith(I, LHSI->getOperand(0));
|
|
|
|
}
|
2004-07-27 21:02:21 +00:00
|
|
|
}
|
2003-08-13 04:18:28 +00:00
|
|
|
|
2002-05-08 22:46:53 +00:00
|
|
|
// -A + B --> B - A
|
2008-02-17 21:03:36 +00:00
|
|
|
// -A + -B --> -(A + B)
|
|
|
|
if (Value *LHSV = dyn_castNegVal(LHS)) {
|
2008-02-18 17:50:16 +00:00
|
|
|
if (LHS->getType()->isIntOrIntVector()) {
|
|
|
|
if (Value *RHSV = dyn_castNegVal(RHS)) {
|
|
|
|
Instruction *NewAdd = BinaryOperator::createAdd(LHSV, RHSV, "sum");
|
|
|
|
InsertNewInstBefore(NewAdd, I);
|
|
|
|
return BinaryOperator::createNeg(NewAdd);
|
|
|
|
}
|
2008-02-17 21:03:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return BinaryOperator::createSub(RHS, LHSV);
|
|
|
|
}
|
2002-05-06 16:49:18 +00:00
|
|
|
|
|
|
|
// A + -B --> A - B
|
2003-03-10 23:06:50 +00:00
|
|
|
if (!isa<Constant>(RHS))
|
|
|
|
if (Value *V = dyn_castNegVal(RHS))
|
2004-06-10 02:07:29 +00:00
|
|
|
return BinaryOperator::createSub(LHS, V);
|
2002-04-18 17:39:14 +00:00
|
|
|
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-11-13 19:50:12 +00:00
|
|
|
ConstantInt *C2;
|
|
|
|
if (Value *X = dyn_castFoldableMul(LHS, C2)) {
|
|
|
|
if (X == RHS) // X*C + X --> X * (C+1)
|
|
|
|
return BinaryOperator::createMul(RHS, AddOne(C2));
|
|
|
|
|
|
|
|
// X*C1 + X*C2 --> X * (C1+C2)
|
|
|
|
ConstantInt *C1;
|
|
|
|
if (X == dyn_castFoldableMul(RHS, C1))
|
2007-03-25 05:33:51 +00:00
|
|
|
return BinaryOperator::createMul(X, Add(C1, C2));
|
2003-02-18 19:57:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// X + X*C --> X * (C+1)
|
2004-11-13 19:50:12 +00:00
|
|
|
if (dyn_castFoldableMul(RHS, C2) == LHS)
|
|
|
|
return BinaryOperator::createMul(LHS, AddOne(C2));
|
|
|
|
|
2007-01-05 02:17:46 +00:00
|
|
|
// X + ~X --> -1 since ~X = -X-1
|
2007-06-15 06:23:19 +00:00
|
|
|
if (dyn_castNotVal(LHS) == RHS || dyn_castNotVal(RHS) == LHS)
|
|
|
|
return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
|
2007-01-05 02:17:46 +00:00
|
|
|
|
2003-02-18 19:57:07 +00:00
|
|
|
|
2003-08-13 19:01:45 +00:00
|
|
|
// (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0
|
2004-07-30 07:50:03 +00:00
|
|
|
if (match(RHS, m_And(m_Value(), m_ConstantInt(C2))))
|
2007-01-05 02:17:46 +00:00
|
|
|
if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2)))
|
|
|
|
return R;
|
2003-03-11 00:12:48 +00:00
|
|
|
|
2008-02-03 07:42:09 +00:00
|
|
|
// W*X + Y*Z --> W * (X+Z) iff W == Y
|
2008-02-03 08:19:11 +00:00
|
|
|
if (I.getType()->isIntOrIntVector()) {
|
2008-02-03 07:42:09 +00:00
|
|
|
Value *W, *X, *Y, *Z;
|
|
|
|
if (match(LHS, m_Mul(m_Value(W), m_Value(X))) &&
|
|
|
|
match(RHS, m_Mul(m_Value(Y), m_Value(Z)))) {
|
|
|
|
if (W != Y) {
|
|
|
|
if (W == Z) {
|
2008-02-26 10:53:30 +00:00
|
|
|
std::swap(Y, Z);
|
2008-02-03 07:42:09 +00:00
|
|
|
} else if (Y == X) {
|
2008-02-26 10:53:30 +00:00
|
|
|
std::swap(W, X);
|
|
|
|
} else if (X == Z) {
|
2008-02-03 07:42:09 +00:00
|
|
|
std::swap(Y, Z);
|
|
|
|
std::swap(W, X);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (W == Y) {
|
|
|
|
Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, Z,
|
|
|
|
LHS->getName()), I);
|
|
|
|
return BinaryOperator::createMul(W, NewAdd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-02 15:11:26 +00:00
|
|
|
if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) {
|
2006-01-06 17:59:59 +00:00
|
|
|
Value *X = 0;
|
2007-03-25 05:33:51 +00:00
|
|
|
if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X
|
|
|
|
return BinaryOperator::createSub(SubOne(CRHS), X);
|
2004-07-30 07:50:03 +00:00
|
|
|
|
2004-10-08 05:07:56 +00:00
|
|
|
// (X & FF00) + xx00 -> (X+xx00) & FF00
|
|
|
|
if (LHS->hasOneUse() && match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) {
|
2007-03-25 05:33:51 +00:00
|
|
|
Constant *Anded = And(CRHS, C2);
|
2004-10-08 05:07:56 +00:00
|
|
|
if (Anded == CRHS) {
|
|
|
|
// See if all bits from the first bit set in the Add RHS up are included
|
|
|
|
// in the mask. First, get the rightmost bit.
|
2007-04-01 17:13:37 +00:00
|
|
|
const APInt& AddRHSV = CRHS->getValue();
|
2004-10-08 05:07:56 +00:00
|
|
|
|
|
|
|
// Form a mask of all bits from the lowest bit added through the top.
|
2007-04-01 17:13:37 +00:00
|
|
|
APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1));
|
2004-10-08 05:07:56 +00:00
|
|
|
|
|
|
|
// See if the and mask includes all of these bits.
|
2007-04-01 17:13:37 +00:00
|
|
|
APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue());
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-10-08 05:07:56 +00:00
|
|
|
if (AddRHSHighBits == AddRHSHighBitsAnd) {
|
|
|
|
// Okay, the xform is safe. Insert the new add pronto.
|
|
|
|
Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, CRHS,
|
|
|
|
LHS->getName()), I);
|
|
|
|
return BinaryOperator::createAnd(NewAdd, C2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-30 07:50:03 +00:00
|
|
|
// Try to fold constant add into select arguments.
|
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(LHS))
|
2005-01-01 16:22:27 +00:00
|
|
|
if (Instruction *R = FoldOpIntoSelect(I, SI, this))
|
2004-07-30 07:50:03 +00:00
|
|
|
return R;
|
2003-10-02 15:11:26 +00:00
|
|
|
}
|
|
|
|
|
2006-10-26 06:15:43 +00:00
|
|
|
// add (cast *A to intptrtype) B ->
|
2007-12-20 01:56:58 +00:00
|
|
|
// cast (GEP (cast *A to sbyte*) B) --> intptrtype
|
2006-09-19 18:24:51 +00:00
|
|
|
{
|
2006-11-27 01:05:10 +00:00
|
|
|
CastInst *CI = dyn_cast<CastInst>(LHS);
|
|
|
|
Value *Other = RHS;
|
2006-09-19 18:24:51 +00:00
|
|
|
if (!CI) {
|
|
|
|
CI = dyn_cast<CastInst>(RHS);
|
|
|
|
Other = LHS;
|
|
|
|
}
|
2006-09-20 15:37:57 +00:00
|
|
|
if (CI && CI->getType()->isSized() &&
|
2007-01-08 16:32:00 +00:00
|
|
|
(CI->getType()->getPrimitiveSizeInBits() ==
|
|
|
|
TD->getIntPtrType()->getPrimitiveSizeInBits())
|
2006-09-20 15:37:57 +00:00
|
|
|
&& isa<PointerType>(CI->getOperand(0)->getType())) {
|
2007-12-17 01:12:55 +00:00
|
|
|
unsigned AS =
|
|
|
|
cast<PointerType>(CI->getOperand(0)->getType())->getAddressSpace();
|
2008-01-13 22:23:22 +00:00
|
|
|
Value *I2 = InsertBitCastBefore(CI->getOperand(0),
|
|
|
|
PointerType::get(Type::Int8Ty, AS), I);
|
2008-04-06 20:25:17 +00:00
|
|
|
I2 = InsertNewInstBefore(GetElementPtrInst::Create(I2, Other, "ctg2"), I);
|
2006-11-27 01:05:10 +00:00
|
|
|
return new PtrToIntInst(I2, CI->getType());
|
2006-09-19 18:24:51 +00:00
|
|
|
}
|
|
|
|
}
|
2007-12-18 09:34:41 +00:00
|
|
|
|
2007-12-20 01:56:58 +00:00
|
|
|
// add (select X 0 (sub n A)) A --> select X A n
|
2007-12-18 09:34:41 +00:00
|
|
|
{
|
|
|
|
SelectInst *SI = dyn_cast<SelectInst>(LHS);
|
|
|
|
Value *Other = RHS;
|
|
|
|
if (!SI) {
|
|
|
|
SI = dyn_cast<SelectInst>(RHS);
|
|
|
|
Other = LHS;
|
|
|
|
}
|
2007-12-20 01:56:58 +00:00
|
|
|
if (SI && SI->hasOneUse()) {
|
2007-12-18 09:34:41 +00:00
|
|
|
Value *TV = SI->getTrueValue();
|
|
|
|
Value *FV = SI->getFalseValue();
|
2007-12-20 01:56:58 +00:00
|
|
|
Value *A, *N;
|
2007-12-18 09:34:41 +00:00
|
|
|
|
|
|
|
// Can we fold the add into the argument of the select?
|
|
|
|
// We check both true and false select arguments for a matching subtract.
|
2007-12-20 01:56:58 +00:00
|
|
|
if (match(FV, m_Zero()) && match(TV, m_Sub(m_Value(N), m_Value(A))) &&
|
|
|
|
A == Other) // Fold the add into the true select value.
|
2008-04-06 20:25:17 +00:00
|
|
|
return SelectInst::Create(SI->getCondition(), N, A);
|
2007-12-20 01:56:58 +00:00
|
|
|
if (match(TV, m_Zero()) && match(FV, m_Sub(m_Value(N), m_Value(A))) &&
|
|
|
|
A == Other) // Fold the add into the false select value.
|
2008-04-06 20:25:17 +00:00
|
|
|
return SelectInst::Create(SI->getCondition(), A, N);
|
2007-12-18 09:34:41 +00:00
|
|
|
}
|
|
|
|
}
|
2008-01-29 06:52:45 +00:00
|
|
|
|
|
|
|
// Check for X+0.0. Simplify it to X if we know X is not -0.0.
|
|
|
|
if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS))
|
|
|
|
if (CFP->getValueAPF().isPosZero() && CannotBeNegativeZero(LHS))
|
|
|
|
return ReplaceInstUsesWith(I, LHS);
|
2006-09-19 18:24:51 +00:00
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
return Changed ? &I : 0;
|
2002-04-18 17:39:14 +00:00
|
|
|
}
|
2002-02-26 21:46:54 +00:00
|
|
|
|
2003-07-22 21:46:59 +00:00
|
|
|
// isSignBit - Return true if the value represented by the constant only has the
|
|
|
|
// highest order bit set.
|
|
|
|
static bool isSignBit(ConstantInt *CI) {
|
2007-04-02 08:20:41 +00:00
|
|
|
uint32_t NumBits = CI->getType()->getPrimitiveSizeInBits();
|
2007-03-19 20:58:18 +00:00
|
|
|
return CI->getValue() == APInt::getSignBit(NumBits);
|
2003-07-22 21:46:59 +00:00
|
|
|
}
|
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
Instruction *InstCombiner::visitSub(BinaryOperator &I) {
|
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
2002-05-06 16:14:14 +00:00
|
|
|
|
2002-08-12 21:17:25 +00:00
|
|
|
if (Op0 == Op1) // sub X, X -> 0
|
|
|
|
return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
|
2002-04-18 17:39:14 +00:00
|
|
|
|
2002-08-12 21:17:25 +00:00
|
|
|
// If this is a 'B = x-(-A)', change to B = x+A...
|
2003-03-10 23:06:50 +00:00
|
|
|
if (Value *V = dyn_castNegVal(Op1))
|
2004-06-10 02:07:29 +00:00
|
|
|
return BinaryOperator::createAdd(Op0, V);
|
2002-05-06 16:49:18 +00:00
|
|
|
|
2004-10-16 18:11:37 +00:00
|
|
|
if (isa<UndefValue>(Op0))
|
|
|
|
return ReplaceInstUsesWith(I, Op0); // undef - X -> undef
|
|
|
|
if (isa<UndefValue>(Op1))
|
|
|
|
return ReplaceInstUsesWith(I, Op1); // X - undef -> undef
|
|
|
|
|
2003-11-05 01:06:05 +00:00
|
|
|
if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) {
|
|
|
|
// Replace (-1 - A) with (~A)...
|
2003-02-18 19:28:33 +00:00
|
|
|
if (C->isAllOnesValue())
|
|
|
|
return BinaryOperator::createNot(Op1);
|
|
|
|
|
2003-11-05 01:06:05 +00:00
|
|
|
// C - ~X == X + (1+C)
|
2005-06-18 17:37:34 +00:00
|
|
|
Value *X = 0;
|
2004-07-30 07:50:03 +00:00
|
|
|
if (match(Op1, m_Not(m_Value(X))))
|
2007-03-25 05:33:51 +00:00
|
|
|
return BinaryOperator::createAdd(X, AddOne(C));
|
|
|
|
|
2007-01-15 07:02:54 +00:00
|
|
|
// -(X >>u 31) -> (X >>s 31)
|
|
|
|
// -(X >>s 31) -> (X >>u 31)
|
2007-03-30 17:20:39 +00:00
|
|
|
if (C->isZero()) {
|
2008-02-20 11:26:25 +00:00
|
|
|
if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op1)) {
|
2006-11-08 06:47:33 +00:00
|
|
|
if (SI->getOpcode() == Instruction::LShr) {
|
2006-10-20 07:07:24 +00:00
|
|
|
if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) {
|
2004-03-12 23:53:13 +00:00
|
|
|
// Check to see if we are shifting out everything but the sign bit.
|
2007-03-30 17:20:39 +00:00
|
|
|
if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) ==
|
2006-10-20 07:07:24 +00:00
|
|
|
SI->getType()->getPrimitiveSizeInBits()-1) {
|
2006-11-08 06:47:33 +00:00
|
|
|
// Ok, the transformation is safe. Insert AShr.
|
2007-02-02 02:16:23 +00:00
|
|
|
return BinaryOperator::create(Instruction::AShr,
|
|
|
|
SI->getOperand(0), CU, SI->getName());
|
2006-11-08 06:47:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (SI->getOpcode() == Instruction::AShr) {
|
|
|
|
if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) {
|
|
|
|
// Check to see if we are shifting out everything but the sign bit.
|
2007-03-30 17:20:39 +00:00
|
|
|
if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) ==
|
2006-11-08 06:47:33 +00:00
|
|
|
SI->getType()->getPrimitiveSizeInBits()-1) {
|
2006-12-31 05:48:39 +00:00
|
|
|
// Ok, the transformation is safe. Insert LShr.
|
2007-02-02 14:08:20 +00:00
|
|
|
return BinaryOperator::createLShr(
|
2007-02-02 02:16:23 +00:00
|
|
|
SI->getOperand(0), CU, SI->getName());
|
2004-03-12 23:53:13 +00:00
|
|
|
}
|
|
|
|
}
|
2008-02-20 11:26:25 +00:00
|
|
|
}
|
|
|
|
}
|
2004-03-13 00:11:49 +00:00
|
|
|
}
|
2004-04-09 19:05:30 +00:00
|
|
|
|
|
|
|
// Try to fold constant sub into select arguments.
|
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
|
2005-01-01 16:22:27 +00:00
|
|
|
if (Instruction *R = FoldOpIntoSelect(I, SI, this))
|
2004-04-09 19:05:30 +00:00
|
|
|
return R;
|
Fold binary expressions and casts into PHI nodes that have all constant inputs.
This takes something like this:
%A = phi int [ 3, %cond_false.0 ], [ 2, %endif.0.i ], [ 2, %endif.1.i ]
%B = div int %tmp.243, 4
and turns it into:
%A = phi int [ 3/4, %cond_false.0 ], [ 2/4, %endif.0.i ], [ 2/4, %endif.1.i ]
which is later simplified (in this case) into %A = 0.
This triggers thousands of times in spec, for example, 269 times in 176.gcc.
This is tested by InstCombine/add.ll:test23 and set.ll:test18.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16582 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-29 05:07:12 +00:00
|
|
|
|
|
|
|
if (isa<PHINode>(Op0))
|
|
|
|
if (Instruction *NV = FoldOpIntoPhi(I))
|
|
|
|
return NV;
|
2003-11-05 01:06:05 +00:00
|
|
|
}
|
|
|
|
|
2005-04-07 16:15:25 +00:00
|
|
|
if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) {
|
|
|
|
if (Op1I->getOpcode() == Instruction::Add &&
|
2006-12-02 00:13:08 +00:00
|
|
|
!Op0->getType()->isFPOrFPVector()) {
|
2005-04-07 16:28:01 +00:00
|
|
|
if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y
|
2005-04-07 16:15:25 +00:00
|
|
|
return BinaryOperator::createNeg(Op1I->getOperand(1), I.getName());
|
2005-04-07 16:28:01 +00:00
|
|
|
else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y
|
2005-04-07 16:15:25 +00:00
|
|
|
return BinaryOperator::createNeg(Op1I->getOperand(0), I.getName());
|
2005-04-07 16:28:01 +00:00
|
|
|
else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) {
|
|
|
|
if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1)))
|
|
|
|
// C1-(X+C2) --> (C1-C2)-X
|
2007-03-25 05:33:51 +00:00
|
|
|
return BinaryOperator::createSub(Subtract(CI1, CI2),
|
2005-04-07 16:28:01 +00:00
|
|
|
Op1I->getOperand(0));
|
|
|
|
}
|
2005-04-07 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2003-10-15 16:48:29 +00:00
|
|
|
if (Op1I->hasOneUse()) {
|
2003-02-18 19:28:33 +00:00
|
|
|
// Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression
|
|
|
|
// is not used by anyone else...
|
|
|
|
//
|
2004-02-02 20:09:56 +00:00
|
|
|
if (Op1I->getOpcode() == Instruction::Sub &&
|
2006-12-02 00:13:08 +00:00
|
|
|
!Op1I->getType()->isFPOrFPVector()) {
|
2003-02-18 19:28:33 +00:00
|
|
|
// Swap the two operands of the subexpr...
|
|
|
|
Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1);
|
|
|
|
Op1I->setOperand(0, IIOp1);
|
|
|
|
Op1I->setOperand(1, IIOp0);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2003-02-18 19:28:33 +00:00
|
|
|
// Create the new top level add instruction...
|
2004-06-10 02:07:29 +00:00
|
|
|
return BinaryOperator::createAdd(Op0, Op1);
|
2003-02-18 19:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)...
|
|
|
|
//
|
|
|
|
if (Op1I->getOpcode() == Instruction::And &&
|
|
|
|
(Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) {
|
|
|
|
Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0);
|
|
|
|
|
Be more careful about the order we put stuff onto the worklist. This allow us to
collapse this:
bool %le(int %A, int %B) {
%c1 = setgt int %A, %B
%tmp = select bool %c1, int 1, int 0
%c2 = setlt int %A, %B
%result = select bool %c2, int -1, int %tmp
%c3 = setle int %result, 0
ret bool %c3
}
into:
bool %le(int %A, int %B) {
%c3 = setle int %A, %B ; <bool> [#uses=1]
ret bool %c3
}
which is handy, because the Java FE makes these sequences all over the place.
This is tested as: test/Regression/Transforms/InstCombine/JavaCompare.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@14086 91177308-0d34-0410-b5e6-96231b3b80d8
2004-06-09 05:08:07 +00:00
|
|
|
Value *NewNot =
|
|
|
|
InsertNewInstBefore(BinaryOperator::createNot(OtherOp, "B.not"), I);
|
2004-06-10 02:07:29 +00:00
|
|
|
return BinaryOperator::createAnd(Op0, NewNot);
|
2003-02-18 19:28:33 +00:00
|
|
|
}
|
2003-02-18 19:57:07 +00:00
|
|
|
|
2006-10-16 23:08:08 +00:00
|
|
|
// 0 - (X sdiv C) -> (X sdiv -C)
|
2006-10-26 06:15:43 +00:00
|
|
|
if (Op1I->getOpcode() == Instruction::SDiv)
|
2006-10-20 07:07:24 +00:00
|
|
|
if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0))
|
2007-04-19 05:39:12 +00:00
|
|
|
if (CSI->isZero())
|
2004-10-06 15:08:25 +00:00
|
|
|
if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1)))
|
2006-10-26 06:15:43 +00:00
|
|
|
return BinaryOperator::createSDiv(Op1I->getOperand(0),
|
2004-10-06 15:08:25 +00:00
|
|
|
ConstantExpr::getNeg(DivRHS));
|
|
|
|
|
2003-02-18 19:57:07 +00:00
|
|
|
// X - X*C --> X * (1-C)
|
2005-06-18 17:37:34 +00:00
|
|
|
ConstantInt *C2 = 0;
|
2004-11-13 19:50:12 +00:00
|
|
|
if (dyn_castFoldableMul(Op1I, C2) == Op0) {
|
2007-03-25 05:33:51 +00:00
|
|
|
Constant *CP1 = Subtract(ConstantInt::get(I.getType(), 1), C2);
|
2004-06-10 02:07:29 +00:00
|
|
|
return BinaryOperator::createMul(Op0, CP1);
|
2003-02-18 19:57:07 +00:00
|
|
|
}
|
2007-09-17 17:31:57 +00:00
|
|
|
|
|
|
|
// X - ((X / Y) * Y) --> X % Y
|
|
|
|
if (Op1I->getOpcode() == Instruction::Mul)
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(Op1I->getOperand(0)))
|
|
|
|
if (Op0 == I->getOperand(0) &&
|
|
|
|
Op1I->getOperand(1) == I->getOperand(1)) {
|
|
|
|
if (I->getOpcode() == Instruction::SDiv)
|
|
|
|
return BinaryOperator::createSRem(Op0, Op1I->getOperand(1));
|
|
|
|
if (I->getOpcode() == Instruction::UDiv)
|
|
|
|
return BinaryOperator::createURem(Op0, Op1I->getOperand(1));
|
|
|
|
}
|
2002-05-09 01:29:19 +00:00
|
|
|
}
|
2005-04-07 16:15:25 +00:00
|
|
|
}
|
2003-02-18 19:28:33 +00:00
|
|
|
|
2006-12-02 00:13:08 +00:00
|
|
|
if (!Op0->getType()->isFPOrFPVector())
|
2008-02-20 11:26:25 +00:00
|
|
|
if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
|
2005-04-07 17:14:51 +00:00
|
|
|
if (Op0I->getOpcode() == Instruction::Add) {
|
2005-01-19 21:50:18 +00:00
|
|
|
if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X
|
|
|
|
return ReplaceInstUsesWith(I, Op0I->getOperand(1));
|
|
|
|
else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X
|
|
|
|
return ReplaceInstUsesWith(I, Op0I->getOperand(0));
|
2005-04-07 17:14:51 +00:00
|
|
|
} else if (Op0I->getOpcode() == Instruction::Sub) {
|
|
|
|
if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y
|
|
|
|
return BinaryOperator::createNeg(Op0I->getOperand(1), I.getName());
|
2005-01-19 21:50:18 +00:00
|
|
|
}
|
2008-02-20 11:26:25 +00:00
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-11-13 19:50:12 +00:00
|
|
|
ConstantInt *C1;
|
|
|
|
if (Value *X = dyn_castFoldableMul(Op0, C1)) {
|
2007-03-25 05:33:51 +00:00
|
|
|
if (X == Op1) // X*C - X --> X * (C-1)
|
|
|
|
return BinaryOperator::createMul(Op1, SubOne(C1));
|
2003-02-18 19:57:07 +00:00
|
|
|
|
2004-11-13 19:50:12 +00:00
|
|
|
ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2)
|
|
|
|
if (X == dyn_castFoldableMul(Op1, C2))
|
2008-02-22 10:00:35 +00:00
|
|
|
return BinaryOperator::createMul(X, Subtract(C1, C2));
|
2004-11-13 19:50:12 +00:00
|
|
|
}
|
2002-05-06 16:14:14 +00:00
|
|
|
return 0;
|
2002-04-18 17:39:14 +00:00
|
|
|
}
|
|
|
|
|
2007-07-15 20:42:37 +00:00
|
|
|
/// isSignBitCheck - Given an exploded icmp instruction, return true if the
|
|
|
|
/// comparison only checks the sign bit. If it only checks the sign bit, set
|
|
|
|
/// TrueIfSigned if the result of the comparison is true when the input value is
|
|
|
|
/// signed.
|
|
|
|
static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS,
|
|
|
|
bool &TrueIfSigned) {
|
2006-12-23 06:05:41 +00:00
|
|
|
switch (pred) {
|
2007-07-15 20:42:37 +00:00
|
|
|
case ICmpInst::ICMP_SLT: // True if LHS s< 0
|
|
|
|
TrueIfSigned = true;
|
|
|
|
return RHS->isZero();
|
2007-07-16 04:15:34 +00:00
|
|
|
case ICmpInst::ICMP_SLE: // True if LHS s<= RHS and RHS == -1
|
|
|
|
TrueIfSigned = true;
|
|
|
|
return RHS->isAllOnesValue();
|
2007-07-15 20:42:37 +00:00
|
|
|
case ICmpInst::ICMP_SGT: // True if LHS s> -1
|
|
|
|
TrueIfSigned = false;
|
|
|
|
return RHS->isAllOnesValue();
|
2007-07-16 04:15:34 +00:00
|
|
|
case ICmpInst::ICMP_UGT:
|
|
|
|
// True if LHS u> RHS and RHS == high-bit-mask - 1
|
|
|
|
TrueIfSigned = true;
|
|
|
|
return RHS->getValue() ==
|
|
|
|
APInt::getSignedMaxValue(RHS->getType()->getPrimitiveSizeInBits());
|
|
|
|
case ICmpInst::ICMP_UGE:
|
|
|
|
// True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc)
|
|
|
|
TrueIfSigned = true;
|
|
|
|
return RHS->getValue() ==
|
|
|
|
APInt::getSignBit(RHS->getType()->getPrimitiveSizeInBits());
|
2007-07-15 20:42:37 +00:00
|
|
|
default:
|
|
|
|
return false;
|
2004-02-23 06:38:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
Instruction *InstCombiner::visitMul(BinaryOperator &I) {
|
2003-03-10 21:43:22 +00:00
|
|
|
bool Changed = SimplifyCommutative(I);
|
2003-02-18 19:28:33 +00:00
|
|
|
Value *Op0 = I.getOperand(0);
|
2002-04-18 17:39:14 +00:00
|
|
|
|
2004-10-16 18:11:37 +00:00
|
|
|
if (isa<UndefValue>(I.getOperand(1))) // undef * X -> 0
|
|
|
|
return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
|
|
|
|
|
2002-08-12 21:17:25 +00:00
|
|
|
// Simplify mul instructions with a constant RHS...
|
2003-02-18 19:28:33 +00:00
|
|
|
if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) {
|
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
|
2003-08-13 04:18:28 +00:00
|
|
|
|
|
|
|
// ((X << C1)*C2) == (X * (C2 << C1))
|
2007-02-02 02:16:23 +00:00
|
|
|
if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op0))
|
2003-08-13 04:18:28 +00:00
|
|
|
if (SI->getOpcode() == Instruction::Shl)
|
|
|
|
if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1)))
|
2004-06-10 02:07:29 +00:00
|
|
|
return BinaryOperator::createMul(SI->getOperand(0),
|
|
|
|
ConstantExpr::getShl(CI, ShOp));
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2007-04-19 05:39:12 +00:00
|
|
|
if (CI->isZero())
|
2003-09-11 22:24:54 +00:00
|
|
|
return ReplaceInstUsesWith(I, Op1); // X * 0 == 0
|
|
|
|
if (CI->equalsInt(1)) // X * 1 == X
|
|
|
|
return ReplaceInstUsesWith(I, Op0);
|
|
|
|
if (CI->isAllOnesValue()) // X * -1 == 0 - X
|
2003-06-25 17:09:20 +00:00
|
|
|
return BinaryOperator::createNeg(Op0, I.getName());
|
2002-04-29 22:24:47 +00:00
|
|
|
|
2007-03-29 01:57:21 +00:00
|
|
|
const APInt& Val = cast<ConstantInt>(CI)->getValue();
|
2007-03-23 20:05:17 +00:00
|
|
|
if (Val.isPowerOf2()) { // Replace X*(2^C) with X << C
|
2007-02-02 14:08:20 +00:00
|
|
|
return BinaryOperator::createShl(Op0,
|
2007-03-23 20:05:17 +00:00
|
|
|
ConstantInt::get(Op0->getType(), Val.logBase2()));
|
2005-08-02 19:16:58 +00:00
|
|
|
}
|
2004-07-27 21:02:21 +00:00
|
|
|
} else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) {
|
2003-02-18 19:28:33 +00:00
|
|
|
if (Op1F->isNullValue())
|
|
|
|
return ReplaceInstUsesWith(I, Op1);
|
|
|
|
|
|
|
|
// "In IEEE floating point, x*1 is not equivalent to x for nans. However,
|
|
|
|
// ANSI says we can drop signals, so we can do this anyway." (from GCC)
|
2007-09-14 22:26:36 +00:00
|
|
|
// We need a better interface for long double here.
|
|
|
|
if (Op1->getType() == Type::FloatTy || Op1->getType() == Type::DoubleTy)
|
|
|
|
if (Op1F->isExactlyValue(1.0))
|
|
|
|
return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0'
|
2003-02-18 19:28:33 +00:00
|
|
|
}
|
2006-03-04 06:04:02 +00:00
|
|
|
|
|
|
|
if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0))
|
|
|
|
if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() &&
|
|
|
|
isa<ConstantInt>(Op0I->getOperand(1))) {
|
|
|
|
// Canonicalize (X+C1)*C2 -> X*C2+C1*C2.
|
|
|
|
Instruction *Add = BinaryOperator::createMul(Op0I->getOperand(0),
|
|
|
|
Op1, "tmp");
|
|
|
|
InsertNewInstBefore(Add, I);
|
|
|
|
Value *C1C2 = ConstantExpr::getMul(Op1,
|
|
|
|
cast<Constant>(Op0I->getOperand(1)));
|
|
|
|
return BinaryOperator::createAdd(Add, C1C2);
|
|
|
|
|
|
|
|
}
|
2004-04-09 19:05:30 +00:00
|
|
|
|
|
|
|
// Try to fold constant mul into select arguments.
|
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
|
2005-01-01 16:22:27 +00:00
|
|
|
if (Instruction *R = FoldOpIntoSelect(I, SI, this))
|
2004-04-09 19:05:30 +00:00
|
|
|
return R;
|
Fold binary expressions and casts into PHI nodes that have all constant inputs.
This takes something like this:
%A = phi int [ 3, %cond_false.0 ], [ 2, %endif.0.i ], [ 2, %endif.1.i ]
%B = div int %tmp.243, 4
and turns it into:
%A = phi int [ 3/4, %cond_false.0 ], [ 2/4, %endif.0.i ], [ 2/4, %endif.1.i ]
which is later simplified (in this case) into %A = 0.
This triggers thousands of times in spec, for example, 269 times in 176.gcc.
This is tested by InstCombine/add.ll:test23 and set.ll:test18.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16582 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-29 05:07:12 +00:00
|
|
|
|
|
|
|
if (isa<PHINode>(Op0))
|
|
|
|
if (Instruction *NV = FoldOpIntoPhi(I))
|
|
|
|
return NV;
|
2001-12-14 16:52:21 +00:00
|
|
|
}
|
|
|
|
|
2003-03-10 23:23:04 +00:00
|
|
|
if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y
|
|
|
|
if (Value *Op1v = dyn_castNegVal(I.getOperand(1)))
|
2004-06-10 02:07:29 +00:00
|
|
|
return BinaryOperator::createMul(Op0v, Op1v);
|
2003-03-10 23:23:04 +00:00
|
|
|
|
2004-02-23 05:39:21 +00:00
|
|
|
// If one of the operands of the multiply is a cast from a boolean value, then
|
|
|
|
// we know the bool is either zero or one, so this is a 'masking' multiply.
|
|
|
|
// See if we can simplify things based on how the boolean was originally
|
|
|
|
// formed.
|
|
|
|
CastInst *BoolCast = 0;
|
2006-12-13 18:21:21 +00:00
|
|
|
if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(0)))
|
2007-01-11 18:21:29 +00:00
|
|
|
if (CI->getOperand(0)->getType() == Type::Int1Ty)
|
2004-02-23 05:39:21 +00:00
|
|
|
BoolCast = CI;
|
|
|
|
if (!BoolCast)
|
2006-12-13 18:21:21 +00:00
|
|
|
if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(1)))
|
2007-01-11 18:21:29 +00:00
|
|
|
if (CI->getOperand(0)->getType() == Type::Int1Ty)
|
2004-02-23 05:39:21 +00:00
|
|
|
BoolCast = CI;
|
|
|
|
if (BoolCast) {
|
2006-12-23 06:05:41 +00:00
|
|
|
if (ICmpInst *SCI = dyn_cast<ICmpInst>(BoolCast->getOperand(0))) {
|
2004-02-23 05:39:21 +00:00
|
|
|
Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1);
|
|
|
|
const Type *SCOpTy = SCIOp0->getType();
|
2007-07-15 20:42:37 +00:00
|
|
|
bool TIS = false;
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
// If the icmp is true iff the sign bit of X is set, then convert this
|
2004-02-23 06:38:22 +00:00
|
|
|
// multiply into a shift/and combination.
|
|
|
|
if (isa<ConstantInt>(SCIOp1) &&
|
2007-07-15 20:42:37 +00:00
|
|
|
isSignBitCheck(SCI->getPredicate(), cast<ConstantInt>(SCIOp1), TIS) &&
|
|
|
|
TIS) {
|
2004-02-23 05:39:21 +00:00
|
|
|
// Shift the X value right to turn it into "all signbits".
|
2007-02-02 02:16:23 +00:00
|
|
|
Constant *Amt = ConstantInt::get(SCIOp0->getType(),
|
2005-04-24 06:59:08 +00:00
|
|
|
SCOpTy->getPrimitiveSizeInBits()-1);
|
2004-02-23 06:38:22 +00:00
|
|
|
Value *V =
|
2007-02-02 02:16:23 +00:00
|
|
|
InsertNewInstBefore(
|
|
|
|
BinaryOperator::create(Instruction::AShr, SCIOp0, Amt,
|
2004-02-23 06:38:22 +00:00
|
|
|
BoolCast->getOperand(0)->getName()+
|
|
|
|
".mask"), I);
|
2004-02-23 05:39:21 +00:00
|
|
|
|
|
|
|
// If the multiply type is not the same as the source type, sign extend
|
|
|
|
// or truncate to the multiply type.
|
2006-12-12 09:18:51 +00:00
|
|
|
if (I.getType() != V->getType()) {
|
2007-04-02 08:20:41 +00:00
|
|
|
uint32_t SrcBits = V->getType()->getPrimitiveSizeInBits();
|
|
|
|
uint32_t DstBits = I.getType()->getPrimitiveSizeInBits();
|
2006-12-12 09:18:51 +00:00
|
|
|
Instruction::CastOps opcode =
|
|
|
|
(SrcBits == DstBits ? Instruction::BitCast :
|
|
|
|
(SrcBits < DstBits ? Instruction::SExt : Instruction::Trunc));
|
|
|
|
V = InsertCastBefore(opcode, V, I.getType(), I);
|
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-02-23 05:39:21 +00:00
|
|
|
Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0;
|
2004-06-10 02:07:29 +00:00
|
|
|
return BinaryOperator::createAnd(V, OtherOp);
|
2004-02-23 05:39:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
return Changed ? &I : 0;
|
2002-04-18 17:39:14 +00:00
|
|
|
}
|
|
|
|
|
2006-10-26 06:15:43 +00:00
|
|
|
/// This function implements the transforms on div instructions that work
|
|
|
|
/// regardless of the kind of div instruction it is (udiv, sdiv, or fdiv). It is
|
|
|
|
/// used by the visitors to those instructions.
|
|
|
|
/// @brief Transforms common to all three div instructions
|
2006-11-27 01:05:10 +00:00
|
|
|
Instruction *InstCombiner::commonDivTransforms(BinaryOperator &I) {
|
2004-12-12 21:48:58 +00:00
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
|
|
|
|
2008-02-19 06:12:18 +00:00
|
|
|
// undef / X -> 0 for integer.
|
|
|
|
// undef / X -> undef for FP (the undef could be a snan).
|
|
|
|
if (isa<UndefValue>(Op0)) {
|
|
|
|
if (Op0->getType()->isFPOrFPVector())
|
|
|
|
return ReplaceInstUsesWith(I, Op0);
|
2004-10-16 18:11:37 +00:00
|
|
|
return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
|
2008-02-19 06:12:18 +00:00
|
|
|
}
|
Fold binary expressions and casts into PHI nodes that have all constant inputs.
This takes something like this:
%A = phi int [ 3, %cond_false.0 ], [ 2, %endif.0.i ], [ 2, %endif.1.i ]
%B = div int %tmp.243, 4
and turns it into:
%A = phi int [ 3/4, %cond_false.0 ], [ 2/4, %endif.0.i ], [ 2/4, %endif.1.i ]
which is later simplified (in this case) into %A = 0.
This triggers thousands of times in spec, for example, 269 times in 176.gcc.
This is tested by InstCombine/add.ll:test23 and set.ll:test18.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16582 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-29 05:07:12 +00:00
|
|
|
|
2006-10-26 06:15:43 +00:00
|
|
|
// X / undef -> undef
|
|
|
|
if (isa<UndefValue>(Op1))
|
|
|
|
return ReplaceInstUsesWith(I, Op1);
|
2003-02-18 19:28:33 +00:00
|
|
|
|
2008-01-28 00:58:18 +00:00
|
|
|
// Handle cases involving: [su]div X, (select Cond, Y, Z)
|
|
|
|
// This does not apply for fdiv.
|
2006-09-09 20:26:32 +00:00
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) {
|
2008-01-28 00:58:18 +00:00
|
|
|
// [su]div X, (Cond ? 0 : Y) -> div X, Y. If the div and the select are in
|
|
|
|
// the same basic block, then we replace the select with Y, and the
|
|
|
|
// condition of the select with false (if the cond value is in the same BB).
|
|
|
|
// If the select has uses other than the div, this allows them to be
|
|
|
|
// simplified also. Note that div X, Y is just as good as div X, 0 (undef)
|
|
|
|
if (ConstantInt *ST = dyn_cast<ConstantInt>(SI->getOperand(1)))
|
2006-09-09 20:26:32 +00:00
|
|
|
if (ST->isNullValue()) {
|
|
|
|
Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0));
|
|
|
|
if (CondI && CondI->getParent() == I.getParent())
|
2007-01-11 12:24:14 +00:00
|
|
|
UpdateValueUsesWith(CondI, ConstantInt::getFalse());
|
2006-09-09 20:26:32 +00:00
|
|
|
else if (I.getParent() != SI->getParent() || SI->hasOneUse())
|
|
|
|
I.setOperand(1, SI->getOperand(2));
|
|
|
|
else
|
|
|
|
UpdateValueUsesWith(SI, SI->getOperand(2));
|
|
|
|
return &I;
|
|
|
|
}
|
2006-10-26 06:15:43 +00:00
|
|
|
|
2008-01-28 00:58:18 +00:00
|
|
|
// Likewise for: [su]div X, (Cond ? Y : 0) -> div X, Y
|
|
|
|
if (ConstantInt *ST = dyn_cast<ConstantInt>(SI->getOperand(2)))
|
2006-09-09 20:26:32 +00:00
|
|
|
if (ST->isNullValue()) {
|
|
|
|
Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0));
|
|
|
|
if (CondI && CondI->getParent() == I.getParent())
|
2007-01-11 12:24:14 +00:00
|
|
|
UpdateValueUsesWith(CondI, ConstantInt::getTrue());
|
2006-09-09 20:26:32 +00:00
|
|
|
else if (I.getParent() != SI->getParent() || SI->hasOneUse())
|
|
|
|
I.setOperand(1, SI->getOperand(1));
|
|
|
|
else
|
|
|
|
UpdateValueUsesWith(SI, SI->getOperand(1));
|
|
|
|
return &I;
|
|
|
|
}
|
2006-10-26 06:15:43 +00:00
|
|
|
}
|
2006-09-09 20:26:32 +00:00
|
|
|
|
2006-10-26 06:15:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This function implements the transforms common to both integer division
|
|
|
|
/// instructions (udiv and sdiv). It is called by the visitors to those integer
|
|
|
|
/// division instructions.
|
|
|
|
/// @brief Common integer divide transforms
|
2006-11-27 01:05:10 +00:00
|
|
|
Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) {
|
2006-10-26 06:15:43 +00:00
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
|
|
|
|
|
|
|
if (Instruction *Common = commonDivTransforms(I))
|
|
|
|
return Common;
|
|
|
|
|
|
|
|
if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
|
|
|
|
// div X, 1 == X
|
|
|
|
if (RHS->equalsInt(1))
|
|
|
|
return ReplaceInstUsesWith(I, Op0);
|
|
|
|
|
|
|
|
// (X / C1) / C2 -> X / (C1*C2)
|
|
|
|
if (Instruction *LHS = dyn_cast<Instruction>(Op0))
|
|
|
|
if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode())
|
|
|
|
if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) {
|
2008-02-18 22:48:05 +00:00
|
|
|
if (MultiplyOverflows(RHS, LHSRHS, I.getOpcode()==Instruction::SDiv))
|
|
|
|
return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
|
|
|
|
else
|
|
|
|
return BinaryOperator::create(I.getOpcode(), LHS->getOperand(0),
|
|
|
|
Multiply(RHS, LHSRHS));
|
2006-10-26 06:15:43 +00:00
|
|
|
}
|
|
|
|
|
2007-03-23 20:05:17 +00:00
|
|
|
if (!RHS->isZero()) { // avoid X udiv 0
|
2006-10-26 06:15:43 +00:00
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
|
|
|
|
if (Instruction *R = FoldOpIntoSelect(I, SI, this))
|
|
|
|
return R;
|
|
|
|
if (isa<PHINode>(Op0))
|
|
|
|
if (Instruction *NV = FoldOpIntoPhi(I))
|
|
|
|
return NV;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 0 / X == 0, we don't need to preserve faults!
|
|
|
|
if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0))
|
|
|
|
if (LHS->equalsInt(0))
|
|
|
|
return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InstCombiner::visitUDiv(BinaryOperator &I) {
|
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
|
|
|
|
|
|
|
// Handle the integer div common cases
|
|
|
|
if (Instruction *Common = commonIDivTransforms(I))
|
|
|
|
return Common;
|
|
|
|
|
|
|
|
// X udiv C^2 -> X >> C
|
|
|
|
// Check to see if this is an unsigned division with an exact power of 2,
|
|
|
|
// if so, convert to a right shift.
|
|
|
|
if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) {
|
2007-03-26 23:58:26 +00:00
|
|
|
if (C->getValue().isPowerOf2()) // 0 not included in isPowerOf2
|
2007-03-23 20:05:17 +00:00
|
|
|
return BinaryOperator::createLShr(Op0,
|
2007-03-25 05:01:29 +00:00
|
|
|
ConstantInt::get(Op0->getType(), C->getValue().logBase2()));
|
2006-10-26 06:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2)
|
2007-02-02 02:16:23 +00:00
|
|
|
if (BinaryOperator *RHSI = dyn_cast<BinaryOperator>(I.getOperand(1))) {
|
2006-10-26 06:15:43 +00:00
|
|
|
if (RHSI->getOpcode() == Instruction::Shl &&
|
|
|
|
isa<ConstantInt>(RHSI->getOperand(0))) {
|
2007-04-01 17:13:37 +00:00
|
|
|
const APInt& C1 = cast<ConstantInt>(RHSI->getOperand(0))->getValue();
|
2007-03-23 20:05:17 +00:00
|
|
|
if (C1.isPowerOf2()) {
|
2006-10-26 06:15:43 +00:00
|
|
|
Value *N = RHSI->getOperand(1);
|
2006-11-27 01:05:10 +00:00
|
|
|
const Type *NTy = N->getType();
|
2007-03-23 21:24:59 +00:00
|
|
|
if (uint32_t C2 = C1.logBase2()) {
|
2006-10-26 06:15:43 +00:00
|
|
|
Constant *C2V = ConstantInt::get(NTy, C2);
|
|
|
|
N = InsertNewInstBefore(BinaryOperator::createAdd(N, C2V, "tmp"), I);
|
|
|
|
}
|
2007-02-02 14:08:20 +00:00
|
|
|
return BinaryOperator::createLShr(Op0, N);
|
2006-10-26 06:15:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// udiv X, (Select Cond, C1, C2) --> Select Cond, (shr X, C1), (shr X, C2)
|
|
|
|
// where C1&C2 are powers of two.
|
2007-03-05 23:36:13 +00:00
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
|
2006-10-20 07:07:24 +00:00
|
|
|
if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1)))
|
2007-03-05 23:36:13 +00:00
|
|
|
if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) {
|
2007-04-01 17:13:37 +00:00
|
|
|
const APInt &TVA = STO->getValue(), &FVA = SFO->getValue();
|
2007-03-23 20:05:17 +00:00
|
|
|
if (TVA.isPowerOf2() && FVA.isPowerOf2()) {
|
2007-03-05 23:36:13 +00:00
|
|
|
// Compute the shift amounts
|
2007-03-23 20:05:17 +00:00
|
|
|
uint32_t TSA = TVA.logBase2(), FSA = FVA.logBase2();
|
2007-03-05 23:36:13 +00:00
|
|
|
// Construct the "on true" case of the select
|
|
|
|
Constant *TC = ConstantInt::get(Op0->getType(), TSA);
|
|
|
|
Instruction *TSI = BinaryOperator::createLShr(
|
|
|
|
Op0, TC, SI->getName()+".t");
|
|
|
|
TSI = InsertNewInstBefore(TSI, I);
|
|
|
|
|
|
|
|
// Construct the "on false" case of the select
|
|
|
|
Constant *FC = ConstantInt::get(Op0->getType(), FSA);
|
|
|
|
Instruction *FSI = BinaryOperator::createLShr(
|
|
|
|
Op0, FC, SI->getName()+".f");
|
|
|
|
FSI = InsertNewInstBefore(FSI, I);
|
|
|
|
|
|
|
|
// construct the select instruction and return it.
|
2008-04-06 20:25:17 +00:00
|
|
|
return SelectInst::Create(SI->getOperand(0), TSI, FSI, SI->getName());
|
2005-04-08 04:03:26 +00:00
|
|
|
}
|
2007-03-05 23:36:13 +00:00
|
|
|
}
|
2006-10-26 06:15:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2006-10-26 06:15:43 +00:00
|
|
|
Instruction *InstCombiner::visitSDiv(BinaryOperator &I) {
|
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
2003-02-18 19:28:33 +00:00
|
|
|
|
2006-10-26 06:15:43 +00:00
|
|
|
// Handle the integer div common cases
|
|
|
|
if (Instruction *Common = commonIDivTransforms(I))
|
|
|
|
return Common;
|
|
|
|
|
|
|
|
if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
|
|
|
|
// sdiv X, -1 == -X
|
|
|
|
if (RHS->isAllOnesValue())
|
|
|
|
return BinaryOperator::createNeg(Op0);
|
|
|
|
|
|
|
|
// -X/C -> X/-C
|
|
|
|
if (Value *LHSNeg = dyn_castNegVal(Op0))
|
|
|
|
return BinaryOperator::createSDiv(LHSNeg, ConstantExpr::getNeg(RHS));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the sign bits of both operands are zero (i.e. we can prove they are
|
|
|
|
// unsigned inputs), turn this into a udiv.
|
2007-01-15 02:27:26 +00:00
|
|
|
if (I.getType()->isInteger()) {
|
2007-03-23 20:05:17 +00:00
|
|
|
APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
|
2006-02-07 07:27:52 +00:00
|
|
|
if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) {
|
2007-11-05 23:16:33 +00:00
|
|
|
// X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set
|
2006-10-26 06:15:43 +00:00
|
|
|
return BinaryOperator::createUDiv(Op0, Op1, I.getName());
|
2006-02-05 07:54:04 +00:00
|
|
|
}
|
2006-10-26 06:15:43 +00:00
|
|
|
}
|
2005-11-05 07:40:31 +00:00
|
|
|
|
2002-05-06 16:14:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-26 06:15:43 +00:00
|
|
|
Instruction *InstCombiner::visitFDiv(BinaryOperator &I) {
|
|
|
|
return commonDivTransforms(I);
|
|
|
|
}
|
2002-05-06 16:14:14 +00:00
|
|
|
|
2006-11-02 01:53:59 +00:00
|
|
|
/// This function implements the transforms on rem instructions that work
|
|
|
|
/// regardless of the kind of rem instruction it is (urem, srem, or frem). It
|
|
|
|
/// is used by the visitors to those instructions.
|
|
|
|
/// @brief Transforms common to all three rem instructions
|
|
|
|
Instruction *InstCombiner::commonRemTransforms(BinaryOperator &I) {
|
2004-12-12 21:48:58 +00:00
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
2006-11-02 01:53:59 +00:00
|
|
|
|
2008-02-19 06:12:18 +00:00
|
|
|
// 0 % X == 0 for integer, we don't need to preserve faults!
|
2006-02-28 05:30:45 +00:00
|
|
|
if (Constant *LHS = dyn_cast<Constant>(Op0))
|
|
|
|
if (LHS->isNullValue())
|
|
|
|
return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
|
|
|
|
|
2008-02-19 06:12:18 +00:00
|
|
|
if (isa<UndefValue>(Op0)) { // undef % X -> 0
|
|
|
|
if (I.getType()->isFPOrFPVector())
|
|
|
|
return ReplaceInstUsesWith(I, Op0); // X % undef -> undef (could be SNaN)
|
2006-02-28 05:30:45 +00:00
|
|
|
return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
|
2008-02-19 06:12:18 +00:00
|
|
|
}
|
2006-02-28 05:30:45 +00:00
|
|
|
if (isa<UndefValue>(Op1))
|
|
|
|
return ReplaceInstUsesWith(I, Op1); // X % undef -> undef
|
2006-11-02 01:53:59 +00:00
|
|
|
|
|
|
|
// Handle cases involving: rem X, (select Cond, Y, Z)
|
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) {
|
|
|
|
// rem X, (Cond ? 0 : Y) -> rem X, Y. If the rem and the select are in
|
|
|
|
// the same basic block, then we replace the select with Y, and the
|
|
|
|
// condition of the select with false (if the cond value is in the same
|
|
|
|
// BB). If the select has uses other than the div, this allows them to be
|
|
|
|
// simplified also.
|
|
|
|
if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1)))
|
|
|
|
if (ST->isNullValue()) {
|
|
|
|
Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0));
|
|
|
|
if (CondI && CondI->getParent() == I.getParent())
|
2007-01-11 12:24:14 +00:00
|
|
|
UpdateValueUsesWith(CondI, ConstantInt::getFalse());
|
2006-11-02 01:53:59 +00:00
|
|
|
else if (I.getParent() != SI->getParent() || SI->hasOneUse())
|
|
|
|
I.setOperand(1, SI->getOperand(2));
|
|
|
|
else
|
|
|
|
UpdateValueUsesWith(SI, SI->getOperand(2));
|
|
|
|
return &I;
|
|
|
|
}
|
|
|
|
// Likewise for: rem X, (Cond ? Y : 0) -> rem X, Y
|
|
|
|
if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2)))
|
|
|
|
if (ST->isNullValue()) {
|
|
|
|
Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0));
|
|
|
|
if (CondI && CondI->getParent() == I.getParent())
|
2007-01-11 12:24:14 +00:00
|
|
|
UpdateValueUsesWith(CondI, ConstantInt::getTrue());
|
2006-11-02 01:53:59 +00:00
|
|
|
else if (I.getParent() != SI->getParent() || SI->hasOneUse())
|
|
|
|
I.setOperand(1, SI->getOperand(1));
|
|
|
|
else
|
|
|
|
UpdateValueUsesWith(SI, SI->getOperand(1));
|
2004-07-06 07:01:22 +00:00
|
|
|
return &I;
|
|
|
|
}
|
Turn srem -> urem when neither input has their sign bit set. This triggers
8 times in vortex, allowing the srems to be turned into shrs:
OLD: %tmp.104 = rem int %tmp.5.i37, 16 ; <int> [#uses=1]
NEW: %tmp.104 = rem uint %tmp.5.i37, 16 ; <uint> [#uses=0]
OLD: %tmp.98 = rem int %tmp.5.i24, 16 ; <int> [#uses=1]
NEW: %tmp.98 = rem uint %tmp.5.i24, 16 ; <uint> [#uses=0]
OLD: %tmp.91 = rem int %tmp.5.i19, 8 ; <int> [#uses=1]
NEW: %tmp.91 = rem uint %tmp.5.i19, 8 ; <uint> [#uses=0]
OLD: %tmp.88 = rem int %tmp.5.i14, 8 ; <int> [#uses=1]
NEW: %tmp.88 = rem uint %tmp.5.i14, 8 ; <uint> [#uses=0]
OLD: %tmp.85 = rem int %tmp.5.i9, 1024 ; <int> [#uses=2]
NEW: %tmp.85 = rem uint %tmp.5.i9, 1024 ; <uint> [#uses=0]
OLD: %tmp.82 = rem int %tmp.5.i, 512 ; <int> [#uses=2]
NEW: %tmp.82 = rem uint %tmp.5.i1, 512 ; <uint> [#uses=0]
OLD: %tmp.48.i = rem int %tmp.5.i.i161, 4 ; <int> [#uses=1]
NEW: %tmp.48.i = rem uint %tmp.5.i.i161, 4 ; <uint> [#uses=0]
OLD: %tmp.20.i2 = rem int %tmp.5.i.i, 4 ; <int> [#uses=1]
NEW: %tmp.20.i2 = rem uint %tmp.5.i.i, 4 ; <uint> [#uses=0]
it also occurs 9 times in gcc, but with odd constant divisors (1009 and 61)
so the payoff isn't as great.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@24189 91177308-0d34-0410-b5e6-96231b3b80d8
2005-11-05 07:28:37 +00:00
|
|
|
}
|
2004-07-06 07:01:22 +00:00
|
|
|
|
2006-11-02 01:53:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This function implements the transforms common to both integer remainder
|
|
|
|
/// instructions (urem and srem). It is called by the visitors to those integer
|
|
|
|
/// remainder instructions.
|
|
|
|
/// @brief Common integer remainder transforms
|
|
|
|
Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) {
|
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
|
|
|
|
|
|
|
if (Instruction *common = commonRemTransforms(I))
|
|
|
|
return common;
|
|
|
|
|
2004-12-12 21:48:58 +00:00
|
|
|
if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
|
2006-02-28 05:30:45 +00:00
|
|
|
// X % 0 == undef, we don't need to preserve faults!
|
|
|
|
if (RHS->equalsInt(0))
|
|
|
|
return ReplaceInstUsesWith(I, UndefValue::get(I.getType()));
|
|
|
|
|
2003-02-18 19:28:33 +00:00
|
|
|
if (RHS->equalsInt(1)) // X % 1 == 0
|
|
|
|
return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
|
|
|
|
|
2006-02-28 05:49:21 +00:00
|
|
|
if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) {
|
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) {
|
|
|
|
if (Instruction *R = FoldOpIntoSelect(I, SI, this))
|
|
|
|
return R;
|
|
|
|
} else if (isa<PHINode>(Op0I)) {
|
|
|
|
if (Instruction *NV = FoldOpIntoPhi(I))
|
|
|
|
return NV;
|
|
|
|
}
|
2008-03-06 06:48:30 +00:00
|
|
|
|
|
|
|
// See if we can fold away this rem instruction.
|
|
|
|
uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth();
|
|
|
|
APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
|
|
|
|
if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth),
|
|
|
|
KnownZero, KnownOne))
|
|
|
|
return &I;
|
2006-02-28 05:49:21 +00:00
|
|
|
}
|
2003-02-18 19:28:33 +00:00
|
|
|
}
|
|
|
|
|
2006-11-02 01:53:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InstCombiner::visitURem(BinaryOperator &I) {
|
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
|
|
|
|
|
|
|
if (Instruction *common = commonIRemTransforms(I))
|
|
|
|
return common;
|
|
|
|
|
|
|
|
if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
|
|
|
|
// X urem C^2 -> X and C
|
|
|
|
// Check to see if this is an unsigned remainder with an exact power of 2,
|
|
|
|
// if so, convert to a bitwise and.
|
|
|
|
if (ConstantInt *C = dyn_cast<ConstantInt>(RHS))
|
2007-03-23 20:05:17 +00:00
|
|
|
if (C->getValue().isPowerOf2())
|
2006-11-02 01:53:59 +00:00
|
|
|
return BinaryOperator::createAnd(Op0, SubOne(C));
|
|
|
|
}
|
|
|
|
|
2006-02-05 07:54:04 +00:00
|
|
|
if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) {
|
2006-11-02 01:53:59 +00:00
|
|
|
// Turn A % (C << N), where C is 2^k, into A & ((C << N)-1)
|
|
|
|
if (RHSI->getOpcode() == Instruction::Shl &&
|
|
|
|
isa<ConstantInt>(RHSI->getOperand(0))) {
|
2007-03-25 05:01:29 +00:00
|
|
|
if (cast<ConstantInt>(RHSI->getOperand(0))->getValue().isPowerOf2()) {
|
2006-02-05 07:54:04 +00:00
|
|
|
Constant *N1 = ConstantInt::getAllOnesValue(I.getType());
|
|
|
|
Value *Add = InsertNewInstBefore(BinaryOperator::createAdd(RHSI, N1,
|
|
|
|
"tmp"), I);
|
|
|
|
return BinaryOperator::createAnd(Op0, Add);
|
|
|
|
}
|
|
|
|
}
|
2006-11-02 01:53:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// urem X, (select Cond, 2^C1, 2^C2) --> select Cond, (and X, C1), (and X, C2)
|
|
|
|
// where C1&C2 are powers of two.
|
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) {
|
|
|
|
if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1)))
|
|
|
|
if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) {
|
|
|
|
// STO == 0 and SFO == 0 handled above.
|
2007-03-23 20:05:17 +00:00
|
|
|
if ((STO->getValue().isPowerOf2()) &&
|
|
|
|
(SFO->getValue().isPowerOf2())) {
|
2006-11-02 01:53:59 +00:00
|
|
|
Value *TrueAnd = InsertNewInstBefore(
|
|
|
|
BinaryOperator::createAnd(Op0, SubOne(STO), SI->getName()+".t"), I);
|
|
|
|
Value *FalseAnd = InsertNewInstBefore(
|
|
|
|
BinaryOperator::createAnd(Op0, SubOne(SFO), SI->getName()+".f"), I);
|
2008-04-06 20:25:17 +00:00
|
|
|
return SelectInst::Create(SI->getOperand(0), TrueAnd, FalseAnd);
|
2006-09-09 20:26:32 +00:00
|
|
|
}
|
2006-11-02 01:53:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2006-09-09 20:26:32 +00:00
|
|
|
|
2006-11-02 01:53:59 +00:00
|
|
|
Instruction *InstCombiner::visitSRem(BinaryOperator &I) {
|
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
|
|
|
|
2007-11-05 23:16:33 +00:00
|
|
|
// Handle the integer rem common cases
|
2006-11-02 01:53:59 +00:00
|
|
|
if (Instruction *common = commonIRemTransforms(I))
|
|
|
|
return common;
|
|
|
|
|
|
|
|
if (Value *RHSNeg = dyn_castNegVal(Op1))
|
|
|
|
if (!isa<ConstantInt>(RHSNeg) ||
|
2007-03-25 05:01:29 +00:00
|
|
|
cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive()) {
|
2006-11-02 01:53:59 +00:00
|
|
|
// X % -Y -> X % Y
|
|
|
|
AddUsesToWorkList(I);
|
|
|
|
I.setOperand(1, RHSNeg);
|
|
|
|
return &I;
|
2006-09-09 20:26:32 +00:00
|
|
|
}
|
2006-11-02 01:53:59 +00:00
|
|
|
|
2007-11-05 23:16:33 +00:00
|
|
|
// If the sign bits of both operands are zero (i.e. we can prove they are
|
2006-11-02 01:53:59 +00:00
|
|
|
// unsigned inputs), turn this into a urem.
|
2007-11-05 23:16:33 +00:00
|
|
|
if (I.getType()->isInteger()) {
|
|
|
|
APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
|
|
|
|
if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) {
|
|
|
|
// X srem Y -> X urem Y, iff X and Y don't have sign bit set
|
|
|
|
return BinaryOperator::createURem(Op0, Op1, I.getName());
|
|
|
|
}
|
2006-02-05 07:54:04 +00:00
|
|
|
}
|
2006-11-02 01:53:59 +00:00
|
|
|
|
2002-08-12 21:17:25 +00:00
|
|
|
return 0;
|
2002-08-09 23:47:40 +00:00
|
|
|
}
|
|
|
|
|
2006-11-02 01:53:59 +00:00
|
|
|
Instruction *InstCombiner::visitFRem(BinaryOperator &I) {
|
|
|
|
return commonRemTransforms(I);
|
|
|
|
}
|
|
|
|
|
2002-08-09 23:47:40 +00:00
|
|
|
// isMaxValueMinusOne - return true if this is Max-1
|
2006-12-23 06:05:41 +00:00
|
|
|
static bool isMaxValueMinusOne(const ConstantInt *C, bool isSigned) {
|
2007-03-19 21:10:28 +00:00
|
|
|
uint32_t TypeBits = C->getType()->getPrimitiveSizeInBits();
|
2007-07-15 20:42:37 +00:00
|
|
|
if (!isSigned)
|
|
|
|
return C->getValue() == APInt::getAllOnesValue(TypeBits) - 1;
|
|
|
|
return C->getValue() == APInt::getSignedMaxValue(TypeBits)-1;
|
2002-08-09 23:47:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// isMinValuePlusOne - return true if this is Min+1
|
2006-12-23 06:05:41 +00:00
|
|
|
static bool isMinValuePlusOne(const ConstantInt *C, bool isSigned) {
|
2007-07-15 20:42:37 +00:00
|
|
|
if (!isSigned)
|
|
|
|
return C->getValue() == 1; // unsigned
|
|
|
|
|
|
|
|
// Calculate 1111111111000000000000
|
|
|
|
uint32_t TypeBits = C->getType()->getPrimitiveSizeInBits();
|
|
|
|
return C->getValue() == APInt::getSignedMinValue(TypeBits)+1;
|
2002-05-06 16:14:14 +00:00
|
|
|
}
|
|
|
|
|
2004-06-09 07:59:58 +00:00
|
|
|
// isOneBitSet - Return true if there is exactly one bit set in the specified
|
|
|
|
// constant.
|
|
|
|
static bool isOneBitSet(const ConstantInt *CI) {
|
2007-03-20 00:16:52 +00:00
|
|
|
return CI->getValue().isPowerOf2();
|
2004-06-09 07:59:58 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 21:46:38 +00:00
|
|
|
// isHighOnes - Return true if the constant is of the form 1+0+.
|
|
|
|
// This is the same as lowones(~X).
|
|
|
|
static bool isHighOnes(const ConstantInt *CI) {
|
2007-03-20 12:49:06 +00:00
|
|
|
return (~CI->getValue() + 1).isPowerOf2();
|
2004-09-23 21:46:38 +00:00
|
|
|
}
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
/// getICmpCode - Encode a icmp predicate into a three bit mask. These bits
|
2003-08-13 20:16:26 +00:00
|
|
|
/// are carefully arranged to allow folding of expressions such as:
|
|
|
|
///
|
|
|
|
/// (A < B) | (A > B) --> (A != B)
|
|
|
|
///
|
2006-12-23 06:05:41 +00:00
|
|
|
/// Note that this is only valid if the first and second predicates have the
|
|
|
|
/// same sign. Is illegal to do: (A u< B) | (A s> B)
|
|
|
|
///
|
|
|
|
/// Three bits are used to represent the condition, as follows:
|
|
|
|
/// 0 A > B
|
|
|
|
/// 1 A == B
|
|
|
|
/// 2 A < B
|
2003-08-13 20:16:26 +00:00
|
|
|
///
|
2006-12-23 06:05:41 +00:00
|
|
|
/// <=> Value Definition
|
|
|
|
/// 000 0 Always false
|
|
|
|
/// 001 1 A > B
|
|
|
|
/// 010 2 A == B
|
|
|
|
/// 011 3 A >= B
|
|
|
|
/// 100 4 A < B
|
|
|
|
/// 101 5 A != B
|
|
|
|
/// 110 6 A <= B
|
|
|
|
/// 111 7 Always true
|
|
|
|
///
|
|
|
|
static unsigned getICmpCode(const ICmpInst *ICI) {
|
|
|
|
switch (ICI->getPredicate()) {
|
2003-08-13 20:16:26 +00:00
|
|
|
// False -> 0
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_UGT: return 1; // 001
|
|
|
|
case ICmpInst::ICMP_SGT: return 1; // 001
|
|
|
|
case ICmpInst::ICMP_EQ: return 2; // 010
|
|
|
|
case ICmpInst::ICMP_UGE: return 3; // 011
|
|
|
|
case ICmpInst::ICMP_SGE: return 3; // 011
|
|
|
|
case ICmpInst::ICMP_ULT: return 4; // 100
|
|
|
|
case ICmpInst::ICMP_SLT: return 4; // 100
|
|
|
|
case ICmpInst::ICMP_NE: return 5; // 101
|
|
|
|
case ICmpInst::ICMP_ULE: return 6; // 110
|
|
|
|
case ICmpInst::ICMP_SLE: return 6; // 110
|
2003-08-13 20:16:26 +00:00
|
|
|
// True -> 7
|
|
|
|
default:
|
2006-12-23 06:05:41 +00:00
|
|
|
assert(0 && "Invalid ICmp predicate!");
|
2003-08-13 20:16:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
/// getICmpValue - This is the complement of getICmpCode, which turns an
|
|
|
|
/// opcode and two operands into either a constant true or false, or a brand
|
2007-09-17 17:31:57 +00:00
|
|
|
/// new ICmp instruction. The sign is passed in to determine which kind
|
2006-12-23 06:05:41 +00:00
|
|
|
/// of predicate to use in new icmp instructions.
|
|
|
|
static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS) {
|
|
|
|
switch (code) {
|
|
|
|
default: assert(0 && "Illegal ICmp code!");
|
2007-01-11 12:24:14 +00:00
|
|
|
case 0: return ConstantInt::getFalse();
|
2006-12-23 06:05:41 +00:00
|
|
|
case 1:
|
|
|
|
if (sign)
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS);
|
|
|
|
else
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS);
|
|
|
|
case 2: return new ICmpInst(ICmpInst::ICMP_EQ, LHS, RHS);
|
|
|
|
case 3:
|
|
|
|
if (sign)
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS);
|
|
|
|
else
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS);
|
|
|
|
case 4:
|
|
|
|
if (sign)
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS);
|
|
|
|
else
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS);
|
|
|
|
case 5: return new ICmpInst(ICmpInst::ICMP_NE, LHS, RHS);
|
|
|
|
case 6:
|
|
|
|
if (sign)
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS);
|
|
|
|
else
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS);
|
2007-01-11 12:24:14 +00:00
|
|
|
case 7: return ConstantInt::getTrue();
|
2003-08-13 20:16:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
static bool PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) {
|
|
|
|
return (ICmpInst::isSignedPredicate(p1) == ICmpInst::isSignedPredicate(p2)) ||
|
|
|
|
(ICmpInst::isSignedPredicate(p1) &&
|
|
|
|
(p2 == ICmpInst::ICMP_EQ || p2 == ICmpInst::ICMP_NE)) ||
|
|
|
|
(ICmpInst::isSignedPredicate(p2) &&
|
|
|
|
(p1 == ICmpInst::ICMP_EQ || p1 == ICmpInst::ICMP_NE));
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
// FoldICmpLogical - Implements (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
|
|
|
|
struct FoldICmpLogical {
|
2003-08-13 20:16:26 +00:00
|
|
|
InstCombiner &IC;
|
|
|
|
Value *LHS, *RHS;
|
2006-12-23 06:05:41 +00:00
|
|
|
ICmpInst::Predicate pred;
|
|
|
|
FoldICmpLogical(InstCombiner &ic, ICmpInst *ICI)
|
|
|
|
: IC(ic), LHS(ICI->getOperand(0)), RHS(ICI->getOperand(1)),
|
|
|
|
pred(ICI->getPredicate()) {}
|
2003-08-13 20:16:26 +00:00
|
|
|
bool shouldApply(Value *V) const {
|
2006-12-23 06:05:41 +00:00
|
|
|
if (ICmpInst *ICI = dyn_cast<ICmpInst>(V))
|
|
|
|
if (PredicatesFoldable(pred, ICI->getPredicate()))
|
2008-02-20 11:26:25 +00:00
|
|
|
return ((ICI->getOperand(0) == LHS && ICI->getOperand(1) == RHS) ||
|
|
|
|
(ICI->getOperand(0) == RHS && ICI->getOperand(1) == LHS));
|
2003-08-13 20:16:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
2006-12-23 06:05:41 +00:00
|
|
|
Instruction *apply(Instruction &Log) const {
|
|
|
|
ICmpInst *ICI = cast<ICmpInst>(Log.getOperand(0));
|
|
|
|
if (ICI->getOperand(0) != LHS) {
|
|
|
|
assert(ICI->getOperand(1) == LHS);
|
|
|
|
ICI->swapOperands(); // Swap the LHS and RHS of the ICmp
|
2003-08-13 20:16:26 +00:00
|
|
|
}
|
|
|
|
|
2007-03-13 14:27:42 +00:00
|
|
|
ICmpInst *RHSICI = cast<ICmpInst>(Log.getOperand(1));
|
2006-12-23 06:05:41 +00:00
|
|
|
unsigned LHSCode = getICmpCode(ICI);
|
2007-03-13 14:27:42 +00:00
|
|
|
unsigned RHSCode = getICmpCode(RHSICI);
|
2003-08-13 20:16:26 +00:00
|
|
|
unsigned Code;
|
|
|
|
switch (Log.getOpcode()) {
|
|
|
|
case Instruction::And: Code = LHSCode & RHSCode; break;
|
|
|
|
case Instruction::Or: Code = LHSCode | RHSCode; break;
|
|
|
|
case Instruction::Xor: Code = LHSCode ^ RHSCode; break;
|
2003-09-22 20:33:34 +00:00
|
|
|
default: assert(0 && "Illegal logical opcode!"); return 0;
|
2003-08-13 20:16:26 +00:00
|
|
|
}
|
|
|
|
|
2007-03-13 14:27:42 +00:00
|
|
|
bool isSigned = ICmpInst::isSignedPredicate(RHSICI->getPredicate()) ||
|
|
|
|
ICmpInst::isSignedPredicate(ICI->getPredicate());
|
|
|
|
|
|
|
|
Value *RV = getICmpValue(isSigned, Code, LHS, RHS);
|
2003-08-13 20:16:26 +00:00
|
|
|
if (Instruction *I = dyn_cast<Instruction>(RV))
|
|
|
|
return I;
|
|
|
|
// Otherwise, it's a constant boolean value...
|
|
|
|
return IC.ReplaceInstUsesWith(Log, RV);
|
|
|
|
}
|
|
|
|
};
|
2006-11-15 04:53:24 +00:00
|
|
|
} // end anonymous namespace
|
2003-08-13 20:16:26 +00:00
|
|
|
|
2003-09-19 17:17:26 +00:00
|
|
|
// OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where
|
|
|
|
// the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'. Op is
|
2007-02-02 02:16:23 +00:00
|
|
|
// guaranteed to be a binary operator.
|
2003-09-19 17:17:26 +00:00
|
|
|
Instruction *InstCombiner::OptAndOp(Instruction *Op,
|
2007-01-11 12:24:14 +00:00
|
|
|
ConstantInt *OpRHS,
|
|
|
|
ConstantInt *AndRHS,
|
2003-09-19 17:17:26 +00:00
|
|
|
BinaryOperator &TheAnd) {
|
|
|
|
Value *X = Op->getOperand(0);
|
2004-01-12 19:47:05 +00:00
|
|
|
Constant *Together = 0;
|
2007-02-02 02:16:23 +00:00
|
|
|
if (!Op->isShift())
|
2007-03-25 05:33:51 +00:00
|
|
|
Together = And(AndRHS, OpRHS);
|
2004-01-12 19:35:11 +00:00
|
|
|
|
2003-09-19 17:17:26 +00:00
|
|
|
switch (Op->getOpcode()) {
|
|
|
|
case Instruction::Xor:
|
2005-01-01 16:22:27 +00:00
|
|
|
if (Op->hasOneUse()) {
|
2003-09-19 17:17:26 +00:00
|
|
|
// (X ^ C1) & C2 --> (X & C2) ^ (C1&C2)
|
2007-02-11 01:23:03 +00:00
|
|
|
Instruction *And = BinaryOperator::createAnd(X, AndRHS);
|
2003-09-19 17:17:26 +00:00
|
|
|
InsertNewInstBefore(And, TheAnd);
|
2007-02-11 01:23:03 +00:00
|
|
|
And->takeName(Op);
|
2004-06-10 02:07:29 +00:00
|
|
|
return BinaryOperator::createXor(And, Together);
|
2003-09-19 17:17:26 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Instruction::Or:
|
2005-01-01 16:22:27 +00:00
|
|
|
if (Together == AndRHS) // (X | C) & C --> C
|
|
|
|
return ReplaceInstUsesWith(TheAnd, AndRHS);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2005-01-01 16:22:27 +00:00
|
|
|
if (Op->hasOneUse() && Together != OpRHS) {
|
|
|
|
// (X | C1) & C2 --> (X | (C1&C2)) & C2
|
2007-02-11 01:23:03 +00:00
|
|
|
Instruction *Or = BinaryOperator::createOr(X, Together);
|
2005-01-01 16:22:27 +00:00
|
|
|
InsertNewInstBefore(Or, TheAnd);
|
2007-02-11 01:23:03 +00:00
|
|
|
Or->takeName(Op);
|
2005-01-01 16:22:27 +00:00
|
|
|
return BinaryOperator::createAnd(Or, AndRHS);
|
2003-09-19 17:17:26 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Instruction::Add:
|
2003-10-15 16:48:29 +00:00
|
|
|
if (Op->hasOneUse()) {
|
2003-09-19 17:17:26 +00:00
|
|
|
// Adding a one to a single bit bit-field should be turned into an XOR
|
|
|
|
// of the bit. First thing to check is to see if this AND is with a
|
|
|
|
// single bit constant.
|
2007-04-01 17:13:37 +00:00
|
|
|
const APInt& AndRHSV = cast<ConstantInt>(AndRHS)->getValue();
|
2003-09-19 17:17:26 +00:00
|
|
|
|
|
|
|
// If there is only one bit set...
|
2004-06-09 07:59:58 +00:00
|
|
|
if (isOneBitSet(cast<ConstantInt>(AndRHS))) {
|
2003-09-19 17:17:26 +00:00
|
|
|
// Ok, at this point, we know that we are masking the result of the
|
|
|
|
// ADD down to exactly one bit. If the constant we are adding has
|
|
|
|
// no bits set below this bit, then we can eliminate the ADD.
|
2007-04-01 17:13:37 +00:00
|
|
|
const APInt& AddRHS = cast<ConstantInt>(OpRHS)->getValue();
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2003-09-19 17:17:26 +00:00
|
|
|
// Check to see if any bits below the one bit set in AndRHSV are set.
|
|
|
|
if ((AddRHS & (AndRHSV-1)) == 0) {
|
|
|
|
// If not, the only thing that can effect the output of the AND is
|
|
|
|
// the bit specified by AndRHSV. If that bit is set, the effect of
|
|
|
|
// the XOR is to toggle the bit. If it is clear, then the ADD has
|
|
|
|
// no effect.
|
|
|
|
if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop
|
|
|
|
TheAnd.setOperand(0, X);
|
|
|
|
return &TheAnd;
|
|
|
|
} else {
|
|
|
|
// Pull the XOR out of the AND.
|
2007-02-11 01:23:03 +00:00
|
|
|
Instruction *NewAnd = BinaryOperator::createAnd(X, AndRHS);
|
2003-09-19 17:17:26 +00:00
|
|
|
InsertNewInstBefore(NewAnd, TheAnd);
|
2007-02-11 01:23:03 +00:00
|
|
|
NewAnd->takeName(Op);
|
2004-06-10 02:07:29 +00:00
|
|
|
return BinaryOperator::createXor(NewAnd, AndRHS);
|
2003-09-19 17:17:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2003-09-19 19:05:02 +00:00
|
|
|
|
|
|
|
case Instruction::Shl: {
|
|
|
|
// We know that the AND will not produce any of the bits shifted in, so if
|
|
|
|
// the anded constant includes them, clear them now!
|
|
|
|
//
|
2007-03-29 08:15:12 +00:00
|
|
|
uint32_t BitWidth = AndRHS->getType()->getBitWidth();
|
2007-03-30 09:29:48 +00:00
|
|
|
uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
|
2007-03-29 08:15:12 +00:00
|
|
|
APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal));
|
|
|
|
ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShlMask);
|
|
|
|
|
|
|
|
if (CI->getValue() == ShlMask) {
|
|
|
|
// Masking out bits that the shift already masks
|
2004-09-24 15:21:34 +00:00
|
|
|
return ReplaceInstUsesWith(TheAnd, Op); // No need for the and.
|
|
|
|
} else if (CI != AndRHS) { // Reducing bits set in and.
|
2003-09-19 19:05:02 +00:00
|
|
|
TheAnd.setOperand(1, CI);
|
|
|
|
return &TheAnd;
|
|
|
|
}
|
|
|
|
break;
|
2005-04-21 23:48:37 +00:00
|
|
|
}
|
2006-11-08 06:47:33 +00:00
|
|
|
case Instruction::LShr:
|
|
|
|
{
|
2003-09-19 19:05:02 +00:00
|
|
|
// We know that the AND will not produce any of the bits shifted in, so if
|
|
|
|
// the anded constant includes them, clear them now! This only applies to
|
|
|
|
// unsigned shifts, because a signed shr may bring in set bits!
|
|
|
|
//
|
2007-03-29 08:15:12 +00:00
|
|
|
uint32_t BitWidth = AndRHS->getType()->getBitWidth();
|
2007-03-30 09:29:48 +00:00
|
|
|
uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
|
2007-03-29 08:15:12 +00:00
|
|
|
APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
|
|
|
|
ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShrMask);
|
|
|
|
|
|
|
|
if (CI->getValue() == ShrMask) {
|
|
|
|
// Masking out bits that the shift already masks.
|
2006-11-08 06:47:33 +00:00
|
|
|
return ReplaceInstUsesWith(TheAnd, Op);
|
|
|
|
} else if (CI != AndRHS) {
|
|
|
|
TheAnd.setOperand(1, CI); // Reduce bits set in and cst.
|
|
|
|
return &TheAnd;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Instruction::AShr:
|
|
|
|
// Signed shr.
|
|
|
|
// See if this is shifting in some sign extension, then masking it out
|
|
|
|
// with an and.
|
|
|
|
if (Op->hasOneUse()) {
|
2007-03-29 08:15:12 +00:00
|
|
|
uint32_t BitWidth = AndRHS->getType()->getBitWidth();
|
2007-03-30 09:29:48 +00:00
|
|
|
uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
|
2007-03-29 08:15:12 +00:00
|
|
|
APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
|
|
|
|
Constant *C = ConstantInt::get(AndRHS->getValue() & ShrMask);
|
2006-12-13 17:19:09 +00:00
|
|
|
if (C == AndRHS) { // Masking out bits shifted in.
|
2006-12-12 09:18:51 +00:00
|
|
|
// (Val ashr C1) & C2 -> (Val lshr C1) & C2
|
2006-11-08 06:47:33 +00:00
|
|
|
// Make the argument unsigned.
|
|
|
|
Value *ShVal = Op->getOperand(0);
|
2007-02-02 02:16:23 +00:00
|
|
|
ShVal = InsertNewInstBefore(
|
2007-02-02 14:08:20 +00:00
|
|
|
BinaryOperator::createLShr(ShVal, OpRHS,
|
2007-02-02 02:16:23 +00:00
|
|
|
Op->getName()), TheAnd);
|
2006-12-13 17:19:09 +00:00
|
|
|
return BinaryOperator::createAnd(ShVal, AndRHS, TheAnd.getName());
|
2004-09-24 15:21:34 +00:00
|
|
|
}
|
2003-09-19 19:05:02 +00:00
|
|
|
}
|
|
|
|
break;
|
2003-09-19 17:17:26 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-05-06 16:14:14 +00:00
|
|
|
|
2004-09-29 17:40:11 +00:00
|
|
|
/// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is
|
|
|
|
/// true, otherwise (V < Lo || V >= Hi). In pratice, we emit the more efficient
|
2006-12-23 06:05:41 +00:00
|
|
|
/// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. isSigned indicates
|
|
|
|
/// whether to treat the V, Lo and HI as signed or not. IB is the location to
|
2004-09-29 17:40:11 +00:00
|
|
|
/// insert new instructions.
|
|
|
|
Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi,
|
2006-12-23 06:05:41 +00:00
|
|
|
bool isSigned, bool Inside,
|
|
|
|
Instruction &IB) {
|
2007-01-11 12:24:14 +00:00
|
|
|
assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ?
|
2007-01-12 04:24:46 +00:00
|
|
|
ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() &&
|
2004-09-29 17:40:11 +00:00
|
|
|
"Lo is not <= Hi in range emission code!");
|
2006-12-23 06:05:41 +00:00
|
|
|
|
2004-09-29 17:40:11 +00:00
|
|
|
if (Inside) {
|
|
|
|
if (Lo == Hi) // Trivially false.
|
2006-12-23 06:05:41 +00:00
|
|
|
return new ICmpInst(ICmpInst::ICMP_NE, V, V);
|
|
|
|
|
|
|
|
// V >= Min && V < Hi --> V < Hi
|
2007-01-11 12:24:14 +00:00
|
|
|
if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
|
2007-03-21 23:19:50 +00:00
|
|
|
ICmpInst::Predicate pred = (isSigned ?
|
2006-12-23 06:05:41 +00:00
|
|
|
ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT);
|
|
|
|
return new ICmpInst(pred, V, Hi);
|
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
// Emit V-Lo <u Hi-Lo
|
|
|
|
Constant *NegLo = ConstantExpr::getNeg(Lo);
|
|
|
|
Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off");
|
2004-09-29 17:40:11 +00:00
|
|
|
InsertNewInstBefore(Add, IB);
|
2006-12-23 06:05:41 +00:00
|
|
|
Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi);
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_ULT, Add, UpperBound);
|
2004-09-29 17:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Lo == Hi) // Trivially true.
|
2006-12-23 06:05:41 +00:00
|
|
|
return new ICmpInst(ICmpInst::ICMP_EQ, V, V);
|
2004-09-29 17:40:11 +00:00
|
|
|
|
2007-03-21 23:19:50 +00:00
|
|
|
// V < Min || V >= Hi -> V > Hi-1
|
2004-09-29 17:40:11 +00:00
|
|
|
Hi = SubOne(cast<ConstantInt>(Hi));
|
2007-01-11 12:24:14 +00:00
|
|
|
if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
|
2006-12-23 06:05:41 +00:00
|
|
|
ICmpInst::Predicate pred = (isSigned ?
|
|
|
|
ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT);
|
|
|
|
return new ICmpInst(pred, V, Hi);
|
|
|
|
}
|
2006-10-20 07:07:24 +00:00
|
|
|
|
2007-03-21 23:19:50 +00:00
|
|
|
// Emit V-Lo >u Hi-1-Lo
|
|
|
|
// Note that Hi has already had one subtracted from it, above.
|
|
|
|
ConstantInt *NegLo = cast<ConstantInt>(ConstantExpr::getNeg(Lo));
|
2006-12-23 06:05:41 +00:00
|
|
|
Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off");
|
2004-09-29 17:40:11 +00:00
|
|
|
InsertNewInstBefore(Add, IB);
|
2006-12-23 06:05:41 +00:00
|
|
|
Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi);
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_UGT, Add, LowerBound);
|
2004-09-29 17:40:11 +00:00
|
|
|
}
|
|
|
|
|
Refactor this code a bit and make it more general. This now compiles:
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) { b.j += x; }
To:
_plus2:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
slwi r3, r3, 6
add r3, r4, r3
rlwimi r3, r4, 0, 26, 14
stw r3, 0(r2)
blr
instead of:
_plus2:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
rlwinm r5, r4, 26, 21, 31
add r3, r5, r3
rlwimi r4, r3, 6, 15, 25
stw r4, 0(r2)
blr
by eliminating an 'and'.
I'm pretty sure this is as small as we can go :)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23386 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 07:22:02 +00:00
|
|
|
// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
|
|
|
|
// any number of 0s on either side. The 1s are allowed to wrap from LSB to
|
|
|
|
// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
|
|
|
|
// not, since all 1s are not contiguous.
|
2007-04-02 08:20:41 +00:00
|
|
|
static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME) {
|
2007-04-01 17:13:37 +00:00
|
|
|
const APInt& V = Val->getValue();
|
2007-03-24 00:42:08 +00:00
|
|
|
uint32_t BitWidth = Val->getType()->getBitWidth();
|
|
|
|
if (!APIntOps::isShiftedMask(BitWidth, V)) return false;
|
Refactor this code a bit and make it more general. This now compiles:
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) { b.j += x; }
To:
_plus2:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
slwi r3, r3, 6
add r3, r4, r3
rlwimi r3, r4, 0, 26, 14
stw r3, 0(r2)
blr
instead of:
_plus2:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
rlwinm r5, r4, 26, 21, 31
add r3, r5, r3
rlwimi r4, r3, 6, 15, 25
stw r4, 0(r2)
blr
by eliminating an 'and'.
I'm pretty sure this is as small as we can go :)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23386 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 07:22:02 +00:00
|
|
|
|
|
|
|
// look for the first zero bit after the run of ones
|
2007-03-24 00:42:08 +00:00
|
|
|
MB = BitWidth - ((V - 1) ^ V).countLeadingZeros();
|
Refactor this code a bit and make it more general. This now compiles:
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) { b.j += x; }
To:
_plus2:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
slwi r3, r3, 6
add r3, r4, r3
rlwimi r3, r4, 0, 26, 14
stw r3, 0(r2)
blr
instead of:
_plus2:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
rlwinm r5, r4, 26, 21, 31
add r3, r5, r3
rlwimi r4, r3, 6, 15, 25
stw r4, 0(r2)
blr
by eliminating an 'and'.
I'm pretty sure this is as small as we can go :)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23386 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 07:22:02 +00:00
|
|
|
// look for the first non-zero bit
|
2007-03-24 00:42:08 +00:00
|
|
|
ME = V.getActiveBits();
|
Refactor this code a bit and make it more general. This now compiles:
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) { b.j += x; }
To:
_plus2:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
slwi r3, r3, 6
add r3, r4, r3
rlwimi r3, r4, 0, 26, 14
stw r3, 0(r2)
blr
instead of:
_plus2:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
rlwinm r5, r4, 26, 21, 31
add r3, r5, r3
rlwimi r4, r3, 6, 15, 25
stw r4, 0(r2)
blr
by eliminating an 'and'.
I'm pretty sure this is as small as we can go :)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23386 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 07:22:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask,
|
|
|
|
/// where isSub determines whether the operator is a sub. If we can fold one of
|
|
|
|
/// the following xforms:
|
Implement add.ll:test29. Codegening:
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus1 (unsigned int x) {
b.i += x;
}
as:
_plus1:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
add r3, r4, r3
rlwimi r3, r4, 0, 0, 25
stw r3, 0(r2)
blr
instead of:
_plus1:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
rlwinm r5, r4, 0, 26, 31
add r3, r5, r3
rlwimi r3, r4, 0, 0, 25
stw r3, 0(r2)
blr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23379 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 04:24:45 +00:00
|
|
|
///
|
|
|
|
/// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask
|
|
|
|
/// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0
|
|
|
|
/// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0
|
|
|
|
///
|
|
|
|
/// return (A +/- B).
|
|
|
|
///
|
|
|
|
Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS,
|
2007-01-11 12:24:14 +00:00
|
|
|
ConstantInt *Mask, bool isSub,
|
Implement add.ll:test29. Codegening:
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus1 (unsigned int x) {
b.i += x;
}
as:
_plus1:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
add r3, r4, r3
rlwimi r3, r4, 0, 0, 25
stw r3, 0(r2)
blr
instead of:
_plus1:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
rlwinm r5, r4, 0, 26, 31
add r3, r5, r3
rlwimi r3, r4, 0, 0, 25
stw r3, 0(r2)
blr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23379 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 04:24:45 +00:00
|
|
|
Instruction &I) {
|
|
|
|
Instruction *LHSI = dyn_cast<Instruction>(LHS);
|
|
|
|
if (!LHSI || LHSI->getNumOperands() != 2 ||
|
|
|
|
!isa<ConstantInt>(LHSI->getOperand(1))) return 0;
|
|
|
|
|
|
|
|
ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1));
|
|
|
|
|
|
|
|
switch (LHSI->getOpcode()) {
|
|
|
|
default: return 0;
|
|
|
|
case Instruction::And:
|
2007-03-25 05:33:51 +00:00
|
|
|
if (And(N, Mask) == Mask) {
|
Refactor this code a bit and make it more general. This now compiles:
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) { b.j += x; }
To:
_plus2:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
slwi r3, r3, 6
add r3, r4, r3
rlwimi r3, r4, 0, 26, 14
stw r3, 0(r2)
blr
instead of:
_plus2:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
rlwinm r5, r4, 26, 21, 31
add r3, r5, r3
rlwimi r4, r3, 6, 15, 25
stw r4, 0(r2)
blr
by eliminating an 'and'.
I'm pretty sure this is as small as we can go :)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23386 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 07:22:02 +00:00
|
|
|
// If the AndRHS is a power of two minus one (0+1+), this is simple.
|
2007-03-24 15:34:37 +00:00
|
|
|
if ((Mask->getValue().countLeadingZeros() +
|
|
|
|
Mask->getValue().countPopulation()) ==
|
|
|
|
Mask->getValue().getBitWidth())
|
Refactor this code a bit and make it more general. This now compiles:
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) { b.j += x; }
To:
_plus2:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
slwi r3, r3, 6
add r3, r4, r3
rlwimi r3, r4, 0, 26, 14
stw r3, 0(r2)
blr
instead of:
_plus2:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
rlwinm r5, r4, 26, 21, 31
add r3, r5, r3
rlwimi r4, r3, 6, 15, 25
stw r4, 0(r2)
blr
by eliminating an 'and'.
I'm pretty sure this is as small as we can go :)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23386 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 07:22:02 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+
|
|
|
|
// part, we don't need any explicit masks to take them out of A. If that
|
|
|
|
// is all N is, ignore it.
|
2007-04-02 08:20:41 +00:00
|
|
|
uint32_t MB = 0, ME = 0;
|
Refactor this code a bit and make it more general. This now compiles:
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) { b.j += x; }
To:
_plus2:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
slwi r3, r3, 6
add r3, r4, r3
rlwimi r3, r4, 0, 26, 14
stw r3, 0(r2)
blr
instead of:
_plus2:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
rlwinm r5, r4, 26, 21, 31
add r3, r5, r3
rlwimi r4, r3, 6, 15, 25
stw r4, 0(r2)
blr
by eliminating an 'and'.
I'm pretty sure this is as small as we can go :)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23386 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 07:22:02 +00:00
|
|
|
if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive
|
2007-03-23 18:46:34 +00:00
|
|
|
uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth();
|
2007-03-29 08:15:12 +00:00
|
|
|
APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1));
|
2006-02-07 07:27:52 +00:00
|
|
|
if (MaskedValueIsZero(RHS, Mask))
|
Refactor this code a bit and make it more general. This now compiles:
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) { b.j += x; }
To:
_plus2:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
slwi r3, r3, 6
add r3, r4, r3
rlwimi r3, r4, 0, 26, 14
stw r3, 0(r2)
blr
instead of:
_plus2:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
rlwinm r5, r4, 26, 21, 31
add r3, r5, r3
rlwimi r4, r3, 6, 15, 25
stw r4, 0(r2)
blr
by eliminating an 'and'.
I'm pretty sure this is as small as we can go :)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23386 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 07:22:02 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
Implement add.ll:test29. Codegening:
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus1 (unsigned int x) {
b.i += x;
}
as:
_plus1:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
add r3, r4, r3
rlwimi r3, r4, 0, 0, 25
stw r3, 0(r2)
blr
instead of:
_plus1:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
rlwinm r5, r4, 0, 26, 31
add r3, r5, r3
rlwimi r3, r4, 0, 0, 25
stw r3, 0(r2)
blr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23379 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 04:24:45 +00:00
|
|
|
return 0;
|
|
|
|
case Instruction::Or:
|
|
|
|
case Instruction::Xor:
|
Refactor this code a bit and make it more general. This now compiles:
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) { b.j += x; }
To:
_plus2:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
slwi r3, r3, 6
add r3, r4, r3
rlwimi r3, r4, 0, 26, 14
stw r3, 0(r2)
blr
instead of:
_plus2:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
rlwinm r5, r4, 26, 21, 31
add r3, r5, r3
rlwimi r4, r3, 6, 15, 25
stw r4, 0(r2)
blr
by eliminating an 'and'.
I'm pretty sure this is as small as we can go :)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23386 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 07:22:02 +00:00
|
|
|
// If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0
|
2007-03-24 15:34:37 +00:00
|
|
|
if ((Mask->getValue().countLeadingZeros() +
|
|
|
|
Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth()
|
2007-03-26 23:58:26 +00:00
|
|
|
&& And(N, Mask)->isZero())
|
Implement add.ll:test29. Codegening:
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus1 (unsigned int x) {
b.i += x;
}
as:
_plus1:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
add r3, r4, r3
rlwimi r3, r4, 0, 0, 25
stw r3, 0(r2)
blr
instead of:
_plus1:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
rlwinm r5, r4, 0, 26, 31
add r3, r5, r3
rlwimi r3, r4, 0, 0, 25
stw r3, 0(r2)
blr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23379 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 04:24:45 +00:00
|
|
|
break;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *New;
|
|
|
|
if (isSub)
|
|
|
|
New = BinaryOperator::createSub(LHSI->getOperand(0), RHS, "fold");
|
|
|
|
else
|
|
|
|
New = BinaryOperator::createAdd(LHSI->getOperand(0), RHS, "fold");
|
|
|
|
return InsertNewInstBefore(New, I);
|
|
|
|
}
|
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
|
2003-03-10 21:43:22 +00:00
|
|
|
bool Changed = SimplifyCommutative(I);
|
2002-06-25 16:13:24 +00:00
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
2002-05-06 16:14:14 +00:00
|
|
|
|
2004-10-16 18:11:37 +00:00
|
|
|
if (isa<UndefValue>(Op1)) // X & undef -> 0
|
|
|
|
return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
|
|
|
|
|
2005-01-01 16:22:27 +00:00
|
|
|
// and X, X = X
|
|
|
|
if (Op0 == Op1)
|
2002-08-12 21:17:25 +00:00
|
|
|
return ReplaceInstUsesWith(I, Op1);
|
2002-05-06 16:14:14 +00:00
|
|
|
|
2006-02-12 08:02:11 +00:00
|
|
|
// See if we can simplify any instructions used by the instruction whose sole
|
Use EraseInstFromFunction in a few cases to put the uses of the removed
instruction onto the worklist (in case they are now dead).
Add a really trivial local DSE implementation to help out bitfield code.
We now fold this:
struct S {
unsigned char a : 1, b : 1, c : 1, d : 2, e : 3;
S();
};
S::S() : a(0), b(0), c(1), d(0), e(6) {}
to this:
void %_ZN1SC1Ev(%struct.S* %this) {
entry:
%tmp.1 = getelementptr %struct.S* %this, int 0, uint 0
store ubyte 38, ubyte* %tmp.1
ret void
}
much earlier (in gccas instead of only in gccld after DSE runs).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@26050 91177308-0d34-0410-b5e6-96231b3b80d8
2006-02-08 03:25:32 +00:00
|
|
|
// purpose is to compute bits we don't care about.
|
2007-02-15 02:26:10 +00:00
|
|
|
if (!isa<VectorType>(I.getType())) {
|
2007-03-22 22:19:58 +00:00
|
|
|
uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth();
|
|
|
|
APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
|
|
|
|
if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth),
|
2007-01-18 22:16:33 +00:00
|
|
|
KnownZero, KnownOne))
|
2007-03-26 23:58:26 +00:00
|
|
|
return &I;
|
2007-01-18 22:16:33 +00:00
|
|
|
} else {
|
2007-02-15 02:26:10 +00:00
|
|
|
if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) {
|
2007-06-15 05:26:55 +00:00
|
|
|
if (CP->isAllOnesValue()) // X & <-1,-1> -> X
|
2007-01-18 22:16:33 +00:00
|
|
|
return ReplaceInstUsesWith(I, I.getOperand(0));
|
2007-06-15 05:26:55 +00:00
|
|
|
} else if (isa<ConstantAggregateZero>(Op1)) {
|
|
|
|
return ReplaceInstUsesWith(I, Op1); // X & <0,0> -> <0,0>
|
2007-01-18 22:16:33 +00:00
|
|
|
}
|
|
|
|
}
|
Use EraseInstFromFunction in a few cases to put the uses of the removed
instruction onto the worklist (in case they are now dead).
Add a really trivial local DSE implementation to help out bitfield code.
We now fold this:
struct S {
unsigned char a : 1, b : 1, c : 1, d : 2, e : 3;
S();
};
S::S() : a(0), b(0), c(1), d(0), e(6) {}
to this:
void %_ZN1SC1Ev(%struct.S* %this) {
entry:
%tmp.1 = getelementptr %struct.S* %this, int 0, uint 0
store ubyte 38, ubyte* %tmp.1
ret void
}
much earlier (in gccas instead of only in gccld after DSE runs).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@26050 91177308-0d34-0410-b5e6-96231b3b80d8
2006-02-08 03:25:32 +00:00
|
|
|
|
2007-01-11 12:24:14 +00:00
|
|
|
if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) {
|
2007-04-01 17:13:37 +00:00
|
|
|
const APInt& AndRHSMask = AndRHS->getValue();
|
|
|
|
APInt NotAndRHS(~AndRHSMask);
|
2005-01-01 16:22:27 +00:00
|
|
|
|
2003-09-19 17:17:26 +00:00
|
|
|
// Optimize a variety of ((val OP C1) & C2) combinations...
|
2007-02-02 02:16:23 +00:00
|
|
|
if (isa<BinaryOperator>(Op0)) {
|
2003-09-19 17:17:26 +00:00
|
|
|
Instruction *Op0I = cast<Instruction>(Op0);
|
2005-01-01 16:22:27 +00:00
|
|
|
Value *Op0LHS = Op0I->getOperand(0);
|
|
|
|
Value *Op0RHS = Op0I->getOperand(1);
|
|
|
|
switch (Op0I->getOpcode()) {
|
|
|
|
case Instruction::Xor:
|
|
|
|
case Instruction::Or:
|
2005-01-23 20:26:55 +00:00
|
|
|
// If the mask is only needed on one incoming arm, push it up.
|
|
|
|
if (Op0I->hasOneUse()) {
|
|
|
|
if (MaskedValueIsZero(Op0LHS, NotAndRHS)) {
|
|
|
|
// Not masking anything out for the LHS, move to RHS.
|
|
|
|
Instruction *NewRHS = BinaryOperator::createAnd(Op0RHS, AndRHS,
|
|
|
|
Op0RHS->getName()+".masked");
|
|
|
|
InsertNewInstBefore(NewRHS, I);
|
|
|
|
return BinaryOperator::create(
|
|
|
|
cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS);
|
2005-04-21 23:48:37 +00:00
|
|
|
}
|
2006-02-07 07:27:52 +00:00
|
|
|
if (!isa<Constant>(Op0RHS) &&
|
2005-01-23 20:26:55 +00:00
|
|
|
MaskedValueIsZero(Op0RHS, NotAndRHS)) {
|
|
|
|
// Not masking anything out for the RHS, move to LHS.
|
|
|
|
Instruction *NewLHS = BinaryOperator::createAnd(Op0LHS, AndRHS,
|
|
|
|
Op0LHS->getName()+".masked");
|
|
|
|
InsertNewInstBefore(NewLHS, I);
|
|
|
|
return BinaryOperator::create(
|
|
|
|
cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-01 16:22:27 +00:00
|
|
|
break;
|
Implement add.ll:test29. Codegening:
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus1 (unsigned int x) {
b.i += x;
}
as:
_plus1:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
add r3, r4, r3
rlwimi r3, r4, 0, 0, 25
stw r3, 0(r2)
blr
instead of:
_plus1:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
rlwinm r5, r4, 0, 26, 31
add r3, r5, r3
rlwimi r3, r4, 0, 0, 25
stw r3, 0(r2)
blr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23379 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 04:24:45 +00:00
|
|
|
case Instruction::Add:
|
Refactor this code a bit and make it more general. This now compiles:
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) { b.j += x; }
To:
_plus2:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
slwi r3, r3, 6
add r3, r4, r3
rlwimi r3, r4, 0, 26, 14
stw r3, 0(r2)
blr
instead of:
_plus2:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
rlwinm r5, r4, 26, 21, 31
add r3, r5, r3
rlwimi r4, r3, 6, 15, 25
stw r4, 0(r2)
blr
by eliminating an 'and'.
I'm pretty sure this is as small as we can go :)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23386 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 07:22:02 +00:00
|
|
|
// ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS.
|
|
|
|
// ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
|
|
|
|
// ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
|
|
|
|
if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I))
|
|
|
|
return BinaryOperator::createAnd(V, AndRHS);
|
|
|
|
if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I))
|
|
|
|
return BinaryOperator::createAnd(V, AndRHS); // Add commutes
|
Implement add.ll:test29. Codegening:
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus1 (unsigned int x) {
b.i += x;
}
as:
_plus1:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
add r3, r4, r3
rlwimi r3, r4, 0, 0, 25
stw r3, 0(r2)
blr
instead of:
_plus1:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
rlwinm r5, r4, 0, 26, 31
add r3, r5, r3
rlwimi r3, r4, 0, 0, 25
stw r3, 0(r2)
blr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23379 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 04:24:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Instruction::Sub:
|
Refactor this code a bit and make it more general. This now compiles:
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) { b.j += x; }
To:
_plus2:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
slwi r3, r3, 6
add r3, r4, r3
rlwimi r3, r4, 0, 26, 14
stw r3, 0(r2)
blr
instead of:
_plus2:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
rlwinm r5, r4, 26, 21, 31
add r3, r5, r3
rlwimi r4, r3, 6, 15, 25
stw r4, 0(r2)
blr
by eliminating an 'and'.
I'm pretty sure this is as small as we can go :)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23386 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 07:22:02 +00:00
|
|
|
// ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS.
|
|
|
|
// ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
|
|
|
|
// ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
|
|
|
|
if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I))
|
|
|
|
return BinaryOperator::createAnd(V, AndRHS);
|
Implement add.ll:test29. Codegening:
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus1 (unsigned int x) {
b.i += x;
}
as:
_plus1:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
add r3, r4, r3
rlwimi r3, r4, 0, 0, 25
stw r3, 0(r2)
blr
instead of:
_plus1:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
rlwinm r5, r4, 0, 26, 31
add r3, r5, r3
rlwimi r3, r4, 0, 0, 25
stw r3, 0(r2)
blr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23379 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 04:24:45 +00:00
|
|
|
break;
|
2005-01-01 16:22:27 +00:00
|
|
|
}
|
|
|
|
|
2003-07-23 19:25:52 +00:00
|
|
|
if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1)))
|
2005-01-01 16:22:27 +00:00
|
|
|
if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I))
|
2003-09-19 17:17:26 +00:00
|
|
|
return Res;
|
2005-01-01 16:22:27 +00:00
|
|
|
} else if (CastInst *CI = dyn_cast<CastInst>(Op0)) {
|
2005-08-07 07:03:10 +00:00
|
|
|
// If this is an integer truncation or change from signed-to-unsigned, and
|
|
|
|
// if the source is an and/or with immediate, transform it. This
|
|
|
|
// frequently occurs for bitfield accesses.
|
|
|
|
if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) {
|
2006-11-27 01:05:10 +00:00
|
|
|
if ((isa<TruncInst>(CI) || isa<BitCastInst>(CI)) &&
|
2005-08-07 07:03:10 +00:00
|
|
|
CastOp->getNumOperands() == 2)
|
2008-02-20 11:26:25 +00:00
|
|
|
if (ConstantInt *AndCI = dyn_cast<ConstantInt>(CastOp->getOperand(1))) {
|
2005-08-07 07:03:10 +00:00
|
|
|
if (CastOp->getOpcode() == Instruction::And) {
|
|
|
|
// Change: and (cast (and X, C1) to T), C2
|
2006-11-27 01:05:10 +00:00
|
|
|
// into : and (cast X to T), trunc_or_bitcast(C1)&C2
|
|
|
|
// This will fold the two constants together, which may allow
|
|
|
|
// other simplifications.
|
2006-12-12 23:36:14 +00:00
|
|
|
Instruction *NewCast = CastInst::createTruncOrBitCast(
|
|
|
|
CastOp->getOperand(0), I.getType(),
|
|
|
|
CastOp->getName()+".shrunk");
|
2005-08-07 07:03:10 +00:00
|
|
|
NewCast = InsertNewInstBefore(NewCast, I);
|
2006-11-27 01:05:10 +00:00
|
|
|
// trunc_or_bitcast(C1)&C2
|
2006-12-12 23:36:14 +00:00
|
|
|
Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType());
|
2006-11-27 01:05:10 +00:00
|
|
|
C3 = ConstantExpr::getAnd(C3, AndRHS);
|
2005-08-07 07:03:10 +00:00
|
|
|
return BinaryOperator::createAnd(NewCast, C3);
|
|
|
|
} else if (CastOp->getOpcode() == Instruction::Or) {
|
|
|
|
// Change: and (cast (or X, C1) to T), C2
|
|
|
|
// into : trunc(C1)&C2 iff trunc(C1)&C2 == C2
|
2006-12-12 19:11:20 +00:00
|
|
|
Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType());
|
2005-08-07 07:03:10 +00:00
|
|
|
if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS) // trunc(C1)&C2
|
|
|
|
return ReplaceInstUsesWith(I, AndRHS);
|
|
|
|
}
|
2008-02-20 11:26:25 +00:00
|
|
|
}
|
2005-08-07 07:03:10 +00:00
|
|
|
}
|
2003-07-23 19:36:21 +00:00
|
|
|
}
|
2004-04-09 19:05:30 +00:00
|
|
|
|
|
|
|
// Try to fold constant and into select arguments.
|
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
|
2005-01-01 16:22:27 +00:00
|
|
|
if (Instruction *R = FoldOpIntoSelect(I, SI, this))
|
2004-04-09 19:05:30 +00:00
|
|
|
return R;
|
Fold binary expressions and casts into PHI nodes that have all constant inputs.
This takes something like this:
%A = phi int [ 3, %cond_false.0 ], [ 2, %endif.0.i ], [ 2, %endif.1.i ]
%B = div int %tmp.243, 4
and turns it into:
%A = phi int [ 3/4, %cond_false.0 ], [ 2/4, %endif.0.i ], [ 2/4, %endif.1.i ]
which is later simplified (in this case) into %A = 0.
This triggers thousands of times in spec, for example, 269 times in 176.gcc.
This is tested by InstCombine/add.ll:test23 and set.ll:test18.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16582 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-29 05:07:12 +00:00
|
|
|
if (isa<PHINode>(Op0))
|
|
|
|
if (Instruction *NV = FoldOpIntoPhi(I))
|
|
|
|
return NV;
|
2003-07-23 17:57:01 +00:00
|
|
|
}
|
|
|
|
|
2003-03-10 23:06:50 +00:00
|
|
|
Value *Op0NotVal = dyn_castNotVal(Op0);
|
|
|
|
Value *Op1NotVal = dyn_castNotVal(Op1);
|
2003-02-18 19:28:33 +00:00
|
|
|
|
2004-06-18 06:07:51 +00:00
|
|
|
if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0
|
|
|
|
return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
|
|
|
|
|
2004-07-30 12:50:08 +00:00
|
|
|
// (~A & ~B) == (~(A | B)) - De Morgan's Law
|
2003-03-10 23:06:50 +00:00
|
|
|
if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) {
|
2004-06-10 02:07:29 +00:00
|
|
|
Instruction *Or = BinaryOperator::createOr(Op0NotVal, Op1NotVal,
|
|
|
|
I.getName()+".demorgan");
|
2003-07-23 17:57:01 +00:00
|
|
|
InsertNewInstBefore(Or, I);
|
2003-02-18 19:28:33 +00:00
|
|
|
return BinaryOperator::createNot(Or);
|
|
|
|
}
|
2006-02-13 23:07:23 +00:00
|
|
|
|
|
|
|
{
|
2007-06-15 05:58:24 +00:00
|
|
|
Value *A = 0, *B = 0, *C = 0, *D = 0;
|
|
|
|
if (match(Op0, m_Or(m_Value(A), m_Value(B)))) {
|
2006-02-13 23:07:23 +00:00
|
|
|
if (A == Op1 || B == Op1) // (A | ?) & A --> A
|
|
|
|
return ReplaceInstUsesWith(I, Op1);
|
2007-06-15 05:58:24 +00:00
|
|
|
|
|
|
|
// (A|B) & ~(A&B) -> A^B
|
|
|
|
if (match(Op1, m_Not(m_And(m_Value(C), m_Value(D))))) {
|
|
|
|
if ((A == C && B == D) || (A == D && B == C))
|
|
|
|
return BinaryOperator::createXor(A, B);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match(Op1, m_Or(m_Value(A), m_Value(B)))) {
|
2006-02-13 23:07:23 +00:00
|
|
|
if (A == Op0 || B == Op0) // A & (A | ?) --> A
|
|
|
|
return ReplaceInstUsesWith(I, Op0);
|
2007-06-15 05:58:24 +00:00
|
|
|
|
|
|
|
// ~(A&B) & (A|B) -> A^B
|
|
|
|
if (match(Op0, m_Not(m_And(m_Value(C), m_Value(D))))) {
|
|
|
|
if ((A == C && B == D) || (A == D && B == C))
|
|
|
|
return BinaryOperator::createXor(A, B);
|
|
|
|
}
|
|
|
|
}
|
2006-04-01 08:03:55 +00:00
|
|
|
|
|
|
|
if (Op0->hasOneUse() &&
|
|
|
|
match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
|
|
|
|
if (A == Op1) { // (A^B)&A -> A&(A^B)
|
|
|
|
I.swapOperands(); // Simplify below
|
|
|
|
std::swap(Op0, Op1);
|
|
|
|
} else if (B == Op1) { // (A^B)&B -> B&(B^A)
|
|
|
|
cast<BinaryOperator>(Op0)->swapOperands();
|
|
|
|
I.swapOperands(); // Simplify below
|
|
|
|
std::swap(Op0, Op1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Op1->hasOneUse() &&
|
|
|
|
match(Op1, m_Xor(m_Value(A), m_Value(B)))) {
|
|
|
|
if (B == Op0) { // B&(A^B) -> B&(B^A)
|
|
|
|
cast<BinaryOperator>(Op1)->swapOperands();
|
|
|
|
std::swap(A, B);
|
|
|
|
}
|
|
|
|
if (A == Op0) { // A&(A^B) -> A & ~B
|
|
|
|
Instruction *NotB = BinaryOperator::createNot(B, "tmp");
|
|
|
|
InsertNewInstBefore(NotB, I);
|
|
|
|
return BinaryOperator::createAnd(A, NotB);
|
|
|
|
}
|
|
|
|
}
|
2006-02-13 23:07:23 +00:00
|
|
|
}
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1)) {
|
|
|
|
// (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
|
|
|
|
if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS)))
|
2003-08-13 20:16:26 +00:00
|
|
|
return R;
|
|
|
|
|
Fold (and (setcc X, C1), (setcc X, C2))
This is important for several reasons:
1. Benchmarks have lots of code that looks like this (perlbmk in particular):
%tmp.2.i = setne int %tmp.0.i, 128 ; <bool> [#uses=1]
%tmp.6343 = seteq int %tmp.0.i, 1 ; <bool> [#uses=1]
%tmp.63 = and bool %tmp.2.i, %tmp.6343 ; <bool> [#uses=1]
we now fold away the setne, a clear improvement.
2. In the more important cases, such as (X >= 10) & (X < 20), we now produce
smaller code: (X-10) < 10.
3. Perhaps the nicest effect of this patch is that it really helps out the
code generators. In particular, for a 'range test' like the above,
instead of generating this on X86 (the difference on PPC is even more
pronounced):
cmp %EAX, 50
setge %CL
cmp %EAX, 100
setl %AL
and %CL, %AL
cmp %CL, 0
we now generate this:
add %EAX, -50
cmp %EAX, 50
Furthermore, this causes setcc's to be folded into branches more often.
These combinations trigger dozens of times in the spec benchmarks, particularly
in 176.gcc, 186.crafty, 253.perlbmk, 254.gap, & 099.go.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16559 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-28 21:48:02 +00:00
|
|
|
Value *LHSVal, *RHSVal;
|
|
|
|
ConstantInt *LHSCst, *RHSCst;
|
2006-12-23 06:05:41 +00:00
|
|
|
ICmpInst::Predicate LHSCC, RHSCC;
|
|
|
|
if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst))))
|
|
|
|
if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst))))
|
|
|
|
if (LHSVal == RHSVal && // Found (X icmp C1) & (X icmp C2)
|
|
|
|
// ICMP_[GL]E X, CST is folded to ICMP_[GL]T elsewhere.
|
|
|
|
LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE &&
|
|
|
|
RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE &&
|
|
|
|
LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE &&
|
2007-11-22 23:47:13 +00:00
|
|
|
RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE &&
|
|
|
|
|
|
|
|
// Don't try to fold ICMP_SLT + ICMP_ULT.
|
|
|
|
(ICmpInst::isEquality(LHSCC) || ICmpInst::isEquality(RHSCC) ||
|
|
|
|
ICmpInst::isSignedPredicate(LHSCC) ==
|
|
|
|
ICmpInst::isSignedPredicate(RHSCC))) {
|
Fold (and (setcc X, C1), (setcc X, C2))
This is important for several reasons:
1. Benchmarks have lots of code that looks like this (perlbmk in particular):
%tmp.2.i = setne int %tmp.0.i, 128 ; <bool> [#uses=1]
%tmp.6343 = seteq int %tmp.0.i, 1 ; <bool> [#uses=1]
%tmp.63 = and bool %tmp.2.i, %tmp.6343 ; <bool> [#uses=1]
we now fold away the setne, a clear improvement.
2. In the more important cases, such as (X >= 10) & (X < 20), we now produce
smaller code: (X-10) < 10.
3. Perhaps the nicest effect of this patch is that it really helps out the
code generators. In particular, for a 'range test' like the above,
instead of generating this on X86 (the difference on PPC is even more
pronounced):
cmp %EAX, 50
setge %CL
cmp %EAX, 100
setl %AL
and %CL, %AL
cmp %CL, 0
we now generate this:
add %EAX, -50
cmp %EAX, 50
Furthermore, this causes setcc's to be folded into branches more often.
These combinations trigger dozens of times in the spec benchmarks, particularly
in 176.gcc, 186.crafty, 253.perlbmk, 254.gap, & 099.go.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16559 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-28 21:48:02 +00:00
|
|
|
// Ensure that the larger constant is on the RHS.
|
2008-01-13 20:59:02 +00:00
|
|
|
ICmpInst::Predicate GT;
|
|
|
|
if (ICmpInst::isSignedPredicate(LHSCC) ||
|
|
|
|
(ICmpInst::isEquality(LHSCC) &&
|
|
|
|
ICmpInst::isSignedPredicate(RHSCC)))
|
|
|
|
GT = ICmpInst::ICMP_SGT;
|
|
|
|
else
|
|
|
|
GT = ICmpInst::ICMP_UGT;
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
Constant *Cmp = ConstantExpr::getICmp(GT, LHSCst, RHSCst);
|
|
|
|
ICmpInst *LHS = cast<ICmpInst>(Op0);
|
2007-01-12 04:24:46 +00:00
|
|
|
if (cast<ConstantInt>(Cmp)->getZExtValue()) {
|
Fold (and (setcc X, C1), (setcc X, C2))
This is important for several reasons:
1. Benchmarks have lots of code that looks like this (perlbmk in particular):
%tmp.2.i = setne int %tmp.0.i, 128 ; <bool> [#uses=1]
%tmp.6343 = seteq int %tmp.0.i, 1 ; <bool> [#uses=1]
%tmp.63 = and bool %tmp.2.i, %tmp.6343 ; <bool> [#uses=1]
we now fold away the setne, a clear improvement.
2. In the more important cases, such as (X >= 10) & (X < 20), we now produce
smaller code: (X-10) < 10.
3. Perhaps the nicest effect of this patch is that it really helps out the
code generators. In particular, for a 'range test' like the above,
instead of generating this on X86 (the difference on PPC is even more
pronounced):
cmp %EAX, 50
setge %CL
cmp %EAX, 100
setl %AL
and %CL, %AL
cmp %CL, 0
we now generate this:
add %EAX, -50
cmp %EAX, 50
Furthermore, this causes setcc's to be folded into branches more often.
These combinations trigger dozens of times in the spec benchmarks, particularly
in 176.gcc, 186.crafty, 253.perlbmk, 254.gap, & 099.go.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16559 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-28 21:48:02 +00:00
|
|
|
std::swap(LHS, RHS);
|
|
|
|
std::swap(LHSCst, RHSCst);
|
|
|
|
std::swap(LHSCC, RHSCC);
|
|
|
|
}
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
// At this point, we know we have have two icmp instructions
|
Fold (and (setcc X, C1), (setcc X, C2))
This is important for several reasons:
1. Benchmarks have lots of code that looks like this (perlbmk in particular):
%tmp.2.i = setne int %tmp.0.i, 128 ; <bool> [#uses=1]
%tmp.6343 = seteq int %tmp.0.i, 1 ; <bool> [#uses=1]
%tmp.63 = and bool %tmp.2.i, %tmp.6343 ; <bool> [#uses=1]
we now fold away the setne, a clear improvement.
2. In the more important cases, such as (X >= 10) & (X < 20), we now produce
smaller code: (X-10) < 10.
3. Perhaps the nicest effect of this patch is that it really helps out the
code generators. In particular, for a 'range test' like the above,
instead of generating this on X86 (the difference on PPC is even more
pronounced):
cmp %EAX, 50
setge %CL
cmp %EAX, 100
setl %AL
and %CL, %AL
cmp %CL, 0
we now generate this:
add %EAX, -50
cmp %EAX, 50
Furthermore, this causes setcc's to be folded into branches more often.
These combinations trigger dozens of times in the spec benchmarks, particularly
in 176.gcc, 186.crafty, 253.perlbmk, 254.gap, & 099.go.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16559 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-28 21:48:02 +00:00
|
|
|
// comparing a value against two constants and and'ing the result
|
|
|
|
// together. Because of the above check, we know that we only have
|
2006-12-23 06:05:41 +00:00
|
|
|
// icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know
|
|
|
|
// (from the FoldICmpLogical check above), that the two constants
|
|
|
|
// are not equal and that the larger constant is on the RHS
|
Fold (and (setcc X, C1), (setcc X, C2))
This is important for several reasons:
1. Benchmarks have lots of code that looks like this (perlbmk in particular):
%tmp.2.i = setne int %tmp.0.i, 128 ; <bool> [#uses=1]
%tmp.6343 = seteq int %tmp.0.i, 1 ; <bool> [#uses=1]
%tmp.63 = and bool %tmp.2.i, %tmp.6343 ; <bool> [#uses=1]
we now fold away the setne, a clear improvement.
2. In the more important cases, such as (X >= 10) & (X < 20), we now produce
smaller code: (X-10) < 10.
3. Perhaps the nicest effect of this patch is that it really helps out the
code generators. In particular, for a 'range test' like the above,
instead of generating this on X86 (the difference on PPC is even more
pronounced):
cmp %EAX, 50
setge %CL
cmp %EAX, 100
setl %AL
and %CL, %AL
cmp %CL, 0
we now generate this:
add %EAX, -50
cmp %EAX, 50
Furthermore, this causes setcc's to be folded into branches more often.
These combinations trigger dozens of times in the spec benchmarks, particularly
in 176.gcc, 186.crafty, 253.perlbmk, 254.gap, & 099.go.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16559 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-28 21:48:02 +00:00
|
|
|
assert(LHSCst != RHSCst && "Compares not folded above?");
|
|
|
|
|
|
|
|
switch (LHSCC) {
|
|
|
|
default: assert(0 && "Unknown integer condition code!");
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_EQ:
|
Fold (and (setcc X, C1), (setcc X, C2))
This is important for several reasons:
1. Benchmarks have lots of code that looks like this (perlbmk in particular):
%tmp.2.i = setne int %tmp.0.i, 128 ; <bool> [#uses=1]
%tmp.6343 = seteq int %tmp.0.i, 1 ; <bool> [#uses=1]
%tmp.63 = and bool %tmp.2.i, %tmp.6343 ; <bool> [#uses=1]
we now fold away the setne, a clear improvement.
2. In the more important cases, such as (X >= 10) & (X < 20), we now produce
smaller code: (X-10) < 10.
3. Perhaps the nicest effect of this patch is that it really helps out the
code generators. In particular, for a 'range test' like the above,
instead of generating this on X86 (the difference on PPC is even more
pronounced):
cmp %EAX, 50
setge %CL
cmp %EAX, 100
setl %AL
and %CL, %AL
cmp %CL, 0
we now generate this:
add %EAX, -50
cmp %EAX, 50
Furthermore, this causes setcc's to be folded into branches more often.
These combinations trigger dozens of times in the spec benchmarks, particularly
in 176.gcc, 186.crafty, 253.perlbmk, 254.gap, & 099.go.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16559 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-28 21:48:02 +00:00
|
|
|
switch (RHSCC) {
|
|
|
|
default: assert(0 && "Unknown integer condition code!");
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_EQ: // (X == 13 & X == 15) -> false
|
|
|
|
case ICmpInst::ICMP_UGT: // (X == 13 & X > 15) -> false
|
|
|
|
case ICmpInst::ICMP_SGT: // (X == 13 & X > 15) -> false
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getFalse());
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13
|
|
|
|
case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13
|
|
|
|
case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13
|
Fold (and (setcc X, C1), (setcc X, C2))
This is important for several reasons:
1. Benchmarks have lots of code that looks like this (perlbmk in particular):
%tmp.2.i = setne int %tmp.0.i, 128 ; <bool> [#uses=1]
%tmp.6343 = seteq int %tmp.0.i, 1 ; <bool> [#uses=1]
%tmp.63 = and bool %tmp.2.i, %tmp.6343 ; <bool> [#uses=1]
we now fold away the setne, a clear improvement.
2. In the more important cases, such as (X >= 10) & (X < 20), we now produce
smaller code: (X-10) < 10.
3. Perhaps the nicest effect of this patch is that it really helps out the
code generators. In particular, for a 'range test' like the above,
instead of generating this on X86 (the difference on PPC is even more
pronounced):
cmp %EAX, 50
setge %CL
cmp %EAX, 100
setl %AL
and %CL, %AL
cmp %CL, 0
we now generate this:
add %EAX, -50
cmp %EAX, 50
Furthermore, this causes setcc's to be folded into branches more often.
These combinations trigger dozens of times in the spec benchmarks, particularly
in 176.gcc, 186.crafty, 253.perlbmk, 254.gap, & 099.go.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16559 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-28 21:48:02 +00:00
|
|
|
return ReplaceInstUsesWith(I, LHS);
|
|
|
|
}
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_NE:
|
Fold (and (setcc X, C1), (setcc X, C2))
This is important for several reasons:
1. Benchmarks have lots of code that looks like this (perlbmk in particular):
%tmp.2.i = setne int %tmp.0.i, 128 ; <bool> [#uses=1]
%tmp.6343 = seteq int %tmp.0.i, 1 ; <bool> [#uses=1]
%tmp.63 = and bool %tmp.2.i, %tmp.6343 ; <bool> [#uses=1]
we now fold away the setne, a clear improvement.
2. In the more important cases, such as (X >= 10) & (X < 20), we now produce
smaller code: (X-10) < 10.
3. Perhaps the nicest effect of this patch is that it really helps out the
code generators. In particular, for a 'range test' like the above,
instead of generating this on X86 (the difference on PPC is even more
pronounced):
cmp %EAX, 50
setge %CL
cmp %EAX, 100
setl %AL
and %CL, %AL
cmp %CL, 0
we now generate this:
add %EAX, -50
cmp %EAX, 50
Furthermore, this causes setcc's to be folded into branches more often.
These combinations trigger dozens of times in the spec benchmarks, particularly
in 176.gcc, 186.crafty, 253.perlbmk, 254.gap, & 099.go.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16559 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-28 21:48:02 +00:00
|
|
|
switch (RHSCC) {
|
|
|
|
default: assert(0 && "Unknown integer condition code!");
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_ULT:
|
|
|
|
if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_ULT, LHSVal, LHSCst);
|
|
|
|
break; // (X != 13 & X u< 15) -> no change
|
|
|
|
case ICmpInst::ICMP_SLT:
|
|
|
|
if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_SLT, LHSVal, LHSCst);
|
|
|
|
break; // (X != 13 & X s< 15) -> no change
|
|
|
|
case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15
|
|
|
|
case ICmpInst::ICMP_UGT: // (X != 13 & X u> 15) -> X u> 15
|
|
|
|
case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15
|
Fold (and (setcc X, C1), (setcc X, C2))
This is important for several reasons:
1. Benchmarks have lots of code that looks like this (perlbmk in particular):
%tmp.2.i = setne int %tmp.0.i, 128 ; <bool> [#uses=1]
%tmp.6343 = seteq int %tmp.0.i, 1 ; <bool> [#uses=1]
%tmp.63 = and bool %tmp.2.i, %tmp.6343 ; <bool> [#uses=1]
we now fold away the setne, a clear improvement.
2. In the more important cases, such as (X >= 10) & (X < 20), we now produce
smaller code: (X-10) < 10.
3. Perhaps the nicest effect of this patch is that it really helps out the
code generators. In particular, for a 'range test' like the above,
instead of generating this on X86 (the difference on PPC is even more
pronounced):
cmp %EAX, 50
setge %CL
cmp %EAX, 100
setl %AL
and %CL, %AL
cmp %CL, 0
we now generate this:
add %EAX, -50
cmp %EAX, 50
Furthermore, this causes setcc's to be folded into branches more often.
These combinations trigger dozens of times in the spec benchmarks, particularly
in 176.gcc, 186.crafty, 253.perlbmk, 254.gap, & 099.go.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16559 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-28 21:48:02 +00:00
|
|
|
return ReplaceInstUsesWith(I, RHS);
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_NE:
|
|
|
|
if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1
|
Fold (and (setcc X, C1), (setcc X, C2))
This is important for several reasons:
1. Benchmarks have lots of code that looks like this (perlbmk in particular):
%tmp.2.i = setne int %tmp.0.i, 128 ; <bool> [#uses=1]
%tmp.6343 = seteq int %tmp.0.i, 1 ; <bool> [#uses=1]
%tmp.63 = and bool %tmp.2.i, %tmp.6343 ; <bool> [#uses=1]
we now fold away the setne, a clear improvement.
2. In the more important cases, such as (X >= 10) & (X < 20), we now produce
smaller code: (X-10) < 10.
3. Perhaps the nicest effect of this patch is that it really helps out the
code generators. In particular, for a 'range test' like the above,
instead of generating this on X86 (the difference on PPC is even more
pronounced):
cmp %EAX, 50
setge %CL
cmp %EAX, 100
setl %AL
and %CL, %AL
cmp %CL, 0
we now generate this:
add %EAX, -50
cmp %EAX, 50
Furthermore, this causes setcc's to be folded into branches more often.
These combinations trigger dozens of times in the spec benchmarks, particularly
in 176.gcc, 186.crafty, 253.perlbmk, 254.gap, & 099.go.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16559 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-28 21:48:02 +00:00
|
|
|
Constant *AddCST = ConstantExpr::getNeg(LHSCst);
|
|
|
|
Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST,
|
|
|
|
LHSVal->getName()+".off");
|
|
|
|
InsertNewInstBefore(Add, I);
|
2007-01-27 23:08:34 +00:00
|
|
|
return new ICmpInst(ICmpInst::ICMP_UGT, Add,
|
|
|
|
ConstantInt::get(Add->getType(), 1));
|
Fold (and (setcc X, C1), (setcc X, C2))
This is important for several reasons:
1. Benchmarks have lots of code that looks like this (perlbmk in particular):
%tmp.2.i = setne int %tmp.0.i, 128 ; <bool> [#uses=1]
%tmp.6343 = seteq int %tmp.0.i, 1 ; <bool> [#uses=1]
%tmp.63 = and bool %tmp.2.i, %tmp.6343 ; <bool> [#uses=1]
we now fold away the setne, a clear improvement.
2. In the more important cases, such as (X >= 10) & (X < 20), we now produce
smaller code: (X-10) < 10.
3. Perhaps the nicest effect of this patch is that it really helps out the
code generators. In particular, for a 'range test' like the above,
instead of generating this on X86 (the difference on PPC is even more
pronounced):
cmp %EAX, 50
setge %CL
cmp %EAX, 100
setl %AL
and %CL, %AL
cmp %CL, 0
we now generate this:
add %EAX, -50
cmp %EAX, 50
Furthermore, this causes setcc's to be folded into branches more often.
These combinations trigger dozens of times in the spec benchmarks, particularly
in 176.gcc, 186.crafty, 253.perlbmk, 254.gap, & 099.go.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16559 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-28 21:48:02 +00:00
|
|
|
}
|
|
|
|
break; // (X != 13 & X != 15) -> no change
|
|
|
|
}
|
|
|
|
break;
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_ULT:
|
Fold (and (setcc X, C1), (setcc X, C2))
This is important for several reasons:
1. Benchmarks have lots of code that looks like this (perlbmk in particular):
%tmp.2.i = setne int %tmp.0.i, 128 ; <bool> [#uses=1]
%tmp.6343 = seteq int %tmp.0.i, 1 ; <bool> [#uses=1]
%tmp.63 = and bool %tmp.2.i, %tmp.6343 ; <bool> [#uses=1]
we now fold away the setne, a clear improvement.
2. In the more important cases, such as (X >= 10) & (X < 20), we now produce
smaller code: (X-10) < 10.
3. Perhaps the nicest effect of this patch is that it really helps out the
code generators. In particular, for a 'range test' like the above,
instead of generating this on X86 (the difference on PPC is even more
pronounced):
cmp %EAX, 50
setge %CL
cmp %EAX, 100
setl %AL
and %CL, %AL
cmp %CL, 0
we now generate this:
add %EAX, -50
cmp %EAX, 50
Furthermore, this causes setcc's to be folded into branches more often.
These combinations trigger dozens of times in the spec benchmarks, particularly
in 176.gcc, 186.crafty, 253.perlbmk, 254.gap, & 099.go.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16559 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-28 21:48:02 +00:00
|
|
|
switch (RHSCC) {
|
|
|
|
default: assert(0 && "Unknown integer condition code!");
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false
|
|
|
|
case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getFalse());
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change
|
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13
|
|
|
|
case ICmpInst::ICMP_ULT: // (X u< 13 & X u< 15) -> X u< 13
|
Fold (and (setcc X, C1), (setcc X, C2))
This is important for several reasons:
1. Benchmarks have lots of code that looks like this (perlbmk in particular):
%tmp.2.i = setne int %tmp.0.i, 128 ; <bool> [#uses=1]
%tmp.6343 = seteq int %tmp.0.i, 1 ; <bool> [#uses=1]
%tmp.63 = and bool %tmp.2.i, %tmp.6343 ; <bool> [#uses=1]
we now fold away the setne, a clear improvement.
2. In the more important cases, such as (X >= 10) & (X < 20), we now produce
smaller code: (X-10) < 10.
3. Perhaps the nicest effect of this patch is that it really helps out the
code generators. In particular, for a 'range test' like the above,
instead of generating this on X86 (the difference on PPC is even more
pronounced):
cmp %EAX, 50
setge %CL
cmp %EAX, 100
setl %AL
and %CL, %AL
cmp %CL, 0
we now generate this:
add %EAX, -50
cmp %EAX, 50
Furthermore, this causes setcc's to be folded into branches more often.
These combinations trigger dozens of times in the spec benchmarks, particularly
in 176.gcc, 186.crafty, 253.perlbmk, 254.gap, & 099.go.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16559 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-28 21:48:02 +00:00
|
|
|
return ReplaceInstUsesWith(I, LHS);
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_SLT: // (X u< 13 & X s< 15) -> no change
|
|
|
|
break;
|
Fold (and (setcc X, C1), (setcc X, C2))
This is important for several reasons:
1. Benchmarks have lots of code that looks like this (perlbmk in particular):
%tmp.2.i = setne int %tmp.0.i, 128 ; <bool> [#uses=1]
%tmp.6343 = seteq int %tmp.0.i, 1 ; <bool> [#uses=1]
%tmp.63 = and bool %tmp.2.i, %tmp.6343 ; <bool> [#uses=1]
we now fold away the setne, a clear improvement.
2. In the more important cases, such as (X >= 10) & (X < 20), we now produce
smaller code: (X-10) < 10.
3. Perhaps the nicest effect of this patch is that it really helps out the
code generators. In particular, for a 'range test' like the above,
instead of generating this on X86 (the difference on PPC is even more
pronounced):
cmp %EAX, 50
setge %CL
cmp %EAX, 100
setl %AL
and %CL, %AL
cmp %CL, 0
we now generate this:
add %EAX, -50
cmp %EAX, 50
Furthermore, this causes setcc's to be folded into branches more often.
These combinations trigger dozens of times in the spec benchmarks, particularly
in 176.gcc, 186.crafty, 253.perlbmk, 254.gap, & 099.go.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16559 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-28 21:48:02 +00:00
|
|
|
}
|
2006-12-23 06:05:41 +00:00
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_SLT:
|
Fold (and (setcc X, C1), (setcc X, C2))
This is important for several reasons:
1. Benchmarks have lots of code that looks like this (perlbmk in particular):
%tmp.2.i = setne int %tmp.0.i, 128 ; <bool> [#uses=1]
%tmp.6343 = seteq int %tmp.0.i, 1 ; <bool> [#uses=1]
%tmp.63 = and bool %tmp.2.i, %tmp.6343 ; <bool> [#uses=1]
we now fold away the setne, a clear improvement.
2. In the more important cases, such as (X >= 10) & (X < 20), we now produce
smaller code: (X-10) < 10.
3. Perhaps the nicest effect of this patch is that it really helps out the
code generators. In particular, for a 'range test' like the above,
instead of generating this on X86 (the difference on PPC is even more
pronounced):
cmp %EAX, 50
setge %CL
cmp %EAX, 100
setl %AL
and %CL, %AL
cmp %CL, 0
we now generate this:
add %EAX, -50
cmp %EAX, 50
Furthermore, this causes setcc's to be folded into branches more often.
These combinations trigger dozens of times in the spec benchmarks, particularly
in 176.gcc, 186.crafty, 253.perlbmk, 254.gap, & 099.go.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16559 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-28 21:48:02 +00:00
|
|
|
switch (RHSCC) {
|
|
|
|
default: assert(0 && "Unknown integer condition code!");
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_EQ: // (X s< 13 & X == 15) -> false
|
|
|
|
case ICmpInst::ICMP_SGT: // (X s< 13 & X s> 15) -> false
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getFalse());
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change
|
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13
|
|
|
|
case ICmpInst::ICMP_SLT: // (X s< 13 & X s< 15) -> X < 13
|
|
|
|
return ReplaceInstUsesWith(I, LHS);
|
|
|
|
case ICmpInst::ICMP_ULT: // (X s< 13 & X u< 15) -> no change
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_UGT:
|
|
|
|
switch (RHSCC) {
|
|
|
|
default: assert(0 && "Unknown integer condition code!");
|
|
|
|
case ICmpInst::ICMP_EQ: // (X u> 13 & X == 15) -> X > 13
|
Fold (and (setcc X, C1), (setcc X, C2))
This is important for several reasons:
1. Benchmarks have lots of code that looks like this (perlbmk in particular):
%tmp.2.i = setne int %tmp.0.i, 128 ; <bool> [#uses=1]
%tmp.6343 = seteq int %tmp.0.i, 1 ; <bool> [#uses=1]
%tmp.63 = and bool %tmp.2.i, %tmp.6343 ; <bool> [#uses=1]
we now fold away the setne, a clear improvement.
2. In the more important cases, such as (X >= 10) & (X < 20), we now produce
smaller code: (X-10) < 10.
3. Perhaps the nicest effect of this patch is that it really helps out the
code generators. In particular, for a 'range test' like the above,
instead of generating this on X86 (the difference on PPC is even more
pronounced):
cmp %EAX, 50
setge %CL
cmp %EAX, 100
setl %AL
and %CL, %AL
cmp %CL, 0
we now generate this:
add %EAX, -50
cmp %EAX, 50
Furthermore, this causes setcc's to be folded into branches more often.
These combinations trigger dozens of times in the spec benchmarks, particularly
in 176.gcc, 186.crafty, 253.perlbmk, 254.gap, & 099.go.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16559 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-28 21:48:02 +00:00
|
|
|
return ReplaceInstUsesWith(I, LHS);
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_UGT: // (X u> 13 & X u> 15) -> X u> 15
|
Fold (and (setcc X, C1), (setcc X, C2))
This is important for several reasons:
1. Benchmarks have lots of code that looks like this (perlbmk in particular):
%tmp.2.i = setne int %tmp.0.i, 128 ; <bool> [#uses=1]
%tmp.6343 = seteq int %tmp.0.i, 1 ; <bool> [#uses=1]
%tmp.63 = and bool %tmp.2.i, %tmp.6343 ; <bool> [#uses=1]
we now fold away the setne, a clear improvement.
2. In the more important cases, such as (X >= 10) & (X < 20), we now produce
smaller code: (X-10) < 10.
3. Perhaps the nicest effect of this patch is that it really helps out the
code generators. In particular, for a 'range test' like the above,
instead of generating this on X86 (the difference on PPC is even more
pronounced):
cmp %EAX, 50
setge %CL
cmp %EAX, 100
setl %AL
and %CL, %AL
cmp %CL, 0
we now generate this:
add %EAX, -50
cmp %EAX, 50
Furthermore, this causes setcc's to be folded into branches more often.
These combinations trigger dozens of times in the spec benchmarks, particularly
in 176.gcc, 186.crafty, 253.perlbmk, 254.gap, & 099.go.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16559 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-28 21:48:02 +00:00
|
|
|
return ReplaceInstUsesWith(I, RHS);
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change
|
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_NE:
|
|
|
|
if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14
|
|
|
|
return new ICmpInst(LHSCC, LHSVal, RHSCst);
|
|
|
|
break; // (X u> 13 & X != 15) -> no change
|
|
|
|
case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) ->(X-14) <u 1
|
|
|
|
return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, false,
|
|
|
|
true, I);
|
|
|
|
case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change
|
|
|
|
break;
|
Fold (and (setcc X, C1), (setcc X, C2))
This is important for several reasons:
1. Benchmarks have lots of code that looks like this (perlbmk in particular):
%tmp.2.i = setne int %tmp.0.i, 128 ; <bool> [#uses=1]
%tmp.6343 = seteq int %tmp.0.i, 1 ; <bool> [#uses=1]
%tmp.63 = and bool %tmp.2.i, %tmp.6343 ; <bool> [#uses=1]
we now fold away the setne, a clear improvement.
2. In the more important cases, such as (X >= 10) & (X < 20), we now produce
smaller code: (X-10) < 10.
3. Perhaps the nicest effect of this patch is that it really helps out the
code generators. In particular, for a 'range test' like the above,
instead of generating this on X86 (the difference on PPC is even more
pronounced):
cmp %EAX, 50
setge %CL
cmp %EAX, 100
setl %AL
and %CL, %AL
cmp %CL, 0
we now generate this:
add %EAX, -50
cmp %EAX, 50
Furthermore, this causes setcc's to be folded into branches more often.
These combinations trigger dozens of times in the spec benchmarks, particularly
in 176.gcc, 186.crafty, 253.perlbmk, 254.gap, & 099.go.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16559 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-28 21:48:02 +00:00
|
|
|
}
|
2006-12-23 06:05:41 +00:00
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_SGT:
|
|
|
|
switch (RHSCC) {
|
|
|
|
default: assert(0 && "Unknown integer condition code!");
|
2007-11-16 06:04:17 +00:00
|
|
|
case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X == 15
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15
|
|
|
|
return ReplaceInstUsesWith(I, RHS);
|
|
|
|
case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change
|
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_NE:
|
|
|
|
if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14
|
|
|
|
return new ICmpInst(LHSCC, LHSVal, RHSCst);
|
|
|
|
break; // (X s> 13 & X != 15) -> no change
|
|
|
|
case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) ->(X-14) s< 1
|
|
|
|
return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, true,
|
|
|
|
true, I);
|
|
|
|
case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
Fold (and (setcc X, C1), (setcc X, C2))
This is important for several reasons:
1. Benchmarks have lots of code that looks like this (perlbmk in particular):
%tmp.2.i = setne int %tmp.0.i, 128 ; <bool> [#uses=1]
%tmp.6343 = seteq int %tmp.0.i, 1 ; <bool> [#uses=1]
%tmp.63 = and bool %tmp.2.i, %tmp.6343 ; <bool> [#uses=1]
we now fold away the setne, a clear improvement.
2. In the more important cases, such as (X >= 10) & (X < 20), we now produce
smaller code: (X-10) < 10.
3. Perhaps the nicest effect of this patch is that it really helps out the
code generators. In particular, for a 'range test' like the above,
instead of generating this on X86 (the difference on PPC is even more
pronounced):
cmp %EAX, 50
setge %CL
cmp %EAX, 100
setl %AL
and %CL, %AL
cmp %CL, 0
we now generate this:
add %EAX, -50
cmp %EAX, 50
Furthermore, this causes setcc's to be folded into branches more often.
These combinations trigger dozens of times in the spec benchmarks, particularly
in 176.gcc, 186.crafty, 253.perlbmk, 254.gap, & 099.go.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16559 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-28 21:48:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-05 06:39:07 +00:00
|
|
|
// fold (and (cast A), (cast B)) -> (cast (and A, B))
|
2006-12-13 08:27:15 +00:00
|
|
|
if (CastInst *Op0C = dyn_cast<CastInst>(Op0))
|
|
|
|
if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
|
|
|
|
if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind ?
|
|
|
|
const Type *SrcTy = Op0C->getOperand(0)->getType();
|
2007-01-15 02:27:26 +00:00
|
|
|
if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() &&
|
2006-12-13 08:27:15 +00:00
|
|
|
// Only do this if the casts both really cause code to be generated.
|
2006-12-23 06:05:41 +00:00
|
|
|
ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0),
|
|
|
|
I.getType(), TD) &&
|
|
|
|
ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0),
|
|
|
|
I.getType(), TD)) {
|
2006-12-13 08:27:15 +00:00
|
|
|
Instruction *NewOp = BinaryOperator::createAnd(Op0C->getOperand(0),
|
|
|
|
Op1C->getOperand(0),
|
|
|
|
I.getName());
|
|
|
|
InsertNewInstBefore(NewOp, I);
|
|
|
|
return CastInst::create(Op0C->getOpcode(), NewOp, I.getType());
|
|
|
|
}
|
2006-05-05 06:39:07 +00:00
|
|
|
}
|
2006-11-14 07:46:50 +00:00
|
|
|
|
|
|
|
// (X >> Z) & (Y >> Z) -> (X&Y) >> Z for all shifts.
|
2007-02-02 02:16:23 +00:00
|
|
|
if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) {
|
|
|
|
if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0))
|
|
|
|
if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() &&
|
2006-11-14 07:46:50 +00:00
|
|
|
SI0->getOperand(1) == SI1->getOperand(1) &&
|
|
|
|
(SI0->hasOneUse() || SI1->hasOneUse())) {
|
|
|
|
Instruction *NewOp =
|
|
|
|
InsertNewInstBefore(BinaryOperator::createAnd(SI0->getOperand(0),
|
|
|
|
SI1->getOperand(0),
|
|
|
|
SI0->getName()), I);
|
2007-02-02 02:16:23 +00:00
|
|
|
return BinaryOperator::create(SI1->getOpcode(), NewOp,
|
|
|
|
SI1->getOperand(1));
|
2006-11-14 07:46:50 +00:00
|
|
|
}
|
2006-05-05 06:39:07 +00:00
|
|
|
}
|
|
|
|
|
2007-10-24 05:38:08 +00:00
|
|
|
// (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y)
|
|
|
|
if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) {
|
|
|
|
if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) {
|
|
|
|
if (LHS->getPredicate() == FCmpInst::FCMP_ORD &&
|
|
|
|
RHS->getPredicate() == FCmpInst::FCMP_ORD)
|
|
|
|
if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
|
|
|
|
if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
|
|
|
|
// If either of the constants are nans, then the whole thing returns
|
|
|
|
// false.
|
2007-10-24 18:54:45 +00:00
|
|
|
if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
|
2007-10-24 05:38:08 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getFalse());
|
|
|
|
return new FCmpInst(FCmpInst::FCMP_ORD, LHS->getOperand(0),
|
|
|
|
RHS->getOperand(0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
return Changed ? &I : 0;
|
2002-05-06 16:14:14 +00:00
|
|
|
}
|
|
|
|
|
2006-06-15 19:07:26 +00:00
|
|
|
/// CollectBSwapParts - Look to see if the specified value defines a single byte
|
|
|
|
/// in the result. If it does, and if the specified byte hasn't been filled in
|
|
|
|
/// yet, fill it in and return false.
|
2007-02-15 22:52:10 +00:00
|
|
|
static bool CollectBSwapParts(Value *V, SmallVector<Value*, 8> &ByteValues) {
|
2006-06-15 19:07:26 +00:00
|
|
|
Instruction *I = dyn_cast<Instruction>(V);
|
|
|
|
if (I == 0) return true;
|
|
|
|
|
|
|
|
// If this is an or instruction, it is an inner node of the bswap.
|
|
|
|
if (I->getOpcode() == Instruction::Or)
|
|
|
|
return CollectBSwapParts(I->getOperand(0), ByteValues) ||
|
|
|
|
CollectBSwapParts(I->getOperand(1), ByteValues);
|
|
|
|
|
2007-03-30 09:29:48 +00:00
|
|
|
uint32_t BitWidth = I->getType()->getPrimitiveSizeInBits();
|
2006-06-15 19:07:26 +00:00
|
|
|
// If this is a shift by a constant int, and it is "24", then its operand
|
|
|
|
// defines a byte. We only handle unsigned types here.
|
2007-02-02 02:16:23 +00:00
|
|
|
if (I->isShift() && isa<ConstantInt>(I->getOperand(1))) {
|
2006-06-15 19:07:26 +00:00
|
|
|
// Not shifting the entire input by N-1 bytes?
|
2007-03-30 09:29:48 +00:00
|
|
|
if (cast<ConstantInt>(I->getOperand(1))->getLimitedValue(BitWidth) !=
|
2006-06-15 19:07:26 +00:00
|
|
|
8*(ByteValues.size()-1))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
unsigned DestNo;
|
|
|
|
if (I->getOpcode() == Instruction::Shl) {
|
|
|
|
// X << 24 defines the top byte with the lowest of the input bytes.
|
|
|
|
DestNo = ByteValues.size()-1;
|
|
|
|
} else {
|
|
|
|
// X >>u 24 defines the low byte with the highest of the input bytes.
|
|
|
|
DestNo = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the destination byte value is already defined, the values are or'd
|
|
|
|
// together, which isn't a bswap (unless it's an or of the same bits).
|
|
|
|
if (ByteValues[DestNo] && ByteValues[DestNo] != I->getOperand(0))
|
|
|
|
return true;
|
|
|
|
ByteValues[DestNo] = I->getOperand(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we can only handle and(shift X, imm), imm). Bail out of if we
|
|
|
|
// don't have this.
|
|
|
|
Value *Shift = 0, *ShiftLHS = 0;
|
|
|
|
ConstantInt *AndAmt = 0, *ShiftAmt = 0;
|
|
|
|
if (!match(I, m_And(m_Value(Shift), m_ConstantInt(AndAmt))) ||
|
|
|
|
!match(Shift, m_Shift(m_Value(ShiftLHS), m_ConstantInt(ShiftAmt))))
|
|
|
|
return true;
|
|
|
|
Instruction *SI = cast<Instruction>(Shift);
|
|
|
|
|
|
|
|
// Make sure that the shift amount is by a multiple of 8 and isn't too big.
|
2007-03-30 09:29:48 +00:00
|
|
|
if (ShiftAmt->getLimitedValue(BitWidth) & 7 ||
|
|
|
|
ShiftAmt->getLimitedValue(BitWidth) > 8*ByteValues.size())
|
2006-06-15 19:07:26 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Turn 0xFF -> 0, 0xFF00 -> 1, 0xFF0000 -> 2, etc.
|
|
|
|
unsigned DestByte;
|
2007-03-30 09:29:48 +00:00
|
|
|
if (AndAmt->getValue().getActiveBits() > 64)
|
|
|
|
return true;
|
|
|
|
uint64_t AndAmtVal = AndAmt->getZExtValue();
|
2006-06-15 19:07:26 +00:00
|
|
|
for (DestByte = 0; DestByte != ByteValues.size(); ++DestByte)
|
2007-03-30 09:29:48 +00:00
|
|
|
if (AndAmtVal == uint64_t(0xFF) << 8*DestByte)
|
2006-06-15 19:07:26 +00:00
|
|
|
break;
|
|
|
|
// Unknown mask for bswap.
|
|
|
|
if (DestByte == ByteValues.size()) return true;
|
|
|
|
|
2006-10-20 07:07:24 +00:00
|
|
|
unsigned ShiftBytes = ShiftAmt->getZExtValue()/8;
|
2006-06-15 19:07:26 +00:00
|
|
|
unsigned SrcByte;
|
|
|
|
if (SI->getOpcode() == Instruction::Shl)
|
|
|
|
SrcByte = DestByte - ShiftBytes;
|
|
|
|
else
|
|
|
|
SrcByte = DestByte + ShiftBytes;
|
|
|
|
|
|
|
|
// If the SrcByte isn't a bswapped value from the DestByte, reject it.
|
|
|
|
if (SrcByte != ByteValues.size()-DestByte-1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// If the destination byte value is already defined, the values are or'd
|
|
|
|
// together, which isn't a bswap (unless it's an or of the same bits).
|
|
|
|
if (ByteValues[DestByte] && ByteValues[DestByte] != SI->getOperand(0))
|
|
|
|
return true;
|
|
|
|
ByteValues[DestByte] = SI->getOperand(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom.
|
|
|
|
/// If so, insert the new bswap intrinsic and return it.
|
|
|
|
Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) {
|
2007-04-01 20:57:36 +00:00
|
|
|
const IntegerType *ITy = dyn_cast<IntegerType>(I.getType());
|
|
|
|
if (!ITy || ITy->getBitWidth() % 16)
|
|
|
|
return 0; // Can only bswap pairs of bytes. Can't do vectors.
|
2006-06-15 19:07:26 +00:00
|
|
|
|
|
|
|
/// ByteValues - For each byte of the result, we keep track of which value
|
|
|
|
/// defines each byte.
|
2007-02-15 22:52:10 +00:00
|
|
|
SmallVector<Value*, 8> ByteValues;
|
2007-04-01 20:57:36 +00:00
|
|
|
ByteValues.resize(ITy->getBitWidth()/8);
|
2006-06-15 19:07:26 +00:00
|
|
|
|
|
|
|
// Try to find all the pieces corresponding to the bswap.
|
|
|
|
if (CollectBSwapParts(I.getOperand(0), ByteValues) ||
|
|
|
|
CollectBSwapParts(I.getOperand(1), ByteValues))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Check to see if all of the bytes come from the same value.
|
|
|
|
Value *V = ByteValues[0];
|
|
|
|
if (V == 0) return 0; // Didn't find a byte? Must be zero.
|
|
|
|
|
|
|
|
// Check to make sure that all of the bytes come from the same value.
|
|
|
|
for (unsigned i = 1, e = ByteValues.size(); i != e; ++i)
|
|
|
|
if (ByteValues[i] != V)
|
|
|
|
return 0;
|
2007-08-04 01:51:18 +00:00
|
|
|
const Type *Tys[] = { ITy };
|
2006-06-15 19:07:26 +00:00
|
|
|
Module *M = I.getParent()->getParent()->getParent();
|
2007-08-04 01:51:18 +00:00
|
|
|
Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
|
2008-04-06 20:25:17 +00:00
|
|
|
return CallInst::Create(F, V);
|
2006-06-15 19:07:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
Instruction *InstCombiner::visitOr(BinaryOperator &I) {
|
2003-03-10 21:43:22 +00:00
|
|
|
bool Changed = SimplifyCommutative(I);
|
2002-06-25 16:13:24 +00:00
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
2002-05-06 16:14:14 +00:00
|
|
|
|
2007-03-24 23:56:43 +00:00
|
|
|
if (isa<UndefValue>(Op1)) // X | undef -> -1
|
2007-06-15 06:23:19 +00:00
|
|
|
return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
|
2004-10-16 18:11:37 +00:00
|
|
|
|
2006-02-12 08:02:11 +00:00
|
|
|
// or X, X = X
|
|
|
|
if (Op0 == Op1)
|
2002-08-12 21:17:25 +00:00
|
|
|
return ReplaceInstUsesWith(I, Op0);
|
2002-05-06 16:14:14 +00:00
|
|
|
|
2006-02-12 08:02:11 +00:00
|
|
|
// See if we can simplify any instructions used by the instruction whose sole
|
|
|
|
// purpose is to compute bits we don't care about.
|
2007-03-24 23:56:43 +00:00
|
|
|
if (!isa<VectorType>(I.getType())) {
|
|
|
|
uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth();
|
|
|
|
APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
|
|
|
|
if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth),
|
|
|
|
KnownZero, KnownOne))
|
|
|
|
return &I;
|
2007-06-15 05:26:55 +00:00
|
|
|
} else if (isa<ConstantAggregateZero>(Op1)) {
|
|
|
|
return ReplaceInstUsesWith(I, Op0); // X | <0,0> -> X
|
|
|
|
} else if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) {
|
|
|
|
if (CP->isAllOnesValue()) // X | <-1,-1> -> <-1,-1>
|
|
|
|
return ReplaceInstUsesWith(I, I.getOperand(1));
|
2007-03-24 23:56:43 +00:00
|
|
|
}
|
2007-06-15 05:26:55 +00:00
|
|
|
|
|
|
|
|
2006-02-12 08:02:11 +00:00
|
|
|
|
2002-05-06 16:14:14 +00:00
|
|
|
// or X, -1 == -1
|
2007-01-11 12:24:14 +00:00
|
|
|
if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
|
2006-01-06 17:59:59 +00:00
|
|
|
ConstantInt *C1 = 0; Value *X = 0;
|
2004-07-30 07:50:03 +00:00
|
|
|
// (X & C1) | C2 --> (X | C2) & (C1|C2)
|
|
|
|
if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) {
|
2007-02-11 01:23:03 +00:00
|
|
|
Instruction *Or = BinaryOperator::createOr(X, RHS);
|
2004-07-30 07:50:03 +00:00
|
|
|
InsertNewInstBefore(Or, I);
|
2007-02-11 01:23:03 +00:00
|
|
|
Or->takeName(Op0);
|
2007-04-02 13:45:30 +00:00
|
|
|
return BinaryOperator::createAnd(Or,
|
|
|
|
ConstantInt::get(RHS->getValue() | C1->getValue()));
|
2004-07-30 07:50:03 +00:00
|
|
|
}
|
2003-07-23 18:29:44 +00:00
|
|
|
|
2004-07-30 07:50:03 +00:00
|
|
|
// (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
|
|
|
|
if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) {
|
2007-02-11 01:23:03 +00:00
|
|
|
Instruction *Or = BinaryOperator::createOr(X, RHS);
|
2004-07-30 07:50:03 +00:00
|
|
|
InsertNewInstBefore(Or, I);
|
2007-02-11 01:23:03 +00:00
|
|
|
Or->takeName(Op0);
|
2004-07-30 07:50:03 +00:00
|
|
|
return BinaryOperator::createXor(Or,
|
2007-04-02 13:45:30 +00:00
|
|
|
ConstantInt::get(C1->getValue() & ~RHS->getValue()));
|
2003-07-23 18:29:44 +00:00
|
|
|
}
|
2004-04-09 19:05:30 +00:00
|
|
|
|
|
|
|
// Try to fold constant and into select arguments.
|
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
|
2005-01-01 16:22:27 +00:00
|
|
|
if (Instruction *R = FoldOpIntoSelect(I, SI, this))
|
2004-04-09 19:05:30 +00:00
|
|
|
return R;
|
Fold binary expressions and casts into PHI nodes that have all constant inputs.
This takes something like this:
%A = phi int [ 3, %cond_false.0 ], [ 2, %endif.0.i ], [ 2, %endif.1.i ]
%B = div int %tmp.243, 4
and turns it into:
%A = phi int [ 3/4, %cond_false.0 ], [ 2/4, %endif.0.i ], [ 2/4, %endif.1.i ]
which is later simplified (in this case) into %A = 0.
This triggers thousands of times in spec, for example, 269 times in 176.gcc.
This is tested by InstCombine/add.ll:test23 and set.ll:test18.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16582 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-29 05:07:12 +00:00
|
|
|
if (isa<PHINode>(Op0))
|
|
|
|
if (Instruction *NV = FoldOpIntoPhi(I))
|
|
|
|
return NV;
|
2003-07-23 18:29:44 +00:00
|
|
|
}
|
|
|
|
|
2006-01-06 17:59:59 +00:00
|
|
|
Value *A = 0, *B = 0;
|
|
|
|
ConstantInt *C1 = 0, *C2 = 0;
|
2005-05-07 23:49:08 +00:00
|
|
|
|
|
|
|
if (match(Op0, m_And(m_Value(A), m_Value(B))))
|
|
|
|
if (A == Op1 || B == Op1) // (A & ?) | A --> A
|
|
|
|
return ReplaceInstUsesWith(I, Op1);
|
|
|
|
if (match(Op1, m_And(m_Value(A), m_Value(B))))
|
|
|
|
if (A == Op0 || B == Op0) // A | (A & ?) --> A
|
|
|
|
return ReplaceInstUsesWith(I, Op0);
|
|
|
|
|
2006-07-10 20:25:24 +00:00
|
|
|
// (A | B) | C and A | (B | C) -> bswap if possible.
|
|
|
|
// (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible.
|
2006-06-15 19:07:26 +00:00
|
|
|
if (match(Op0, m_Or(m_Value(), m_Value())) ||
|
2006-07-10 20:25:24 +00:00
|
|
|
match(Op1, m_Or(m_Value(), m_Value())) ||
|
|
|
|
(match(Op0, m_Shift(m_Value(), m_Value())) &&
|
|
|
|
match(Op1, m_Shift(m_Value(), m_Value())))) {
|
2006-06-15 19:07:26 +00:00
|
|
|
if (Instruction *BSwap = MatchBSwap(I))
|
|
|
|
return BSwap;
|
|
|
|
}
|
|
|
|
|
2005-05-09 04:58:36 +00:00
|
|
|
// (X^C)|Y -> (X|Y)^C iff Y&C == 0
|
|
|
|
if (Op0->hasOneUse() && match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
|
2007-03-22 22:19:58 +00:00
|
|
|
MaskedValueIsZero(Op1, C1->getValue())) {
|
2007-02-11 01:23:03 +00:00
|
|
|
Instruction *NOr = BinaryOperator::createOr(A, Op1);
|
|
|
|
InsertNewInstBefore(NOr, I);
|
|
|
|
NOr->takeName(Op0);
|
|
|
|
return BinaryOperator::createXor(NOr, C1);
|
2005-05-09 04:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Y|(X^C) -> (X|Y)^C iff Y&C == 0
|
|
|
|
if (Op1->hasOneUse() && match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
|
2007-03-22 22:19:58 +00:00
|
|
|
MaskedValueIsZero(Op0, C1->getValue())) {
|
2007-02-11 01:23:03 +00:00
|
|
|
Instruction *NOr = BinaryOperator::createOr(A, Op0);
|
|
|
|
InsertNewInstBefore(NOr, I);
|
|
|
|
NOr->takeName(Op0);
|
|
|
|
return BinaryOperator::createXor(NOr, C1);
|
2005-05-09 04:58:36 +00:00
|
|
|
}
|
|
|
|
|
2007-04-08 07:47:01 +00:00
|
|
|
// (A & C)|(B & D)
|
2007-06-19 05:43:49 +00:00
|
|
|
Value *C = 0, *D = 0;
|
2007-04-08 07:47:01 +00:00
|
|
|
if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
|
|
|
|
match(Op1, m_And(m_Value(B), m_Value(D)))) {
|
2007-04-08 07:55:22 +00:00
|
|
|
Value *V1 = 0, *V2 = 0, *V3 = 0;
|
|
|
|
C1 = dyn_cast<ConstantInt>(C);
|
|
|
|
C2 = dyn_cast<ConstantInt>(D);
|
|
|
|
if (C1 && C2) { // (A & C1)|(B & C2)
|
|
|
|
// If we have: ((V + N) & C1) | (V & C2)
|
|
|
|
// .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
|
|
|
|
// replace with V+N.
|
|
|
|
if (C1->getValue() == ~C2->getValue()) {
|
|
|
|
if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+
|
|
|
|
match(A, m_Add(m_Value(V1), m_Value(V2)))) {
|
|
|
|
// Add commutes, try both ways.
|
|
|
|
if (V1 == B && MaskedValueIsZero(V2, C2->getValue()))
|
|
|
|
return ReplaceInstUsesWith(I, A);
|
|
|
|
if (V2 == B && MaskedValueIsZero(V1, C2->getValue()))
|
|
|
|
return ReplaceInstUsesWith(I, A);
|
|
|
|
}
|
|
|
|
// Or commutes, try both ways.
|
|
|
|
if ((C1->getValue() & (C1->getValue()+1)) == 0 &&
|
|
|
|
match(B, m_Add(m_Value(V1), m_Value(V2)))) {
|
|
|
|
// Add commutes, try both ways.
|
|
|
|
if (V1 == A && MaskedValueIsZero(V2, C1->getValue()))
|
|
|
|
return ReplaceInstUsesWith(I, B);
|
|
|
|
if (V2 == A && MaskedValueIsZero(V1, C1->getValue()))
|
|
|
|
return ReplaceInstUsesWith(I, B);
|
|
|
|
}
|
|
|
|
}
|
2007-04-08 08:01:49 +00:00
|
|
|
V1 = 0; V2 = 0; V3 = 0;
|
2007-04-08 07:55:22 +00:00
|
|
|
}
|
|
|
|
|
2007-04-08 07:47:01 +00:00
|
|
|
// Check to see if we have any common things being and'ed. If so, find the
|
|
|
|
// terms for V1 & (V2|V3).
|
|
|
|
if (isOnlyUse(Op0) || isOnlyUse(Op1)) {
|
|
|
|
if (A == B) // (A & C)|(A & D) == A & (C|D)
|
|
|
|
V1 = A, V2 = C, V3 = D;
|
|
|
|
else if (A == D) // (A & C)|(B & A) == A & (B|C)
|
|
|
|
V1 = A, V2 = B, V3 = C;
|
|
|
|
else if (C == B) // (A & C)|(C & D) == C & (A|D)
|
|
|
|
V1 = C, V2 = A, V3 = D;
|
|
|
|
else if (C == D) // (A & C)|(B & C) == C & (A|B)
|
|
|
|
V1 = C, V2 = A, V3 = B;
|
|
|
|
|
|
|
|
if (V1) {
|
|
|
|
Value *Or =
|
|
|
|
InsertNewInstBefore(BinaryOperator::createOr(V2, V3, "tmp"), I);
|
|
|
|
return BinaryOperator::createAnd(V1, Or);
|
|
|
|
}
|
|
|
|
}
|
2005-09-18 03:42:07 +00:00
|
|
|
}
|
2006-11-14 07:46:50 +00:00
|
|
|
|
|
|
|
// (X >> Z) | (Y >> Z) -> (X|Y) >> Z for all shifts.
|
2007-02-02 02:16:23 +00:00
|
|
|
if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) {
|
|
|
|
if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0))
|
|
|
|
if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() &&
|
2006-11-14 07:46:50 +00:00
|
|
|
SI0->getOperand(1) == SI1->getOperand(1) &&
|
|
|
|
(SI0->hasOneUse() || SI1->hasOneUse())) {
|
|
|
|
Instruction *NewOp =
|
|
|
|
InsertNewInstBefore(BinaryOperator::createOr(SI0->getOperand(0),
|
|
|
|
SI1->getOperand(0),
|
|
|
|
SI0->getName()), I);
|
2007-02-02 02:16:23 +00:00
|
|
|
return BinaryOperator::create(SI1->getOpcode(), NewOp,
|
|
|
|
SI1->getOperand(1));
|
2006-11-14 07:46:50 +00:00
|
|
|
}
|
|
|
|
}
|
2004-07-30 07:50:03 +00:00
|
|
|
|
|
|
|
if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1
|
|
|
|
if (A == Op1) // ~A | A == -1
|
2007-06-15 06:23:19 +00:00
|
|
|
return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
|
2004-07-30 07:50:03 +00:00
|
|
|
} else {
|
|
|
|
A = 0;
|
|
|
|
}
|
2005-05-07 23:49:08 +00:00
|
|
|
// Note, A is still live here!
|
2004-07-30 07:50:03 +00:00
|
|
|
if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B
|
|
|
|
if (Op0 == B)
|
2007-06-15 06:23:19 +00:00
|
|
|
return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
|
2003-03-10 23:13:59 +00:00
|
|
|
|
2004-07-30 12:50:08 +00:00
|
|
|
// (~A | ~B) == (~(A & B)) - De Morgan's Law
|
2004-07-30 07:50:03 +00:00
|
|
|
if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) {
|
|
|
|
Value *And = InsertNewInstBefore(BinaryOperator::createAnd(A, B,
|
|
|
|
I.getName()+".demorgan"), I);
|
|
|
|
return BinaryOperator::createNot(And);
|
|
|
|
}
|
2003-03-10 23:13:59 +00:00
|
|
|
}
|
2003-02-18 19:28:33 +00:00
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
// (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B)
|
|
|
|
if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) {
|
|
|
|
if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS)))
|
2003-08-13 20:16:26 +00:00
|
|
|
return R;
|
|
|
|
|
2004-09-28 22:33:08 +00:00
|
|
|
Value *LHSVal, *RHSVal;
|
|
|
|
ConstantInt *LHSCst, *RHSCst;
|
2006-12-23 06:05:41 +00:00
|
|
|
ICmpInst::Predicate LHSCC, RHSCC;
|
|
|
|
if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst))))
|
|
|
|
if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst))))
|
|
|
|
if (LHSVal == RHSVal && // Found (X icmp C1) | (X icmp C2)
|
|
|
|
// icmp [us][gl]e x, cst is folded to icmp [us][gl]t elsewhere.
|
|
|
|
LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE &&
|
|
|
|
RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE &&
|
|
|
|
LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE &&
|
2007-05-11 05:55:56 +00:00
|
|
|
RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE &&
|
|
|
|
// We can't fold (ugt x, C) | (sgt x, C2).
|
|
|
|
PredicatesFoldable(LHSCC, RHSCC)) {
|
2004-09-28 22:33:08 +00:00
|
|
|
// Ensure that the larger constant is on the RHS.
|
2006-12-23 06:05:41 +00:00
|
|
|
ICmpInst *LHS = cast<ICmpInst>(Op0);
|
2007-05-11 05:55:56 +00:00
|
|
|
bool NeedsSwap;
|
|
|
|
if (ICmpInst::isSignedPredicate(LHSCC))
|
2007-05-11 16:58:45 +00:00
|
|
|
NeedsSwap = LHSCst->getValue().sgt(RHSCst->getValue());
|
2007-05-11 05:55:56 +00:00
|
|
|
else
|
2007-05-11 16:58:45 +00:00
|
|
|
NeedsSwap = LHSCst->getValue().ugt(RHSCst->getValue());
|
2007-05-11 05:55:56 +00:00
|
|
|
|
|
|
|
if (NeedsSwap) {
|
2004-09-28 22:33:08 +00:00
|
|
|
std::swap(LHS, RHS);
|
|
|
|
std::swap(LHSCst, RHSCst);
|
|
|
|
std::swap(LHSCC, RHSCC);
|
|
|
|
}
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
// At this point, we know we have have two icmp instructions
|
2004-09-28 22:33:08 +00:00
|
|
|
// comparing a value against two constants and or'ing the result
|
|
|
|
// together. Because of the above check, we know that we only have
|
2006-12-23 06:05:41 +00:00
|
|
|
// ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the
|
|
|
|
// FoldICmpLogical check above), that the two constants are not
|
2004-09-28 22:33:08 +00:00
|
|
|
// equal.
|
|
|
|
assert(LHSCst != RHSCst && "Compares not folded above?");
|
|
|
|
|
|
|
|
switch (LHSCC) {
|
|
|
|
default: assert(0 && "Unknown integer condition code!");
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_EQ:
|
2004-09-28 22:33:08 +00:00
|
|
|
switch (RHSCC) {
|
|
|
|
default: assert(0 && "Unknown integer condition code!");
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_EQ:
|
2004-09-28 22:33:08 +00:00
|
|
|
if (LHSCst == SubOne(RHSCst)) {// (X == 13 | X == 14) -> X-13 <u 2
|
|
|
|
Constant *AddCST = ConstantExpr::getNeg(LHSCst);
|
|
|
|
Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST,
|
|
|
|
LHSVal->getName()+".off");
|
|
|
|
InsertNewInstBefore(Add, I);
|
2007-04-02 13:45:30 +00:00
|
|
|
AddCST = Subtract(AddOne(RHSCst), LHSCst);
|
2006-12-23 06:05:41 +00:00
|
|
|
return new ICmpInst(ICmpInst::ICMP_ULT, Add, AddCST);
|
2004-09-28 22:33:08 +00:00
|
|
|
}
|
2006-12-23 06:05:41 +00:00
|
|
|
break; // (X == 13 | X == 15) -> no change
|
|
|
|
case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change
|
|
|
|
case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change
|
2005-04-19 06:04:18 +00:00
|
|
|
break;
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_NE: // (X == 13 | X != 15) -> X != 15
|
|
|
|
case ICmpInst::ICMP_ULT: // (X == 13 | X u< 15) -> X u< 15
|
|
|
|
case ICmpInst::ICMP_SLT: // (X == 13 | X s< 15) -> X s< 15
|
2004-09-28 22:33:08 +00:00
|
|
|
return ReplaceInstUsesWith(I, RHS);
|
|
|
|
}
|
|
|
|
break;
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_NE:
|
2004-09-28 22:33:08 +00:00
|
|
|
switch (RHSCC) {
|
|
|
|
default: assert(0 && "Unknown integer condition code!");
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_EQ: // (X != 13 | X == 15) -> X != 13
|
|
|
|
case ICmpInst::ICMP_UGT: // (X != 13 | X u> 15) -> X != 13
|
|
|
|
case ICmpInst::ICMP_SGT: // (X != 13 | X s> 15) -> X != 13
|
2004-09-28 22:33:08 +00:00
|
|
|
return ReplaceInstUsesWith(I, LHS);
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true
|
|
|
|
case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true
|
|
|
|
case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getTrue());
|
2004-09-28 22:33:08 +00:00
|
|
|
}
|
|
|
|
break;
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_ULT:
|
|
|
|
switch (RHSCC) {
|
|
|
|
default: assert(0 && "Unknown integer condition code!");
|
|
|
|
case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change
|
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_UGT: // (X u< 13 | X u> 15) ->(X-13) u> 2
|
2007-11-01 02:18:41 +00:00
|
|
|
// If RHSCst is [us]MAXINT, it is always false. Not handling
|
|
|
|
// this can cause overflow.
|
|
|
|
if (RHSCst->isMaxValue(false))
|
|
|
|
return ReplaceInstUsesWith(I, LHS);
|
2006-12-23 06:05:41 +00:00
|
|
|
return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false,
|
|
|
|
false, I);
|
|
|
|
case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change
|
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_NE: // (X u< 13 | X != 15) -> X != 15
|
|
|
|
case ICmpInst::ICMP_ULT: // (X u< 13 | X u< 15) -> X u< 15
|
|
|
|
return ReplaceInstUsesWith(I, RHS);
|
|
|
|
case ICmpInst::ICMP_SLT: // (X u< 13 | X s< 15) -> no change
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_SLT:
|
2004-09-28 22:33:08 +00:00
|
|
|
switch (RHSCC) {
|
|
|
|
default: assert(0 && "Unknown integer condition code!");
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change
|
2004-09-28 22:33:08 +00:00
|
|
|
break;
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_SGT: // (X s< 13 | X s> 15) ->(X-13) s> 2
|
2007-11-01 02:18:41 +00:00
|
|
|
// If RHSCst is [us]MAXINT, it is always false. Not handling
|
|
|
|
// this can cause overflow.
|
|
|
|
if (RHSCst->isMaxValue(true))
|
|
|
|
return ReplaceInstUsesWith(I, LHS);
|
2006-12-23 06:05:41 +00:00
|
|
|
return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), true,
|
|
|
|
false, I);
|
|
|
|
case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change
|
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_NE: // (X s< 13 | X != 15) -> X != 15
|
|
|
|
case ICmpInst::ICMP_SLT: // (X s< 13 | X s< 15) -> X s< 15
|
2004-09-28 22:33:08 +00:00
|
|
|
return ReplaceInstUsesWith(I, RHS);
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_ULT: // (X s< 13 | X u< 15) -> no change
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_UGT:
|
|
|
|
switch (RHSCC) {
|
|
|
|
default: assert(0 && "Unknown integer condition code!");
|
|
|
|
case ICmpInst::ICMP_EQ: // (X u> 13 | X == 15) -> X u> 13
|
|
|
|
case ICmpInst::ICMP_UGT: // (X u> 13 | X u> 15) -> X u> 13
|
|
|
|
return ReplaceInstUsesWith(I, LHS);
|
|
|
|
case ICmpInst::ICMP_SGT: // (X u> 13 | X s> 15) -> no change
|
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true
|
|
|
|
case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getTrue());
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change
|
|
|
|
break;
|
2004-09-28 22:33:08 +00:00
|
|
|
}
|
|
|
|
break;
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_SGT:
|
2004-09-28 22:33:08 +00:00
|
|
|
switch (RHSCC) {
|
|
|
|
default: assert(0 && "Unknown integer condition code!");
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_EQ: // (X s> 13 | X == 15) -> X > 13
|
|
|
|
case ICmpInst::ICMP_SGT: // (X s> 13 | X s> 15) -> X > 13
|
2004-09-28 22:33:08 +00:00
|
|
|
return ReplaceInstUsesWith(I, LHS);
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_UGT: // (X s> 13 | X u> 15) -> no change
|
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_NE: // (X s> 13 | X != 15) -> true
|
|
|
|
case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getTrue());
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change
|
|
|
|
break;
|
2004-09-28 22:33:08 +00:00
|
|
|
}
|
2006-12-23 06:05:41 +00:00
|
|
|
break;
|
2004-09-28 22:33:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-05-05 06:39:07 +00:00
|
|
|
|
|
|
|
// fold (or (cast A), (cast B)) -> (cast (or A, B))
|
2007-10-24 05:38:08 +00:00
|
|
|
if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
|
2006-05-05 06:39:07 +00:00
|
|
|
if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
|
2006-12-13 08:27:15 +00:00
|
|
|
if (Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ?
|
2008-03-24 00:21:34 +00:00
|
|
|
if (!isa<ICmpInst>(Op0C->getOperand(0)) ||
|
|
|
|
!isa<ICmpInst>(Op1C->getOperand(0))) {
|
|
|
|
const Type *SrcTy = Op0C->getOperand(0)->getType();
|
|
|
|
if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() &&
|
|
|
|
// Only do this if the casts both really cause code to be
|
|
|
|
// generated.
|
|
|
|
ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0),
|
|
|
|
I.getType(), TD) &&
|
|
|
|
ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0),
|
|
|
|
I.getType(), TD)) {
|
|
|
|
Instruction *NewOp = BinaryOperator::createOr(Op0C->getOperand(0),
|
|
|
|
Op1C->getOperand(0),
|
|
|
|
I.getName());
|
|
|
|
InsertNewInstBefore(NewOp, I);
|
|
|
|
return CastInst::create(Op0C->getOpcode(), NewOp, I.getType());
|
|
|
|
}
|
2006-12-13 08:27:15 +00:00
|
|
|
}
|
2006-05-05 06:39:07 +00:00
|
|
|
}
|
2007-10-24 05:38:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// (fcmp uno x, c) | (fcmp uno y, c) -> (fcmp uno x, y)
|
|
|
|
if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) {
|
|
|
|
if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) {
|
|
|
|
if (LHS->getPredicate() == FCmpInst::FCMP_UNO &&
|
2008-02-29 06:09:11 +00:00
|
|
|
RHS->getPredicate() == FCmpInst::FCMP_UNO &&
|
|
|
|
LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType())
|
2007-10-24 05:38:08 +00:00
|
|
|
if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
|
|
|
|
if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
|
|
|
|
// If either of the constants are nans, then the whole thing returns
|
|
|
|
// true.
|
2007-10-24 18:54:45 +00:00
|
|
|
if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
|
2007-10-24 05:38:08 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getTrue());
|
|
|
|
|
|
|
|
// Otherwise, no need to compare the two constants, compare the
|
|
|
|
// rest.
|
|
|
|
return new FCmpInst(FCmpInst::FCMP_UNO, LHS->getOperand(0),
|
|
|
|
RHS->getOperand(0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-09-18 03:42:07 +00:00
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
return Changed ? &I : 0;
|
2002-05-06 16:14:14 +00:00
|
|
|
}
|
|
|
|
|
2004-02-16 01:20:27 +00:00
|
|
|
// XorSelf - Implements: X ^ X --> 0
|
|
|
|
struct XorSelf {
|
|
|
|
Value *RHS;
|
|
|
|
XorSelf(Value *rhs) : RHS(rhs) {}
|
|
|
|
bool shouldApply(Value *LHS) const { return LHS == RHS; }
|
|
|
|
Instruction *apply(BinaryOperator &Xor) const {
|
|
|
|
return &Xor;
|
|
|
|
}
|
|
|
|
};
|
2002-05-06 16:14:14 +00:00
|
|
|
|
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
Instruction *InstCombiner::visitXor(BinaryOperator &I) {
|
2003-03-10 21:43:22 +00:00
|
|
|
bool Changed = SimplifyCommutative(I);
|
2002-06-25 16:13:24 +00:00
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
2002-05-06 16:14:14 +00:00
|
|
|
|
2008-03-25 20:07:13 +00:00
|
|
|
if (isa<UndefValue>(Op1)) {
|
|
|
|
if (isa<UndefValue>(Op0))
|
|
|
|
// Handle undef ^ undef -> 0 special case. This is a common
|
|
|
|
// idiom (misuse).
|
|
|
|
return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
|
2004-10-16 18:11:37 +00:00
|
|
|
return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef
|
2008-03-25 20:07:13 +00:00
|
|
|
}
|
2004-10-16 18:11:37 +00:00
|
|
|
|
2004-02-16 01:20:27 +00:00
|
|
|
// xor X, X = 0, even if X is nested in a sequence of Xor's.
|
|
|
|
if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) {
|
2007-08-05 08:47:58 +00:00
|
|
|
assert(Result == &I && "AssociativeOpt didn't work?"); Result=Result;
|
2002-08-12 21:17:25 +00:00
|
|
|
return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
|
2004-02-16 01:20:27 +00:00
|
|
|
}
|
2006-02-12 08:02:11 +00:00
|
|
|
|
|
|
|
// See if we can simplify any instructions used by the instruction whose sole
|
|
|
|
// purpose is to compute bits we don't care about.
|
2007-03-22 22:19:58 +00:00
|
|
|
if (!isa<VectorType>(I.getType())) {
|
|
|
|
uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth();
|
|
|
|
APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
|
|
|
|
if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth),
|
|
|
|
KnownZero, KnownOne))
|
|
|
|
return &I;
|
2007-06-15 05:26:55 +00:00
|
|
|
} else if (isa<ConstantAggregateZero>(Op1)) {
|
|
|
|
return ReplaceInstUsesWith(I, Op0); // X ^ <0,0> -> X
|
2007-03-22 22:19:58 +00:00
|
|
|
}
|
2002-05-06 16:14:14 +00:00
|
|
|
|
2007-06-15 06:23:19 +00:00
|
|
|
// Is this a ~ operation?
|
|
|
|
if (Value *NotOp = dyn_castNotVal(&I)) {
|
|
|
|
// ~(~X & Y) --> (X | ~Y) - De Morgan's Law
|
|
|
|
// ~(~X | Y) === (X & ~Y) - De Morgan's Law
|
|
|
|
if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) {
|
|
|
|
if (Op0I->getOpcode() == Instruction::And ||
|
|
|
|
Op0I->getOpcode() == Instruction::Or) {
|
|
|
|
if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands();
|
|
|
|
if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) {
|
|
|
|
Instruction *NotY =
|
|
|
|
BinaryOperator::createNot(Op0I->getOperand(1),
|
|
|
|
Op0I->getOperand(1)->getName()+".not");
|
|
|
|
InsertNewInstBefore(NotY, I);
|
|
|
|
if (Op0I->getOpcode() == Instruction::And)
|
|
|
|
return BinaryOperator::createOr(Op0NotVal, NotY);
|
|
|
|
else
|
|
|
|
return BinaryOperator::createAnd(Op0NotVal, NotY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-11 12:24:14 +00:00
|
|
|
if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
|
2007-08-06 20:04:16 +00:00
|
|
|
// xor (cmp A, B), true = not (cmp A, B) = !cmp A, B
|
|
|
|
if (RHS == ConstantInt::getTrue() && Op0->hasOneUse()) {
|
|
|
|
if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0))
|
2006-12-23 06:05:41 +00:00
|
|
|
return new ICmpInst(ICI->getInversePredicate(),
|
|
|
|
ICI->getOperand(0), ICI->getOperand(1));
|
2003-11-04 23:50:51 +00:00
|
|
|
|
2007-08-06 20:04:16 +00:00
|
|
|
if (FCmpInst *FCI = dyn_cast<FCmpInst>(Op0))
|
|
|
|
return new FCmpInst(FCI->getInversePredicate(),
|
|
|
|
FCI->getOperand(0), FCI->getOperand(1));
|
|
|
|
}
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
|
2003-11-05 01:06:05 +00:00
|
|
|
// ~(c-X) == X-c-1 == X+(-c-1)
|
2004-01-12 19:35:11 +00:00
|
|
|
if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue())
|
|
|
|
if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) {
|
2004-06-10 02:07:29 +00:00
|
|
|
Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C);
|
|
|
|
Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C,
|
2004-01-12 19:35:11 +00:00
|
|
|
ConstantInt::get(I.getType(), 1));
|
2004-06-10 02:07:29 +00:00
|
|
|
return BinaryOperator::createAdd(Op0I->getOperand(1), ConstantRHS);
|
2004-01-12 19:35:11 +00:00
|
|
|
}
|
2007-04-02 05:36:22 +00:00
|
|
|
|
2008-02-20 11:26:25 +00:00
|
|
|
if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
|
2006-02-12 08:02:11 +00:00
|
|
|
if (Op0I->getOpcode() == Instruction::Add) {
|
2003-11-04 23:37:10 +00:00
|
|
|
// ~(X-c) --> (-c-1)-X
|
2004-01-12 19:35:11 +00:00
|
|
|
if (RHS->isAllOnesValue()) {
|
2004-06-10 02:07:29 +00:00
|
|
|
Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI);
|
|
|
|
return BinaryOperator::createSub(
|
|
|
|
ConstantExpr::getSub(NegOp0CI,
|
2004-01-12 19:35:11 +00:00
|
|
|
ConstantInt::get(I.getType(), 1)),
|
2003-11-04 23:37:10 +00:00
|
|
|
Op0I->getOperand(0));
|
2007-04-02 05:42:22 +00:00
|
|
|
} else if (RHS->getValue().isSignBit()) {
|
2007-04-02 05:36:22 +00:00
|
|
|
// (X + C) ^ signbit -> (X + C + signbit)
|
|
|
|
Constant *C = ConstantInt::get(RHS->getValue() + Op0CI->getValue());
|
|
|
|
return BinaryOperator::createAdd(Op0I->getOperand(0), C);
|
2007-04-02 05:48:58 +00:00
|
|
|
|
2004-01-12 19:35:11 +00:00
|
|
|
}
|
2006-02-26 19:57:54 +00:00
|
|
|
} else if (Op0I->getOpcode() == Instruction::Or) {
|
|
|
|
// (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0
|
2007-03-22 22:19:58 +00:00
|
|
|
if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue())) {
|
2006-02-26 19:57:54 +00:00
|
|
|
Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS);
|
|
|
|
// Anything in both C1 and C2 is known to be zero, remove it from
|
|
|
|
// NewRHS.
|
2007-04-02 13:45:30 +00:00
|
|
|
Constant *CommonBits = And(Op0CI, RHS);
|
2006-02-26 19:57:54 +00:00
|
|
|
NewRHS = ConstantExpr::getAnd(NewRHS,
|
|
|
|
ConstantExpr::getNot(CommonBits));
|
2007-03-02 21:28:56 +00:00
|
|
|
AddToWorkList(Op0I);
|
2006-02-26 19:57:54 +00:00
|
|
|
I.setOperand(0, Op0I->getOperand(0));
|
|
|
|
I.setOperand(1, NewRHS);
|
|
|
|
return &I;
|
|
|
|
}
|
2003-07-23 21:37:07 +00:00
|
|
|
}
|
2008-02-20 11:26:25 +00:00
|
|
|
}
|
2002-08-20 18:24:26 +00:00
|
|
|
}
|
2004-04-09 19:05:30 +00:00
|
|
|
|
|
|
|
// Try to fold constant and into select arguments.
|
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
|
2005-01-01 16:22:27 +00:00
|
|
|
if (Instruction *R = FoldOpIntoSelect(I, SI, this))
|
2004-04-09 19:05:30 +00:00
|
|
|
return R;
|
Fold binary expressions and casts into PHI nodes that have all constant inputs.
This takes something like this:
%A = phi int [ 3, %cond_false.0 ], [ 2, %endif.0.i ], [ 2, %endif.1.i ]
%B = div int %tmp.243, 4
and turns it into:
%A = phi int [ 3/4, %cond_false.0 ], [ 2/4, %endif.0.i ], [ 2/4, %endif.1.i ]
which is later simplified (in this case) into %A = 0.
This triggers thousands of times in spec, for example, 269 times in 176.gcc.
This is tested by InstCombine/add.ll:test23 and set.ll:test18.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16582 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-29 05:07:12 +00:00
|
|
|
if (isa<PHINode>(Op0))
|
|
|
|
if (Instruction *NV = FoldOpIntoPhi(I))
|
|
|
|
return NV;
|
2002-05-06 16:14:14 +00:00
|
|
|
}
|
|
|
|
|
2003-03-10 23:06:50 +00:00
|
|
|
if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1
|
2003-02-18 19:28:33 +00:00
|
|
|
if (X == Op1)
|
2007-06-15 06:23:19 +00:00
|
|
|
return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
|
2003-02-18 19:28:33 +00:00
|
|
|
|
2003-03-10 23:06:50 +00:00
|
|
|
if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1
|
2003-02-18 19:28:33 +00:00
|
|
|
if (X == Op0)
|
2007-06-15 06:23:19 +00:00
|
|
|
return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
|
2003-02-18 19:28:33 +00:00
|
|
|
|
2007-03-18 22:51:34 +00:00
|
|
|
|
|
|
|
BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1);
|
|
|
|
if (Op1I) {
|
|
|
|
Value *A, *B;
|
|
|
|
if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) {
|
|
|
|
if (A == Op0) { // B^(B|A) == (A|B)^B
|
2006-04-01 08:03:55 +00:00
|
|
|
Op1I->swapOperands();
|
2003-03-10 18:24:17 +00:00
|
|
|
I.swapOperands();
|
|
|
|
std::swap(Op0, Op1);
|
2007-03-18 22:51:34 +00:00
|
|
|
} else if (B == Op0) { // B^(A|B) == (A|B)^B
|
2006-04-01 08:03:55 +00:00
|
|
|
I.swapOperands(); // Simplified below.
|
2003-03-10 18:24:17 +00:00
|
|
|
std::swap(Op0, Op1);
|
2005-04-21 23:48:37 +00:00
|
|
|
}
|
2007-03-18 22:51:34 +00:00
|
|
|
} else if (match(Op1I, m_Xor(m_Value(A), m_Value(B)))) {
|
|
|
|
if (Op0 == A) // A^(A^B) == B
|
|
|
|
return ReplaceInstUsesWith(I, B);
|
|
|
|
else if (Op0 == B) // A^(B^A) == B
|
|
|
|
return ReplaceInstUsesWith(I, A);
|
|
|
|
} else if (match(Op1I, m_And(m_Value(A), m_Value(B))) && Op1I->hasOneUse()){
|
2007-04-01 05:36:37 +00:00
|
|
|
if (A == Op0) { // A^(A&B) -> A^(B&A)
|
2006-04-01 08:03:55 +00:00
|
|
|
Op1I->swapOperands();
|
2007-04-01 05:36:37 +00:00
|
|
|
std::swap(A, B);
|
|
|
|
}
|
2007-03-18 22:51:34 +00:00
|
|
|
if (B == Op0) { // A^(B&A) -> (B&A)^A
|
2006-04-01 08:03:55 +00:00
|
|
|
I.swapOperands(); // Simplified below.
|
|
|
|
std::swap(Op0, Op1);
|
|
|
|
}
|
2004-02-16 03:54:20 +00:00
|
|
|
}
|
2007-03-18 22:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0);
|
|
|
|
if (Op0I) {
|
|
|
|
Value *A, *B;
|
|
|
|
if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && Op0I->hasOneUse()) {
|
|
|
|
if (A == Op1) // (B|A)^B == (A|B)^B
|
|
|
|
std::swap(A, B);
|
|
|
|
if (B == Op1) { // (A|B)^B == A & ~B
|
|
|
|
Instruction *NotB =
|
|
|
|
InsertNewInstBefore(BinaryOperator::createNot(Op1, "tmp"), I);
|
|
|
|
return BinaryOperator::createAnd(A, NotB);
|
2003-03-10 18:24:17 +00:00
|
|
|
}
|
2007-03-18 22:51:34 +00:00
|
|
|
} else if (match(Op0I, m_Xor(m_Value(A), m_Value(B)))) {
|
|
|
|
if (Op1 == A) // (A^B)^A == B
|
|
|
|
return ReplaceInstUsesWith(I, B);
|
|
|
|
else if (Op1 == B) // (B^A)^A == B
|
|
|
|
return ReplaceInstUsesWith(I, A);
|
|
|
|
} else if (match(Op0I, m_And(m_Value(A), m_Value(B))) && Op0I->hasOneUse()){
|
|
|
|
if (A == Op1) // (A&B)^A -> (B&A)^A
|
|
|
|
std::swap(A, B);
|
|
|
|
if (B == Op1 && // (B&A)^A == ~B & A
|
2006-04-01 22:05:01 +00:00
|
|
|
!isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C
|
2007-03-18 22:51:34 +00:00
|
|
|
Instruction *N =
|
|
|
|
InsertNewInstBefore(BinaryOperator::createNot(A, "tmp"), I);
|
2006-04-01 08:03:55 +00:00
|
|
|
return BinaryOperator::createAnd(N, Op1);
|
|
|
|
}
|
2003-03-10 18:24:17 +00:00
|
|
|
}
|
2007-03-18 22:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// (X >> Z) ^ (Y >> Z) -> (X^Y) >> Z for all shifts.
|
|
|
|
if (Op0I && Op1I && Op0I->isShift() &&
|
|
|
|
Op0I->getOpcode() == Op1I->getOpcode() &&
|
|
|
|
Op0I->getOperand(1) == Op1I->getOperand(1) &&
|
|
|
|
(Op1I->hasOneUse() || Op1I->hasOneUse())) {
|
|
|
|
Instruction *NewOp =
|
|
|
|
InsertNewInstBefore(BinaryOperator::createXor(Op0I->getOperand(0),
|
|
|
|
Op1I->getOperand(0),
|
|
|
|
Op0I->getName()), I);
|
|
|
|
return BinaryOperator::create(Op1I->getOpcode(), NewOp,
|
|
|
|
Op1I->getOperand(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Op0I && Op1I) {
|
|
|
|
Value *A, *B, *C, *D;
|
|
|
|
// (A & B)^(A | B) -> A ^ B
|
|
|
|
if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
|
|
|
|
match(Op1I, m_Or(m_Value(C), m_Value(D)))) {
|
|
|
|
if ((A == C && B == D) || (A == D && B == C))
|
|
|
|
return BinaryOperator::createXor(A, B);
|
|
|
|
}
|
|
|
|
// (A | B)^(A & B) -> A ^ B
|
|
|
|
if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
|
|
|
|
match(Op1I, m_And(m_Value(C), m_Value(D)))) {
|
|
|
|
if ((A == C && B == D) || (A == D && B == C))
|
|
|
|
return BinaryOperator::createXor(A, B);
|
|
|
|
}
|
|
|
|
|
|
|
|
// (A & B)^(C & D)
|
|
|
|
if ((Op0I->hasOneUse() || Op1I->hasOneUse()) &&
|
|
|
|
match(Op0I, m_And(m_Value(A), m_Value(B))) &&
|
|
|
|
match(Op1I, m_And(m_Value(C), m_Value(D)))) {
|
|
|
|
// (X & Y)^(X & Y) -> (Y^Z) & X
|
|
|
|
Value *X = 0, *Y = 0, *Z = 0;
|
|
|
|
if (A == C)
|
|
|
|
X = A, Y = B, Z = D;
|
|
|
|
else if (A == D)
|
|
|
|
X = A, Y = B, Z = C;
|
|
|
|
else if (B == C)
|
|
|
|
X = B, Y = A, Z = D;
|
|
|
|
else if (B == D)
|
|
|
|
X = B, Y = A, Z = C;
|
|
|
|
|
|
|
|
if (X) {
|
|
|
|
Instruction *NewOp =
|
|
|
|
InsertNewInstBefore(BinaryOperator::createXor(Y, Z, Op0->getName()), I);
|
|
|
|
return BinaryOperator::createAnd(NewOp, X);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
// (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B)
|
|
|
|
if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
|
|
|
|
if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS)))
|
2003-08-13 20:16:26 +00:00
|
|
|
return R;
|
|
|
|
|
2006-05-05 06:39:07 +00:00
|
|
|
// fold (xor (cast A), (cast B)) -> (cast (xor A, B))
|
2007-10-24 05:38:08 +00:00
|
|
|
if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
|
2006-05-05 06:39:07 +00:00
|
|
|
if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
|
2006-12-13 08:27:15 +00:00
|
|
|
if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind?
|
|
|
|
const Type *SrcTy = Op0C->getOperand(0)->getType();
|
2007-01-15 02:27:26 +00:00
|
|
|
if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() &&
|
2006-12-13 08:27:15 +00:00
|
|
|
// Only do this if the casts both really cause code to be generated.
|
2006-12-23 06:05:41 +00:00
|
|
|
ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0),
|
|
|
|
I.getType(), TD) &&
|
|
|
|
ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0),
|
|
|
|
I.getType(), TD)) {
|
2006-12-13 08:27:15 +00:00
|
|
|
Instruction *NewOp = BinaryOperator::createXor(Op0C->getOperand(0),
|
|
|
|
Op1C->getOperand(0),
|
|
|
|
I.getName());
|
|
|
|
InsertNewInstBefore(NewOp, I);
|
|
|
|
return CastInst::create(Op0C->getOpcode(), NewOp, I.getType());
|
|
|
|
}
|
2006-05-05 06:39:07 +00:00
|
|
|
}
|
2007-10-24 05:38:08 +00:00
|
|
|
}
|
2002-06-25 16:13:24 +00:00
|
|
|
return Changed ? &I : 0;
|
2002-05-06 16:14:14 +00:00
|
|
|
}
|
|
|
|
|
2004-09-29 17:40:11 +00:00
|
|
|
/// AddWithOverflow - Compute Result = In1+In2, returning true if the result
|
|
|
|
/// overflowed for this type.
|
|
|
|
static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1,
|
2007-03-21 23:19:50 +00:00
|
|
|
ConstantInt *In2, bool IsSigned = false) {
|
2007-04-02 13:45:30 +00:00
|
|
|
Result = cast<ConstantInt>(Add(In1, In2));
|
2004-09-29 17:40:11 +00:00
|
|
|
|
2007-03-21 23:19:50 +00:00
|
|
|
if (IsSigned)
|
|
|
|
if (In2->getValue().isNegative())
|
|
|
|
return Result->getValue().sgt(In1->getValue());
|
|
|
|
else
|
|
|
|
return Result->getValue().slt(In1->getValue());
|
|
|
|
else
|
|
|
|
return Result->getValue().ult(In1->getValue());
|
2004-09-29 17:40:11 +00:00
|
|
|
}
|
|
|
|
|
2005-01-13 20:14:25 +00:00
|
|
|
/// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the
|
|
|
|
/// code necessary to compute the offset from the base pointer (without adding
|
|
|
|
/// in the base pointer). Return the result as a signed integer of intptr size.
|
|
|
|
static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) {
|
|
|
|
TargetData &TD = IC.getTargetData();
|
|
|
|
gep_type_iterator GTI = gep_type_begin(GEP);
|
2006-12-23 06:05:41 +00:00
|
|
|
const Type *IntPtrTy = TD.getIntPtrType();
|
|
|
|
Value *Result = Constant::getNullValue(IntPtrTy);
|
2005-01-13 20:14:25 +00:00
|
|
|
|
|
|
|
// Build a mask for high order bits.
|
2008-04-22 02:53:33 +00:00
|
|
|
unsigned IntPtrWidth = TD.getPointerSizeInBits();
|
2007-04-28 04:52:43 +00:00
|
|
|
uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth);
|
2005-01-13 20:14:25 +00:00
|
|
|
|
|
|
|
for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {
|
|
|
|
Value *Op = GEP->getOperand(i);
|
Executive summary: getTypeSize -> getTypeStoreSize / getABITypeSize.
The meaning of getTypeSize was not clear - clarifying it is important
now that we have x86 long double and arbitrary precision integers.
The issue with long double is that it requires 80 bits, and this is
not a multiple of its alignment. This gives a primitive type for
which getTypeSize differed from getABITypeSize. For arbitrary precision
integers it is even worse: there is the minimum number of bits needed to
hold the type (eg: 36 for an i36), the maximum number of bits that will
be overwriten when storing the type (40 bits for i36) and the ABI size
(i.e. the storage size rounded up to a multiple of the alignment; 64 bits
for i36).
This patch removes getTypeSize (not really - it is still there but
deprecated to allow for a gradual transition). Instead there is:
(1) getTypeSizeInBits - a number of bits that suffices to hold all
values of the type. For a primitive type, this is the minimum number
of bits. For an i36 this is 36 bits. For x86 long double it is 80.
This corresponds to gcc's TYPE_PRECISION.
(2) getTypeStoreSizeInBits - the maximum number of bits that is
written when storing the type (or read when reading it). For an
i36 this is 40 bits, for an x86 long double it is 80 bits. This
is the size alias analysis is interested in (getTypeStoreSize
returns the number of bytes). There doesn't seem to be anything
corresponding to this in gcc.
(3) getABITypeSizeInBits - this is getTypeStoreSizeInBits rounded
up to a multiple of the alignment. For an i36 this is 64, for an
x86 long double this is 96 or 128 depending on the OS. This is the
spacing between consecutive elements when you form an array out of
this type (getABITypeSize returns the number of bytes). This is
TYPE_SIZE in gcc.
Since successive elements in a SequentialType (arrays, pointers
and vectors) need to be aligned, the spacing between them will be
given by getABITypeSize. This means that the size of an array
is the length times the getABITypeSize. It also means that GEP
computations need to use getABITypeSize when computing offsets.
Furthermore, if an alloca allocates several elements at once then
these too need to be aligned, so the size of the alloca has to be
the number of elements multiplied by getABITypeSize. Logically
speaking this doesn't have to be the case when allocating just
one element, but it is simpler to also use getABITypeSize in this
case. So alloca's and mallocs should use getABITypeSize. Finally,
since gcc's only notion of size is that given by getABITypeSize, if
you want to output assembler etc the same as gcc then getABITypeSize
is the size you want.
Since a store will overwrite no more than getTypeStoreSize bytes,
and a read will read no more than that many bytes, this is the
notion of size appropriate for alias analysis calculations.
In this patch I have corrected all type size uses except some of
those in ScalarReplAggregates, lib/Codegen, lib/Target (the hard
cases). I will get around to auditing these too at some point,
but I could do with some help.
Finally, I made one change which I think wise but others might
consider pointless and suboptimal: in an unpacked struct the
amount of space allocated for a field is now given by the ABI
size rather than getTypeStoreSize. I did this because every
other place that reserves memory for a type (eg: alloca) now
uses getABITypeSize, and I didn't want to make an exception
for unpacked structs, i.e. I did it to make things more uniform.
This only effects structs containing long doubles and arbitrary
precision integers. If someone wants to pack these types more
tightly they can always use a packed struct.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@43620 91177308-0d34-0410-b5e6-96231b3b80d8
2007-11-01 20:53:16 +00:00
|
|
|
uint64_t Size = TD.getABITypeSize(GTI.getIndexedType()) & PtrSizeMask;
|
2007-04-28 04:52:43 +00:00
|
|
|
if (ConstantInt *OpC = dyn_cast<ConstantInt>(Op)) {
|
|
|
|
if (OpC->isZero()) continue;
|
|
|
|
|
|
|
|
// Handle a struct index, which adds its field offset to the pointer.
|
|
|
|
if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
|
|
|
|
Size = TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue());
|
|
|
|
|
|
|
|
if (ConstantInt *RC = dyn_cast<ConstantInt>(Result))
|
|
|
|
Result = ConstantInt::get(RC->getValue() + APInt(IntPtrWidth, Size));
|
2007-04-28 00:57:34 +00:00
|
|
|
else
|
2007-04-28 04:52:43 +00:00
|
|
|
Result = IC.InsertNewInstBefore(
|
|
|
|
BinaryOperator::createAdd(Result,
|
|
|
|
ConstantInt::get(IntPtrTy, Size),
|
|
|
|
GEP->getName()+".offs"), I);
|
|
|
|
continue;
|
2007-04-28 00:57:34 +00:00
|
|
|
}
|
2007-04-28 04:52:43 +00:00
|
|
|
|
|
|
|
Constant *Scale = ConstantInt::get(IntPtrTy, Size);
|
|
|
|
Constant *OC = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/);
|
|
|
|
Scale = ConstantExpr::getMul(OC, Scale);
|
|
|
|
if (Constant *RC = dyn_cast<Constant>(Result))
|
|
|
|
Result = ConstantExpr::getAdd(RC, Scale);
|
|
|
|
else {
|
|
|
|
// Emit an add instruction.
|
|
|
|
Result = IC.InsertNewInstBefore(
|
|
|
|
BinaryOperator::createAdd(Result, Scale,
|
|
|
|
GEP->getName()+".offs"), I);
|
2007-04-28 00:57:34 +00:00
|
|
|
}
|
2007-04-28 04:52:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Convert to correct type.
|
|
|
|
if (Op->getType() != IntPtrTy) {
|
|
|
|
if (Constant *OpC = dyn_cast<Constant>(Op))
|
|
|
|
Op = ConstantExpr::getSExt(OpC, IntPtrTy);
|
2007-04-28 00:57:34 +00:00
|
|
|
else
|
2007-04-28 04:52:43 +00:00
|
|
|
Op = IC.InsertNewInstBefore(new SExtInst(Op, IntPtrTy,
|
|
|
|
Op->getName()+".c"), I);
|
2005-01-13 20:14:25 +00:00
|
|
|
}
|
2007-04-28 04:52:43 +00:00
|
|
|
if (Size != 1) {
|
|
|
|
Constant *Scale = ConstantInt::get(IntPtrTy, Size);
|
|
|
|
if (Constant *OpC = dyn_cast<Constant>(Op))
|
|
|
|
Op = ConstantExpr::getMul(OpC, Scale);
|
|
|
|
else // We'll let instcombine(mul) convert this to a shl if possible.
|
|
|
|
Op = IC.InsertNewInstBefore(BinaryOperator::createMul(Op, Scale,
|
|
|
|
GEP->getName()+".idx"), I);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit an add instruction.
|
|
|
|
if (isa<Constant>(Op) && isa<Constant>(Result))
|
|
|
|
Result = ConstantExpr::getAdd(cast<Constant>(Op),
|
|
|
|
cast<Constant>(Result));
|
|
|
|
else
|
|
|
|
Result = IC.InsertNewInstBefore(BinaryOperator::createAdd(Op, Result,
|
|
|
|
GEP->getName()+".offs"), I);
|
2005-01-13 20:14:25 +00:00
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2008-04-22 02:53:33 +00:00
|
|
|
|
|
|
|
/// EvaluateGEPOffsetExpression - Return an value that can be used to compare of
|
|
|
|
/// the *offset* implied by GEP to zero. For example, if we have &A[i], we want
|
|
|
|
/// to return 'i' for "icmp ne i, 0". Note that, in general, indices can be
|
|
|
|
/// complex, and scales are involved. The above expression would also be legal
|
|
|
|
/// to codegen as "icmp ne (i*4), 0" (assuming A is a pointer to i32). This
|
|
|
|
/// later form is less amenable to optimization though, and we are allowed to
|
|
|
|
/// generate the first by knowing that pointer arithmetic doesn't overflow.
|
|
|
|
///
|
|
|
|
/// If we can't emit an optimized form for this expression, this returns null.
|
|
|
|
///
|
|
|
|
static Value *EvaluateGEPOffsetExpression(User *GEP, Instruction &I,
|
|
|
|
InstCombiner &IC) {
|
|
|
|
TargetData &TD = IC.getTargetData();
|
|
|
|
gep_type_iterator GTI = gep_type_begin(GEP);
|
|
|
|
|
|
|
|
// Check to see if this gep only has a single variable index. If so, and if
|
|
|
|
// any constant indices are a multiple of its scale, then we can compute this
|
|
|
|
// in terms of the scale of the variable index. For example, if the GEP
|
|
|
|
// implies an offset of "12 + i*4", then we can codegen this as "3 + i",
|
|
|
|
// because the expression will cross zero at the same point.
|
|
|
|
unsigned i, e = GEP->getNumOperands();
|
|
|
|
int64_t Offset = 0;
|
|
|
|
for (i = 1; i != e; ++i, ++GTI) {
|
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) {
|
|
|
|
// Compute the aggregate offset of constant indices.
|
|
|
|
if (CI->isZero()) continue;
|
|
|
|
|
|
|
|
// Handle a struct index, which adds its field offset to the pointer.
|
|
|
|
if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
|
|
|
|
Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
|
|
|
|
} else {
|
|
|
|
uint64_t Size = TD.getABITypeSize(GTI.getIndexedType());
|
|
|
|
Offset += Size*CI->getSExtValue();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Found our variable index.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no variable indices, we must have a constant offset, just
|
|
|
|
// evaluate it the general way.
|
|
|
|
if (i == e) return 0;
|
|
|
|
|
|
|
|
Value *VariableIdx = GEP->getOperand(i);
|
|
|
|
// Determine the scale factor of the variable element. For example, this is
|
|
|
|
// 4 if the variable index is into an array of i32.
|
|
|
|
uint64_t VariableScale = TD.getABITypeSize(GTI.getIndexedType());
|
|
|
|
|
|
|
|
// Verify that there are no other variable indices. If so, emit the hard way.
|
|
|
|
for (++i, ++GTI; i != e; ++i, ++GTI) {
|
|
|
|
ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i));
|
|
|
|
if (!CI) return 0;
|
|
|
|
|
|
|
|
// Compute the aggregate offset of constant indices.
|
|
|
|
if (CI->isZero()) continue;
|
|
|
|
|
|
|
|
// Handle a struct index, which adds its field offset to the pointer.
|
|
|
|
if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
|
|
|
|
Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
|
|
|
|
} else {
|
|
|
|
uint64_t Size = TD.getABITypeSize(GTI.getIndexedType());
|
|
|
|
Offset += Size*CI->getSExtValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Okay, we know we have a single variable index, which must be a
|
|
|
|
// pointer/array/vector index. If there is no offset, life is simple, return
|
|
|
|
// the index.
|
|
|
|
unsigned IntPtrWidth = TD.getPointerSizeInBits();
|
|
|
|
if (Offset == 0) {
|
|
|
|
// Cast to intptrty in case a truncation occurs. If an extension is needed,
|
|
|
|
// we don't need to bother extending: the extension won't affect where the
|
|
|
|
// computation crosses zero.
|
|
|
|
if (VariableIdx->getType()->getPrimitiveSizeInBits() > IntPtrWidth)
|
|
|
|
VariableIdx = new TruncInst(VariableIdx, TD.getIntPtrType(),
|
|
|
|
VariableIdx->getNameStart(), &I);
|
|
|
|
return VariableIdx;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, there is an index. The computation we will do will be modulo
|
|
|
|
// the pointer size, so get it.
|
|
|
|
uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth);
|
|
|
|
|
|
|
|
Offset &= PtrSizeMask;
|
|
|
|
VariableScale &= PtrSizeMask;
|
|
|
|
|
|
|
|
// To do this transformation, any constant index must be a multiple of the
|
|
|
|
// variable scale factor. For example, we can evaluate "12 + 4*i" as "3 + i",
|
|
|
|
// but we can't evaluate "10 + 3*i" in terms of i. Check that the offset is a
|
|
|
|
// multiple of the variable scale.
|
|
|
|
int64_t NewOffs = Offset / (int64_t)VariableScale;
|
|
|
|
if (Offset != NewOffs*(int64_t)VariableScale)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Okay, we can do this evaluation. Start by converting the index to intptr.
|
|
|
|
const Type *IntPtrTy = TD.getIntPtrType();
|
|
|
|
if (VariableIdx->getType() != IntPtrTy)
|
|
|
|
VariableIdx = CastInst::createIntegerCast(VariableIdx, IntPtrTy,
|
|
|
|
true /*SExt*/,
|
|
|
|
VariableIdx->getNameStart(), &I);
|
|
|
|
Constant *OffsetVal = ConstantInt::get(IntPtrTy, NewOffs);
|
|
|
|
return BinaryOperator::createAdd(VariableIdx, OffsetVal, "offset", &I);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
/// FoldGEPICmp - Fold comparisons between a GEP instruction and something
|
2005-01-13 20:14:25 +00:00
|
|
|
/// else. At this point we know that the GEP is on the LHS of the comparison.
|
2006-12-23 06:05:41 +00:00
|
|
|
Instruction *InstCombiner::FoldGEPICmp(User *GEPLHS, Value *RHS,
|
|
|
|
ICmpInst::Predicate Cond,
|
|
|
|
Instruction &I) {
|
2005-01-13 20:14:25 +00:00
|
|
|
assert(dyn_castGetElementPtr(GEPLHS) && "LHS is not a getelementptr!");
|
Implement an optimization for == and != comparisons like this:
_Bool test2(int X, int Y) {
return &arr[X][Y] == arr;
}
instead of generating this:
bool %test2(int %X, int %Y) {
%tmp.3.idx = mul int %X, 160 ; <int> [#uses=1]
%tmp.3.idx1 = shl int %Y, ubyte 2 ; <int> [#uses=1]
%tmp.3.offs2 = sub int 0, %tmp.3.idx ; <int> [#uses=1]
%tmp.7 = seteq int %tmp.3.idx1, %tmp.3.offs2 ; <bool> [#uses=1]
ret bool %tmp.7
}
generate this:
bool %test2(int %X, int %Y) {
seteq int %X, 0 ; <bool>:0 [#uses=1]
seteq int %Y, 0 ; <bool>:1 [#uses=1]
%tmp.7 = and bool %0, %1 ; <bool> [#uses=1]
ret bool %tmp.7
}
This idiom occurs in C++ programs when iterating from begin() to end(),
in a vector or array. For example, we now compile this:
void test(int X, int Y) {
for (int *i = arr; i != arr+100; ++i)
foo(*i);
}
to this:
no_exit: ; preds = %entry, %no_exit
...
%exitcond = seteq uint %indvar.next, 100 ; <bool> [#uses=1]
br bool %exitcond, label %return, label %no_exit
instead of this:
no_exit: ; preds = %entry, %no_exit
...
%inc5 = getelementptr [100 x [40 x int]]* %arr, int 0, int 0, int %inc.rec ; <int*> [#uses=1]
%tmp.8 = seteq int* %inc5, getelementptr ([100 x [40 x int]]* %arr, int 0, int 100, int 0) ; <bool> [#uses=1]
%indvar.next = add uint %indvar, 1 ; <uint> [#uses=1]
br bool %tmp.8, label %return, label %no_exit
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@19536 91177308-0d34-0410-b5e6-96231b3b80d8
2005-01-13 22:25:21 +00:00
|
|
|
|
2008-04-22 02:53:33 +00:00
|
|
|
// Look through bitcasts.
|
|
|
|
if (BitCastInst *BCI = dyn_cast<BitCastInst>(RHS))
|
|
|
|
RHS = BCI->getOperand(0);
|
Implement an optimization for == and != comparisons like this:
_Bool test2(int X, int Y) {
return &arr[X][Y] == arr;
}
instead of generating this:
bool %test2(int %X, int %Y) {
%tmp.3.idx = mul int %X, 160 ; <int> [#uses=1]
%tmp.3.idx1 = shl int %Y, ubyte 2 ; <int> [#uses=1]
%tmp.3.offs2 = sub int 0, %tmp.3.idx ; <int> [#uses=1]
%tmp.7 = seteq int %tmp.3.idx1, %tmp.3.offs2 ; <bool> [#uses=1]
ret bool %tmp.7
}
generate this:
bool %test2(int %X, int %Y) {
seteq int %X, 0 ; <bool>:0 [#uses=1]
seteq int %Y, 0 ; <bool>:1 [#uses=1]
%tmp.7 = and bool %0, %1 ; <bool> [#uses=1]
ret bool %tmp.7
}
This idiom occurs in C++ programs when iterating from begin() to end(),
in a vector or array. For example, we now compile this:
void test(int X, int Y) {
for (int *i = arr; i != arr+100; ++i)
foo(*i);
}
to this:
no_exit: ; preds = %entry, %no_exit
...
%exitcond = seteq uint %indvar.next, 100 ; <bool> [#uses=1]
br bool %exitcond, label %return, label %no_exit
instead of this:
no_exit: ; preds = %entry, %no_exit
...
%inc5 = getelementptr [100 x [40 x int]]* %arr, int 0, int 0, int %inc.rec ; <int*> [#uses=1]
%tmp.8 = seteq int* %inc5, getelementptr ([100 x [40 x int]]* %arr, int 0, int 100, int 0) ; <bool> [#uses=1]
%indvar.next = add uint %indvar, 1 ; <uint> [#uses=1]
br bool %tmp.8, label %return, label %no_exit
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@19536 91177308-0d34-0410-b5e6-96231b3b80d8
2005-01-13 22:25:21 +00:00
|
|
|
|
2005-01-13 20:14:25 +00:00
|
|
|
Value *PtrBase = GEPLHS->getOperand(0);
|
|
|
|
if (PtrBase == RHS) {
|
2008-02-05 04:45:32 +00:00
|
|
|
// ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0).
|
2008-04-22 02:53:33 +00:00
|
|
|
// This transformation (ignoring the base and scales) is valid because we
|
|
|
|
// know pointers can't overflow. See if we can output an optimized form.
|
|
|
|
Value *Offset = EvaluateGEPOffsetExpression(GEPLHS, I, *this);
|
|
|
|
|
|
|
|
// If not, synthesize the offset the hard way.
|
|
|
|
if (Offset == 0)
|
|
|
|
Offset = EmitGEPOffset(GEPLHS, I, *this);
|
2008-02-05 04:45:32 +00:00
|
|
|
return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset,
|
|
|
|
Constant::getNullValue(Offset->getType()));
|
2005-01-13 20:14:25 +00:00
|
|
|
} else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) {
|
2005-04-25 20:17:30 +00:00
|
|
|
// If the base pointers are different, but the indices are the same, just
|
|
|
|
// compare the base pointer.
|
|
|
|
if (PtrBase != GEPRHS->getOperand(0)) {
|
|
|
|
bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands();
|
2005-07-27 06:12:32 +00:00
|
|
|
IndicesTheSame &= GEPLHS->getOperand(0)->getType() ==
|
2005-04-26 14:40:41 +00:00
|
|
|
GEPRHS->getOperand(0)->getType();
|
2005-04-25 20:17:30 +00:00
|
|
|
if (IndicesTheSame)
|
|
|
|
for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
|
|
|
|
if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
|
|
|
|
IndicesTheSame = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If all indices are the same, just compare the base pointers.
|
|
|
|
if (IndicesTheSame)
|
2006-12-23 06:05:41 +00:00
|
|
|
return new ICmpInst(ICmpInst::getSignedPredicate(Cond),
|
|
|
|
GEPLHS->getOperand(0), GEPRHS->getOperand(0));
|
2005-04-25 20:17:30 +00:00
|
|
|
|
|
|
|
// Otherwise, the base pointers are different and the indices are
|
|
|
|
// different, bail out.
|
2005-01-13 20:14:25 +00:00
|
|
|
return 0;
|
2005-04-25 20:17:30 +00:00
|
|
|
}
|
2005-01-13 20:14:25 +00:00
|
|
|
|
Implement an optimization for == and != comparisons like this:
_Bool test2(int X, int Y) {
return &arr[X][Y] == arr;
}
instead of generating this:
bool %test2(int %X, int %Y) {
%tmp.3.idx = mul int %X, 160 ; <int> [#uses=1]
%tmp.3.idx1 = shl int %Y, ubyte 2 ; <int> [#uses=1]
%tmp.3.offs2 = sub int 0, %tmp.3.idx ; <int> [#uses=1]
%tmp.7 = seteq int %tmp.3.idx1, %tmp.3.offs2 ; <bool> [#uses=1]
ret bool %tmp.7
}
generate this:
bool %test2(int %X, int %Y) {
seteq int %X, 0 ; <bool>:0 [#uses=1]
seteq int %Y, 0 ; <bool>:1 [#uses=1]
%tmp.7 = and bool %0, %1 ; <bool> [#uses=1]
ret bool %tmp.7
}
This idiom occurs in C++ programs when iterating from begin() to end(),
in a vector or array. For example, we now compile this:
void test(int X, int Y) {
for (int *i = arr; i != arr+100; ++i)
foo(*i);
}
to this:
no_exit: ; preds = %entry, %no_exit
...
%exitcond = seteq uint %indvar.next, 100 ; <bool> [#uses=1]
br bool %exitcond, label %return, label %no_exit
instead of this:
no_exit: ; preds = %entry, %no_exit
...
%inc5 = getelementptr [100 x [40 x int]]* %arr, int 0, int 0, int %inc.rec ; <int*> [#uses=1]
%tmp.8 = seteq int* %inc5, getelementptr ([100 x [40 x int]]* %arr, int 0, int 100, int 0) ; <bool> [#uses=1]
%indvar.next = add uint %indvar, 1 ; <uint> [#uses=1]
br bool %tmp.8, label %return, label %no_exit
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@19536 91177308-0d34-0410-b5e6-96231b3b80d8
2005-01-13 22:25:21 +00:00
|
|
|
// If one of the GEPs has all zero indices, recurse.
|
|
|
|
bool AllZeros = true;
|
|
|
|
for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
|
|
|
|
if (!isa<Constant>(GEPLHS->getOperand(i)) ||
|
|
|
|
!cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) {
|
|
|
|
AllZeros = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (AllZeros)
|
2006-12-23 06:05:41 +00:00
|
|
|
return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0),
|
|
|
|
ICmpInst::getSwappedPredicate(Cond), I);
|
2005-01-14 00:20:05 +00:00
|
|
|
|
|
|
|
// If the other GEP has all zero indices, recurse.
|
Implement an optimization for == and != comparisons like this:
_Bool test2(int X, int Y) {
return &arr[X][Y] == arr;
}
instead of generating this:
bool %test2(int %X, int %Y) {
%tmp.3.idx = mul int %X, 160 ; <int> [#uses=1]
%tmp.3.idx1 = shl int %Y, ubyte 2 ; <int> [#uses=1]
%tmp.3.offs2 = sub int 0, %tmp.3.idx ; <int> [#uses=1]
%tmp.7 = seteq int %tmp.3.idx1, %tmp.3.offs2 ; <bool> [#uses=1]
ret bool %tmp.7
}
generate this:
bool %test2(int %X, int %Y) {
seteq int %X, 0 ; <bool>:0 [#uses=1]
seteq int %Y, 0 ; <bool>:1 [#uses=1]
%tmp.7 = and bool %0, %1 ; <bool> [#uses=1]
ret bool %tmp.7
}
This idiom occurs in C++ programs when iterating from begin() to end(),
in a vector or array. For example, we now compile this:
void test(int X, int Y) {
for (int *i = arr; i != arr+100; ++i)
foo(*i);
}
to this:
no_exit: ; preds = %entry, %no_exit
...
%exitcond = seteq uint %indvar.next, 100 ; <bool> [#uses=1]
br bool %exitcond, label %return, label %no_exit
instead of this:
no_exit: ; preds = %entry, %no_exit
...
%inc5 = getelementptr [100 x [40 x int]]* %arr, int 0, int 0, int %inc.rec ; <int*> [#uses=1]
%tmp.8 = seteq int* %inc5, getelementptr ([100 x [40 x int]]* %arr, int 0, int 100, int 0) ; <bool> [#uses=1]
%indvar.next = add uint %indvar, 1 ; <uint> [#uses=1]
br bool %tmp.8, label %return, label %no_exit
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@19536 91177308-0d34-0410-b5e6-96231b3b80d8
2005-01-13 22:25:21 +00:00
|
|
|
AllZeros = true;
|
|
|
|
for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
|
|
|
|
if (!isa<Constant>(GEPRHS->getOperand(i)) ||
|
|
|
|
!cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) {
|
|
|
|
AllZeros = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (AllZeros)
|
2006-12-23 06:05:41 +00:00
|
|
|
return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I);
|
Implement an optimization for == and != comparisons like this:
_Bool test2(int X, int Y) {
return &arr[X][Y] == arr;
}
instead of generating this:
bool %test2(int %X, int %Y) {
%tmp.3.idx = mul int %X, 160 ; <int> [#uses=1]
%tmp.3.idx1 = shl int %Y, ubyte 2 ; <int> [#uses=1]
%tmp.3.offs2 = sub int 0, %tmp.3.idx ; <int> [#uses=1]
%tmp.7 = seteq int %tmp.3.idx1, %tmp.3.offs2 ; <bool> [#uses=1]
ret bool %tmp.7
}
generate this:
bool %test2(int %X, int %Y) {
seteq int %X, 0 ; <bool>:0 [#uses=1]
seteq int %Y, 0 ; <bool>:1 [#uses=1]
%tmp.7 = and bool %0, %1 ; <bool> [#uses=1]
ret bool %tmp.7
}
This idiom occurs in C++ programs when iterating from begin() to end(),
in a vector or array. For example, we now compile this:
void test(int X, int Y) {
for (int *i = arr; i != arr+100; ++i)
foo(*i);
}
to this:
no_exit: ; preds = %entry, %no_exit
...
%exitcond = seteq uint %indvar.next, 100 ; <bool> [#uses=1]
br bool %exitcond, label %return, label %no_exit
instead of this:
no_exit: ; preds = %entry, %no_exit
...
%inc5 = getelementptr [100 x [40 x int]]* %arr, int 0, int 0, int %inc.rec ; <int*> [#uses=1]
%tmp.8 = seteq int* %inc5, getelementptr ([100 x [40 x int]]* %arr, int 0, int 100, int 0) ; <bool> [#uses=1]
%indvar.next = add uint %indvar, 1 ; <uint> [#uses=1]
br bool %tmp.8, label %return, label %no_exit
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@19536 91177308-0d34-0410-b5e6-96231b3b80d8
2005-01-13 22:25:21 +00:00
|
|
|
|
2005-01-14 00:20:05 +00:00
|
|
|
if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) {
|
|
|
|
// If the GEPs only differ by one index, compare it.
|
|
|
|
unsigned NumDifferences = 0; // Keep track of # differences.
|
|
|
|
unsigned DiffOperand = 0; // The operand that differs.
|
|
|
|
for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
|
|
|
|
if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
|
2005-04-24 06:59:08 +00:00
|
|
|
if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() !=
|
|
|
|
GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) {
|
2005-01-21 23:06:49 +00:00
|
|
|
// Irreconcilable differences.
|
2005-01-14 00:20:05 +00:00
|
|
|
NumDifferences = 2;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if (NumDifferences++) break;
|
|
|
|
DiffOperand = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NumDifferences == 0) // SAME GEP?
|
|
|
|
return ReplaceInstUsesWith(I, // No comparison is needed here.
|
2007-09-06 02:40:25 +00:00
|
|
|
ConstantInt::get(Type::Int1Ty,
|
|
|
|
isTrueWhenEqual(Cond)));
|
|
|
|
|
2005-01-14 00:20:05 +00:00
|
|
|
else if (NumDifferences == 1) {
|
2005-01-21 23:06:49 +00:00
|
|
|
Value *LHSV = GEPLHS->getOperand(DiffOperand);
|
|
|
|
Value *RHSV = GEPRHS->getOperand(DiffOperand);
|
2006-12-23 06:05:41 +00:00
|
|
|
// Make sure we do a signed comparison here.
|
|
|
|
return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV);
|
2005-01-14 00:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
// Only lower this if the icmp is the only user of the GEP or if we expect
|
2005-01-13 20:14:25 +00:00
|
|
|
// the result to fold to a constant!
|
|
|
|
if ((isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) &&
|
|
|
|
(isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) {
|
|
|
|
// ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2)
|
|
|
|
Value *L = EmitGEPOffset(GEPLHS, I, *this);
|
|
|
|
Value *R = EmitGEPOffset(GEPRHS, I, *this);
|
2006-12-23 06:05:41 +00:00
|
|
|
return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R);
|
2005-01-13 20:14:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) {
|
|
|
|
bool Changed = SimplifyCompare(I);
|
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
2005-01-13 20:14:25 +00:00
|
|
|
|
2007-01-14 19:42:17 +00:00
|
|
|
// Fold trivial predicates.
|
|
|
|
if (I.getPredicate() == FCmpInst::FCMP_FALSE)
|
|
|
|
return ReplaceInstUsesWith(I, Constant::getNullValue(Type::Int1Ty));
|
|
|
|
if (I.getPredicate() == FCmpInst::FCMP_TRUE)
|
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1));
|
|
|
|
|
|
|
|
// Simplify 'fcmp pred X, X'
|
|
|
|
if (Op0 == Op1) {
|
|
|
|
switch (I.getPredicate()) {
|
|
|
|
default: assert(0 && "Unknown predicate!");
|
|
|
|
case FCmpInst::FCMP_UEQ: // True if unordered or equal
|
|
|
|
case FCmpInst::FCMP_UGE: // True if unordered, greater than, or equal
|
|
|
|
case FCmpInst::FCMP_ULE: // True if unordered, less than, or equal
|
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1));
|
|
|
|
case FCmpInst::FCMP_OGT: // True if ordered and greater than
|
|
|
|
case FCmpInst::FCMP_OLT: // True if ordered and less than
|
|
|
|
case FCmpInst::FCMP_ONE: // True if ordered and operands are unequal
|
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 0));
|
|
|
|
|
|
|
|
case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y)
|
|
|
|
case FCmpInst::FCMP_ULT: // True if unordered or less than
|
|
|
|
case FCmpInst::FCMP_UGT: // True if unordered or greater than
|
|
|
|
case FCmpInst::FCMP_UNE: // True if unordered or not equal
|
|
|
|
// Canonicalize these to be 'fcmp uno %X, 0.0'.
|
|
|
|
I.setPredicate(FCmpInst::FCMP_UNO);
|
|
|
|
I.setOperand(1, Constant::getNullValue(Op0->getType()));
|
|
|
|
return &I;
|
|
|
|
|
|
|
|
case FCmpInst::FCMP_ORD: // True if ordered (no nans)
|
|
|
|
case FCmpInst::FCMP_OEQ: // True if ordered and equal
|
|
|
|
case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal
|
|
|
|
case FCmpInst::FCMP_OLE: // True if ordered and less than or equal
|
|
|
|
// Canonicalize these to be 'fcmp ord %X, 0.0'.
|
|
|
|
I.setPredicate(FCmpInst::FCMP_ORD);
|
|
|
|
I.setOperand(1, Constant::getNullValue(Op0->getType()));
|
|
|
|
return &I;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
if (isa<UndefValue>(Op1)) // fcmp pred X, undef -> undef
|
2007-01-11 18:21:29 +00:00
|
|
|
return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty));
|
2006-12-23 06:05:41 +00:00
|
|
|
|
|
|
|
// Handle fcmp with constant RHS
|
|
|
|
if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
|
|
|
|
if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
|
|
|
|
switch (LHSI->getOpcode()) {
|
|
|
|
case Instruction::PHI:
|
|
|
|
if (Instruction *NV = FoldOpIntoPhi(I))
|
|
|
|
return NV;
|
|
|
|
break;
|
|
|
|
case Instruction::Select:
|
|
|
|
// If either operand of the select is a constant, we can fold the
|
|
|
|
// comparison into the select arms, which will cause one to be
|
|
|
|
// constant folded and the select turned into a bitwise or.
|
|
|
|
Value *Op1 = 0, *Op2 = 0;
|
|
|
|
if (LHSI->hasOneUse()) {
|
|
|
|
if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) {
|
|
|
|
// Fold the known value into the constant operand.
|
|
|
|
Op1 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC);
|
|
|
|
// Insert a new FCmp of the other select operand.
|
|
|
|
Op2 = InsertNewInstBefore(new FCmpInst(I.getPredicate(),
|
|
|
|
LHSI->getOperand(2), RHSC,
|
|
|
|
I.getName()), I);
|
|
|
|
} else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) {
|
|
|
|
// Fold the known value into the constant operand.
|
|
|
|
Op2 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC);
|
|
|
|
// Insert a new FCmp of the other select operand.
|
|
|
|
Op1 = InsertNewInstBefore(new FCmpInst(I.getPredicate(),
|
|
|
|
LHSI->getOperand(1), RHSC,
|
|
|
|
I.getName()), I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Op1)
|
2008-04-06 20:25:17 +00:00
|
|
|
return SelectInst::Create(LHSI->getOperand(0), Op1, Op2);
|
2006-12-23 06:05:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed ? &I : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
|
|
|
|
bool Changed = SimplifyCompare(I);
|
2002-08-09 23:47:40 +00:00
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
|
|
|
const Type *Ty = Op0->getType();
|
2002-05-06 16:14:14 +00:00
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
// icmp X, X
|
2002-08-09 23:47:40 +00:00
|
|
|
if (Op0 == Op1)
|
2007-01-12 04:24:46 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty,
|
|
|
|
isTrueWhenEqual(I)));
|
2002-05-09 20:11:54 +00:00
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
if (isa<UndefValue>(Op1)) // X icmp undef -> undef
|
2007-01-11 18:21:29 +00:00
|
|
|
return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty));
|
Fold subtracts into integer compares vs. zero. This improves generate code for this case on X86
from
_foo:
movl $99, %ecx
movl 4(%esp), %eax
subl %eax, %ecx
xorl %edx, %edx
testl %ecx, %ecx
cmovs %edx, %eax
ret
to
_foo:
xorl %ecx, %ecx
movl 4(%esp), %eax
cmpl $99, %eax
cmovg %ecx, %eax
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45173 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-18 21:32:20 +00:00
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
// icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value
|
2004-11-14 07:33:16 +00:00
|
|
|
// addresses never equal each other! We already know that Op0 != Op1.
|
2005-04-21 23:48:37 +00:00
|
|
|
if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) ||
|
|
|
|
isa<ConstantPointerNull>(Op0)) &&
|
|
|
|
(isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) ||
|
2004-11-14 07:33:16 +00:00
|
|
|
isa<ConstantPointerNull>(Op1)))
|
2007-01-12 04:24:46 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty,
|
|
|
|
!isTrueWhenEqual(I)));
|
2002-08-09 23:47:40 +00:00
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
// icmp's with boolean values can always be turned into bitwise operations
|
2007-01-11 18:21:29 +00:00
|
|
|
if (Ty == Type::Int1Ty) {
|
2006-12-23 06:05:41 +00:00
|
|
|
switch (I.getPredicate()) {
|
|
|
|
default: assert(0 && "Invalid icmp instruction!");
|
|
|
|
case ICmpInst::ICMP_EQ: { // icmp eq bool %A, %B -> ~(A^B)
|
2004-06-10 02:07:29 +00:00
|
|
|
Instruction *Xor = BinaryOperator::createXor(Op0, Op1, I.getName()+"tmp");
|
2002-08-09 23:47:40 +00:00
|
|
|
InsertNewInstBefore(Xor, I);
|
2003-11-03 04:25:02 +00:00
|
|
|
return BinaryOperator::createNot(Xor);
|
2002-08-09 23:47:40 +00:00
|
|
|
}
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_NE: // icmp eq bool %A, %B -> A^B
|
2004-08-11 00:50:51 +00:00
|
|
|
return BinaryOperator::createXor(Op0, Op1);
|
2002-08-09 23:47:40 +00:00
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_UGT:
|
|
|
|
case ICmpInst::ICMP_SGT:
|
|
|
|
std::swap(Op0, Op1); // Change icmp gt -> icmp lt
|
2004-08-11 00:50:51 +00:00
|
|
|
// FALL THROUGH
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_ULT:
|
|
|
|
case ICmpInst::ICMP_SLT: { // icmp lt bool A, B -> ~X & Y
|
2004-08-11 00:50:51 +00:00
|
|
|
Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp");
|
|
|
|
InsertNewInstBefore(Not, I);
|
|
|
|
return BinaryOperator::createAnd(Not, Op1);
|
|
|
|
}
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_UGE:
|
|
|
|
case ICmpInst::ICMP_SGE:
|
|
|
|
std::swap(Op0, Op1); // Change icmp ge -> icmp le
|
2004-08-11 00:50:51 +00:00
|
|
|
// FALL THROUGH
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_ULE:
|
|
|
|
case ICmpInst::ICMP_SLE: { // icmp le bool %A, %B -> ~A | B
|
2004-08-11 00:50:51 +00:00
|
|
|
Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp");
|
|
|
|
InsertNewInstBefore(Not, I);
|
|
|
|
return BinaryOperator::createOr(Not, Op1);
|
|
|
|
}
|
|
|
|
}
|
2002-08-09 23:47:40 +00:00
|
|
|
}
|
|
|
|
|
2004-06-09 04:24:29 +00:00
|
|
|
// See if we are doing a comparison between a constant and an instruction that
|
|
|
|
// can be folded into the comparison.
|
2002-08-09 23:47:40 +00:00
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
|
2007-12-20 07:21:11 +00:00
|
|
|
Value *A, *B;
|
|
|
|
|
2008-01-05 01:18:20 +00:00
|
|
|
// (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B)
|
|
|
|
if (I.isEquality() && CI->isNullValue() &&
|
|
|
|
match(Op0, m_Sub(m_Value(A), m_Value(B)))) {
|
|
|
|
// (icmp cond A B) if cond is equality
|
|
|
|
return new ICmpInst(I.getPredicate(), A, B);
|
2007-12-28 07:42:12 +00:00
|
|
|
}
|
2007-12-20 07:21:11 +00:00
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
switch (I.getPredicate()) {
|
|
|
|
default: break;
|
|
|
|
case ICmpInst::ICMP_ULT: // A <u MIN -> FALSE
|
|
|
|
if (CI->isMinValue(false))
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getFalse());
|
2006-12-23 06:05:41 +00:00
|
|
|
if (CI->isMaxValue(false)) // A <u MAX -> A != MAX
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_NE, Op0,Op1);
|
|
|
|
if (isMinValuePlusOne(CI,false)) // A <u MIN+1 -> A == MIN
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI));
|
2007-04-11 06:12:58 +00:00
|
|
|
// (x <u 2147483648) -> (x >s -1) -> true if sign bit clear
|
|
|
|
if (CI->isMinValue(true))
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_SGT, Op0,
|
|
|
|
ConstantInt::getAllOnesValue(Op0->getType()));
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ICmpInst::ICMP_SLT:
|
|
|
|
if (CI->isMinValue(true)) // A <s MIN -> FALSE
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getFalse());
|
2006-12-23 06:05:41 +00:00
|
|
|
if (CI->isMaxValue(true)) // A <s MAX -> A != MAX
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
|
|
|
|
if (isMinValuePlusOne(CI,true)) // A <s MIN+1 -> A == MIN
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICmpInst::ICMP_UGT:
|
|
|
|
if (CI->isMaxValue(false)) // A >u MAX -> FALSE
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getFalse());
|
2006-12-23 06:05:41 +00:00
|
|
|
if (CI->isMinValue(false)) // A >u MIN -> A != MIN
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
|
|
|
|
if (isMaxValueMinusOne(CI, false)) // A >u MAX-1 -> A == MAX
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI));
|
2007-04-11 06:12:58 +00:00
|
|
|
|
|
|
|
// (x >u 2147483647) -> (x <s 0) -> true if sign bit set
|
|
|
|
if (CI->isMaxValue(true))
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_SLT, Op0,
|
|
|
|
ConstantInt::getNullValue(Op0->getType()));
|
2006-12-23 06:05:41 +00:00
|
|
|
break;
|
2004-09-29 17:40:11 +00:00
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_SGT:
|
|
|
|
if (CI->isMaxValue(true)) // A >s MAX -> FALSE
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getFalse());
|
2006-12-23 06:05:41 +00:00
|
|
|
if (CI->isMinValue(true)) // A >s MIN -> A != MIN
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
|
|
|
|
if (isMaxValueMinusOne(CI, true)) // A >s MAX-1 -> A == MAX
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICmpInst::ICMP_ULE:
|
|
|
|
if (CI->isMaxValue(false)) // A <=u MAX -> TRUE
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getTrue());
|
2006-12-23 06:05:41 +00:00
|
|
|
if (CI->isMinValue(false)) // A <=u MIN -> A == MIN
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
|
|
|
|
if (isMaxValueMinusOne(CI,false)) // A <=u MAX-1 -> A != MAX
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICmpInst::ICMP_SLE:
|
|
|
|
if (CI->isMaxValue(true)) // A <=s MAX -> TRUE
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getTrue());
|
2006-12-23 06:05:41 +00:00
|
|
|
if (CI->isMinValue(true)) // A <=s MIN -> A == MIN
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
|
|
|
|
if (isMaxValueMinusOne(CI,true)) // A <=s MAX-1 -> A != MAX
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICmpInst::ICMP_UGE:
|
|
|
|
if (CI->isMinValue(false)) // A >=u MIN -> TRUE
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getTrue());
|
2006-12-23 06:05:41 +00:00
|
|
|
if (CI->isMaxValue(false)) // A >=u MAX -> A == MAX
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
|
|
|
|
if (isMinValuePlusOne(CI,false)) // A >=u MIN-1 -> A != MIN
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICmpInst::ICMP_SGE:
|
|
|
|
if (CI->isMinValue(true)) // A >=s MIN -> TRUE
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getTrue());
|
2006-12-23 06:05:41 +00:00
|
|
|
if (CI->isMaxValue(true)) // A >=s MAX -> A == MAX
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1);
|
|
|
|
if (isMinValuePlusOne(CI,true)) // A >=s MIN-1 -> A != MIN
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we still have a icmp le or icmp ge instruction, turn it into the
|
|
|
|
// appropriate icmp lt or icmp gt instruction. Since the border cases have
|
2004-09-29 17:40:11 +00:00
|
|
|
// already been handled above, this requires little checking.
|
|
|
|
//
|
2007-03-25 19:55:33 +00:00
|
|
|
switch (I.getPredicate()) {
|
2007-07-15 20:54:51 +00:00
|
|
|
default: break;
|
|
|
|
case ICmpInst::ICMP_ULE:
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_ULT, Op0, AddOne(CI));
|
|
|
|
case ICmpInst::ICMP_SLE:
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_SLT, Op0, AddOne(CI));
|
|
|
|
case ICmpInst::ICMP_UGE:
|
|
|
|
return new ICmpInst( ICmpInst::ICMP_UGT, Op0, SubOne(CI));
|
|
|
|
case ICmpInst::ICMP_SGE:
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_SGT, Op0, SubOne(CI));
|
2007-03-25 19:55:33 +00:00
|
|
|
}
|
2006-02-12 02:07:56 +00:00
|
|
|
|
|
|
|
// See if we can fold the comparison based on bits known to be zero or one
|
2007-07-15 20:54:51 +00:00
|
|
|
// in the input. If this comparison is a normal comparison, it demands all
|
|
|
|
// bits, if it is a sign bit comparison, it only demands the sign bit.
|
|
|
|
|
|
|
|
bool UnusedBit;
|
|
|
|
bool isSignBit = isSignBitCheck(I.getPredicate(), CI, UnusedBit);
|
|
|
|
|
2007-03-22 20:36:03 +00:00
|
|
|
uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth();
|
|
|
|
APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
|
2007-07-15 20:54:51 +00:00
|
|
|
if (SimplifyDemandedBits(Op0,
|
|
|
|
isSignBit ? APInt::getSignBit(BitWidth)
|
|
|
|
: APInt::getAllOnesValue(BitWidth),
|
2006-02-12 02:07:56 +00:00
|
|
|
KnownZero, KnownOne, 0))
|
|
|
|
return &I;
|
|
|
|
|
|
|
|
// Given the known and unknown bits, compute a range that the LHS could be
|
|
|
|
// in.
|
2007-03-22 20:36:03 +00:00
|
|
|
if ((KnownOne | KnownZero) != 0) {
|
2006-12-23 06:05:41 +00:00
|
|
|
// Compute the Min, Max and RHS values based on the known bits. For the
|
|
|
|
// EQ and NE we use unsigned values.
|
2007-04-01 17:13:37 +00:00
|
|
|
APInt Min(BitWidth, 0), Max(BitWidth, 0);
|
|
|
|
const APInt& RHSVal = CI->getValue();
|
2006-12-23 06:05:41 +00:00
|
|
|
if (ICmpInst::isSignedPredicate(I.getPredicate())) {
|
2007-03-22 20:36:03 +00:00
|
|
|
ComputeSignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, Min,
|
|
|
|
Max);
|
2006-12-23 06:05:41 +00:00
|
|
|
} else {
|
2007-03-22 20:36:03 +00:00
|
|
|
ComputeUnsignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, Min,
|
|
|
|
Max);
|
2006-12-23 06:05:41 +00:00
|
|
|
}
|
|
|
|
switch (I.getPredicate()) { // LE/GE have been folded already.
|
|
|
|
default: assert(0 && "Unknown icmp opcode!");
|
|
|
|
case ICmpInst::ICMP_EQ:
|
2007-03-22 20:36:03 +00:00
|
|
|
if (Max.ult(RHSVal) || Min.ugt(RHSVal))
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getFalse());
|
2006-12-23 06:05:41 +00:00
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_NE:
|
2007-03-22 20:36:03 +00:00
|
|
|
if (Max.ult(RHSVal) || Min.ugt(RHSVal))
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getTrue());
|
2006-12-23 06:05:41 +00:00
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_ULT:
|
2007-03-22 20:36:03 +00:00
|
|
|
if (Max.ult(RHSVal))
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getTrue());
|
2007-04-09 23:52:13 +00:00
|
|
|
if (Min.uge(RHSVal))
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getFalse());
|
2006-12-23 06:05:41 +00:00
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_UGT:
|
2007-03-22 20:36:03 +00:00
|
|
|
if (Min.ugt(RHSVal))
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getTrue());
|
2007-04-09 23:52:13 +00:00
|
|
|
if (Max.ule(RHSVal))
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getFalse());
|
2006-12-23 06:05:41 +00:00
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_SLT:
|
2007-03-22 20:36:03 +00:00
|
|
|
if (Max.slt(RHSVal))
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getTrue());
|
2007-03-22 20:36:03 +00:00
|
|
|
if (Min.sgt(RHSVal))
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getFalse());
|
2006-12-23 06:05:41 +00:00
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_SGT:
|
2007-03-22 20:36:03 +00:00
|
|
|
if (Min.sgt(RHSVal))
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getTrue());
|
2007-04-09 23:52:13 +00:00
|
|
|
if (Max.sle(RHSVal))
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::getFalse());
|
2006-12-23 06:05:41 +00:00
|
|
|
break;
|
2006-02-12 02:07:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
// Since the RHS is a ConstantInt (CI), if the left hand side is an
|
2006-10-26 06:15:43 +00:00
|
|
|
// instruction, see if that instruction also has constants so that the
|
2006-12-23 06:05:41 +00:00
|
|
|
// instruction can be folded into the icmp
|
2004-05-25 06:32:08 +00:00
|
|
|
if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
|
2007-04-03 17:43:25 +00:00
|
|
|
if (Instruction *Res = visitICmpInstWithInstAndIntCst(I, LHSI, CI))
|
|
|
|
return Res;
|
2002-05-06 16:14:14 +00:00
|
|
|
}
|
|
|
|
|
2007-04-03 17:43:25 +00:00
|
|
|
// Handle icmp with constant (but not simple integer constant) RHS
|
2005-04-23 15:31:55 +00:00
|
|
|
if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
|
|
|
|
if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
|
|
|
|
switch (LHSI->getOpcode()) {
|
2005-05-01 04:42:15 +00:00
|
|
|
case Instruction::GetElementPtr:
|
|
|
|
if (RHSC->isNullValue()) {
|
2006-12-23 06:05:41 +00:00
|
|
|
// icmp pred GEP (P, int 0, int 0, int 0), null -> icmp pred P, null
|
2005-05-01 04:42:15 +00:00
|
|
|
bool isAllZeros = true;
|
|
|
|
for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i)
|
|
|
|
if (!isa<Constant>(LHSI->getOperand(i)) ||
|
|
|
|
!cast<Constant>(LHSI->getOperand(i))->isNullValue()) {
|
|
|
|
isAllZeros = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (isAllZeros)
|
2006-12-23 06:05:41 +00:00
|
|
|
return new ICmpInst(I.getPredicate(), LHSI->getOperand(0),
|
2005-05-01 04:42:15 +00:00
|
|
|
Constant::getNullValue(LHSI->getOperand(0)->getType()));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2005-04-23 15:31:55 +00:00
|
|
|
case Instruction::PHI:
|
|
|
|
if (Instruction *NV = FoldOpIntoPhi(I))
|
|
|
|
return NV;
|
|
|
|
break;
|
2007-04-06 18:57:34 +00:00
|
|
|
case Instruction::Select: {
|
2005-04-23 15:31:55 +00:00
|
|
|
// If either operand of the select is a constant, we can fold the
|
|
|
|
// comparison into the select arms, which will cause one to be
|
|
|
|
// constant folded and the select turned into a bitwise or.
|
|
|
|
Value *Op1 = 0, *Op2 = 0;
|
|
|
|
if (LHSI->hasOneUse()) {
|
|
|
|
if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) {
|
|
|
|
// Fold the known value into the constant operand.
|
2006-12-23 06:05:41 +00:00
|
|
|
Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
|
|
|
|
// Insert a new ICmp of the other select operand.
|
|
|
|
Op2 = InsertNewInstBefore(new ICmpInst(I.getPredicate(),
|
|
|
|
LHSI->getOperand(2), RHSC,
|
|
|
|
I.getName()), I);
|
2005-04-23 15:31:55 +00:00
|
|
|
} else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) {
|
|
|
|
// Fold the known value into the constant operand.
|
2006-12-23 06:05:41 +00:00
|
|
|
Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
|
|
|
|
// Insert a new ICmp of the other select operand.
|
|
|
|
Op1 = InsertNewInstBefore(new ICmpInst(I.getPredicate(),
|
|
|
|
LHSI->getOperand(1), RHSC,
|
|
|
|
I.getName()), I);
|
2005-04-23 15:31:55 +00:00
|
|
|
}
|
|
|
|
}
|
2005-04-23 21:38:35 +00:00
|
|
|
|
2005-04-23 15:31:55 +00:00
|
|
|
if (Op1)
|
2008-04-06 20:25:17 +00:00
|
|
|
return SelectInst::Create(LHSI->getOperand(0), Op1, Op2);
|
2005-04-23 15:31:55 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-04-06 18:57:34 +00:00
|
|
|
case Instruction::Malloc:
|
|
|
|
// If we have (malloc != null), and if the malloc has a single use, we
|
|
|
|
// can assume it is successful and remove the malloc.
|
|
|
|
if (LHSI->hasOneUse() && isa<ConstantPointerNull>(RHSC)) {
|
|
|
|
AddToWorkList(LHSI);
|
|
|
|
return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty,
|
|
|
|
!isTrueWhenEqual(I)));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2005-04-23 15:31:55 +00:00
|
|
|
}
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
// If we can optimize a 'icmp GEP, P' or 'icmp P, GEP', do so now.
|
2005-01-13 20:14:25 +00:00
|
|
|
if (User *GEP = dyn_castGetElementPtr(Op0))
|
2006-12-23 06:05:41 +00:00
|
|
|
if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I))
|
2005-01-13 20:14:25 +00:00
|
|
|
return NI;
|
|
|
|
if (User *GEP = dyn_castGetElementPtr(Op1))
|
2006-12-23 06:05:41 +00:00
|
|
|
if (Instruction *NI = FoldGEPICmp(GEP, Op0,
|
|
|
|
ICmpInst::getSwappedPredicate(I.getPredicate()), I))
|
2005-01-13 20:14:25 +00:00
|
|
|
return NI;
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
// Test to see if the operands of the icmp are casted versions of other
|
2007-01-06 01:45:59 +00:00
|
|
|
// values. If the ptr->ptr cast can be stripped off both arguments, we do so
|
|
|
|
// now.
|
|
|
|
if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) {
|
|
|
|
if (isa<PointerType>(Op0->getType()) &&
|
|
|
|
(isa<Constant>(Op1) || isa<BitCastInst>(Op1))) {
|
2003-11-03 04:25:02 +00:00
|
|
|
// We keep moving the cast from the left operand over to the right
|
|
|
|
// operand, where it can often be eliminated completely.
|
2007-01-06 01:45:59 +00:00
|
|
|
Op0 = CI->getOperand(0);
|
|
|
|
|
|
|
|
// If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast
|
|
|
|
// so eliminate it as well.
|
|
|
|
if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1))
|
|
|
|
Op1 = CI2->getOperand(0);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2003-11-03 04:25:02 +00:00
|
|
|
// If Op1 is a constant, we can fold the cast into the constant.
|
2008-02-20 11:26:25 +00:00
|
|
|
if (Op0->getType() != Op1->getType()) {
|
2003-11-03 04:25:02 +00:00
|
|
|
if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
|
2006-12-12 23:36:14 +00:00
|
|
|
Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType());
|
2003-11-03 04:25:02 +00:00
|
|
|
} else {
|
2006-12-23 06:05:41 +00:00
|
|
|
// Otherwise, cast the RHS right before the icmp
|
2008-01-13 22:23:22 +00:00
|
|
|
Op1 = InsertBitCastBefore(Op1, Op0->getType(), I);
|
2003-11-03 04:25:02 +00:00
|
|
|
}
|
2008-02-20 11:26:25 +00:00
|
|
|
}
|
2006-12-23 06:05:41 +00:00
|
|
|
return new ICmpInst(I.getPredicate(), Op0, Op1);
|
2003-11-03 04:25:02 +00:00
|
|
|
}
|
2007-01-06 01:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isa<CastInst>(Op0)) {
|
2006-12-23 06:05:41 +00:00
|
|
|
// Handle the special case of: icmp (cast bool to X), <cst>
|
2003-11-03 05:17:03 +00:00
|
|
|
// This comes up when you have code like
|
|
|
|
// int X = A < B;
|
|
|
|
// if (X) ...
|
|
|
|
// For generality, we handle any zero-extension of any operand comparison
|
2005-04-24 06:59:08 +00:00
|
|
|
// with a constant or another cast from the same type.
|
|
|
|
if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1))
|
2006-12-23 06:05:41 +00:00
|
|
|
if (Instruction *R = visitICmpInstWithCastAndCast(I))
|
2005-04-24 06:59:08 +00:00
|
|
|
return R;
|
2003-11-03 05:17:03 +00:00
|
|
|
}
|
2006-02-27 01:44:11 +00:00
|
|
|
|
2006-09-18 04:22:48 +00:00
|
|
|
if (I.isEquality()) {
|
2007-01-05 03:04:57 +00:00
|
|
|
Value *A, *B, *C, *D;
|
|
|
|
if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
|
|
|
|
if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0
|
|
|
|
Value *OtherVal = A == Op1 ? B : A;
|
|
|
|
return new ICmpInst(I.getPredicate(), OtherVal,
|
|
|
|
Constant::getNullValue(A->getType()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) {
|
|
|
|
// A^c1 == C^c2 --> A == C^(c1^c2)
|
|
|
|
if (ConstantInt *C1 = dyn_cast<ConstantInt>(B))
|
|
|
|
if (ConstantInt *C2 = dyn_cast<ConstantInt>(D))
|
|
|
|
if (Op1->hasOneUse()) {
|
2007-04-02 13:45:30 +00:00
|
|
|
Constant *NC = ConstantInt::get(C1->getValue() ^ C2->getValue());
|
2007-01-05 03:04:57 +00:00
|
|
|
Instruction *Xor = BinaryOperator::createXor(C, NC, "tmp");
|
|
|
|
return new ICmpInst(I.getPredicate(), A,
|
|
|
|
InsertNewInstBefore(Xor, I));
|
|
|
|
}
|
|
|
|
|
|
|
|
// A^B == A^D -> B == D
|
|
|
|
if (A == C) return new ICmpInst(I.getPredicate(), B, D);
|
|
|
|
if (A == D) return new ICmpInst(I.getPredicate(), B, C);
|
|
|
|
if (B == C) return new ICmpInst(I.getPredicate(), A, D);
|
|
|
|
if (B == D) return new ICmpInst(I.getPredicate(), A, C);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match(Op1, m_Xor(m_Value(A), m_Value(B))) &&
|
|
|
|
(A == Op0 || B == Op0)) {
|
2006-02-27 01:44:11 +00:00
|
|
|
// A == (A^B) -> B == 0
|
|
|
|
Value *OtherVal = A == Op0 ? B : A;
|
2006-12-23 06:05:41 +00:00
|
|
|
return new ICmpInst(I.getPredicate(), OtherVal,
|
|
|
|
Constant::getNullValue(A->getType()));
|
2007-01-05 03:04:57 +00:00
|
|
|
}
|
|
|
|
if (match(Op0, m_Sub(m_Value(A), m_Value(B))) && A == Op1) {
|
2006-02-27 01:44:11 +00:00
|
|
|
// (A-B) == A -> B == 0
|
2006-12-23 06:05:41 +00:00
|
|
|
return new ICmpInst(I.getPredicate(), B,
|
|
|
|
Constant::getNullValue(B->getType()));
|
2007-01-05 03:04:57 +00:00
|
|
|
}
|
|
|
|
if (match(Op1, m_Sub(m_Value(A), m_Value(B))) && A == Op0) {
|
2006-02-27 01:44:11 +00:00
|
|
|
// A == (A-B) -> B == 0
|
2006-12-23 06:05:41 +00:00
|
|
|
return new ICmpInst(I.getPredicate(), B,
|
|
|
|
Constant::getNullValue(B->getType()));
|
2006-02-27 01:44:11 +00:00
|
|
|
}
|
implement InstCombine/and-compare.ll:test1. This compiles:
typedef struct { unsigned prefix : 4; unsigned code : 4; unsigned unsigned_p : 4; } tree_common;
int foo(tree_common *a, tree_common *b) { return a->code == b->code; }
into:
_foo:
movl 4(%esp), %eax
movl 8(%esp), %ecx
movl (%eax), %eax
xorl (%ecx), %eax
# TRUNCATE movb %al, %al
shrb $4, %al
testb %al, %al
sete %al
movzbl %al, %eax
ret
instead of:
_foo:
movl 8(%esp), %eax
movb (%eax), %al
shrb $4, %al
movl 4(%esp), %ecx
movb (%ecx), %cl
shrb $4, %cl
cmpb %al, %cl
sete %al
movzbl %al, %eax
ret
saving one cycle by eliminating a shift.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@31727 91177308-0d34-0410-b5e6-96231b3b80d8
2006-11-14 06:06:06 +00:00
|
|
|
|
|
|
|
// (X&Z) == (Y&Z) -> (X^Y) & Z == 0
|
|
|
|
if (Op0->hasOneUse() && Op1->hasOneUse() &&
|
|
|
|
match(Op0, m_And(m_Value(A), m_Value(B))) &&
|
|
|
|
match(Op1, m_And(m_Value(C), m_Value(D)))) {
|
|
|
|
Value *X = 0, *Y = 0, *Z = 0;
|
|
|
|
|
|
|
|
if (A == C) {
|
|
|
|
X = B; Y = D; Z = A;
|
|
|
|
} else if (A == D) {
|
|
|
|
X = B; Y = C; Z = A;
|
|
|
|
} else if (B == C) {
|
|
|
|
X = A; Y = D; Z = B;
|
|
|
|
} else if (B == D) {
|
|
|
|
X = A; Y = C; Z = B;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (X) { // Build (X^Y) & Z
|
|
|
|
Op1 = InsertNewInstBefore(BinaryOperator::createXor(X, Y, "tmp"), I);
|
|
|
|
Op1 = InsertNewInstBefore(BinaryOperator::createAnd(Op1, Z, "tmp"), I);
|
|
|
|
I.setOperand(0, Op1);
|
|
|
|
I.setOperand(1, Constant::getNullValue(Op1->getType()));
|
|
|
|
return &I;
|
|
|
|
}
|
|
|
|
}
|
2006-02-27 01:44:11 +00:00
|
|
|
}
|
2002-06-25 16:13:24 +00:00
|
|
|
return Changed ? &I : 0;
|
2002-05-06 16:14:14 +00:00
|
|
|
}
|
|
|
|
|
2007-06-20 23:46:26 +00:00
|
|
|
|
|
|
|
/// FoldICmpDivCst - Fold "icmp pred, ([su]div X, DivRHS), CmpRHS" where DivRHS
|
|
|
|
/// and CmpRHS are both known to be integer constants.
|
|
|
|
Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
|
|
|
|
ConstantInt *DivRHS) {
|
|
|
|
ConstantInt *CmpRHS = cast<ConstantInt>(ICI.getOperand(1));
|
|
|
|
const APInt &CmpRHSV = CmpRHS->getValue();
|
|
|
|
|
|
|
|
// FIXME: If the operand types don't match the type of the divide
|
|
|
|
// then don't attempt this transform. The code below doesn't have the
|
|
|
|
// logic to deal with a signed divide and an unsigned compare (and
|
|
|
|
// vice versa). This is because (x /s C1) <s C2 produces different
|
|
|
|
// results than (x /s C1) <u C2 or (x /u C1) <s C2 or even
|
|
|
|
// (x /u C1) <u C2. Simply casting the operands and result won't
|
|
|
|
// work. :( The if statement below tests that condition and bails
|
|
|
|
// if it finds it.
|
|
|
|
bool DivIsSigned = DivI->getOpcode() == Instruction::SDiv;
|
|
|
|
if (!ICI.isEquality() && DivIsSigned != ICI.isSignedPredicate())
|
|
|
|
return 0;
|
|
|
|
if (DivRHS->isZero())
|
2007-06-21 18:11:19 +00:00
|
|
|
return 0; // The ProdOV computation fails on divide by zero.
|
2007-06-20 23:46:26 +00:00
|
|
|
|
|
|
|
// Compute Prod = CI * DivRHS. We are essentially solving an equation
|
|
|
|
// of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and
|
|
|
|
// C2 (CI). By solving for X we can turn this into a range check
|
|
|
|
// instead of computing a divide.
|
|
|
|
ConstantInt *Prod = Multiply(CmpRHS, DivRHS);
|
|
|
|
|
|
|
|
// Determine if the product overflows by seeing if the product is
|
|
|
|
// not equal to the divide. Make sure we do the same kind of divide
|
|
|
|
// as in the LHS instruction that we're folding.
|
|
|
|
bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) :
|
|
|
|
ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS;
|
|
|
|
|
|
|
|
// Get the ICmp opcode
|
2007-06-21 18:11:19 +00:00
|
|
|
ICmpInst::Predicate Pred = ICI.getPredicate();
|
|
|
|
|
|
|
|
// Figure out the interval that is being checked. For example, a comparison
|
|
|
|
// like "X /u 5 == 0" is really checking that X is in the interval [0, 5).
|
|
|
|
// Compute this interval based on the constants involved and the signedness of
|
|
|
|
// the compare/divide. This computes a half-open interval, keeping track of
|
|
|
|
// whether either value in the interval overflows. After analysis each
|
|
|
|
// overflow variable is set to 0 if it's corresponding bound variable is valid
|
|
|
|
// -1 if overflowed off the bottom end, or +1 if overflowed off the top end.
|
|
|
|
int LoOverflow = 0, HiOverflow = 0;
|
|
|
|
ConstantInt *LoBound = 0, *HiBound = 0;
|
|
|
|
|
|
|
|
|
2007-06-20 23:46:26 +00:00
|
|
|
if (!DivIsSigned) { // udiv
|
2007-06-21 18:11:19 +00:00
|
|
|
// e.g. X/5 op 3 --> [15, 20)
|
2007-06-20 23:46:26 +00:00
|
|
|
LoBound = Prod;
|
2007-06-21 18:11:19 +00:00
|
|
|
HiOverflow = LoOverflow = ProdOV;
|
|
|
|
if (!HiOverflow)
|
|
|
|
HiOverflow = AddWithOverflow(HiBound, LoBound, DivRHS, false);
|
2008-02-13 22:09:18 +00:00
|
|
|
} else if (DivRHS->getValue().isStrictlyPositive()) { // Divisor is > 0.
|
2007-06-20 23:46:26 +00:00
|
|
|
if (CmpRHSV == 0) { // (X / pos) op 0
|
2007-06-21 18:11:19 +00:00
|
|
|
// Can't overflow. e.g. X/2 op 0 --> [-1, 2)
|
2007-06-20 23:46:26 +00:00
|
|
|
LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS)));
|
|
|
|
HiBound = DivRHS;
|
2008-02-13 22:09:18 +00:00
|
|
|
} else if (CmpRHSV.isStrictlyPositive()) { // (X / pos) op pos
|
2007-06-21 18:11:19 +00:00
|
|
|
LoBound = Prod; // e.g. X/5 op 3 --> [15, 20)
|
|
|
|
HiOverflow = LoOverflow = ProdOV;
|
|
|
|
if (!HiOverflow)
|
|
|
|
HiOverflow = AddWithOverflow(HiBound, Prod, DivRHS, true);
|
2007-06-20 23:46:26 +00:00
|
|
|
} else { // (X / pos) op neg
|
2007-06-21 18:11:19 +00:00
|
|
|
// e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14)
|
2007-06-20 23:46:26 +00:00
|
|
|
Constant *DivRHSH = ConstantExpr::getNeg(SubOne(DivRHS));
|
|
|
|
LoOverflow = AddWithOverflow(LoBound, Prod,
|
2007-06-21 18:11:19 +00:00
|
|
|
cast<ConstantInt>(DivRHSH), true) ? -1 : 0;
|
2007-06-20 23:46:26 +00:00
|
|
|
HiBound = AddOne(Prod);
|
2007-06-21 18:11:19 +00:00
|
|
|
HiOverflow = ProdOV ? -1 : 0;
|
2007-06-20 23:46:26 +00:00
|
|
|
}
|
2008-02-13 22:09:18 +00:00
|
|
|
} else if (DivRHS->getValue().isNegative()) { // Divisor is < 0.
|
2007-06-20 23:46:26 +00:00
|
|
|
if (CmpRHSV == 0) { // (X / neg) op 0
|
2007-06-21 18:11:19 +00:00
|
|
|
// e.g. X/-5 op 0 --> [-4, 5)
|
2007-06-20 23:46:26 +00:00
|
|
|
LoBound = AddOne(DivRHS);
|
|
|
|
HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS));
|
2007-06-21 18:11:19 +00:00
|
|
|
if (HiBound == DivRHS) { // -INTMIN = INTMIN
|
|
|
|
HiOverflow = 1; // [INTMIN+1, overflow)
|
|
|
|
HiBound = 0; // e.g. X/INTMIN = 0 --> X > INTMIN
|
|
|
|
}
|
2008-02-13 22:09:18 +00:00
|
|
|
} else if (CmpRHSV.isStrictlyPositive()) { // (X / neg) op pos
|
2007-06-21 18:11:19 +00:00
|
|
|
// e.g. X/-5 op 3 --> [-19, -14)
|
|
|
|
HiOverflow = LoOverflow = ProdOV ? -1 : 0;
|
2007-06-20 23:46:26 +00:00
|
|
|
if (!LoOverflow)
|
2007-06-21 18:11:19 +00:00
|
|
|
LoOverflow = AddWithOverflow(LoBound, Prod, AddOne(DivRHS), true) ?-1:0;
|
2007-06-20 23:46:26 +00:00
|
|
|
HiBound = AddOne(Prod);
|
|
|
|
} else { // (X / neg) op neg
|
2007-06-21 18:11:19 +00:00
|
|
|
// e.g. X/-5 op -3 --> [15, 20)
|
2007-06-20 23:46:26 +00:00
|
|
|
LoBound = Prod;
|
2007-06-21 18:11:19 +00:00
|
|
|
LoOverflow = HiOverflow = ProdOV ? 1 : 0;
|
2007-06-20 23:46:26 +00:00
|
|
|
HiBound = Subtract(Prod, DivRHS);
|
|
|
|
}
|
|
|
|
|
2007-06-21 18:11:19 +00:00
|
|
|
// Dividing by a negative swaps the condition. LT <-> GT
|
|
|
|
Pred = ICmpInst::getSwappedPredicate(Pred);
|
2007-06-20 23:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Value *X = DivI->getOperand(0);
|
2007-06-21 18:11:19 +00:00
|
|
|
switch (Pred) {
|
2007-06-20 23:46:26 +00:00
|
|
|
default: assert(0 && "Unhandled icmp opcode!");
|
|
|
|
case ICmpInst::ICMP_EQ:
|
|
|
|
if (LoOverflow && HiOverflow)
|
|
|
|
return ReplaceInstUsesWith(ICI, ConstantInt::getFalse());
|
|
|
|
else if (HiOverflow)
|
2007-06-21 18:11:19 +00:00
|
|
|
return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE :
|
2007-06-20 23:46:26 +00:00
|
|
|
ICmpInst::ICMP_UGE, X, LoBound);
|
|
|
|
else if (LoOverflow)
|
|
|
|
return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT :
|
|
|
|
ICmpInst::ICMP_ULT, X, HiBound);
|
|
|
|
else
|
2007-06-21 18:11:19 +00:00
|
|
|
return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, true, ICI);
|
2007-06-20 23:46:26 +00:00
|
|
|
case ICmpInst::ICMP_NE:
|
|
|
|
if (LoOverflow && HiOverflow)
|
|
|
|
return ReplaceInstUsesWith(ICI, ConstantInt::getTrue());
|
|
|
|
else if (HiOverflow)
|
2007-06-21 18:11:19 +00:00
|
|
|
return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT :
|
2007-06-20 23:46:26 +00:00
|
|
|
ICmpInst::ICMP_ULT, X, LoBound);
|
|
|
|
else if (LoOverflow)
|
|
|
|
return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE :
|
|
|
|
ICmpInst::ICMP_UGE, X, HiBound);
|
|
|
|
else
|
2007-06-21 18:11:19 +00:00
|
|
|
return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, false, ICI);
|
2007-06-20 23:46:26 +00:00
|
|
|
case ICmpInst::ICMP_ULT:
|
|
|
|
case ICmpInst::ICMP_SLT:
|
2007-06-21 18:11:19 +00:00
|
|
|
if (LoOverflow == +1) // Low bound is greater than input range.
|
|
|
|
return ReplaceInstUsesWith(ICI, ConstantInt::getTrue());
|
|
|
|
if (LoOverflow == -1) // Low bound is less than input range.
|
2007-06-20 23:46:26 +00:00
|
|
|
return ReplaceInstUsesWith(ICI, ConstantInt::getFalse());
|
2007-06-21 18:11:19 +00:00
|
|
|
return new ICmpInst(Pred, X, LoBound);
|
2007-06-20 23:46:26 +00:00
|
|
|
case ICmpInst::ICMP_UGT:
|
|
|
|
case ICmpInst::ICMP_SGT:
|
2007-06-21 18:11:19 +00:00
|
|
|
if (HiOverflow == +1) // High bound greater than input range.
|
2007-06-20 23:46:26 +00:00
|
|
|
return ReplaceInstUsesWith(ICI, ConstantInt::getFalse());
|
2007-06-21 18:11:19 +00:00
|
|
|
else if (HiOverflow == -1) // High bound less than input range.
|
|
|
|
return ReplaceInstUsesWith(ICI, ConstantInt::getTrue());
|
|
|
|
if (Pred == ICmpInst::ICMP_UGT)
|
2007-06-20 23:46:26 +00:00
|
|
|
return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound);
|
|
|
|
else
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-03 17:43:25 +00:00
|
|
|
/// visitICmpInstWithInstAndIntCst - Handle "icmp (instr, intcst)".
|
|
|
|
///
|
|
|
|
Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
|
|
|
|
Instruction *LHSI,
|
|
|
|
ConstantInt *RHS) {
|
|
|
|
const APInt &RHSV = RHS->getValue();
|
|
|
|
|
|
|
|
switch (LHSI->getOpcode()) {
|
2007-04-04 06:42:45 +00:00
|
|
|
case Instruction::Xor: // (icmp pred (xor X, XorCST), CI)
|
2007-04-03 17:43:25 +00:00
|
|
|
if (ConstantInt *XorCST = dyn_cast<ConstantInt>(LHSI->getOperand(1))) {
|
|
|
|
// If this is a comparison that tests the signbit (X < 0) or (x > -1),
|
|
|
|
// fold the xor.
|
2008-02-20 11:26:25 +00:00
|
|
|
if ((ICI.getPredicate() == ICmpInst::ICMP_SLT && RHSV == 0) ||
|
|
|
|
(ICI.getPredicate() == ICmpInst::ICMP_SGT && RHSV.isAllOnesValue())) {
|
2007-04-03 17:43:25 +00:00
|
|
|
Value *CompareVal = LHSI->getOperand(0);
|
|
|
|
|
|
|
|
// If the sign bit of the XorCST is not set, there is no change to
|
|
|
|
// the operation, just stop using the Xor.
|
|
|
|
if (!XorCST->getValue().isNegative()) {
|
|
|
|
ICI.setOperand(0, CompareVal);
|
|
|
|
AddToWorkList(LHSI);
|
|
|
|
return &ICI;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Was the old condition true if the operand is positive?
|
|
|
|
bool isTrueIfPositive = ICI.getPredicate() == ICmpInst::ICMP_SGT;
|
|
|
|
|
|
|
|
// If so, the new one isn't.
|
|
|
|
isTrueIfPositive ^= true;
|
|
|
|
|
|
|
|
if (isTrueIfPositive)
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_SGT, CompareVal, SubOne(RHS));
|
|
|
|
else
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_SLT, CompareVal, AddOne(RHS));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Instruction::And: // (icmp pred (and X, AndCST), RHS)
|
|
|
|
if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) &&
|
|
|
|
LHSI->getOperand(0)->hasOneUse()) {
|
|
|
|
ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1));
|
|
|
|
|
|
|
|
// If the LHS is an AND of a truncating cast, we can widen the
|
|
|
|
// and/compare to be the input width without changing the value
|
|
|
|
// produced, eliminating a cast.
|
|
|
|
if (TruncInst *Cast = dyn_cast<TruncInst>(LHSI->getOperand(0))) {
|
|
|
|
// We can do this transformation if either the AND constant does not
|
|
|
|
// have its sign bit set or if it is an equality comparison.
|
|
|
|
// Extending a relational comparison when we're checking the sign
|
|
|
|
// bit would not work.
|
|
|
|
if (Cast->hasOneUse() &&
|
2008-02-20 12:07:57 +00:00
|
|
|
(ICI.isEquality() ||
|
|
|
|
(AndCST->getValue().isNonNegative() && RHSV.isNonNegative()))) {
|
2007-04-03 17:43:25 +00:00
|
|
|
uint32_t BitWidth =
|
|
|
|
cast<IntegerType>(Cast->getOperand(0)->getType())->getBitWidth();
|
|
|
|
APInt NewCST = AndCST->getValue();
|
|
|
|
NewCST.zext(BitWidth);
|
|
|
|
APInt NewCI = RHSV;
|
|
|
|
NewCI.zext(BitWidth);
|
|
|
|
Instruction *NewAnd =
|
|
|
|
BinaryOperator::createAnd(Cast->getOperand(0),
|
|
|
|
ConstantInt::get(NewCST),LHSI->getName());
|
|
|
|
InsertNewInstBefore(NewAnd, ICI);
|
|
|
|
return new ICmpInst(ICI.getPredicate(), NewAnd,
|
|
|
|
ConstantInt::get(NewCI));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is: (X >> C1) & C2 != C3 (where any shift and any compare
|
|
|
|
// could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This
|
|
|
|
// happens a LOT in code produced by the C front-end, for bitfield
|
|
|
|
// access.
|
|
|
|
BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0));
|
|
|
|
if (Shift && !Shift->isShift())
|
|
|
|
Shift = 0;
|
|
|
|
|
|
|
|
ConstantInt *ShAmt;
|
|
|
|
ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : 0;
|
|
|
|
const Type *Ty = Shift ? Shift->getType() : 0; // Type of the shift.
|
|
|
|
const Type *AndTy = AndCST->getType(); // Type of the and.
|
|
|
|
|
|
|
|
// We can fold this as long as we can't shift unknown bits
|
|
|
|
// into the mask. This can only happen with signed shift
|
|
|
|
// rights, as they sign-extend.
|
|
|
|
if (ShAmt) {
|
|
|
|
bool CanFold = Shift->isLogicalShift();
|
|
|
|
if (!CanFold) {
|
|
|
|
// To test for the bad case of the signed shr, see if any
|
|
|
|
// of the bits shifted in could be tested after the mask.
|
|
|
|
uint32_t TyBits = Ty->getPrimitiveSizeInBits();
|
|
|
|
int ShAmtVal = TyBits - ShAmt->getLimitedValue(TyBits);
|
|
|
|
|
|
|
|
uint32_t BitWidth = AndTy->getPrimitiveSizeInBits();
|
|
|
|
if ((APInt::getHighBitsSet(BitWidth, BitWidth-ShAmtVal) &
|
|
|
|
AndCST->getValue()) == 0)
|
|
|
|
CanFold = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CanFold) {
|
|
|
|
Constant *NewCst;
|
|
|
|
if (Shift->getOpcode() == Instruction::Shl)
|
|
|
|
NewCst = ConstantExpr::getLShr(RHS, ShAmt);
|
|
|
|
else
|
|
|
|
NewCst = ConstantExpr::getShl(RHS, ShAmt);
|
|
|
|
|
|
|
|
// Check to see if we are shifting out any of the bits being
|
|
|
|
// compared.
|
|
|
|
if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != RHS) {
|
|
|
|
// If we shifted bits out, the fold is not going to work out.
|
|
|
|
// As a special case, check to see if this means that the
|
|
|
|
// result is always true or false now.
|
|
|
|
if (ICI.getPredicate() == ICmpInst::ICMP_EQ)
|
|
|
|
return ReplaceInstUsesWith(ICI, ConstantInt::getFalse());
|
|
|
|
if (ICI.getPredicate() == ICmpInst::ICMP_NE)
|
|
|
|
return ReplaceInstUsesWith(ICI, ConstantInt::getTrue());
|
|
|
|
} else {
|
|
|
|
ICI.setOperand(1, NewCst);
|
|
|
|
Constant *NewAndCST;
|
|
|
|
if (Shift->getOpcode() == Instruction::Shl)
|
|
|
|
NewAndCST = ConstantExpr::getLShr(AndCST, ShAmt);
|
|
|
|
else
|
|
|
|
NewAndCST = ConstantExpr::getShl(AndCST, ShAmt);
|
|
|
|
LHSI->setOperand(1, NewAndCST);
|
|
|
|
LHSI->setOperand(0, Shift->getOperand(0));
|
|
|
|
AddToWorkList(Shift); // Shift is dead.
|
|
|
|
AddUsesToWorkList(ICI);
|
|
|
|
return &ICI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Turn ((X >> Y) & C) == 0 into (X & (C << Y)) == 0. The later is
|
|
|
|
// preferable because it allows the C<<Y expression to be hoisted out
|
|
|
|
// of a loop if Y is invariant and X is not.
|
|
|
|
if (Shift && Shift->hasOneUse() && RHSV == 0 &&
|
|
|
|
ICI.isEquality() && !Shift->isArithmeticShift() &&
|
|
|
|
isa<Instruction>(Shift->getOperand(0))) {
|
|
|
|
// Compute C << Y.
|
|
|
|
Value *NS;
|
|
|
|
if (Shift->getOpcode() == Instruction::LShr) {
|
|
|
|
NS = BinaryOperator::createShl(AndCST,
|
|
|
|
Shift->getOperand(1), "tmp");
|
|
|
|
} else {
|
|
|
|
// Insert a logical shift.
|
|
|
|
NS = BinaryOperator::createLShr(AndCST,
|
|
|
|
Shift->getOperand(1), "tmp");
|
|
|
|
}
|
|
|
|
InsertNewInstBefore(cast<Instruction>(NS), ICI);
|
|
|
|
|
|
|
|
// Compute X & (C << Y).
|
|
|
|
Instruction *NewAnd =
|
|
|
|
BinaryOperator::createAnd(Shift->getOperand(0), NS, LHSI->getName());
|
|
|
|
InsertNewInstBefore(NewAnd, ICI);
|
|
|
|
|
|
|
|
ICI.setOperand(0, NewAnd);
|
|
|
|
return &ICI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2007-07-15 20:42:37 +00:00
|
|
|
case Instruction::Shl: { // (icmp pred (shl X, ShAmt), CI)
|
|
|
|
ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1));
|
|
|
|
if (!ShAmt) break;
|
|
|
|
|
|
|
|
uint32_t TypeBits = RHSV.getBitWidth();
|
|
|
|
|
|
|
|
// Check that the shift amount is in range. If not, don't perform
|
|
|
|
// undefined shifts. When the shift is visited it will be
|
|
|
|
// simplified.
|
|
|
|
if (ShAmt->uge(TypeBits))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (ICI.isEquality()) {
|
|
|
|
// If we are comparing against bits always shifted out, the
|
|
|
|
// comparison cannot succeed.
|
|
|
|
Constant *Comp =
|
|
|
|
ConstantExpr::getShl(ConstantExpr::getLShr(RHS, ShAmt), ShAmt);
|
|
|
|
if (Comp != RHS) {// Comparing against a bit that we know is zero.
|
|
|
|
bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
|
|
|
|
Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE);
|
|
|
|
return ReplaceInstUsesWith(ICI, Cst);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LHSI->hasOneUse()) {
|
|
|
|
// Otherwise strength reduce the shift into an and.
|
|
|
|
uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits);
|
|
|
|
Constant *Mask =
|
|
|
|
ConstantInt::get(APInt::getLowBitsSet(TypeBits, TypeBits-ShAmtVal));
|
2007-04-03 17:43:25 +00:00
|
|
|
|
2007-07-15 20:42:37 +00:00
|
|
|
Instruction *AndI =
|
|
|
|
BinaryOperator::createAnd(LHSI->getOperand(0),
|
|
|
|
Mask, LHSI->getName()+".mask");
|
|
|
|
Value *And = InsertNewInstBefore(AndI, ICI);
|
|
|
|
return new ICmpInst(ICI.getPredicate(), And,
|
|
|
|
ConstantInt::get(RHSV.lshr(ShAmtVal)));
|
2007-04-03 17:43:25 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-15 20:42:37 +00:00
|
|
|
|
|
|
|
// Otherwise, if this is a comparison of the sign bit, simplify to and/test.
|
|
|
|
bool TrueIfSigned = false;
|
|
|
|
if (LHSI->hasOneUse() &&
|
|
|
|
isSignBitCheck(ICI.getPredicate(), RHS, TrueIfSigned)) {
|
|
|
|
// (X << 31) <s 0 --> (X&1) != 0
|
|
|
|
Constant *Mask = ConstantInt::get(APInt(TypeBits, 1) <<
|
|
|
|
(TypeBits-ShAmt->getZExtValue()-1));
|
|
|
|
Instruction *AndI =
|
|
|
|
BinaryOperator::createAnd(LHSI->getOperand(0),
|
|
|
|
Mask, LHSI->getName()+".mask");
|
|
|
|
Value *And = InsertNewInstBefore(AndI, ICI);
|
|
|
|
|
|
|
|
return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,
|
|
|
|
And, Constant::getNullValue(And->getType()));
|
|
|
|
}
|
2007-04-03 17:43:25 +00:00
|
|
|
break;
|
2007-07-15 20:42:37 +00:00
|
|
|
}
|
2007-04-03 17:43:25 +00:00
|
|
|
|
|
|
|
case Instruction::LShr: // (icmp pred (shr X, ShAmt), CI)
|
2007-07-15 20:42:37 +00:00
|
|
|
case Instruction::AShr: {
|
2008-03-21 05:19:58 +00:00
|
|
|
// Only handle equality comparisons of shift-by-constant.
|
2007-07-15 20:42:37 +00:00
|
|
|
ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1));
|
2008-03-21 05:19:58 +00:00
|
|
|
if (!ShAmt || !ICI.isEquality()) break;
|
2007-07-15 20:42:37 +00:00
|
|
|
|
2008-03-21 05:19:58 +00:00
|
|
|
// Check that the shift amount is in range. If not, don't perform
|
|
|
|
// undefined shifts. When the shift is visited it will be
|
|
|
|
// simplified.
|
|
|
|
uint32_t TypeBits = RHSV.getBitWidth();
|
|
|
|
if (ShAmt->uge(TypeBits))
|
|
|
|
break;
|
|
|
|
|
|
|
|
uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits);
|
2007-07-15 20:42:37 +00:00
|
|
|
|
2008-03-21 05:19:58 +00:00
|
|
|
// If we are comparing against bits always shifted out, the
|
|
|
|
// comparison cannot succeed.
|
|
|
|
APInt Comp = RHSV << ShAmtVal;
|
|
|
|
if (LHSI->getOpcode() == Instruction::LShr)
|
|
|
|
Comp = Comp.lshr(ShAmtVal);
|
|
|
|
else
|
|
|
|
Comp = Comp.ashr(ShAmtVal);
|
|
|
|
|
|
|
|
if (Comp != RHSV) { // Comparing against a bit that we know is zero.
|
|
|
|
bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
|
|
|
|
Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE);
|
|
|
|
return ReplaceInstUsesWith(ICI, Cst);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, check to see if the bits shifted out are known to be zero.
|
|
|
|
// If so, we can compare against the unshifted value:
|
|
|
|
// (X & 4) >> 1 == 2 --> (X & 4) == 4.
|
2008-04-23 00:38:06 +00:00
|
|
|
if (LHSI->hasOneUse() &&
|
|
|
|
MaskedValueIsZero(LHSI->getOperand(0),
|
2008-03-21 05:19:58 +00:00
|
|
|
APInt::getLowBitsSet(Comp.getBitWidth(), ShAmtVal))) {
|
|
|
|
return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0),
|
|
|
|
ConstantExpr::getShl(RHS, ShAmt));
|
|
|
|
}
|
2007-07-15 20:42:37 +00:00
|
|
|
|
2008-04-23 00:38:06 +00:00
|
|
|
if (LHSI->hasOneUse()) {
|
2008-03-21 05:19:58 +00:00
|
|
|
// Otherwise strength reduce the shift into an and.
|
|
|
|
APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal));
|
|
|
|
Constant *Mask = ConstantInt::get(Val);
|
2007-07-15 20:42:37 +00:00
|
|
|
|
2008-03-21 05:19:58 +00:00
|
|
|
Instruction *AndI =
|
|
|
|
BinaryOperator::createAnd(LHSI->getOperand(0),
|
|
|
|
Mask, LHSI->getName()+".mask");
|
|
|
|
Value *And = InsertNewInstBefore(AndI, ICI);
|
|
|
|
return new ICmpInst(ICI.getPredicate(), And,
|
|
|
|
ConstantExpr::getShl(RHS, ShAmt));
|
2007-04-03 17:43:25 +00:00
|
|
|
}
|
|
|
|
break;
|
2007-07-15 20:42:37 +00:00
|
|
|
}
|
2007-04-03 17:43:25 +00:00
|
|
|
|
|
|
|
case Instruction::SDiv:
|
|
|
|
case Instruction::UDiv:
|
|
|
|
// Fold: icmp pred ([us]div X, C1), C2 -> range test
|
|
|
|
// Fold this div into the comparison, producing a range check.
|
|
|
|
// Determine, based on the divide type, what the range is being
|
|
|
|
// checked. If there is an overflow on the low or high side, remember
|
|
|
|
// it, otherwise compute the range [low, hi) bounding the new value.
|
|
|
|
// See: InsertRangeTest above for the kinds of replacements possible.
|
2007-06-20 23:46:26 +00:00
|
|
|
if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1)))
|
|
|
|
if (Instruction *R = FoldICmpDivCst(ICI, cast<BinaryOperator>(LHSI),
|
|
|
|
DivRHS))
|
|
|
|
return R;
|
2007-04-03 17:43:25 +00:00
|
|
|
break;
|
2008-02-03 16:33:09 +00:00
|
|
|
|
|
|
|
case Instruction::Add:
|
|
|
|
// Fold: icmp pred (add, X, C1), C2
|
|
|
|
|
|
|
|
if (!ICI.isEquality()) {
|
|
|
|
ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(1));
|
|
|
|
if (!LHSC) break;
|
|
|
|
const APInt &LHSV = LHSC->getValue();
|
|
|
|
|
|
|
|
ConstantRange CR = ICI.makeConstantRange(ICI.getPredicate(), RHSV)
|
|
|
|
.subtract(LHSV);
|
|
|
|
|
|
|
|
if (ICI.isSignedPredicate()) {
|
|
|
|
if (CR.getLower().isSignBit()) {
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0),
|
|
|
|
ConstantInt::get(CR.getUpper()));
|
|
|
|
} else if (CR.getUpper().isSignBit()) {
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0),
|
|
|
|
ConstantInt::get(CR.getLower()));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (CR.getLower().isMinValue()) {
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0),
|
|
|
|
ConstantInt::get(CR.getUpper()));
|
|
|
|
} else if (CR.getUpper().isMinValue()) {
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0),
|
|
|
|
ConstantInt::get(CR.getLower()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2007-04-03 17:43:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Simplify icmp_eq and icmp_ne instructions with integer constant RHS.
|
|
|
|
if (ICI.isEquality()) {
|
|
|
|
bool isICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
|
|
|
|
|
|
|
|
// If the first operand is (add|sub|and|or|xor|rem) with a constant, and
|
|
|
|
// the second operand is a constant, simplify a bit.
|
|
|
|
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(LHSI)) {
|
|
|
|
switch (BO->getOpcode()) {
|
|
|
|
case Instruction::SRem:
|
|
|
|
// If we have a signed (X % (2^c)) == 0, turn it into an unsigned one.
|
|
|
|
if (RHSV == 0 && isa<ConstantInt>(BO->getOperand(1)) &&BO->hasOneUse()){
|
|
|
|
const APInt &V = cast<ConstantInt>(BO->getOperand(1))->getValue();
|
|
|
|
if (V.sgt(APInt(V.getBitWidth(), 1)) && V.isPowerOf2()) {
|
|
|
|
Instruction *NewRem =
|
|
|
|
BinaryOperator::createURem(BO->getOperand(0), BO->getOperand(1),
|
|
|
|
BO->getName());
|
|
|
|
InsertNewInstBefore(NewRem, ICI);
|
|
|
|
return new ICmpInst(ICI.getPredicate(), NewRem,
|
|
|
|
Constant::getNullValue(BO->getType()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Instruction::Add:
|
|
|
|
// Replace ((add A, B) != C) with (A != C-B) if B & C are constants.
|
|
|
|
if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) {
|
|
|
|
if (BO->hasOneUse())
|
|
|
|
return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
|
|
|
|
Subtract(RHS, BOp1C));
|
|
|
|
} else if (RHSV == 0) {
|
|
|
|
// Replace ((add A, B) != 0) with (A != -B) if A or B is
|
|
|
|
// efficiently invertible, or if the add has just this one use.
|
|
|
|
Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1);
|
|
|
|
|
|
|
|
if (Value *NegVal = dyn_castNegVal(BOp1))
|
|
|
|
return new ICmpInst(ICI.getPredicate(), BOp0, NegVal);
|
|
|
|
else if (Value *NegVal = dyn_castNegVal(BOp0))
|
|
|
|
return new ICmpInst(ICI.getPredicate(), NegVal, BOp1);
|
|
|
|
else if (BO->hasOneUse()) {
|
|
|
|
Instruction *Neg = BinaryOperator::createNeg(BOp1);
|
|
|
|
InsertNewInstBefore(Neg, ICI);
|
|
|
|
Neg->takeName(BO);
|
|
|
|
return new ICmpInst(ICI.getPredicate(), BOp0, Neg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Instruction::Xor:
|
|
|
|
// For the xor case, we can xor two constants together, eliminating
|
|
|
|
// the explicit xor.
|
|
|
|
if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1)))
|
|
|
|
return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
|
|
|
|
ConstantExpr::getXor(RHS, BOC));
|
|
|
|
|
|
|
|
// FALLTHROUGH
|
|
|
|
case Instruction::Sub:
|
|
|
|
// Replace (([sub|xor] A, B) != 0) with (A != B)
|
|
|
|
if (RHSV == 0)
|
|
|
|
return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
|
|
|
|
BO->getOperand(1));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Instruction::Or:
|
|
|
|
// If bits are being or'd in that are not present in the constant we
|
|
|
|
// are comparing against, then the comparison could never succeed!
|
|
|
|
if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) {
|
|
|
|
Constant *NotCI = ConstantExpr::getNot(RHS);
|
|
|
|
if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue())
|
|
|
|
return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty,
|
|
|
|
isICMP_NE));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Instruction::And:
|
|
|
|
if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) {
|
|
|
|
// If bits are being compared against that are and'd out, then the
|
|
|
|
// comparison can never succeed!
|
|
|
|
if ((RHSV & ~BOC->getValue()) != 0)
|
|
|
|
return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty,
|
|
|
|
isICMP_NE));
|
|
|
|
|
|
|
|
// If we have ((X & C) == C), turn it into ((X & C) != 0).
|
|
|
|
if (RHS == BOC && RHSV.isPowerOf2())
|
|
|
|
return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ :
|
|
|
|
ICmpInst::ICMP_NE, LHSI,
|
|
|
|
Constant::getNullValue(RHS->getType()));
|
|
|
|
|
|
|
|
// Replace (and X, (1 << size(X)-1) != 0) with x s< 0
|
|
|
|
if (isSignBit(BOC)) {
|
|
|
|
Value *X = BO->getOperand(0);
|
|
|
|
Constant *Zero = Constant::getNullValue(X->getType());
|
|
|
|
ICmpInst::Predicate pred = isICMP_NE ?
|
|
|
|
ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE;
|
|
|
|
return new ICmpInst(pred, X, Zero);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ((X & ~7) == 0) --> X < 8
|
|
|
|
if (RHSV == 0 && isHighOnes(BOC)) {
|
|
|
|
Value *X = BO->getOperand(0);
|
|
|
|
Constant *NegX = ConstantExpr::getNeg(BOC);
|
|
|
|
ICmpInst::Predicate pred = isICMP_NE ?
|
|
|
|
ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT;
|
|
|
|
return new ICmpInst(pred, X, NegX);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
} else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(LHSI)) {
|
|
|
|
// Handle icmp {eq|ne} <intrinsic>, intcst.
|
|
|
|
if (II->getIntrinsicID() == Intrinsic::bswap) {
|
|
|
|
AddToWorkList(II);
|
|
|
|
ICI.setOperand(0, II->getOperand(1));
|
|
|
|
ICI.setOperand(1, ConstantInt::get(RHSV.byteSwap()));
|
|
|
|
return &ICI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { // Not a ICMP_EQ/ICMP_NE
|
2007-04-14 23:32:02 +00:00
|
|
|
// If the LHS is a cast from an integral value of the same size,
|
|
|
|
// then since we know the RHS is a constant, try to simlify.
|
2007-04-03 17:43:25 +00:00
|
|
|
if (CastInst *Cast = dyn_cast<CastInst>(LHSI)) {
|
|
|
|
Value *CastOp = Cast->getOperand(0);
|
|
|
|
const Type *SrcTy = CastOp->getType();
|
|
|
|
uint32_t SrcTySize = SrcTy->getPrimitiveSizeInBits();
|
|
|
|
if (SrcTy->isInteger() &&
|
|
|
|
SrcTySize == Cast->getType()->getPrimitiveSizeInBits()) {
|
|
|
|
// If this is an unsigned comparison, try to make the comparison use
|
|
|
|
// smaller constant values.
|
|
|
|
if (ICI.getPredicate() == ICmpInst::ICMP_ULT && RHSV.isSignBit()) {
|
|
|
|
// X u< 128 => X s> -1
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_SGT, CastOp,
|
|
|
|
ConstantInt::get(APInt::getAllOnesValue(SrcTySize)));
|
|
|
|
} else if (ICI.getPredicate() == ICmpInst::ICMP_UGT &&
|
|
|
|
RHSV == APInt::getSignedMaxValue(SrcTySize)) {
|
|
|
|
// X u> 127 => X s< 0
|
|
|
|
return new ICmpInst(ICmpInst::ICMP_SLT, CastOp,
|
|
|
|
Constant::getNullValue(SrcTy));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst).
|
|
|
|
/// We only handle extending casts so far.
|
|
|
|
///
|
2006-12-23 06:05:41 +00:00
|
|
|
Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) {
|
|
|
|
const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0));
|
2006-11-27 01:05:10 +00:00
|
|
|
Value *LHSCIOp = LHSCI->getOperand(0);
|
|
|
|
const Type *SrcTy = LHSCIOp->getType();
|
2006-12-23 06:05:41 +00:00
|
|
|
const Type *DestTy = LHSCI->getType();
|
2005-04-24 06:59:08 +00:00
|
|
|
Value *RHSCIOp;
|
|
|
|
|
2007-05-05 22:41:33 +00:00
|
|
|
// Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
|
|
|
|
// integer type is the same size as the pointer type.
|
|
|
|
if (LHSCI->getOpcode() == Instruction::PtrToInt &&
|
|
|
|
getTargetData().getPointerSizeInBits() ==
|
|
|
|
cast<IntegerType>(DestTy)->getBitWidth()) {
|
|
|
|
Value *RHSOp = 0;
|
|
|
|
if (Constant *RHSC = dyn_cast<Constant>(ICI.getOperand(1))) {
|
2007-05-06 07:24:03 +00:00
|
|
|
RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy);
|
2007-05-05 22:41:33 +00:00
|
|
|
} else if (PtrToIntInst *RHSC = dyn_cast<PtrToIntInst>(ICI.getOperand(1))) {
|
|
|
|
RHSOp = RHSC->getOperand(0);
|
|
|
|
// If the pointer types don't match, insert a bitcast.
|
|
|
|
if (LHSCIOp->getType() != RHSOp->getType())
|
2008-01-13 22:23:22 +00:00
|
|
|
RHSOp = InsertBitCastBefore(RHSOp, LHSCIOp->getType(), ICI);
|
2007-05-05 22:41:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (RHSOp)
|
|
|
|
return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSOp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The code below only handles extension cast instructions, so far.
|
|
|
|
// Enforce this.
|
2006-12-23 06:05:41 +00:00
|
|
|
if (LHSCI->getOpcode() != Instruction::ZExt &&
|
|
|
|
LHSCI->getOpcode() != Instruction::SExt)
|
2005-01-17 03:20:02 +00:00
|
|
|
return 0;
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt;
|
|
|
|
bool isSignedCmp = ICI.isSignedPredicate();
|
2005-04-24 06:59:08 +00:00
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) {
|
2005-04-24 06:59:08 +00:00
|
|
|
// Not an extension from the same type?
|
|
|
|
RHSCIOp = CI->getOperand(0);
|
2006-12-23 06:05:41 +00:00
|
|
|
if (RHSCIOp->getType() != LHSCIOp->getType())
|
|
|
|
return 0;
|
2007-01-13 23:11:38 +00:00
|
|
|
|
2008-01-28 03:48:02 +00:00
|
|
|
// If the signedness of the two casts doesn't agree (i.e. one is a sext
|
2007-01-13 23:11:38 +00:00
|
|
|
// and the other is a zext), then we can't handle this.
|
|
|
|
if (CI->getOpcode() != LHSCI->getOpcode())
|
|
|
|
return 0;
|
|
|
|
|
2008-01-28 03:48:02 +00:00
|
|
|
// Deal with equality cases early.
|
|
|
|
if (ICI.isEquality())
|
|
|
|
return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp);
|
|
|
|
|
|
|
|
// A signed comparison of sign extended values simplifies into a
|
|
|
|
// signed comparison.
|
|
|
|
if (isSignedCmp && isSignedExt)
|
|
|
|
return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp);
|
|
|
|
|
|
|
|
// The other three cases all fold into an unsigned comparison.
|
|
|
|
return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, RHSCIOp);
|
2006-12-23 06:05:41 +00:00
|
|
|
}
|
2005-01-17 03:20:02 +00:00
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
// If we aren't dealing with a constant on the RHS, exit early
|
|
|
|
ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1));
|
|
|
|
if (!CI)
|
2005-04-24 06:59:08 +00:00
|
|
|
return 0;
|
2006-12-23 06:05:41 +00:00
|
|
|
|
|
|
|
// Compute the constant that would happen if we truncated to SrcTy then
|
|
|
|
// reextended to DestTy.
|
|
|
|
Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy);
|
|
|
|
Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(), Res1, DestTy);
|
|
|
|
|
|
|
|
// If the re-extended constant didn't change...
|
|
|
|
if (Res2 == CI) {
|
|
|
|
// Make sure that sign of the Cmp and the sign of the Cast are the same.
|
|
|
|
// For example, we might have:
|
|
|
|
// %A = sext short %X to uint
|
|
|
|
// %B = icmp ugt uint %A, 1330
|
|
|
|
// It is incorrect to transform this into
|
|
|
|
// %B = icmp ugt short %X, 1330
|
|
|
|
// because %A may have negative value.
|
|
|
|
//
|
|
|
|
// However, it is OK if SrcTy is bool (See cast-set.ll testcase)
|
|
|
|
// OR operation is EQ/NE.
|
2007-01-11 18:21:29 +00:00
|
|
|
if (isSignedExt == isSignedCmp || SrcTy == Type::Int1Ty || ICI.isEquality())
|
2006-12-23 06:05:41 +00:00
|
|
|
return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1);
|
|
|
|
else
|
|
|
|
return 0;
|
2004-11-28 21:31:15 +00:00
|
|
|
}
|
2002-05-06 16:14:14 +00:00
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
// The re-extended constant changed so the constant cannot be represented
|
|
|
|
// in the shorter type. Consequently, we cannot emit a simple comparison.
|
|
|
|
|
|
|
|
// First, handle some easy cases. We know the result cannot be equal at this
|
|
|
|
// point so handle the ICI.isEquality() cases
|
|
|
|
if (ICI.getPredicate() == ICmpInst::ICMP_EQ)
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(ICI, ConstantInt::getFalse());
|
2006-12-23 06:05:41 +00:00
|
|
|
if (ICI.getPredicate() == ICmpInst::ICMP_NE)
|
2007-01-11 12:24:14 +00:00
|
|
|
return ReplaceInstUsesWith(ICI, ConstantInt::getTrue());
|
2006-12-23 06:05:41 +00:00
|
|
|
|
|
|
|
// Evaluate the comparison for LT (we invert for GT below). LE and GE cases
|
|
|
|
// should have been folded away previously and not enter in here.
|
|
|
|
Value *Result;
|
|
|
|
if (isSignedCmp) {
|
|
|
|
// We're performing a signed comparison.
|
2007-03-22 20:36:03 +00:00
|
|
|
if (cast<ConstantInt>(CI)->getValue().isNegative())
|
2007-01-11 12:24:14 +00:00
|
|
|
Result = ConstantInt::getFalse(); // X < (small) --> false
|
2006-12-23 06:05:41 +00:00
|
|
|
else
|
2007-01-11 12:24:14 +00:00
|
|
|
Result = ConstantInt::getTrue(); // X < (large) --> true
|
2006-12-23 06:05:41 +00:00
|
|
|
} else {
|
|
|
|
// We're performing an unsigned comparison.
|
|
|
|
if (isSignedExt) {
|
|
|
|
// We're performing an unsigned comp with a sign extended value.
|
|
|
|
// This is true if the input is >= 0. [aka >s -1]
|
2007-01-11 12:24:14 +00:00
|
|
|
Constant *NegOne = ConstantInt::getAllOnesValue(SrcTy);
|
2006-12-23 06:05:41 +00:00
|
|
|
Result = InsertNewInstBefore(new ICmpInst(ICmpInst::ICMP_SGT, LHSCIOp,
|
|
|
|
NegOne, ICI.getName()), ICI);
|
|
|
|
} else {
|
|
|
|
// Unsigned extend & unsigned compare -> always true.
|
2007-01-11 12:24:14 +00:00
|
|
|
Result = ConstantInt::getTrue();
|
2006-12-23 06:05:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, return the value computed.
|
|
|
|
if (ICI.getPredicate() == ICmpInst::ICMP_ULT ||
|
|
|
|
ICI.getPredicate() == ICmpInst::ICMP_SLT) {
|
|
|
|
return ReplaceInstUsesWith(ICI, Result);
|
|
|
|
} else {
|
|
|
|
assert((ICI.getPredicate()==ICmpInst::ICMP_UGT ||
|
|
|
|
ICI.getPredicate()==ICmpInst::ICMP_SGT) &&
|
|
|
|
"ICmp should be folded!");
|
|
|
|
if (Constant *CI = dyn_cast<Constant>(Result))
|
|
|
|
return ReplaceInstUsesWith(ICI, ConstantExpr::getNot(CI));
|
|
|
|
else
|
|
|
|
return BinaryOperator::createNot(Result);
|
|
|
|
}
|
2005-04-24 06:59:08 +00:00
|
|
|
}
|
2002-05-06 16:14:14 +00:00
|
|
|
|
2007-02-02 02:16:23 +00:00
|
|
|
Instruction *InstCombiner::visitShl(BinaryOperator &I) {
|
|
|
|
return commonShiftTransforms(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InstCombiner::visitLShr(BinaryOperator &I) {
|
|
|
|
return commonShiftTransforms(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InstCombiner::visitAShr(BinaryOperator &I) {
|
2007-12-06 01:59:46 +00:00
|
|
|
if (Instruction *R = commonShiftTransforms(I))
|
|
|
|
return R;
|
|
|
|
|
|
|
|
Value *Op0 = I.getOperand(0);
|
|
|
|
|
|
|
|
// ashr int -1, X = -1 (for any arithmetic shift rights of ~0)
|
|
|
|
if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0))
|
|
|
|
if (CSI->isAllOnesValue())
|
|
|
|
return ReplaceInstUsesWith(I, CSI);
|
|
|
|
|
|
|
|
// See if we can turn a signed shr into an unsigned shr.
|
|
|
|
if (MaskedValueIsZero(Op0,
|
|
|
|
APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())))
|
|
|
|
return BinaryOperator::createLShr(Op0, I.getOperand(1));
|
|
|
|
|
|
|
|
return 0;
|
2007-02-02 02:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) {
|
|
|
|
assert(I.getOperand(1)->getType() == I.getOperand(0)->getType());
|
2002-06-25 16:13:24 +00:00
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
2002-05-06 16:14:14 +00:00
|
|
|
|
|
|
|
// shl X, 0 == X and shr X, 0 == X
|
|
|
|
// shl 0, X == 0 and shr 0, X == 0
|
2007-02-02 02:16:23 +00:00
|
|
|
if (Op1 == Constant::getNullValue(Op1->getType()) ||
|
2002-08-12 21:17:25 +00:00
|
|
|
Op0 == Constant::getNullValue(Op0->getType()))
|
|
|
|
return ReplaceInstUsesWith(I, Op0);
|
2006-02-12 08:07:37 +00:00
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
if (isa<UndefValue>(Op0)) {
|
|
|
|
if (I.getOpcode() == Instruction::AShr) // undef >>s X -> undef
|
2004-10-16 23:28:04 +00:00
|
|
|
return ReplaceInstUsesWith(I, Op0);
|
2006-12-23 06:05:41 +00:00
|
|
|
else // undef << X -> 0, undef >>u X -> 0
|
2004-10-16 18:11:37 +00:00
|
|
|
return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
|
|
|
|
}
|
|
|
|
if (isa<UndefValue>(Op1)) {
|
2006-12-23 06:05:41 +00:00
|
|
|
if (I.getOpcode() == Instruction::AShr) // X >>s undef -> X
|
|
|
|
return ReplaceInstUsesWith(I, Op0);
|
|
|
|
else // X << undef, X >>u undef -> 0
|
2004-10-16 18:11:37 +00:00
|
|
|
return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
|
|
|
|
}
|
|
|
|
|
2004-04-09 19:05:30 +00:00
|
|
|
// Try to fold constant and into select arguments.
|
|
|
|
if (isa<Constant>(Op0))
|
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
|
2005-01-01 16:22:27 +00:00
|
|
|
if (Instruction *R = FoldOpIntoSelect(I, SI, this))
|
2004-04-09 19:05:30 +00:00
|
|
|
return R;
|
|
|
|
|
2006-10-20 07:07:24 +00:00
|
|
|
if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1))
|
2006-12-31 05:48:39 +00:00
|
|
|
if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
|
|
|
|
return Res;
|
2006-01-06 07:12:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2006-10-20 07:07:24 +00:00
|
|
|
Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1,
|
2007-02-02 02:16:23 +00:00
|
|
|
BinaryOperator &I) {
|
2006-12-23 06:05:41 +00:00
|
|
|
bool isLeftShift = I.getOpcode() == Instruction::Shl;
|
2003-08-13 04:18:28 +00:00
|
|
|
|
2006-02-12 08:07:37 +00:00
|
|
|
// See if we can simplify any instructions used by the instruction whose sole
|
|
|
|
// purpose is to compute bits we don't care about.
|
2007-03-23 18:46:34 +00:00
|
|
|
uint32_t TypeBits = Op0->getType()->getPrimitiveSizeInBits();
|
|
|
|
APInt KnownZero(TypeBits, 0), KnownOne(TypeBits, 0);
|
|
|
|
if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(TypeBits),
|
2006-02-12 08:07:37 +00:00
|
|
|
KnownZero, KnownOne))
|
|
|
|
return &I;
|
|
|
|
|
2006-01-06 07:12:35 +00:00
|
|
|
// shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr
|
|
|
|
// of a signed value.
|
|
|
|
//
|
2007-03-30 09:29:48 +00:00
|
|
|
if (Op1->uge(TypeBits)) {
|
2007-02-02 05:29:55 +00:00
|
|
|
if (I.getOpcode() != Instruction::AShr)
|
2006-01-06 07:12:35 +00:00
|
|
|
return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType()));
|
|
|
|
else {
|
2007-02-02 05:29:55 +00:00
|
|
|
I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1));
|
2006-01-06 07:12:35 +00:00
|
|
|
return &I;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ((X*C1) << C2) == (X * (C1 << C2))
|
|
|
|
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0))
|
|
|
|
if (BO->getOpcode() == Instruction::Mul && isLeftShift)
|
|
|
|
if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1)))
|
|
|
|
return BinaryOperator::createMul(BO->getOperand(0),
|
|
|
|
ConstantExpr::getShl(BOOp, Op1));
|
|
|
|
|
|
|
|
// Try to fold constant and into select arguments.
|
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
|
|
|
|
if (Instruction *R = FoldOpIntoSelect(I, SI, this))
|
|
|
|
return R;
|
|
|
|
if (isa<PHINode>(Op0))
|
|
|
|
if (Instruction *NV = FoldOpIntoPhi(I))
|
|
|
|
return NV;
|
|
|
|
|
2007-12-22 09:07:47 +00:00
|
|
|
// Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2))
|
|
|
|
if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) {
|
|
|
|
Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0));
|
|
|
|
// If 'shift2' is an ashr, we would have to get the sign bit into a funny
|
|
|
|
// place. Don't try to do this transformation in this case. Also, we
|
|
|
|
// require that the input operand is a shift-by-constant so that we have
|
|
|
|
// confidence that the shifts will get folded together. We could do this
|
|
|
|
// xform in more cases, but it is unlikely to be profitable.
|
|
|
|
if (TrOp && I.isLogicalShift() && TrOp->isShift() &&
|
|
|
|
isa<ConstantInt>(TrOp->getOperand(1))) {
|
|
|
|
// Okay, we'll do this xform. Make the shift of shift.
|
|
|
|
Constant *ShAmt = ConstantExpr::getZExt(Op1, TrOp->getType());
|
|
|
|
Instruction *NSh = BinaryOperator::create(I.getOpcode(), TrOp, ShAmt,
|
|
|
|
I.getName());
|
|
|
|
InsertNewInstBefore(NSh, I); // (shift2 (shift1 & 0x00FF), c2)
|
|
|
|
|
|
|
|
// For logical shifts, the truncation has the effect of making the high
|
|
|
|
// part of the register be zeros. Emulate this by inserting an AND to
|
|
|
|
// clear the top bits as needed. This 'and' will usually be zapped by
|
|
|
|
// other xforms later if dead.
|
|
|
|
unsigned SrcSize = TrOp->getType()->getPrimitiveSizeInBits();
|
|
|
|
unsigned DstSize = TI->getType()->getPrimitiveSizeInBits();
|
|
|
|
APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize));
|
|
|
|
|
|
|
|
// The mask we constructed says what the trunc would do if occurring
|
|
|
|
// between the shifts. We want to know the effect *after* the second
|
|
|
|
// shift. We know that it is a logical shift by a constant, so adjust the
|
|
|
|
// mask as appropriate.
|
|
|
|
if (I.getOpcode() == Instruction::Shl)
|
|
|
|
MaskV <<= Op1->getZExtValue();
|
|
|
|
else {
|
|
|
|
assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift");
|
|
|
|
MaskV = MaskV.lshr(Op1->getZExtValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *And = BinaryOperator::createAnd(NSh, ConstantInt::get(MaskV),
|
|
|
|
TI->getName());
|
|
|
|
InsertNewInstBefore(And, I); // shift1 & 0x00FF
|
|
|
|
|
|
|
|
// Return the value truncated to the interesting size.
|
|
|
|
return new TruncInst(And, I.getType());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-06 07:12:35 +00:00
|
|
|
if (Op0->hasOneUse()) {
|
|
|
|
if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) {
|
|
|
|
// Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
|
|
|
|
Value *V1, *V2;
|
|
|
|
ConstantInt *CC;
|
|
|
|
switch (Op0BO->getOpcode()) {
|
implement shift.ll:test25. This compiles:
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus3 (unsigned int x) {
b.k += x;
}
to:
_plus3:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r3, 0(r2)
rlwinm r4, r3, 0, 0, 14
add r4, r4, r3
rlwimi r4, r3, 0, 15, 31
stw r4, 0(r2)
blr
instead of:
_plus3:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
srwi r5, r4, 17
add r3, r5, r3
slwi r3, r3, 17
rlwimi r3, r4, 0, 15, 31
stw r3, 0(r2)
blr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23381 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 05:12:10 +00:00
|
|
|
default: break;
|
|
|
|
case Instruction::Add:
|
|
|
|
case Instruction::And:
|
|
|
|
case Instruction::Or:
|
2007-02-02 14:41:37 +00:00
|
|
|
case Instruction::Xor: {
|
implement shift.ll:test25. This compiles:
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus3 (unsigned int x) {
b.k += x;
}
to:
_plus3:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r3, 0(r2)
rlwinm r4, r3, 0, 0, 14
add r4, r4, r3
rlwimi r4, r3, 0, 15, 31
stw r4, 0(r2)
blr
instead of:
_plus3:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
srwi r5, r4, 17
add r3, r5, r3
slwi r3, r3, 17
rlwimi r3, r4, 0, 15, 31
stw r3, 0(r2)
blr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23381 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 05:12:10 +00:00
|
|
|
// These operators commute.
|
|
|
|
// Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C)
|
Compile
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) {
b.j += x;
}
to:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
and %ECX, 131008
mov %EDX, DWORD PTR [%ESP + 4]
shl %EDX, 6
add %EDX, %ECX
and %EDX, 131008
and %EAX, -131009
or %EDX, %EAX
mov DWORD PTR [b], %EDX
ret
instead of:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
shr %ECX, 6
and %ECX, 2047
add %ECX, DWORD PTR [%ESP + 4]
shl %ECX, 6
and %ECX, 131008
and %EAX, -131009
or %ECX, %EAX
mov DWORD PTR [b], %ECX
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23385 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 06:30:59 +00:00
|
|
|
if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() &&
|
|
|
|
match(Op0BO->getOperand(1),
|
2006-01-06 07:12:35 +00:00
|
|
|
m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) {
|
2007-02-02 14:08:20 +00:00
|
|
|
Instruction *YS = BinaryOperator::createShl(
|
2006-01-06 07:12:35 +00:00
|
|
|
Op0BO->getOperand(0), Op1,
|
Compile
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) {
b.j += x;
}
to:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
and %ECX, 131008
mov %EDX, DWORD PTR [%ESP + 4]
shl %EDX, 6
add %EDX, %ECX
and %EDX, 131008
and %EAX, -131009
or %EDX, %EAX
mov DWORD PTR [b], %EDX
ret
instead of:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
shr %ECX, 6
and %ECX, 2047
add %ECX, DWORD PTR [%ESP + 4]
shl %ECX, 6
and %ECX, 131008
and %EAX, -131009
or %ECX, %EAX
mov DWORD PTR [b], %ECX
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23385 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 06:30:59 +00:00
|
|
|
Op0BO->getName());
|
|
|
|
InsertNewInstBefore(YS, I); // (Y << C)
|
2006-02-09 07:41:14 +00:00
|
|
|
Instruction *X =
|
|
|
|
BinaryOperator::create(Op0BO->getOpcode(), YS, V1,
|
|
|
|
Op0BO->getOperand(1)->getName());
|
Compile
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) {
b.j += x;
}
to:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
and %ECX, 131008
mov %EDX, DWORD PTR [%ESP + 4]
shl %EDX, 6
add %EDX, %ECX
and %EDX, 131008
and %EAX, -131009
or %EDX, %EAX
mov DWORD PTR [b], %EDX
ret
instead of:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
shr %ECX, 6
and %ECX, 2047
add %ECX, DWORD PTR [%ESP + 4]
shl %ECX, 6
and %ECX, 131008
and %EAX, -131009
or %ECX, %EAX
mov DWORD PTR [b], %ECX
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23385 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 06:30:59 +00:00
|
|
|
InsertNewInstBefore(X, I); // (X + (Y << C))
|
2007-03-30 17:20:39 +00:00
|
|
|
uint32_t Op1Val = Op1->getLimitedValue(TypeBits);
|
2007-03-30 05:45:18 +00:00
|
|
|
return BinaryOperator::createAnd(X, ConstantInt::get(
|
|
|
|
APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val)));
|
Compile
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) {
b.j += x;
}
to:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
and %ECX, 131008
mov %EDX, DWORD PTR [%ESP + 4]
shl %EDX, 6
add %EDX, %ECX
and %EDX, 131008
and %EAX, -131009
or %EDX, %EAX
mov DWORD PTR [b], %EDX
ret
instead of:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
shr %ECX, 6
and %ECX, 2047
add %ECX, DWORD PTR [%ESP + 4]
shl %ECX, 6
and %ECX, 131008
and %EAX, -131009
or %ECX, %EAX
mov DWORD PTR [b], %ECX
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23385 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 06:30:59 +00:00
|
|
|
}
|
2006-01-06 07:12:35 +00:00
|
|
|
|
Compile
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) {
b.j += x;
}
to:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
and %ECX, 131008
mov %EDX, DWORD PTR [%ESP + 4]
shl %EDX, 6
add %EDX, %ECX
and %EDX, 131008
and %EAX, -131009
or %EDX, %EAX
mov DWORD PTR [b], %EDX
ret
instead of:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
shr %ECX, 6
and %ECX, 2047
add %ECX, DWORD PTR [%ESP + 4]
shl %ECX, 6
and %ECX, 131008
and %EAX, -131009
or %ECX, %EAX
mov DWORD PTR [b], %ECX
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23385 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 06:30:59 +00:00
|
|
|
// Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C))
|
2007-02-02 14:41:37 +00:00
|
|
|
Value *Op0BOOp1 = Op0BO->getOperand(1);
|
2007-03-05 00:11:19 +00:00
|
|
|
if (isLeftShift && Op0BOOp1->hasOneUse() &&
|
2007-02-02 14:41:37 +00:00
|
|
|
match(Op0BOOp1,
|
|
|
|
m_And(m_Shr(m_Value(V1), m_Value(V2)),m_ConstantInt(CC))) &&
|
2007-03-05 00:11:19 +00:00
|
|
|
cast<BinaryOperator>(Op0BOOp1)->getOperand(0)->hasOneUse() &&
|
|
|
|
V2 == Op1) {
|
2007-02-02 14:08:20 +00:00
|
|
|
Instruction *YS = BinaryOperator::createShl(
|
2007-02-02 02:16:23 +00:00
|
|
|
Op0BO->getOperand(0), Op1,
|
|
|
|
Op0BO->getName());
|
Compile
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) {
b.j += x;
}
to:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
and %ECX, 131008
mov %EDX, DWORD PTR [%ESP + 4]
shl %EDX, 6
add %EDX, %ECX
and %EDX, 131008
and %EAX, -131009
or %EDX, %EAX
mov DWORD PTR [b], %EDX
ret
instead of:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
shr %ECX, 6
and %ECX, 2047
add %ECX, DWORD PTR [%ESP + 4]
shl %ECX, 6
and %ECX, 131008
and %EAX, -131009
or %ECX, %EAX
mov DWORD PTR [b], %ECX
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23385 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 06:30:59 +00:00
|
|
|
InsertNewInstBefore(YS, I); // (Y << C)
|
|
|
|
Instruction *XM =
|
2006-01-06 07:12:35 +00:00
|
|
|
BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1),
|
Compile
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) {
b.j += x;
}
to:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
and %ECX, 131008
mov %EDX, DWORD PTR [%ESP + 4]
shl %EDX, 6
add %EDX, %ECX
and %EDX, 131008
and %EAX, -131009
or %EDX, %EAX
mov DWORD PTR [b], %EDX
ret
instead of:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
shr %ECX, 6
and %ECX, 2047
add %ECX, DWORD PTR [%ESP + 4]
shl %ECX, 6
and %ECX, 131008
and %EAX, -131009
or %ECX, %EAX
mov DWORD PTR [b], %ECX
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23385 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 06:30:59 +00:00
|
|
|
V1->getName()+".mask");
|
|
|
|
InsertNewInstBefore(XM, I); // X & (CC << C)
|
|
|
|
|
|
|
|
return BinaryOperator::create(Op0BO->getOpcode(), YS, XM);
|
|
|
|
}
|
2007-02-02 14:41:37 +00:00
|
|
|
}
|
2006-01-06 07:12:35 +00:00
|
|
|
|
2007-02-02 14:41:37 +00:00
|
|
|
// FALL THROUGH.
|
|
|
|
case Instruction::Sub: {
|
implement shift.ll:test25. This compiles:
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus3 (unsigned int x) {
b.k += x;
}
to:
_plus3:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r3, 0(r2)
rlwinm r4, r3, 0, 0, 14
add r4, r4, r3
rlwimi r4, r3, 0, 15, 31
stw r4, 0(r2)
blr
instead of:
_plus3:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
srwi r5, r4, 17
add r3, r5, r3
slwi r3, r3, 17
rlwimi r3, r4, 0, 15, 31
stw r3, 0(r2)
blr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23381 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 05:12:10 +00:00
|
|
|
// Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
|
Compile
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) {
b.j += x;
}
to:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
and %ECX, 131008
mov %EDX, DWORD PTR [%ESP + 4]
shl %EDX, 6
add %EDX, %ECX
and %EDX, 131008
and %EAX, -131009
or %EDX, %EAX
mov DWORD PTR [b], %EDX
ret
instead of:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
shr %ECX, 6
and %ECX, 2047
add %ECX, DWORD PTR [%ESP + 4]
shl %ECX, 6
and %ECX, 131008
and %EAX, -131009
or %ECX, %EAX
mov DWORD PTR [b], %ECX
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23385 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 06:30:59 +00:00
|
|
|
if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
|
|
|
|
match(Op0BO->getOperand(0),
|
2006-01-06 07:12:35 +00:00
|
|
|
m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) {
|
2007-02-02 14:08:20 +00:00
|
|
|
Instruction *YS = BinaryOperator::createShl(
|
2007-02-02 02:16:23 +00:00
|
|
|
Op0BO->getOperand(1), Op1,
|
|
|
|
Op0BO->getName());
|
Compile
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) {
b.j += x;
}
to:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
and %ECX, 131008
mov %EDX, DWORD PTR [%ESP + 4]
shl %EDX, 6
add %EDX, %ECX
and %EDX, 131008
and %EAX, -131009
or %EDX, %EAX
mov DWORD PTR [b], %EDX
ret
instead of:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
shr %ECX, 6
and %ECX, 2047
add %ECX, DWORD PTR [%ESP + 4]
shl %ECX, 6
and %ECX, 131008
and %EAX, -131009
or %ECX, %EAX
mov DWORD PTR [b], %ECX
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23385 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 06:30:59 +00:00
|
|
|
InsertNewInstBefore(YS, I); // (Y << C)
|
2006-02-09 07:41:14 +00:00
|
|
|
Instruction *X =
|
2006-05-31 21:14:00 +00:00
|
|
|
BinaryOperator::create(Op0BO->getOpcode(), V1, YS,
|
2006-02-09 07:41:14 +00:00
|
|
|
Op0BO->getOperand(0)->getName());
|
Compile
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) {
b.j += x;
}
to:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
and %ECX, 131008
mov %EDX, DWORD PTR [%ESP + 4]
shl %EDX, 6
add %EDX, %ECX
and %EDX, 131008
and %EAX, -131009
or %EDX, %EAX
mov DWORD PTR [b], %EDX
ret
instead of:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
shr %ECX, 6
and %ECX, 2047
add %ECX, DWORD PTR [%ESP + 4]
shl %ECX, 6
and %ECX, 131008
and %EAX, -131009
or %ECX, %EAX
mov DWORD PTR [b], %ECX
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23385 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 06:30:59 +00:00
|
|
|
InsertNewInstBefore(X, I); // (X + (Y << C))
|
2007-03-30 17:20:39 +00:00
|
|
|
uint32_t Op1Val = Op1->getLimitedValue(TypeBits);
|
2007-03-30 05:45:18 +00:00
|
|
|
return BinaryOperator::createAnd(X, ConstantInt::get(
|
|
|
|
APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val)));
|
Compile
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) {
b.j += x;
}
to:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
and %ECX, 131008
mov %EDX, DWORD PTR [%ESP + 4]
shl %EDX, 6
add %EDX, %ECX
and %EDX, 131008
and %EAX, -131009
or %EDX, %EAX
mov DWORD PTR [b], %EDX
ret
instead of:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
shr %ECX, 6
and %ECX, 2047
add %ECX, DWORD PTR [%ESP + 4]
shl %ECX, 6
and %ECX, 131008
and %EAX, -131009
or %ECX, %EAX
mov DWORD PTR [b], %ECX
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23385 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 06:30:59 +00:00
|
|
|
}
|
2006-01-06 07:12:35 +00:00
|
|
|
|
2006-05-31 21:14:00 +00:00
|
|
|
// Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C)
|
Compile
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) {
b.j += x;
}
to:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
and %ECX, 131008
mov %EDX, DWORD PTR [%ESP + 4]
shl %EDX, 6
add %EDX, %ECX
and %EDX, 131008
and %EAX, -131009
or %EDX, %EAX
mov DWORD PTR [b], %EDX
ret
instead of:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
shr %ECX, 6
and %ECX, 2047
add %ECX, DWORD PTR [%ESP + 4]
shl %ECX, 6
and %ECX, 131008
and %EAX, -131009
or %ECX, %EAX
mov DWORD PTR [b], %ECX
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23385 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 06:30:59 +00:00
|
|
|
if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
|
|
|
|
match(Op0BO->getOperand(0),
|
|
|
|
m_And(m_Shr(m_Value(V1), m_Value(V2)),
|
2006-01-06 07:12:35 +00:00
|
|
|
m_ConstantInt(CC))) && V2 == Op1 &&
|
2006-02-09 07:41:14 +00:00
|
|
|
cast<BinaryOperator>(Op0BO->getOperand(0))
|
|
|
|
->getOperand(0)->hasOneUse()) {
|
2007-02-02 14:08:20 +00:00
|
|
|
Instruction *YS = BinaryOperator::createShl(
|
2007-02-02 02:16:23 +00:00
|
|
|
Op0BO->getOperand(1), Op1,
|
|
|
|
Op0BO->getName());
|
Compile
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) {
b.j += x;
}
to:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
and %ECX, 131008
mov %EDX, DWORD PTR [%ESP + 4]
shl %EDX, 6
add %EDX, %ECX
and %EDX, 131008
and %EAX, -131009
or %EDX, %EAX
mov DWORD PTR [b], %EDX
ret
instead of:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
shr %ECX, 6
and %ECX, 2047
add %ECX, DWORD PTR [%ESP + 4]
shl %ECX, 6
and %ECX, 131008
and %EAX, -131009
or %ECX, %EAX
mov DWORD PTR [b], %ECX
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23385 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 06:30:59 +00:00
|
|
|
InsertNewInstBefore(YS, I); // (Y << C)
|
|
|
|
Instruction *XM =
|
2006-01-06 07:12:35 +00:00
|
|
|
BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1),
|
Compile
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) {
b.j += x;
}
to:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
and %ECX, 131008
mov %EDX, DWORD PTR [%ESP + 4]
shl %EDX, 6
add %EDX, %ECX
and %EDX, 131008
and %EAX, -131009
or %EDX, %EAX
mov DWORD PTR [b], %EDX
ret
instead of:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
shr %ECX, 6
and %ECX, 2047
add %ECX, DWORD PTR [%ESP + 4]
shl %ECX, 6
and %ECX, 131008
and %EAX, -131009
or %ECX, %EAX
mov DWORD PTR [b], %ECX
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23385 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 06:30:59 +00:00
|
|
|
V1->getName()+".mask");
|
|
|
|
InsertNewInstBefore(XM, I); // X & (CC << C)
|
|
|
|
|
2006-05-31 21:14:00 +00:00
|
|
|
return BinaryOperator::create(Op0BO->getOpcode(), XM, YS);
|
Compile
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus2 (unsigned int x) {
b.j += x;
}
to:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
and %ECX, 131008
mov %EDX, DWORD PTR [%ESP + 4]
shl %EDX, 6
add %EDX, %ECX
and %EDX, 131008
and %EAX, -131009
or %EDX, %EAX
mov DWORD PTR [b], %EDX
ret
instead of:
plus2:
mov %EAX, DWORD PTR [b]
mov %ECX, %EAX
shr %ECX, 6
and %ECX, 2047
add %ECX, DWORD PTR [%ESP + 4]
shl %ECX, 6
and %ECX, 131008
and %EAX, -131009
or %ECX, %EAX
mov DWORD PTR [b], %ECX
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23385 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 06:30:59 +00:00
|
|
|
}
|
2006-01-06 07:12:35 +00:00
|
|
|
|
implement shift.ll:test25. This compiles:
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus3 (unsigned int x) {
b.k += x;
}
to:
_plus3:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r3, 0(r2)
rlwinm r4, r3, 0, 0, 14
add r4, r4, r3
rlwimi r4, r3, 0, 15, 31
stw r4, 0(r2)
blr
instead of:
_plus3:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
srwi r5, r4, 17
add r3, r5, r3
slwi r3, r3, 17
rlwimi r3, r4, 0, 15, 31
stw r3, 0(r2)
blr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23381 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 05:12:10 +00:00
|
|
|
break;
|
2007-02-02 14:41:37 +00:00
|
|
|
}
|
2006-01-06 07:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If the operand is an bitwise operator with a constant RHS, and the
|
|
|
|
// shift is the only use, we can pull it out of the shift.
|
|
|
|
if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) {
|
|
|
|
bool isValid = true; // Valid only for And, Or, Xor
|
|
|
|
bool highBitSet = false; // Transform if high bit of constant set?
|
|
|
|
|
|
|
|
switch (Op0BO->getOpcode()) {
|
2003-08-12 21:53:41 +00:00
|
|
|
default: isValid = false; break; // Do not perform transform!
|
2004-10-08 03:46:20 +00:00
|
|
|
case Instruction::Add:
|
|
|
|
isValid = isLeftShift;
|
|
|
|
break;
|
2003-08-12 21:53:41 +00:00
|
|
|
case Instruction::Or:
|
|
|
|
case Instruction::Xor:
|
|
|
|
highBitSet = false;
|
|
|
|
break;
|
|
|
|
case Instruction::And:
|
|
|
|
highBitSet = true;
|
|
|
|
break;
|
2006-01-06 07:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a signed shift right, and the high bit is modified
|
|
|
|
// by the logical operation, do not perform the transformation.
|
|
|
|
// The highBitSet boolean indicates the value of the high bit of
|
|
|
|
// the constant which would cause it to be modified for this
|
|
|
|
// operation.
|
|
|
|
//
|
2007-12-06 06:25:04 +00:00
|
|
|
if (isValid && I.getOpcode() == Instruction::AShr)
|
2007-03-28 15:02:20 +00:00
|
|
|
isValid = Op0C->getValue()[TypeBits-1] == highBitSet;
|
2006-01-06 07:12:35 +00:00
|
|
|
|
|
|
|
if (isValid) {
|
|
|
|
Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1);
|
|
|
|
|
|
|
|
Instruction *NewShift =
|
2007-02-11 01:23:03 +00:00
|
|
|
BinaryOperator::create(I.getOpcode(), Op0BO->getOperand(0), Op1);
|
2006-01-06 07:12:35 +00:00
|
|
|
InsertNewInstBefore(NewShift, I);
|
2007-02-11 01:23:03 +00:00
|
|
|
NewShift->takeName(Op0BO);
|
2006-01-06 07:12:35 +00:00
|
|
|
|
|
|
|
return BinaryOperator::create(Op0BO->getOpcode(), NewShift,
|
|
|
|
NewRHS);
|
2003-08-12 21:53:41 +00:00
|
|
|
}
|
implement shift.ll:test25. This compiles:
struct S { unsigned int i : 6, j : 11, k : 15; } b;
void plus3 (unsigned int x) {
b.k += x;
}
to:
_plus3:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r3, 0(r2)
rlwinm r4, r3, 0, 0, 14
add r4, r4, r3
rlwimi r4, r3, 0, 15, 31
stw r4, 0(r2)
blr
instead of:
_plus3:
lis r2, ha16(L_b$non_lazy_ptr)
lwz r2, lo16(L_b$non_lazy_ptr)(r2)
lwz r4, 0(r2)
srwi r5, r4, 17
add r3, r5, r3
slwi r3, r3, 17
rlwimi r3, r4, 0, 15, 31
stw r3, 0(r2)
blr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23381 91177308-0d34-0410-b5e6-96231b3b80d8
2005-09-18 05:12:10 +00:00
|
|
|
}
|
2005-01-01 16:22:27 +00:00
|
|
|
}
|
2006-01-06 07:12:35 +00:00
|
|
|
}
|
|
|
|
|
2006-01-06 07:52:12 +00:00
|
|
|
// Find out if this is a shift of a shift by a constant.
|
2007-02-02 02:16:23 +00:00
|
|
|
BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0);
|
|
|
|
if (ShiftOp && !ShiftOp->isShift())
|
|
|
|
ShiftOp = 0;
|
2006-01-06 07:52:12 +00:00
|
|
|
|
2006-10-20 07:07:24 +00:00
|
|
|
if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) {
|
|
|
|
ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1));
|
2007-03-30 09:29:48 +00:00
|
|
|
uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits);
|
|
|
|
uint32_t ShiftAmt2 = Op1->getLimitedValue(TypeBits);
|
2007-02-05 00:57:54 +00:00
|
|
|
assert(ShiftAmt2 != 0 && "Should have been simplified earlier");
|
|
|
|
if (ShiftAmt1 == 0) return 0; // Will be simplified in the future.
|
|
|
|
Value *X = ShiftOp->getOperand(0);
|
|
|
|
|
2007-04-02 08:20:41 +00:00
|
|
|
uint32_t AmtSum = ShiftAmt1+ShiftAmt2; // Fold into one big shift.
|
2007-03-23 18:46:34 +00:00
|
|
|
if (AmtSum > TypeBits)
|
|
|
|
AmtSum = TypeBits;
|
2007-02-05 00:57:54 +00:00
|
|
|
|
|
|
|
const IntegerType *Ty = cast<IntegerType>(I.getType());
|
2006-01-06 07:52:12 +00:00
|
|
|
|
2007-02-05 00:57:54 +00:00
|
|
|
// Check for (X << c1) << c2 and (X >> c1) >> c2
|
2007-02-03 23:28:07 +00:00
|
|
|
if (I.getOpcode() == ShiftOp->getOpcode()) {
|
2007-02-05 00:57:54 +00:00
|
|
|
return BinaryOperator::create(I.getOpcode(), X,
|
|
|
|
ConstantInt::get(Ty, AmtSum));
|
|
|
|
} else if (ShiftOp->getOpcode() == Instruction::LShr &&
|
|
|
|
I.getOpcode() == Instruction::AShr) {
|
|
|
|
// ((X >>u C1) >>s C2) -> (X >>u (C1+C2)) since C1 != 0.
|
|
|
|
return BinaryOperator::createLShr(X, ConstantInt::get(Ty, AmtSum));
|
|
|
|
} else if (ShiftOp->getOpcode() == Instruction::AShr &&
|
|
|
|
I.getOpcode() == Instruction::LShr) {
|
|
|
|
// ((X >>s C1) >>u C2) -> ((X >>s (C1+C2)) & mask) since C1 != 0.
|
|
|
|
Instruction *Shift =
|
|
|
|
BinaryOperator::createAShr(X, ConstantInt::get(Ty, AmtSum));
|
|
|
|
InsertNewInstBefore(Shift, I);
|
|
|
|
|
2007-03-28 15:02:20 +00:00
|
|
|
APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
|
2007-03-23 18:46:34 +00:00
|
|
|
return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask));
|
2006-01-06 07:52:12 +00:00
|
|
|
}
|
|
|
|
|
2007-02-05 00:57:54 +00:00
|
|
|
// Okay, if we get here, one shift must be left, and the other shift must be
|
|
|
|
// right. See if the amounts are equal.
|
|
|
|
if (ShiftAmt1 == ShiftAmt2) {
|
|
|
|
// If we have ((X >>? C) << C), turn this into X & (-1 << C).
|
|
|
|
if (I.getOpcode() == Instruction::Shl) {
|
2007-03-25 21:11:44 +00:00
|
|
|
APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt1));
|
2007-03-23 18:46:34 +00:00
|
|
|
return BinaryOperator::createAnd(X, ConstantInt::get(Mask));
|
2007-02-05 00:57:54 +00:00
|
|
|
}
|
|
|
|
// If we have ((X << C) >>u C), turn this into X & (-1 >>u C).
|
|
|
|
if (I.getOpcode() == Instruction::LShr) {
|
2007-04-01 17:13:37 +00:00
|
|
|
APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1));
|
2007-03-23 18:46:34 +00:00
|
|
|
return BinaryOperator::createAnd(X, ConstantInt::get(Mask));
|
2007-02-05 00:57:54 +00:00
|
|
|
}
|
|
|
|
// We can simplify ((X << C) >>s C) into a trunc + sext.
|
|
|
|
// NOTE: we could do this for any C, but that would make 'unusual' integer
|
|
|
|
// types. For now, just stick to ones well-supported by the code
|
|
|
|
// generators.
|
|
|
|
const Type *SExtType = 0;
|
|
|
|
switch (Ty->getBitWidth() - ShiftAmt1) {
|
2007-03-28 15:02:20 +00:00
|
|
|
case 1 :
|
|
|
|
case 8 :
|
|
|
|
case 16 :
|
|
|
|
case 32 :
|
|
|
|
case 64 :
|
|
|
|
case 128:
|
|
|
|
SExtType = IntegerType::get(Ty->getBitWidth() - ShiftAmt1);
|
|
|
|
break;
|
2007-02-05 00:57:54 +00:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
if (SExtType) {
|
|
|
|
Instruction *NewTrunc = new TruncInst(X, SExtType, "sext");
|
|
|
|
InsertNewInstBefore(NewTrunc, I);
|
|
|
|
return new SExtInst(NewTrunc, Ty);
|
|
|
|
}
|
|
|
|
// Otherwise, we can't handle it yet.
|
|
|
|
} else if (ShiftAmt1 < ShiftAmt2) {
|
2007-04-02 08:20:41 +00:00
|
|
|
uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1;
|
2006-01-06 07:52:12 +00:00
|
|
|
|
2007-02-05 05:57:49 +00:00
|
|
|
// (X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2)
|
2007-02-05 00:57:54 +00:00
|
|
|
if (I.getOpcode() == Instruction::Shl) {
|
|
|
|
assert(ShiftOp->getOpcode() == Instruction::LShr ||
|
|
|
|
ShiftOp->getOpcode() == Instruction::AShr);
|
|
|
|
Instruction *Shift =
|
|
|
|
BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff));
|
|
|
|
InsertNewInstBefore(Shift, I);
|
|
|
|
|
2007-03-25 21:11:44 +00:00
|
|
|
APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2));
|
|
|
|
return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask));
|
2007-02-05 00:57:54 +00:00
|
|
|
}
|
2006-01-06 07:52:12 +00:00
|
|
|
|
2007-02-05 05:57:49 +00:00
|
|
|
// (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2)
|
2007-02-05 00:57:54 +00:00
|
|
|
if (I.getOpcode() == Instruction::LShr) {
|
|
|
|
assert(ShiftOp->getOpcode() == Instruction::Shl);
|
2006-01-07 01:32:28 +00:00
|
|
|
Instruction *Shift =
|
2007-02-05 00:57:54 +00:00
|
|
|
BinaryOperator::createLShr(X, ConstantInt::get(Ty, ShiftDiff));
|
2006-01-07 01:32:28 +00:00
|
|
|
InsertNewInstBefore(Shift, I);
|
|
|
|
|
2007-03-26 17:18:58 +00:00
|
|
|
APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
|
2007-03-23 18:46:34 +00:00
|
|
|
return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask));
|
2006-01-06 07:52:12 +00:00
|
|
|
}
|
2007-02-05 00:57:54 +00:00
|
|
|
|
|
|
|
// We can't handle (X << C1) >>s C2, it shifts arbitrary bits in.
|
2006-01-06 07:52:12 +00:00
|
|
|
} else {
|
2007-02-05 00:57:54 +00:00
|
|
|
assert(ShiftAmt2 < ShiftAmt1);
|
2007-04-02 08:20:41 +00:00
|
|
|
uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2;
|
2007-02-05 00:57:54 +00:00
|
|
|
|
2007-02-05 05:57:49 +00:00
|
|
|
// (X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2)
|
2007-02-05 00:57:54 +00:00
|
|
|
if (I.getOpcode() == Instruction::Shl) {
|
|
|
|
assert(ShiftOp->getOpcode() == Instruction::LShr ||
|
|
|
|
ShiftOp->getOpcode() == Instruction::AShr);
|
|
|
|
Instruction *Shift =
|
|
|
|
BinaryOperator::create(ShiftOp->getOpcode(), X,
|
|
|
|
ConstantInt::get(Ty, ShiftDiff));
|
|
|
|
InsertNewInstBefore(Shift, I);
|
2006-01-06 07:52:12 +00:00
|
|
|
|
2007-03-25 21:11:44 +00:00
|
|
|
APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2));
|
2007-03-23 18:46:34 +00:00
|
|
|
return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask));
|
2007-02-05 00:57:54 +00:00
|
|
|
}
|
|
|
|
|
2007-02-05 05:57:49 +00:00
|
|
|
// (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2)
|
2007-02-05 00:57:54 +00:00
|
|
|
if (I.getOpcode() == Instruction::LShr) {
|
|
|
|
assert(ShiftOp->getOpcode() == Instruction::Shl);
|
|
|
|
Instruction *Shift =
|
|
|
|
BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff));
|
|
|
|
InsertNewInstBefore(Shift, I);
|
|
|
|
|
2007-03-26 23:45:51 +00:00
|
|
|
APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
|
2007-03-23 18:46:34 +00:00
|
|
|
return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask));
|
2003-07-24 17:52:58 +00:00
|
|
|
}
|
2007-02-05 00:57:54 +00:00
|
|
|
|
|
|
|
// We can't handle (X << C1) >>a C2, it shifts arbitrary bits in.
|
2006-01-06 07:12:35 +00:00
|
|
|
}
|
2006-01-06 07:52:12 +00:00
|
|
|
}
|
2002-05-06 16:14:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-05-02 17:06:02 +00:00
|
|
|
|
2005-10-29 04:36:15 +00:00
|
|
|
/// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear
|
|
|
|
/// expression. If so, decompose it, returning some value X, such that Val is
|
|
|
|
/// X*Scale+Offset.
|
|
|
|
///
|
|
|
|
static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale,
|
2007-04-04 16:58:57 +00:00
|
|
|
int &Offset) {
|
2006-12-31 05:48:39 +00:00
|
|
|
assert(Val->getType() == Type::Int32Ty && "Unexpected allocation size type!");
|
2006-10-20 07:07:24 +00:00
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
|
2006-12-31 05:48:39 +00:00
|
|
|
Offset = CI->getZExtValue();
|
2007-10-12 05:30:59 +00:00
|
|
|
Scale = 0;
|
2006-12-31 05:48:39 +00:00
|
|
|
return ConstantInt::get(Type::Int32Ty, 0);
|
2007-10-12 05:30:59 +00:00
|
|
|
} else if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) {
|
|
|
|
if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
|
|
|
|
if (I->getOpcode() == Instruction::Shl) {
|
|
|
|
// This is a value scaled by '1 << the shift amt'.
|
|
|
|
Scale = 1U << RHS->getZExtValue();
|
|
|
|
Offset = 0;
|
|
|
|
return I->getOperand(0);
|
|
|
|
} else if (I->getOpcode() == Instruction::Mul) {
|
|
|
|
// This value is scaled by 'RHS'.
|
|
|
|
Scale = RHS->getZExtValue();
|
|
|
|
Offset = 0;
|
|
|
|
return I->getOperand(0);
|
|
|
|
} else if (I->getOpcode() == Instruction::Add) {
|
|
|
|
// We have X+C. Check to see if we really have (X*C2)+C1,
|
|
|
|
// where C1 is divisible by C2.
|
|
|
|
unsigned SubScale;
|
|
|
|
Value *SubVal =
|
|
|
|
DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset);
|
|
|
|
Offset += RHS->getZExtValue();
|
|
|
|
Scale = SubScale;
|
|
|
|
return SubVal;
|
2005-10-29 04:36:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we can't look past this.
|
|
|
|
Scale = 1;
|
|
|
|
Offset = 0;
|
|
|
|
return Val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-24 06:03:58 +00:00
|
|
|
/// PromoteCastOfAllocation - If we find a cast of an allocation instruction,
|
|
|
|
/// try to eliminate the cast by moving the type information into the alloc.
|
2007-04-27 17:44:50 +00:00
|
|
|
Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI,
|
2005-10-24 06:03:58 +00:00
|
|
|
AllocationInst &AI) {
|
2007-04-27 17:44:50 +00:00
|
|
|
const PointerType *PTy = cast<PointerType>(CI.getType());
|
2005-10-24 06:03:58 +00:00
|
|
|
|
2005-10-24 06:22:12 +00:00
|
|
|
// Remove any uses of AI that are dead.
|
|
|
|
assert(!CI.use_empty() && "Dead instructions should be removed earlier!");
|
2007-02-15 22:52:10 +00:00
|
|
|
|
2005-10-24 06:22:12 +00:00
|
|
|
for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) {
|
|
|
|
Instruction *User = cast<Instruction>(*UI++);
|
|
|
|
if (isInstructionTriviallyDead(User)) {
|
|
|
|
while (UI != E && *UI == User)
|
|
|
|
++UI; // If this instruction uses AI more than once, don't break UI.
|
|
|
|
|
|
|
|
++NumDeadInst;
|
2006-11-26 09:46:52 +00:00
|
|
|
DOUT << "IC: DCE: " << *User;
|
2007-03-02 19:59:19 +00:00
|
|
|
EraseInstFromFunction(*User);
|
2005-10-24 06:22:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-24 06:03:58 +00:00
|
|
|
// Get the type really allocated and the type casted to.
|
|
|
|
const Type *AllocElTy = AI.getAllocatedType();
|
|
|
|
const Type *CastElTy = PTy->getElementType();
|
|
|
|
if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0;
|
2005-10-24 06:26:18 +00:00
|
|
|
|
2007-02-14 05:52:17 +00:00
|
|
|
unsigned AllocElTyAlign = TD->getABITypeAlignment(AllocElTy);
|
|
|
|
unsigned CastElTyAlign = TD->getABITypeAlignment(CastElTy);
|
2005-10-24 06:26:18 +00:00
|
|
|
if (CastElTyAlign < AllocElTyAlign) return 0;
|
|
|
|
|
2005-10-24 06:35:18 +00:00
|
|
|
// If the allocation has multiple uses, only promote it if we are strictly
|
|
|
|
// increasing the alignment of the resultant allocation. If we keep it the
|
|
|
|
// same, we open the door to infinite loops of various kinds.
|
|
|
|
if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0;
|
|
|
|
|
Executive summary: getTypeSize -> getTypeStoreSize / getABITypeSize.
The meaning of getTypeSize was not clear - clarifying it is important
now that we have x86 long double and arbitrary precision integers.
The issue with long double is that it requires 80 bits, and this is
not a multiple of its alignment. This gives a primitive type for
which getTypeSize differed from getABITypeSize. For arbitrary precision
integers it is even worse: there is the minimum number of bits needed to
hold the type (eg: 36 for an i36), the maximum number of bits that will
be overwriten when storing the type (40 bits for i36) and the ABI size
(i.e. the storage size rounded up to a multiple of the alignment; 64 bits
for i36).
This patch removes getTypeSize (not really - it is still there but
deprecated to allow for a gradual transition). Instead there is:
(1) getTypeSizeInBits - a number of bits that suffices to hold all
values of the type. For a primitive type, this is the minimum number
of bits. For an i36 this is 36 bits. For x86 long double it is 80.
This corresponds to gcc's TYPE_PRECISION.
(2) getTypeStoreSizeInBits - the maximum number of bits that is
written when storing the type (or read when reading it). For an
i36 this is 40 bits, for an x86 long double it is 80 bits. This
is the size alias analysis is interested in (getTypeStoreSize
returns the number of bytes). There doesn't seem to be anything
corresponding to this in gcc.
(3) getABITypeSizeInBits - this is getTypeStoreSizeInBits rounded
up to a multiple of the alignment. For an i36 this is 64, for an
x86 long double this is 96 or 128 depending on the OS. This is the
spacing between consecutive elements when you form an array out of
this type (getABITypeSize returns the number of bytes). This is
TYPE_SIZE in gcc.
Since successive elements in a SequentialType (arrays, pointers
and vectors) need to be aligned, the spacing between them will be
given by getABITypeSize. This means that the size of an array
is the length times the getABITypeSize. It also means that GEP
computations need to use getABITypeSize when computing offsets.
Furthermore, if an alloca allocates several elements at once then
these too need to be aligned, so the size of the alloca has to be
the number of elements multiplied by getABITypeSize. Logically
speaking this doesn't have to be the case when allocating just
one element, but it is simpler to also use getABITypeSize in this
case. So alloca's and mallocs should use getABITypeSize. Finally,
since gcc's only notion of size is that given by getABITypeSize, if
you want to output assembler etc the same as gcc then getABITypeSize
is the size you want.
Since a store will overwrite no more than getTypeStoreSize bytes,
and a read will read no more than that many bytes, this is the
notion of size appropriate for alias analysis calculations.
In this patch I have corrected all type size uses except some of
those in ScalarReplAggregates, lib/Codegen, lib/Target (the hard
cases). I will get around to auditing these too at some point,
but I could do with some help.
Finally, I made one change which I think wise but others might
consider pointless and suboptimal: in an unpacked struct the
amount of space allocated for a field is now given by the ABI
size rather than getTypeStoreSize. I did this because every
other place that reserves memory for a type (eg: alloca) now
uses getABITypeSize, and I didn't want to make an exception
for unpacked structs, i.e. I did it to make things more uniform.
This only effects structs containing long doubles and arbitrary
precision integers. If someone wants to pack these types more
tightly they can always use a packed struct.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@43620 91177308-0d34-0410-b5e6-96231b3b80d8
2007-11-01 20:53:16 +00:00
|
|
|
uint64_t AllocElTySize = TD->getABITypeSize(AllocElTy);
|
|
|
|
uint64_t CastElTySize = TD->getABITypeSize(CastElTy);
|
2005-10-27 05:53:56 +00:00
|
|
|
if (CastElTySize == 0 || AllocElTySize == 0) return 0;
|
2005-10-24 06:26:18 +00:00
|
|
|
|
2005-10-29 03:19:53 +00:00
|
|
|
// See if we can satisfy the modulus by pulling a scale out of the array
|
|
|
|
// size argument.
|
2007-04-04 16:58:57 +00:00
|
|
|
unsigned ArraySizeScale;
|
|
|
|
int ArrayOffset;
|
2005-10-29 04:36:15 +00:00
|
|
|
Value *NumElements = // See if the array size is a decomposable linear expr.
|
|
|
|
DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset);
|
|
|
|
|
2005-10-29 03:19:53 +00:00
|
|
|
// If we can now satisfy the modulus, by using a non-1 scale, we really can
|
|
|
|
// do the xform.
|
2005-10-29 04:36:15 +00:00
|
|
|
if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 ||
|
|
|
|
(AllocElTySize*ArrayOffset ) % CastElTySize != 0) return 0;
|
2005-10-27 06:12:00 +00:00
|
|
|
|
2005-10-29 03:19:53 +00:00
|
|
|
unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize;
|
|
|
|
Value *Amt = 0;
|
|
|
|
if (Scale == 1) {
|
|
|
|
Amt = NumElements;
|
|
|
|
} else {
|
2006-10-20 07:07:24 +00:00
|
|
|
// If the allocation size is constant, form a constant mul expression
|
2006-12-31 05:48:39 +00:00
|
|
|
Amt = ConstantInt::get(Type::Int32Ty, Scale);
|
|
|
|
if (isa<ConstantInt>(NumElements))
|
2007-04-02 13:45:30 +00:00
|
|
|
Amt = Multiply(cast<ConstantInt>(NumElements), cast<ConstantInt>(Amt));
|
2006-10-20 07:07:24 +00:00
|
|
|
// otherwise multiply the amount and the number of elements
|
2005-10-29 03:19:53 +00:00
|
|
|
else if (Scale != 1) {
|
|
|
|
Instruction *Tmp = BinaryOperator::createMul(Amt, NumElements, "tmp");
|
|
|
|
Amt = InsertNewInstBefore(Tmp, AI);
|
2005-10-27 06:12:00 +00:00
|
|
|
}
|
2005-10-27 05:53:56 +00:00
|
|
|
}
|
|
|
|
|
2007-04-04 16:58:57 +00:00
|
|
|
if (int Offset = (AllocElTySize*ArrayOffset)/CastElTySize) {
|
|
|
|
Value *Off = ConstantInt::get(Type::Int32Ty, Offset, true);
|
2005-10-29 04:36:15 +00:00
|
|
|
Instruction *Tmp = BinaryOperator::createAdd(Amt, Off, "tmp");
|
|
|
|
Amt = InsertNewInstBefore(Tmp, AI);
|
|
|
|
}
|
|
|
|
|
2005-10-24 06:03:58 +00:00
|
|
|
AllocationInst *New;
|
|
|
|
if (isa<MallocInst>(AI))
|
2007-02-11 01:23:03 +00:00
|
|
|
New = new MallocInst(CastElTy, Amt, AI.getAlignment());
|
2005-10-24 06:03:58 +00:00
|
|
|
else
|
2007-02-11 01:23:03 +00:00
|
|
|
New = new AllocaInst(CastElTy, Amt, AI.getAlignment());
|
2005-10-24 06:03:58 +00:00
|
|
|
InsertNewInstBefore(New, AI);
|
2007-02-11 01:23:03 +00:00
|
|
|
New->takeName(&AI);
|
2005-10-24 06:35:18 +00:00
|
|
|
|
|
|
|
// If the allocation has multiple uses, insert a cast and change all things
|
|
|
|
// that used it to use the new cast. This will also hack on CI, but it will
|
|
|
|
// die soon.
|
|
|
|
if (!AI.hasOneUse()) {
|
|
|
|
AddUsesToWorkList(AI);
|
2006-11-27 01:05:10 +00:00
|
|
|
// New is the allocation instruction, pointer typed. AI is the original
|
|
|
|
// allocation instruction, also pointer typed. Thus, cast to use is BitCast.
|
|
|
|
CastInst *NewCast = new BitCastInst(New, AI.getType(), "tmpcast");
|
2005-10-24 06:35:18 +00:00
|
|
|
InsertNewInstBefore(NewCast, AI);
|
|
|
|
AI.replaceAllUsesWith(NewCast);
|
|
|
|
}
|
2005-10-24 06:03:58 +00:00
|
|
|
return ReplaceInstUsesWith(CI, New);
|
|
|
|
}
|
|
|
|
|
Implement simple promotion for cast elimination in instcombine. This is
currently very limited, but can be extended in the future. For example,
we now compile:
uint %test30(uint %c1) {
%c2 = cast uint %c1 to ubyte
%c3 = xor ubyte %c2, 1
%c4 = cast ubyte %c3 to uint
ret uint %c4
}
to:
_xor:
movzbl 4(%esp), %eax
xorl $1, %eax
ret
instead of:
_xor:
movb $1, %al
xorb 4(%esp), %al
movzbl %al, %eax
ret
More impressively, we now compile:
struct B { unsigned bit : 1; };
void xor(struct B *b) { b->bit = b->bit ^ 1; }
To (X86/PPC):
_xor:
movl 4(%esp), %eax
xorl $-2147483648, (%eax)
ret
_xor:
lwz r2, 0(r3)
xoris r2, r2, 32768
stw r2, 0(r3)
blr
instead of (X86/PPC):
_xor:
movl 4(%esp), %eax
movl (%eax), %ecx
movl %ecx, %edx
shrl $31, %edx
# TRUNCATE movb %dl, %dl
xorb $1, %dl
movzbl %dl, %edx
andl $2147483647, %ecx
shll $31, %edx
orl %ecx, %edx
movl %edx, (%eax)
ret
_xor:
lwz r2, 0(r3)
srwi r4, r2, 31
xori r4, r4, 1
rlwimi r2, r4, 31, 0, 0
stw r2, 0(r3)
blr
This implements InstCombine/cast.ll:test30.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@28273 91177308-0d34-0410-b5e6-96231b3b80d8
2006-05-13 02:06:03 +00:00
|
|
|
/// CanEvaluateInDifferentType - Return true if we can take the specified value
|
2007-03-03 05:27:34 +00:00
|
|
|
/// and return it as type Ty without inserting any new casts and without
|
|
|
|
/// changing the computed value. This is used by code that tries to decide
|
|
|
|
/// whether promoting or shrinking integer operations to wider or smaller types
|
|
|
|
/// will allow us to eliminate a truncate or extend.
|
|
|
|
///
|
|
|
|
/// This is a truncation operation if Ty is smaller than V->getType(), or an
|
|
|
|
/// extension operation if Ty is larger.
|
2008-04-10 18:43:06 +00:00
|
|
|
bool InstCombiner::CanEvaluateInDifferentType(Value *V, const IntegerType *Ty,
|
|
|
|
unsigned CastOpc,
|
|
|
|
int &NumCastsRemoved) {
|
2007-03-03 05:27:34 +00:00
|
|
|
// We can always evaluate constants in another type.
|
|
|
|
if (isa<ConstantInt>(V))
|
|
|
|
return true;
|
Implement simple promotion for cast elimination in instcombine. This is
currently very limited, but can be extended in the future. For example,
we now compile:
uint %test30(uint %c1) {
%c2 = cast uint %c1 to ubyte
%c3 = xor ubyte %c2, 1
%c4 = cast ubyte %c3 to uint
ret uint %c4
}
to:
_xor:
movzbl 4(%esp), %eax
xorl $1, %eax
ret
instead of:
_xor:
movb $1, %al
xorb 4(%esp), %al
movzbl %al, %eax
ret
More impressively, we now compile:
struct B { unsigned bit : 1; };
void xor(struct B *b) { b->bit = b->bit ^ 1; }
To (X86/PPC):
_xor:
movl 4(%esp), %eax
xorl $-2147483648, (%eax)
ret
_xor:
lwz r2, 0(r3)
xoris r2, r2, 32768
stw r2, 0(r3)
blr
instead of (X86/PPC):
_xor:
movl 4(%esp), %eax
movl (%eax), %ecx
movl %ecx, %edx
shrl $31, %edx
# TRUNCATE movb %dl, %dl
xorb $1, %dl
movzbl %dl, %edx
andl $2147483647, %ecx
shll $31, %edx
orl %ecx, %edx
movl %edx, (%eax)
ret
_xor:
lwz r2, 0(r3)
srwi r4, r2, 31
xori r4, r4, 1
rlwimi r2, r4, 31, 0, 0
stw r2, 0(r3)
blr
This implements InstCombine/cast.ll:test30.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@28273 91177308-0d34-0410-b5e6-96231b3b80d8
2006-05-13 02:06:03 +00:00
|
|
|
|
|
|
|
Instruction *I = dyn_cast<Instruction>(V);
|
2007-03-03 05:27:34 +00:00
|
|
|
if (!I) return false;
|
|
|
|
|
|
|
|
const IntegerType *OrigTy = cast<IntegerType>(V->getType());
|
Implement simple promotion for cast elimination in instcombine. This is
currently very limited, but can be extended in the future. For example,
we now compile:
uint %test30(uint %c1) {
%c2 = cast uint %c1 to ubyte
%c3 = xor ubyte %c2, 1
%c4 = cast ubyte %c3 to uint
ret uint %c4
}
to:
_xor:
movzbl 4(%esp), %eax
xorl $1, %eax
ret
instead of:
_xor:
movb $1, %al
xorb 4(%esp), %al
movzbl %al, %eax
ret
More impressively, we now compile:
struct B { unsigned bit : 1; };
void xor(struct B *b) { b->bit = b->bit ^ 1; }
To (X86/PPC):
_xor:
movl 4(%esp), %eax
xorl $-2147483648, (%eax)
ret
_xor:
lwz r2, 0(r3)
xoris r2, r2, 32768
stw r2, 0(r3)
blr
instead of (X86/PPC):
_xor:
movl 4(%esp), %eax
movl (%eax), %ecx
movl %ecx, %edx
shrl $31, %edx
# TRUNCATE movb %dl, %dl
xorb $1, %dl
movzbl %dl, %edx
andl $2147483647, %ecx
shll $31, %edx
orl %ecx, %edx
movl %edx, (%eax)
ret
_xor:
lwz r2, 0(r3)
srwi r4, r2, 31
xori r4, r4, 1
rlwimi r2, r4, 31, 0, 0
stw r2, 0(r3)
blr
This implements InstCombine/cast.ll:test30.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@28273 91177308-0d34-0410-b5e6-96231b3b80d8
2006-05-13 02:06:03 +00:00
|
|
|
|
2007-08-02 06:11:14 +00:00
|
|
|
// If this is an extension or truncate, we can often eliminate it.
|
|
|
|
if (isa<TruncInst>(I) || isa<ZExtInst>(I) || isa<SExtInst>(I)) {
|
|
|
|
// If this is a cast from the destination type, we can trivially eliminate
|
|
|
|
// it, and this will remove a cast overall.
|
|
|
|
if (I->getOperand(0)->getType() == Ty) {
|
|
|
|
// If the first operand is itself a cast, and is eliminable, do not count
|
|
|
|
// this as an eliminable cast. We would prefer to eliminate those two
|
|
|
|
// casts first.
|
|
|
|
if (!isa<CastInst>(I->getOperand(0)))
|
|
|
|
++NumCastsRemoved;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can't extend or shrink something that has multiple uses: doing so would
|
|
|
|
// require duplicating the instruction in general, which isn't profitable.
|
|
|
|
if (!I->hasOneUse()) return false;
|
|
|
|
|
Implement simple promotion for cast elimination in instcombine. This is
currently very limited, but can be extended in the future. For example,
we now compile:
uint %test30(uint %c1) {
%c2 = cast uint %c1 to ubyte
%c3 = xor ubyte %c2, 1
%c4 = cast ubyte %c3 to uint
ret uint %c4
}
to:
_xor:
movzbl 4(%esp), %eax
xorl $1, %eax
ret
instead of:
_xor:
movb $1, %al
xorb 4(%esp), %al
movzbl %al, %eax
ret
More impressively, we now compile:
struct B { unsigned bit : 1; };
void xor(struct B *b) { b->bit = b->bit ^ 1; }
To (X86/PPC):
_xor:
movl 4(%esp), %eax
xorl $-2147483648, (%eax)
ret
_xor:
lwz r2, 0(r3)
xoris r2, r2, 32768
stw r2, 0(r3)
blr
instead of (X86/PPC):
_xor:
movl 4(%esp), %eax
movl (%eax), %ecx
movl %ecx, %edx
shrl $31, %edx
# TRUNCATE movb %dl, %dl
xorb $1, %dl
movzbl %dl, %edx
andl $2147483647, %ecx
shll $31, %edx
orl %ecx, %edx
movl %edx, (%eax)
ret
_xor:
lwz r2, 0(r3)
srwi r4, r2, 31
xori r4, r4, 1
rlwimi r2, r4, 31, 0, 0
stw r2, 0(r3)
blr
This implements InstCombine/cast.ll:test30.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@28273 91177308-0d34-0410-b5e6-96231b3b80d8
2006-05-13 02:06:03 +00:00
|
|
|
switch (I->getOpcode()) {
|
2007-03-03 05:27:34 +00:00
|
|
|
case Instruction::Add:
|
|
|
|
case Instruction::Sub:
|
Implement simple promotion for cast elimination in instcombine. This is
currently very limited, but can be extended in the future. For example,
we now compile:
uint %test30(uint %c1) {
%c2 = cast uint %c1 to ubyte
%c3 = xor ubyte %c2, 1
%c4 = cast ubyte %c3 to uint
ret uint %c4
}
to:
_xor:
movzbl 4(%esp), %eax
xorl $1, %eax
ret
instead of:
_xor:
movb $1, %al
xorb 4(%esp), %al
movzbl %al, %eax
ret
More impressively, we now compile:
struct B { unsigned bit : 1; };
void xor(struct B *b) { b->bit = b->bit ^ 1; }
To (X86/PPC):
_xor:
movl 4(%esp), %eax
xorl $-2147483648, (%eax)
ret
_xor:
lwz r2, 0(r3)
xoris r2, r2, 32768
stw r2, 0(r3)
blr
instead of (X86/PPC):
_xor:
movl 4(%esp), %eax
movl (%eax), %ecx
movl %ecx, %edx
shrl $31, %edx
# TRUNCATE movb %dl, %dl
xorb $1, %dl
movzbl %dl, %edx
andl $2147483647, %ecx
shll $31, %edx
orl %ecx, %edx
movl %edx, (%eax)
ret
_xor:
lwz r2, 0(r3)
srwi r4, r2, 31
xori r4, r4, 1
rlwimi r2, r4, 31, 0, 0
stw r2, 0(r3)
blr
This implements InstCombine/cast.ll:test30.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@28273 91177308-0d34-0410-b5e6-96231b3b80d8
2006-05-13 02:06:03 +00:00
|
|
|
case Instruction::And:
|
|
|
|
case Instruction::Or:
|
|
|
|
case Instruction::Xor:
|
|
|
|
// These operators can all arbitrarily be extended or truncated.
|
2007-08-02 06:11:14 +00:00
|
|
|
return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc,
|
|
|
|
NumCastsRemoved) &&
|
|
|
|
CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc,
|
|
|
|
NumCastsRemoved);
|
2007-03-03 05:27:34 +00:00
|
|
|
|
2008-01-22 05:08:48 +00:00
|
|
|
case Instruction::Mul:
|
|
|
|
// A multiply can be truncated by truncating its operands.
|
|
|
|
return Ty->getBitWidth() < OrigTy->getBitWidth() &&
|
|
|
|
CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc,
|
|
|
|
NumCastsRemoved) &&
|
|
|
|
CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc,
|
|
|
|
NumCastsRemoved);
|
|
|
|
|
2006-11-29 07:18:39 +00:00
|
|
|
case Instruction::Shl:
|
2007-03-03 05:27:34 +00:00
|
|
|
// If we are truncating the result of this SHL, and if it's a shift of a
|
|
|
|
// constant amount, we can always perform a SHL in a smaller type.
|
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
|
2007-03-30 17:20:39 +00:00
|
|
|
uint32_t BitWidth = Ty->getBitWidth();
|
|
|
|
if (BitWidth < OrigTy->getBitWidth() &&
|
|
|
|
CI->getLimitedValue(BitWidth) < BitWidth)
|
2007-08-02 06:11:14 +00:00
|
|
|
return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc,
|
|
|
|
NumCastsRemoved);
|
2007-03-03 05:27:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Instruction::LShr:
|
|
|
|
// If this is a truncate of a logical shr, we can truncate it to a smaller
|
|
|
|
// lshr iff we know that the bits we would otherwise be shifting in are
|
|
|
|
// already zeros.
|
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
|
2007-03-30 17:20:39 +00:00
|
|
|
uint32_t OrigBitWidth = OrigTy->getBitWidth();
|
|
|
|
uint32_t BitWidth = Ty->getBitWidth();
|
|
|
|
if (BitWidth < OrigBitWidth &&
|
2007-03-03 05:27:34 +00:00
|
|
|
MaskedValueIsZero(I->getOperand(0),
|
2007-03-30 17:20:39 +00:00
|
|
|
APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) &&
|
|
|
|
CI->getLimitedValue(BitWidth) < BitWidth) {
|
2007-08-02 06:11:14 +00:00
|
|
|
return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc,
|
|
|
|
NumCastsRemoved);
|
2007-03-03 05:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
2006-11-29 07:18:39 +00:00
|
|
|
break;
|
2006-11-27 01:05:10 +00:00
|
|
|
case Instruction::ZExt:
|
|
|
|
case Instruction::SExt:
|
2007-08-02 06:11:14 +00:00
|
|
|
case Instruction::Trunc:
|
|
|
|
// If this is the same kind of case as our original (e.g. zext+zext), we
|
2007-08-02 17:23:38 +00:00
|
|
|
// can safely replace it. Note that replacing it does not reduce the number
|
|
|
|
// of casts in the input.
|
|
|
|
if (I->getOpcode() == CastOpc)
|
Implement simple promotion for cast elimination in instcombine. This is
currently very limited, but can be extended in the future. For example,
we now compile:
uint %test30(uint %c1) {
%c2 = cast uint %c1 to ubyte
%c3 = xor ubyte %c2, 1
%c4 = cast ubyte %c3 to uint
ret uint %c4
}
to:
_xor:
movzbl 4(%esp), %eax
xorl $1, %eax
ret
instead of:
_xor:
movb $1, %al
xorb 4(%esp), %al
movzbl %al, %eax
ret
More impressively, we now compile:
struct B { unsigned bit : 1; };
void xor(struct B *b) { b->bit = b->bit ^ 1; }
To (X86/PPC):
_xor:
movl 4(%esp), %eax
xorl $-2147483648, (%eax)
ret
_xor:
lwz r2, 0(r3)
xoris r2, r2, 32768
stw r2, 0(r3)
blr
instead of (X86/PPC):
_xor:
movl 4(%esp), %eax
movl (%eax), %ecx
movl %ecx, %edx
shrl $31, %edx
# TRUNCATE movb %dl, %dl
xorb $1, %dl
movzbl %dl, %edx
andl $2147483647, %ecx
shll $31, %edx
orl %ecx, %edx
movl %edx, (%eax)
ret
_xor:
lwz r2, 0(r3)
srwi r4, r2, 31
xori r4, r4, 1
rlwimi r2, r4, 31, 0, 0
stw r2, 0(r3)
blr
This implements InstCombine/cast.ll:test30.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@28273 91177308-0d34-0410-b5e6-96231b3b80d8
2006-05-13 02:06:03 +00:00
|
|
|
return true;
|
2007-09-10 23:46:29 +00:00
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
break;
|
|
|
|
default:
|
Implement simple promotion for cast elimination in instcombine. This is
currently very limited, but can be extended in the future. For example,
we now compile:
uint %test30(uint %c1) {
%c2 = cast uint %c1 to ubyte
%c3 = xor ubyte %c2, 1
%c4 = cast ubyte %c3 to uint
ret uint %c4
}
to:
_xor:
movzbl 4(%esp), %eax
xorl $1, %eax
ret
instead of:
_xor:
movb $1, %al
xorb 4(%esp), %al
movzbl %al, %eax
ret
More impressively, we now compile:
struct B { unsigned bit : 1; };
void xor(struct B *b) { b->bit = b->bit ^ 1; }
To (X86/PPC):
_xor:
movl 4(%esp), %eax
xorl $-2147483648, (%eax)
ret
_xor:
lwz r2, 0(r3)
xoris r2, r2, 32768
stw r2, 0(r3)
blr
instead of (X86/PPC):
_xor:
movl 4(%esp), %eax
movl (%eax), %ecx
movl %ecx, %edx
shrl $31, %edx
# TRUNCATE movb %dl, %dl
xorb $1, %dl
movzbl %dl, %edx
andl $2147483647, %ecx
shll $31, %edx
orl %ecx, %edx
movl %edx, (%eax)
ret
_xor:
lwz r2, 0(r3)
srwi r4, r2, 31
xori r4, r4, 1
rlwimi r2, r4, 31, 0, 0
stw r2, 0(r3)
blr
This implements InstCombine/cast.ll:test30.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@28273 91177308-0d34-0410-b5e6-96231b3b80d8
2006-05-13 02:06:03 +00:00
|
|
|
// TODO: Can handle more cases here.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EvaluateInDifferentType - Given an expression that
|
|
|
|
/// CanEvaluateInDifferentType returns true for, actually insert the code to
|
|
|
|
/// evaluate the expression.
|
2006-12-13 18:21:21 +00:00
|
|
|
Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty,
|
2007-03-03 05:27:34 +00:00
|
|
|
bool isSigned) {
|
Implement simple promotion for cast elimination in instcombine. This is
currently very limited, but can be extended in the future. For example,
we now compile:
uint %test30(uint %c1) {
%c2 = cast uint %c1 to ubyte
%c3 = xor ubyte %c2, 1
%c4 = cast ubyte %c3 to uint
ret uint %c4
}
to:
_xor:
movzbl 4(%esp), %eax
xorl $1, %eax
ret
instead of:
_xor:
movb $1, %al
xorb 4(%esp), %al
movzbl %al, %eax
ret
More impressively, we now compile:
struct B { unsigned bit : 1; };
void xor(struct B *b) { b->bit = b->bit ^ 1; }
To (X86/PPC):
_xor:
movl 4(%esp), %eax
xorl $-2147483648, (%eax)
ret
_xor:
lwz r2, 0(r3)
xoris r2, r2, 32768
stw r2, 0(r3)
blr
instead of (X86/PPC):
_xor:
movl 4(%esp), %eax
movl (%eax), %ecx
movl %ecx, %edx
shrl $31, %edx
# TRUNCATE movb %dl, %dl
xorb $1, %dl
movzbl %dl, %edx
andl $2147483647, %ecx
shll $31, %edx
orl %ecx, %edx
movl %edx, (%eax)
ret
_xor:
lwz r2, 0(r3)
srwi r4, r2, 31
xori r4, r4, 1
rlwimi r2, r4, 31, 0, 0
stw r2, 0(r3)
blr
This implements InstCombine/cast.ll:test30.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@28273 91177308-0d34-0410-b5e6-96231b3b80d8
2006-05-13 02:06:03 +00:00
|
|
|
if (Constant *C = dyn_cast<Constant>(V))
|
2006-12-13 18:21:21 +00:00
|
|
|
return ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/);
|
Implement simple promotion for cast elimination in instcombine. This is
currently very limited, but can be extended in the future. For example,
we now compile:
uint %test30(uint %c1) {
%c2 = cast uint %c1 to ubyte
%c3 = xor ubyte %c2, 1
%c4 = cast ubyte %c3 to uint
ret uint %c4
}
to:
_xor:
movzbl 4(%esp), %eax
xorl $1, %eax
ret
instead of:
_xor:
movb $1, %al
xorb 4(%esp), %al
movzbl %al, %eax
ret
More impressively, we now compile:
struct B { unsigned bit : 1; };
void xor(struct B *b) { b->bit = b->bit ^ 1; }
To (X86/PPC):
_xor:
movl 4(%esp), %eax
xorl $-2147483648, (%eax)
ret
_xor:
lwz r2, 0(r3)
xoris r2, r2, 32768
stw r2, 0(r3)
blr
instead of (X86/PPC):
_xor:
movl 4(%esp), %eax
movl (%eax), %ecx
movl %ecx, %edx
shrl $31, %edx
# TRUNCATE movb %dl, %dl
xorb $1, %dl
movzbl %dl, %edx
andl $2147483647, %ecx
shll $31, %edx
orl %ecx, %edx
movl %edx, (%eax)
ret
_xor:
lwz r2, 0(r3)
srwi r4, r2, 31
xori r4, r4, 1
rlwimi r2, r4, 31, 0, 0
stw r2, 0(r3)
blr
This implements InstCombine/cast.ll:test30.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@28273 91177308-0d34-0410-b5e6-96231b3b80d8
2006-05-13 02:06:03 +00:00
|
|
|
|
|
|
|
// Otherwise, it must be an instruction.
|
|
|
|
Instruction *I = cast<Instruction>(V);
|
2006-05-20 23:14:03 +00:00
|
|
|
Instruction *Res = 0;
|
Implement simple promotion for cast elimination in instcombine. This is
currently very limited, but can be extended in the future. For example,
we now compile:
uint %test30(uint %c1) {
%c2 = cast uint %c1 to ubyte
%c3 = xor ubyte %c2, 1
%c4 = cast ubyte %c3 to uint
ret uint %c4
}
to:
_xor:
movzbl 4(%esp), %eax
xorl $1, %eax
ret
instead of:
_xor:
movb $1, %al
xorb 4(%esp), %al
movzbl %al, %eax
ret
More impressively, we now compile:
struct B { unsigned bit : 1; };
void xor(struct B *b) { b->bit = b->bit ^ 1; }
To (X86/PPC):
_xor:
movl 4(%esp), %eax
xorl $-2147483648, (%eax)
ret
_xor:
lwz r2, 0(r3)
xoris r2, r2, 32768
stw r2, 0(r3)
blr
instead of (X86/PPC):
_xor:
movl 4(%esp), %eax
movl (%eax), %ecx
movl %ecx, %edx
shrl $31, %edx
# TRUNCATE movb %dl, %dl
xorb $1, %dl
movzbl %dl, %edx
andl $2147483647, %ecx
shll $31, %edx
orl %ecx, %edx
movl %edx, (%eax)
ret
_xor:
lwz r2, 0(r3)
srwi r4, r2, 31
xori r4, r4, 1
rlwimi r2, r4, 31, 0, 0
stw r2, 0(r3)
blr
This implements InstCombine/cast.ll:test30.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@28273 91177308-0d34-0410-b5e6-96231b3b80d8
2006-05-13 02:06:03 +00:00
|
|
|
switch (I->getOpcode()) {
|
2007-03-03 05:27:34 +00:00
|
|
|
case Instruction::Add:
|
|
|
|
case Instruction::Sub:
|
2008-01-22 05:08:48 +00:00
|
|
|
case Instruction::Mul:
|
Implement simple promotion for cast elimination in instcombine. This is
currently very limited, but can be extended in the future. For example,
we now compile:
uint %test30(uint %c1) {
%c2 = cast uint %c1 to ubyte
%c3 = xor ubyte %c2, 1
%c4 = cast ubyte %c3 to uint
ret uint %c4
}
to:
_xor:
movzbl 4(%esp), %eax
xorl $1, %eax
ret
instead of:
_xor:
movb $1, %al
xorb 4(%esp), %al
movzbl %al, %eax
ret
More impressively, we now compile:
struct B { unsigned bit : 1; };
void xor(struct B *b) { b->bit = b->bit ^ 1; }
To (X86/PPC):
_xor:
movl 4(%esp), %eax
xorl $-2147483648, (%eax)
ret
_xor:
lwz r2, 0(r3)
xoris r2, r2, 32768
stw r2, 0(r3)
blr
instead of (X86/PPC):
_xor:
movl 4(%esp), %eax
movl (%eax), %ecx
movl %ecx, %edx
shrl $31, %edx
# TRUNCATE movb %dl, %dl
xorb $1, %dl
movzbl %dl, %edx
andl $2147483647, %ecx
shll $31, %edx
orl %ecx, %edx
movl %edx, (%eax)
ret
_xor:
lwz r2, 0(r3)
srwi r4, r2, 31
xori r4, r4, 1
rlwimi r2, r4, 31, 0, 0
stw r2, 0(r3)
blr
This implements InstCombine/cast.ll:test30.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@28273 91177308-0d34-0410-b5e6-96231b3b80d8
2006-05-13 02:06:03 +00:00
|
|
|
case Instruction::And:
|
|
|
|
case Instruction::Or:
|
2007-03-03 05:27:34 +00:00
|
|
|
case Instruction::Xor:
|
2006-11-29 07:18:39 +00:00
|
|
|
case Instruction::AShr:
|
|
|
|
case Instruction::LShr:
|
|
|
|
case Instruction::Shl: {
|
2006-12-13 18:21:21 +00:00
|
|
|
Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned);
|
2007-03-03 05:27:34 +00:00
|
|
|
Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);
|
|
|
|
Res = BinaryOperator::create((Instruction::BinaryOps)I->getOpcode(),
|
|
|
|
LHS, RHS, I->getName());
|
2006-11-29 07:18:39 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-11-27 01:05:10 +00:00
|
|
|
case Instruction::Trunc:
|
|
|
|
case Instruction::ZExt:
|
|
|
|
case Instruction::SExt:
|
|
|
|
// If the source type of the cast is the type we're trying for then we can
|
2007-08-02 06:11:14 +00:00
|
|
|
// just return the source. There's no need to insert it because it is not
|
|
|
|
// new.
|
Implement simple promotion for cast elimination in instcombine. This is
currently very limited, but can be extended in the future. For example,
we now compile:
uint %test30(uint %c1) {
%c2 = cast uint %c1 to ubyte
%c3 = xor ubyte %c2, 1
%c4 = cast ubyte %c3 to uint
ret uint %c4
}
to:
_xor:
movzbl 4(%esp), %eax
xorl $1, %eax
ret
instead of:
_xor:
movb $1, %al
xorb 4(%esp), %al
movzbl %al, %eax
ret
More impressively, we now compile:
struct B { unsigned bit : 1; };
void xor(struct B *b) { b->bit = b->bit ^ 1; }
To (X86/PPC):
_xor:
movl 4(%esp), %eax
xorl $-2147483648, (%eax)
ret
_xor:
lwz r2, 0(r3)
xoris r2, r2, 32768
stw r2, 0(r3)
blr
instead of (X86/PPC):
_xor:
movl 4(%esp), %eax
movl (%eax), %ecx
movl %ecx, %edx
shrl $31, %edx
# TRUNCATE movb %dl, %dl
xorb $1, %dl
movzbl %dl, %edx
andl $2147483647, %ecx
shll $31, %edx
orl %ecx, %edx
movl %edx, (%eax)
ret
_xor:
lwz r2, 0(r3)
srwi r4, r2, 31
xori r4, r4, 1
rlwimi r2, r4, 31, 0, 0
stw r2, 0(r3)
blr
This implements InstCombine/cast.ll:test30.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@28273 91177308-0d34-0410-b5e6-96231b3b80d8
2006-05-13 02:06:03 +00:00
|
|
|
if (I->getOperand(0)->getType() == Ty)
|
|
|
|
return I->getOperand(0);
|
|
|
|
|
2007-08-02 06:11:14 +00:00
|
|
|
// Otherwise, must be the same type of case, so just reinsert a new one.
|
|
|
|
Res = CastInst::create(cast<CastInst>(I)->getOpcode(), I->getOperand(0),
|
|
|
|
Ty, I->getName());
|
|
|
|
break;
|
2006-11-27 01:05:10 +00:00
|
|
|
default:
|
Implement simple promotion for cast elimination in instcombine. This is
currently very limited, but can be extended in the future. For example,
we now compile:
uint %test30(uint %c1) {
%c2 = cast uint %c1 to ubyte
%c3 = xor ubyte %c2, 1
%c4 = cast ubyte %c3 to uint
ret uint %c4
}
to:
_xor:
movzbl 4(%esp), %eax
xorl $1, %eax
ret
instead of:
_xor:
movb $1, %al
xorb 4(%esp), %al
movzbl %al, %eax
ret
More impressively, we now compile:
struct B { unsigned bit : 1; };
void xor(struct B *b) { b->bit = b->bit ^ 1; }
To (X86/PPC):
_xor:
movl 4(%esp), %eax
xorl $-2147483648, (%eax)
ret
_xor:
lwz r2, 0(r3)
xoris r2, r2, 32768
stw r2, 0(r3)
blr
instead of (X86/PPC):
_xor:
movl 4(%esp), %eax
movl (%eax), %ecx
movl %ecx, %edx
shrl $31, %edx
# TRUNCATE movb %dl, %dl
xorb $1, %dl
movzbl %dl, %edx
andl $2147483647, %ecx
shll $31, %edx
orl %ecx, %edx
movl %edx, (%eax)
ret
_xor:
lwz r2, 0(r3)
srwi r4, r2, 31
xori r4, r4, 1
rlwimi r2, r4, 31, 0, 0
stw r2, 0(r3)
blr
This implements InstCombine/cast.ll:test30.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@28273 91177308-0d34-0410-b5e6-96231b3b80d8
2006-05-13 02:06:03 +00:00
|
|
|
// TODO: Can handle more cases here.
|
|
|
|
assert(0 && "Unreachable!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return InsertNewInstBefore(Res, *I);
|
|
|
|
}
|
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
/// @brief Implement the transforms common to all CastInst visitors.
|
|
|
|
Instruction *InstCombiner::commonCastTransforms(CastInst &CI) {
|
2003-06-23 21:59:52 +00:00
|
|
|
Value *Src = CI.getOperand(0);
|
|
|
|
|
2007-05-11 21:10:54 +00:00
|
|
|
// Many cases of "cast of a cast" are eliminable. If it's eliminable we just
|
2006-11-27 01:05:10 +00:00
|
|
|
// eliminate it now.
|
2005-01-01 16:22:27 +00:00
|
|
|
if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast
|
2006-11-27 01:05:10 +00:00
|
|
|
if (Instruction::CastOps opc =
|
|
|
|
isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), TD)) {
|
|
|
|
// The first cast (CSrc) is eliminable so we need to fix up or replace
|
|
|
|
// the second cast (CI). CSrc will then have a good chance of being dead.
|
|
|
|
return CastInst::create(opc, CSrc->getOperand(0), CI.getType());
|
2002-08-02 20:00:25 +00:00
|
|
|
}
|
|
|
|
}
|
2004-05-25 04:29:21 +00:00
|
|
|
|
2007-04-27 17:44:50 +00:00
|
|
|
// If we are casting a select then fold the cast into the select
|
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(Src))
|
|
|
|
if (Instruction *NV = FoldOpIntoSelect(CI, SI, this))
|
|
|
|
return NV;
|
|
|
|
|
|
|
|
// If we are casting a PHI then fold the cast into the PHI
|
|
|
|
if (isa<PHINode>(Src))
|
|
|
|
if (Instruction *NV = FoldOpIntoPhi(CI))
|
|
|
|
return NV;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Implement the transforms for cast of pointer (bitcast/ptrtoint)
|
|
|
|
Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) {
|
|
|
|
Value *Src = CI.getOperand(0);
|
|
|
|
|
2003-06-23 21:59:52 +00:00
|
|
|
if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) {
|
2007-04-28 00:57:34 +00:00
|
|
|
// If casting the result of a getelementptr instruction with no offset, turn
|
|
|
|
// this into a cast of the original pointer!
|
2007-04-27 17:44:50 +00:00
|
|
|
if (GEP->hasAllZeroIndices()) {
|
2006-11-27 01:05:10 +00:00
|
|
|
// Changing the cast operand is usually not a good idea but it is safe
|
|
|
|
// here because the pointer operand is being replaced with another
|
|
|
|
// pointer operand so the opcode doesn't need to change.
|
2007-04-28 00:57:34 +00:00
|
|
|
AddToWorkList(GEP);
|
2003-06-21 23:12:02 +00:00
|
|
|
CI.setOperand(0, GEP->getOperand(0));
|
|
|
|
return &CI;
|
|
|
|
}
|
2007-04-28 00:57:34 +00:00
|
|
|
|
|
|
|
// If the GEP has a single use, and the base pointer is a bitcast, and the
|
|
|
|
// GEP computes a constant offset, see if we can convert these three
|
|
|
|
// instructions into fewer. This typically happens with unions and other
|
|
|
|
// non-type-safe code.
|
|
|
|
if (GEP->hasOneUse() && isa<BitCastInst>(GEP->getOperand(0))) {
|
|
|
|
if (GEP->hasAllConstantIndices()) {
|
|
|
|
// We are guaranteed to get a constant from EmitGEPOffset.
|
|
|
|
ConstantInt *OffsetV = cast<ConstantInt>(EmitGEPOffset(GEP, CI, *this));
|
|
|
|
int64_t Offset = OffsetV->getSExtValue();
|
|
|
|
|
|
|
|
// Get the base pointer input of the bitcast, and the type it points to.
|
|
|
|
Value *OrigBase = cast<BitCastInst>(GEP->getOperand(0))->getOperand(0);
|
|
|
|
const Type *GEPIdxTy =
|
|
|
|
cast<PointerType>(OrigBase->getType())->getElementType();
|
|
|
|
if (GEPIdxTy->isSized()) {
|
|
|
|
SmallVector<Value*, 8> NewIndices;
|
|
|
|
|
2007-05-05 01:59:31 +00:00
|
|
|
// Start with the index over the outer type. Note that the type size
|
|
|
|
// might be zero (even if the offset isn't zero) if the indexed type
|
|
|
|
// is something like [0 x {int, int}]
|
2007-04-28 00:57:34 +00:00
|
|
|
const Type *IntPtrTy = TD->getIntPtrType();
|
2007-05-05 01:59:31 +00:00
|
|
|
int64_t FirstIdx = 0;
|
Executive summary: getTypeSize -> getTypeStoreSize / getABITypeSize.
The meaning of getTypeSize was not clear - clarifying it is important
now that we have x86 long double and arbitrary precision integers.
The issue with long double is that it requires 80 bits, and this is
not a multiple of its alignment. This gives a primitive type for
which getTypeSize differed from getABITypeSize. For arbitrary precision
integers it is even worse: there is the minimum number of bits needed to
hold the type (eg: 36 for an i36), the maximum number of bits that will
be overwriten when storing the type (40 bits for i36) and the ABI size
(i.e. the storage size rounded up to a multiple of the alignment; 64 bits
for i36).
This patch removes getTypeSize (not really - it is still there but
deprecated to allow for a gradual transition). Instead there is:
(1) getTypeSizeInBits - a number of bits that suffices to hold all
values of the type. For a primitive type, this is the minimum number
of bits. For an i36 this is 36 bits. For x86 long double it is 80.
This corresponds to gcc's TYPE_PRECISION.
(2) getTypeStoreSizeInBits - the maximum number of bits that is
written when storing the type (or read when reading it). For an
i36 this is 40 bits, for an x86 long double it is 80 bits. This
is the size alias analysis is interested in (getTypeStoreSize
returns the number of bytes). There doesn't seem to be anything
corresponding to this in gcc.
(3) getABITypeSizeInBits - this is getTypeStoreSizeInBits rounded
up to a multiple of the alignment. For an i36 this is 64, for an
x86 long double this is 96 or 128 depending on the OS. This is the
spacing between consecutive elements when you form an array out of
this type (getABITypeSize returns the number of bytes). This is
TYPE_SIZE in gcc.
Since successive elements in a SequentialType (arrays, pointers
and vectors) need to be aligned, the spacing between them will be
given by getABITypeSize. This means that the size of an array
is the length times the getABITypeSize. It also means that GEP
computations need to use getABITypeSize when computing offsets.
Furthermore, if an alloca allocates several elements at once then
these too need to be aligned, so the size of the alloca has to be
the number of elements multiplied by getABITypeSize. Logically
speaking this doesn't have to be the case when allocating just
one element, but it is simpler to also use getABITypeSize in this
case. So alloca's and mallocs should use getABITypeSize. Finally,
since gcc's only notion of size is that given by getABITypeSize, if
you want to output assembler etc the same as gcc then getABITypeSize
is the size you want.
Since a store will overwrite no more than getTypeStoreSize bytes,
and a read will read no more than that many bytes, this is the
notion of size appropriate for alias analysis calculations.
In this patch I have corrected all type size uses except some of
those in ScalarReplAggregates, lib/Codegen, lib/Target (the hard
cases). I will get around to auditing these too at some point,
but I could do with some help.
Finally, I made one change which I think wise but others might
consider pointless and suboptimal: in an unpacked struct the
amount of space allocated for a field is now given by the ABI
size rather than getTypeStoreSize. I did this because every
other place that reserves memory for a type (eg: alloca) now
uses getABITypeSize, and I didn't want to make an exception
for unpacked structs, i.e. I did it to make things more uniform.
This only effects structs containing long doubles and arbitrary
precision integers. If someone wants to pack these types more
tightly they can always use a packed struct.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@43620 91177308-0d34-0410-b5e6-96231b3b80d8
2007-11-01 20:53:16 +00:00
|
|
|
if (int64_t TySize = TD->getABITypeSize(GEPIdxTy)) {
|
2007-05-05 01:59:31 +00:00
|
|
|
FirstIdx = Offset/TySize;
|
|
|
|
Offset %= TySize;
|
2007-04-28 00:57:34 +00:00
|
|
|
|
2007-05-05 01:59:31 +00:00
|
|
|
// Handle silly modulus not returning values values [0..TySize).
|
|
|
|
if (Offset < 0) {
|
|
|
|
--FirstIdx;
|
|
|
|
Offset += TySize;
|
|
|
|
assert(Offset >= 0);
|
|
|
|
}
|
2007-05-05 22:32:24 +00:00
|
|
|
assert((uint64_t)Offset < (uint64_t)TySize &&"Out of range offset");
|
2007-04-28 00:57:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NewIndices.push_back(ConstantInt::get(IntPtrTy, FirstIdx));
|
|
|
|
|
|
|
|
// Index into the types. If we fail, set OrigBase to null.
|
|
|
|
while (Offset) {
|
|
|
|
if (const StructType *STy = dyn_cast<StructType>(GEPIdxTy)) {
|
|
|
|
const StructLayout *SL = TD->getStructLayout(STy);
|
2007-05-15 00:16:00 +00:00
|
|
|
if (Offset < (int64_t)SL->getSizeInBytes()) {
|
|
|
|
unsigned Elt = SL->getElementContainingOffset(Offset);
|
|
|
|
NewIndices.push_back(ConstantInt::get(Type::Int32Ty, Elt));
|
2007-04-28 00:57:34 +00:00
|
|
|
|
2007-05-15 00:16:00 +00:00
|
|
|
Offset -= SL->getElementOffset(Elt);
|
|
|
|
GEPIdxTy = STy->getElementType(Elt);
|
|
|
|
} else {
|
|
|
|
// Otherwise, we can't index into this, bail out.
|
|
|
|
Offset = 0;
|
|
|
|
OrigBase = 0;
|
|
|
|
}
|
2007-04-28 00:57:34 +00:00
|
|
|
} else if (isa<ArrayType>(GEPIdxTy) || isa<VectorType>(GEPIdxTy)) {
|
|
|
|
const SequentialType *STy = cast<SequentialType>(GEPIdxTy);
|
Executive summary: getTypeSize -> getTypeStoreSize / getABITypeSize.
The meaning of getTypeSize was not clear - clarifying it is important
now that we have x86 long double and arbitrary precision integers.
The issue with long double is that it requires 80 bits, and this is
not a multiple of its alignment. This gives a primitive type for
which getTypeSize differed from getABITypeSize. For arbitrary precision
integers it is even worse: there is the minimum number of bits needed to
hold the type (eg: 36 for an i36), the maximum number of bits that will
be overwriten when storing the type (40 bits for i36) and the ABI size
(i.e. the storage size rounded up to a multiple of the alignment; 64 bits
for i36).
This patch removes getTypeSize (not really - it is still there but
deprecated to allow for a gradual transition). Instead there is:
(1) getTypeSizeInBits - a number of bits that suffices to hold all
values of the type. For a primitive type, this is the minimum number
of bits. For an i36 this is 36 bits. For x86 long double it is 80.
This corresponds to gcc's TYPE_PRECISION.
(2) getTypeStoreSizeInBits - the maximum number of bits that is
written when storing the type (or read when reading it). For an
i36 this is 40 bits, for an x86 long double it is 80 bits. This
is the size alias analysis is interested in (getTypeStoreSize
returns the number of bytes). There doesn't seem to be anything
corresponding to this in gcc.
(3) getABITypeSizeInBits - this is getTypeStoreSizeInBits rounded
up to a multiple of the alignment. For an i36 this is 64, for an
x86 long double this is 96 or 128 depending on the OS. This is the
spacing between consecutive elements when you form an array out of
this type (getABITypeSize returns the number of bytes). This is
TYPE_SIZE in gcc.
Since successive elements in a SequentialType (arrays, pointers
and vectors) need to be aligned, the spacing between them will be
given by getABITypeSize. This means that the size of an array
is the length times the getABITypeSize. It also means that GEP
computations need to use getABITypeSize when computing offsets.
Furthermore, if an alloca allocates several elements at once then
these too need to be aligned, so the size of the alloca has to be
the number of elements multiplied by getABITypeSize. Logically
speaking this doesn't have to be the case when allocating just
one element, but it is simpler to also use getABITypeSize in this
case. So alloca's and mallocs should use getABITypeSize. Finally,
since gcc's only notion of size is that given by getABITypeSize, if
you want to output assembler etc the same as gcc then getABITypeSize
is the size you want.
Since a store will overwrite no more than getTypeStoreSize bytes,
and a read will read no more than that many bytes, this is the
notion of size appropriate for alias analysis calculations.
In this patch I have corrected all type size uses except some of
those in ScalarReplAggregates, lib/Codegen, lib/Target (the hard
cases). I will get around to auditing these too at some point,
but I could do with some help.
Finally, I made one change which I think wise but others might
consider pointless and suboptimal: in an unpacked struct the
amount of space allocated for a field is now given by the ABI
size rather than getTypeStoreSize. I did this because every
other place that reserves memory for a type (eg: alloca) now
uses getABITypeSize, and I didn't want to make an exception
for unpacked structs, i.e. I did it to make things more uniform.
This only effects structs containing long doubles and arbitrary
precision integers. If someone wants to pack these types more
tightly they can always use a packed struct.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@43620 91177308-0d34-0410-b5e6-96231b3b80d8
2007-11-01 20:53:16 +00:00
|
|
|
if (uint64_t EltSize = TD->getABITypeSize(STy->getElementType())){
|
2007-05-15 00:16:00 +00:00
|
|
|
NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize));
|
|
|
|
Offset %= EltSize;
|
|
|
|
} else {
|
|
|
|
NewIndices.push_back(ConstantInt::get(IntPtrTy, 0));
|
|
|
|
}
|
2007-04-28 00:57:34 +00:00
|
|
|
GEPIdxTy = STy->getElementType();
|
|
|
|
} else {
|
|
|
|
// Otherwise, we can't index into this, bail out.
|
|
|
|
Offset = 0;
|
|
|
|
OrigBase = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (OrigBase) {
|
|
|
|
// If we were able to index down into an element, create the GEP
|
|
|
|
// and bitcast the result. This eliminates one bitcast, potentially
|
|
|
|
// two.
|
2008-04-06 20:25:17 +00:00
|
|
|
Instruction *NGEP = GetElementPtrInst::Create(OrigBase,
|
|
|
|
NewIndices.begin(),
|
|
|
|
NewIndices.end(), "");
|
2007-04-28 00:57:34 +00:00
|
|
|
InsertNewInstBefore(NGEP, CI);
|
|
|
|
NGEP->takeName(GEP);
|
|
|
|
|
|
|
|
if (isa<BitCastInst>(CI))
|
|
|
|
return new BitCastInst(NGEP, CI.getType());
|
|
|
|
assert(isa<PtrToIntInst>(CI));
|
|
|
|
return new PtrToIntInst(NGEP, CI.getType());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-06-21 23:12:02 +00:00
|
|
|
}
|
2006-11-21 17:05:13 +00:00
|
|
|
|
2007-04-27 17:44:50 +00:00
|
|
|
return commonCastTransforms(CI);
|
|
|
|
}
|
2003-11-02 05:57:39 +00:00
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
|
Fold binary expressions and casts into PHI nodes that have all constant inputs.
This takes something like this:
%A = phi int [ 3, %cond_false.0 ], [ 2, %endif.0.i ], [ 2, %endif.1.i ]
%B = div int %tmp.243, 4
and turns it into:
%A = phi int [ 3/4, %cond_false.0 ], [ 2/4, %endif.0.i ], [ 2/4, %endif.1.i ]
which is later simplified (in this case) into %A = 0.
This triggers thousands of times in spec, for example, 269 times in 176.gcc.
This is tested by InstCombine/add.ll:test23 and set.ll:test18.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16582 91177308-0d34-0410-b5e6-96231b3b80d8
2004-09-29 05:07:12 +00:00
|
|
|
|
2007-03-03 05:27:34 +00:00
|
|
|
/// Only the TRUNC, ZEXT, SEXT, and BITCAST can both operand and result as
|
|
|
|
/// integer types. This function implements the common transforms for all those
|
2006-11-27 01:05:10 +00:00
|
|
|
/// cases.
|
|
|
|
/// @brief Implement the transforms common to CastInst with integer operands
|
|
|
|
Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) {
|
|
|
|
if (Instruction *Result = commonCastTransforms(CI))
|
|
|
|
return Result;
|
|
|
|
|
|
|
|
Value *Src = CI.getOperand(0);
|
|
|
|
const Type *SrcTy = Src->getType();
|
|
|
|
const Type *DestTy = CI.getType();
|
2007-04-02 08:20:41 +00:00
|
|
|
uint32_t SrcBitSize = SrcTy->getPrimitiveSizeInBits();
|
|
|
|
uint32_t DestBitSize = DestTy->getPrimitiveSizeInBits();
|
2006-11-27 01:05:10 +00:00
|
|
|
|
|
|
|
// See if we can simplify any instructions used by the LHS whose sole
|
|
|
|
// purpose is to compute bits we don't care about.
|
2007-03-22 20:56:53 +00:00
|
|
|
APInt KnownZero(DestBitSize, 0), KnownOne(DestBitSize, 0);
|
|
|
|
if (SimplifyDemandedBits(&CI, APInt::getAllOnesValue(DestBitSize),
|
2006-11-27 01:05:10 +00:00
|
|
|
KnownZero, KnownOne))
|
|
|
|
return &CI;
|
|
|
|
|
|
|
|
// If the source isn't an instruction or has more than one use then we
|
|
|
|
// can't do anything more.
|
2006-12-23 06:05:41 +00:00
|
|
|
Instruction *SrcI = dyn_cast<Instruction>(Src);
|
|
|
|
if (!SrcI || !Src->hasOneUse())
|
2006-11-27 01:05:10 +00:00
|
|
|
return 0;
|
|
|
|
|
2007-03-03 05:27:34 +00:00
|
|
|
// Attempt to propagate the cast into the instruction for int->int casts.
|
2006-11-27 01:05:10 +00:00
|
|
|
int NumCastsRemoved = 0;
|
2007-03-03 05:27:34 +00:00
|
|
|
if (!isa<BitCastInst>(CI) &&
|
|
|
|
CanEvaluateInDifferentType(SrcI, cast<IntegerType>(DestTy),
|
2007-08-02 06:11:14 +00:00
|
|
|
CI.getOpcode(), NumCastsRemoved)) {
|
2006-11-27 01:05:10 +00:00
|
|
|
// If this cast is a truncate, evaluting in a different type always
|
2007-08-02 06:11:14 +00:00
|
|
|
// eliminates the cast, so it is always a win. If this is a zero-extension,
|
|
|
|
// we need to do an AND to maintain the clear top-part of the computation,
|
|
|
|
// so we require that the input have eliminated at least one cast. If this
|
|
|
|
// is a sign extension, we insert two new casts (to do the extension) so we
|
2006-11-27 01:05:10 +00:00
|
|
|
// require that two casts have been eliminated.
|
2007-03-03 05:27:34 +00:00
|
|
|
bool DoXForm;
|
|
|
|
switch (CI.getOpcode()) {
|
|
|
|
default:
|
|
|
|
// All the others use floating point so we shouldn't actually
|
|
|
|
// get here because of the check above.
|
|
|
|
assert(0 && "Unknown cast type");
|
|
|
|
case Instruction::Trunc:
|
|
|
|
DoXForm = true;
|
|
|
|
break;
|
|
|
|
case Instruction::ZExt:
|
|
|
|
DoXForm = NumCastsRemoved >= 1;
|
|
|
|
break;
|
|
|
|
case Instruction::SExt:
|
|
|
|
DoXForm = NumCastsRemoved >= 2;
|
|
|
|
break;
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (DoXForm) {
|
2006-12-13 18:21:21 +00:00
|
|
|
Value *Res = EvaluateInDifferentType(SrcI, DestTy,
|
|
|
|
CI.getOpcode() == Instruction::SExt);
|
2006-11-27 01:05:10 +00:00
|
|
|
assert(Res->getType() == DestTy);
|
|
|
|
switch (CI.getOpcode()) {
|
|
|
|
default: assert(0 && "Unknown cast type!");
|
|
|
|
case Instruction::Trunc:
|
|
|
|
case Instruction::BitCast:
|
|
|
|
// Just replace this cast with the result.
|
|
|
|
return ReplaceInstUsesWith(CI, Res);
|
|
|
|
case Instruction::ZExt: {
|
|
|
|
// We need to emit an AND to clear the high bits.
|
|
|
|
assert(SrcBitSize < DestBitSize && "Not a zext?");
|
2007-04-02 05:48:58 +00:00
|
|
|
Constant *C = ConstantInt::get(APInt::getLowBitsSet(DestBitSize,
|
|
|
|
SrcBitSize));
|
2006-11-27 01:05:10 +00:00
|
|
|
return BinaryOperator::createAnd(Res, C);
|
|
|
|
}
|
|
|
|
case Instruction::SExt:
|
|
|
|
// We need to emit a cast to truncate, then a cast to sext.
|
|
|
|
return CastInst::create(Instruction::SExt,
|
2006-12-12 09:18:51 +00:00
|
|
|
InsertCastBefore(Instruction::Trunc, Res, Src->getType(),
|
|
|
|
CI), DestTy);
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0;
|
|
|
|
Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0;
|
|
|
|
|
|
|
|
switch (SrcI->getOpcode()) {
|
|
|
|
case Instruction::Add:
|
|
|
|
case Instruction::Mul:
|
|
|
|
case Instruction::And:
|
|
|
|
case Instruction::Or:
|
|
|
|
case Instruction::Xor:
|
2007-04-03 17:43:25 +00:00
|
|
|
// If we are discarding information, rewrite.
|
2006-11-27 01:05:10 +00:00
|
|
|
if (DestBitSize <= SrcBitSize && DestBitSize != 1) {
|
|
|
|
// Don't insert two casts if they cannot be eliminated. We allow
|
|
|
|
// two casts to be inserted if the sizes are the same. This could
|
|
|
|
// only be converting signedness, which is a noop.
|
|
|
|
if (DestBitSize == SrcBitSize ||
|
2006-12-23 06:05:41 +00:00
|
|
|
!ValueRequiresCast(CI.getOpcode(), Op1, DestTy,TD) ||
|
|
|
|
!ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) {
|
2006-12-13 17:19:09 +00:00
|
|
|
Instruction::CastOps opcode = CI.getOpcode();
|
2006-12-12 09:18:51 +00:00
|
|
|
Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI);
|
|
|
|
Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI);
|
|
|
|
return BinaryOperator::create(
|
|
|
|
cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c);
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cast (xor bool X, true) to int --> xor (cast bool X to int), 1
|
|
|
|
if (isa<ZExtInst>(CI) && SrcBitSize == 1 &&
|
|
|
|
SrcI->getOpcode() == Instruction::Xor &&
|
2007-01-11 12:24:14 +00:00
|
|
|
Op1 == ConstantInt::getTrue() &&
|
2006-12-23 06:05:41 +00:00
|
|
|
(!Op0->hasOneUse() || !isa<CmpInst>(Op0))) {
|
2006-12-12 09:18:51 +00:00
|
|
|
Value *New = InsertOperandCastBefore(Instruction::ZExt, Op0, DestTy, &CI);
|
2006-11-27 01:05:10 +00:00
|
|
|
return BinaryOperator::createXor(New, ConstantInt::get(CI.getType(), 1));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Instruction::SDiv:
|
|
|
|
case Instruction::UDiv:
|
|
|
|
case Instruction::SRem:
|
|
|
|
case Instruction::URem:
|
|
|
|
// If we are just changing the sign, rewrite.
|
|
|
|
if (DestBitSize == SrcBitSize) {
|
|
|
|
// Don't insert two casts if they cannot be eliminated. We allow
|
|
|
|
// two casts to be inserted if the sizes are the same. This could
|
|
|
|
// only be converting signedness, which is a noop.
|
2006-12-23 06:05:41 +00:00
|
|
|
if (!ValueRequiresCast(CI.getOpcode(), Op1, DestTy, TD) ||
|
|
|
|
!ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) {
|
2006-12-12 09:18:51 +00:00
|
|
|
Value *Op0c = InsertOperandCastBefore(Instruction::BitCast,
|
|
|
|
Op0, DestTy, SrcI);
|
|
|
|
Value *Op1c = InsertOperandCastBefore(Instruction::BitCast,
|
|
|
|
Op1, DestTy, SrcI);
|
2006-11-27 01:05:10 +00:00
|
|
|
return BinaryOperator::create(
|
|
|
|
cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Instruction::Shl:
|
|
|
|
// Allow changing the sign of the source operand. Do not allow
|
|
|
|
// changing the size of the shift, UNLESS the shift amount is a
|
|
|
|
// constant. We must not change variable sized shifts to a smaller
|
|
|
|
// size, because it is undefined to shift more bits out than exist
|
|
|
|
// in the value.
|
|
|
|
if (DestBitSize == SrcBitSize ||
|
|
|
|
(DestBitSize < SrcBitSize && isa<Constant>(Op1))) {
|
2006-12-12 09:18:51 +00:00
|
|
|
Instruction::CastOps opcode = (DestBitSize == SrcBitSize ?
|
|
|
|
Instruction::BitCast : Instruction::Trunc);
|
|
|
|
Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI);
|
2007-02-02 02:16:23 +00:00
|
|
|
Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI);
|
2007-02-02 14:08:20 +00:00
|
|
|
return BinaryOperator::createShl(Op0c, Op1c);
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Instruction::AShr:
|
|
|
|
// If this is a signed shr, and if all bits shifted in are about to be
|
|
|
|
// truncated off, turn it into an unsigned shr to allow greater
|
|
|
|
// simplifications.
|
|
|
|
if (DestBitSize < SrcBitSize &&
|
|
|
|
isa<ConstantInt>(Op1)) {
|
2007-03-30 17:20:39 +00:00
|
|
|
uint32_t ShiftAmt = cast<ConstantInt>(Op1)->getLimitedValue(SrcBitSize);
|
2006-11-27 01:05:10 +00:00
|
|
|
if (SrcBitSize > ShiftAmt && SrcBitSize-ShiftAmt >= DestBitSize) {
|
|
|
|
// Insert the new logical shift right.
|
2007-02-02 14:08:20 +00:00
|
|
|
return BinaryOperator::createLShr(Op0, Op1);
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2003-07-24 17:35:25 +00:00
|
|
|
|
2007-04-11 06:57:46 +00:00
|
|
|
Instruction *InstCombiner::visitTrunc(TruncInst &CI) {
|
2006-11-29 07:04:07 +00:00
|
|
|
if (Instruction *Result = commonIntCastTransforms(CI))
|
|
|
|
return Result;
|
|
|
|
|
|
|
|
Value *Src = CI.getOperand(0);
|
|
|
|
const Type *Ty = CI.getType();
|
2007-04-02 08:20:41 +00:00
|
|
|
uint32_t DestBitWidth = Ty->getPrimitiveSizeInBits();
|
|
|
|
uint32_t SrcBitWidth = cast<IntegerType>(Src->getType())->getBitWidth();
|
2006-11-29 07:04:07 +00:00
|
|
|
|
|
|
|
if (Instruction *SrcI = dyn_cast<Instruction>(Src)) {
|
|
|
|
switch (SrcI->getOpcode()) {
|
|
|
|
default: break;
|
|
|
|
case Instruction::LShr:
|
|
|
|
// We can shrink lshr to something smaller if we know the bits shifted in
|
|
|
|
// are already zeros.
|
|
|
|
if (ConstantInt *ShAmtV = dyn_cast<ConstantInt>(SrcI->getOperand(1))) {
|
2007-03-30 17:20:39 +00:00
|
|
|
uint32_t ShAmt = ShAmtV->getLimitedValue(SrcBitWidth);
|
2006-11-29 07:04:07 +00:00
|
|
|
|
|
|
|
// Get a mask for the bits shifting in.
|
2007-03-28 09:19:01 +00:00
|
|
|
APInt Mask(APInt::getLowBitsSet(SrcBitWidth, ShAmt).shl(DestBitWidth));
|
2006-12-12 09:18:51 +00:00
|
|
|
Value* SrcIOp0 = SrcI->getOperand(0);
|
|
|
|
if (SrcI->hasOneUse() && MaskedValueIsZero(SrcIOp0, Mask)) {
|
2006-11-29 07:04:07 +00:00
|
|
|
if (ShAmt >= DestBitWidth) // All zeros.
|
|
|
|
return ReplaceInstUsesWith(CI, Constant::getNullValue(Ty));
|
|
|
|
|
|
|
|
// Okay, we can shrink this. Truncate the input, then return a new
|
|
|
|
// shift.
|
2007-02-02 02:16:23 +00:00
|
|
|
Value *V1 = InsertCastBefore(Instruction::Trunc, SrcIOp0, Ty, CI);
|
|
|
|
Value *V2 = InsertCastBefore(Instruction::Trunc, SrcI->getOperand(1),
|
|
|
|
Ty, CI);
|
2007-02-02 14:08:20 +00:00
|
|
|
return BinaryOperator::createLShr(V1, V2);
|
2006-11-29 07:04:07 +00:00
|
|
|
}
|
2006-12-05 01:26:29 +00:00
|
|
|
} else { // This is a variable shr.
|
|
|
|
|
|
|
|
// Turn 'trunc (lshr X, Y) to bool' into '(X & (1 << Y)) != 0'. This is
|
|
|
|
// more LLVM instructions, but allows '1 << Y' to be hoisted if
|
|
|
|
// loop-invariant and CSE'd.
|
2007-01-11 18:21:29 +00:00
|
|
|
if (CI.getType() == Type::Int1Ty && SrcI->hasOneUse()) {
|
2006-12-05 01:26:29 +00:00
|
|
|
Value *One = ConstantInt::get(SrcI->getType(), 1);
|
|
|
|
|
2007-02-02 02:16:23 +00:00
|
|
|
Value *V = InsertNewInstBefore(
|
2007-02-02 14:08:20 +00:00
|
|
|
BinaryOperator::createShl(One, SrcI->getOperand(1),
|
2007-02-02 02:16:23 +00:00
|
|
|
"tmp"), CI);
|
2006-12-05 01:26:29 +00:00
|
|
|
V = InsertNewInstBefore(BinaryOperator::createAnd(V,
|
|
|
|
SrcI->getOperand(0),
|
|
|
|
"tmp"), CI);
|
|
|
|
Value *Zero = Constant::getNullValue(V->getType());
|
2006-12-23 06:05:41 +00:00
|
|
|
return new ICmpInst(ICmpInst::ICMP_NE, V, Zero);
|
2006-12-05 01:26:29 +00:00
|
|
|
}
|
2006-11-29 07:04:07 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
2003-07-24 17:35:25 +00:00
|
|
|
|
2008-03-24 00:21:34 +00:00
|
|
|
/// transformZExtICmp - Transform (zext icmp) to bitwise / integer operations
|
|
|
|
/// in order to eliminate the icmp.
|
|
|
|
Instruction *InstCombiner::transformZExtICmp(ICmpInst *ICI, Instruction &CI,
|
|
|
|
bool DoXform) {
|
|
|
|
// If we are just checking for a icmp eq of a single bit and zext'ing it
|
|
|
|
// to an integer, then shift the bit to the appropriate place and then
|
|
|
|
// cast to integer to avoid the comparison.
|
|
|
|
if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
|
|
|
|
const APInt &Op1CV = Op1C->getValue();
|
|
|
|
|
|
|
|
// zext (x <s 0) to i32 --> x>>u31 true if signbit set.
|
|
|
|
// zext (x >s -1) to i32 --> (x>>u31)^1 true if signbit clear.
|
|
|
|
if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) ||
|
|
|
|
(ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())) {
|
|
|
|
if (!DoXform) return ICI;
|
|
|
|
|
|
|
|
Value *In = ICI->getOperand(0);
|
|
|
|
Value *Sh = ConstantInt::get(In->getType(),
|
|
|
|
In->getType()->getPrimitiveSizeInBits()-1);
|
|
|
|
In = InsertNewInstBefore(BinaryOperator::createLShr(In, Sh,
|
|
|
|
In->getName()+".lobit"),
|
|
|
|
CI);
|
|
|
|
if (In->getType() != CI.getType())
|
|
|
|
In = CastInst::createIntegerCast(In, CI.getType(),
|
|
|
|
false/*ZExt*/, "tmp", &CI);
|
|
|
|
|
|
|
|
if (ICI->getPredicate() == ICmpInst::ICMP_SGT) {
|
|
|
|
Constant *One = ConstantInt::get(In->getType(), 1);
|
|
|
|
In = InsertNewInstBefore(BinaryOperator::createXor(In, One,
|
|
|
|
In->getName()+".not"),
|
|
|
|
CI);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ReplaceInstUsesWith(CI, In);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// zext (X == 0) to i32 --> X^1 iff X has only the low bit set.
|
|
|
|
// zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set.
|
|
|
|
// zext (X == 1) to i32 --> X iff X has only the low bit set.
|
|
|
|
// zext (X == 2) to i32 --> X>>1 iff X has only the 2nd bit set.
|
|
|
|
// zext (X != 0) to i32 --> X iff X has only the low bit set.
|
|
|
|
// zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set.
|
|
|
|
// zext (X != 1) to i32 --> X^1 iff X has only the low bit set.
|
|
|
|
// zext (X != 2) to i32 --> (X>>1)^1 iff X has only the 2nd bit set.
|
|
|
|
if ((Op1CV == 0 || Op1CV.isPowerOf2()) &&
|
|
|
|
// This only works for EQ and NE
|
|
|
|
ICI->isEquality()) {
|
|
|
|
// If Op1C some other power of two, convert:
|
|
|
|
uint32_t BitWidth = Op1C->getType()->getBitWidth();
|
|
|
|
APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
|
|
|
|
APInt TypeMask(APInt::getAllOnesValue(BitWidth));
|
|
|
|
ComputeMaskedBits(ICI->getOperand(0), TypeMask, KnownZero, KnownOne);
|
|
|
|
|
|
|
|
APInt KnownZeroMask(~KnownZero);
|
|
|
|
if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1?
|
|
|
|
if (!DoXform) return ICI;
|
|
|
|
|
|
|
|
bool isNE = ICI->getPredicate() == ICmpInst::ICMP_NE;
|
|
|
|
if (Op1CV != 0 && (Op1CV != KnownZeroMask)) {
|
|
|
|
// (X&4) == 2 --> false
|
|
|
|
// (X&4) != 2 --> true
|
|
|
|
Constant *Res = ConstantInt::get(Type::Int1Ty, isNE);
|
|
|
|
Res = ConstantExpr::getZExt(Res, CI.getType());
|
|
|
|
return ReplaceInstUsesWith(CI, Res);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ShiftAmt = KnownZeroMask.logBase2();
|
|
|
|
Value *In = ICI->getOperand(0);
|
|
|
|
if (ShiftAmt) {
|
|
|
|
// Perform a logical shr by shiftamt.
|
|
|
|
// Insert the shift to put the result in the low bit.
|
|
|
|
In = InsertNewInstBefore(BinaryOperator::createLShr(In,
|
|
|
|
ConstantInt::get(In->getType(), ShiftAmt),
|
|
|
|
In->getName()+".lobit"), CI);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((Op1CV != 0) == isNE) { // Toggle the low bit.
|
|
|
|
Constant *One = ConstantInt::get(In->getType(), 1);
|
|
|
|
In = BinaryOperator::createXor(In, One, "tmp");
|
|
|
|
InsertNewInstBefore(cast<Instruction>(In), CI);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CI.getType() == In->getType())
|
|
|
|
return ReplaceInstUsesWith(CI, In);
|
|
|
|
else
|
|
|
|
return CastInst::createIntegerCast(In, CI.getType(), false/*ZExt*/);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-11 06:57:46 +00:00
|
|
|
Instruction *InstCombiner::visitZExt(ZExtInst &CI) {
|
2006-11-27 01:05:10 +00:00
|
|
|
// If one of the common conversion will work ..
|
|
|
|
if (Instruction *Result = commonIntCastTransforms(CI))
|
|
|
|
return Result;
|
2005-05-06 02:07:39 +00:00
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
Value *Src = CI.getOperand(0);
|
2006-10-26 06:15:43 +00:00
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
// If this is a cast of a cast
|
|
|
|
if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast
|
|
|
|
// If this is a TRUNC followed by a ZEXT then we are dealing with integral
|
|
|
|
// types and if the sizes are just right we can convert this into a logical
|
|
|
|
// 'and' which will be much cheaper than the pair of casts.
|
|
|
|
if (isa<TruncInst>(CSrc)) {
|
|
|
|
// Get the sizes of the types involved
|
|
|
|
Value *A = CSrc->getOperand(0);
|
2007-04-02 08:20:41 +00:00
|
|
|
uint32_t SrcSize = A->getType()->getPrimitiveSizeInBits();
|
|
|
|
uint32_t MidSize = CSrc->getType()->getPrimitiveSizeInBits();
|
|
|
|
uint32_t DstSize = CI.getType()->getPrimitiveSizeInBits();
|
2006-11-27 01:05:10 +00:00
|
|
|
// If we're actually extending zero bits and the trunc is a no-op
|
|
|
|
if (MidSize < DstSize && SrcSize == DstSize) {
|
|
|
|
// Replace both of the casts with an And of the type mask.
|
2007-03-28 09:19:01 +00:00
|
|
|
APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
|
2007-03-22 20:56:53 +00:00
|
|
|
Constant *AndConst = ConstantInt::get(AndValue);
|
2006-11-27 01:05:10 +00:00
|
|
|
Instruction *And =
|
|
|
|
BinaryOperator::createAnd(CSrc->getOperand(0), AndConst);
|
|
|
|
// Unfortunately, if the type changed, we need to cast it back.
|
|
|
|
if (And->getType() != CI.getType()) {
|
|
|
|
And->setName(CSrc->getName()+".mask");
|
|
|
|
InsertNewInstBefore(And, CI);
|
2006-12-12 23:36:14 +00:00
|
|
|
And = CastInst::createIntegerCast(And, CI.getType(), false/*ZExt*/);
|
Implement shift.ll:test23. If we are shifting right then immediately truncating
the result, turn signed shift rights into unsigned shift rights if possible.
This leads to later simplification and happens *often* in 176.gcc. For example,
this testcase:
struct xxx { unsigned int code : 8; };
enum codes { A, B, C, D, E, F };
int foo(struct xxx *P) {
if ((enum codes)P->code == A)
bar();
}
used to be compiled to:
int %foo(%struct.xxx* %P) {
%tmp.1 = getelementptr %struct.xxx* %P, int 0, uint 0 ; <uint*> [#uses=1]
%tmp.2 = load uint* %tmp.1 ; <uint> [#uses=1]
%tmp.3 = cast uint %tmp.2 to int ; <int> [#uses=1]
%tmp.4 = shl int %tmp.3, ubyte 24 ; <int> [#uses=1]
%tmp.5 = shr int %tmp.4, ubyte 24 ; <int> [#uses=1]
%tmp.6 = cast int %tmp.5 to sbyte ; <sbyte> [#uses=1]
%tmp.8 = seteq sbyte %tmp.6, 0 ; <bool> [#uses=1]
br bool %tmp.8, label %then, label %UnifiedReturnBlock
Now it is compiled to:
%tmp.1 = getelementptr %struct.xxx* %P, int 0, uint 0 ; <uint*> [#uses=1]
%tmp.2 = load uint* %tmp.1 ; <uint> [#uses=1]
%tmp.2 = cast uint %tmp.2 to sbyte ; <sbyte> [#uses=1]
%tmp.8 = seteq sbyte %tmp.2, 0 ; <bool> [#uses=1]
br bool %tmp.8, label %then, label %UnifiedReturnBlock
which is the difference between this:
foo:
subl $4, %esp
movl 8(%esp), %eax
movl (%eax), %eax
shll $24, %eax
sarl $24, %eax
testb %al, %al
jne .LBBfoo_2
and this:
foo:
subl $4, %esp
movl 8(%esp), %eax
movl (%eax), %eax
testb %al, %al
jne .LBBfoo_2
This occurs 3243 times total in the External tests, 215x in povray,
6x in each f2c'd program, 1451x in 176.gcc, 7x in crafty, 20x in perl,
25x in gap, 3x in m88ksim, 25x in ijpeg.
Maybe this will cause a little jump on gcc tommorow :)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@21715 91177308-0d34-0410-b5e6-96231b3b80d8
2005-05-06 04:18:52 +00:00
|
|
|
}
|
2006-11-27 01:05:10 +00:00
|
|
|
return And;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
Implement shift.ll:test23. If we are shifting right then immediately truncating
the result, turn signed shift rights into unsigned shift rights if possible.
This leads to later simplification and happens *often* in 176.gcc. For example,
this testcase:
struct xxx { unsigned int code : 8; };
enum codes { A, B, C, D, E, F };
int foo(struct xxx *P) {
if ((enum codes)P->code == A)
bar();
}
used to be compiled to:
int %foo(%struct.xxx* %P) {
%tmp.1 = getelementptr %struct.xxx* %P, int 0, uint 0 ; <uint*> [#uses=1]
%tmp.2 = load uint* %tmp.1 ; <uint> [#uses=1]
%tmp.3 = cast uint %tmp.2 to int ; <int> [#uses=1]
%tmp.4 = shl int %tmp.3, ubyte 24 ; <int> [#uses=1]
%tmp.5 = shr int %tmp.4, ubyte 24 ; <int> [#uses=1]
%tmp.6 = cast int %tmp.5 to sbyte ; <sbyte> [#uses=1]
%tmp.8 = seteq sbyte %tmp.6, 0 ; <bool> [#uses=1]
br bool %tmp.8, label %then, label %UnifiedReturnBlock
Now it is compiled to:
%tmp.1 = getelementptr %struct.xxx* %P, int 0, uint 0 ; <uint*> [#uses=1]
%tmp.2 = load uint* %tmp.1 ; <uint> [#uses=1]
%tmp.2 = cast uint %tmp.2 to sbyte ; <sbyte> [#uses=1]
%tmp.8 = seteq sbyte %tmp.2, 0 ; <bool> [#uses=1]
br bool %tmp.8, label %then, label %UnifiedReturnBlock
which is the difference between this:
foo:
subl $4, %esp
movl 8(%esp), %eax
movl (%eax), %eax
shll $24, %eax
sarl $24, %eax
testb %al, %al
jne .LBBfoo_2
and this:
foo:
subl $4, %esp
movl 8(%esp), %eax
movl (%eax), %eax
testb %al, %al
jne .LBBfoo_2
This occurs 3243 times total in the External tests, 215x in povray,
6x in each f2c'd program, 1451x in 176.gcc, 7x in crafty, 20x in perl,
25x in gap, 3x in m88ksim, 25x in ijpeg.
Maybe this will cause a little jump on gcc tommorow :)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@21715 91177308-0d34-0410-b5e6-96231b3b80d8
2005-05-06 04:18:52 +00:00
|
|
|
|
2008-03-24 00:21:34 +00:00
|
|
|
if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src))
|
|
|
|
return transformZExtICmp(ICI, CI);
|
2007-04-11 06:53:04 +00:00
|
|
|
|
2008-03-24 00:21:34 +00:00
|
|
|
BinaryOperator *SrcI = dyn_cast<BinaryOperator>(Src);
|
|
|
|
if (SrcI && SrcI->getOpcode() == Instruction::Or) {
|
|
|
|
// zext (or icmp, icmp) --> or (zext icmp), (zext icmp) if at least one
|
|
|
|
// of the (zext icmp) will be transformed.
|
|
|
|
ICmpInst *LHS = dyn_cast<ICmpInst>(SrcI->getOperand(0));
|
|
|
|
ICmpInst *RHS = dyn_cast<ICmpInst>(SrcI->getOperand(1));
|
|
|
|
if (LHS && RHS && LHS->hasOneUse() && RHS->hasOneUse() &&
|
|
|
|
(transformZExtICmp(LHS, CI, false) ||
|
|
|
|
transformZExtICmp(RHS, CI, false))) {
|
|
|
|
Value *LCast = InsertCastBefore(Instruction::ZExt, LHS, CI.getType(), CI);
|
|
|
|
Value *RCast = InsertCastBefore(Instruction::ZExt, RHS, CI.getType(), CI);
|
|
|
|
return BinaryOperator::create(Instruction::Or, LCast, RCast);
|
2007-04-11 05:45:39 +00:00
|
|
|
}
|
2008-03-24 00:21:34 +00:00
|
|
|
}
|
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-11 06:57:46 +00:00
|
|
|
Instruction *InstCombiner::visitSExt(SExtInst &CI) {
|
2007-04-11 06:12:58 +00:00
|
|
|
if (Instruction *I = commonIntCastTransforms(CI))
|
|
|
|
return I;
|
|
|
|
|
2007-04-11 06:57:46 +00:00
|
|
|
Value *Src = CI.getOperand(0);
|
|
|
|
|
|
|
|
// sext (x <s 0) -> ashr x, 31 -> all ones if signed
|
|
|
|
// sext (x >s -1) -> ashr x, 31 -> all ones if not signed
|
|
|
|
if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) {
|
|
|
|
// If we are just checking for a icmp eq of a single bit and zext'ing it
|
|
|
|
// to an integer, then shift the bit to the appropriate place and then
|
|
|
|
// cast to integer to avoid the comparison.
|
|
|
|
if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
|
|
|
|
const APInt &Op1CV = Op1C->getValue();
|
|
|
|
|
|
|
|
// sext (x <s 0) to i32 --> x>>s31 true if signbit set.
|
|
|
|
// sext (x >s -1) to i32 --> (x>>s31)^-1 true if signbit clear.
|
|
|
|
if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) ||
|
|
|
|
(ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())){
|
|
|
|
Value *In = ICI->getOperand(0);
|
|
|
|
Value *Sh = ConstantInt::get(In->getType(),
|
|
|
|
In->getType()->getPrimitiveSizeInBits()-1);
|
|
|
|
In = InsertNewInstBefore(BinaryOperator::createAShr(In, Sh,
|
2007-04-14 23:32:02 +00:00
|
|
|
In->getName()+".lobit"),
|
2007-04-11 06:57:46 +00:00
|
|
|
CI);
|
|
|
|
if (In->getType() != CI.getType())
|
|
|
|
In = CastInst::createIntegerCast(In, CI.getType(),
|
|
|
|
true/*SExt*/, "tmp", &CI);
|
|
|
|
|
|
|
|
if (ICI->getPredicate() == ICmpInst::ICMP_SGT)
|
|
|
|
In = InsertNewInstBefore(BinaryOperator::createNot(In,
|
|
|
|
In->getName()+".not"), CI);
|
|
|
|
|
|
|
|
return ReplaceInstUsesWith(CI, In);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-11 06:12:58 +00:00
|
|
|
return 0;
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
2008-01-27 05:29:54 +00:00
|
|
|
/// FitsInFPType - Return a Constant* for the specified FP constant if it fits
|
|
|
|
/// in the specified FP type without changing its value.
|
2008-04-20 00:41:09 +00:00
|
|
|
static Constant *FitsInFPType(ConstantFP *CFP, const fltSemantics &Sem) {
|
2008-01-27 05:29:54 +00:00
|
|
|
APFloat F = CFP->getValueAPF();
|
|
|
|
if (F.convert(Sem, APFloat::rmNearestTiesToEven) == APFloat::opOK)
|
2008-04-20 00:41:09 +00:00
|
|
|
return ConstantFP::get(F);
|
2008-01-27 05:29:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// LookThroughFPExtensions - If this is an fp extension instruction, look
|
|
|
|
/// through it until we get the source value.
|
|
|
|
static Value *LookThroughFPExtensions(Value *V) {
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V))
|
|
|
|
if (I->getOpcode() == Instruction::FPExt)
|
|
|
|
return LookThroughFPExtensions(I->getOperand(0));
|
|
|
|
|
|
|
|
// If this value is a constant, return the constant in the smallest FP type
|
|
|
|
// that can accurately represent it. This allows us to turn
|
|
|
|
// (float)((double)X+2.0) into x+2.0f.
|
|
|
|
if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
|
|
|
|
if (CFP->getType() == Type::PPC_FP128Ty)
|
|
|
|
return V; // No constant folding of this.
|
|
|
|
// See if the value can be truncated to float and then reextended.
|
2008-04-20 00:41:09 +00:00
|
|
|
if (Value *V = FitsInFPType(CFP, APFloat::IEEEsingle))
|
2008-01-27 05:29:54 +00:00
|
|
|
return V;
|
|
|
|
if (CFP->getType() == Type::DoubleTy)
|
|
|
|
return V; // Won't shrink.
|
2008-04-20 00:41:09 +00:00
|
|
|
if (Value *V = FitsInFPType(CFP, APFloat::IEEEdouble))
|
2008-01-27 05:29:54 +00:00
|
|
|
return V;
|
|
|
|
// Don't try to shrink to various long double types.
|
|
|
|
}
|
|
|
|
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) {
|
|
|
|
if (Instruction *I = commonCastTransforms(CI))
|
|
|
|
return I;
|
|
|
|
|
|
|
|
// If we have fptrunc(add (fpextend x), (fpextend y)), where x and y are
|
|
|
|
// smaller than the destination type, we can eliminate the truncate by doing
|
|
|
|
// the add as the smaller type. This applies to add/sub/mul/div as well as
|
|
|
|
// many builtins (sqrt, etc).
|
|
|
|
BinaryOperator *OpI = dyn_cast<BinaryOperator>(CI.getOperand(0));
|
|
|
|
if (OpI && OpI->hasOneUse()) {
|
|
|
|
switch (OpI->getOpcode()) {
|
|
|
|
default: break;
|
|
|
|
case Instruction::Add:
|
|
|
|
case Instruction::Sub:
|
|
|
|
case Instruction::Mul:
|
|
|
|
case Instruction::FDiv:
|
|
|
|
case Instruction::FRem:
|
|
|
|
const Type *SrcTy = OpI->getType();
|
|
|
|
Value *LHSTrunc = LookThroughFPExtensions(OpI->getOperand(0));
|
|
|
|
Value *RHSTrunc = LookThroughFPExtensions(OpI->getOperand(1));
|
|
|
|
if (LHSTrunc->getType() != SrcTy &&
|
|
|
|
RHSTrunc->getType() != SrcTy) {
|
|
|
|
unsigned DstSize = CI.getType()->getPrimitiveSizeInBits();
|
|
|
|
// If the source types were both smaller than the destination type of
|
|
|
|
// the cast, do this xform.
|
|
|
|
if (LHSTrunc->getType()->getPrimitiveSizeInBits() <= DstSize &&
|
|
|
|
RHSTrunc->getType()->getPrimitiveSizeInBits() <= DstSize) {
|
|
|
|
LHSTrunc = InsertCastBefore(Instruction::FPExt, LHSTrunc,
|
|
|
|
CI.getType(), CI);
|
|
|
|
RHSTrunc = InsertCastBefore(Instruction::FPExt, RHSTrunc,
|
|
|
|
CI.getType(), CI);
|
|
|
|
return BinaryOperator::create(OpI->getOpcode(), LHSTrunc, RHSTrunc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InstCombiner::visitFPExt(CastInst &CI) {
|
|
|
|
return commonCastTransforms(CI);
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InstCombiner::visitFPToUI(CastInst &CI) {
|
2006-11-30 23:13:36 +00:00
|
|
|
return commonCastTransforms(CI);
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InstCombiner::visitFPToSI(CastInst &CI) {
|
2006-11-30 23:13:36 +00:00
|
|
|
return commonCastTransforms(CI);
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InstCombiner::visitUIToFP(CastInst &CI) {
|
|
|
|
return commonCastTransforms(CI);
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InstCombiner::visitSIToFP(CastInst &CI) {
|
|
|
|
return commonCastTransforms(CI);
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InstCombiner::visitPtrToInt(CastInst &CI) {
|
2007-04-27 17:44:50 +00:00
|
|
|
return commonPointerCastTransforms(CI);
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
2008-01-08 07:23:51 +00:00
|
|
|
Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) {
|
|
|
|
if (Instruction *I = commonCastTransforms(CI))
|
|
|
|
return I;
|
|
|
|
|
|
|
|
const Type *DestPointee = cast<PointerType>(CI.getType())->getElementType();
|
|
|
|
if (!DestPointee->isSized()) return 0;
|
|
|
|
|
|
|
|
// If this is inttoptr(add (ptrtoint x), cst), try to turn this into a GEP.
|
|
|
|
ConstantInt *Cst;
|
|
|
|
Value *X;
|
|
|
|
if (match(CI.getOperand(0), m_Add(m_Cast<PtrToIntInst>(m_Value(X)),
|
|
|
|
m_ConstantInt(Cst)))) {
|
|
|
|
// If the source and destination operands have the same type, see if this
|
|
|
|
// is a single-index GEP.
|
|
|
|
if (X->getType() == CI.getType()) {
|
|
|
|
// Get the size of the pointee type.
|
2008-03-14 05:12:19 +00:00
|
|
|
uint64_t Size = TD->getABITypeSize(DestPointee);
|
2008-01-08 07:23:51 +00:00
|
|
|
|
|
|
|
// Convert the constant to intptr type.
|
|
|
|
APInt Offset = Cst->getValue();
|
|
|
|
Offset.sextOrTrunc(TD->getPointerSizeInBits());
|
|
|
|
|
|
|
|
// If Offset is evenly divisible by Size, we can do this xform.
|
|
|
|
if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){
|
|
|
|
Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size));
|
2008-04-06 20:25:17 +00:00
|
|
|
return GetElementPtrInst::Create(X, ConstantInt::get(Offset));
|
2008-01-08 07:23:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: Could handle other cases, e.g. where add is indexing into field of
|
|
|
|
// struct etc.
|
|
|
|
} else if (CI.getOperand(0)->hasOneUse() &&
|
|
|
|
match(CI.getOperand(0), m_Add(m_Value(X), m_ConstantInt(Cst)))) {
|
|
|
|
// Otherwise, if this is inttoptr(add x, cst), try to turn this into an
|
|
|
|
// "inttoptr+GEP" instead of "add+intptr".
|
|
|
|
|
|
|
|
// Get the size of the pointee type.
|
|
|
|
uint64_t Size = TD->getABITypeSize(DestPointee);
|
|
|
|
|
|
|
|
// Convert the constant to intptr type.
|
|
|
|
APInt Offset = Cst->getValue();
|
|
|
|
Offset.sextOrTrunc(TD->getPointerSizeInBits());
|
|
|
|
|
|
|
|
// If Offset is evenly divisible by Size, we can do this xform.
|
|
|
|
if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){
|
|
|
|
Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size));
|
|
|
|
|
|
|
|
Instruction *P = InsertNewInstBefore(new IntToPtrInst(X, CI.getType(),
|
|
|
|
"tmp"), CI);
|
2008-04-06 20:25:17 +00:00
|
|
|
return GetElementPtrInst::Create(P, ConstantInt::get(Offset), "tmp");
|
2008-01-08 07:23:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
2007-04-27 17:44:50 +00:00
|
|
|
Instruction *InstCombiner::visitBitCast(BitCastInst &CI) {
|
2006-11-27 01:05:10 +00:00
|
|
|
// If the operands are integer typed then apply the integer transforms,
|
|
|
|
// otherwise just apply the common ones.
|
|
|
|
Value *Src = CI.getOperand(0);
|
|
|
|
const Type *SrcTy = Src->getType();
|
|
|
|
const Type *DestTy = CI.getType();
|
|
|
|
|
2007-01-15 02:27:26 +00:00
|
|
|
if (SrcTy->isInteger() && DestTy->isInteger()) {
|
2006-11-27 01:05:10 +00:00
|
|
|
if (Instruction *Result = commonIntCastTransforms(CI))
|
|
|
|
return Result;
|
2007-04-27 17:44:50 +00:00
|
|
|
} else if (isa<PointerType>(SrcTy)) {
|
|
|
|
if (Instruction *I = commonPointerCastTransforms(CI))
|
|
|
|
return I;
|
2006-11-27 01:05:10 +00:00
|
|
|
} else {
|
|
|
|
if (Instruction *Result = commonCastTransforms(CI))
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get rid of casts from one type to the same type. These are useless and can
|
|
|
|
// be replaced by the operand.
|
|
|
|
if (DestTy == Src->getType())
|
|
|
|
return ReplaceInstUsesWith(CI, Src);
|
|
|
|
|
|
|
|
if (const PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) {
|
2007-04-27 17:44:50 +00:00
|
|
|
const PointerType *SrcPTy = cast<PointerType>(SrcTy);
|
|
|
|
const Type *DstElTy = DstPTy->getElementType();
|
|
|
|
const Type *SrcElTy = SrcPTy->getElementType();
|
|
|
|
|
2008-03-31 00:22:16 +00:00
|
|
|
// If the address spaces don't match, don't eliminate the bitcast, which is
|
|
|
|
// required for changing types.
|
|
|
|
if (SrcPTy->getAddressSpace() != DstPTy->getAddressSpace())
|
|
|
|
return 0;
|
|
|
|
|
2007-04-27 17:44:50 +00:00
|
|
|
// If we are casting a malloc or alloca to a pointer to a type of the same
|
|
|
|
// size, rewrite the allocation instruction to allocate the "right" type.
|
|
|
|
if (AllocationInst *AI = dyn_cast<AllocationInst>(Src))
|
|
|
|
if (Instruction *V = PromoteCastOfAllocation(CI, *AI))
|
|
|
|
return V;
|
|
|
|
|
2007-05-05 22:32:24 +00:00
|
|
|
// If the source and destination are pointers, and this cast is equivalent
|
|
|
|
// to a getelementptr X, 0, 0, 0... turn it into the appropriate gep.
|
2007-04-27 17:44:50 +00:00
|
|
|
// This can enhance SROA and other transforms that want type-safe pointers.
|
|
|
|
Constant *ZeroUInt = Constant::getNullValue(Type::Int32Ty);
|
|
|
|
unsigned NumZeros = 0;
|
|
|
|
while (SrcElTy != DstElTy &&
|
|
|
|
isa<CompositeType>(SrcElTy) && !isa<PointerType>(SrcElTy) &&
|
|
|
|
SrcElTy->getNumContainedTypes() /* not "{}" */) {
|
|
|
|
SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt);
|
|
|
|
++NumZeros;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we found a path from the src to dest, create the getelementptr now.
|
|
|
|
if (SrcElTy == DstElTy) {
|
|
|
|
SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt);
|
2008-04-06 20:25:17 +00:00
|
|
|
return GetElementPtrInst::Create(Src, Idxs.begin(), Idxs.end(), "",
|
|
|
|
((Instruction*) NULL));
|
2003-07-24 17:35:25 +00:00
|
|
|
}
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) {
|
|
|
|
if (SVI->hasOneUse()) {
|
|
|
|
// Okay, we have (bitconvert (shuffle ..)). Check to see if this is
|
|
|
|
// a bitconvert to a vector with the same # elts.
|
2007-02-15 02:26:10 +00:00
|
|
|
if (isa<VectorType>(DestTy) &&
|
|
|
|
cast<VectorType>(DestTy)->getNumElements() ==
|
2006-11-27 01:05:10 +00:00
|
|
|
SVI->getType()->getNumElements()) {
|
|
|
|
CastInst *Tmp;
|
|
|
|
// If either of the operands is a cast from CI.getType(), then
|
|
|
|
// evaluating the shuffle in the casted destination's type will allow
|
|
|
|
// us to eliminate at least one cast.
|
|
|
|
if (((Tmp = dyn_cast<CastInst>(SVI->getOperand(0))) &&
|
|
|
|
Tmp->getOperand(0)->getType() == DestTy) ||
|
|
|
|
((Tmp = dyn_cast<CastInst>(SVI->getOperand(1))) &&
|
|
|
|
Tmp->getOperand(0)->getType() == DestTy)) {
|
2006-12-12 09:18:51 +00:00
|
|
|
Value *LHS = InsertOperandCastBefore(Instruction::BitCast,
|
|
|
|
SVI->getOperand(0), DestTy, &CI);
|
|
|
|
Value *RHS = InsertOperandCastBefore(Instruction::BitCast,
|
|
|
|
SVI->getOperand(1), DestTy, &CI);
|
2006-11-27 01:05:10 +00:00
|
|
|
// Return a new shuffle vector. Use the same element ID's, as we
|
|
|
|
// know the vector types match #elts.
|
|
|
|
return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2));
|
2006-05-25 23:24:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-04-18 17:39:14 +00:00
|
|
|
return 0;
|
2001-12-14 16:52:21 +00:00
|
|
|
}
|
|
|
|
|
Implement select.ll:test12*
This transforms code like this:
%C = or %A, %B
%D = select %cond, %C, %A
into:
%C = select %cond, %B, 0
%D = or %A, %C
Since B is often a constant, the select can often be eliminated. In any case,
this reduces the usage count of A, allowing subsequent optimizations to happen.
This xform applies when the operator is any of:
add, sub, mul, or, xor, and, shl, shr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12800 91177308-0d34-0410-b5e6-96231b3b80d8
2004-04-09 23:46:01 +00:00
|
|
|
/// GetSelectFoldableOperands - We want to turn code that looks like this:
|
|
|
|
/// %C = or %A, %B
|
|
|
|
/// %D = select %cond, %C, %A
|
|
|
|
/// into:
|
|
|
|
/// %C = select %cond, %B, 0
|
|
|
|
/// %D = or %A, %C
|
|
|
|
///
|
|
|
|
/// Assuming that the specified instruction is an operand to the select, return
|
|
|
|
/// a bitmask indicating which operands of this instruction are foldable if they
|
|
|
|
/// equal the other incoming value of the select.
|
|
|
|
///
|
|
|
|
static unsigned GetSelectFoldableOperands(Instruction *I) {
|
|
|
|
switch (I->getOpcode()) {
|
|
|
|
case Instruction::Add:
|
|
|
|
case Instruction::Mul:
|
|
|
|
case Instruction::And:
|
|
|
|
case Instruction::Or:
|
|
|
|
case Instruction::Xor:
|
|
|
|
return 3; // Can fold through either operand.
|
|
|
|
case Instruction::Sub: // Can only fold on the amount subtracted.
|
|
|
|
case Instruction::Shl: // Can only fold on the shift amount.
|
2006-11-08 06:47:33 +00:00
|
|
|
case Instruction::LShr:
|
|
|
|
case Instruction::AShr:
|
2005-04-21 23:48:37 +00:00
|
|
|
return 1;
|
Implement select.ll:test12*
This transforms code like this:
%C = or %A, %B
%D = select %cond, %C, %A
into:
%C = select %cond, %B, 0
%D = or %A, %C
Since B is often a constant, the select can often be eliminated. In any case,
this reduces the usage count of A, allowing subsequent optimizations to happen.
This xform applies when the operator is any of:
add, sub, mul, or, xor, and, shl, shr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12800 91177308-0d34-0410-b5e6-96231b3b80d8
2004-04-09 23:46:01 +00:00
|
|
|
default:
|
|
|
|
return 0; // Cannot fold
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// GetSelectFoldableConstant - For the same transformation as the previous
|
|
|
|
/// function, return the identity constant that goes into the select.
|
|
|
|
static Constant *GetSelectFoldableConstant(Instruction *I) {
|
|
|
|
switch (I->getOpcode()) {
|
|
|
|
default: assert(0 && "This cannot happen!"); abort();
|
|
|
|
case Instruction::Add:
|
|
|
|
case Instruction::Sub:
|
|
|
|
case Instruction::Or:
|
|
|
|
case Instruction::Xor:
|
|
|
|
case Instruction::Shl:
|
2006-11-08 06:47:33 +00:00
|
|
|
case Instruction::LShr:
|
|
|
|
case Instruction::AShr:
|
2007-02-02 02:16:23 +00:00
|
|
|
return Constant::getNullValue(I->getType());
|
Implement select.ll:test12*
This transforms code like this:
%C = or %A, %B
%D = select %cond, %C, %A
into:
%C = select %cond, %B, 0
%D = or %A, %C
Since B is often a constant, the select can often be eliminated. In any case,
this reduces the usage count of A, allowing subsequent optimizations to happen.
This xform applies when the operator is any of:
add, sub, mul, or, xor, and, shl, shr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12800 91177308-0d34-0410-b5e6-96231b3b80d8
2004-04-09 23:46:01 +00:00
|
|
|
case Instruction::And:
|
2007-06-15 06:23:19 +00:00
|
|
|
return Constant::getAllOnesValue(I->getType());
|
Implement select.ll:test12*
This transforms code like this:
%C = or %A, %B
%D = select %cond, %C, %A
into:
%C = select %cond, %B, 0
%D = or %A, %C
Since B is often a constant, the select can often be eliminated. In any case,
this reduces the usage count of A, allowing subsequent optimizations to happen.
This xform applies when the operator is any of:
add, sub, mul, or, xor, and, shl, shr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12800 91177308-0d34-0410-b5e6-96231b3b80d8
2004-04-09 23:46:01 +00:00
|
|
|
case Instruction::Mul:
|
|
|
|
return ConstantInt::get(I->getType(), 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-19 21:50:18 +00:00
|
|
|
/// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI
|
|
|
|
/// have the same opcode and only one use each. Try to simplify this.
|
|
|
|
Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI,
|
|
|
|
Instruction *FI) {
|
|
|
|
if (TI->getNumOperands() == 1) {
|
|
|
|
// If this is a non-volatile load or a cast from the same type,
|
|
|
|
// merge.
|
2006-11-27 01:05:10 +00:00
|
|
|
if (TI->isCast()) {
|
2005-01-19 21:50:18 +00:00
|
|
|
if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType())
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 0; // unknown unary op.
|
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2005-01-19 21:50:18 +00:00
|
|
|
// Fold this by inserting a select from the input values.
|
2008-04-06 20:25:17 +00:00
|
|
|
SelectInst *NewSI = SelectInst::Create(SI.getCondition(), TI->getOperand(0),
|
|
|
|
FI->getOperand(0), SI.getName()+".v");
|
2005-01-19 21:50:18 +00:00
|
|
|
InsertNewInstBefore(NewSI, SI);
|
2006-11-27 01:05:10 +00:00
|
|
|
return CastInst::create(Instruction::CastOps(TI->getOpcode()), NewSI,
|
|
|
|
TI->getType());
|
2005-01-19 21:50:18 +00:00
|
|
|
}
|
|
|
|
|
2007-02-02 02:16:23 +00:00
|
|
|
// Only handle binary operators here.
|
|
|
|
if (!isa<BinaryOperator>(TI))
|
2005-01-19 21:50:18 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Figure out if the operations have any operands in common.
|
|
|
|
Value *MatchOp, *OtherOpT, *OtherOpF;
|
|
|
|
bool MatchIsOpZero;
|
|
|
|
if (TI->getOperand(0) == FI->getOperand(0)) {
|
|
|
|
MatchOp = TI->getOperand(0);
|
|
|
|
OtherOpT = TI->getOperand(1);
|
|
|
|
OtherOpF = FI->getOperand(1);
|
|
|
|
MatchIsOpZero = true;
|
|
|
|
} else if (TI->getOperand(1) == FI->getOperand(1)) {
|
|
|
|
MatchOp = TI->getOperand(1);
|
|
|
|
OtherOpT = TI->getOperand(0);
|
|
|
|
OtherOpF = FI->getOperand(0);
|
|
|
|
MatchIsOpZero = false;
|
|
|
|
} else if (!TI->isCommutative()) {
|
|
|
|
return 0;
|
|
|
|
} else if (TI->getOperand(0) == FI->getOperand(1)) {
|
|
|
|
MatchOp = TI->getOperand(0);
|
|
|
|
OtherOpT = TI->getOperand(1);
|
|
|
|
OtherOpF = FI->getOperand(0);
|
|
|
|
MatchIsOpZero = true;
|
|
|
|
} else if (TI->getOperand(1) == FI->getOperand(0)) {
|
|
|
|
MatchOp = TI->getOperand(1);
|
|
|
|
OtherOpT = TI->getOperand(0);
|
|
|
|
OtherOpF = FI->getOperand(1);
|
|
|
|
MatchIsOpZero = true;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we reach here, they do have operations in common.
|
2008-04-06 20:25:17 +00:00
|
|
|
SelectInst *NewSI = SelectInst::Create(SI.getCondition(), OtherOpT,
|
|
|
|
OtherOpF, SI.getName()+".v");
|
2005-01-19 21:50:18 +00:00
|
|
|
InsertNewInstBefore(NewSI, SI);
|
|
|
|
|
|
|
|
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) {
|
|
|
|
if (MatchIsOpZero)
|
|
|
|
return BinaryOperator::create(BO->getOpcode(), MatchOp, NewSI);
|
|
|
|
else
|
|
|
|
return BinaryOperator::create(BO->getOpcode(), NewSI, MatchOp);
|
|
|
|
}
|
2007-02-02 14:41:37 +00:00
|
|
|
assert(0 && "Shouldn't get here");
|
|
|
|
return 0;
|
2005-01-19 21:50:18 +00:00
|
|
|
}
|
|
|
|
|
2004-03-12 05:52:32 +00:00
|
|
|
Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
|
2004-03-30 19:37:13 +00:00
|
|
|
Value *CondVal = SI.getCondition();
|
|
|
|
Value *TrueVal = SI.getTrueValue();
|
|
|
|
Value *FalseVal = SI.getFalseValue();
|
|
|
|
|
|
|
|
// select true, X, Y -> X
|
|
|
|
// select false, X, Y -> Y
|
2007-01-11 12:24:14 +00:00
|
|
|
if (ConstantInt *C = dyn_cast<ConstantInt>(CondVal))
|
2007-01-12 04:24:46 +00:00
|
|
|
return ReplaceInstUsesWith(SI, C->getZExtValue() ? TrueVal : FalseVal);
|
2004-03-30 19:37:13 +00:00
|
|
|
|
|
|
|
// select C, X, X -> X
|
|
|
|
if (TrueVal == FalseVal)
|
|
|
|
return ReplaceInstUsesWith(SI, TrueVal);
|
|
|
|
|
2004-10-16 18:11:37 +00:00
|
|
|
if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X
|
|
|
|
return ReplaceInstUsesWith(SI, FalseVal);
|
|
|
|
if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X
|
|
|
|
return ReplaceInstUsesWith(SI, TrueVal);
|
|
|
|
if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y
|
|
|
|
if (isa<Constant>(TrueVal))
|
|
|
|
return ReplaceInstUsesWith(SI, TrueVal);
|
|
|
|
else
|
|
|
|
return ReplaceInstUsesWith(SI, FalseVal);
|
|
|
|
}
|
|
|
|
|
2007-01-11 18:21:29 +00:00
|
|
|
if (SI.getType() == Type::Int1Ty) {
|
For PR1064:
Implement the arbitrary bit-width integer feature. The feature allows
integers of any bitwidth (up to 64) to be defined instead of just 1, 8,
16, 32, and 64 bit integers.
This change does several things:
1. Introduces a new Derived Type, IntegerType, to represent the number of
bits in an integer. The Type classes SubclassData field is used to
store the number of bits. This allows 2^23 bits in an integer type.
2. Removes the five integer Type::TypeID values for the 1, 8, 16, 32 and
64-bit integers. These are replaced with just IntegerType which is not
a primitive any more.
3. Adjust the rest of LLVM to account for this change.
Note that while this incremental change lays the foundation for arbitrary
bit-width integers, LLVM has not yet been converted to actually deal with
them in any significant way. Most optimization passes, for example, will
still only deal with the byte-width integer types. Future increments
will rectify this situation.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33113 91177308-0d34-0410-b5e6-96231b3b80d8
2007-01-12 07:05:14 +00:00
|
|
|
if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) {
|
2007-01-12 04:24:46 +00:00
|
|
|
if (C->getZExtValue()) {
|
2004-04-08 04:43:23 +00:00
|
|
|
// Change: A = select B, true, C --> A = or B, C
|
2004-06-10 02:07:29 +00:00
|
|
|
return BinaryOperator::createOr(CondVal, FalseVal);
|
2004-04-08 04:43:23 +00:00
|
|
|
} else {
|
|
|
|
// Change: A = select B, false, C --> A = and !B, C
|
|
|
|
Value *NotCond =
|
|
|
|
InsertNewInstBefore(BinaryOperator::createNot(CondVal,
|
|
|
|
"not."+CondVal->getName()), SI);
|
2004-06-10 02:07:29 +00:00
|
|
|
return BinaryOperator::createAnd(NotCond, FalseVal);
|
2004-04-08 04:43:23 +00:00
|
|
|
}
|
For PR1064:
Implement the arbitrary bit-width integer feature. The feature allows
integers of any bitwidth (up to 64) to be defined instead of just 1, 8,
16, 32, and 64 bit integers.
This change does several things:
1. Introduces a new Derived Type, IntegerType, to represent the number of
bits in an integer. The Type classes SubclassData field is used to
store the number of bits. This allows 2^23 bits in an integer type.
2. Removes the five integer Type::TypeID values for the 1, 8, 16, 32 and
64-bit integers. These are replaced with just IntegerType which is not
a primitive any more.
3. Adjust the rest of LLVM to account for this change.
Note that while this incremental change lays the foundation for arbitrary
bit-width integers, LLVM has not yet been converted to actually deal with
them in any significant way. Most optimization passes, for example, will
still only deal with the byte-width integer types. Future increments
will rectify this situation.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33113 91177308-0d34-0410-b5e6-96231b3b80d8
2007-01-12 07:05:14 +00:00
|
|
|
} else if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) {
|
2007-01-12 04:24:46 +00:00
|
|
|
if (C->getZExtValue() == false) {
|
2004-04-08 04:43:23 +00:00
|
|
|
// Change: A = select B, C, false --> A = and B, C
|
2004-06-10 02:07:29 +00:00
|
|
|
return BinaryOperator::createAnd(CondVal, TrueVal);
|
2004-04-08 04:43:23 +00:00
|
|
|
} else {
|
|
|
|
// Change: A = select B, C, true --> A = or !B, C
|
|
|
|
Value *NotCond =
|
|
|
|
InsertNewInstBefore(BinaryOperator::createNot(CondVal,
|
|
|
|
"not."+CondVal->getName()), SI);
|
2004-06-10 02:07:29 +00:00
|
|
|
return BinaryOperator::createOr(NotCond, TrueVal);
|
2004-04-08 04:43:23 +00:00
|
|
|
}
|
|
|
|
}
|
2007-11-25 21:27:53 +00:00
|
|
|
|
|
|
|
// select a, b, a -> a&b
|
|
|
|
// select a, a, b -> a|b
|
|
|
|
if (CondVal == TrueVal)
|
|
|
|
return BinaryOperator::createOr(CondVal, FalseVal);
|
|
|
|
else if (CondVal == FalseVal)
|
|
|
|
return BinaryOperator::createAnd(CondVal, TrueVal);
|
2007-01-11 12:24:14 +00:00
|
|
|
}
|
2004-04-08 04:43:23 +00:00
|
|
|
|
2004-04-09 19:05:30 +00:00
|
|
|
// Selecting between two integer constants?
|
|
|
|
if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal))
|
|
|
|
if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) {
|
2007-04-11 06:12:58 +00:00
|
|
|
// select C, 1, 0 -> zext C to int
|
2007-03-23 21:24:59 +00:00
|
|
|
if (FalseValC->isZero() && TrueValC->getValue() == 1) {
|
2006-11-27 01:05:10 +00:00
|
|
|
return CastInst::create(Instruction::ZExt, CondVal, SI.getType());
|
2007-03-23 21:24:59 +00:00
|
|
|
} else if (TrueValC->isZero() && FalseValC->getValue() == 1) {
|
2007-04-11 06:12:58 +00:00
|
|
|
// select C, 0, 1 -> zext !C to int
|
2004-04-09 19:05:30 +00:00
|
|
|
Value *NotCond =
|
|
|
|
InsertNewInstBefore(BinaryOperator::createNot(CondVal,
|
2004-04-09 18:19:44 +00:00
|
|
|
"not."+CondVal->getName()), SI);
|
2006-11-27 01:05:10 +00:00
|
|
|
return CastInst::create(Instruction::ZExt, NotCond, SI.getType());
|
2004-04-09 18:19:44 +00:00
|
|
|
}
|
2007-04-11 06:12:58 +00:00
|
|
|
|
|
|
|
// FIXME: Turn select 0/-1 and -1/0 into sext from condition!
|
2004-06-09 07:59:58 +00:00
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
if (ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition())) {
|
2006-09-20 04:44:59 +00:00
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
// (x <s 0) ? -1 : 0 -> ashr x, 31
|
2007-03-23 21:24:59 +00:00
|
|
|
if (TrueValC->isAllOnesValue() && FalseValC->isZero())
|
2006-09-20 04:44:59 +00:00
|
|
|
if (ConstantInt *CmpCst = dyn_cast<ConstantInt>(IC->getOperand(1))) {
|
2007-04-11 06:12:58 +00:00
|
|
|
if (IC->getPredicate() == ICmpInst::ICMP_SLT && CmpCst->isZero()) {
|
2006-09-20 04:44:59 +00:00
|
|
|
// The comparison constant and the result are not neccessarily the
|
2006-11-27 01:05:10 +00:00
|
|
|
// same width. Make an all-ones value by inserting a AShr.
|
2006-09-20 04:44:59 +00:00
|
|
|
Value *X = IC->getOperand(0);
|
2007-04-02 08:20:41 +00:00
|
|
|
uint32_t Bits = X->getType()->getPrimitiveSizeInBits();
|
2007-02-02 02:16:23 +00:00
|
|
|
Constant *ShAmt = ConstantInt::get(X->getType(), Bits-1);
|
|
|
|
Instruction *SRA = BinaryOperator::create(Instruction::AShr, X,
|
|
|
|
ShAmt, "ones");
|
2006-09-20 04:44:59 +00:00
|
|
|
InsertNewInstBefore(SRA, SI);
|
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
// Finally, convert to the type of the select RHS. We figure out
|
|
|
|
// if this requires a SExt, Trunc or BitCast based on the sizes.
|
|
|
|
Instruction::CastOps opc = Instruction::BitCast;
|
2007-04-02 08:20:41 +00:00
|
|
|
uint32_t SRASize = SRA->getType()->getPrimitiveSizeInBits();
|
|
|
|
uint32_t SISize = SI.getType()->getPrimitiveSizeInBits();
|
2006-11-27 01:05:10 +00:00
|
|
|
if (SRASize < SISize)
|
|
|
|
opc = Instruction::SExt;
|
|
|
|
else if (SRASize > SISize)
|
|
|
|
opc = Instruction::Trunc;
|
|
|
|
return CastInst::create(opc, SRA, SI.getType());
|
2006-09-20 04:44:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If one of the constants is zero (we know they can't both be) and we
|
2007-04-11 06:12:58 +00:00
|
|
|
// have an icmp instruction with zero, and we have an 'and' with the
|
2006-09-20 04:44:59 +00:00
|
|
|
// non-constant value, eliminate this whole mess. This corresponds to
|
|
|
|
// cases like this: ((X & 27) ? 27 : 0)
|
2007-03-23 21:24:59 +00:00
|
|
|
if (TrueValC->isZero() || FalseValC->isZero())
|
2006-09-18 04:22:48 +00:00
|
|
|
if (IC->isEquality() && isa<ConstantInt>(IC->getOperand(1)) &&
|
2004-06-09 07:59:58 +00:00
|
|
|
cast<Constant>(IC->getOperand(1))->isNullValue())
|
|
|
|
if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0)))
|
|
|
|
if (ICA->getOpcode() == Instruction::And &&
|
2005-04-21 23:48:37 +00:00
|
|
|
isa<ConstantInt>(ICA->getOperand(1)) &&
|
|
|
|
(ICA->getOperand(1) == TrueValC ||
|
|
|
|
ICA->getOperand(1) == FalseValC) &&
|
2004-06-09 07:59:58 +00:00
|
|
|
isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) {
|
|
|
|
// Okay, now we know that everything is set up, we just don't
|
2006-12-23 06:05:41 +00:00
|
|
|
// know whether we have a icmp_ne or icmp_eq and whether the
|
|
|
|
// true or false val is the zero.
|
2007-03-23 21:24:59 +00:00
|
|
|
bool ShouldNotVal = !TrueValC->isZero();
|
2006-12-23 06:05:41 +00:00
|
|
|
ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE;
|
2004-06-09 07:59:58 +00:00
|
|
|
Value *V = ICA;
|
|
|
|
if (ShouldNotVal)
|
|
|
|
V = InsertNewInstBefore(BinaryOperator::create(
|
|
|
|
Instruction::Xor, V, ICA->getOperand(1)), SI);
|
|
|
|
return ReplaceInstUsesWith(SI, V);
|
|
|
|
}
|
2006-09-20 04:44:59 +00:00
|
|
|
}
|
2004-03-12 05:52:32 +00:00
|
|
|
}
|
2004-04-10 22:21:27 +00:00
|
|
|
|
|
|
|
// See if we are selecting two values based on a comparison of the two values.
|
2006-12-23 06:05:41 +00:00
|
|
|
if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) {
|
|
|
|
if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) {
|
2004-04-10 22:21:27 +00:00
|
|
|
// Transform (X == Y) ? X : Y -> Y
|
2007-10-03 17:45:27 +00:00
|
|
|
if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) {
|
|
|
|
// This is not safe in general for floating point:
|
|
|
|
// consider X== -0, Y== +0.
|
|
|
|
// It becomes safe if either operand is a nonzero constant.
|
|
|
|
ConstantFP *CFPt, *CFPf;
|
|
|
|
if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
|
|
|
|
!CFPt->getValueAPF().isZero()) ||
|
|
|
|
((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
|
|
|
|
!CFPf->getValueAPF().isZero()))
|
2004-04-10 22:21:27 +00:00
|
|
|
return ReplaceInstUsesWith(SI, FalseVal);
|
2007-10-03 17:45:27 +00:00
|
|
|
}
|
2004-04-10 22:21:27 +00:00
|
|
|
// Transform (X != Y) ? X : Y -> X
|
2006-12-23 06:05:41 +00:00
|
|
|
if (FCI->getPredicate() == FCmpInst::FCMP_ONE)
|
2004-04-10 22:21:27 +00:00
|
|
|
return ReplaceInstUsesWith(SI, TrueVal);
|
|
|
|
// NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc.
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
} else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){
|
2004-04-10 22:21:27 +00:00
|
|
|
// Transform (X == Y) ? Y : X -> X
|
2007-10-03 17:45:27 +00:00
|
|
|
if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) {
|
|
|
|
// This is not safe in general for floating point:
|
|
|
|
// consider X== -0, Y== +0.
|
|
|
|
// It becomes safe if either operand is a nonzero constant.
|
|
|
|
ConstantFP *CFPt, *CFPf;
|
|
|
|
if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
|
|
|
|
!CFPt->getValueAPF().isZero()) ||
|
|
|
|
((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
|
|
|
|
!CFPf->getValueAPF().isZero()))
|
|
|
|
return ReplaceInstUsesWith(SI, FalseVal);
|
|
|
|
}
|
2004-04-10 22:21:27 +00:00
|
|
|
// Transform (X != Y) ? Y : X -> Y
|
2006-12-23 06:05:41 +00:00
|
|
|
if (FCI->getPredicate() == FCmpInst::FCMP_ONE)
|
|
|
|
return ReplaceInstUsesWith(SI, TrueVal);
|
|
|
|
// NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if we are selecting two values based on a comparison of the two values.
|
|
|
|
if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal)) {
|
|
|
|
if (ICI->getOperand(0) == TrueVal && ICI->getOperand(1) == FalseVal) {
|
|
|
|
// Transform (X == Y) ? X : Y -> Y
|
|
|
|
if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
|
|
|
|
return ReplaceInstUsesWith(SI, FalseVal);
|
|
|
|
// Transform (X != Y) ? X : Y -> X
|
|
|
|
if (ICI->getPredicate() == ICmpInst::ICMP_NE)
|
|
|
|
return ReplaceInstUsesWith(SI, TrueVal);
|
|
|
|
// NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc.
|
|
|
|
|
|
|
|
} else if (ICI->getOperand(0) == FalseVal && ICI->getOperand(1) == TrueVal){
|
|
|
|
// Transform (X == Y) ? Y : X -> X
|
|
|
|
if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
|
|
|
|
return ReplaceInstUsesWith(SI, FalseVal);
|
|
|
|
// Transform (X != Y) ? Y : X -> Y
|
|
|
|
if (ICI->getPredicate() == ICmpInst::ICMP_NE)
|
2004-04-11 01:39:19 +00:00
|
|
|
return ReplaceInstUsesWith(SI, TrueVal);
|
2004-04-10 22:21:27 +00:00
|
|
|
// NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc.
|
|
|
|
}
|
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This occurs in
the 'sim' program and probably elsewhere. In sim, it comes up for cases
like this:
#define round(x) ((x)>0.0 ? (x)+0.5 : (x)-0.5)
double G;
void T(double X) { G = round(X); }
(it uses the round macro a lot). This changes the LLVM code from:
%tmp.1 = setgt double %X, 0.000000e+00 ; <bool> [#uses=1]
%tmp.4 = add double %X, 5.000000e-01 ; <double> [#uses=1]
%tmp.6 = sub double %X, 5.000000e-01 ; <double> [#uses=1]
%mem_tmp.0 = select bool %tmp.1, double %tmp.4, double %tmp.6
store double %mem_tmp.0, double* %G
to:
%tmp.1 = setgt double %X, 0.000000e+00 ; <bool> [#uses=1]
%mem_tmp.0.p = select bool %tmp.1, double 5.000000e-01, double -5.000000e-01
%mem_tmp.0 = add double %mem_tmp.0.p, %X
store double %mem_tmp.0, double* %G
ret void
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@19537 91177308-0d34-0410-b5e6-96231b3b80d8
2005-01-13 22:52:24 +00:00
|
|
|
if (Instruction *TI = dyn_cast<Instruction>(TrueVal))
|
|
|
|
if (Instruction *FI = dyn_cast<Instruction>(FalseVal))
|
|
|
|
if (TI->hasOneUse() && FI->hasOneUse()) {
|
|
|
|
Instruction *AddOp = 0, *SubOp = 0;
|
|
|
|
|
2005-01-19 21:50:18 +00:00
|
|
|
// Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z))
|
|
|
|
if (TI->getOpcode() == FI->getOpcode())
|
|
|
|
if (Instruction *IV = FoldSelectOpOp(SI, TI, FI))
|
|
|
|
return IV;
|
|
|
|
|
|
|
|
// Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is
|
|
|
|
// even legal for FP.
|
Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This occurs in
the 'sim' program and probably elsewhere. In sim, it comes up for cases
like this:
#define round(x) ((x)>0.0 ? (x)+0.5 : (x)-0.5)
double G;
void T(double X) { G = round(X); }
(it uses the round macro a lot). This changes the LLVM code from:
%tmp.1 = setgt double %X, 0.000000e+00 ; <bool> [#uses=1]
%tmp.4 = add double %X, 5.000000e-01 ; <double> [#uses=1]
%tmp.6 = sub double %X, 5.000000e-01 ; <double> [#uses=1]
%mem_tmp.0 = select bool %tmp.1, double %tmp.4, double %tmp.6
store double %mem_tmp.0, double* %G
to:
%tmp.1 = setgt double %X, 0.000000e+00 ; <bool> [#uses=1]
%mem_tmp.0.p = select bool %tmp.1, double 5.000000e-01, double -5.000000e-01
%mem_tmp.0 = add double %mem_tmp.0.p, %X
store double %mem_tmp.0, double* %G
ret void
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@19537 91177308-0d34-0410-b5e6-96231b3b80d8
2005-01-13 22:52:24 +00:00
|
|
|
if (TI->getOpcode() == Instruction::Sub &&
|
|
|
|
FI->getOpcode() == Instruction::Add) {
|
|
|
|
AddOp = FI; SubOp = TI;
|
|
|
|
} else if (FI->getOpcode() == Instruction::Sub &&
|
|
|
|
TI->getOpcode() == Instruction::Add) {
|
|
|
|
AddOp = TI; SubOp = FI;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (AddOp) {
|
|
|
|
Value *OtherAddOp = 0;
|
|
|
|
if (SubOp->getOperand(0) == AddOp->getOperand(0)) {
|
|
|
|
OtherAddOp = AddOp->getOperand(1);
|
|
|
|
} else if (SubOp->getOperand(0) == AddOp->getOperand(1)) {
|
|
|
|
OtherAddOp = AddOp->getOperand(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (OtherAddOp) {
|
Fix a problem that Nate noticed that boils down to an over conservative check
in the code that does "select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y)))".
We now compile this loop:
LBB1_1: ; no_exit
add r6, r2, r3
subf r3, r2, r3
cmpwi cr0, r2, 0
addi r7, r5, 4
lwz r2, 0(r5)
addi r4, r4, 1
blt cr0, LBB1_4 ; no_exit
LBB1_3: ; no_exit
mr r3, r6
LBB1_4: ; no_exit
cmpwi cr0, r4, 16
mr r5, r7
bne cr0, LBB1_1 ; no_exit
into this instead:
LBB1_1: ; no_exit
srawi r6, r2, 31
add r2, r2, r6
xor r6, r2, r6
addi r7, r5, 4
lwz r2, 0(r5)
addi r4, r4, 1
add r3, r3, r6
cmpwi cr0, r4, 16
mr r5, r7
bne cr0, LBB1_1 ; no_exit
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@26356 91177308-0d34-0410-b5e6-96231b3b80d8
2006-02-24 18:05:58 +00:00
|
|
|
// So at this point we know we have (Y -> OtherAddOp):
|
|
|
|
// select C, (add X, Y), (sub X, Z)
|
|
|
|
Value *NegVal; // Compute -Z
|
|
|
|
if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) {
|
|
|
|
NegVal = ConstantExpr::getNeg(C);
|
|
|
|
} else {
|
|
|
|
NegVal = InsertNewInstBefore(
|
|
|
|
BinaryOperator::createNeg(SubOp->getOperand(1), "tmp"), SI);
|
|
|
|
}
|
Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This occurs in
the 'sim' program and probably elsewhere. In sim, it comes up for cases
like this:
#define round(x) ((x)>0.0 ? (x)+0.5 : (x)-0.5)
double G;
void T(double X) { G = round(X); }
(it uses the round macro a lot). This changes the LLVM code from:
%tmp.1 = setgt double %X, 0.000000e+00 ; <bool> [#uses=1]
%tmp.4 = add double %X, 5.000000e-01 ; <double> [#uses=1]
%tmp.6 = sub double %X, 5.000000e-01 ; <double> [#uses=1]
%mem_tmp.0 = select bool %tmp.1, double %tmp.4, double %tmp.6
store double %mem_tmp.0, double* %G
to:
%tmp.1 = setgt double %X, 0.000000e+00 ; <bool> [#uses=1]
%mem_tmp.0.p = select bool %tmp.1, double 5.000000e-01, double -5.000000e-01
%mem_tmp.0 = add double %mem_tmp.0.p, %X
store double %mem_tmp.0, double* %G
ret void
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@19537 91177308-0d34-0410-b5e6-96231b3b80d8
2005-01-13 22:52:24 +00:00
|
|
|
|
Fix a problem that Nate noticed that boils down to an over conservative check
in the code that does "select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y)))".
We now compile this loop:
LBB1_1: ; no_exit
add r6, r2, r3
subf r3, r2, r3
cmpwi cr0, r2, 0
addi r7, r5, 4
lwz r2, 0(r5)
addi r4, r4, 1
blt cr0, LBB1_4 ; no_exit
LBB1_3: ; no_exit
mr r3, r6
LBB1_4: ; no_exit
cmpwi cr0, r4, 16
mr r5, r7
bne cr0, LBB1_1 ; no_exit
into this instead:
LBB1_1: ; no_exit
srawi r6, r2, 31
add r2, r2, r6
xor r6, r2, r6
addi r7, r5, 4
lwz r2, 0(r5)
addi r4, r4, 1
add r3, r3, r6
cmpwi cr0, r4, 16
mr r5, r7
bne cr0, LBB1_1 ; no_exit
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@26356 91177308-0d34-0410-b5e6-96231b3b80d8
2006-02-24 18:05:58 +00:00
|
|
|
Value *NewTrueOp = OtherAddOp;
|
|
|
|
Value *NewFalseOp = NegVal;
|
|
|
|
if (AddOp != TI)
|
|
|
|
std::swap(NewTrueOp, NewFalseOp);
|
|
|
|
Instruction *NewSel =
|
2008-04-06 20:25:17 +00:00
|
|
|
SelectInst::Create(CondVal, NewTrueOp,NewFalseOp,SI.getName()+".p");
|
2005-04-21 23:48:37 +00:00
|
|
|
|
Fix a problem that Nate noticed that boils down to an over conservative check
in the code that does "select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y)))".
We now compile this loop:
LBB1_1: ; no_exit
add r6, r2, r3
subf r3, r2, r3
cmpwi cr0, r2, 0
addi r7, r5, 4
lwz r2, 0(r5)
addi r4, r4, 1
blt cr0, LBB1_4 ; no_exit
LBB1_3: ; no_exit
mr r3, r6
LBB1_4: ; no_exit
cmpwi cr0, r4, 16
mr r5, r7
bne cr0, LBB1_1 ; no_exit
into this instead:
LBB1_1: ; no_exit
srawi r6, r2, 31
add r2, r2, r6
xor r6, r2, r6
addi r7, r5, 4
lwz r2, 0(r5)
addi r4, r4, 1
add r3, r3, r6
cmpwi cr0, r4, 16
mr r5, r7
bne cr0, LBB1_1 ; no_exit
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@26356 91177308-0d34-0410-b5e6-96231b3b80d8
2006-02-24 18:05:58 +00:00
|
|
|
NewSel = InsertNewInstBefore(NewSel, SI);
|
|
|
|
return BinaryOperator::createAdd(SubOp->getOperand(0), NewSel);
|
Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This occurs in
the 'sim' program and probably elsewhere. In sim, it comes up for cases
like this:
#define round(x) ((x)>0.0 ? (x)+0.5 : (x)-0.5)
double G;
void T(double X) { G = round(X); }
(it uses the round macro a lot). This changes the LLVM code from:
%tmp.1 = setgt double %X, 0.000000e+00 ; <bool> [#uses=1]
%tmp.4 = add double %X, 5.000000e-01 ; <double> [#uses=1]
%tmp.6 = sub double %X, 5.000000e-01 ; <double> [#uses=1]
%mem_tmp.0 = select bool %tmp.1, double %tmp.4, double %tmp.6
store double %mem_tmp.0, double* %G
to:
%tmp.1 = setgt double %X, 0.000000e+00 ; <bool> [#uses=1]
%mem_tmp.0.p = select bool %tmp.1, double 5.000000e-01, double -5.000000e-01
%mem_tmp.0 = add double %mem_tmp.0.p, %X
store double %mem_tmp.0, double* %G
ret void
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@19537 91177308-0d34-0410-b5e6-96231b3b80d8
2005-01-13 22:52:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
Implement select.ll:test12*
This transforms code like this:
%C = or %A, %B
%D = select %cond, %C, %A
into:
%C = select %cond, %B, 0
%D = or %A, %C
Since B is often a constant, the select can often be eliminated. In any case,
this reduces the usage count of A, allowing subsequent optimizations to happen.
This xform applies when the operator is any of:
add, sub, mul, or, xor, and, shl, shr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12800 91177308-0d34-0410-b5e6-96231b3b80d8
2004-04-09 23:46:01 +00:00
|
|
|
// See if we can fold the select into one of our operands.
|
2007-01-15 02:27:26 +00:00
|
|
|
if (SI.getType()->isInteger()) {
|
Implement select.ll:test12*
This transforms code like this:
%C = or %A, %B
%D = select %cond, %C, %A
into:
%C = select %cond, %B, 0
%D = or %A, %C
Since B is often a constant, the select can often be eliminated. In any case,
this reduces the usage count of A, allowing subsequent optimizations to happen.
This xform applies when the operator is any of:
add, sub, mul, or, xor, and, shl, shr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12800 91177308-0d34-0410-b5e6-96231b3b80d8
2004-04-09 23:46:01 +00:00
|
|
|
// See the comment above GetSelectFoldableOperands for a description of the
|
|
|
|
// transformation we are doing here.
|
|
|
|
if (Instruction *TVI = dyn_cast<Instruction>(TrueVal))
|
|
|
|
if (TVI->hasOneUse() && TVI->getNumOperands() == 2 &&
|
|
|
|
!isa<Constant>(FalseVal))
|
|
|
|
if (unsigned SFO = GetSelectFoldableOperands(TVI)) {
|
|
|
|
unsigned OpToFold = 0;
|
|
|
|
if ((SFO & 1) && FalseVal == TVI->getOperand(0)) {
|
|
|
|
OpToFold = 1;
|
|
|
|
} else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) {
|
|
|
|
OpToFold = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (OpToFold) {
|
|
|
|
Constant *C = GetSelectFoldableConstant(TVI);
|
|
|
|
Instruction *NewSel =
|
2008-04-06 20:25:17 +00:00
|
|
|
SelectInst::Create(SI.getCondition(), TVI->getOperand(2-OpToFold), C);
|
Implement select.ll:test12*
This transforms code like this:
%C = or %A, %B
%D = select %cond, %C, %A
into:
%C = select %cond, %B, 0
%D = or %A, %C
Since B is often a constant, the select can often be eliminated. In any case,
this reduces the usage count of A, allowing subsequent optimizations to happen.
This xform applies when the operator is any of:
add, sub, mul, or, xor, and, shl, shr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12800 91177308-0d34-0410-b5e6-96231b3b80d8
2004-04-09 23:46:01 +00:00
|
|
|
InsertNewInstBefore(NewSel, SI);
|
2007-02-11 01:23:03 +00:00
|
|
|
NewSel->takeName(TVI);
|
Implement select.ll:test12*
This transforms code like this:
%C = or %A, %B
%D = select %cond, %C, %A
into:
%C = select %cond, %B, 0
%D = or %A, %C
Since B is often a constant, the select can often be eliminated. In any case,
this reduces the usage count of A, allowing subsequent optimizations to happen.
This xform applies when the operator is any of:
add, sub, mul, or, xor, and, shl, shr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12800 91177308-0d34-0410-b5e6-96231b3b80d8
2004-04-09 23:46:01 +00:00
|
|
|
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI))
|
|
|
|
return BinaryOperator::create(BO->getOpcode(), FalseVal, NewSel);
|
|
|
|
else {
|
|
|
|
assert(0 && "Unknown instruction!!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-09-29 17:40:11 +00:00
|
|
|
|
Implement select.ll:test12*
This transforms code like this:
%C = or %A, %B
%D = select %cond, %C, %A
into:
%C = select %cond, %B, 0
%D = or %A, %C
Since B is often a constant, the select can often be eliminated. In any case,
this reduces the usage count of A, allowing subsequent optimizations to happen.
This xform applies when the operator is any of:
add, sub, mul, or, xor, and, shl, shr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12800 91177308-0d34-0410-b5e6-96231b3b80d8
2004-04-09 23:46:01 +00:00
|
|
|
if (Instruction *FVI = dyn_cast<Instruction>(FalseVal))
|
|
|
|
if (FVI->hasOneUse() && FVI->getNumOperands() == 2 &&
|
|
|
|
!isa<Constant>(TrueVal))
|
|
|
|
if (unsigned SFO = GetSelectFoldableOperands(FVI)) {
|
|
|
|
unsigned OpToFold = 0;
|
|
|
|
if ((SFO & 1) && TrueVal == FVI->getOperand(0)) {
|
|
|
|
OpToFold = 1;
|
|
|
|
} else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) {
|
|
|
|
OpToFold = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (OpToFold) {
|
|
|
|
Constant *C = GetSelectFoldableConstant(FVI);
|
|
|
|
Instruction *NewSel =
|
2008-04-06 20:25:17 +00:00
|
|
|
SelectInst::Create(SI.getCondition(), C, FVI->getOperand(2-OpToFold));
|
Implement select.ll:test12*
This transforms code like this:
%C = or %A, %B
%D = select %cond, %C, %A
into:
%C = select %cond, %B, 0
%D = or %A, %C
Since B is often a constant, the select can often be eliminated. In any case,
this reduces the usage count of A, allowing subsequent optimizations to happen.
This xform applies when the operator is any of:
add, sub, mul, or, xor, and, shl, shr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12800 91177308-0d34-0410-b5e6-96231b3b80d8
2004-04-09 23:46:01 +00:00
|
|
|
InsertNewInstBefore(NewSel, SI);
|
2007-02-11 01:23:03 +00:00
|
|
|
NewSel->takeName(FVI);
|
Implement select.ll:test12*
This transforms code like this:
%C = or %A, %B
%D = select %cond, %C, %A
into:
%C = select %cond, %B, 0
%D = or %A, %C
Since B is often a constant, the select can often be eliminated. In any case,
this reduces the usage count of A, allowing subsequent optimizations to happen.
This xform applies when the operator is any of:
add, sub, mul, or, xor, and, shl, shr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12800 91177308-0d34-0410-b5e6-96231b3b80d8
2004-04-09 23:46:01 +00:00
|
|
|
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI))
|
|
|
|
return BinaryOperator::create(BO->getOpcode(), TrueVal, NewSel);
|
2007-02-02 02:16:23 +00:00
|
|
|
else
|
Implement select.ll:test12*
This transforms code like this:
%C = or %A, %B
%D = select %cond, %C, %A
into:
%C = select %cond, %B, 0
%D = or %A, %C
Since B is often a constant, the select can often be eliminated. In any case,
this reduces the usage count of A, allowing subsequent optimizations to happen.
This xform applies when the operator is any of:
add, sub, mul, or, xor, and, shl, shr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12800 91177308-0d34-0410-b5e6-96231b3b80d8
2004-04-09 23:46:01 +00:00
|
|
|
assert(0 && "Unknown instruction!!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-04-24 07:30:14 +00:00
|
|
|
|
|
|
|
if (BinaryOperator::isNot(CondVal)) {
|
|
|
|
SI.setOperand(0, BinaryOperator::getNotArgument(CondVal));
|
|
|
|
SI.setOperand(1, FalseVal);
|
|
|
|
SI.setOperand(2, TrueVal);
|
|
|
|
return &SI;
|
|
|
|
}
|
|
|
|
|
2004-03-12 05:52:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-10 18:43:06 +00:00
|
|
|
/// EnforceKnownAlignment - If the specified pointer points to an object that
|
|
|
|
/// we control, modify the object's alignment to PrefAlign. This isn't
|
|
|
|
/// often possible though. If alignment is important, a more reliable approach
|
|
|
|
/// is to simply align all global variables and allocation instructions to
|
|
|
|
/// their preferred alignment from the beginning.
|
|
|
|
///
|
|
|
|
static unsigned EnforceKnownAlignment(Value *V,
|
|
|
|
unsigned Align, unsigned PrefAlign) {
|
|
|
|
|
|
|
|
User *U = dyn_cast<User>(V);
|
|
|
|
if (!U) return Align;
|
|
|
|
|
|
|
|
switch (getOpcode(U)) {
|
|
|
|
default: break;
|
|
|
|
case Instruction::BitCast:
|
|
|
|
return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign);
|
|
|
|
case Instruction::GetElementPtr: {
|
|
|
|
// If all indexes are zero, it is just the alignment of the base pointer.
|
|
|
|
bool AllZeroOperands = true;
|
|
|
|
for (unsigned i = 1, e = U->getNumOperands(); i != e; ++i)
|
|
|
|
if (!isa<Constant>(U->getOperand(i)) ||
|
|
|
|
!cast<Constant>(U->getOperand(i))->isNullValue()) {
|
|
|
|
AllZeroOperands = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (AllZeroOperands) {
|
|
|
|
// Treat this like a bitcast.
|
|
|
|
return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
when we see a unaligned load from an insufficiently aligned global or
alloca, increase the alignment of the load, turning it into an aligned load.
This allows us to compile:
#include <xmmintrin.h>
__m128i foo(__m128i x){
static const unsigned int c_0[4] = { 0, 0, 0, 0 };
__m128i v_Zero = _mm_loadu_si128((__m128i*)c_0);
x = _mm_unpacklo_epi8(x, v_Zero);
return x;
}
into:
_foo:
punpcklbw _c_0.5944, %xmm0
ret
.data
.lcomm _c_0.5944,16,4 # c_0.5944
instead of:
_foo:
movdqu _c_0.5944, %xmm1
punpcklbw %xmm1, %xmm0
ret
.data
.lcomm _c_0.5944,16,2 # c_0.5944
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@40971 91177308-0d34-0410-b5e6-96231b3b80d8
2007-08-09 19:05:49 +00:00
|
|
|
|
2008-04-10 18:43:06 +00:00
|
|
|
if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
|
when we see a unaligned load from an insufficiently aligned global or
alloca, increase the alignment of the load, turning it into an aligned load.
This allows us to compile:
#include <xmmintrin.h>
__m128i foo(__m128i x){
static const unsigned int c_0[4] = { 0, 0, 0, 0 };
__m128i v_Zero = _mm_loadu_si128((__m128i*)c_0);
x = _mm_unpacklo_epi8(x, v_Zero);
return x;
}
into:
_foo:
punpcklbw _c_0.5944, %xmm0
ret
.data
.lcomm _c_0.5944,16,4 # c_0.5944
instead of:
_foo:
movdqu _c_0.5944, %xmm1
punpcklbw %xmm1, %xmm0
ret
.data
.lcomm _c_0.5944,16,2 # c_0.5944
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@40971 91177308-0d34-0410-b5e6-96231b3b80d8
2007-08-09 19:05:49 +00:00
|
|
|
// If there is a large requested alignment and we can, bump up the alignment
|
|
|
|
// of the global.
|
2008-04-10 18:43:06 +00:00
|
|
|
if (!GV->isDeclaration()) {
|
when we see a unaligned load from an insufficiently aligned global or
alloca, increase the alignment of the load, turning it into an aligned load.
This allows us to compile:
#include <xmmintrin.h>
__m128i foo(__m128i x){
static const unsigned int c_0[4] = { 0, 0, 0, 0 };
__m128i v_Zero = _mm_loadu_si128((__m128i*)c_0);
x = _mm_unpacklo_epi8(x, v_Zero);
return x;
}
into:
_foo:
punpcklbw _c_0.5944, %xmm0
ret
.data
.lcomm _c_0.5944,16,4 # c_0.5944
instead of:
_foo:
movdqu _c_0.5944, %xmm1
punpcklbw %xmm1, %xmm0
ret
.data
.lcomm _c_0.5944,16,2 # c_0.5944
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@40971 91177308-0d34-0410-b5e6-96231b3b80d8
2007-08-09 19:05:49 +00:00
|
|
|
GV->setAlignment(PrefAlign);
|
|
|
|
Align = PrefAlign;
|
|
|
|
}
|
2006-03-06 20:18:44 +00:00
|
|
|
} else if (AllocationInst *AI = dyn_cast<AllocationInst>(V)) {
|
when we see a unaligned load from an insufficiently aligned global or
alloca, increase the alignment of the load, turning it into an aligned load.
This allows us to compile:
#include <xmmintrin.h>
__m128i foo(__m128i x){
static const unsigned int c_0[4] = { 0, 0, 0, 0 };
__m128i v_Zero = _mm_loadu_si128((__m128i*)c_0);
x = _mm_unpacklo_epi8(x, v_Zero);
return x;
}
into:
_foo:
punpcklbw _c_0.5944, %xmm0
ret
.data
.lcomm _c_0.5944,16,4 # c_0.5944
instead of:
_foo:
movdqu _c_0.5944, %xmm1
punpcklbw %xmm1, %xmm0
ret
.data
.lcomm _c_0.5944,16,2 # c_0.5944
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@40971 91177308-0d34-0410-b5e6-96231b3b80d8
2007-08-09 19:05:49 +00:00
|
|
|
// If there is a requested alignment and if this is an alloca, round up. We
|
|
|
|
// don't do this for malloc, because some systems can't respect the request.
|
2008-04-10 18:43:06 +00:00
|
|
|
if (isa<AllocaInst>(AI)) {
|
when we see a unaligned load from an insufficiently aligned global or
alloca, increase the alignment of the load, turning it into an aligned load.
This allows us to compile:
#include <xmmintrin.h>
__m128i foo(__m128i x){
static const unsigned int c_0[4] = { 0, 0, 0, 0 };
__m128i v_Zero = _mm_loadu_si128((__m128i*)c_0);
x = _mm_unpacklo_epi8(x, v_Zero);
return x;
}
into:
_foo:
punpcklbw _c_0.5944, %xmm0
ret
.data
.lcomm _c_0.5944,16,4 # c_0.5944
instead of:
_foo:
movdqu _c_0.5944, %xmm1
punpcklbw %xmm1, %xmm0
ret
.data
.lcomm _c_0.5944,16,2 # c_0.5944
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@40971 91177308-0d34-0410-b5e6-96231b3b80d8
2007-08-09 19:05:49 +00:00
|
|
|
AI->setAlignment(PrefAlign);
|
|
|
|
Align = PrefAlign;
|
|
|
|
}
|
2008-04-10 18:43:06 +00:00
|
|
|
}
|
when we see a unaligned load from an insufficiently aligned global or
alloca, increase the alignment of the load, turning it into an aligned load.
This allows us to compile:
#include <xmmintrin.h>
__m128i foo(__m128i x){
static const unsigned int c_0[4] = { 0, 0, 0, 0 };
__m128i v_Zero = _mm_loadu_si128((__m128i*)c_0);
x = _mm_unpacklo_epi8(x, v_Zero);
return x;
}
into:
_foo:
punpcklbw _c_0.5944, %xmm0
ret
.data
.lcomm _c_0.5944,16,4 # c_0.5944
instead of:
_foo:
movdqu _c_0.5944, %xmm1
punpcklbw %xmm1, %xmm0
ret
.data
.lcomm _c_0.5944,16,2 # c_0.5944
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@40971 91177308-0d34-0410-b5e6-96231b3b80d8
2007-08-09 19:05:49 +00:00
|
|
|
|
2008-04-10 18:43:06 +00:00
|
|
|
return Align;
|
|
|
|
}
|
2006-03-06 20:18:44 +00:00
|
|
|
|
2008-04-10 18:43:06 +00:00
|
|
|
/// GetOrEnforceKnownAlignment - If the specified pointer has an alignment that
|
|
|
|
/// we can determine, return it, otherwise return 0. If PrefAlign is specified,
|
|
|
|
/// and it is more than the alignment of the ultimate object, see if we can
|
|
|
|
/// increase the alignment of the ultimate object, making this check succeed.
|
|
|
|
unsigned InstCombiner::GetOrEnforceKnownAlignment(Value *V,
|
|
|
|
unsigned PrefAlign) {
|
|
|
|
unsigned BitWidth = TD ? TD->getTypeSizeInBits(V->getType()) :
|
|
|
|
sizeof(PrefAlign) * CHAR_BIT;
|
|
|
|
APInt Mask = APInt::getAllOnesValue(BitWidth);
|
|
|
|
APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
|
|
|
|
ComputeMaskedBits(V, Mask, KnownZero, KnownOne);
|
|
|
|
unsigned TrailZ = KnownZero.countTrailingOnes();
|
|
|
|
unsigned Align = 1u << std::min(BitWidth - 1, TrailZ);
|
|
|
|
|
|
|
|
if (PrefAlign > Align)
|
|
|
|
Align = EnforceKnownAlignment(V, Align, PrefAlign);
|
|
|
|
|
|
|
|
// We don't need to make any adjustment.
|
|
|
|
return Align;
|
2006-03-06 20:18:44 +00:00
|
|
|
}
|
|
|
|
|
2008-01-13 23:50:23 +00:00
|
|
|
Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) {
|
2008-04-10 18:43:06 +00:00
|
|
|
unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(1));
|
|
|
|
unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(2));
|
2008-01-13 23:50:23 +00:00
|
|
|
unsigned MinAlign = std::min(DstAlign, SrcAlign);
|
|
|
|
unsigned CopyAlign = MI->getAlignment()->getZExtValue();
|
|
|
|
|
|
|
|
if (CopyAlign < MinAlign) {
|
|
|
|
MI->setAlignment(ConstantInt::get(Type::Int32Ty, MinAlign));
|
|
|
|
return MI;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with
|
|
|
|
// load/store.
|
|
|
|
ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getOperand(3));
|
|
|
|
if (MemOpLength == 0) return 0;
|
|
|
|
|
2008-01-14 00:28:35 +00:00
|
|
|
// Source and destination pointer types are always "i8*" for intrinsic. See
|
|
|
|
// if the size is something we can handle with a single primitive load/store.
|
|
|
|
// A single load+store correctly handles overlapping memory in the memmove
|
|
|
|
// case.
|
2008-01-13 23:50:23 +00:00
|
|
|
unsigned Size = MemOpLength->getZExtValue();
|
2008-04-30 06:39:11 +00:00
|
|
|
if (Size == 0) return MI; // Delete this mem transfer.
|
|
|
|
|
|
|
|
if (Size > 8 || (Size&(Size-1)))
|
2008-01-14 00:28:35 +00:00
|
|
|
return 0; // If not 1/2/4/8 bytes, exit.
|
2008-01-13 23:50:23 +00:00
|
|
|
|
2008-01-14 00:28:35 +00:00
|
|
|
// Use an integer load+store unless we can find something better.
|
2008-01-13 23:50:23 +00:00
|
|
|
Type *NewPtrTy = PointerType::getUnqual(IntegerType::get(Size<<3));
|
2008-01-14 00:28:35 +00:00
|
|
|
|
|
|
|
// Memcpy forces the use of i8* for the source and destination. That means
|
|
|
|
// that if you're using memcpy to move one double around, you'll get a cast
|
|
|
|
// from double* to i8*. We'd much rather use a double load+store rather than
|
|
|
|
// an i64 load+store, here because this improves the odds that the source or
|
|
|
|
// dest address will be promotable. See if we can find a better type than the
|
|
|
|
// integer datatype.
|
|
|
|
if (Value *Op = getBitCastOperand(MI->getOperand(1))) {
|
|
|
|
const Type *SrcETy = cast<PointerType>(Op->getType())->getElementType();
|
|
|
|
if (SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) {
|
|
|
|
// The SrcETy might be something like {{{double}}} or [1 x double]. Rip
|
|
|
|
// down through these levels if so.
|
|
|
|
while (!SrcETy->isFirstClassType()) {
|
|
|
|
if (const StructType *STy = dyn_cast<StructType>(SrcETy)) {
|
|
|
|
if (STy->getNumElements() == 1)
|
|
|
|
SrcETy = STy->getElementType(0);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
} else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcETy)) {
|
|
|
|
if (ATy->getNumElements() == 1)
|
|
|
|
SrcETy = ATy->getElementType();
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SrcETy->isFirstClassType())
|
|
|
|
NewPtrTy = PointerType::getUnqual(SrcETy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-13 23:50:23 +00:00
|
|
|
// If the memcpy/memmove provides better alignment info than we can
|
|
|
|
// infer, use it.
|
|
|
|
SrcAlign = std::max(SrcAlign, CopyAlign);
|
|
|
|
DstAlign = std::max(DstAlign, CopyAlign);
|
|
|
|
|
|
|
|
Value *Src = InsertBitCastBefore(MI->getOperand(2), NewPtrTy, *MI);
|
|
|
|
Value *Dest = InsertBitCastBefore(MI->getOperand(1), NewPtrTy, *MI);
|
2008-01-14 00:28:35 +00:00
|
|
|
Instruction *L = new LoadInst(Src, "tmp", false, SrcAlign);
|
|
|
|
InsertNewInstBefore(L, *MI);
|
|
|
|
InsertNewInstBefore(new StoreInst(L, Dest, false, DstAlign), *MI);
|
|
|
|
|
|
|
|
// Set the size of the copy to 0, it will be deleted on the next iteration.
|
|
|
|
MI->setOperand(3, Constant::getNullValue(MemOpLength->getType()));
|
|
|
|
return MI;
|
2008-01-13 23:50:23 +00:00
|
|
|
}
|
2004-03-12 05:52:32 +00:00
|
|
|
|
2008-04-30 06:39:11 +00:00
|
|
|
Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) {
|
|
|
|
unsigned Alignment = GetOrEnforceKnownAlignment(MI->getDest());
|
|
|
|
if (MI->getAlignment()->getZExtValue() < Alignment) {
|
|
|
|
MI->setAlignment(ConstantInt::get(Type::Int32Ty, Alignment));
|
|
|
|
return MI;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the length and alignment and fill if they are constant.
|
|
|
|
ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength());
|
|
|
|
ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue());
|
|
|
|
if (!LenC || !FillC || FillC->getType() != Type::Int8Ty)
|
|
|
|
return 0;
|
|
|
|
uint64_t Len = LenC->getZExtValue();
|
|
|
|
Alignment = MI->getAlignment()->getZExtValue();
|
|
|
|
|
|
|
|
// If the length is zero, this is a no-op
|
|
|
|
if (Len == 0) return MI; // memset(d,c,0,a) -> noop
|
|
|
|
|
|
|
|
// memset(s,c,n) -> store s, c (for n=1,2,4,8)
|
|
|
|
if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) {
|
|
|
|
const Type *ITy = IntegerType::get(Len*8); // n=1 -> i8.
|
|
|
|
|
|
|
|
Value *Dest = MI->getDest();
|
|
|
|
Dest = InsertBitCastBefore(Dest, PointerType::getUnqual(ITy), *MI);
|
|
|
|
|
|
|
|
// Alignment 0 is identity for alignment 1 for memset, but not store.
|
|
|
|
if (Alignment == 0) Alignment = 1;
|
|
|
|
|
|
|
|
// Extract the fill value and store.
|
|
|
|
uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL;
|
|
|
|
InsertNewInstBefore(new StoreInst(ConstantInt::get(ITy, Fill), Dest, false,
|
|
|
|
Alignment), *MI);
|
|
|
|
|
|
|
|
// Set the size of the copy to 0, it will be deleted on the next iteration.
|
|
|
|
MI->setLength(Constant::getNullValue(LenC->getType()));
|
|
|
|
return MI;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-01-13 20:11:04 +00:00
|
|
|
/// visitCallInst - CallInst simplification. This mostly only handles folding
|
|
|
|
/// of intrinsic instructions. For normal calls, it allows visitCallSite to do
|
|
|
|
/// the heavy lifting.
|
|
|
|
///
|
2003-06-19 17:00:31 +00:00
|
|
|
Instruction *InstCombiner::visitCallInst(CallInst &CI) {
|
2006-01-13 20:11:04 +00:00
|
|
|
IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI);
|
|
|
|
if (!II) return visitCallSite(&CI);
|
|
|
|
|
2004-02-28 05:22:00 +00:00
|
|
|
// Intrinsics cannot occur in an invoke, so handle them here instead of in
|
|
|
|
// visitCallSite.
|
2006-01-13 20:11:04 +00:00
|
|
|
if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) {
|
2004-10-12 04:52:52 +00:00
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
// memmove/cpy/set of zero bytes is a noop.
|
|
|
|
if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) {
|
|
|
|
if (NumBytes->isNullValue()) return EraseInstFromFunction(CI);
|
|
|
|
|
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes))
|
2006-10-20 07:07:24 +00:00
|
|
|
if (CI->getZExtValue() == 1) {
|
2004-10-12 04:52:52 +00:00
|
|
|
// Replace the instruction with just byte operations. We would
|
|
|
|
// transform other cases to loads/stores, but we don't know if
|
|
|
|
// alignment is sufficient.
|
|
|
|
}
|
2004-02-28 05:22:00 +00:00
|
|
|
}
|
|
|
|
|
2004-10-12 04:52:52 +00:00
|
|
|
// If we have a memmove and the source operation is a constant global,
|
|
|
|
// then the source and dest pointers can't alias, so we can change this
|
|
|
|
// into a call to memcpy.
|
2008-01-13 23:50:23 +00:00
|
|
|
if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) {
|
2004-10-12 04:52:52 +00:00
|
|
|
if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource()))
|
|
|
|
if (GVSrc->isConstant()) {
|
|
|
|
Module *M = CI.getParent()->getParent()->getParent();
|
2008-01-13 22:23:22 +00:00
|
|
|
Intrinsic::ID MemCpyID;
|
|
|
|
if (CI.getOperand(3)->getType() == Type::Int32Ty)
|
|
|
|
MemCpyID = Intrinsic::memcpy_i32;
|
2006-03-03 01:34:17 +00:00
|
|
|
else
|
2008-01-13 22:23:22 +00:00
|
|
|
MemCpyID = Intrinsic::memcpy_i64;
|
|
|
|
CI.setOperand(0, Intrinsic::getDeclaration(M, MemCpyID));
|
2004-10-12 04:52:52 +00:00
|
|
|
Changed = true;
|
|
|
|
}
|
2006-03-06 20:18:44 +00:00
|
|
|
}
|
2004-10-12 04:52:52 +00:00
|
|
|
|
2006-03-06 20:18:44 +00:00
|
|
|
// If we can determine a pointer alignment that is bigger than currently
|
|
|
|
// set, update the alignment.
|
|
|
|
if (isa<MemCpyInst>(MI) || isa<MemMoveInst>(MI)) {
|
2008-01-13 23:50:23 +00:00
|
|
|
if (Instruction *I = SimplifyMemTransfer(MI))
|
|
|
|
return I;
|
2008-04-30 06:39:11 +00:00
|
|
|
} else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) {
|
|
|
|
if (Instruction *I = SimplifyMemSet(MSI))
|
|
|
|
return I;
|
2006-03-06 20:18:44 +00:00
|
|
|
}
|
|
|
|
|
2006-01-13 20:11:04 +00:00
|
|
|
if (Changed) return II;
|
2006-01-13 21:28:09 +00:00
|
|
|
} else {
|
|
|
|
switch (II->getIntrinsicID()) {
|
|
|
|
default: break;
|
Turn altivec lvx/stvx intrinsics into loads and stores. This allows the
elimination of one load from this:
int AreSecondAndThirdElementsBothNegative( vector float *in ) {
#define QNaN 0x7FC00000
const vector unsigned int testData = (vector unsigned int)( QNaN, 0, 0, QNaN );
vector float test = vec_ld( 0, (float*) &testData );
return ! vec_any_ge( test, *in );
}
Now generating:
_AreSecondAndThirdElementsBothNegative:
mfspr r2, 256
oris r4, r2, 49152
mtspr 256, r4
li r4, lo16(LCPI1_0)
lis r5, ha16(LCPI1_0)
addi r6, r1, -16
lvx v0, r5, r4
stvx v0, 0, r6
lvx v1, 0, r3
vcmpgefp. v0, v0, v1
mfcr r3, 2
rlwinm r3, r3, 27, 31, 31
xori r3, r3, 1
cntlzw r3, r3
srwi r3, r3, 5
mtspr 256, r2
blr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@27352 91177308-0d34-0410-b5e6-96231b3b80d8
2006-04-02 05:30:25 +00:00
|
|
|
case Intrinsic::ppc_altivec_lvx:
|
|
|
|
case Intrinsic::ppc_altivec_lvxl:
|
2006-04-17 22:26:56 +00:00
|
|
|
case Intrinsic::x86_sse_loadu_ps:
|
|
|
|
case Intrinsic::x86_sse2_loadu_pd:
|
|
|
|
case Intrinsic::x86_sse2_loadu_dq:
|
|
|
|
// Turn PPC lvx -> load if the pointer is known aligned.
|
|
|
|
// Turn X86 loadups -> load if the pointer is known aligned.
|
2008-04-10 18:43:06 +00:00
|
|
|
if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) {
|
2008-01-13 22:23:22 +00:00
|
|
|
Value *Ptr = InsertBitCastBefore(II->getOperand(1),
|
|
|
|
PointerType::getUnqual(II->getType()),
|
|
|
|
CI);
|
Turn altivec lvx/stvx intrinsics into loads and stores. This allows the
elimination of one load from this:
int AreSecondAndThirdElementsBothNegative( vector float *in ) {
#define QNaN 0x7FC00000
const vector unsigned int testData = (vector unsigned int)( QNaN, 0, 0, QNaN );
vector float test = vec_ld( 0, (float*) &testData );
return ! vec_any_ge( test, *in );
}
Now generating:
_AreSecondAndThirdElementsBothNegative:
mfspr r2, 256
oris r4, r2, 49152
mtspr 256, r4
li r4, lo16(LCPI1_0)
lis r5, ha16(LCPI1_0)
addi r6, r1, -16
lvx v0, r5, r4
stvx v0, 0, r6
lvx v1, 0, r3
vcmpgefp. v0, v0, v1
mfcr r3, 2
rlwinm r3, r3, 27, 31, 31
xori r3, r3, 1
cntlzw r3, r3
srwi r3, r3, 5
mtspr 256, r2
blr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@27352 91177308-0d34-0410-b5e6-96231b3b80d8
2006-04-02 05:30:25 +00:00
|
|
|
return new LoadInst(Ptr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Intrinsic::ppc_altivec_stvx:
|
|
|
|
case Intrinsic::ppc_altivec_stvxl:
|
|
|
|
// Turn stvx -> store if the pointer is known aligned.
|
2008-04-10 18:43:06 +00:00
|
|
|
if (GetOrEnforceKnownAlignment(II->getOperand(2), 16) >= 16) {
|
2007-12-17 01:12:55 +00:00
|
|
|
const Type *OpPtrTy =
|
|
|
|
PointerType::getUnqual(II->getOperand(1)->getType());
|
2008-01-13 22:23:22 +00:00
|
|
|
Value *Ptr = InsertBitCastBefore(II->getOperand(2), OpPtrTy, CI);
|
Turn altivec lvx/stvx intrinsics into loads and stores. This allows the
elimination of one load from this:
int AreSecondAndThirdElementsBothNegative( vector float *in ) {
#define QNaN 0x7FC00000
const vector unsigned int testData = (vector unsigned int)( QNaN, 0, 0, QNaN );
vector float test = vec_ld( 0, (float*) &testData );
return ! vec_any_ge( test, *in );
}
Now generating:
_AreSecondAndThirdElementsBothNegative:
mfspr r2, 256
oris r4, r2, 49152
mtspr 256, r4
li r4, lo16(LCPI1_0)
lis r5, ha16(LCPI1_0)
addi r6, r1, -16
lvx v0, r5, r4
stvx v0, 0, r6
lvx v1, 0, r3
vcmpgefp. v0, v0, v1
mfcr r3, 2
rlwinm r3, r3, 27, 31, 31
xori r3, r3, 1
cntlzw r3, r3
srwi r3, r3, 5
mtspr 256, r2
blr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@27352 91177308-0d34-0410-b5e6-96231b3b80d8
2006-04-02 05:30:25 +00:00
|
|
|
return new StoreInst(II->getOperand(1), Ptr);
|
|
|
|
}
|
|
|
|
break;
|
2006-04-17 22:26:56 +00:00
|
|
|
case Intrinsic::x86_sse_storeu_ps:
|
|
|
|
case Intrinsic::x86_sse2_storeu_pd:
|
|
|
|
case Intrinsic::x86_sse2_storeu_dq:
|
|
|
|
case Intrinsic::x86_sse2_storel_dq:
|
|
|
|
// Turn X86 storeu -> store if the pointer is known aligned.
|
2008-04-10 18:43:06 +00:00
|
|
|
if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) {
|
2007-12-17 01:12:55 +00:00
|
|
|
const Type *OpPtrTy =
|
|
|
|
PointerType::getUnqual(II->getOperand(2)->getType());
|
2008-01-13 22:23:22 +00:00
|
|
|
Value *Ptr = InsertBitCastBefore(II->getOperand(1), OpPtrTy, CI);
|
2006-04-17 22:26:56 +00:00
|
|
|
return new StoreInst(II->getOperand(2), Ptr);
|
|
|
|
}
|
|
|
|
break;
|
add a new SimplifyDemandedVectorElts method, which works similarly to
SimplifyDemandedBits. The idea is that some operations can be simplified if
not all of the computed elements are needed. Some targets (like x86) have a
large number of intrinsics that operate on a single element, but pass other
elts through unmodified. If those other elements are not needed, the
intrinsics can be simplified to scalar operations, and insertelement ops can
be removed.
This turns (f.e.):
ushort %Convert_sse(float %f) {
%tmp = insertelement <4 x float> undef, float %f, uint 0 ; <<4 x float>> [#uses=1]
%tmp10 = insertelement <4 x float> %tmp, float 0.000000e+00, uint 1 ; <<4 x float>> [#uses=1]
%tmp11 = insertelement <4 x float> %tmp10, float 0.000000e+00, uint 2 ; <<4 x float>> [#uses=1]
%tmp12 = insertelement <4 x float> %tmp11, float 0.000000e+00, uint 3 ; <<4 x float>> [#uses=1]
%tmp28 = tail call <4 x float> %llvm.x86.sse.sub.ss( <4 x float> %tmp12, <4 x float> < float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp37 = tail call <4 x float> %llvm.x86.sse.mul.ss( <4 x float> %tmp28, <4 x float> < float 5.000000e-01, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp37, <4 x float> < float 6.553500e+04, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> zeroinitializer ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
into:
ushort %Convert_sse(float %f) {
entry:
%tmp28 = sub float %f, 1.000000e+00 ; <float> [#uses=1]
%tmp37 = mul float %tmp28, 5.000000e-01 ; <float> [#uses=1]
%tmp375 = insertelement <4 x float> undef, float %tmp37, uint 0 ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp375, <4 x float> < float 6.553500e+04, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> < float 0.000000e+00, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
which improves codegen from:
_Convert_sse:
movss LCPI1_0, %xmm0
movss 4(%esp), %xmm1
subss %xmm0, %xmm1
movss LCPI1_1, %xmm0
mulss %xmm0, %xmm1
movss LCPI1_2, %xmm0
minss %xmm0, %xmm1
xorps %xmm0, %xmm0
maxss %xmm0, %xmm1
cvttss2si %xmm1, %eax
andl $65535, %eax
ret
to:
_Convert_sse:
movss 4(%esp), %xmm0
subss LCPI1_0, %xmm0
mulss LCPI1_1, %xmm0
movss LCPI1_2, %xmm1
minss %xmm1, %xmm0
xorps %xmm1, %xmm1
maxss %xmm1, %xmm0
cvttss2si %xmm0, %eax
andl $65535, %eax
ret
This is just a first step, it can be extended in many ways. Testcase here:
Transforms/InstCombine/vec_demanded_elts.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30752 91177308-0d34-0410-b5e6-96231b3b80d8
2006-10-05 06:55:50 +00:00
|
|
|
|
|
|
|
case Intrinsic::x86_sse_cvttss2si: {
|
|
|
|
// These intrinsics only demands the 0th element of its input vector. If
|
|
|
|
// we can simplify the input based on that, do so now.
|
|
|
|
uint64_t UndefElts;
|
|
|
|
if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), 1,
|
|
|
|
UndefElts)) {
|
|
|
|
II->setOperand(1, V);
|
|
|
|
return II;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
Lower vperm(x,y, mask) -> shuffle(x,y,mask) if mask is constant. This allows
us to compile oh-so-realistic stuff like this:
vec_vperm(A, B, (vector unsigned char){14});
to:
vspltb v0, v0, 14
instead of:
vspltisb v0, 14
vperm v0, v2, v1, v0
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@27452 91177308-0d34-0410-b5e6-96231b3b80d8
2006-04-06 19:19:17 +00:00
|
|
|
case Intrinsic::ppc_altivec_vperm:
|
|
|
|
// Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant.
|
2007-02-15 02:26:10 +00:00
|
|
|
if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getOperand(3))) {
|
Lower vperm(x,y, mask) -> shuffle(x,y,mask) if mask is constant. This allows
us to compile oh-so-realistic stuff like this:
vec_vperm(A, B, (vector unsigned char){14});
to:
vspltb v0, v0, 14
instead of:
vspltisb v0, 14
vperm v0, v2, v1, v0
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@27452 91177308-0d34-0410-b5e6-96231b3b80d8
2006-04-06 19:19:17 +00:00
|
|
|
assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!");
|
|
|
|
|
|
|
|
// Check that all of the elements are integer constants or undefs.
|
|
|
|
bool AllEltsOk = true;
|
|
|
|
for (unsigned i = 0; i != 16; ++i) {
|
|
|
|
if (!isa<ConstantInt>(Mask->getOperand(i)) &&
|
|
|
|
!isa<UndefValue>(Mask->getOperand(i))) {
|
|
|
|
AllEltsOk = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (AllEltsOk) {
|
|
|
|
// Cast the input vectors to byte vectors.
|
2008-01-13 22:23:22 +00:00
|
|
|
Value *Op0 =InsertBitCastBefore(II->getOperand(1),Mask->getType(),CI);
|
|
|
|
Value *Op1 =InsertBitCastBefore(II->getOperand(2),Mask->getType(),CI);
|
Lower vperm(x,y, mask) -> shuffle(x,y,mask) if mask is constant. This allows
us to compile oh-so-realistic stuff like this:
vec_vperm(A, B, (vector unsigned char){14});
to:
vspltb v0, v0, 14
instead of:
vspltisb v0, 14
vperm v0, v2, v1, v0
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@27452 91177308-0d34-0410-b5e6-96231b3b80d8
2006-04-06 19:19:17 +00:00
|
|
|
Value *Result = UndefValue::get(Op0->getType());
|
|
|
|
|
|
|
|
// Only extract each element once.
|
|
|
|
Value *ExtractedElts[32];
|
|
|
|
memset(ExtractedElts, 0, sizeof(ExtractedElts));
|
|
|
|
|
|
|
|
for (unsigned i = 0; i != 16; ++i) {
|
|
|
|
if (isa<UndefValue>(Mask->getOperand(i)))
|
|
|
|
continue;
|
2007-04-14 23:32:02 +00:00
|
|
|
unsigned Idx=cast<ConstantInt>(Mask->getOperand(i))->getZExtValue();
|
Lower vperm(x,y, mask) -> shuffle(x,y,mask) if mask is constant. This allows
us to compile oh-so-realistic stuff like this:
vec_vperm(A, B, (vector unsigned char){14});
to:
vspltb v0, v0, 14
instead of:
vspltisb v0, 14
vperm v0, v2, v1, v0
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@27452 91177308-0d34-0410-b5e6-96231b3b80d8
2006-04-06 19:19:17 +00:00
|
|
|
Idx &= 31; // Match the hardware behavior.
|
|
|
|
|
|
|
|
if (ExtractedElts[Idx] == 0) {
|
|
|
|
Instruction *Elt =
|
add a new SimplifyDemandedVectorElts method, which works similarly to
SimplifyDemandedBits. The idea is that some operations can be simplified if
not all of the computed elements are needed. Some targets (like x86) have a
large number of intrinsics that operate on a single element, but pass other
elts through unmodified. If those other elements are not needed, the
intrinsics can be simplified to scalar operations, and insertelement ops can
be removed.
This turns (f.e.):
ushort %Convert_sse(float %f) {
%tmp = insertelement <4 x float> undef, float %f, uint 0 ; <<4 x float>> [#uses=1]
%tmp10 = insertelement <4 x float> %tmp, float 0.000000e+00, uint 1 ; <<4 x float>> [#uses=1]
%tmp11 = insertelement <4 x float> %tmp10, float 0.000000e+00, uint 2 ; <<4 x float>> [#uses=1]
%tmp12 = insertelement <4 x float> %tmp11, float 0.000000e+00, uint 3 ; <<4 x float>> [#uses=1]
%tmp28 = tail call <4 x float> %llvm.x86.sse.sub.ss( <4 x float> %tmp12, <4 x float> < float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp37 = tail call <4 x float> %llvm.x86.sse.mul.ss( <4 x float> %tmp28, <4 x float> < float 5.000000e-01, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp37, <4 x float> < float 6.553500e+04, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> zeroinitializer ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
into:
ushort %Convert_sse(float %f) {
entry:
%tmp28 = sub float %f, 1.000000e+00 ; <float> [#uses=1]
%tmp37 = mul float %tmp28, 5.000000e-01 ; <float> [#uses=1]
%tmp375 = insertelement <4 x float> undef, float %tmp37, uint 0 ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp375, <4 x float> < float 6.553500e+04, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> < float 0.000000e+00, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
which improves codegen from:
_Convert_sse:
movss LCPI1_0, %xmm0
movss 4(%esp), %xmm1
subss %xmm0, %xmm1
movss LCPI1_1, %xmm0
mulss %xmm0, %xmm1
movss LCPI1_2, %xmm0
minss %xmm0, %xmm1
xorps %xmm0, %xmm0
maxss %xmm0, %xmm1
cvttss2si %xmm1, %eax
andl $65535, %eax
ret
to:
_Convert_sse:
movss 4(%esp), %xmm0
subss LCPI1_0, %xmm0
mulss LCPI1_1, %xmm0
movss LCPI1_2, %xmm1
minss %xmm1, %xmm0
xorps %xmm1, %xmm1
maxss %xmm1, %xmm0
cvttss2si %xmm0, %eax
andl $65535, %eax
ret
This is just a first step, it can be extended in many ways. Testcase here:
Transforms/InstCombine/vec_demanded_elts.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30752 91177308-0d34-0410-b5e6-96231b3b80d8
2006-10-05 06:55:50 +00:00
|
|
|
new ExtractElementInst(Idx < 16 ? Op0 : Op1, Idx&15, "tmp");
|
Lower vperm(x,y, mask) -> shuffle(x,y,mask) if mask is constant. This allows
us to compile oh-so-realistic stuff like this:
vec_vperm(A, B, (vector unsigned char){14});
to:
vspltb v0, v0, 14
instead of:
vspltisb v0, 14
vperm v0, v2, v1, v0
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@27452 91177308-0d34-0410-b5e6-96231b3b80d8
2006-04-06 19:19:17 +00:00
|
|
|
InsertNewInstBefore(Elt, CI);
|
|
|
|
ExtractedElts[Idx] = Elt;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert this value into the result vector.
|
2008-04-06 20:25:17 +00:00
|
|
|
Result = InsertElementInst::Create(Result, ExtractedElts[Idx], i, "tmp");
|
Lower vperm(x,y, mask) -> shuffle(x,y,mask) if mask is constant. This allows
us to compile oh-so-realistic stuff like this:
vec_vperm(A, B, (vector unsigned char){14});
to:
vspltb v0, v0, 14
instead of:
vspltisb v0, 14
vperm v0, v2, v1, v0
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@27452 91177308-0d34-0410-b5e6-96231b3b80d8
2006-04-06 19:19:17 +00:00
|
|
|
InsertNewInstBefore(cast<Instruction>(Result), CI);
|
|
|
|
}
|
2006-11-27 01:05:10 +00:00
|
|
|
return CastInst::create(Instruction::BitCast, Result, CI.getType());
|
Lower vperm(x,y, mask) -> shuffle(x,y,mask) if mask is constant. This allows
us to compile oh-so-realistic stuff like this:
vec_vperm(A, B, (vector unsigned char){14});
to:
vspltb v0, v0, 14
instead of:
vspltisb v0, 14
vperm v0, v2, v1, v0
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@27452 91177308-0d34-0410-b5e6-96231b3b80d8
2006-04-06 19:19:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2006-01-13 21:28:09 +00:00
|
|
|
case Intrinsic::stackrestore: {
|
|
|
|
// If the save is right next to the restore, remove the restore. This can
|
|
|
|
// happen when variable allocas are DCE'd.
|
|
|
|
if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) {
|
|
|
|
if (SS->getIntrinsicID() == Intrinsic::stacksave) {
|
|
|
|
BasicBlock::iterator BI = SS;
|
|
|
|
if (&*++BI == II)
|
|
|
|
return EraseInstFromFunction(CI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-18 06:12:38 +00:00
|
|
|
// Scan down this block to see if there is another stack restore in the
|
|
|
|
// same block without an intervening call/alloca.
|
|
|
|
BasicBlock::iterator BI = II;
|
2006-01-13 21:28:09 +00:00
|
|
|
TerminatorInst *TI = II->getParent()->getTerminator();
|
2008-02-18 06:12:38 +00:00
|
|
|
bool CannotRemove = false;
|
|
|
|
for (++BI; &*BI != TI; ++BI) {
|
|
|
|
if (isa<AllocaInst>(BI)) {
|
|
|
|
CannotRemove = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (isa<CallInst>(BI)) {
|
|
|
|
if (!isa<IntrinsicInst>(BI)) {
|
2006-01-13 21:28:09 +00:00
|
|
|
CannotRemove = true;
|
|
|
|
break;
|
|
|
|
}
|
2008-02-18 06:12:38 +00:00
|
|
|
// If there is a stackrestore below this one, remove this one.
|
2006-01-13 21:28:09 +00:00
|
|
|
return EraseInstFromFunction(CI);
|
2008-02-18 06:12:38 +00:00
|
|
|
}
|
2006-01-13 21:28:09 +00:00
|
|
|
}
|
2008-02-18 06:12:38 +00:00
|
|
|
|
|
|
|
// If the stack restore is in a return/unwind block and if there are no
|
|
|
|
// allocas or calls between the restore and the return, nuke the restore.
|
|
|
|
if (!CannotRemove && (isa<ReturnInst>(TI) || isa<UnwindInst>(TI)))
|
|
|
|
return EraseInstFromFunction(CI);
|
2006-01-13 21:28:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-10-12 04:52:52 +00:00
|
|
|
}
|
|
|
|
|
2006-01-13 20:11:04 +00:00
|
|
|
return visitCallSite(II);
|
2003-06-19 17:00:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InvokeInst simplification
|
|
|
|
//
|
|
|
|
Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) {
|
2003-10-07 22:32:43 +00:00
|
|
|
return visitCallSite(&II);
|
2003-06-19 17:00:31 +00:00
|
|
|
}
|
|
|
|
|
2008-04-25 21:16:07 +00:00
|
|
|
/// isSafeToEliminateVarargsCast - If this cast does not affect the value
|
|
|
|
/// passed through the varargs area, we can eliminate the use of the cast.
|
2008-04-23 18:34:37 +00:00
|
|
|
static bool isSafeToEliminateVarargsCast(const CallSite CS,
|
|
|
|
const CastInst * const CI,
|
|
|
|
const TargetData * const TD,
|
|
|
|
const int ix) {
|
|
|
|
if (!CI->isLosslessCast())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// The size of ByVal arguments is derived from the type, so we
|
|
|
|
// can't change to a type with a different size. If the size were
|
|
|
|
// passed explicitly we could avoid this check.
|
|
|
|
if (!CS.paramHasAttr(ix, ParamAttr::ByVal))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const Type* SrcTy =
|
|
|
|
cast<PointerType>(CI->getOperand(0)->getType())->getElementType();
|
|
|
|
const Type* DstTy = cast<PointerType>(CI->getType())->getElementType();
|
|
|
|
if (!SrcTy->isSized() || !DstTy->isSized())
|
|
|
|
return false;
|
|
|
|
if (TD->getABITypeSize(SrcTy) != TD->getABITypeSize(DstTy))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2003-10-07 22:32:43 +00:00
|
|
|
// visitCallSite - Improvements for call and invoke instructions.
|
|
|
|
//
|
|
|
|
Instruction *InstCombiner::visitCallSite(CallSite CS) {
|
2003-10-07 22:54:13 +00:00
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
// If the callee is a constexpr cast of a function, attempt to move the cast
|
|
|
|
// to the arguments of the call/invoke.
|
2003-10-07 22:32:43 +00:00
|
|
|
if (transformConstExprCastCall(CS)) return 0;
|
|
|
|
|
2003-10-07 22:54:13 +00:00
|
|
|
Value *Callee = CS.getCalledValue();
|
2004-10-16 18:11:37 +00:00
|
|
|
|
2005-05-13 07:09:09 +00:00
|
|
|
if (Function *CalleeF = dyn_cast<Function>(Callee))
|
|
|
|
if (CalleeF->getCallingConv() != CS.getCallingConv()) {
|
|
|
|
Instruction *OldCall = CS.getInstruction();
|
|
|
|
// If the call and callee calling conventions don't match, this call must
|
|
|
|
// be unreachable, as the call is undefined.
|
2007-01-11 12:24:14 +00:00
|
|
|
new StoreInst(ConstantInt::getTrue(),
|
2007-12-17 01:12:55 +00:00
|
|
|
UndefValue::get(PointerType::getUnqual(Type::Int1Ty)),
|
|
|
|
OldCall);
|
2005-05-13 07:09:09 +00:00
|
|
|
if (!OldCall->use_empty())
|
|
|
|
OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType()));
|
|
|
|
if (isa<CallInst>(OldCall)) // Not worth removing an invoke here.
|
|
|
|
return EraseInstFromFunction(*OldCall);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-10-18 02:59:09 +00:00
|
|
|
if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) {
|
|
|
|
// This instruction is not reachable, just remove it. We insert a store to
|
|
|
|
// undef so that we know that this code is not reachable, despite the fact
|
|
|
|
// that we can't modify the CFG here.
|
2007-01-11 12:24:14 +00:00
|
|
|
new StoreInst(ConstantInt::getTrue(),
|
2007-12-17 01:12:55 +00:00
|
|
|
UndefValue::get(PointerType::getUnqual(Type::Int1Ty)),
|
2004-10-18 02:59:09 +00:00
|
|
|
CS.getInstruction());
|
|
|
|
|
|
|
|
if (!CS.getInstruction()->use_empty())
|
|
|
|
CS.getInstruction()->
|
|
|
|
replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType()));
|
|
|
|
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
|
|
|
|
// Don't break the CFG, insert a dummy cond branch.
|
2008-04-06 20:25:17 +00:00
|
|
|
BranchInst::Create(II->getNormalDest(), II->getUnwindDest(),
|
|
|
|
ConstantInt::getTrue(), II);
|
2004-10-16 18:11:37 +00:00
|
|
|
}
|
2004-10-18 02:59:09 +00:00
|
|
|
return EraseInstFromFunction(*CS.getInstruction());
|
|
|
|
}
|
2004-10-16 18:11:37 +00:00
|
|
|
|
2007-09-17 10:26:40 +00:00
|
|
|
if (BitCastInst *BC = dyn_cast<BitCastInst>(Callee))
|
|
|
|
if (IntrinsicInst *In = dyn_cast<IntrinsicInst>(BC->getOperand(0)))
|
|
|
|
if (In->getIntrinsicID() == Intrinsic::init_trampoline)
|
|
|
|
return transformCallThroughTrampoline(CS);
|
|
|
|
|
2003-10-07 22:54:13 +00:00
|
|
|
const PointerType *PTy = cast<PointerType>(Callee->getType());
|
|
|
|
const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
|
|
|
|
if (FTy->isVarArg()) {
|
2008-04-23 01:03:05 +00:00
|
|
|
int ix = FTy->getNumParams() + (isa<InvokeInst>(Callee) ? 3 : 1);
|
2003-10-07 22:54:13 +00:00
|
|
|
// See if we can optimize any arguments passed through the varargs area of
|
|
|
|
// the call.
|
|
|
|
for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(),
|
2008-04-23 18:34:37 +00:00
|
|
|
E = CS.arg_end(); I != E; ++I, ++ix) {
|
|
|
|
CastInst *CI = dyn_cast<CastInst>(*I);
|
|
|
|
if (CI && isSafeToEliminateVarargsCast(CS, CI, TD, ix)) {
|
|
|
|
*I = CI->getOperand(0);
|
|
|
|
Changed = true;
|
2003-10-07 22:54:13 +00:00
|
|
|
}
|
2008-04-23 18:34:37 +00:00
|
|
|
}
|
2003-10-07 22:54:13 +00:00
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2007-12-19 21:13:37 +00:00
|
|
|
if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) {
|
2007-12-16 15:51:49 +00:00
|
|
|
// Inline asm calls cannot throw - mark them 'nounwind'.
|
2007-12-19 21:13:37 +00:00
|
|
|
CS.setDoesNotThrow();
|
2007-12-16 15:51:49 +00:00
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
2003-10-07 22:54:13 +00:00
|
|
|
return Changed ? CS.getInstruction() : 0;
|
2003-10-07 22:32:43 +00:00
|
|
|
}
|
|
|
|
|
2003-06-19 17:00:31 +00:00
|
|
|
// transformConstExprCastCall - If the callee is a constexpr cast of a function,
|
|
|
|
// attempt to move the cast to the arguments of the call/invoke.
|
|
|
|
//
|
|
|
|
bool InstCombiner::transformConstExprCastCall(CallSite CS) {
|
|
|
|
if (!isa<ConstantExpr>(CS.getCalledValue())) return false;
|
|
|
|
ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue());
|
2006-11-27 01:05:10 +00:00
|
|
|
if (CE->getOpcode() != Instruction::BitCast ||
|
|
|
|
!isa<Function>(CE->getOperand(0)))
|
2003-06-19 17:00:31 +00:00
|
|
|
return false;
|
2004-07-18 00:38:32 +00:00
|
|
|
Function *Callee = cast<Function>(CE->getOperand(0));
|
2003-06-19 17:00:31 +00:00
|
|
|
Instruction *Caller = CS.getInstruction();
|
2008-03-12 17:45:29 +00:00
|
|
|
const PAListPtr &CallerPAL = CS.getParamAttrs();
|
2003-06-19 17:00:31 +00:00
|
|
|
|
|
|
|
// Okay, this is a cast from a function to a different type. Unless doing so
|
|
|
|
// would cause a type conversion of one of our arguments, change this call to
|
|
|
|
// be a direct call with arguments casted to the appropriate types.
|
|
|
|
//
|
|
|
|
const FunctionType *FT = Callee->getFunctionType();
|
|
|
|
const Type *OldRetTy = Caller->getType();
|
|
|
|
|
2008-03-11 18:04:06 +00:00
|
|
|
if (isa<StructType>(FT->getReturnType()))
|
|
|
|
return false; // TODO: Handle multiple return values.
|
|
|
|
|
2004-01-14 06:06:08 +00:00
|
|
|
// Check to see if we are changing the return type...
|
|
|
|
if (OldRetTy != FT->getReturnType()) {
|
2007-01-30 20:08:39 +00:00
|
|
|
if (Callee->isDeclaration() && !Caller->use_empty() &&
|
2007-01-06 19:53:32 +00:00
|
|
|
// Conversion is ok if changing from pointer to int of same size.
|
|
|
|
!(isa<PointerType>(FT->getReturnType()) &&
|
|
|
|
TD->getIntPtrType() == OldRetTy))
|
2007-01-06 02:09:32 +00:00
|
|
|
return false; // Cannot transform this return value.
|
2004-01-14 06:06:08 +00:00
|
|
|
|
2008-01-06 10:12:28 +00:00
|
|
|
if (!Caller->use_empty() &&
|
|
|
|
// void -> non-void is handled specially
|
2008-01-13 08:02:44 +00:00
|
|
|
FT->getReturnType() != Type::VoidTy &&
|
|
|
|
!CastInst::isCastable(FT->getReturnType(), OldRetTy))
|
2008-01-06 10:12:28 +00:00
|
|
|
return false; // Cannot transform this return value.
|
|
|
|
|
2008-03-12 17:45:29 +00:00
|
|
|
if (!CallerPAL.isEmpty() && !Caller->use_empty()) {
|
|
|
|
ParameterAttributes RAttrs = CallerPAL.getParamAttrs(0);
|
2008-01-07 17:16:06 +00:00
|
|
|
if (RAttrs & ParamAttr::typeIncompatible(FT->getReturnType()))
|
|
|
|
return false; // Attribute not compatible with transformed value.
|
|
|
|
}
|
2008-01-06 18:27:01 +00:00
|
|
|
|
2004-01-14 06:06:08 +00:00
|
|
|
// If the callsite is an invoke instruction, and the return value is used by
|
|
|
|
// a PHI node in a successor, we cannot change the return type of the call
|
|
|
|
// because there is no place to put the cast instruction (without breaking
|
|
|
|
// the critical edge). Bail out in this case.
|
|
|
|
if (!Caller->use_empty())
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(Caller))
|
|
|
|
for (Value::use_iterator UI = II->use_begin(), E = II->use_end();
|
|
|
|
UI != E; ++UI)
|
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(*UI))
|
|
|
|
if (PN->getParent() == II->getNormalDest() ||
|
2004-02-08 21:44:31 +00:00
|
|
|
PN->getParent() == II->getUnwindDest())
|
2004-01-14 06:06:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2003-06-19 17:00:31 +00:00
|
|
|
|
|
|
|
unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin());
|
|
|
|
unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2003-06-19 17:00:31 +00:00
|
|
|
CallSite::arg_iterator AI = CS.arg_begin();
|
|
|
|
for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) {
|
|
|
|
const Type *ParamTy = FT->getParamType(i);
|
2006-06-28 01:01:52 +00:00
|
|
|
const Type *ActTy = (*AI)->getType();
|
2008-01-06 10:12:28 +00:00
|
|
|
|
|
|
|
if (!CastInst::isCastable(ActTy, ParamTy))
|
2008-01-06 18:27:01 +00:00
|
|
|
return false; // Cannot transform this parameter value.
|
|
|
|
|
2008-03-12 17:45:29 +00:00
|
|
|
if (CallerPAL.getParamAttrs(i + 1) & ParamAttr::typeIncompatible(ParamTy))
|
|
|
|
return false; // Attribute not compatible with transformed value.
|
2008-01-06 10:12:28 +00:00
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
ConstantInt *c = dyn_cast<ConstantInt>(*AI);
|
2008-01-06 10:12:28 +00:00
|
|
|
// Some conversions are safe even if we do not have a body.
|
|
|
|
// Either we can cast directly, or we can upconvert the argument
|
2007-01-06 02:09:32 +00:00
|
|
|
bool isConvertible = ActTy == ParamTy ||
|
2007-01-06 19:53:32 +00:00
|
|
|
(isa<PointerType>(ParamTy) && isa<PointerType>(ActTy)) ||
|
2007-01-15 02:27:26 +00:00
|
|
|
(ParamTy->isInteger() && ActTy->isInteger() &&
|
2007-01-08 16:32:00 +00:00
|
|
|
ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits()) ||
|
|
|
|
(c && ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits()
|
2007-03-25 05:01:29 +00:00
|
|
|
&& c->getValue().isStrictlyPositive());
|
2007-01-30 20:08:39 +00:00
|
|
|
if (Callee->isDeclaration() && !isConvertible) return false;
|
2003-06-19 17:00:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() &&
|
2007-01-30 20:08:39 +00:00
|
|
|
Callee->isDeclaration())
|
2008-03-12 17:45:29 +00:00
|
|
|
return false; // Do not delete arguments unless we have a function body.
|
2003-06-19 17:00:31 +00:00
|
|
|
|
2008-03-12 17:45:29 +00:00
|
|
|
if (FT->getNumParams() < NumActualArgs && FT->isVarArg() &&
|
|
|
|
!CallerPAL.isEmpty())
|
2008-01-06 18:27:01 +00:00
|
|
|
// In this case we have more arguments than the new function type, but we
|
2008-01-13 08:02:44 +00:00
|
|
|
// won't be dropping them. Check that these extra arguments have attributes
|
|
|
|
// that are compatible with being a vararg call argument.
|
2008-03-12 17:45:29 +00:00
|
|
|
for (unsigned i = CallerPAL.getNumSlots(); i; --i) {
|
|
|
|
if (CallerPAL.getSlot(i - 1).Index <= FT->getNumParams())
|
2008-01-13 08:02:44 +00:00
|
|
|
break;
|
2008-03-12 17:45:29 +00:00
|
|
|
ParameterAttributes PAttrs = CallerPAL.getSlot(i - 1).Attrs;
|
2008-01-13 08:02:44 +00:00
|
|
|
if (PAttrs & ParamAttr::VarArgsIncompatible)
|
|
|
|
return false;
|
|
|
|
}
|
2008-01-06 18:27:01 +00:00
|
|
|
|
2003-06-19 17:00:31 +00:00
|
|
|
// Okay, we decided that this is a safe thing to do: go ahead and start
|
|
|
|
// inserting cast instructions as necessary...
|
|
|
|
std::vector<Value*> Args;
|
|
|
|
Args.reserve(NumActualArgs);
|
2008-03-12 17:45:29 +00:00
|
|
|
SmallVector<ParamAttrsWithIndex, 8> attrVec;
|
2008-01-06 18:27:01 +00:00
|
|
|
attrVec.reserve(NumCommonArgs);
|
|
|
|
|
|
|
|
// Get any return attributes.
|
2008-03-12 17:45:29 +00:00
|
|
|
ParameterAttributes RAttrs = CallerPAL.getParamAttrs(0);
|
2008-01-06 18:27:01 +00:00
|
|
|
|
|
|
|
// If the return value is not being used, the type may not be compatible
|
|
|
|
// with the existing attributes. Wipe out any problematic attributes.
|
2008-01-07 17:16:06 +00:00
|
|
|
RAttrs &= ~ParamAttr::typeIncompatible(FT->getReturnType());
|
2008-01-06 18:27:01 +00:00
|
|
|
|
|
|
|
// Add the new return attributes.
|
|
|
|
if (RAttrs)
|
|
|
|
attrVec.push_back(ParamAttrsWithIndex::get(0, RAttrs));
|
2003-06-19 17:00:31 +00:00
|
|
|
|
|
|
|
AI = CS.arg_begin();
|
|
|
|
for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) {
|
|
|
|
const Type *ParamTy = FT->getParamType(i);
|
|
|
|
if ((*AI)->getType() == ParamTy) {
|
|
|
|
Args.push_back(*AI);
|
|
|
|
} else {
|
2006-12-18 08:47:13 +00:00
|
|
|
Instruction::CastOps opcode = CastInst::getCastOpcode(*AI,
|
2006-12-31 05:48:39 +00:00
|
|
|
false, ParamTy, false);
|
2006-12-18 08:47:13 +00:00
|
|
|
CastInst *NewCast = CastInst::create(opcode, *AI, ParamTy, "tmp");
|
2006-11-27 01:05:10 +00:00
|
|
|
Args.push_back(InsertNewInstBefore(NewCast, *Caller));
|
2003-06-19 17:00:31 +00:00
|
|
|
}
|
2008-01-06 18:27:01 +00:00
|
|
|
|
|
|
|
// Add any parameter attributes.
|
2008-03-12 17:45:29 +00:00
|
|
|
if (ParameterAttributes PAttrs = CallerPAL.getParamAttrs(i + 1))
|
2008-01-06 18:27:01 +00:00
|
|
|
attrVec.push_back(ParamAttrsWithIndex::get(i + 1, PAttrs));
|
2003-06-19 17:00:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the function takes more arguments than the call was taking, add them
|
|
|
|
// now...
|
|
|
|
for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i)
|
|
|
|
Args.push_back(Constant::getNullValue(FT->getParamType(i)));
|
|
|
|
|
|
|
|
// If we are removing arguments to the function, emit an obnoxious warning...
|
2008-02-20 11:26:25 +00:00
|
|
|
if (FT->getNumParams() < NumActualArgs) {
|
2003-06-19 17:00:31 +00:00
|
|
|
if (!FT->isVarArg()) {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << "WARNING: While resolving call to function '"
|
|
|
|
<< Callee->getName() << "' arguments were dropped!\n";
|
2003-06-19 17:00:31 +00:00
|
|
|
} else {
|
|
|
|
// Add all of the arguments in their promoted form to the arg list...
|
|
|
|
for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) {
|
|
|
|
const Type *PTy = getPromotedType((*AI)->getType());
|
|
|
|
if (PTy != (*AI)->getType()) {
|
|
|
|
// Must promote to pass through va_arg area!
|
2006-12-31 05:48:39 +00:00
|
|
|
Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, false,
|
|
|
|
PTy, false);
|
2006-12-18 08:47:13 +00:00
|
|
|
Instruction *Cast = CastInst::create(opcode, *AI, PTy, "tmp");
|
2003-06-19 17:00:31 +00:00
|
|
|
InsertNewInstBefore(Cast, *Caller);
|
|
|
|
Args.push_back(Cast);
|
|
|
|
} else {
|
|
|
|
Args.push_back(*AI);
|
|
|
|
}
|
2008-01-06 18:27:01 +00:00
|
|
|
|
2008-01-13 08:02:44 +00:00
|
|
|
// Add any parameter attributes.
|
2008-03-12 17:45:29 +00:00
|
|
|
if (ParameterAttributes PAttrs = CallerPAL.getParamAttrs(i + 1))
|
2008-01-13 08:02:44 +00:00
|
|
|
attrVec.push_back(ParamAttrsWithIndex::get(i + 1, PAttrs));
|
|
|
|
}
|
2003-06-19 17:00:31 +00:00
|
|
|
}
|
2008-02-20 11:26:25 +00:00
|
|
|
}
|
2003-06-19 17:00:31 +00:00
|
|
|
|
|
|
|
if (FT->getReturnType() == Type::VoidTy)
|
2007-02-11 01:23:03 +00:00
|
|
|
Caller->setName(""); // Void type should not have a name.
|
2003-06-19 17:00:31 +00:00
|
|
|
|
2008-03-12 17:45:29 +00:00
|
|
|
const PAListPtr &NewCallerPAL = PAListPtr::get(attrVec.begin(),attrVec.end());
|
2008-01-06 18:27:01 +00:00
|
|
|
|
2003-06-19 17:00:31 +00:00
|
|
|
Instruction *NC;
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
|
2008-04-06 20:25:17 +00:00
|
|
|
NC = InvokeInst::Create(Callee, II->getNormalDest(), II->getUnwindDest(),
|
|
|
|
Args.begin(), Args.end(), Caller->getName(), Caller);
|
2007-07-30 19:53:57 +00:00
|
|
|
cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv());
|
2008-01-06 18:27:01 +00:00
|
|
|
cast<InvokeInst>(NC)->setParamAttrs(NewCallerPAL);
|
2003-06-19 17:00:31 +00:00
|
|
|
} else {
|
2008-04-06 20:25:17 +00:00
|
|
|
NC = CallInst::Create(Callee, Args.begin(), Args.end(),
|
|
|
|
Caller->getName(), Caller);
|
2007-11-27 13:23:08 +00:00
|
|
|
CallInst *CI = cast<CallInst>(Caller);
|
|
|
|
if (CI->isTailCall())
|
2005-05-06 06:48:21 +00:00
|
|
|
cast<CallInst>(NC)->setTailCall();
|
2007-11-27 13:23:08 +00:00
|
|
|
cast<CallInst>(NC)->setCallingConv(CI->getCallingConv());
|
2008-01-06 18:27:01 +00:00
|
|
|
cast<CallInst>(NC)->setParamAttrs(NewCallerPAL);
|
2003-06-19 17:00:31 +00:00
|
|
|
}
|
|
|
|
|
2007-02-11 01:23:03 +00:00
|
|
|
// Insert a cast of the return type as necessary.
|
2003-06-19 17:00:31 +00:00
|
|
|
Value *NV = NC;
|
2008-01-06 10:12:28 +00:00
|
|
|
if (OldRetTy != NV->getType() && !Caller->use_empty()) {
|
2003-06-19 17:00:31 +00:00
|
|
|
if (NV->getType() != Type::VoidTy) {
|
2006-12-31 05:48:39 +00:00
|
|
|
Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false,
|
2008-01-06 10:12:28 +00:00
|
|
|
OldRetTy, false);
|
|
|
|
NV = NC = CastInst::create(opcode, NC, OldRetTy, "tmp");
|
2003-10-30 00:46:41 +00:00
|
|
|
|
|
|
|
// If this is an invoke instruction, we should insert it after the first
|
|
|
|
// non-phi, instruction in the normal successor block.
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
|
|
|
|
BasicBlock::iterator I = II->getNormalDest()->begin();
|
|
|
|
while (isa<PHINode>(I)) ++I;
|
|
|
|
InsertNewInstBefore(NC, *I);
|
|
|
|
} else {
|
|
|
|
// Otherwise, it's a call, just insert cast right after the call instr
|
|
|
|
InsertNewInstBefore(NC, *Caller);
|
|
|
|
}
|
2004-02-28 05:22:00 +00:00
|
|
|
AddUsersToWorkList(*Caller);
|
2003-06-19 17:00:31 +00:00
|
|
|
} else {
|
2004-10-17 21:22:38 +00:00
|
|
|
NV = UndefValue::get(Caller->getType());
|
2003-06-19 17:00:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Caller->getType() != Type::VoidTy && !Caller->use_empty())
|
|
|
|
Caller->replaceAllUsesWith(NV);
|
2007-03-02 19:59:19 +00:00
|
|
|
Caller->eraseFromParent();
|
2007-03-02 21:28:56 +00:00
|
|
|
RemoveFromWorkList(Caller);
|
2003-06-19 17:00:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-09-17 10:26:40 +00:00
|
|
|
// transformCallThroughTrampoline - Turn a call to a function created by the
|
|
|
|
// init_trampoline intrinsic into a direct call to the underlying function.
|
|
|
|
//
|
|
|
|
Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) {
|
|
|
|
Value *Callee = CS.getCalledValue();
|
|
|
|
const PointerType *PTy = cast<PointerType>(Callee->getType());
|
|
|
|
const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
|
2008-03-12 17:45:29 +00:00
|
|
|
const PAListPtr &Attrs = CS.getParamAttrs();
|
2008-01-14 19:52:09 +00:00
|
|
|
|
|
|
|
// If the call already has the 'nest' attribute somewhere then give up -
|
|
|
|
// otherwise 'nest' would occur twice after splicing in the chain.
|
2008-03-12 17:45:29 +00:00
|
|
|
if (Attrs.hasAttrSomewhere(ParamAttr::Nest))
|
2008-01-14 19:52:09 +00:00
|
|
|
return 0;
|
2007-09-17 10:26:40 +00:00
|
|
|
|
|
|
|
IntrinsicInst *Tramp =
|
|
|
|
cast<IntrinsicInst>(cast<BitCastInst>(Callee)->getOperand(0));
|
|
|
|
|
|
|
|
Function *NestF =
|
|
|
|
cast<Function>(IntrinsicInst::StripPointerCasts(Tramp->getOperand(2)));
|
|
|
|
const PointerType *NestFPTy = cast<PointerType>(NestF->getType());
|
|
|
|
const FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType());
|
|
|
|
|
2008-03-12 17:45:29 +00:00
|
|
|
const PAListPtr &NestAttrs = NestF->getParamAttrs();
|
|
|
|
if (!NestAttrs.isEmpty()) {
|
2007-09-17 10:26:40 +00:00
|
|
|
unsigned NestIdx = 1;
|
|
|
|
const Type *NestTy = 0;
|
2008-02-19 21:38:47 +00:00
|
|
|
ParameterAttributes NestAttr = ParamAttr::None;
|
2007-09-17 10:26:40 +00:00
|
|
|
|
|
|
|
// Look for a parameter marked with the 'nest' attribute.
|
|
|
|
for (FunctionType::param_iterator I = NestFTy->param_begin(),
|
|
|
|
E = NestFTy->param_end(); I != E; ++NestIdx, ++I)
|
2008-03-12 17:45:29 +00:00
|
|
|
if (NestAttrs.paramHasAttr(NestIdx, ParamAttr::Nest)) {
|
2007-09-17 10:26:40 +00:00
|
|
|
// Record the parameter type and any other attributes.
|
|
|
|
NestTy = *I;
|
2008-03-12 17:45:29 +00:00
|
|
|
NestAttr = NestAttrs.getParamAttrs(NestIdx);
|
2007-09-17 10:26:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NestTy) {
|
|
|
|
Instruction *Caller = CS.getInstruction();
|
|
|
|
std::vector<Value*> NewArgs;
|
|
|
|
NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1);
|
|
|
|
|
2008-03-12 17:45:29 +00:00
|
|
|
SmallVector<ParamAttrsWithIndex, 8> NewAttrs;
|
|
|
|
NewAttrs.reserve(Attrs.getNumSlots() + 1);
|
2008-01-14 19:52:09 +00:00
|
|
|
|
2007-09-17 10:26:40 +00:00
|
|
|
// Insert the nest argument into the call argument list, which may
|
2008-01-14 19:52:09 +00:00
|
|
|
// mean appending it. Likewise for attributes.
|
|
|
|
|
|
|
|
// Add any function result attributes.
|
2008-03-12 17:45:29 +00:00
|
|
|
if (ParameterAttributes Attr = Attrs.getParamAttrs(0))
|
|
|
|
NewAttrs.push_back(ParamAttrsWithIndex::get(0, Attr));
|
2008-01-14 19:52:09 +00:00
|
|
|
|
2007-09-17 10:26:40 +00:00
|
|
|
{
|
|
|
|
unsigned Idx = 1;
|
|
|
|
CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
|
|
|
|
do {
|
|
|
|
if (Idx == NestIdx) {
|
2008-01-14 19:52:09 +00:00
|
|
|
// Add the chain argument and attributes.
|
2007-09-17 10:26:40 +00:00
|
|
|
Value *NestVal = Tramp->getOperand(3);
|
|
|
|
if (NestVal->getType() != NestTy)
|
|
|
|
NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller);
|
|
|
|
NewArgs.push_back(NestVal);
|
2008-01-14 19:52:09 +00:00
|
|
|
NewAttrs.push_back(ParamAttrsWithIndex::get(NestIdx, NestAttr));
|
2007-09-17 10:26:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (I == E)
|
|
|
|
break;
|
|
|
|
|
2008-01-14 19:52:09 +00:00
|
|
|
// Add the original argument and attributes.
|
2007-09-17 10:26:40 +00:00
|
|
|
NewArgs.push_back(*I);
|
2008-03-12 17:45:29 +00:00
|
|
|
if (ParameterAttributes Attr = Attrs.getParamAttrs(Idx))
|
2008-01-14 19:52:09 +00:00
|
|
|
NewAttrs.push_back
|
|
|
|
(ParamAttrsWithIndex::get(Idx + (Idx >= NestIdx), Attr));
|
2007-09-17 10:26:40 +00:00
|
|
|
|
|
|
|
++Idx, ++I;
|
|
|
|
} while (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The trampoline may have been bitcast to a bogus type (FTy).
|
|
|
|
// Handle this by synthesizing a new function type, equal to FTy
|
2008-01-14 19:52:09 +00:00
|
|
|
// with the chain parameter inserted.
|
2007-09-17 10:26:40 +00:00
|
|
|
|
|
|
|
std::vector<const Type*> NewTypes;
|
|
|
|
NewTypes.reserve(FTy->getNumParams()+1);
|
|
|
|
|
|
|
|
// Insert the chain's type into the list of parameter types, which may
|
2008-01-14 19:52:09 +00:00
|
|
|
// mean appending it.
|
2007-09-17 10:26:40 +00:00
|
|
|
{
|
|
|
|
unsigned Idx = 1;
|
|
|
|
FunctionType::param_iterator I = FTy->param_begin(),
|
|
|
|
E = FTy->param_end();
|
|
|
|
|
|
|
|
do {
|
2008-01-14 19:52:09 +00:00
|
|
|
if (Idx == NestIdx)
|
|
|
|
// Add the chain's type.
|
2007-09-17 10:26:40 +00:00
|
|
|
NewTypes.push_back(NestTy);
|
|
|
|
|
|
|
|
if (I == E)
|
|
|
|
break;
|
|
|
|
|
2008-01-14 19:52:09 +00:00
|
|
|
// Add the original type.
|
2007-09-17 10:26:40 +00:00
|
|
|
NewTypes.push_back(*I);
|
|
|
|
|
|
|
|
++Idx, ++I;
|
|
|
|
} while (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace the trampoline call with a direct call. Let the generic
|
|
|
|
// code sort out any function type mismatches.
|
|
|
|
FunctionType *NewFTy =
|
2007-11-27 13:23:08 +00:00
|
|
|
FunctionType::get(FTy->getReturnType(), NewTypes, FTy->isVarArg());
|
2007-12-17 01:12:55 +00:00
|
|
|
Constant *NewCallee = NestF->getType() == PointerType::getUnqual(NewFTy) ?
|
|
|
|
NestF : ConstantExpr::getBitCast(NestF, PointerType::getUnqual(NewFTy));
|
2008-03-12 17:45:29 +00:00
|
|
|
const PAListPtr &NewPAL = PAListPtr::get(NewAttrs.begin(),NewAttrs.end());
|
2007-09-17 10:26:40 +00:00
|
|
|
|
|
|
|
Instruction *NewCaller;
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
|
2008-04-06 20:25:17 +00:00
|
|
|
NewCaller = InvokeInst::Create(NewCallee,
|
|
|
|
II->getNormalDest(), II->getUnwindDest(),
|
|
|
|
NewArgs.begin(), NewArgs.end(),
|
|
|
|
Caller->getName(), Caller);
|
2007-09-17 10:26:40 +00:00
|
|
|
cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv());
|
2007-11-27 13:23:08 +00:00
|
|
|
cast<InvokeInst>(NewCaller)->setParamAttrs(NewPAL);
|
2007-09-17 10:26:40 +00:00
|
|
|
} else {
|
2008-04-06 20:25:17 +00:00
|
|
|
NewCaller = CallInst::Create(NewCallee, NewArgs.begin(), NewArgs.end(),
|
|
|
|
Caller->getName(), Caller);
|
2007-09-17 10:26:40 +00:00
|
|
|
if (cast<CallInst>(Caller)->isTailCall())
|
|
|
|
cast<CallInst>(NewCaller)->setTailCall();
|
|
|
|
cast<CallInst>(NewCaller)->
|
|
|
|
setCallingConv(cast<CallInst>(Caller)->getCallingConv());
|
2007-11-27 13:23:08 +00:00
|
|
|
cast<CallInst>(NewCaller)->setParamAttrs(NewPAL);
|
2007-09-17 10:26:40 +00:00
|
|
|
}
|
|
|
|
if (Caller->getType() != Type::VoidTy && !Caller->use_empty())
|
|
|
|
Caller->replaceAllUsesWith(NewCaller);
|
|
|
|
Caller->eraseFromParent();
|
|
|
|
RemoveFromWorkList(Caller);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace the trampoline call with a direct call. Since there is no 'nest'
|
|
|
|
// parameter, there is no need to adjust the argument list. Let the generic
|
|
|
|
// code sort out any function type mismatches.
|
|
|
|
Constant *NewCallee =
|
|
|
|
NestF->getType() == PTy ? NestF : ConstantExpr::getBitCast(NestF, PTy);
|
|
|
|
CS.setCalledFunction(NewCallee);
|
|
|
|
return CS.getInstruction();
|
|
|
|
}
|
|
|
|
|
2006-11-01 04:51:18 +00:00
|
|
|
/// FoldPHIArgBinOpIntoPHI - If we have something like phi [add (a,b), add(c,d)]
|
|
|
|
/// and if a/b/c/d and the add's all have a single use, turn this into two phi's
|
|
|
|
/// and a single binop.
|
|
|
|
Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI(PHINode &PN) {
|
|
|
|
Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0));
|
2007-02-02 02:16:23 +00:00
|
|
|
assert(isa<BinaryOperator>(FirstInst) || isa<GetElementPtrInst>(FirstInst) ||
|
|
|
|
isa<CmpInst>(FirstInst));
|
2006-11-01 04:51:18 +00:00
|
|
|
unsigned Opc = FirstInst->getOpcode();
|
2006-11-08 19:29:23 +00:00
|
|
|
Value *LHSVal = FirstInst->getOperand(0);
|
|
|
|
Value *RHSVal = FirstInst->getOperand(1);
|
|
|
|
|
|
|
|
const Type *LHSType = LHSVal->getType();
|
|
|
|
const Type *RHSType = RHSVal->getType();
|
2006-11-01 04:51:18 +00:00
|
|
|
|
|
|
|
// Scan to see if all operands are the same opcode, all have one use, and all
|
|
|
|
// kill their operands (i.e. the operands have one use).
|
2006-11-01 04:55:47 +00:00
|
|
|
for (unsigned i = 0; i != PN.getNumIncomingValues(); ++i) {
|
2006-11-01 04:51:18 +00:00
|
|
|
Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i));
|
2006-11-01 04:55:47 +00:00
|
|
|
if (!I || I->getOpcode() != Opc || !I->hasOneUse() ||
|
2006-12-23 06:05:41 +00:00
|
|
|
// Verify type of the LHS matches so we don't fold cmp's of different
|
2006-11-01 07:43:41 +00:00
|
|
|
// types or GEP's with different index types.
|
|
|
|
I->getOperand(0)->getType() != LHSType ||
|
|
|
|
I->getOperand(1)->getType() != RHSType)
|
2006-11-01 04:51:18 +00:00
|
|
|
return 0;
|
2006-12-23 06:05:41 +00:00
|
|
|
|
|
|
|
// If they are CmpInst instructions, check their predicates
|
|
|
|
if (Opc == Instruction::ICmp || Opc == Instruction::FCmp)
|
|
|
|
if (cast<CmpInst>(I)->getPredicate() !=
|
|
|
|
cast<CmpInst>(FirstInst)->getPredicate())
|
|
|
|
return 0;
|
2006-11-08 19:29:23 +00:00
|
|
|
|
|
|
|
// Keep track of which operand needs a phi node.
|
|
|
|
if (I->getOperand(0) != LHSVal) LHSVal = 0;
|
|
|
|
if (I->getOperand(1) != RHSVal) RHSVal = 0;
|
2006-11-01 04:51:18 +00:00
|
|
|
}
|
|
|
|
|
2006-11-08 19:42:28 +00:00
|
|
|
// Otherwise, this is safe to transform, determine if it is profitable.
|
|
|
|
|
|
|
|
// If this is a GEP, and if the index (not the pointer) needs a PHI, bail out.
|
|
|
|
// Indexes are often folded into load/store instructions, so we don't want to
|
|
|
|
// hide them behind a phi.
|
|
|
|
if (isa<GetElementPtrInst>(FirstInst) && RHSVal == 0)
|
|
|
|
return 0;
|
|
|
|
|
2006-11-01 04:51:18 +00:00
|
|
|
Value *InLHS = FirstInst->getOperand(0);
|
|
|
|
Value *InRHS = FirstInst->getOperand(1);
|
2006-11-08 19:42:28 +00:00
|
|
|
PHINode *NewLHS = 0, *NewRHS = 0;
|
2006-11-08 19:29:23 +00:00
|
|
|
if (LHSVal == 0) {
|
2008-04-06 20:25:17 +00:00
|
|
|
NewLHS = PHINode::Create(LHSType, FirstInst->getOperand(0)->getName()+".pn");
|
2006-11-08 19:29:23 +00:00
|
|
|
NewLHS->reserveOperandSpace(PN.getNumOperands()/2);
|
|
|
|
NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0));
|
2006-11-01 07:43:41 +00:00
|
|
|
InsertNewInstBefore(NewLHS, PN);
|
|
|
|
LHSVal = NewLHS;
|
|
|
|
}
|
2006-11-08 19:29:23 +00:00
|
|
|
|
|
|
|
if (RHSVal == 0) {
|
2008-04-06 20:25:17 +00:00
|
|
|
NewRHS = PHINode::Create(RHSType, FirstInst->getOperand(1)->getName()+".pn");
|
2006-11-08 19:29:23 +00:00
|
|
|
NewRHS->reserveOperandSpace(PN.getNumOperands()/2);
|
|
|
|
NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0));
|
2006-11-01 07:43:41 +00:00
|
|
|
InsertNewInstBefore(NewRHS, PN);
|
|
|
|
RHSVal = NewRHS;
|
|
|
|
}
|
|
|
|
|
2006-11-08 19:29:23 +00:00
|
|
|
// Add all operands to the new PHIs.
|
|
|
|
for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
|
|
|
|
if (NewLHS) {
|
|
|
|
Value *NewInLHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(0);
|
|
|
|
NewLHS->addIncoming(NewInLHS, PN.getIncomingBlock(i));
|
|
|
|
}
|
|
|
|
if (NewRHS) {
|
|
|
|
Value *NewInRHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(1);
|
|
|
|
NewRHS->addIncoming(NewInRHS, PN.getIncomingBlock(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-01 04:51:18 +00:00
|
|
|
if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst))
|
2006-11-01 07:43:41 +00:00
|
|
|
return BinaryOperator::create(BinOp->getOpcode(), LHSVal, RHSVal);
|
2006-12-23 06:05:41 +00:00
|
|
|
else if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst))
|
|
|
|
return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(), LHSVal,
|
|
|
|
RHSVal);
|
2006-11-01 07:43:41 +00:00
|
|
|
else {
|
|
|
|
assert(isa<GetElementPtrInst>(FirstInst));
|
2008-04-06 20:25:17 +00:00
|
|
|
return GetElementPtrInst::Create(LHSVal, RHSVal);
|
2006-11-01 07:43:41 +00:00
|
|
|
}
|
2006-11-01 04:51:18 +00:00
|
|
|
}
|
|
|
|
|
2006-11-01 07:13:54 +00:00
|
|
|
/// isSafeToSinkLoad - Return true if we know that it is safe sink the load out
|
|
|
|
/// of the block that defines it. This means that it must be obvious the value
|
|
|
|
/// of the load is not changed from the point of the load to the end of the
|
|
|
|
/// block it is in.
|
2007-02-01 22:30:07 +00:00
|
|
|
///
|
|
|
|
/// Finally, it is safe, but not profitable, to sink a load targetting a
|
|
|
|
/// non-address-taken alloca. Doing so will cause us to not promote the alloca
|
|
|
|
/// to a register.
|
2006-11-01 07:13:54 +00:00
|
|
|
static bool isSafeToSinkLoad(LoadInst *L) {
|
|
|
|
BasicBlock::iterator BBI = L, E = L->getParent()->end();
|
|
|
|
|
|
|
|
for (++BBI; BBI != E; ++BBI)
|
|
|
|
if (BBI->mayWriteToMemory())
|
|
|
|
return false;
|
2007-02-01 22:30:07 +00:00
|
|
|
|
|
|
|
// Check for non-address taken alloca. If not address-taken already, it isn't
|
|
|
|
// profitable to do this xform.
|
|
|
|
if (AllocaInst *AI = dyn_cast<AllocaInst>(L->getOperand(0))) {
|
|
|
|
bool isAddressTaken = false;
|
|
|
|
for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end();
|
|
|
|
UI != E; ++UI) {
|
|
|
|
if (isa<LoadInst>(UI)) continue;
|
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
|
|
|
|
// If storing TO the alloca, then the address isn't taken.
|
|
|
|
if (SI->getOperand(1) == AI) continue;
|
|
|
|
}
|
|
|
|
isAddressTaken = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isAddressTaken)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-11-01 07:13:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2003-06-19 17:00:31 +00:00
|
|
|
|
2004-11-14 19:13:23 +00:00
|
|
|
// FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary"
|
|
|
|
// operator and they all are only used by the PHI, PHI together their
|
|
|
|
// inputs, and do the operation once, to the result of the PHI.
|
|
|
|
Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) {
|
|
|
|
Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0));
|
|
|
|
|
|
|
|
// Scan the instruction, looking for input operations that can be folded away.
|
|
|
|
// If all input operands to the phi are the same instruction (e.g. a cast from
|
|
|
|
// the same type or "+42") we can pull the operation through the PHI, reducing
|
|
|
|
// code size and simplifying code.
|
|
|
|
Constant *ConstantOp = 0;
|
|
|
|
const Type *CastSrcTy = 0;
|
2006-11-01 07:13:54 +00:00
|
|
|
bool isVolatile = false;
|
2004-11-14 19:13:23 +00:00
|
|
|
if (isa<CastInst>(FirstInst)) {
|
|
|
|
CastSrcTy = FirstInst->getOperand(0)->getType();
|
2007-02-02 02:16:23 +00:00
|
|
|
} else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) {
|
2006-12-23 06:05:41 +00:00
|
|
|
// Can fold binop, compare or shift here if the RHS is a constant,
|
|
|
|
// otherwise call FoldPHIArgBinOpIntoPHI.
|
2004-11-14 19:13:23 +00:00
|
|
|
ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1));
|
2006-11-01 04:51:18 +00:00
|
|
|
if (ConstantOp == 0)
|
|
|
|
return FoldPHIArgBinOpIntoPHI(PN);
|
2006-11-01 07:13:54 +00:00
|
|
|
} else if (LoadInst *LI = dyn_cast<LoadInst>(FirstInst)) {
|
|
|
|
isVolatile = LI->isVolatile();
|
|
|
|
// We can't sink the load if the loaded value could be modified between the
|
|
|
|
// load and the PHI.
|
|
|
|
if (LI->getParent() != PN.getIncomingBlock(0) ||
|
|
|
|
!isSafeToSinkLoad(LI))
|
|
|
|
return 0;
|
2006-11-01 07:43:41 +00:00
|
|
|
} else if (isa<GetElementPtrInst>(FirstInst)) {
|
2006-11-08 19:42:28 +00:00
|
|
|
if (FirstInst->getNumOperands() == 2)
|
2006-11-01 07:43:41 +00:00
|
|
|
return FoldPHIArgBinOpIntoPHI(PN);
|
|
|
|
// Can't handle general GEPs yet.
|
|
|
|
return 0;
|
2004-11-14 19:13:23 +00:00
|
|
|
} else {
|
|
|
|
return 0; // Cannot fold this operation.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if all arguments are the same operation.
|
|
|
|
for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
|
|
|
|
if (!isa<Instruction>(PN.getIncomingValue(i))) return 0;
|
|
|
|
Instruction *I = cast<Instruction>(PN.getIncomingValue(i));
|
2006-12-23 06:05:41 +00:00
|
|
|
if (!I->hasOneUse() || !I->isSameOperationAs(FirstInst))
|
2004-11-14 19:13:23 +00:00
|
|
|
return 0;
|
|
|
|
if (CastSrcTy) {
|
|
|
|
if (I->getOperand(0)->getType() != CastSrcTy)
|
|
|
|
return 0; // Cast operation must match.
|
2006-11-01 07:13:54 +00:00
|
|
|
} else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
|
2006-12-23 06:05:41 +00:00
|
|
|
// We can't sink the load if the loaded value could be modified between
|
|
|
|
// the load and the PHI.
|
2006-11-01 07:13:54 +00:00
|
|
|
if (LI->isVolatile() != isVolatile ||
|
|
|
|
LI->getParent() != PN.getIncomingBlock(i) ||
|
|
|
|
!isSafeToSinkLoad(LI))
|
|
|
|
return 0;
|
2008-04-29 17:28:22 +00:00
|
|
|
|
|
|
|
// If the PHI is volatile and its block has multiple successors, sinking
|
|
|
|
// it would remove a load of the volatile value from the path through the
|
|
|
|
// other successor.
|
|
|
|
if (isVolatile &&
|
|
|
|
LI->getParent()->getTerminator()->getNumSuccessors() != 1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
2004-11-14 19:13:23 +00:00
|
|
|
} else if (I->getOperand(1) != ConstantOp) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Okay, they are all the same operation. Create a new PHI node of the
|
|
|
|
// correct type, and PHI together all of the LHS's of the instructions.
|
2008-04-06 20:25:17 +00:00
|
|
|
PHINode *NewPN = PHINode::Create(FirstInst->getOperand(0)->getType(),
|
|
|
|
PN.getName()+".in");
|
2005-01-29 00:39:08 +00:00
|
|
|
NewPN->reserveOperandSpace(PN.getNumOperands()/2);
|
2004-11-14 19:29:34 +00:00
|
|
|
|
|
|
|
Value *InVal = FirstInst->getOperand(0);
|
|
|
|
NewPN->addIncoming(InVal, PN.getIncomingBlock(0));
|
2004-11-14 19:13:23 +00:00
|
|
|
|
|
|
|
// Add all operands to the new PHI.
|
2004-11-14 19:29:34 +00:00
|
|
|
for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
|
|
|
|
Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0);
|
|
|
|
if (NewInVal != InVal)
|
|
|
|
InVal = 0;
|
|
|
|
NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *PhiVal;
|
|
|
|
if (InVal) {
|
|
|
|
// The new PHI unions all of the same values together. This is really
|
|
|
|
// common, so we handle it intelligently here for compile-time speed.
|
|
|
|
PhiVal = InVal;
|
|
|
|
delete NewPN;
|
|
|
|
} else {
|
|
|
|
InsertNewInstBefore(NewPN, PN);
|
|
|
|
PhiVal = NewPN;
|
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-11-14 19:13:23 +00:00
|
|
|
// Insert and return the new operation.
|
2006-11-27 01:05:10 +00:00
|
|
|
if (CastInst* FirstCI = dyn_cast<CastInst>(FirstInst))
|
|
|
|
return CastInst::create(FirstCI->getOpcode(), PhiVal, PN.getType());
|
2008-04-29 17:13:43 +00:00
|
|
|
if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst))
|
2004-11-14 19:29:34 +00:00
|
|
|
return BinaryOperator::create(BinOp->getOpcode(), PhiVal, ConstantOp);
|
2008-04-29 17:13:43 +00:00
|
|
|
if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst))
|
2006-12-23 06:05:41 +00:00
|
|
|
return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(),
|
|
|
|
PhiVal, ConstantOp);
|
2008-04-29 17:13:43 +00:00
|
|
|
assert(isa<LoadInst>(FirstInst) && "Unknown operation");
|
|
|
|
|
|
|
|
// If this was a volatile load that we are merging, make sure to loop through
|
|
|
|
// and mark all the input loads as non-volatile. If we don't do this, we will
|
|
|
|
// insert a new volatile load and the old ones will not be deletable.
|
|
|
|
if (isVolatile)
|
|
|
|
for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
|
|
|
|
cast<LoadInst>(PN.getIncomingValue(i))->setVolatile(false);
|
|
|
|
|
|
|
|
return new LoadInst(PhiVal, "", isVolatile);
|
2004-11-14 19:13:23 +00:00
|
|
|
}
|
2002-05-02 17:06:02 +00:00
|
|
|
|
2005-01-17 05:10:15 +00:00
|
|
|
/// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle
|
|
|
|
/// that is dead.
|
2007-03-26 20:40:50 +00:00
|
|
|
static bool DeadPHICycle(PHINode *PN,
|
|
|
|
SmallPtrSet<PHINode*, 16> &PotentiallyDeadPHIs) {
|
2005-01-17 05:10:15 +00:00
|
|
|
if (PN->use_empty()) return true;
|
|
|
|
if (!PN->hasOneUse()) return false;
|
|
|
|
|
|
|
|
// Remember this node, and if we find the cycle, return.
|
2007-03-26 20:40:50 +00:00
|
|
|
if (!PotentiallyDeadPHIs.insert(PN))
|
2005-01-17 05:10:15 +00:00
|
|
|
return true;
|
2007-08-28 04:23:55 +00:00
|
|
|
|
|
|
|
// Don't scan crazily complex things.
|
|
|
|
if (PotentiallyDeadPHIs.size() == 16)
|
|
|
|
return false;
|
2005-01-17 05:10:15 +00:00
|
|
|
|
|
|
|
if (PHINode *PU = dyn_cast<PHINode>(PN->use_back()))
|
|
|
|
return DeadPHICycle(PU, PotentiallyDeadPHIs);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2005-01-17 05:10:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-11-06 21:52:06 +00:00
|
|
|
/// PHIsEqualValue - Return true if this phi node is always equal to
|
|
|
|
/// NonPhiInVal. This happens with mutually cyclic phi nodes like:
|
|
|
|
/// z = some value; x = phi (y, z); y = phi (x, z)
|
|
|
|
static bool PHIsEqualValue(PHINode *PN, Value *NonPhiInVal,
|
|
|
|
SmallPtrSet<PHINode*, 16> &ValueEqualPHIs) {
|
|
|
|
// See if we already saw this PHI node.
|
|
|
|
if (!ValueEqualPHIs.insert(PN))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Don't scan crazily complex things.
|
|
|
|
if (ValueEqualPHIs.size() == 16)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Scan the operands to see if they are either phi nodes or are equal to
|
|
|
|
// the value.
|
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
|
|
|
|
Value *Op = PN->getIncomingValue(i);
|
|
|
|
if (PHINode *OpPN = dyn_cast<PHINode>(Op)) {
|
|
|
|
if (!PHIsEqualValue(OpPN, NonPhiInVal, ValueEqualPHIs))
|
|
|
|
return false;
|
|
|
|
} else if (Op != NonPhiInVal)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-06 18:06:38 +00:00
|
|
|
// PHINode simplification
|
|
|
|
//
|
2002-06-25 16:13:24 +00:00
|
|
|
Instruction *InstCombiner::visitPHINode(PHINode &PN) {
|
2006-07-10 22:15:25 +00:00
|
|
|
// If LCSSA is around, don't mess with Phi nodes
|
2007-03-04 04:27:24 +00:00
|
|
|
if (MustPreserveLCSSA) return 0;
|
2006-07-10 22:03:18 +00:00
|
|
|
|
|
|
|
if (Value *V = PN.hasConstantValue())
|
|
|
|
return ReplaceInstUsesWith(PN, V);
|
|
|
|
|
|
|
|
// If all PHI operands are the same operation, pull them through the PHI,
|
|
|
|
// reducing code size.
|
|
|
|
if (isa<Instruction>(PN.getIncomingValue(0)) &&
|
|
|
|
PN.getIncomingValue(0)->hasOneUse())
|
|
|
|
if (Instruction *Result = FoldPHIArgOpIntoPHI(PN))
|
|
|
|
return Result;
|
|
|
|
|
|
|
|
// If this is a trivial cycle in the PHI node graph, remove it. Basically, if
|
|
|
|
// this PHI only has a single use (a PHI), and if that PHI only has one use (a
|
|
|
|
// PHI)... break the cycle.
|
2007-01-15 07:30:06 +00:00
|
|
|
if (PN.hasOneUse()) {
|
|
|
|
Instruction *PHIUser = cast<Instruction>(PN.use_back());
|
|
|
|
if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) {
|
2007-03-26 20:40:50 +00:00
|
|
|
SmallPtrSet<PHINode*, 16> PotentiallyDeadPHIs;
|
2006-07-10 22:03:18 +00:00
|
|
|
PotentiallyDeadPHIs.insert(&PN);
|
|
|
|
if (DeadPHICycle(PU, PotentiallyDeadPHIs))
|
|
|
|
return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType()));
|
|
|
|
}
|
2007-01-15 07:30:06 +00:00
|
|
|
|
|
|
|
// If this phi has a single use, and if that use just computes a value for
|
|
|
|
// the next iteration of a loop, delete the phi. This occurs with unused
|
|
|
|
// induction variables, e.g. "for (int j = 0; ; ++j);". Detecting this
|
|
|
|
// common case here is good because the only other things that catch this
|
|
|
|
// are induction variable analysis (sometimes) and ADCE, which is only run
|
|
|
|
// late.
|
|
|
|
if (PHIUser->hasOneUse() &&
|
|
|
|
(isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) &&
|
|
|
|
PHIUser->use_back() == &PN) {
|
|
|
|
return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType()));
|
|
|
|
}
|
|
|
|
}
|
2006-07-10 22:03:18 +00:00
|
|
|
|
2007-11-06 21:52:06 +00:00
|
|
|
// We sometimes end up with phi cycles that non-obviously end up being the
|
|
|
|
// same value, for example:
|
|
|
|
// z = some value; x = phi (y, z); y = phi (x, z)
|
|
|
|
// where the phi nodes don't necessarily need to be in the same block. Do a
|
|
|
|
// quick check to see if the PHI node only contains a single non-phi value, if
|
|
|
|
// so, scan to see if the phi cycle is actually equal to that value.
|
|
|
|
{
|
|
|
|
unsigned InValNo = 0, NumOperandVals = PN.getNumIncomingValues();
|
|
|
|
// Scan for the first non-phi operand.
|
|
|
|
while (InValNo != NumOperandVals &&
|
|
|
|
isa<PHINode>(PN.getIncomingValue(InValNo)))
|
|
|
|
++InValNo;
|
|
|
|
|
|
|
|
if (InValNo != NumOperandVals) {
|
|
|
|
Value *NonPhiInVal = PN.getOperand(InValNo);
|
|
|
|
|
|
|
|
// Scan the rest of the operands to see if there are any conflicts, if so
|
|
|
|
// there is no need to recursively scan other phis.
|
|
|
|
for (++InValNo; InValNo != NumOperandVals; ++InValNo) {
|
|
|
|
Value *OpVal = PN.getIncomingValue(InValNo);
|
|
|
|
if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we scanned over all operands, then we have one unique value plus
|
|
|
|
// phi values. Scan PHI nodes to see if they all merge in each other or
|
|
|
|
// the value.
|
|
|
|
if (InValNo == NumOperandVals) {
|
|
|
|
SmallPtrSet<PHINode*, 16> ValueEqualPHIs;
|
|
|
|
if (PHIsEqualValue(&PN, NonPhiInVal, ValueEqualPHIs))
|
|
|
|
return ReplaceInstUsesWith(PN, NonPhiInVal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-12-19 05:58:40 +00:00
|
|
|
return 0;
|
2002-05-06 18:06:38 +00:00
|
|
|
}
|
|
|
|
|
2006-12-12 09:18:51 +00:00
|
|
|
static Value *InsertCastToIntPtrTy(Value *V, const Type *DTy,
|
|
|
|
Instruction *InsertPoint,
|
|
|
|
InstCombiner *IC) {
|
2007-01-08 16:32:00 +00:00
|
|
|
unsigned PtrSize = DTy->getPrimitiveSizeInBits();
|
|
|
|
unsigned VTySize = V->getType()->getPrimitiveSizeInBits();
|
2006-12-12 09:18:51 +00:00
|
|
|
// We must cast correctly to the pointer type. Ensure that we
|
|
|
|
// sign extend the integer value if it is smaller as this is
|
|
|
|
// used for address computation.
|
|
|
|
Instruction::CastOps opcode =
|
|
|
|
(VTySize < PtrSize ? Instruction::SExt :
|
|
|
|
(VTySize == PtrSize ? Instruction::BitCast : Instruction::Trunc));
|
|
|
|
return IC->InsertCastBefore(opcode, V, DTy, *InsertPoint);
|
2004-04-05 01:30:19 +00:00
|
|
|
}
|
|
|
|
|
2002-05-02 17:06:02 +00:00
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
|
2004-05-07 22:09:22 +00:00
|
|
|
Value *PtrOp = GEP.getOperand(0);
|
2007-04-28 00:57:34 +00:00
|
|
|
// Is it 'getelementptr %P, i32 0' or 'getelementptr %P'
|
2002-06-25 16:13:24 +00:00
|
|
|
// If so, eliminate the noop.
|
2004-02-22 05:25:17 +00:00
|
|
|
if (GEP.getNumOperands() == 1)
|
2004-05-07 22:09:22 +00:00
|
|
|
return ReplaceInstUsesWith(GEP, PtrOp);
|
2004-02-22 05:25:17 +00:00
|
|
|
|
2004-10-16 18:11:37 +00:00
|
|
|
if (isa<UndefValue>(GEP.getOperand(0)))
|
|
|
|
return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType()));
|
|
|
|
|
2004-02-22 05:25:17 +00:00
|
|
|
bool HasZeroPointerIndex = false;
|
|
|
|
if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1)))
|
|
|
|
HasZeroPointerIndex = C->isNullValue();
|
|
|
|
|
|
|
|
if (GEP.getNumOperands() == 2 && HasZeroPointerIndex)
|
2004-05-07 22:09:22 +00:00
|
|
|
return ReplaceInstUsesWith(GEP, PtrOp);
|
2002-05-02 17:06:02 +00:00
|
|
|
|
2004-04-05 01:30:19 +00:00
|
|
|
// Eliminate unneeded casts for indices.
|
|
|
|
bool MadeChange = false;
|
2007-03-25 20:43:09 +00:00
|
|
|
|
2004-04-07 18:38:20 +00:00
|
|
|
gep_type_iterator GTI = gep_type_begin(GEP);
|
2007-03-25 20:43:09 +00:00
|
|
|
for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI) {
|
2004-04-07 18:38:20 +00:00
|
|
|
if (isa<SequentialType>(*GTI)) {
|
|
|
|
if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) {
|
2007-01-15 07:02:54 +00:00
|
|
|
if (CI->getOpcode() == Instruction::ZExt ||
|
|
|
|
CI->getOpcode() == Instruction::SExt) {
|
|
|
|
const Type *SrcTy = CI->getOperand(0)->getType();
|
|
|
|
// We can eliminate a cast from i32 to i64 iff the target
|
|
|
|
// is a 32-bit pointer target.
|
|
|
|
if (SrcTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()) {
|
|
|
|
MadeChange = true;
|
|
|
|
GEP.setOperand(i, CI->getOperand(0));
|
2004-04-05 01:30:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-04-07 18:38:20 +00:00
|
|
|
// If we are using a wider index than needed for this platform, shrink it
|
|
|
|
// to what we need. If the incoming value needs a cast instruction,
|
|
|
|
// insert it. This explicit cast can make subsequent optimizations more
|
|
|
|
// obvious.
|
|
|
|
Value *Op = GEP.getOperand(i);
|
2008-02-20 11:26:25 +00:00
|
|
|
if (TD->getTypeSizeInBits(Op->getType()) > TD->getPointerSizeInBits()) {
|
Fix a HUGE pessimization on X86. The indvars pass was taking this
(familiar) function:
int _strlen(const char *str) {
int len = 0;
while (*str++) len++;
return len;
}
And transforming it to use a ulong induction variable, because the type of
the pointer index was left as a constant long. This is obviously very bad.
The fix is to shrink long constants in getelementptr instructions to intptr_t,
making the indvars pass insert a uint induction variable, which is much more
efficient.
Here's the before code for this function:
int %_strlen(sbyte* %str) {
entry:
%tmp.13 = load sbyte* %str ; <sbyte> [#uses=1]
%tmp.24 = seteq sbyte %tmp.13, 0 ; <bool> [#uses=1]
br bool %tmp.24, label %loopexit, label %no_exit
no_exit: ; preds = %entry, %no_exit
*** %indvar = phi uint [ %indvar.next, %no_exit ], [ 0, %entry ] ; <uint> [#uses=2]
*** %indvar = phi ulong [ %indvar.next, %no_exit ], [ 0, %entry ] ; <ulong> [#uses=2]
%indvar1 = cast ulong %indvar to uint ; <uint> [#uses=1]
%inc.02.sum = add uint %indvar1, 1 ; <uint> [#uses=1]
%inc.0.0 = getelementptr sbyte* %str, uint %inc.02.sum ; <sbyte*> [#uses=1]
%tmp.1 = load sbyte* %inc.0.0 ; <sbyte> [#uses=1]
%tmp.2 = seteq sbyte %tmp.1, 0 ; <bool> [#uses=1]
%indvar.next = add ulong %indvar, 1 ; <ulong> [#uses=1]
%indvar.next = add uint %indvar, 1 ; <uint> [#uses=1]
br bool %tmp.2, label %loopexit.loopexit, label %no_exit
loopexit.loopexit: ; preds = %no_exit
%indvar = cast uint %indvar to int ; <int> [#uses=1]
%inc.1 = add int %indvar, 1 ; <int> [#uses=1]
ret int %inc.1
loopexit: ; preds = %entry
ret int 0
}
Here's the after code:
int %_strlen(sbyte* %str) {
entry:
%inc.02 = getelementptr sbyte* %str, uint 1 ; <sbyte*> [#uses=1]
%tmp.13 = load sbyte* %str ; <sbyte> [#uses=1]
%tmp.24 = seteq sbyte %tmp.13, 0 ; <bool> [#uses=1]
br bool %tmp.24, label %loopexit, label %no_exit
no_exit: ; preds = %entry, %no_exit
*** %indvar = phi uint [ %indvar.next, %no_exit ], [ 0, %entry ] ; <uint> [#uses=3]
%indvar = cast uint %indvar to int ; <int> [#uses=1]
%inc.0.0 = getelementptr sbyte* %inc.02, uint %indvar ; <sbyte*> [#uses=1]
%inc.1 = add int %indvar, 1 ; <int> [#uses=1]
%tmp.1 = load sbyte* %inc.0.0 ; <sbyte> [#uses=1]
%tmp.2 = seteq sbyte %tmp.1, 0 ; <bool> [#uses=1]
%indvar.next = add uint %indvar, 1 ; <uint> [#uses=1]
br bool %tmp.2, label %loopexit, label %no_exit
loopexit: ; preds = %entry, %no_exit
%len.0.1 = phi int [ 0, %entry ], [ %inc.1, %no_exit ] ; <int> [#uses=1]
ret int %len.0.1
}
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@13016 91177308-0d34-0410-b5e6-96231b3b80d8
2004-04-17 18:16:10 +00:00
|
|
|
if (Constant *C = dyn_cast<Constant>(Op)) {
|
2006-12-23 06:05:41 +00:00
|
|
|
GEP.setOperand(i, ConstantExpr::getTrunc(C, TD->getIntPtrType()));
|
Fix a HUGE pessimization on X86. The indvars pass was taking this
(familiar) function:
int _strlen(const char *str) {
int len = 0;
while (*str++) len++;
return len;
}
And transforming it to use a ulong induction variable, because the type of
the pointer index was left as a constant long. This is obviously very bad.
The fix is to shrink long constants in getelementptr instructions to intptr_t,
making the indvars pass insert a uint induction variable, which is much more
efficient.
Here's the before code for this function:
int %_strlen(sbyte* %str) {
entry:
%tmp.13 = load sbyte* %str ; <sbyte> [#uses=1]
%tmp.24 = seteq sbyte %tmp.13, 0 ; <bool> [#uses=1]
br bool %tmp.24, label %loopexit, label %no_exit
no_exit: ; preds = %entry, %no_exit
*** %indvar = phi uint [ %indvar.next, %no_exit ], [ 0, %entry ] ; <uint> [#uses=2]
*** %indvar = phi ulong [ %indvar.next, %no_exit ], [ 0, %entry ] ; <ulong> [#uses=2]
%indvar1 = cast ulong %indvar to uint ; <uint> [#uses=1]
%inc.02.sum = add uint %indvar1, 1 ; <uint> [#uses=1]
%inc.0.0 = getelementptr sbyte* %str, uint %inc.02.sum ; <sbyte*> [#uses=1]
%tmp.1 = load sbyte* %inc.0.0 ; <sbyte> [#uses=1]
%tmp.2 = seteq sbyte %tmp.1, 0 ; <bool> [#uses=1]
%indvar.next = add ulong %indvar, 1 ; <ulong> [#uses=1]
%indvar.next = add uint %indvar, 1 ; <uint> [#uses=1]
br bool %tmp.2, label %loopexit.loopexit, label %no_exit
loopexit.loopexit: ; preds = %no_exit
%indvar = cast uint %indvar to int ; <int> [#uses=1]
%inc.1 = add int %indvar, 1 ; <int> [#uses=1]
ret int %inc.1
loopexit: ; preds = %entry
ret int 0
}
Here's the after code:
int %_strlen(sbyte* %str) {
entry:
%inc.02 = getelementptr sbyte* %str, uint 1 ; <sbyte*> [#uses=1]
%tmp.13 = load sbyte* %str ; <sbyte> [#uses=1]
%tmp.24 = seteq sbyte %tmp.13, 0 ; <bool> [#uses=1]
br bool %tmp.24, label %loopexit, label %no_exit
no_exit: ; preds = %entry, %no_exit
*** %indvar = phi uint [ %indvar.next, %no_exit ], [ 0, %entry ] ; <uint> [#uses=3]
%indvar = cast uint %indvar to int ; <int> [#uses=1]
%inc.0.0 = getelementptr sbyte* %inc.02, uint %indvar ; <sbyte*> [#uses=1]
%inc.1 = add int %indvar, 1 ; <int> [#uses=1]
%tmp.1 = load sbyte* %inc.0.0 ; <sbyte> [#uses=1]
%tmp.2 = seteq sbyte %tmp.1, 0 ; <bool> [#uses=1]
%indvar.next = add uint %indvar, 1 ; <uint> [#uses=1]
br bool %tmp.2, label %loopexit, label %no_exit
loopexit: ; preds = %entry, %no_exit
%len.0.1 = phi int [ 0, %entry ], [ %inc.1, %no_exit ] ; <int> [#uses=1]
ret int %len.0.1
}
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@13016 91177308-0d34-0410-b5e6-96231b3b80d8
2004-04-17 18:16:10 +00:00
|
|
|
MadeChange = true;
|
|
|
|
} else {
|
2006-12-12 09:18:51 +00:00
|
|
|
Op = InsertCastBefore(Instruction::Trunc, Op, TD->getIntPtrType(),
|
|
|
|
GEP);
|
2004-04-07 18:38:20 +00:00
|
|
|
GEP.setOperand(i, Op);
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
2008-02-20 11:26:25 +00:00
|
|
|
}
|
2004-04-05 01:30:19 +00:00
|
|
|
}
|
2007-03-25 20:43:09 +00:00
|
|
|
}
|
2004-04-05 01:30:19 +00:00
|
|
|
if (MadeChange) return &GEP;
|
|
|
|
|
2007-03-25 20:43:09 +00:00
|
|
|
// If this GEP instruction doesn't move the pointer, and if the input operand
|
|
|
|
// is a bitcast of another pointer, just replace the GEP with a bitcast of the
|
|
|
|
// real input to the dest type.
|
2007-10-12 05:30:59 +00:00
|
|
|
if (GEP.hasAllZeroIndices()) {
|
|
|
|
if (BitCastInst *BCI = dyn_cast<BitCastInst>(GEP.getOperand(0))) {
|
|
|
|
// If the bitcast is of an allocation, and the allocation will be
|
|
|
|
// converted to match the type of the cast, don't touch this.
|
|
|
|
if (isa<AllocationInst>(BCI->getOperand(0))) {
|
|
|
|
// See if the bitcast simplifies, if so, don't nuke this GEP yet.
|
2007-10-12 18:05:47 +00:00
|
|
|
if (Instruction *I = visitBitCast(*BCI)) {
|
|
|
|
if (I != BCI) {
|
|
|
|
I->takeName(BCI);
|
|
|
|
BCI->getParent()->getInstList().insert(BCI, I);
|
|
|
|
ReplaceInstUsesWith(*BCI, I);
|
|
|
|
}
|
2007-10-12 05:30:59 +00:00
|
|
|
return &GEP;
|
2007-10-12 18:05:47 +00:00
|
|
|
}
|
2007-10-12 05:30:59 +00:00
|
|
|
}
|
|
|
|
return new BitCastInst(BCI->getOperand(0), GEP.getType());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-02 19:29:35 +00:00
|
|
|
// Combine Indices - If the source pointer to this getelementptr instruction
|
|
|
|
// is a getelementptr instruction, combine the indices of the two
|
|
|
|
// getelementptr instructions into a single instruction.
|
|
|
|
//
|
2007-02-15 22:48:32 +00:00
|
|
|
SmallVector<Value*, 8> SrcGEPOperands;
|
2005-01-13 20:14:25 +00:00
|
|
|
if (User *Src = dyn_castGetElementPtr(PtrOp))
|
2007-02-15 22:48:32 +00:00
|
|
|
SrcGEPOperands.append(Src->op_begin(), Src->op_end());
|
2004-03-25 22:59:29 +00:00
|
|
|
|
|
|
|
if (!SrcGEPOperands.empty()) {
|
2004-05-07 22:09:22 +00:00
|
|
|
// Note that if our source is a gep chain itself that we wait for that
|
|
|
|
// chain to be resolved before we perform this transformation. This
|
|
|
|
// avoids us creating a TON of code in some cases.
|
|
|
|
//
|
|
|
|
if (isa<GetElementPtrInst>(SrcGEPOperands[0]) &&
|
|
|
|
cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2)
|
|
|
|
return 0; // Wait until our source is folded to completion.
|
|
|
|
|
2007-02-15 22:48:32 +00:00
|
|
|
SmallVector<Value*, 8> Indices;
|
2004-05-07 22:09:22 +00:00
|
|
|
|
|
|
|
// Find out whether the last index in the source GEP is a sequential idx.
|
|
|
|
bool EndsWithSequential = false;
|
|
|
|
for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)),
|
|
|
|
E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I)
|
2004-05-08 22:41:42 +00:00
|
|
|
EndsWithSequential = !isa<StructType>(*I);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2002-08-02 19:29:35 +00:00
|
|
|
// Can we combine the two pointer arithmetics offsets?
|
2004-05-07 22:09:22 +00:00
|
|
|
if (EndsWithSequential) {
|
2003-03-05 22:33:14 +00:00
|
|
|
// Replace: gep (gep %P, long B), long A, ...
|
|
|
|
// With: T = long A+B; gep %P, T, ...
|
|
|
|
//
|
2004-05-07 22:09:22 +00:00
|
|
|
Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1);
|
2004-04-05 01:30:19 +00:00
|
|
|
if (SO1 == Constant::getNullValue(SO1->getType())) {
|
|
|
|
Sum = GO1;
|
|
|
|
} else if (GO1 == Constant::getNullValue(GO1->getType())) {
|
|
|
|
Sum = SO1;
|
|
|
|
} else {
|
|
|
|
// If they aren't the same type, convert both to an integer of the
|
|
|
|
// target's pointer size.
|
|
|
|
if (SO1->getType() != GO1->getType()) {
|
|
|
|
if (Constant *SO1C = dyn_cast<Constant>(SO1)) {
|
2006-12-12 09:18:51 +00:00
|
|
|
SO1 = ConstantExpr::getIntegerCast(SO1C, GO1->getType(), true);
|
2004-04-05 01:30:19 +00:00
|
|
|
} else if (Constant *GO1C = dyn_cast<Constant>(GO1)) {
|
2006-12-12 09:18:51 +00:00
|
|
|
GO1 = ConstantExpr::getIntegerCast(GO1C, SO1->getType(), true);
|
2004-04-05 01:30:19 +00:00
|
|
|
} else {
|
Executive summary: getTypeSize -> getTypeStoreSize / getABITypeSize.
The meaning of getTypeSize was not clear - clarifying it is important
now that we have x86 long double and arbitrary precision integers.
The issue with long double is that it requires 80 bits, and this is
not a multiple of its alignment. This gives a primitive type for
which getTypeSize differed from getABITypeSize. For arbitrary precision
integers it is even worse: there is the minimum number of bits needed to
hold the type (eg: 36 for an i36), the maximum number of bits that will
be overwriten when storing the type (40 bits for i36) and the ABI size
(i.e. the storage size rounded up to a multiple of the alignment; 64 bits
for i36).
This patch removes getTypeSize (not really - it is still there but
deprecated to allow for a gradual transition). Instead there is:
(1) getTypeSizeInBits - a number of bits that suffices to hold all
values of the type. For a primitive type, this is the minimum number
of bits. For an i36 this is 36 bits. For x86 long double it is 80.
This corresponds to gcc's TYPE_PRECISION.
(2) getTypeStoreSizeInBits - the maximum number of bits that is
written when storing the type (or read when reading it). For an
i36 this is 40 bits, for an x86 long double it is 80 bits. This
is the size alias analysis is interested in (getTypeStoreSize
returns the number of bytes). There doesn't seem to be anything
corresponding to this in gcc.
(3) getABITypeSizeInBits - this is getTypeStoreSizeInBits rounded
up to a multiple of the alignment. For an i36 this is 64, for an
x86 long double this is 96 or 128 depending on the OS. This is the
spacing between consecutive elements when you form an array out of
this type (getABITypeSize returns the number of bytes). This is
TYPE_SIZE in gcc.
Since successive elements in a SequentialType (arrays, pointers
and vectors) need to be aligned, the spacing between them will be
given by getABITypeSize. This means that the size of an array
is the length times the getABITypeSize. It also means that GEP
computations need to use getABITypeSize when computing offsets.
Furthermore, if an alloca allocates several elements at once then
these too need to be aligned, so the size of the alloca has to be
the number of elements multiplied by getABITypeSize. Logically
speaking this doesn't have to be the case when allocating just
one element, but it is simpler to also use getABITypeSize in this
case. So alloca's and mallocs should use getABITypeSize. Finally,
since gcc's only notion of size is that given by getABITypeSize, if
you want to output assembler etc the same as gcc then getABITypeSize
is the size you want.
Since a store will overwrite no more than getTypeStoreSize bytes,
and a read will read no more than that many bytes, this is the
notion of size appropriate for alias analysis calculations.
In this patch I have corrected all type size uses except some of
those in ScalarReplAggregates, lib/Codegen, lib/Target (the hard
cases). I will get around to auditing these too at some point,
but I could do with some help.
Finally, I made one change which I think wise but others might
consider pointless and suboptimal: in an unpacked struct the
amount of space allocated for a field is now given by the ABI
size rather than getTypeStoreSize. I did this because every
other place that reserves memory for a type (eg: alloca) now
uses getABITypeSize, and I didn't want to make an exception
for unpacked structs, i.e. I did it to make things more uniform.
This only effects structs containing long doubles and arbitrary
precision integers. If someone wants to pack these types more
tightly they can always use a packed struct.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@43620 91177308-0d34-0410-b5e6-96231b3b80d8
2007-11-01 20:53:16 +00:00
|
|
|
unsigned PS = TD->getPointerSizeInBits();
|
|
|
|
if (TD->getTypeSizeInBits(SO1->getType()) == PS) {
|
2004-04-05 01:30:19 +00:00
|
|
|
// Convert GO1 to SO1's type.
|
2006-12-12 09:18:51 +00:00
|
|
|
GO1 = InsertCastToIntPtrTy(GO1, SO1->getType(), &GEP, this);
|
2004-04-05 01:30:19 +00:00
|
|
|
|
Executive summary: getTypeSize -> getTypeStoreSize / getABITypeSize.
The meaning of getTypeSize was not clear - clarifying it is important
now that we have x86 long double and arbitrary precision integers.
The issue with long double is that it requires 80 bits, and this is
not a multiple of its alignment. This gives a primitive type for
which getTypeSize differed from getABITypeSize. For arbitrary precision
integers it is even worse: there is the minimum number of bits needed to
hold the type (eg: 36 for an i36), the maximum number of bits that will
be overwriten when storing the type (40 bits for i36) and the ABI size
(i.e. the storage size rounded up to a multiple of the alignment; 64 bits
for i36).
This patch removes getTypeSize (not really - it is still there but
deprecated to allow for a gradual transition). Instead there is:
(1) getTypeSizeInBits - a number of bits that suffices to hold all
values of the type. For a primitive type, this is the minimum number
of bits. For an i36 this is 36 bits. For x86 long double it is 80.
This corresponds to gcc's TYPE_PRECISION.
(2) getTypeStoreSizeInBits - the maximum number of bits that is
written when storing the type (or read when reading it). For an
i36 this is 40 bits, for an x86 long double it is 80 bits. This
is the size alias analysis is interested in (getTypeStoreSize
returns the number of bytes). There doesn't seem to be anything
corresponding to this in gcc.
(3) getABITypeSizeInBits - this is getTypeStoreSizeInBits rounded
up to a multiple of the alignment. For an i36 this is 64, for an
x86 long double this is 96 or 128 depending on the OS. This is the
spacing between consecutive elements when you form an array out of
this type (getABITypeSize returns the number of bytes). This is
TYPE_SIZE in gcc.
Since successive elements in a SequentialType (arrays, pointers
and vectors) need to be aligned, the spacing between them will be
given by getABITypeSize. This means that the size of an array
is the length times the getABITypeSize. It also means that GEP
computations need to use getABITypeSize when computing offsets.
Furthermore, if an alloca allocates several elements at once then
these too need to be aligned, so the size of the alloca has to be
the number of elements multiplied by getABITypeSize. Logically
speaking this doesn't have to be the case when allocating just
one element, but it is simpler to also use getABITypeSize in this
case. So alloca's and mallocs should use getABITypeSize. Finally,
since gcc's only notion of size is that given by getABITypeSize, if
you want to output assembler etc the same as gcc then getABITypeSize
is the size you want.
Since a store will overwrite no more than getTypeStoreSize bytes,
and a read will read no more than that many bytes, this is the
notion of size appropriate for alias analysis calculations.
In this patch I have corrected all type size uses except some of
those in ScalarReplAggregates, lib/Codegen, lib/Target (the hard
cases). I will get around to auditing these too at some point,
but I could do with some help.
Finally, I made one change which I think wise but others might
consider pointless and suboptimal: in an unpacked struct the
amount of space allocated for a field is now given by the ABI
size rather than getTypeStoreSize. I did this because every
other place that reserves memory for a type (eg: alloca) now
uses getABITypeSize, and I didn't want to make an exception
for unpacked structs, i.e. I did it to make things more uniform.
This only effects structs containing long doubles and arbitrary
precision integers. If someone wants to pack these types more
tightly they can always use a packed struct.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@43620 91177308-0d34-0410-b5e6-96231b3b80d8
2007-11-01 20:53:16 +00:00
|
|
|
} else if (TD->getTypeSizeInBits(GO1->getType()) == PS) {
|
2004-04-05 01:30:19 +00:00
|
|
|
// Convert SO1 to GO1's type.
|
2006-12-12 09:18:51 +00:00
|
|
|
SO1 = InsertCastToIntPtrTy(SO1, GO1->getType(), &GEP, this);
|
2004-04-05 01:30:19 +00:00
|
|
|
} else {
|
|
|
|
const Type *PT = TD->getIntPtrType();
|
2006-12-12 09:18:51 +00:00
|
|
|
SO1 = InsertCastToIntPtrTy(SO1, PT, &GEP, this);
|
|
|
|
GO1 = InsertCastToIntPtrTy(GO1, PT, &GEP, this);
|
2004-04-05 01:30:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-05-07 22:09:22 +00:00
|
|
|
if (isa<Constant>(SO1) && isa<Constant>(GO1))
|
|
|
|
Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1));
|
|
|
|
else {
|
2004-06-10 02:07:29 +00:00
|
|
|
Sum = BinaryOperator::createAdd(SO1, GO1, PtrOp->getName()+".sum");
|
|
|
|
InsertNewInstBefore(cast<Instruction>(Sum), GEP);
|
2004-05-07 22:09:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recycle the GEP we already have if possible.
|
|
|
|
if (SrcGEPOperands.size() == 2) {
|
|
|
|
GEP.setOperand(0, SrcGEPOperands[0]);
|
|
|
|
GEP.setOperand(1, Sum);
|
|
|
|
return &GEP;
|
|
|
|
} else {
|
|
|
|
Indices.insert(Indices.end(), SrcGEPOperands.begin()+1,
|
|
|
|
SrcGEPOperands.end()-1);
|
|
|
|
Indices.push_back(Sum);
|
|
|
|
Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end());
|
2004-04-05 01:30:19 +00:00
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
} else if (isa<Constant>(*GEP.idx_begin()) &&
|
2004-04-05 01:30:19 +00:00
|
|
|
cast<Constant>(*GEP.idx_begin())->isNullValue() &&
|
2005-04-21 23:48:37 +00:00
|
|
|
SrcGEPOperands.size() != 1) {
|
2002-08-02 19:29:35 +00:00
|
|
|
// Otherwise we can do the fold if the first index of the GEP is a zero
|
2004-03-25 22:59:29 +00:00
|
|
|
Indices.insert(Indices.end(), SrcGEPOperands.begin()+1,
|
|
|
|
SrcGEPOperands.end());
|
2002-08-02 19:29:35 +00:00
|
|
|
Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end());
|
|
|
|
}
|
2001-12-14 16:52:21 +00:00
|
|
|
|
2002-08-02 19:29:35 +00:00
|
|
|
if (!Indices.empty())
|
2008-04-06 20:25:17 +00:00
|
|
|
return GetElementPtrInst::Create(SrcGEPOperands[0], Indices.begin(),
|
|
|
|
Indices.end(), GEP.getName());
|
2002-08-17 22:21:59 +00:00
|
|
|
|
2004-05-07 22:09:22 +00:00
|
|
|
} else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) {
|
2002-08-17 22:21:59 +00:00
|
|
|
// GEP of global variable. If all of the indices for this GEP are
|
|
|
|
// constants, we can promote this to a constexpr instead of an instruction.
|
|
|
|
|
|
|
|
// Scan for nonconstants...
|
2007-01-31 04:40:53 +00:00
|
|
|
SmallVector<Constant*, 8> Indices;
|
2002-08-17 22:21:59 +00:00
|
|
|
User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end();
|
|
|
|
for (; I != E && isa<Constant>(*I); ++I)
|
|
|
|
Indices.push_back(cast<Constant>(*I));
|
|
|
|
|
|
|
|
if (I == E) { // If they are all constants...
|
2007-01-31 04:40:53 +00:00
|
|
|
Constant *CE = ConstantExpr::getGetElementPtr(GV,
|
|
|
|
&Indices[0],Indices.size());
|
2002-08-17 22:21:59 +00:00
|
|
|
|
|
|
|
// Replace all uses of the GEP with the new constexpr...
|
|
|
|
return ReplaceInstUsesWith(GEP, CE);
|
|
|
|
}
|
2006-11-27 01:05:10 +00:00
|
|
|
} else if (Value *X = getBitCastOperand(PtrOp)) { // Is the operand a cast?
|
2005-09-13 00:40:14 +00:00
|
|
|
if (!isa<PointerType>(X->getType())) {
|
|
|
|
// Not interesting. Source pointer must be a cast from pointer.
|
|
|
|
} else if (HasZeroPointerIndex) {
|
2007-12-12 15:21:32 +00:00
|
|
|
// transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ...
|
|
|
|
// into : GEP [10 x i8]* X, i32 0, ...
|
2005-09-13 00:40:14 +00:00
|
|
|
//
|
|
|
|
// This occurs when the program declares an array extern like "int X[];"
|
|
|
|
//
|
|
|
|
const PointerType *CPTy = cast<PointerType>(PtrOp->getType());
|
|
|
|
const PointerType *XTy = cast<PointerType>(X->getType());
|
|
|
|
if (const ArrayType *XATy =
|
|
|
|
dyn_cast<ArrayType>(XTy->getElementType()))
|
|
|
|
if (const ArrayType *CATy =
|
|
|
|
dyn_cast<ArrayType>(CPTy->getElementType()))
|
|
|
|
if (CATy->getElementType() == XATy->getElementType()) {
|
|
|
|
// At this point, we know that the cast source type is a pointer
|
|
|
|
// to an array of the same type as the destination pointer
|
|
|
|
// array. Because the array type is never stepped over (there
|
|
|
|
// is a leading zero) we can fold the cast into this GEP.
|
|
|
|
GEP.setOperand(0, X);
|
|
|
|
return &GEP;
|
|
|
|
}
|
|
|
|
} else if (GEP.getNumOperands() == 2) {
|
|
|
|
// Transform things like:
|
2007-12-12 15:21:32 +00:00
|
|
|
// %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V
|
|
|
|
// into: %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast
|
2005-09-13 00:40:14 +00:00
|
|
|
const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType();
|
|
|
|
const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType();
|
|
|
|
if (isa<ArrayType>(SrcElTy) &&
|
Executive summary: getTypeSize -> getTypeStoreSize / getABITypeSize.
The meaning of getTypeSize was not clear - clarifying it is important
now that we have x86 long double and arbitrary precision integers.
The issue with long double is that it requires 80 bits, and this is
not a multiple of its alignment. This gives a primitive type for
which getTypeSize differed from getABITypeSize. For arbitrary precision
integers it is even worse: there is the minimum number of bits needed to
hold the type (eg: 36 for an i36), the maximum number of bits that will
be overwriten when storing the type (40 bits for i36) and the ABI size
(i.e. the storage size rounded up to a multiple of the alignment; 64 bits
for i36).
This patch removes getTypeSize (not really - it is still there but
deprecated to allow for a gradual transition). Instead there is:
(1) getTypeSizeInBits - a number of bits that suffices to hold all
values of the type. For a primitive type, this is the minimum number
of bits. For an i36 this is 36 bits. For x86 long double it is 80.
This corresponds to gcc's TYPE_PRECISION.
(2) getTypeStoreSizeInBits - the maximum number of bits that is
written when storing the type (or read when reading it). For an
i36 this is 40 bits, for an x86 long double it is 80 bits. This
is the size alias analysis is interested in (getTypeStoreSize
returns the number of bytes). There doesn't seem to be anything
corresponding to this in gcc.
(3) getABITypeSizeInBits - this is getTypeStoreSizeInBits rounded
up to a multiple of the alignment. For an i36 this is 64, for an
x86 long double this is 96 or 128 depending on the OS. This is the
spacing between consecutive elements when you form an array out of
this type (getABITypeSize returns the number of bytes). This is
TYPE_SIZE in gcc.
Since successive elements in a SequentialType (arrays, pointers
and vectors) need to be aligned, the spacing between them will be
given by getABITypeSize. This means that the size of an array
is the length times the getABITypeSize. It also means that GEP
computations need to use getABITypeSize when computing offsets.
Furthermore, if an alloca allocates several elements at once then
these too need to be aligned, so the size of the alloca has to be
the number of elements multiplied by getABITypeSize. Logically
speaking this doesn't have to be the case when allocating just
one element, but it is simpler to also use getABITypeSize in this
case. So alloca's and mallocs should use getABITypeSize. Finally,
since gcc's only notion of size is that given by getABITypeSize, if
you want to output assembler etc the same as gcc then getABITypeSize
is the size you want.
Since a store will overwrite no more than getTypeStoreSize bytes,
and a read will read no more than that many bytes, this is the
notion of size appropriate for alias analysis calculations.
In this patch I have corrected all type size uses except some of
those in ScalarReplAggregates, lib/Codegen, lib/Target (the hard
cases). I will get around to auditing these too at some point,
but I could do with some help.
Finally, I made one change which I think wise but others might
consider pointless and suboptimal: in an unpacked struct the
amount of space allocated for a field is now given by the ABI
size rather than getTypeStoreSize. I did this because every
other place that reserves memory for a type (eg: alloca) now
uses getABITypeSize, and I didn't want to make an exception
for unpacked structs, i.e. I did it to make things more uniform.
This only effects structs containing long doubles and arbitrary
precision integers. If someone wants to pack these types more
tightly they can always use a packed struct.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@43620 91177308-0d34-0410-b5e6-96231b3b80d8
2007-11-01 20:53:16 +00:00
|
|
|
TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType()) ==
|
|
|
|
TD->getABITypeSize(ResElTy)) {
|
2007-09-04 15:46:09 +00:00
|
|
|
Value *Idx[2];
|
|
|
|
Idx[0] = Constant::getNullValue(Type::Int32Ty);
|
|
|
|
Idx[1] = GEP.getOperand(1);
|
2005-09-13 00:40:14 +00:00
|
|
|
Value *V = InsertNewInstBefore(
|
2008-04-06 20:25:17 +00:00
|
|
|
GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName()), GEP);
|
2006-11-27 01:05:10 +00:00
|
|
|
// V and GEP are both pointer types --> BitCast
|
|
|
|
return new BitCastInst(V, GEP.getType());
|
2004-02-22 05:25:17 +00:00
|
|
|
}
|
2005-09-13 18:36:04 +00:00
|
|
|
|
|
|
|
// Transform things like:
|
2007-12-12 15:21:32 +00:00
|
|
|
// getelementptr i8* bitcast ([100 x double]* X to i8*), i32 %tmp
|
2005-09-13 18:36:04 +00:00
|
|
|
// (where tmp = 8*tmp2) into:
|
2007-12-12 15:21:32 +00:00
|
|
|
// getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast
|
2005-09-13 18:36:04 +00:00
|
|
|
|
2007-12-12 15:21:32 +00:00
|
|
|
if (isa<ArrayType>(SrcElTy) && ResElTy == Type::Int8Ty) {
|
2005-09-13 18:36:04 +00:00
|
|
|
uint64_t ArrayEltSize =
|
Executive summary: getTypeSize -> getTypeStoreSize / getABITypeSize.
The meaning of getTypeSize was not clear - clarifying it is important
now that we have x86 long double and arbitrary precision integers.
The issue with long double is that it requires 80 bits, and this is
not a multiple of its alignment. This gives a primitive type for
which getTypeSize differed from getABITypeSize. For arbitrary precision
integers it is even worse: there is the minimum number of bits needed to
hold the type (eg: 36 for an i36), the maximum number of bits that will
be overwriten when storing the type (40 bits for i36) and the ABI size
(i.e. the storage size rounded up to a multiple of the alignment; 64 bits
for i36).
This patch removes getTypeSize (not really - it is still there but
deprecated to allow for a gradual transition). Instead there is:
(1) getTypeSizeInBits - a number of bits that suffices to hold all
values of the type. For a primitive type, this is the minimum number
of bits. For an i36 this is 36 bits. For x86 long double it is 80.
This corresponds to gcc's TYPE_PRECISION.
(2) getTypeStoreSizeInBits - the maximum number of bits that is
written when storing the type (or read when reading it). For an
i36 this is 40 bits, for an x86 long double it is 80 bits. This
is the size alias analysis is interested in (getTypeStoreSize
returns the number of bytes). There doesn't seem to be anything
corresponding to this in gcc.
(3) getABITypeSizeInBits - this is getTypeStoreSizeInBits rounded
up to a multiple of the alignment. For an i36 this is 64, for an
x86 long double this is 96 or 128 depending on the OS. This is the
spacing between consecutive elements when you form an array out of
this type (getABITypeSize returns the number of bytes). This is
TYPE_SIZE in gcc.
Since successive elements in a SequentialType (arrays, pointers
and vectors) need to be aligned, the spacing between them will be
given by getABITypeSize. This means that the size of an array
is the length times the getABITypeSize. It also means that GEP
computations need to use getABITypeSize when computing offsets.
Furthermore, if an alloca allocates several elements at once then
these too need to be aligned, so the size of the alloca has to be
the number of elements multiplied by getABITypeSize. Logically
speaking this doesn't have to be the case when allocating just
one element, but it is simpler to also use getABITypeSize in this
case. So alloca's and mallocs should use getABITypeSize. Finally,
since gcc's only notion of size is that given by getABITypeSize, if
you want to output assembler etc the same as gcc then getABITypeSize
is the size you want.
Since a store will overwrite no more than getTypeStoreSize bytes,
and a read will read no more than that many bytes, this is the
notion of size appropriate for alias analysis calculations.
In this patch I have corrected all type size uses except some of
those in ScalarReplAggregates, lib/Codegen, lib/Target (the hard
cases). I will get around to auditing these too at some point,
but I could do with some help.
Finally, I made one change which I think wise but others might
consider pointless and suboptimal: in an unpacked struct the
amount of space allocated for a field is now given by the ABI
size rather than getTypeStoreSize. I did this because every
other place that reserves memory for a type (eg: alloca) now
uses getABITypeSize, and I didn't want to make an exception
for unpacked structs, i.e. I did it to make things more uniform.
This only effects structs containing long doubles and arbitrary
precision integers. If someone wants to pack these types more
tightly they can always use a packed struct.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@43620 91177308-0d34-0410-b5e6-96231b3b80d8
2007-11-01 20:53:16 +00:00
|
|
|
TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType());
|
2005-09-13 18:36:04 +00:00
|
|
|
|
|
|
|
// Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We
|
|
|
|
// allow either a mul, shift, or constant here.
|
|
|
|
Value *NewIdx = 0;
|
|
|
|
ConstantInt *Scale = 0;
|
|
|
|
if (ArrayEltSize == 1) {
|
|
|
|
NewIdx = GEP.getOperand(1);
|
|
|
|
Scale = ConstantInt::get(NewIdx->getType(), 1);
|
|
|
|
} else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) {
|
2005-09-14 17:32:56 +00:00
|
|
|
NewIdx = ConstantInt::get(CI->getType(), 1);
|
2005-09-13 18:36:04 +00:00
|
|
|
Scale = CI;
|
|
|
|
} else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){
|
|
|
|
if (Inst->getOpcode() == Instruction::Shl &&
|
|
|
|
isa<ConstantInt>(Inst->getOperand(1))) {
|
2007-03-30 09:29:48 +00:00
|
|
|
ConstantInt *ShAmt = cast<ConstantInt>(Inst->getOperand(1));
|
|
|
|
uint32_t ShAmtVal = ShAmt->getLimitedValue(64);
|
|
|
|
Scale = ConstantInt::get(Inst->getType(), 1ULL << ShAmtVal);
|
2005-09-13 18:36:04 +00:00
|
|
|
NewIdx = Inst->getOperand(0);
|
|
|
|
} else if (Inst->getOpcode() == Instruction::Mul &&
|
|
|
|
isa<ConstantInt>(Inst->getOperand(1))) {
|
|
|
|
Scale = cast<ConstantInt>(Inst->getOperand(1));
|
|
|
|
NewIdx = Inst->getOperand(0);
|
|
|
|
}
|
|
|
|
}
|
2007-12-12 15:21:32 +00:00
|
|
|
|
2005-09-13 18:36:04 +00:00
|
|
|
// If the index will be to exactly the right offset with the scale taken
|
2007-12-12 15:21:32 +00:00
|
|
|
// out, perform the transformation. Note, we don't know whether Scale is
|
|
|
|
// signed or not. We'll use unsigned version of division/modulo
|
|
|
|
// operation after making sure Scale doesn't have the sign bit set.
|
|
|
|
if (Scale && Scale->getSExtValue() >= 0LL &&
|
|
|
|
Scale->getZExtValue() % ArrayEltSize == 0) {
|
|
|
|
Scale = ConstantInt::get(Scale->getType(),
|
|
|
|
Scale->getZExtValue() / ArrayEltSize);
|
2006-10-20 07:07:24 +00:00
|
|
|
if (Scale->getZExtValue() != 1) {
|
2006-12-12 09:18:51 +00:00
|
|
|
Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(),
|
2007-12-12 15:21:32 +00:00
|
|
|
false /*ZExt*/);
|
2005-09-13 18:36:04 +00:00
|
|
|
Instruction *Sc = BinaryOperator::createMul(NewIdx, C, "idxscale");
|
|
|
|
NewIdx = InsertNewInstBefore(Sc, GEP);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert the new GEP instruction.
|
2007-09-04 15:46:09 +00:00
|
|
|
Value *Idx[2];
|
|
|
|
Idx[0] = Constant::getNullValue(Type::Int32Ty);
|
|
|
|
Idx[1] = NewIdx;
|
2006-11-27 01:05:10 +00:00
|
|
|
Instruction *NewGEP =
|
2008-04-06 20:25:17 +00:00
|
|
|
GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName());
|
2006-11-27 01:05:10 +00:00
|
|
|
NewGEP = InsertNewInstBefore(NewGEP, GEP);
|
|
|
|
// The NewGEP must be pointer typed, so must the old one -> BitCast
|
|
|
|
return new BitCastInst(NewGEP, GEP.getType());
|
2005-09-13 18:36:04 +00:00
|
|
|
}
|
|
|
|
}
|
2004-02-22 05:25:17 +00:00
|
|
|
}
|
2001-12-14 16:52:21 +00:00
|
|
|
}
|
2002-08-02 19:29:35 +00:00
|
|
|
|
2001-12-14 16:52:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-11-04 16:18:53 +00:00
|
|
|
Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) {
|
|
|
|
// Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1
|
2008-02-20 11:26:25 +00:00
|
|
|
if (AI.isArrayAllocation()) { // Check C != 1
|
2006-10-20 07:07:24 +00:00
|
|
|
if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) {
|
|
|
|
const Type *NewTy =
|
|
|
|
ArrayType::get(AI.getAllocatedType(), C->getZExtValue());
|
2002-11-09 00:49:43 +00:00
|
|
|
AllocationInst *New = 0;
|
2002-11-04 16:18:53 +00:00
|
|
|
|
|
|
|
// Create and insert the replacement instruction...
|
|
|
|
if (isa<MallocInst>(AI))
|
2005-11-05 09:21:28 +00:00
|
|
|
New = new MallocInst(NewTy, 0, AI.getAlignment(), AI.getName());
|
2002-11-09 00:49:43 +00:00
|
|
|
else {
|
|
|
|
assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!");
|
2005-11-05 09:21:28 +00:00
|
|
|
New = new AllocaInst(NewTy, 0, AI.getAlignment(), AI.getName());
|
2002-11-09 00:49:43 +00:00
|
|
|
}
|
2004-03-19 06:08:10 +00:00
|
|
|
|
|
|
|
InsertNewInstBefore(New, AI);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2002-11-04 16:18:53 +00:00
|
|
|
// Scan to the end of the allocation instructions, to skip over a block of
|
|
|
|
// allocas if possible...
|
|
|
|
//
|
|
|
|
BasicBlock::iterator It = New;
|
|
|
|
while (isa<AllocationInst>(*It)) ++It;
|
|
|
|
|
|
|
|
// Now that I is pointing to the first non-allocation-inst in the block,
|
|
|
|
// insert our getelementptr instruction...
|
|
|
|
//
|
2006-12-31 05:48:39 +00:00
|
|
|
Value *NullIdx = Constant::getNullValue(Type::Int32Ty);
|
2007-09-04 15:46:09 +00:00
|
|
|
Value *Idx[2];
|
|
|
|
Idx[0] = NullIdx;
|
|
|
|
Idx[1] = NullIdx;
|
2008-04-06 20:25:17 +00:00
|
|
|
Value *V = GetElementPtrInst::Create(New, Idx, Idx + 2,
|
|
|
|
New->getName()+".sub", It);
|
2002-11-04 16:18:53 +00:00
|
|
|
|
|
|
|
// Now make everything use the getelementptr instead of the original
|
|
|
|
// allocation.
|
2004-03-19 06:08:10 +00:00
|
|
|
return ReplaceInstUsesWith(AI, V);
|
2004-10-16 18:11:37 +00:00
|
|
|
} else if (isa<UndefValue>(AI.getArraySize())) {
|
|
|
|
return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
|
2002-11-04 16:18:53 +00:00
|
|
|
}
|
2008-02-20 11:26:25 +00:00
|
|
|
}
|
2004-03-19 06:08:10 +00:00
|
|
|
|
|
|
|
// If alloca'ing a zero byte object, replace the alloca with a null pointer.
|
|
|
|
// Note that we only do this for alloca's, because malloc should allocate and
|
|
|
|
// return a unique pointer, even for a zero byte allocation.
|
2005-04-21 23:48:37 +00:00
|
|
|
if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() &&
|
Executive summary: getTypeSize -> getTypeStoreSize / getABITypeSize.
The meaning of getTypeSize was not clear - clarifying it is important
now that we have x86 long double and arbitrary precision integers.
The issue with long double is that it requires 80 bits, and this is
not a multiple of its alignment. This gives a primitive type for
which getTypeSize differed from getABITypeSize. For arbitrary precision
integers it is even worse: there is the minimum number of bits needed to
hold the type (eg: 36 for an i36), the maximum number of bits that will
be overwriten when storing the type (40 bits for i36) and the ABI size
(i.e. the storage size rounded up to a multiple of the alignment; 64 bits
for i36).
This patch removes getTypeSize (not really - it is still there but
deprecated to allow for a gradual transition). Instead there is:
(1) getTypeSizeInBits - a number of bits that suffices to hold all
values of the type. For a primitive type, this is the minimum number
of bits. For an i36 this is 36 bits. For x86 long double it is 80.
This corresponds to gcc's TYPE_PRECISION.
(2) getTypeStoreSizeInBits - the maximum number of bits that is
written when storing the type (or read when reading it). For an
i36 this is 40 bits, for an x86 long double it is 80 bits. This
is the size alias analysis is interested in (getTypeStoreSize
returns the number of bytes). There doesn't seem to be anything
corresponding to this in gcc.
(3) getABITypeSizeInBits - this is getTypeStoreSizeInBits rounded
up to a multiple of the alignment. For an i36 this is 64, for an
x86 long double this is 96 or 128 depending on the OS. This is the
spacing between consecutive elements when you form an array out of
this type (getABITypeSize returns the number of bytes). This is
TYPE_SIZE in gcc.
Since successive elements in a SequentialType (arrays, pointers
and vectors) need to be aligned, the spacing between them will be
given by getABITypeSize. This means that the size of an array
is the length times the getABITypeSize. It also means that GEP
computations need to use getABITypeSize when computing offsets.
Furthermore, if an alloca allocates several elements at once then
these too need to be aligned, so the size of the alloca has to be
the number of elements multiplied by getABITypeSize. Logically
speaking this doesn't have to be the case when allocating just
one element, but it is simpler to also use getABITypeSize in this
case. So alloca's and mallocs should use getABITypeSize. Finally,
since gcc's only notion of size is that given by getABITypeSize, if
you want to output assembler etc the same as gcc then getABITypeSize
is the size you want.
Since a store will overwrite no more than getTypeStoreSize bytes,
and a read will read no more than that many bytes, this is the
notion of size appropriate for alias analysis calculations.
In this patch I have corrected all type size uses except some of
those in ScalarReplAggregates, lib/Codegen, lib/Target (the hard
cases). I will get around to auditing these too at some point,
but I could do with some help.
Finally, I made one change which I think wise but others might
consider pointless and suboptimal: in an unpacked struct the
amount of space allocated for a field is now given by the ABI
size rather than getTypeStoreSize. I did this because every
other place that reserves memory for a type (eg: alloca) now
uses getABITypeSize, and I didn't want to make an exception
for unpacked structs, i.e. I did it to make things more uniform.
This only effects structs containing long doubles and arbitrary
precision integers. If someone wants to pack these types more
tightly they can always use a packed struct.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@43620 91177308-0d34-0410-b5e6-96231b3b80d8
2007-11-01 20:53:16 +00:00
|
|
|
TD->getABITypeSize(AI.getAllocatedType()) == 0)
|
2004-03-19 06:08:10 +00:00
|
|
|
return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
|
|
|
|
|
2002-11-04 16:18:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-12-07 01:24:23 +00:00
|
|
|
Instruction *InstCombiner::visitFreeInst(FreeInst &FI) {
|
|
|
|
Value *Op = FI.getOperand(0);
|
|
|
|
|
2004-10-18 02:59:09 +00:00
|
|
|
// free undef -> unreachable.
|
|
|
|
if (isa<UndefValue>(Op)) {
|
|
|
|
// Insert a new store to null because we cannot modify the CFG here.
|
2007-01-11 12:24:14 +00:00
|
|
|
new StoreInst(ConstantInt::getTrue(),
|
2007-12-17 01:12:55 +00:00
|
|
|
UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), &FI);
|
2004-10-18 02:59:09 +00:00
|
|
|
return EraseInstFromFunction(FI);
|
|
|
|
}
|
2007-04-14 00:20:02 +00:00
|
|
|
|
2004-02-28 04:57:37 +00:00
|
|
|
// If we have 'free null' delete the instruction. This can happen in stl code
|
|
|
|
// when lots of inlining happens.
|
2004-10-18 02:59:09 +00:00
|
|
|
if (isa<ConstantPointerNull>(Op))
|
2004-02-28 05:22:00 +00:00
|
|
|
return EraseInstFromFunction(FI);
|
2007-04-14 00:20:02 +00:00
|
|
|
|
|
|
|
// Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X
|
|
|
|
if (BitCastInst *CI = dyn_cast<BitCastInst>(Op)) {
|
|
|
|
FI.setOperand(0, CI->getOperand(0));
|
|
|
|
return &FI;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change free (gep X, 0,0,0,0) into free(X)
|
|
|
|
if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) {
|
|
|
|
if (GEPI->hasAllZeroIndices()) {
|
|
|
|
AddToWorkList(GEPI);
|
|
|
|
FI.setOperand(0, GEPI->getOperand(0));
|
|
|
|
return &FI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change free(malloc) into nothing, if the malloc has a single use.
|
|
|
|
if (MallocInst *MI = dyn_cast<MallocInst>(Op))
|
|
|
|
if (MI->hasOneUse()) {
|
|
|
|
EraseInstFromFunction(FI);
|
|
|
|
return EraseInstFromFunction(*MI);
|
|
|
|
}
|
2004-02-28 04:57:37 +00:00
|
|
|
|
2003-12-07 01:24:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-31 05:51:45 +00:00
|
|
|
/// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible.
|
2007-10-18 19:52:32 +00:00
|
|
|
static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI,
|
2008-02-26 10:53:30 +00:00
|
|
|
const TargetData *TD) {
|
Factor some code to handle "load (constantexpr cast foo)" just like
"load (cast foo)". This allows us to compile C++ code like this:
class Bclass {
public: virtual int operator()() { return 666; }
};
class Dclass: public Bclass {
public: virtual int operator()() { return 667; }
} ;
int main(int argc, char** argv) {
Dclass x;
return x();
}
Into this:
int %main(int %argc, sbyte** %argv) {
entry:
call void %__main( )
ret int 667
}
Instead of this:
int %main(int %argc, sbyte** %argv) {
entry:
%x = alloca "struct.std::bad_typeid" ; <"struct.std::bad_typeid"*> [#uses=3]
call void %__main( )
%tmp.1.i.i = getelementptr "struct.std::bad_typeid"* %x, uint 0, uint 0, uint 0 ; <int (...)***> [#uses=1]
store int (...)** getelementptr ([3 x int (...)*]* %vtable for Bclass, int 0, long 2), int (...)*** %tmp.1.i.i
%tmp.3.i = getelementptr "struct.std::bad_typeid"* %x, int 0, uint 0, uint 0 ; <int (...)***> [#uses=1]
store int (...)** getelementptr ([3 x int (...)*]* %vtable for Dclass, int 0, long 2), int (...)*** %tmp.3.i
%tmp.5 = load int ("struct.std::bad_typeid"*)** cast (int (...)** getelementptr ([3 x int (...)*]* %vtable for Dclass, int 0, long 2) to int
("struct.std::bad_typeid"*)**) ; <int ("struct.std::bad_typeid"*)*> [#uses=1]
%tmp.6 = call int %tmp.5( "struct.std::bad_typeid"* %x ) ; <int> [#uses=1]
ret int %tmp.6
ret int 0
}
In order words, we now resolve the virtual function call.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@14783 91177308-0d34-0410-b5e6-96231b3b80d8
2004-07-13 01:49:43 +00:00
|
|
|
User *CI = cast<User>(LI.getOperand(0));
|
2005-01-31 04:50:46 +00:00
|
|
|
Value *CastOp = CI->getOperand(0);
|
Factor some code to handle "load (constantexpr cast foo)" just like
"load (cast foo)". This allows us to compile C++ code like this:
class Bclass {
public: virtual int operator()() { return 666; }
};
class Dclass: public Bclass {
public: virtual int operator()() { return 667; }
} ;
int main(int argc, char** argv) {
Dclass x;
return x();
}
Into this:
int %main(int %argc, sbyte** %argv) {
entry:
call void %__main( )
ret int 667
}
Instead of this:
int %main(int %argc, sbyte** %argv) {
entry:
%x = alloca "struct.std::bad_typeid" ; <"struct.std::bad_typeid"*> [#uses=3]
call void %__main( )
%tmp.1.i.i = getelementptr "struct.std::bad_typeid"* %x, uint 0, uint 0, uint 0 ; <int (...)***> [#uses=1]
store int (...)** getelementptr ([3 x int (...)*]* %vtable for Bclass, int 0, long 2), int (...)*** %tmp.1.i.i
%tmp.3.i = getelementptr "struct.std::bad_typeid"* %x, int 0, uint 0, uint 0 ; <int (...)***> [#uses=1]
store int (...)** getelementptr ([3 x int (...)*]* %vtable for Dclass, int 0, long 2), int (...)*** %tmp.3.i
%tmp.5 = load int ("struct.std::bad_typeid"*)** cast (int (...)** getelementptr ([3 x int (...)*]* %vtable for Dclass, int 0, long 2) to int
("struct.std::bad_typeid"*)**) ; <int ("struct.std::bad_typeid"*)*> [#uses=1]
%tmp.6 = call int %tmp.5( "struct.std::bad_typeid"* %x ) ; <int> [#uses=1]
ret int %tmp.6
ret int 0
}
In order words, we now resolve the virtual function call.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@14783 91177308-0d34-0410-b5e6-96231b3b80d8
2004-07-13 01:49:43 +00:00
|
|
|
|
2007-10-18 19:52:32 +00:00
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(CI)) {
|
|
|
|
// Instead of loading constant c string, use corresponding integer value
|
|
|
|
// directly if string length is small enough.
|
|
|
|
const std::string &Str = CE->getOperand(0)->getStringValue();
|
|
|
|
if (!Str.empty()) {
|
|
|
|
unsigned len = Str.length();
|
|
|
|
const Type *Ty = cast<PointerType>(CE->getType())->getElementType();
|
|
|
|
unsigned numBits = Ty->getPrimitiveSizeInBits();
|
|
|
|
// Replace LI with immediate integer store.
|
|
|
|
if ((numBits >> 3) == len + 1) {
|
2008-02-26 10:53:30 +00:00
|
|
|
APInt StrVal(numBits, 0);
|
|
|
|
APInt SingleChar(numBits, 0);
|
|
|
|
if (TD->isLittleEndian()) {
|
|
|
|
for (signed i = len-1; i >= 0; i--) {
|
|
|
|
SingleChar = (uint64_t) Str[i];
|
|
|
|
StrVal = (StrVal << 8) | SingleChar;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (unsigned i = 0; i < len; i++) {
|
|
|
|
SingleChar = (uint64_t) Str[i];
|
|
|
|
StrVal = (StrVal << 8) | SingleChar;
|
|
|
|
}
|
|
|
|
// Append NULL at the end.
|
|
|
|
SingleChar = 0;
|
|
|
|
StrVal = (StrVal << 8) | SingleChar;
|
|
|
|
}
|
|
|
|
Value *NL = ConstantInt::get(StrVal);
|
|
|
|
return IC.ReplaceInstUsesWith(LI, NL);
|
2007-10-18 19:52:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Factor some code to handle "load (constantexpr cast foo)" just like
"load (cast foo)". This allows us to compile C++ code like this:
class Bclass {
public: virtual int operator()() { return 666; }
};
class Dclass: public Bclass {
public: virtual int operator()() { return 667; }
} ;
int main(int argc, char** argv) {
Dclass x;
return x();
}
Into this:
int %main(int %argc, sbyte** %argv) {
entry:
call void %__main( )
ret int 667
}
Instead of this:
int %main(int %argc, sbyte** %argv) {
entry:
%x = alloca "struct.std::bad_typeid" ; <"struct.std::bad_typeid"*> [#uses=3]
call void %__main( )
%tmp.1.i.i = getelementptr "struct.std::bad_typeid"* %x, uint 0, uint 0, uint 0 ; <int (...)***> [#uses=1]
store int (...)** getelementptr ([3 x int (...)*]* %vtable for Bclass, int 0, long 2), int (...)*** %tmp.1.i.i
%tmp.3.i = getelementptr "struct.std::bad_typeid"* %x, int 0, uint 0, uint 0 ; <int (...)***> [#uses=1]
store int (...)** getelementptr ([3 x int (...)*]* %vtable for Dclass, int 0, long 2), int (...)*** %tmp.3.i
%tmp.5 = load int ("struct.std::bad_typeid"*)** cast (int (...)** getelementptr ([3 x int (...)*]* %vtable for Dclass, int 0, long 2) to int
("struct.std::bad_typeid"*)**) ; <int ("struct.std::bad_typeid"*)*> [#uses=1]
%tmp.6 = call int %tmp.5( "struct.std::bad_typeid"* %x ) ; <int> [#uses=1]
ret int %tmp.6
ret int 0
}
In order words, we now resolve the virtual function call.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@14783 91177308-0d34-0410-b5e6-96231b3b80d8
2004-07-13 01:49:43 +00:00
|
|
|
const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType();
|
2005-01-31 04:50:46 +00:00
|
|
|
if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) {
|
Factor some code to handle "load (constantexpr cast foo)" just like
"load (cast foo)". This allows us to compile C++ code like this:
class Bclass {
public: virtual int operator()() { return 666; }
};
class Dclass: public Bclass {
public: virtual int operator()() { return 667; }
} ;
int main(int argc, char** argv) {
Dclass x;
return x();
}
Into this:
int %main(int %argc, sbyte** %argv) {
entry:
call void %__main( )
ret int 667
}
Instead of this:
int %main(int %argc, sbyte** %argv) {
entry:
%x = alloca "struct.std::bad_typeid" ; <"struct.std::bad_typeid"*> [#uses=3]
call void %__main( )
%tmp.1.i.i = getelementptr "struct.std::bad_typeid"* %x, uint 0, uint 0, uint 0 ; <int (...)***> [#uses=1]
store int (...)** getelementptr ([3 x int (...)*]* %vtable for Bclass, int 0, long 2), int (...)*** %tmp.1.i.i
%tmp.3.i = getelementptr "struct.std::bad_typeid"* %x, int 0, uint 0, uint 0 ; <int (...)***> [#uses=1]
store int (...)** getelementptr ([3 x int (...)*]* %vtable for Dclass, int 0, long 2), int (...)*** %tmp.3.i
%tmp.5 = load int ("struct.std::bad_typeid"*)** cast (int (...)** getelementptr ([3 x int (...)*]* %vtable for Dclass, int 0, long 2) to int
("struct.std::bad_typeid"*)**) ; <int ("struct.std::bad_typeid"*)*> [#uses=1]
%tmp.6 = call int %tmp.5( "struct.std::bad_typeid"* %x ) ; <int> [#uses=1]
ret int %tmp.6
ret int 0
}
In order words, we now resolve the virtual function call.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@14783 91177308-0d34-0410-b5e6-96231b3b80d8
2004-07-13 01:49:43 +00:00
|
|
|
const Type *SrcPTy = SrcTy->getElementType();
|
2005-01-31 04:50:46 +00:00
|
|
|
|
2007-01-22 05:51:25 +00:00
|
|
|
if (DestPTy->isInteger() || isa<PointerType>(DestPTy) ||
|
2007-02-15 02:26:10 +00:00
|
|
|
isa<VectorType>(DestPTy)) {
|
2005-01-31 04:50:46 +00:00
|
|
|
// If the source is an array, the code below will not succeed. Check to
|
|
|
|
// see if a trivial 'gep P, 0, 0' will help matters. Only do this for
|
|
|
|
// constants.
|
|
|
|
if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy))
|
|
|
|
if (Constant *CSrc = dyn_cast<Constant>(CastOp))
|
|
|
|
if (ASrcTy->getNumElements() != 0) {
|
2007-01-31 04:40:53 +00:00
|
|
|
Value *Idxs[2];
|
|
|
|
Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty);
|
|
|
|
CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2);
|
2005-01-31 04:50:46 +00:00
|
|
|
SrcTy = cast<PointerType>(CastOp->getType());
|
|
|
|
SrcPTy = SrcTy->getElementType();
|
|
|
|
}
|
|
|
|
|
2007-01-22 05:51:25 +00:00
|
|
|
if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy) ||
|
2007-02-15 02:26:10 +00:00
|
|
|
isa<VectorType>(SrcPTy)) &&
|
2005-03-29 06:37:47 +00:00
|
|
|
// Do not allow turning this into a load of an integer, which is then
|
|
|
|
// casted to a pointer, this pessimizes pointer analysis a lot.
|
|
|
|
(isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) &&
|
2007-01-22 05:51:25 +00:00
|
|
|
IC.getTargetData().getTypeSizeInBits(SrcPTy) ==
|
|
|
|
IC.getTargetData().getTypeSizeInBits(DestPTy)) {
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2005-01-31 04:50:46 +00:00
|
|
|
// Okay, we are casting from one integer or pointer type to another of
|
|
|
|
// the same size. Instead of casting the pointer before the load, cast
|
|
|
|
// the result of the loaded value.
|
|
|
|
Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CastOp,
|
|
|
|
CI->getName(),
|
|
|
|
LI.isVolatile()),LI);
|
|
|
|
// Now cast the result of the load.
|
2006-12-12 23:36:14 +00:00
|
|
|
return new BitCastInst(NewLoad, LI.getType());
|
2005-01-31 04:50:46 +00:00
|
|
|
}
|
Factor some code to handle "load (constantexpr cast foo)" just like
"load (cast foo)". This allows us to compile C++ code like this:
class Bclass {
public: virtual int operator()() { return 666; }
};
class Dclass: public Bclass {
public: virtual int operator()() { return 667; }
} ;
int main(int argc, char** argv) {
Dclass x;
return x();
}
Into this:
int %main(int %argc, sbyte** %argv) {
entry:
call void %__main( )
ret int 667
}
Instead of this:
int %main(int %argc, sbyte** %argv) {
entry:
%x = alloca "struct.std::bad_typeid" ; <"struct.std::bad_typeid"*> [#uses=3]
call void %__main( )
%tmp.1.i.i = getelementptr "struct.std::bad_typeid"* %x, uint 0, uint 0, uint 0 ; <int (...)***> [#uses=1]
store int (...)** getelementptr ([3 x int (...)*]* %vtable for Bclass, int 0, long 2), int (...)*** %tmp.1.i.i
%tmp.3.i = getelementptr "struct.std::bad_typeid"* %x, int 0, uint 0, uint 0 ; <int (...)***> [#uses=1]
store int (...)** getelementptr ([3 x int (...)*]* %vtable for Dclass, int 0, long 2), int (...)*** %tmp.3.i
%tmp.5 = load int ("struct.std::bad_typeid"*)** cast (int (...)** getelementptr ([3 x int (...)*]* %vtable for Dclass, int 0, long 2) to int
("struct.std::bad_typeid"*)**) ; <int ("struct.std::bad_typeid"*)*> [#uses=1]
%tmp.6 = call int %tmp.5( "struct.std::bad_typeid"* %x ) ; <int> [#uses=1]
ret int %tmp.6
ret int 0
}
In order words, we now resolve the virtual function call.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@14783 91177308-0d34-0410-b5e6-96231b3b80d8
2004-07-13 01:49:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-09-19 18:43:46 +00:00
|
|
|
/// isSafeToLoadUnconditionally - Return true if we know that executing a load
|
2004-09-19 19:18:10 +00:00
|
|
|
/// from this value cannot trap. If it is not obviously safe to load from the
|
|
|
|
/// specified pointer, we do a quick local scan of the basic block containing
|
|
|
|
/// ScanFrom, to determine if the address is already accessed.
|
|
|
|
static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) {
|
2007-09-19 10:10:31 +00:00
|
|
|
// If it is an alloca it is always safe to load from.
|
|
|
|
if (isa<AllocaInst>(V)) return true;
|
|
|
|
|
2007-09-19 10:25:38 +00:00
|
|
|
// If it is a global variable it is mostly safe to load from.
|
2007-09-19 10:10:31 +00:00
|
|
|
if (const GlobalValue *GV = dyn_cast<GlobalVariable>(V))
|
2007-09-19 10:25:38 +00:00
|
|
|
// Don't try to evaluate aliases. External weak GV can be null.
|
2007-09-19 10:10:31 +00:00
|
|
|
return !isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage();
|
2004-09-19 19:18:10 +00:00
|
|
|
|
|
|
|
// Otherwise, be a little bit agressive by scanning the local block where we
|
|
|
|
// want to check to see if the pointer is already being loaded or stored
|
2004-09-20 06:42:58 +00:00
|
|
|
// from/to. If so, the previous load or store would have already trapped,
|
|
|
|
// so there is no harm doing an extra load (also, CSE will later eliminate
|
|
|
|
// the load entirely).
|
2004-09-19 19:18:10 +00:00
|
|
|
BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin();
|
|
|
|
|
2004-09-20 06:42:58 +00:00
|
|
|
while (BBI != E) {
|
2004-09-19 19:18:10 +00:00
|
|
|
--BBI;
|
|
|
|
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
|
|
|
|
if (LI->getOperand(0) == V) return true;
|
|
|
|
} else if (StoreInst *SI = dyn_cast<StoreInst>(BBI))
|
|
|
|
if (SI->getOperand(1) == V) return true;
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-09-20 06:42:58 +00:00
|
|
|
}
|
2004-09-19 19:18:10 +00:00
|
|
|
return false;
|
2004-09-19 18:43:46 +00:00
|
|
|
}
|
|
|
|
|
2007-08-11 18:48:48 +00:00
|
|
|
/// GetUnderlyingObject - Trace through a series of getelementptrs and bitcasts
|
|
|
|
/// until we find the underlying object a pointer is referring to or something
|
|
|
|
/// we don't understand. Note that the returned pointer may be offset from the
|
|
|
|
/// input, because we ignore GEP indices.
|
|
|
|
static Value *GetUnderlyingObject(Value *Ptr) {
|
|
|
|
while (1) {
|
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
|
|
|
|
if (CE->getOpcode() == Instruction::BitCast ||
|
|
|
|
CE->getOpcode() == Instruction::GetElementPtr)
|
|
|
|
Ptr = CE->getOperand(0);
|
|
|
|
else
|
|
|
|
return Ptr;
|
|
|
|
} else if (BitCastInst *BCI = dyn_cast<BitCastInst>(Ptr)) {
|
|
|
|
Ptr = BCI->getOperand(0);
|
|
|
|
} else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
|
|
|
|
Ptr = GEP->getOperand(0);
|
|
|
|
} else {
|
|
|
|
return Ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-26 05:06:25 +00:00
|
|
|
Instruction *InstCombiner::visitLoadInst(LoadInst &LI) {
|
|
|
|
Value *Op = LI.getOperand(0);
|
2004-01-12 04:13:56 +00:00
|
|
|
|
2007-07-20 16:34:21 +00:00
|
|
|
// Attempt to improve the alignment.
|
2008-04-10 18:43:06 +00:00
|
|
|
unsigned KnownAlign = GetOrEnforceKnownAlignment(Op);
|
|
|
|
if (KnownAlign >
|
|
|
|
(LI.getAlignment() == 0 ? TD->getABITypeAlignment(LI.getType()) :
|
|
|
|
LI.getAlignment()))
|
2007-07-20 16:34:21 +00:00
|
|
|
LI.setAlignment(KnownAlign);
|
|
|
|
|
2005-05-01 04:24:53 +00:00
|
|
|
// load (cast X) --> cast (load X) iff safe
|
2006-11-02 20:25:50 +00:00
|
|
|
if (isa<CastInst>(Op))
|
2007-10-18 19:52:32 +00:00
|
|
|
if (Instruction *Res = InstCombineLoadCast(*this, LI, TD))
|
2005-05-01 04:24:53 +00:00
|
|
|
return Res;
|
|
|
|
|
|
|
|
// None of the following transforms are legal for volatile loads.
|
|
|
|
if (LI.isVolatile()) return 0;
|
2005-09-12 22:00:15 +00:00
|
|
|
|
|
|
|
if (&LI.getParent()->front() != &LI) {
|
|
|
|
BasicBlock::iterator BBI = &LI; --BBI;
|
2005-09-12 22:21:03 +00:00
|
|
|
// If the instruction immediately before this is a store to the same
|
|
|
|
// address, do a simple form of store->load forwarding.
|
2005-09-12 22:00:15 +00:00
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(BBI))
|
|
|
|
if (SI->getOperand(1) == LI.getOperand(0))
|
|
|
|
return ReplaceInstUsesWith(LI, SI->getOperand(0));
|
2005-09-12 22:21:03 +00:00
|
|
|
if (LoadInst *LIB = dyn_cast<LoadInst>(BBI))
|
|
|
|
if (LIB->getOperand(0) == LI.getOperand(0))
|
|
|
|
return ReplaceInstUsesWith(LI, LIB);
|
2005-09-12 22:00:15 +00:00
|
|
|
}
|
2005-05-01 04:24:53 +00:00
|
|
|
|
2007-12-29 07:56:53 +00:00
|
|
|
if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) {
|
|
|
|
const Value *GEPI0 = GEPI->getOperand(0);
|
|
|
|
// TODO: Consider a target hook for valid address spaces for this xform.
|
|
|
|
if (isa<ConstantPointerNull>(GEPI0) &&
|
|
|
|
cast<PointerType>(GEPI0->getType())->getAddressSpace() == 0) {
|
2005-05-01 04:24:53 +00:00
|
|
|
// Insert a new store to null instruction before the load to indicate
|
|
|
|
// that this code is not reachable. We do this instead of inserting
|
|
|
|
// an unreachable instruction directly because we cannot modify the
|
|
|
|
// CFG.
|
|
|
|
new StoreInst(UndefValue::get(LI.getType()),
|
|
|
|
Constant::getNullValue(Op->getType()), &LI);
|
|
|
|
return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
|
|
|
|
}
|
2007-12-29 07:56:53 +00:00
|
|
|
}
|
2005-05-01 04:24:53 +00:00
|
|
|
|
2004-10-16 18:11:37 +00:00
|
|
|
if (Constant *C = dyn_cast<Constant>(Op)) {
|
2005-05-01 04:24:53 +00:00
|
|
|
// load null/undef -> undef
|
2007-12-29 07:56:53 +00:00
|
|
|
// TODO: Consider a target hook for valid address spaces for this xform.
|
|
|
|
if (isa<UndefValue>(C) || (C->isNullValue() &&
|
|
|
|
cast<PointerType>(Op->getType())->getAddressSpace() == 0)) {
|
2004-10-18 02:59:09 +00:00
|
|
|
// Insert a new store to null instruction before the load to indicate that
|
|
|
|
// this code is not reachable. We do this instead of inserting an
|
|
|
|
// unreachable instruction directly because we cannot modify the CFG.
|
2005-05-01 04:24:53 +00:00
|
|
|
new StoreInst(UndefValue::get(LI.getType()),
|
|
|
|
Constant::getNullValue(Op->getType()), &LI);
|
2004-10-16 18:11:37 +00:00
|
|
|
return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
|
2004-10-18 02:59:09 +00:00
|
|
|
}
|
2003-06-26 05:06:25 +00:00
|
|
|
|
2004-10-16 18:11:37 +00:00
|
|
|
// Instcombine load (constant global) into the value loaded.
|
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op))
|
2007-01-30 20:08:39 +00:00
|
|
|
if (GV->isConstant() && !GV->isDeclaration())
|
2004-10-16 18:11:37 +00:00
|
|
|
return ReplaceInstUsesWith(LI, GV->getInitializer());
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-10-16 18:11:37 +00:00
|
|
|
// Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded.
|
2008-02-20 11:26:25 +00:00
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) {
|
2004-10-16 18:11:37 +00:00
|
|
|
if (CE->getOpcode() == Instruction::GetElementPtr) {
|
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0)))
|
2007-01-30 20:08:39 +00:00
|
|
|
if (GV->isConstant() && !GV->isDeclaration())
|
2005-09-26 05:28:06 +00:00
|
|
|
if (Constant *V =
|
|
|
|
ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE))
|
2004-10-16 18:11:37 +00:00
|
|
|
return ReplaceInstUsesWith(LI, V);
|
2005-05-01 04:24:53 +00:00
|
|
|
if (CE->getOperand(0)->isNullValue()) {
|
|
|
|
// Insert a new store to null instruction before the load to indicate
|
|
|
|
// that this code is not reachable. We do this instead of inserting
|
|
|
|
// an unreachable instruction directly because we cannot modify the
|
|
|
|
// CFG.
|
|
|
|
new StoreInst(UndefValue::get(LI.getType()),
|
|
|
|
Constant::getNullValue(Op->getType()), &LI);
|
|
|
|
return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
|
|
|
|
}
|
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
} else if (CE->isCast()) {
|
2007-10-18 19:52:32 +00:00
|
|
|
if (Instruction *Res = InstCombineLoadCast(*this, LI, TD))
|
2004-10-16 18:11:37 +00:00
|
|
|
return Res;
|
|
|
|
}
|
2008-02-20 11:26:25 +00:00
|
|
|
}
|
2004-10-16 18:11:37 +00:00
|
|
|
}
|
2007-08-11 18:48:48 +00:00
|
|
|
|
|
|
|
// If this load comes from anywhere in a constant global, and if the global
|
|
|
|
// is all undef or zero, we know what it loads.
|
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GetUnderlyingObject(Op))) {
|
|
|
|
if (GV->isConstant() && GV->hasInitializer()) {
|
|
|
|
if (GV->getInitializer()->isNullValue())
|
|
|
|
return ReplaceInstUsesWith(LI, Constant::getNullValue(LI.getType()));
|
|
|
|
else if (isa<UndefValue>(GV->getInitializer()))
|
|
|
|
return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
|
|
|
|
}
|
|
|
|
}
|
2004-04-08 20:39:49 +00:00
|
|
|
|
2005-05-01 04:24:53 +00:00
|
|
|
if (Op->hasOneUse()) {
|
2004-09-19 18:43:46 +00:00
|
|
|
// Change select and PHI nodes to select values instead of addresses: this
|
|
|
|
// helps alias analysis out a lot, allows many others simplifications, and
|
|
|
|
// exposes redundancy in the code.
|
|
|
|
//
|
|
|
|
// Note that we cannot do the transformation unless we know that the
|
|
|
|
// introduced loads cannot trap! Something like this is valid as long as
|
|
|
|
// the condition is always false: load (select bool %C, int* null, int* %G),
|
|
|
|
// but it would not be valid if we transformed it to load from null
|
|
|
|
// unconditionally.
|
|
|
|
//
|
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(Op)) {
|
|
|
|
// load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2).
|
2004-09-19 19:18:10 +00:00
|
|
|
if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) &&
|
|
|
|
isSafeToLoadUnconditionally(SI->getOperand(2), SI)) {
|
2004-09-19 18:43:46 +00:00
|
|
|
Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1),
|
2004-09-20 10:15:10 +00:00
|
|
|
SI->getOperand(1)->getName()+".val"), LI);
|
2004-09-19 18:43:46 +00:00
|
|
|
Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2),
|
2004-09-20 10:15:10 +00:00
|
|
|
SI->getOperand(2)->getName()+".val"), LI);
|
2008-04-06 20:25:17 +00:00
|
|
|
return SelectInst::Create(SI->getCondition(), V1, V2);
|
2004-09-19 18:43:46 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 15:46:00 +00:00
|
|
|
// load (select (cond, null, P)) -> load P
|
|
|
|
if (Constant *C = dyn_cast<Constant>(SI->getOperand(1)))
|
|
|
|
if (C->isNullValue()) {
|
|
|
|
LI.setOperand(0, SI->getOperand(2));
|
|
|
|
return &LI;
|
|
|
|
}
|
|
|
|
|
|
|
|
// load (select (cond, P, null)) -> load P
|
|
|
|
if (Constant *C = dyn_cast<Constant>(SI->getOperand(2)))
|
|
|
|
if (C->isNullValue()) {
|
|
|
|
LI.setOperand(0, SI->getOperand(1));
|
|
|
|
return &LI;
|
|
|
|
}
|
2004-09-19 18:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
2003-06-26 05:06:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-01-19 21:20:31 +00:00
|
|
|
/// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P
|
2005-01-31 05:51:45 +00:00
|
|
|
/// when possible.
|
|
|
|
static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) {
|
|
|
|
User *CI = cast<User>(SI.getOperand(1));
|
|
|
|
Value *CastOp = CI->getOperand(0);
|
|
|
|
|
|
|
|
const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType();
|
|
|
|
if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) {
|
|
|
|
const Type *SrcPTy = SrcTy->getElementType();
|
|
|
|
|
2007-01-22 05:51:25 +00:00
|
|
|
if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) {
|
2005-01-31 05:51:45 +00:00
|
|
|
// If the source is an array, the code below will not succeed. Check to
|
|
|
|
// see if a trivial 'gep P, 0, 0' will help matters. Only do this for
|
|
|
|
// constants.
|
|
|
|
if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy))
|
|
|
|
if (Constant *CSrc = dyn_cast<Constant>(CastOp))
|
|
|
|
if (ASrcTy->getNumElements() != 0) {
|
2007-01-31 04:40:53 +00:00
|
|
|
Value* Idxs[2];
|
|
|
|
Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty);
|
|
|
|
CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2);
|
2005-01-31 05:51:45 +00:00
|
|
|
SrcTy = cast<PointerType>(CastOp->getType());
|
|
|
|
SrcPTy = SrcTy->getElementType();
|
|
|
|
}
|
|
|
|
|
2007-01-20 23:35:48 +00:00
|
|
|
if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) &&
|
|
|
|
IC.getTargetData().getTypeSizeInBits(SrcPTy) ==
|
|
|
|
IC.getTargetData().getTypeSizeInBits(DestPTy)) {
|
2005-01-31 05:51:45 +00:00
|
|
|
|
|
|
|
// Okay, we are casting from one integer or pointer type to another of
|
2007-01-18 18:54:33 +00:00
|
|
|
// the same size. Instead of casting the pointer before
|
|
|
|
// the store, cast the value to be stored.
|
2005-01-31 05:51:45 +00:00
|
|
|
Value *NewCast;
|
2006-12-12 23:36:14 +00:00
|
|
|
Value *SIOp0 = SI.getOperand(0);
|
2007-01-18 18:54:33 +00:00
|
|
|
Instruction::CastOps opcode = Instruction::BitCast;
|
|
|
|
const Type* CastSrcTy = SIOp0->getType();
|
|
|
|
const Type* CastDstTy = SrcPTy;
|
|
|
|
if (isa<PointerType>(CastDstTy)) {
|
|
|
|
if (CastSrcTy->isInteger())
|
2006-12-12 23:36:14 +00:00
|
|
|
opcode = Instruction::IntToPtr;
|
2007-01-20 23:35:48 +00:00
|
|
|
} else if (isa<IntegerType>(CastDstTy)) {
|
2006-12-13 18:21:21 +00:00
|
|
|
if (isa<PointerType>(SIOp0->getType()))
|
2006-12-12 23:36:14 +00:00
|
|
|
opcode = Instruction::PtrToInt;
|
|
|
|
}
|
|
|
|
if (Constant *C = dyn_cast<Constant>(SIOp0))
|
2007-01-18 18:54:33 +00:00
|
|
|
NewCast = ConstantExpr::getCast(opcode, C, CastDstTy);
|
2005-01-31 05:51:45 +00:00
|
|
|
else
|
2006-11-27 01:05:10 +00:00
|
|
|
NewCast = IC.InsertNewInstBefore(
|
2007-01-18 18:54:33 +00:00
|
|
|
CastInst::create(opcode, SIOp0, CastDstTy, SIOp0->getName()+".c"),
|
|
|
|
SI);
|
2005-01-31 05:51:45 +00:00
|
|
|
return new StoreInst(NewCast, CastOp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-01-31 05:36:43 +00:00
|
|
|
Instruction *InstCombiner::visitStoreInst(StoreInst &SI) {
|
|
|
|
Value *Val = SI.getOperand(0);
|
|
|
|
Value *Ptr = SI.getOperand(1);
|
|
|
|
|
|
|
|
if (isa<UndefValue>(Ptr)) { // store X, undef -> noop (even if volatile)
|
Use EraseInstFromFunction in a few cases to put the uses of the removed
instruction onto the worklist (in case they are now dead).
Add a really trivial local DSE implementation to help out bitfield code.
We now fold this:
struct S {
unsigned char a : 1, b : 1, c : 1, d : 2, e : 3;
S();
};
S::S() : a(0), b(0), c(1), d(0), e(6) {}
to this:
void %_ZN1SC1Ev(%struct.S* %this) {
entry:
%tmp.1 = getelementptr %struct.S* %this, int 0, uint 0
store ubyte 38, ubyte* %tmp.1
ret void
}
much earlier (in gccas instead of only in gccld after DSE runs).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@26050 91177308-0d34-0410-b5e6-96231b3b80d8
2006-02-08 03:25:32 +00:00
|
|
|
EraseInstFromFunction(SI);
|
2005-01-31 05:36:43 +00:00
|
|
|
++NumCombined;
|
|
|
|
return 0;
|
|
|
|
}
|
2007-01-15 06:51:56 +00:00
|
|
|
|
|
|
|
// If the RHS is an alloca with a single use, zapify the store, making the
|
|
|
|
// alloca dead.
|
2008-04-29 04:58:38 +00:00
|
|
|
if (Ptr->hasOneUse() && !SI.isVolatile()) {
|
2007-01-15 06:51:56 +00:00
|
|
|
if (isa<AllocaInst>(Ptr)) {
|
|
|
|
EraseInstFromFunction(SI);
|
|
|
|
++NumCombined;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr))
|
|
|
|
if (isa<AllocaInst>(GEP->getOperand(0)) &&
|
|
|
|
GEP->getOperand(0)->hasOneUse()) {
|
|
|
|
EraseInstFromFunction(SI);
|
|
|
|
++NumCombined;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2005-01-31 05:36:43 +00:00
|
|
|
|
2007-07-20 16:34:21 +00:00
|
|
|
// Attempt to improve the alignment.
|
2008-04-10 18:43:06 +00:00
|
|
|
unsigned KnownAlign = GetOrEnforceKnownAlignment(Ptr);
|
|
|
|
if (KnownAlign >
|
|
|
|
(SI.getAlignment() == 0 ? TD->getABITypeAlignment(Val->getType()) :
|
|
|
|
SI.getAlignment()))
|
2007-07-20 16:34:21 +00:00
|
|
|
SI.setAlignment(KnownAlign);
|
|
|
|
|
Use EraseInstFromFunction in a few cases to put the uses of the removed
instruction onto the worklist (in case they are now dead).
Add a really trivial local DSE implementation to help out bitfield code.
We now fold this:
struct S {
unsigned char a : 1, b : 1, c : 1, d : 2, e : 3;
S();
};
S::S() : a(0), b(0), c(1), d(0), e(6) {}
to this:
void %_ZN1SC1Ev(%struct.S* %this) {
entry:
%tmp.1 = getelementptr %struct.S* %this, int 0, uint 0
store ubyte 38, ubyte* %tmp.1
ret void
}
much earlier (in gccas instead of only in gccld after DSE runs).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@26050 91177308-0d34-0410-b5e6-96231b3b80d8
2006-02-08 03:25:32 +00:00
|
|
|
// Do really simple DSE, to catch cases where there are several consequtive
|
|
|
|
// stores to the same location, separated by a few arithmetic operations. This
|
|
|
|
// situation often occurs with bitfield accesses.
|
|
|
|
BasicBlock::iterator BBI = &SI;
|
|
|
|
for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts;
|
|
|
|
--ScanInsts) {
|
|
|
|
--BBI;
|
|
|
|
|
|
|
|
if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) {
|
|
|
|
// Prev store isn't volatile, and stores to the same location?
|
|
|
|
if (!PrevSI->isVolatile() && PrevSI->getOperand(1) == SI.getOperand(1)) {
|
|
|
|
++NumDeadStore;
|
|
|
|
++BBI;
|
|
|
|
EraseInstFromFunction(*PrevSI);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-05-26 19:19:20 +00:00
|
|
|
// If this is a load, we have to stop. However, if the loaded value is from
|
|
|
|
// the pointer we're loading and is producing the pointer we're storing,
|
|
|
|
// then *this* store is dead (X = load P; store X -> P).
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
|
2007-09-07 05:33:03 +00:00
|
|
|
if (LI == Val && LI->getOperand(0) == Ptr && !SI.isVolatile()) {
|
2006-05-26 19:19:20 +00:00
|
|
|
EraseInstFromFunction(SI);
|
|
|
|
++NumCombined;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// Otherwise, this is a load from some other location. Stores before it
|
|
|
|
// may not be dead.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
Use EraseInstFromFunction in a few cases to put the uses of the removed
instruction onto the worklist (in case they are now dead).
Add a really trivial local DSE implementation to help out bitfield code.
We now fold this:
struct S {
unsigned char a : 1, b : 1, c : 1, d : 2, e : 3;
S();
};
S::S() : a(0), b(0), c(1), d(0), e(6) {}
to this:
void %_ZN1SC1Ev(%struct.S* %this) {
entry:
%tmp.1 = getelementptr %struct.S* %this, int 0, uint 0
store ubyte 38, ubyte* %tmp.1
ret void
}
much earlier (in gccas instead of only in gccld after DSE runs).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@26050 91177308-0d34-0410-b5e6-96231b3b80d8
2006-02-08 03:25:32 +00:00
|
|
|
// Don't skip over loads or things that can modify memory.
|
2006-05-26 19:19:20 +00:00
|
|
|
if (BBI->mayWriteToMemory())
|
Use EraseInstFromFunction in a few cases to put the uses of the removed
instruction onto the worklist (in case they are now dead).
Add a really trivial local DSE implementation to help out bitfield code.
We now fold this:
struct S {
unsigned char a : 1, b : 1, c : 1, d : 2, e : 3;
S();
};
S::S() : a(0), b(0), c(1), d(0), e(6) {}
to this:
void %_ZN1SC1Ev(%struct.S* %this) {
entry:
%tmp.1 = getelementptr %struct.S* %this, int 0, uint 0
store ubyte 38, ubyte* %tmp.1
ret void
}
much earlier (in gccas instead of only in gccld after DSE runs).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@26050 91177308-0d34-0410-b5e6-96231b3b80d8
2006-02-08 03:25:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (SI.isVolatile()) return 0; // Don't hack volatile stores.
|
2005-01-31 05:36:43 +00:00
|
|
|
|
|
|
|
// store X, null -> turns into 'unreachable' in SimplifyCFG
|
|
|
|
if (isa<ConstantPointerNull>(Ptr)) {
|
|
|
|
if (!isa<UndefValue>(Val)) {
|
|
|
|
SI.setOperand(0, UndefValue::get(Val->getType()));
|
|
|
|
if (Instruction *U = dyn_cast<Instruction>(Val))
|
2007-03-02 21:28:56 +00:00
|
|
|
AddToWorkList(U); // Dropped a use.
|
2005-01-31 05:36:43 +00:00
|
|
|
++NumCombined;
|
|
|
|
}
|
|
|
|
return 0; // Do not modify these!
|
|
|
|
}
|
|
|
|
|
|
|
|
// store undef, Ptr -> noop
|
|
|
|
if (isa<UndefValue>(Val)) {
|
Use EraseInstFromFunction in a few cases to put the uses of the removed
instruction onto the worklist (in case they are now dead).
Add a really trivial local DSE implementation to help out bitfield code.
We now fold this:
struct S {
unsigned char a : 1, b : 1, c : 1, d : 2, e : 3;
S();
};
S::S() : a(0), b(0), c(1), d(0), e(6) {}
to this:
void %_ZN1SC1Ev(%struct.S* %this) {
entry:
%tmp.1 = getelementptr %struct.S* %this, int 0, uint 0
store ubyte 38, ubyte* %tmp.1
ret void
}
much earlier (in gccas instead of only in gccld after DSE runs).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@26050 91177308-0d34-0410-b5e6-96231b3b80d8
2006-02-08 03:25:32 +00:00
|
|
|
EraseInstFromFunction(SI);
|
2005-01-31 05:36:43 +00:00
|
|
|
++NumCombined;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-01-31 05:51:45 +00:00
|
|
|
// If the pointer destination is a cast, see if we can fold the cast into the
|
|
|
|
// source instead.
|
2006-11-02 20:25:50 +00:00
|
|
|
if (isa<CastInst>(Ptr))
|
2005-01-31 05:51:45 +00:00
|
|
|
if (Instruction *Res = InstCombineStoreToCast(*this, SI))
|
|
|
|
return Res;
|
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
|
2006-11-27 01:05:10 +00:00
|
|
|
if (CE->isCast())
|
2005-01-31 05:51:45 +00:00
|
|
|
if (Instruction *Res = InstCombineStoreToCast(*this, SI))
|
|
|
|
return Res;
|
|
|
|
|
2005-09-12 23:23:25 +00:00
|
|
|
|
|
|
|
// If this store is the last instruction in the basic block, and if the block
|
|
|
|
// ends with an unconditional branch, try to move it to the successor block.
|
Use EraseInstFromFunction in a few cases to put the uses of the removed
instruction onto the worklist (in case they are now dead).
Add a really trivial local DSE implementation to help out bitfield code.
We now fold this:
struct S {
unsigned char a : 1, b : 1, c : 1, d : 2, e : 3;
S();
};
S::S() : a(0), b(0), c(1), d(0), e(6) {}
to this:
void %_ZN1SC1Ev(%struct.S* %this) {
entry:
%tmp.1 = getelementptr %struct.S* %this, int 0, uint 0
store ubyte 38, ubyte* %tmp.1
ret void
}
much earlier (in gccas instead of only in gccld after DSE runs).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@26050 91177308-0d34-0410-b5e6-96231b3b80d8
2006-02-08 03:25:32 +00:00
|
|
|
BBI = &SI; ++BBI;
|
2005-09-12 23:23:25 +00:00
|
|
|
if (BranchInst *BI = dyn_cast<BranchInst>(BBI))
|
2007-04-15 00:07:55 +00:00
|
|
|
if (BI->isUnconditional())
|
|
|
|
if (SimplifyStoreAtEndOfBlock(SI))
|
|
|
|
return 0; // xform done!
|
2005-09-12 23:23:25 +00:00
|
|
|
|
2005-01-31 05:36:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-15 00:07:55 +00:00
|
|
|
/// SimplifyStoreAtEndOfBlock - Turn things like:
|
|
|
|
/// if () { *P = v1; } else { *P = v2 }
|
|
|
|
/// into a phi node with a store in the successor.
|
|
|
|
///
|
2007-04-15 01:02:18 +00:00
|
|
|
/// Simplify things like:
|
|
|
|
/// *P = v1; if () { *P = v2; }
|
|
|
|
/// into a phi node with a store in the successor.
|
|
|
|
///
|
2007-04-15 00:07:55 +00:00
|
|
|
bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) {
|
|
|
|
BasicBlock *StoreBB = SI.getParent();
|
|
|
|
|
|
|
|
// Check to see if the successor block has exactly two incoming edges. If
|
|
|
|
// so, see if the other predecessor contains a store to the same location.
|
|
|
|
// if so, insert a PHI node (if needed) and move the stores down.
|
2007-04-15 01:02:18 +00:00
|
|
|
BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0);
|
2007-04-15 00:07:55 +00:00
|
|
|
|
|
|
|
// Determine whether Dest has exactly two predecessors and, if so, compute
|
|
|
|
// the other predecessor.
|
2007-04-15 01:02:18 +00:00
|
|
|
pred_iterator PI = pred_begin(DestBB);
|
|
|
|
BasicBlock *OtherBB = 0;
|
2007-04-15 00:07:55 +00:00
|
|
|
if (*PI != StoreBB)
|
2007-04-15 01:02:18 +00:00
|
|
|
OtherBB = *PI;
|
2007-04-15 00:07:55 +00:00
|
|
|
++PI;
|
2007-04-15 01:02:18 +00:00
|
|
|
if (PI == pred_end(DestBB))
|
2007-04-15 00:07:55 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (*PI != StoreBB) {
|
2007-04-15 01:02:18 +00:00
|
|
|
if (OtherBB)
|
2007-04-15 00:07:55 +00:00
|
|
|
return false;
|
2007-04-15 01:02:18 +00:00
|
|
|
OtherBB = *PI;
|
2007-04-15 00:07:55 +00:00
|
|
|
}
|
2007-04-15 01:02:18 +00:00
|
|
|
if (++PI != pred_end(DestBB))
|
2007-04-15 00:07:55 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
2007-04-15 01:02:18 +00:00
|
|
|
// Verify that the other block ends in a branch and is not otherwise empty.
|
|
|
|
BasicBlock::iterator BBI = OtherBB->getTerminator();
|
2007-04-15 00:07:55 +00:00
|
|
|
BranchInst *OtherBr = dyn_cast<BranchInst>(BBI);
|
2007-04-15 01:02:18 +00:00
|
|
|
if (!OtherBr || BBI == OtherBB->begin())
|
2007-04-15 00:07:55 +00:00
|
|
|
return false;
|
|
|
|
|
2007-04-15 01:02:18 +00:00
|
|
|
// If the other block ends in an unconditional branch, check for the 'if then
|
|
|
|
// else' case. there is an instruction before the branch.
|
|
|
|
StoreInst *OtherStore = 0;
|
|
|
|
if (OtherBr->isUnconditional()) {
|
|
|
|
// If this isn't a store, or isn't a store to the same location, bail out.
|
|
|
|
--BBI;
|
|
|
|
OtherStore = dyn_cast<StoreInst>(BBI);
|
|
|
|
if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1))
|
|
|
|
return false;
|
|
|
|
} else {
|
2007-05-05 22:32:24 +00:00
|
|
|
// Otherwise, the other block ended with a conditional branch. If one of the
|
2007-04-15 01:02:18 +00:00
|
|
|
// destinations is StoreBB, then we have the if/then case.
|
|
|
|
if (OtherBr->getSuccessor(0) != StoreBB &&
|
|
|
|
OtherBr->getSuccessor(1) != StoreBB)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Okay, we know that OtherBr now goes to Dest and StoreBB, so this is an
|
2007-05-05 22:32:24 +00:00
|
|
|
// if/then triangle. See if there is a store to the same ptr as SI that
|
|
|
|
// lives in OtherBB.
|
2007-04-15 01:02:18 +00:00
|
|
|
for (;; --BBI) {
|
|
|
|
// Check to see if we find the matching store.
|
|
|
|
if ((OtherStore = dyn_cast<StoreInst>(BBI))) {
|
|
|
|
if (OtherStore->getOperand(1) != SI.getOperand(1))
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
2007-05-05 22:32:24 +00:00
|
|
|
// If we find something that may be using the stored value, or if we run
|
|
|
|
// out of instructions, we can't do the xform.
|
2007-04-15 01:02:18 +00:00
|
|
|
if (isa<LoadInst>(BBI) || BBI->mayWriteToMemory() ||
|
|
|
|
BBI == OtherBB->begin())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In order to eliminate the store in OtherBr, we have to
|
|
|
|
// make sure nothing reads the stored value in StoreBB.
|
|
|
|
for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) {
|
|
|
|
// FIXME: This should really be AA driven.
|
|
|
|
if (isa<LoadInst>(I) || I->mayWriteToMemory())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2007-04-15 00:07:55 +00:00
|
|
|
|
2007-04-15 01:02:18 +00:00
|
|
|
// Insert a PHI node now if we need it.
|
2007-04-15 00:07:55 +00:00
|
|
|
Value *MergedVal = OtherStore->getOperand(0);
|
|
|
|
if (MergedVal != SI.getOperand(0)) {
|
2008-04-06 20:25:17 +00:00
|
|
|
PHINode *PN = PHINode::Create(MergedVal->getType(), "storemerge");
|
2007-04-15 00:07:55 +00:00
|
|
|
PN->reserveOperandSpace(2);
|
|
|
|
PN->addIncoming(SI.getOperand(0), SI.getParent());
|
2007-04-15 01:02:18 +00:00
|
|
|
PN->addIncoming(OtherStore->getOperand(0), OtherBB);
|
|
|
|
MergedVal = InsertNewInstBefore(PN, DestBB->front());
|
2007-04-15 00:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Advance to a place where it is safe to insert the new store and
|
|
|
|
// insert it.
|
2007-04-15 01:02:18 +00:00
|
|
|
BBI = DestBB->begin();
|
2007-04-15 00:07:55 +00:00
|
|
|
while (isa<PHINode>(BBI)) ++BBI;
|
|
|
|
InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1),
|
|
|
|
OtherStore->isVolatile()), *BBI);
|
|
|
|
|
|
|
|
// Nuke the old stores.
|
|
|
|
EraseInstFromFunction(SI);
|
|
|
|
EraseInstFromFunction(*OtherStore);
|
|
|
|
++NumCombined;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-01-31 05:36:43 +00:00
|
|
|
|
2003-06-04 04:46:00 +00:00
|
|
|
Instruction *InstCombiner::visitBranchInst(BranchInst &BI) {
|
|
|
|
// Change br (not X), label True, label False to: br X, label False, True
|
2005-06-18 17:37:34 +00:00
|
|
|
Value *X = 0;
|
2004-07-30 07:50:03 +00:00
|
|
|
BasicBlock *TrueDest;
|
|
|
|
BasicBlock *FalseDest;
|
|
|
|
if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) &&
|
|
|
|
!isa<Constant>(X)) {
|
|
|
|
// Swap Destinations and condition...
|
|
|
|
BI.setCondition(X);
|
|
|
|
BI.setSuccessor(0, FalseDest);
|
|
|
|
BI.setSuccessor(1, TrueDest);
|
|
|
|
return &BI;
|
|
|
|
}
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
// Cannonicalize fcmp_one -> fcmp_oeq
|
|
|
|
FCmpInst::Predicate FPred; Value *Y;
|
|
|
|
if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)),
|
|
|
|
TrueDest, FalseDest)))
|
|
|
|
if ((FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE ||
|
|
|
|
FPred == FCmpInst::FCMP_OGE) && BI.getCondition()->hasOneUse()) {
|
|
|
|
FCmpInst *I = cast<FCmpInst>(BI.getCondition());
|
|
|
|
FCmpInst::Predicate NewPred = FCmpInst::getInversePredicate(FPred);
|
2007-02-11 01:23:03 +00:00
|
|
|
Instruction *NewSCC = new FCmpInst(NewPred, X, Y, "", I);
|
|
|
|
NewSCC->takeName(I);
|
2006-12-23 06:05:41 +00:00
|
|
|
// Swap Destinations and condition...
|
|
|
|
BI.setCondition(NewSCC);
|
|
|
|
BI.setSuccessor(0, FalseDest);
|
|
|
|
BI.setSuccessor(1, TrueDest);
|
2007-03-02 21:28:56 +00:00
|
|
|
RemoveFromWorkList(I);
|
2007-02-11 01:23:03 +00:00
|
|
|
I->eraseFromParent();
|
2007-03-02 21:28:56 +00:00
|
|
|
AddToWorkList(NewSCC);
|
2006-12-23 06:05:41 +00:00
|
|
|
return &BI;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cannonicalize icmp_ne -> icmp_eq
|
|
|
|
ICmpInst::Predicate IPred;
|
|
|
|
if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)),
|
2004-07-30 07:50:03 +00:00
|
|
|
TrueDest, FalseDest)))
|
2006-12-23 06:05:41 +00:00
|
|
|
if ((IPred == ICmpInst::ICMP_NE || IPred == ICmpInst::ICMP_ULE ||
|
|
|
|
IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE ||
|
|
|
|
IPred == ICmpInst::ICMP_SGE) && BI.getCondition()->hasOneUse()) {
|
|
|
|
ICmpInst *I = cast<ICmpInst>(BI.getCondition());
|
|
|
|
ICmpInst::Predicate NewPred = ICmpInst::getInversePredicate(IPred);
|
2007-02-11 01:23:03 +00:00
|
|
|
Instruction *NewSCC = new ICmpInst(NewPred, X, Y, "", I);
|
|
|
|
NewSCC->takeName(I);
|
2003-06-04 05:10:11 +00:00
|
|
|
// Swap Destinations and condition...
|
2004-07-30 07:50:03 +00:00
|
|
|
BI.setCondition(NewSCC);
|
2003-06-04 05:10:11 +00:00
|
|
|
BI.setSuccessor(0, FalseDest);
|
|
|
|
BI.setSuccessor(1, TrueDest);
|
2007-03-02 21:28:56 +00:00
|
|
|
RemoveFromWorkList(I);
|
2007-02-11 01:23:03 +00:00
|
|
|
I->eraseFromParent();;
|
2007-03-02 21:28:56 +00:00
|
|
|
AddToWorkList(NewSCC);
|
2003-06-04 05:10:11 +00:00
|
|
|
return &BI;
|
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2003-06-04 04:46:00 +00:00
|
|
|
return 0;
|
2004-07-03 00:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) {
|
|
|
|
Value *Cond = SI.getCondition();
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(Cond)) {
|
|
|
|
if (I->getOpcode() == Instruction::Add)
|
|
|
|
if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
|
|
|
|
// change 'switch (X+4) case 1:' into 'switch (X) case -3'
|
|
|
|
for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2)
|
2004-10-16 18:11:37 +00:00
|
|
|
SI.setOperand(i,ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)),
|
2004-07-03 00:26:11 +00:00
|
|
|
AddRHS));
|
|
|
|
SI.setOperand(0, I->getOperand(0));
|
2007-03-02 21:28:56 +00:00
|
|
|
AddToWorkList(I);
|
2004-07-03 00:26:11 +00:00
|
|
|
return &SI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2003-06-04 04:46:00 +00:00
|
|
|
}
|
2002-11-04 16:18:53 +00:00
|
|
|
|
2006-03-05 00:22:33 +00:00
|
|
|
/// CheapToScalarize - Return true if the value is cheaper to scalarize than it
|
|
|
|
/// is to leave as a vector operation.
|
|
|
|
static bool CheapToScalarize(Value *V, bool isConstant) {
|
|
|
|
if (isa<ConstantAggregateZero>(V))
|
|
|
|
return true;
|
2007-02-15 02:26:10 +00:00
|
|
|
if (ConstantVector *C = dyn_cast<ConstantVector>(V)) {
|
2006-03-05 00:22:33 +00:00
|
|
|
if (isConstant) return true;
|
|
|
|
// If all elts are the same, we can extract.
|
|
|
|
Constant *Op0 = C->getOperand(0);
|
|
|
|
for (unsigned i = 1; i < C->getNumOperands(); ++i)
|
|
|
|
if (C->getOperand(i) != Op0)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Instruction *I = dyn_cast<Instruction>(V);
|
|
|
|
if (!I) return false;
|
|
|
|
|
|
|
|
// Insert element gets simplified to the inserted element or is deleted if
|
|
|
|
// this is constant idx extract element and its a constant idx insertelt.
|
|
|
|
if (I->getOpcode() == Instruction::InsertElement && isConstant &&
|
|
|
|
isa<ConstantInt>(I->getOperand(2)))
|
|
|
|
return true;
|
|
|
|
if (I->getOpcode() == Instruction::Load && I->hasOneUse())
|
|
|
|
return true;
|
|
|
|
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I))
|
|
|
|
if (BO->hasOneUse() &&
|
|
|
|
(CheapToScalarize(BO->getOperand(0), isConstant) ||
|
|
|
|
CheapToScalarize(BO->getOperand(1), isConstant)))
|
|
|
|
return true;
|
2006-12-23 06:05:41 +00:00
|
|
|
if (CmpInst *CI = dyn_cast<CmpInst>(I))
|
|
|
|
if (CI->hasOneUse() &&
|
|
|
|
(CheapToScalarize(CI->getOperand(0), isConstant) ||
|
|
|
|
CheapToScalarize(CI->getOperand(1), isConstant)))
|
|
|
|
return true;
|
2006-03-05 00:22:33 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-02-14 05:52:17 +00:00
|
|
|
/// Read and decode a shufflevector mask.
|
|
|
|
///
|
|
|
|
/// It turns undef elements into values that are larger than the number of
|
|
|
|
/// elements in the input.
|
2006-05-25 23:48:38 +00:00
|
|
|
static std::vector<unsigned> getShuffleMask(const ShuffleVectorInst *SVI) {
|
|
|
|
unsigned NElts = SVI->getType()->getNumElements();
|
|
|
|
if (isa<ConstantAggregateZero>(SVI->getOperand(2)))
|
|
|
|
return std::vector<unsigned>(NElts, 0);
|
|
|
|
if (isa<UndefValue>(SVI->getOperand(2)))
|
|
|
|
return std::vector<unsigned>(NElts, 2*NElts);
|
|
|
|
|
|
|
|
std::vector<unsigned> Result;
|
2007-02-15 02:26:10 +00:00
|
|
|
const ConstantVector *CP = cast<ConstantVector>(SVI->getOperand(2));
|
2006-05-25 23:48:38 +00:00
|
|
|
for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
|
|
|
|
if (isa<UndefValue>(CP->getOperand(i)))
|
|
|
|
Result.push_back(NElts*2); // undef -> 8
|
|
|
|
else
|
2006-10-20 07:07:24 +00:00
|
|
|
Result.push_back(cast<ConstantInt>(CP->getOperand(i))->getZExtValue());
|
2006-05-25 23:48:38 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2006-03-31 23:01:56 +00:00
|
|
|
/// FindScalarElement - Given a vector and an element number, see if the scalar
|
|
|
|
/// value is already around as a register, for example if it were inserted then
|
|
|
|
/// extracted from the vector.
|
|
|
|
static Value *FindScalarElement(Value *V, unsigned EltNo) {
|
2007-02-15 02:26:10 +00:00
|
|
|
assert(isa<VectorType>(V->getType()) && "Not looking at a vector?");
|
|
|
|
const VectorType *PTy = cast<VectorType>(V->getType());
|
2006-04-10 23:06:36 +00:00
|
|
|
unsigned Width = PTy->getNumElements();
|
|
|
|
if (EltNo >= Width) // Out of range access.
|
2006-03-31 23:01:56 +00:00
|
|
|
return UndefValue::get(PTy->getElementType());
|
|
|
|
|
|
|
|
if (isa<UndefValue>(V))
|
|
|
|
return UndefValue::get(PTy->getElementType());
|
|
|
|
else if (isa<ConstantAggregateZero>(V))
|
|
|
|
return Constant::getNullValue(PTy->getElementType());
|
2007-02-15 02:26:10 +00:00
|
|
|
else if (ConstantVector *CP = dyn_cast<ConstantVector>(V))
|
2006-03-31 23:01:56 +00:00
|
|
|
return CP->getOperand(EltNo);
|
|
|
|
else if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) {
|
|
|
|
// If this is an insert to a variable element, we don't know what it is.
|
2006-10-20 07:07:24 +00:00
|
|
|
if (!isa<ConstantInt>(III->getOperand(2)))
|
|
|
|
return 0;
|
|
|
|
unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue();
|
2006-03-31 23:01:56 +00:00
|
|
|
|
|
|
|
// If this is an insert to the element we are looking for, return the
|
|
|
|
// inserted value.
|
2006-10-20 07:07:24 +00:00
|
|
|
if (EltNo == IIElt)
|
|
|
|
return III->getOperand(1);
|
2006-03-31 23:01:56 +00:00
|
|
|
|
|
|
|
// Otherwise, the insertelement doesn't modify the value, recurse on its
|
|
|
|
// vector input.
|
|
|
|
return FindScalarElement(III->getOperand(0), EltNo);
|
2006-04-10 23:06:36 +00:00
|
|
|
} else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) {
|
2006-05-25 23:48:38 +00:00
|
|
|
unsigned InEl = getShuffleMask(SVI)[EltNo];
|
|
|
|
if (InEl < Width)
|
|
|
|
return FindScalarElement(SVI->getOperand(0), InEl);
|
|
|
|
else if (InEl < Width*2)
|
|
|
|
return FindScalarElement(SVI->getOperand(1), InEl - Width);
|
|
|
|
else
|
|
|
|
return UndefValue::get(PTy->getElementType());
|
2006-03-31 23:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we don't know.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-01-13 22:48:06 +00:00
|
|
|
Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) {
|
2006-03-31 23:01:56 +00:00
|
|
|
|
2007-07-16 14:29:03 +00:00
|
|
|
// If vector val is undef, replace extract with scalar undef.
|
2006-03-31 18:25:14 +00:00
|
|
|
if (isa<UndefValue>(EI.getOperand(0)))
|
|
|
|
return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
|
|
|
|
|
2007-07-16 14:29:03 +00:00
|
|
|
// If vector val is constant 0, replace extract with scalar 0.
|
2006-03-31 18:25:14 +00:00
|
|
|
if (isa<ConstantAggregateZero>(EI.getOperand(0)))
|
|
|
|
return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType()));
|
|
|
|
|
2007-02-15 02:26:10 +00:00
|
|
|
if (ConstantVector *C = dyn_cast<ConstantVector>(EI.getOperand(0))) {
|
2007-07-16 14:29:03 +00:00
|
|
|
// If vector val is constant with uniform operands, replace EI
|
2006-01-13 22:48:06 +00:00
|
|
|
// with that operand
|
2006-03-05 00:22:33 +00:00
|
|
|
Constant *op0 = C->getOperand(0);
|
2006-01-13 22:48:06 +00:00
|
|
|
for (unsigned i = 1; i < C->getNumOperands(); ++i)
|
2006-03-05 00:22:33 +00:00
|
|
|
if (C->getOperand(i) != op0) {
|
|
|
|
op0 = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (op0)
|
|
|
|
return ReplaceInstUsesWith(EI, op0);
|
2006-01-13 22:48:06 +00:00
|
|
|
}
|
2006-03-05 00:22:33 +00:00
|
|
|
|
2006-03-31 23:01:56 +00:00
|
|
|
// If extracting a specified index from the vector, see if we can recursively
|
|
|
|
// find a previously computed scalar that was inserted into the vector.
|
2006-10-20 07:07:24 +00:00
|
|
|
if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) {
|
2007-04-09 01:37:55 +00:00
|
|
|
unsigned IndexVal = IdxC->getZExtValue();
|
|
|
|
unsigned VectorWidth =
|
|
|
|
cast<VectorType>(EI.getOperand(0)->getType())->getNumElements();
|
|
|
|
|
|
|
|
// If this is extracting an invalid index, turn this into undef, to avoid
|
|
|
|
// crashing the code below.
|
|
|
|
if (IndexVal >= VectorWidth)
|
|
|
|
return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
|
|
|
|
|
add a new SimplifyDemandedVectorElts method, which works similarly to
SimplifyDemandedBits. The idea is that some operations can be simplified if
not all of the computed elements are needed. Some targets (like x86) have a
large number of intrinsics that operate on a single element, but pass other
elts through unmodified. If those other elements are not needed, the
intrinsics can be simplified to scalar operations, and insertelement ops can
be removed.
This turns (f.e.):
ushort %Convert_sse(float %f) {
%tmp = insertelement <4 x float> undef, float %f, uint 0 ; <<4 x float>> [#uses=1]
%tmp10 = insertelement <4 x float> %tmp, float 0.000000e+00, uint 1 ; <<4 x float>> [#uses=1]
%tmp11 = insertelement <4 x float> %tmp10, float 0.000000e+00, uint 2 ; <<4 x float>> [#uses=1]
%tmp12 = insertelement <4 x float> %tmp11, float 0.000000e+00, uint 3 ; <<4 x float>> [#uses=1]
%tmp28 = tail call <4 x float> %llvm.x86.sse.sub.ss( <4 x float> %tmp12, <4 x float> < float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp37 = tail call <4 x float> %llvm.x86.sse.mul.ss( <4 x float> %tmp28, <4 x float> < float 5.000000e-01, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp37, <4 x float> < float 6.553500e+04, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> zeroinitializer ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
into:
ushort %Convert_sse(float %f) {
entry:
%tmp28 = sub float %f, 1.000000e+00 ; <float> [#uses=1]
%tmp37 = mul float %tmp28, 5.000000e-01 ; <float> [#uses=1]
%tmp375 = insertelement <4 x float> undef, float %tmp37, uint 0 ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp375, <4 x float> < float 6.553500e+04, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> < float 0.000000e+00, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
which improves codegen from:
_Convert_sse:
movss LCPI1_0, %xmm0
movss 4(%esp), %xmm1
subss %xmm0, %xmm1
movss LCPI1_1, %xmm0
mulss %xmm0, %xmm1
movss LCPI1_2, %xmm0
minss %xmm0, %xmm1
xorps %xmm0, %xmm0
maxss %xmm0, %xmm1
cvttss2si %xmm1, %eax
andl $65535, %eax
ret
to:
_Convert_sse:
movss 4(%esp), %xmm0
subss LCPI1_0, %xmm0
mulss LCPI1_1, %xmm0
movss LCPI1_2, %xmm1
minss %xmm1, %xmm0
xorps %xmm1, %xmm1
maxss %xmm1, %xmm0
cvttss2si %xmm0, %eax
andl $65535, %eax
ret
This is just a first step, it can be extended in many ways. Testcase here:
Transforms/InstCombine/vec_demanded_elts.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30752 91177308-0d34-0410-b5e6-96231b3b80d8
2006-10-05 06:55:50 +00:00
|
|
|
// This instruction only demands the single element from the input vector.
|
|
|
|
// If the input vector has a single use, simplify it based on this use
|
|
|
|
// property.
|
2007-04-09 01:37:55 +00:00
|
|
|
if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) {
|
add a new SimplifyDemandedVectorElts method, which works similarly to
SimplifyDemandedBits. The idea is that some operations can be simplified if
not all of the computed elements are needed. Some targets (like x86) have a
large number of intrinsics that operate on a single element, but pass other
elts through unmodified. If those other elements are not needed, the
intrinsics can be simplified to scalar operations, and insertelement ops can
be removed.
This turns (f.e.):
ushort %Convert_sse(float %f) {
%tmp = insertelement <4 x float> undef, float %f, uint 0 ; <<4 x float>> [#uses=1]
%tmp10 = insertelement <4 x float> %tmp, float 0.000000e+00, uint 1 ; <<4 x float>> [#uses=1]
%tmp11 = insertelement <4 x float> %tmp10, float 0.000000e+00, uint 2 ; <<4 x float>> [#uses=1]
%tmp12 = insertelement <4 x float> %tmp11, float 0.000000e+00, uint 3 ; <<4 x float>> [#uses=1]
%tmp28 = tail call <4 x float> %llvm.x86.sse.sub.ss( <4 x float> %tmp12, <4 x float> < float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp37 = tail call <4 x float> %llvm.x86.sse.mul.ss( <4 x float> %tmp28, <4 x float> < float 5.000000e-01, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp37, <4 x float> < float 6.553500e+04, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> zeroinitializer ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
into:
ushort %Convert_sse(float %f) {
entry:
%tmp28 = sub float %f, 1.000000e+00 ; <float> [#uses=1]
%tmp37 = mul float %tmp28, 5.000000e-01 ; <float> [#uses=1]
%tmp375 = insertelement <4 x float> undef, float %tmp37, uint 0 ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp375, <4 x float> < float 6.553500e+04, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> < float 0.000000e+00, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
which improves codegen from:
_Convert_sse:
movss LCPI1_0, %xmm0
movss 4(%esp), %xmm1
subss %xmm0, %xmm1
movss LCPI1_1, %xmm0
mulss %xmm0, %xmm1
movss LCPI1_2, %xmm0
minss %xmm0, %xmm1
xorps %xmm0, %xmm0
maxss %xmm0, %xmm1
cvttss2si %xmm1, %eax
andl $65535, %eax
ret
to:
_Convert_sse:
movss 4(%esp), %xmm0
subss LCPI1_0, %xmm0
mulss LCPI1_1, %xmm0
movss LCPI1_2, %xmm1
minss %xmm1, %xmm0
xorps %xmm1, %xmm1
maxss %xmm1, %xmm0
cvttss2si %xmm0, %eax
andl $65535, %eax
ret
This is just a first step, it can be extended in many ways. Testcase here:
Transforms/InstCombine/vec_demanded_elts.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30752 91177308-0d34-0410-b5e6-96231b3b80d8
2006-10-05 06:55:50 +00:00
|
|
|
uint64_t UndefElts;
|
|
|
|
if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0),
|
2006-10-20 07:07:24 +00:00
|
|
|
1 << IndexVal,
|
add a new SimplifyDemandedVectorElts method, which works similarly to
SimplifyDemandedBits. The idea is that some operations can be simplified if
not all of the computed elements are needed. Some targets (like x86) have a
large number of intrinsics that operate on a single element, but pass other
elts through unmodified. If those other elements are not needed, the
intrinsics can be simplified to scalar operations, and insertelement ops can
be removed.
This turns (f.e.):
ushort %Convert_sse(float %f) {
%tmp = insertelement <4 x float> undef, float %f, uint 0 ; <<4 x float>> [#uses=1]
%tmp10 = insertelement <4 x float> %tmp, float 0.000000e+00, uint 1 ; <<4 x float>> [#uses=1]
%tmp11 = insertelement <4 x float> %tmp10, float 0.000000e+00, uint 2 ; <<4 x float>> [#uses=1]
%tmp12 = insertelement <4 x float> %tmp11, float 0.000000e+00, uint 3 ; <<4 x float>> [#uses=1]
%tmp28 = tail call <4 x float> %llvm.x86.sse.sub.ss( <4 x float> %tmp12, <4 x float> < float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp37 = tail call <4 x float> %llvm.x86.sse.mul.ss( <4 x float> %tmp28, <4 x float> < float 5.000000e-01, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp37, <4 x float> < float 6.553500e+04, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> zeroinitializer ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
into:
ushort %Convert_sse(float %f) {
entry:
%tmp28 = sub float %f, 1.000000e+00 ; <float> [#uses=1]
%tmp37 = mul float %tmp28, 5.000000e-01 ; <float> [#uses=1]
%tmp375 = insertelement <4 x float> undef, float %tmp37, uint 0 ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp375, <4 x float> < float 6.553500e+04, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> < float 0.000000e+00, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
which improves codegen from:
_Convert_sse:
movss LCPI1_0, %xmm0
movss 4(%esp), %xmm1
subss %xmm0, %xmm1
movss LCPI1_1, %xmm0
mulss %xmm0, %xmm1
movss LCPI1_2, %xmm0
minss %xmm0, %xmm1
xorps %xmm0, %xmm0
maxss %xmm0, %xmm1
cvttss2si %xmm1, %eax
andl $65535, %eax
ret
to:
_Convert_sse:
movss 4(%esp), %xmm0
subss LCPI1_0, %xmm0
mulss LCPI1_1, %xmm0
movss LCPI1_2, %xmm1
minss %xmm1, %xmm0
xorps %xmm1, %xmm1
maxss %xmm1, %xmm0
cvttss2si %xmm0, %eax
andl $65535, %eax
ret
This is just a first step, it can be extended in many ways. Testcase here:
Transforms/InstCombine/vec_demanded_elts.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30752 91177308-0d34-0410-b5e6-96231b3b80d8
2006-10-05 06:55:50 +00:00
|
|
|
UndefElts)) {
|
|
|
|
EI.setOperand(0, V);
|
|
|
|
return &EI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-20 07:07:24 +00:00
|
|
|
if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal))
|
2006-03-31 23:01:56 +00:00
|
|
|
return ReplaceInstUsesWith(EI, Elt);
|
Implement Transforms/InstCombine/vec_extract_elt.ll, transforming:
define i32 @test(float %f) {
%tmp7 = insertelement <4 x float> undef, float %f, i32 0
%tmp17 = bitcast <4 x float> %tmp7 to <4 x i32>
%tmp19 = extractelement <4 x i32> %tmp17, i32 0
ret i32 %tmp19
}
into:
define i32 @test(float %f) {
%tmp19 = bitcast float %f to i32 ; <i32> [#uses=1]
ret i32 %tmp19
}
On PPC, this is the difference between:
_test:
mfspr r2, 256
oris r3, r2, 8192
mtspr 256, r3
stfs f1, -16(r1)
addi r3, r1, -16
addi r4, r1, -32
lvx v2, 0, r3
stvx v2, 0, r4
lwz r3, -32(r1)
mtspr 256, r2
blr
and:
_test:
stfs f1, -4(r1)
nop
nop
nop
lwz r3, -4(r1)
blr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36025 91177308-0d34-0410-b5e6-96231b3b80d8
2007-04-14 23:02:14 +00:00
|
|
|
|
|
|
|
// If the this extractelement is directly using a bitcast from a vector of
|
|
|
|
// the same number of elements, see if we can find the source element from
|
|
|
|
// it. In this case, we will end up needing to bitcast the scalars.
|
|
|
|
if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) {
|
|
|
|
if (const VectorType *VT =
|
|
|
|
dyn_cast<VectorType>(BCI->getOperand(0)->getType()))
|
|
|
|
if (VT->getNumElements() == VectorWidth)
|
|
|
|
if (Value *Elt = FindScalarElement(BCI->getOperand(0), IndexVal))
|
|
|
|
return new BitCastInst(Elt, EI.getType());
|
|
|
|
}
|
2006-04-10 23:06:36 +00:00
|
|
|
}
|
2006-03-31 23:01:56 +00:00
|
|
|
|
2006-05-25 22:53:38 +00:00
|
|
|
if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) {
|
2006-01-13 22:48:06 +00:00
|
|
|
if (I->hasOneUse()) {
|
|
|
|
// Push extractelement into predecessor operation if legal and
|
|
|
|
// profitable to do so
|
|
|
|
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
|
2006-03-05 00:22:33 +00:00
|
|
|
bool isConstantElt = isa<ConstantInt>(EI.getOperand(1));
|
|
|
|
if (CheapToScalarize(BO, isConstantElt)) {
|
|
|
|
ExtractElementInst *newEI0 =
|
|
|
|
new ExtractElementInst(BO->getOperand(0), EI.getOperand(1),
|
|
|
|
EI.getName()+".lhs");
|
|
|
|
ExtractElementInst *newEI1 =
|
|
|
|
new ExtractElementInst(BO->getOperand(1), EI.getOperand(1),
|
|
|
|
EI.getName()+".rhs");
|
|
|
|
InsertNewInstBefore(newEI0, EI);
|
|
|
|
InsertNewInstBefore(newEI1, EI);
|
|
|
|
return BinaryOperator::create(BO->getOpcode(), newEI0, newEI1);
|
|
|
|
}
|
2006-11-02 20:25:50 +00:00
|
|
|
} else if (isa<LoadInst>(I)) {
|
2007-12-17 01:12:55 +00:00
|
|
|
unsigned AS =
|
|
|
|
cast<PointerType>(I->getOperand(0)->getType())->getAddressSpace();
|
2008-01-13 22:23:22 +00:00
|
|
|
Value *Ptr = InsertBitCastBefore(I->getOperand(0),
|
|
|
|
PointerType::get(EI.getType(), AS),EI);
|
2006-01-13 22:48:06 +00:00
|
|
|
GetElementPtrInst *GEP =
|
2008-04-06 20:25:17 +00:00
|
|
|
GetElementPtrInst::Create(Ptr, EI.getOperand(1), I->getName() + ".gep");
|
2006-01-13 22:48:06 +00:00
|
|
|
InsertNewInstBefore(GEP, EI);
|
|
|
|
return new LoadInst(GEP);
|
2006-05-25 22:53:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) {
|
|
|
|
// Extracting the inserted element?
|
|
|
|
if (IE->getOperand(2) == EI.getOperand(1))
|
|
|
|
return ReplaceInstUsesWith(EI, IE->getOperand(1));
|
|
|
|
// If the inserted and extracted elements are constants, they must not
|
|
|
|
// be the same value, extract from the pre-inserted value instead.
|
|
|
|
if (isa<Constant>(IE->getOperand(2)) &&
|
|
|
|
isa<Constant>(EI.getOperand(1))) {
|
|
|
|
AddUsesToWorkList(EI);
|
|
|
|
EI.setOperand(0, IE->getOperand(0));
|
|
|
|
return &EI;
|
|
|
|
}
|
|
|
|
} else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) {
|
|
|
|
// If this is extracting an element from a shufflevector, figure out where
|
|
|
|
// it came from and extract from the appropriate input element instead.
|
2006-10-20 07:07:24 +00:00
|
|
|
if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) {
|
|
|
|
unsigned SrcIdx = getShuffleMask(SVI)[Elt->getZExtValue()];
|
2006-05-25 23:48:38 +00:00
|
|
|
Value *Src;
|
|
|
|
if (SrcIdx < SVI->getType()->getNumElements())
|
|
|
|
Src = SVI->getOperand(0);
|
|
|
|
else if (SrcIdx < SVI->getType()->getNumElements()*2) {
|
|
|
|
SrcIdx -= SVI->getType()->getNumElements();
|
|
|
|
Src = SVI->getOperand(1);
|
|
|
|
} else {
|
|
|
|
return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
|
2006-03-30 22:02:40 +00:00
|
|
|
}
|
add a new SimplifyDemandedVectorElts method, which works similarly to
SimplifyDemandedBits. The idea is that some operations can be simplified if
not all of the computed elements are needed. Some targets (like x86) have a
large number of intrinsics that operate on a single element, but pass other
elts through unmodified. If those other elements are not needed, the
intrinsics can be simplified to scalar operations, and insertelement ops can
be removed.
This turns (f.e.):
ushort %Convert_sse(float %f) {
%tmp = insertelement <4 x float> undef, float %f, uint 0 ; <<4 x float>> [#uses=1]
%tmp10 = insertelement <4 x float> %tmp, float 0.000000e+00, uint 1 ; <<4 x float>> [#uses=1]
%tmp11 = insertelement <4 x float> %tmp10, float 0.000000e+00, uint 2 ; <<4 x float>> [#uses=1]
%tmp12 = insertelement <4 x float> %tmp11, float 0.000000e+00, uint 3 ; <<4 x float>> [#uses=1]
%tmp28 = tail call <4 x float> %llvm.x86.sse.sub.ss( <4 x float> %tmp12, <4 x float> < float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp37 = tail call <4 x float> %llvm.x86.sse.mul.ss( <4 x float> %tmp28, <4 x float> < float 5.000000e-01, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp37, <4 x float> < float 6.553500e+04, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> zeroinitializer ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
into:
ushort %Convert_sse(float %f) {
entry:
%tmp28 = sub float %f, 1.000000e+00 ; <float> [#uses=1]
%tmp37 = mul float %tmp28, 5.000000e-01 ; <float> [#uses=1]
%tmp375 = insertelement <4 x float> undef, float %tmp37, uint 0 ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp375, <4 x float> < float 6.553500e+04, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> < float 0.000000e+00, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
which improves codegen from:
_Convert_sse:
movss LCPI1_0, %xmm0
movss 4(%esp), %xmm1
subss %xmm0, %xmm1
movss LCPI1_1, %xmm0
mulss %xmm0, %xmm1
movss LCPI1_2, %xmm0
minss %xmm0, %xmm1
xorps %xmm0, %xmm0
maxss %xmm0, %xmm1
cvttss2si %xmm1, %eax
andl $65535, %eax
ret
to:
_Convert_sse:
movss 4(%esp), %xmm0
subss LCPI1_0, %xmm0
mulss LCPI1_1, %xmm0
movss LCPI1_2, %xmm1
minss %xmm1, %xmm0
xorps %xmm1, %xmm1
maxss %xmm1, %xmm0
cvttss2si %xmm0, %eax
andl $65535, %eax
ret
This is just a first step, it can be extended in many ways. Testcase here:
Transforms/InstCombine/vec_demanded_elts.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30752 91177308-0d34-0410-b5e6-96231b3b80d8
2006-10-05 06:55:50 +00:00
|
|
|
return new ExtractElementInst(Src, SrcIdx);
|
2006-01-13 22:48:06 +00:00
|
|
|
}
|
|
|
|
}
|
2006-05-25 22:53:38 +00:00
|
|
|
}
|
2006-01-13 22:48:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-04-16 00:51:47 +00:00
|
|
|
/// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns
|
|
|
|
/// elements from either LHS or RHS, return the shuffle mask and true.
|
|
|
|
/// Otherwise, return false.
|
|
|
|
static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS,
|
|
|
|
std::vector<Constant*> &Mask) {
|
|
|
|
assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() &&
|
|
|
|
"Invalid CollectSingleShuffleElements");
|
2007-02-15 02:26:10 +00:00
|
|
|
unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
|
2006-04-16 00:51:47 +00:00
|
|
|
|
|
|
|
if (isa<UndefValue>(V)) {
|
2006-12-31 05:48:39 +00:00
|
|
|
Mask.assign(NumElts, UndefValue::get(Type::Int32Ty));
|
2006-04-16 00:51:47 +00:00
|
|
|
return true;
|
|
|
|
} else if (V == LHS) {
|
|
|
|
for (unsigned i = 0; i != NumElts; ++i)
|
2006-12-31 05:48:39 +00:00
|
|
|
Mask.push_back(ConstantInt::get(Type::Int32Ty, i));
|
2006-04-16 00:51:47 +00:00
|
|
|
return true;
|
|
|
|
} else if (V == RHS) {
|
|
|
|
for (unsigned i = 0; i != NumElts; ++i)
|
2006-12-31 05:48:39 +00:00
|
|
|
Mask.push_back(ConstantInt::get(Type::Int32Ty, i+NumElts));
|
2006-04-16 00:51:47 +00:00
|
|
|
return true;
|
|
|
|
} else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
|
|
|
|
// If this is an insert of an extract from some other vector, include it.
|
|
|
|
Value *VecOp = IEI->getOperand(0);
|
|
|
|
Value *ScalarOp = IEI->getOperand(1);
|
|
|
|
Value *IdxOp = IEI->getOperand(2);
|
|
|
|
|
2006-04-27 21:14:21 +00:00
|
|
|
if (!isa<ConstantInt>(IdxOp))
|
|
|
|
return false;
|
2006-10-20 07:07:24 +00:00
|
|
|
unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
|
2006-04-27 21:14:21 +00:00
|
|
|
|
|
|
|
if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector.
|
|
|
|
// Okay, we can handle this if the vector we are insertinting into is
|
|
|
|
// transitively ok.
|
|
|
|
if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) {
|
|
|
|
// If so, update the mask to reflect the inserted undef.
|
2006-12-31 05:48:39 +00:00
|
|
|
Mask[InsertedIdx] = UndefValue::get(Type::Int32Ty);
|
2006-04-27 21:14:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){
|
|
|
|
if (isa<ConstantInt>(EI->getOperand(1)) &&
|
2006-04-16 00:51:47 +00:00
|
|
|
EI->getOperand(0)->getType() == V->getType()) {
|
|
|
|
unsigned ExtractedIdx =
|
2006-10-20 07:07:24 +00:00
|
|
|
cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
|
2006-04-16 00:51:47 +00:00
|
|
|
|
|
|
|
// This must be extracting from either LHS or RHS.
|
|
|
|
if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) {
|
|
|
|
// Okay, we can handle this if the vector we are insertinting into is
|
|
|
|
// transitively ok.
|
|
|
|
if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) {
|
|
|
|
// If so, update the mask to reflect the inserted value.
|
|
|
|
if (EI->getOperand(0) == LHS) {
|
|
|
|
Mask[InsertedIdx & (NumElts-1)] =
|
2006-12-31 05:48:39 +00:00
|
|
|
ConstantInt::get(Type::Int32Ty, ExtractedIdx);
|
2006-04-16 00:51:47 +00:00
|
|
|
} else {
|
|
|
|
assert(EI->getOperand(0) == RHS);
|
|
|
|
Mask[InsertedIdx & (NumElts-1)] =
|
2006-12-31 05:48:39 +00:00
|
|
|
ConstantInt::get(Type::Int32Ty, ExtractedIdx+NumElts);
|
2006-04-16 00:51:47 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: Handle shufflevector here!
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CollectShuffleElements - We are building a shuffle of V, using RHS as the
|
|
|
|
/// RHS of the shuffle instruction, if it is not null. Return a shuffle mask
|
|
|
|
/// that computes V and the LHS value of the shuffle.
|
2006-04-15 01:39:45 +00:00
|
|
|
static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask,
|
2006-04-16 00:51:47 +00:00
|
|
|
Value *&RHS) {
|
2007-02-15 02:26:10 +00:00
|
|
|
assert(isa<VectorType>(V->getType()) &&
|
2006-04-16 00:51:47 +00:00
|
|
|
(RHS == 0 || V->getType() == RHS->getType()) &&
|
2006-04-15 01:39:45 +00:00
|
|
|
"Invalid shuffle!");
|
2007-02-15 02:26:10 +00:00
|
|
|
unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
|
2006-04-15 01:39:45 +00:00
|
|
|
|
|
|
|
if (isa<UndefValue>(V)) {
|
2006-12-31 05:48:39 +00:00
|
|
|
Mask.assign(NumElts, UndefValue::get(Type::Int32Ty));
|
2006-04-15 01:39:45 +00:00
|
|
|
return V;
|
|
|
|
} else if (isa<ConstantAggregateZero>(V)) {
|
2006-12-31 05:48:39 +00:00
|
|
|
Mask.assign(NumElts, ConstantInt::get(Type::Int32Ty, 0));
|
2006-04-15 01:39:45 +00:00
|
|
|
return V;
|
|
|
|
} else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
|
|
|
|
// If this is an insert of an extract from some other vector, include it.
|
|
|
|
Value *VecOp = IEI->getOperand(0);
|
|
|
|
Value *ScalarOp = IEI->getOperand(1);
|
|
|
|
Value *IdxOp = IEI->getOperand(2);
|
|
|
|
|
|
|
|
if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
|
|
|
|
if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) &&
|
|
|
|
EI->getOperand(0)->getType() == V->getType()) {
|
|
|
|
unsigned ExtractedIdx =
|
2006-10-20 07:07:24 +00:00
|
|
|
cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
|
|
|
|
unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
|
2006-04-15 01:39:45 +00:00
|
|
|
|
|
|
|
// Either the extracted from or inserted into vector must be RHSVec,
|
|
|
|
// otherwise we'd end up with a shuffle of three inputs.
|
2006-04-16 00:51:47 +00:00
|
|
|
if (EI->getOperand(0) == RHS || RHS == 0) {
|
|
|
|
RHS = EI->getOperand(0);
|
|
|
|
Value *V = CollectShuffleElements(VecOp, Mask, RHS);
|
2006-04-15 01:39:45 +00:00
|
|
|
Mask[InsertedIdx & (NumElts-1)] =
|
2006-12-31 05:48:39 +00:00
|
|
|
ConstantInt::get(Type::Int32Ty, NumElts+ExtractedIdx);
|
2006-04-15 01:39:45 +00:00
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2006-04-16 00:51:47 +00:00
|
|
|
if (VecOp == RHS) {
|
|
|
|
Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS);
|
2006-04-15 01:39:45 +00:00
|
|
|
// Everything but the extracted element is replaced with the RHS.
|
|
|
|
for (unsigned i = 0; i != NumElts; ++i) {
|
|
|
|
if (i != InsertedIdx)
|
2006-12-31 05:48:39 +00:00
|
|
|
Mask[i] = ConstantInt::get(Type::Int32Ty, NumElts+i);
|
2006-04-15 01:39:45 +00:00
|
|
|
}
|
|
|
|
return V;
|
|
|
|
}
|
2006-04-16 00:51:47 +00:00
|
|
|
|
|
|
|
// If this insertelement is a chain that comes from exactly these two
|
|
|
|
// vectors, return the vector and the effective shuffle.
|
|
|
|
if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask))
|
|
|
|
return EI->getOperand(0);
|
|
|
|
|
2006-04-15 01:39:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-04-16 00:51:47 +00:00
|
|
|
// TODO: Handle shufflevector here!
|
2006-04-15 01:39:45 +00:00
|
|
|
|
|
|
|
// Otherwise, can't do anything fancy. Return an identity vector.
|
|
|
|
for (unsigned i = 0; i != NumElts; ++i)
|
2006-12-31 05:48:39 +00:00
|
|
|
Mask.push_back(ConstantInt::get(Type::Int32Ty, i));
|
2006-04-15 01:39:45 +00:00
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) {
|
|
|
|
Value *VecOp = IE.getOperand(0);
|
|
|
|
Value *ScalarOp = IE.getOperand(1);
|
|
|
|
Value *IdxOp = IE.getOperand(2);
|
|
|
|
|
Eliminate useless insertelement instructions. This implements
Transforms/InstCombine/vec_insertelt.ll and fixes PR1286.
We now compile the code from that bug into:
_foo:
movl 4(%esp), %eax
movdqa (%eax), %xmm0
movl 8(%esp), %ecx
psllw (%ecx), %xmm0
movdqa %xmm0, (%eax)
ret
instead of:
_foo:
subl $4, %esp
movl %ebp, (%esp)
movl %esp, %ebp
movl 12(%ebp), %eax
movdqa (%eax), %xmm0
#IMPLICIT_DEF %eax
pinsrw $2, %eax, %xmm0
xorl %ecx, %ecx
pinsrw $3, %ecx, %xmm0
pinsrw $4, %eax, %xmm0
pinsrw $5, %ecx, %xmm0
pinsrw $6, %eax, %xmm0
pinsrw $7, %ecx, %xmm0
movl 8(%ebp), %eax
movdqa (%eax), %xmm1
psllw %xmm0, %xmm1
movdqa %xmm1, (%eax)
movl %ebp, %esp
popl %ebp
ret
woo :)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@35788 91177308-0d34-0410-b5e6-96231b3b80d8
2007-04-09 01:11:16 +00:00
|
|
|
// Inserting an undef or into an undefined place, remove this.
|
|
|
|
if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp))
|
|
|
|
ReplaceInstUsesWith(IE, VecOp);
|
|
|
|
|
2006-04-15 01:39:45 +00:00
|
|
|
// If the inserted element was extracted from some other vector, and if the
|
|
|
|
// indexes are constant, try to turn this into a shufflevector operation.
|
|
|
|
if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
|
|
|
|
if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) &&
|
|
|
|
EI->getOperand(0)->getType() == IE.getType()) {
|
|
|
|
unsigned NumVectorElts = IE.getType()->getNumElements();
|
2007-04-14 23:32:02 +00:00
|
|
|
unsigned ExtractedIdx =
|
|
|
|
cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
|
2006-10-20 07:07:24 +00:00
|
|
|
unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
|
2006-04-15 01:39:45 +00:00
|
|
|
|
|
|
|
if (ExtractedIdx >= NumVectorElts) // Out of range extract.
|
|
|
|
return ReplaceInstUsesWith(IE, VecOp);
|
|
|
|
|
|
|
|
if (InsertedIdx >= NumVectorElts) // Out of range insert.
|
|
|
|
return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType()));
|
|
|
|
|
|
|
|
// If we are extracting a value from a vector, then inserting it right
|
|
|
|
// back into the same place, just use the input vector.
|
|
|
|
if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx)
|
|
|
|
return ReplaceInstUsesWith(IE, VecOp);
|
|
|
|
|
|
|
|
// We could theoretically do this for ANY input. However, doing so could
|
|
|
|
// turn chains of insertelement instructions into a chain of shufflevector
|
|
|
|
// instructions, and right now we do not merge shufflevectors. As such,
|
|
|
|
// only do this in a situation where it is clear that there is benefit.
|
|
|
|
if (isa<UndefValue>(VecOp) || isa<ConstantAggregateZero>(VecOp)) {
|
|
|
|
// Turn this into shuffle(EIOp0, VecOp, Mask). The result has all of
|
|
|
|
// the values of VecOp, except then one read from EIOp0.
|
|
|
|
// Build a new shuffle mask.
|
|
|
|
std::vector<Constant*> Mask;
|
|
|
|
if (isa<UndefValue>(VecOp))
|
2006-12-31 05:48:39 +00:00
|
|
|
Mask.assign(NumVectorElts, UndefValue::get(Type::Int32Ty));
|
2006-04-15 01:39:45 +00:00
|
|
|
else {
|
|
|
|
assert(isa<ConstantAggregateZero>(VecOp) && "Unknown thing");
|
2006-12-31 05:48:39 +00:00
|
|
|
Mask.assign(NumVectorElts, ConstantInt::get(Type::Int32Ty,
|
2006-04-15 01:39:45 +00:00
|
|
|
NumVectorElts));
|
|
|
|
}
|
2006-12-31 05:48:39 +00:00
|
|
|
Mask[InsertedIdx] = ConstantInt::get(Type::Int32Ty, ExtractedIdx);
|
2006-04-15 01:39:45 +00:00
|
|
|
return new ShuffleVectorInst(EI->getOperand(0), VecOp,
|
2007-02-15 02:26:10 +00:00
|
|
|
ConstantVector::get(Mask));
|
2006-04-15 01:39:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If this insertelement isn't used by some other insertelement, turn it
|
|
|
|
// (and any insertelements it points to), into one big shuffle.
|
|
|
|
if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) {
|
|
|
|
std::vector<Constant*> Mask;
|
2006-04-16 00:51:47 +00:00
|
|
|
Value *RHS = 0;
|
|
|
|
Value *LHS = CollectShuffleElements(&IE, Mask, RHS);
|
|
|
|
if (RHS == 0) RHS = UndefValue::get(LHS->getType());
|
|
|
|
// We now have a shuffle of LHS, RHS, Mask.
|
2007-02-15 02:26:10 +00:00
|
|
|
return new ShuffleVectorInst(LHS, RHS, ConstantVector::get(Mask));
|
2006-04-15 01:39:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-10 22:45:52 +00:00
|
|
|
Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
|
|
|
|
Value *LHS = SVI.getOperand(0);
|
|
|
|
Value *RHS = SVI.getOperand(1);
|
2006-05-25 23:48:38 +00:00
|
|
|
std::vector<unsigned> Mask = getShuffleMask(&SVI);
|
2006-04-10 22:45:52 +00:00
|
|
|
|
|
|
|
bool MadeChange = false;
|
|
|
|
|
add a new SimplifyDemandedVectorElts method, which works similarly to
SimplifyDemandedBits. The idea is that some operations can be simplified if
not all of the computed elements are needed. Some targets (like x86) have a
large number of intrinsics that operate on a single element, but pass other
elts through unmodified. If those other elements are not needed, the
intrinsics can be simplified to scalar operations, and insertelement ops can
be removed.
This turns (f.e.):
ushort %Convert_sse(float %f) {
%tmp = insertelement <4 x float> undef, float %f, uint 0 ; <<4 x float>> [#uses=1]
%tmp10 = insertelement <4 x float> %tmp, float 0.000000e+00, uint 1 ; <<4 x float>> [#uses=1]
%tmp11 = insertelement <4 x float> %tmp10, float 0.000000e+00, uint 2 ; <<4 x float>> [#uses=1]
%tmp12 = insertelement <4 x float> %tmp11, float 0.000000e+00, uint 3 ; <<4 x float>> [#uses=1]
%tmp28 = tail call <4 x float> %llvm.x86.sse.sub.ss( <4 x float> %tmp12, <4 x float> < float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp37 = tail call <4 x float> %llvm.x86.sse.mul.ss( <4 x float> %tmp28, <4 x float> < float 5.000000e-01, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp37, <4 x float> < float 6.553500e+04, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> zeroinitializer ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
into:
ushort %Convert_sse(float %f) {
entry:
%tmp28 = sub float %f, 1.000000e+00 ; <float> [#uses=1]
%tmp37 = mul float %tmp28, 5.000000e-01 ; <float> [#uses=1]
%tmp375 = insertelement <4 x float> undef, float %tmp37, uint 0 ; <<4 x float>> [#uses=1]
%tmp48 = tail call <4 x float> %llvm.x86.sse.min.ss( <4 x float> %tmp375, <4 x float> < float 6.553500e+04, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp59 = tail call <4 x float> %llvm.x86.sse.max.ss( <4 x float> %tmp48, <4 x float> < float 0.000000e+00, float undef, float undef, float undef > ) ; <<4 x float>> [#uses=1]
%tmp = tail call int %llvm.x86.sse.cvttss2si( <4 x float> %tmp59 ) ; <int> [#uses=1]
%tmp69 = cast int %tmp to ushort ; <ushort> [#uses=1]
ret ushort %tmp69
}
which improves codegen from:
_Convert_sse:
movss LCPI1_0, %xmm0
movss 4(%esp), %xmm1
subss %xmm0, %xmm1
movss LCPI1_1, %xmm0
mulss %xmm0, %xmm1
movss LCPI1_2, %xmm0
minss %xmm0, %xmm1
xorps %xmm0, %xmm0
maxss %xmm0, %xmm1
cvttss2si %xmm1, %eax
andl $65535, %eax
ret
to:
_Convert_sse:
movss 4(%esp), %xmm0
subss LCPI1_0, %xmm0
mulss LCPI1_1, %xmm0
movss LCPI1_2, %xmm1
minss %xmm1, %xmm0
xorps %xmm1, %xmm1
maxss %xmm1, %xmm0
cvttss2si %xmm0, %eax
andl $65535, %eax
ret
This is just a first step, it can be extended in many ways. Testcase here:
Transforms/InstCombine/vec_demanded_elts.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30752 91177308-0d34-0410-b5e6-96231b3b80d8
2006-10-05 06:55:50 +00:00
|
|
|
// Undefined shuffle mask -> undefined value.
|
2006-05-25 23:48:38 +00:00
|
|
|
if (isa<UndefValue>(SVI.getOperand(2)))
|
2006-04-10 22:45:52 +00:00
|
|
|
return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType()));
|
|
|
|
|
2007-01-05 07:36:08 +00:00
|
|
|
// If we have shuffle(x, undef, mask) and any elements of mask refer to
|
2006-04-15 01:39:45 +00:00
|
|
|
// the undef, change them to undefs.
|
2007-01-05 07:36:08 +00:00
|
|
|
if (isa<UndefValue>(SVI.getOperand(1))) {
|
|
|
|
// Scan to see if there are any references to the RHS. If so, replace them
|
|
|
|
// with undef element refs and set MadeChange to true.
|
|
|
|
for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
|
|
|
|
if (Mask[i] >= e && Mask[i] != 2*e) {
|
|
|
|
Mask[i] = 2*e;
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MadeChange) {
|
|
|
|
// Remap any references to RHS to use LHS.
|
|
|
|
std::vector<Constant*> Elts;
|
|
|
|
for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
|
|
|
|
if (Mask[i] == 2*e)
|
|
|
|
Elts.push_back(UndefValue::get(Type::Int32Ty));
|
|
|
|
else
|
|
|
|
Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i]));
|
|
|
|
}
|
2007-02-15 02:26:10 +00:00
|
|
|
SVI.setOperand(2, ConstantVector::get(Elts));
|
2007-01-05 07:36:08 +00:00
|
|
|
}
|
|
|
|
}
|
2006-04-15 01:39:45 +00:00
|
|
|
|
2006-05-25 23:48:38 +00:00
|
|
|
// Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask')
|
|
|
|
// Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask').
|
|
|
|
if (LHS == RHS || isa<UndefValue>(LHS)) {
|
|
|
|
if (isa<UndefValue>(LHS) && LHS == RHS) {
|
2006-04-10 22:45:52 +00:00
|
|
|
// shuffle(undef,undef,mask) -> undef.
|
|
|
|
return ReplaceInstUsesWith(SVI, LHS);
|
|
|
|
}
|
|
|
|
|
2006-05-25 23:48:38 +00:00
|
|
|
// Remap any references to RHS to use LHS.
|
|
|
|
std::vector<Constant*> Elts;
|
|
|
|
for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
|
2006-05-26 00:29:06 +00:00
|
|
|
if (Mask[i] >= 2*e)
|
2006-12-31 05:48:39 +00:00
|
|
|
Elts.push_back(UndefValue::get(Type::Int32Ty));
|
2006-05-26 00:29:06 +00:00
|
|
|
else {
|
|
|
|
if ((Mask[i] >= e && isa<UndefValue>(RHS)) ||
|
|
|
|
(Mask[i] < e && isa<UndefValue>(LHS)))
|
|
|
|
Mask[i] = 2*e; // Turn into undef.
|
|
|
|
else
|
|
|
|
Mask[i] &= (e-1); // Force to LHS.
|
2006-12-31 05:48:39 +00:00
|
|
|
Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i]));
|
2006-05-26 00:29:06 +00:00
|
|
|
}
|
2006-04-10 22:45:52 +00:00
|
|
|
}
|
2006-05-25 23:48:38 +00:00
|
|
|
SVI.setOperand(0, SVI.getOperand(1));
|
2006-04-10 22:45:52 +00:00
|
|
|
SVI.setOperand(1, UndefValue::get(RHS->getType()));
|
2007-02-15 02:26:10 +00:00
|
|
|
SVI.setOperand(2, ConstantVector::get(Elts));
|
2006-05-26 00:29:06 +00:00
|
|
|
LHS = SVI.getOperand(0);
|
|
|
|
RHS = SVI.getOperand(1);
|
2006-04-10 22:45:52 +00:00
|
|
|
MadeChange = true;
|
|
|
|
}
|
|
|
|
|
2006-05-26 00:29:06 +00:00
|
|
|
// Analyze the shuffle, are the LHS or RHS and identity shuffles?
|
2006-05-25 23:48:38 +00:00
|
|
|
bool isLHSID = true, isRHSID = true;
|
2006-04-16 00:03:56 +00:00
|
|
|
|
2006-05-25 23:48:38 +00:00
|
|
|
for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
|
|
|
|
if (Mask[i] >= e*2) continue; // Ignore undef values.
|
|
|
|
// Is this an identity shuffle of the LHS value?
|
|
|
|
isLHSID &= (Mask[i] == i);
|
2006-04-10 22:45:52 +00:00
|
|
|
|
2006-05-25 23:48:38 +00:00
|
|
|
// Is this an identity shuffle of the RHS value?
|
|
|
|
isRHSID &= (Mask[i]-e == i);
|
2006-04-10 22:45:52 +00:00
|
|
|
}
|
2006-05-25 23:48:38 +00:00
|
|
|
|
|
|
|
// Eliminate identity shuffles.
|
|
|
|
if (isLHSID) return ReplaceInstUsesWith(SVI, LHS);
|
|
|
|
if (isRHSID) return ReplaceInstUsesWith(SVI, RHS);
|
2006-04-10 22:45:52 +00:00
|
|
|
|
2006-05-26 00:29:06 +00:00
|
|
|
// If the LHS is a shufflevector itself, see if we can combine it with this
|
|
|
|
// one without producing an unusual shuffle. Here we are really conservative:
|
|
|
|
// we are absolutely afraid of producing a shuffle mask not in the input
|
|
|
|
// program, because the code gen may not be smart enough to turn a merged
|
|
|
|
// shuffle into two specific shuffles: it may produce worse code. As such,
|
|
|
|
// we only merge two shuffles if the result is one of the two input shuffle
|
|
|
|
// masks. In this case, merging the shuffles just removes one instruction,
|
|
|
|
// which we know is safe. This is good for things like turning:
|
|
|
|
// (splat(splat)) -> splat.
|
|
|
|
if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) {
|
|
|
|
if (isa<UndefValue>(RHS)) {
|
|
|
|
std::vector<unsigned> LHSMask = getShuffleMask(LHSSVI);
|
|
|
|
|
|
|
|
std::vector<unsigned> NewMask;
|
|
|
|
for (unsigned i = 0, e = Mask.size(); i != e; ++i)
|
|
|
|
if (Mask[i] >= 2*e)
|
|
|
|
NewMask.push_back(2*e);
|
|
|
|
else
|
|
|
|
NewMask.push_back(LHSMask[Mask[i]]);
|
|
|
|
|
|
|
|
// If the result mask is equal to the src shuffle or this shuffle mask, do
|
|
|
|
// the replacement.
|
|
|
|
if (NewMask == LHSMask || NewMask == Mask) {
|
|
|
|
std::vector<Constant*> Elts;
|
|
|
|
for (unsigned i = 0, e = NewMask.size(); i != e; ++i) {
|
|
|
|
if (NewMask[i] >= e*2) {
|
2006-12-31 05:48:39 +00:00
|
|
|
Elts.push_back(UndefValue::get(Type::Int32Ty));
|
2006-05-26 00:29:06 +00:00
|
|
|
} else {
|
2006-12-31 05:48:39 +00:00
|
|
|
Elts.push_back(ConstantInt::get(Type::Int32Ty, NewMask[i]));
|
2006-05-26 00:29:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return new ShuffleVectorInst(LHSSVI->getOperand(0),
|
|
|
|
LHSSVI->getOperand(1),
|
2007-02-15 02:26:10 +00:00
|
|
|
ConstantVector::get(Elts));
|
2006-05-26 00:29:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
Symbolically evaluate constant expressions like &A[123] - &A[4].f.
This occurs in C++ code like:
#include <iostream>
#include <iterator>
int a[] = { 1, 2, 3, 4, 5 };
int main() {
using namespace std;
copy(a, a + sizeof(a)/sizeof(a[0]), ostream_iterator<int>(cout, "\n"));
return 0;
}
Before we would decide the loop trip count is:
sdiv (i32 sub (i32 ptrtoint (i32* getelementptr ([5 x i32]* @a, i32 0, i32 5) to i32), i32 ptrtoint ([5 x i32]* @a to i32)), i32 4)
Now we decide it is "5". Amazing.
This code will need to be refactored, but I'm doing that as a separate
commit.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33665 91177308-0d34-0410-b5e6-96231b3b80d8
2007-01-30 22:32:46 +00:00
|
|
|
|
2006-04-10 22:45:52 +00:00
|
|
|
return MadeChange ? &SVI : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-01-13 22:48:06 +00:00
|
|
|
|
2004-12-08 23:43:58 +00:00
|
|
|
|
|
|
|
/// TryToSinkInstruction - Try to move the specified instruction from its
|
|
|
|
/// current block into the beginning of DestBlock, which can only happen if it's
|
|
|
|
/// safe to move the instruction past all of the instructions between it and the
|
|
|
|
/// end of its block.
|
|
|
|
static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) {
|
|
|
|
assert(I->hasOneUse() && "Invariants didn't hold!");
|
|
|
|
|
2005-10-27 17:13:11 +00:00
|
|
|
// Cannot move control-flow-involving, volatile loads, vaarg, etc.
|
|
|
|
if (isa<PHINode>(I) || I->mayWriteToMemory()) return false;
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-12-08 23:43:58 +00:00
|
|
|
// Do not sink alloca instructions out of the entry block.
|
2007-03-22 16:38:57 +00:00
|
|
|
if (isa<AllocaInst>(I) && I->getParent() ==
|
|
|
|
&DestBlock->getParent()->getEntryBlock())
|
2004-12-08 23:43:58 +00:00
|
|
|
return false;
|
|
|
|
|
2004-12-09 07:14:34 +00:00
|
|
|
// We can only sink load instructions if there is nothing between the load and
|
|
|
|
// the end of block that could change the value.
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
|
|
|
|
for (BasicBlock::iterator Scan = LI, E = LI->getParent()->end();
|
|
|
|
Scan != E; ++Scan)
|
|
|
|
if (Scan->mayWriteToMemory())
|
|
|
|
return false;
|
|
|
|
}
|
2004-12-08 23:43:58 +00:00
|
|
|
|
|
|
|
BasicBlock::iterator InsertPos = DestBlock->begin();
|
|
|
|
while (isa<PHINode>(InsertPos)) ++InsertPos;
|
|
|
|
|
2005-08-08 19:11:57 +00:00
|
|
|
I->moveBefore(InsertPos);
|
2004-12-08 23:43:58 +00:00
|
|
|
++NumSunkInst;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-05-10 19:00:36 +00:00
|
|
|
|
|
|
|
/// AddReachableCodeToWorklist - Walk the function in depth-first order, adding
|
|
|
|
/// all reachable code to the worklist.
|
|
|
|
///
|
|
|
|
/// This has a couple of tricks to make the code faster and more powerful. In
|
|
|
|
/// particular, we constant fold and DCE instructions as we go, to avoid adding
|
|
|
|
/// them to the worklist (this significantly speeds up instcombine on code where
|
|
|
|
/// many instructions are dead or constant). Additionally, if we find a branch
|
|
|
|
/// whose condition is a known constant, we only visit the reachable successors.
|
|
|
|
///
|
|
|
|
static void AddReachableCodeToWorklist(BasicBlock *BB,
|
2007-02-15 19:41:52 +00:00
|
|
|
SmallPtrSet<BasicBlock*, 64> &Visited,
|
2007-03-02 21:28:56 +00:00
|
|
|
InstCombiner &IC,
|
2006-05-11 17:11:52 +00:00
|
|
|
const TargetData *TD) {
|
2007-03-23 19:17:18 +00:00
|
|
|
std::vector<BasicBlock*> Worklist;
|
|
|
|
Worklist.push_back(BB);
|
|
|
|
|
|
|
|
while (!Worklist.empty()) {
|
|
|
|
BB = Worklist.back();
|
|
|
|
Worklist.pop_back();
|
2006-05-10 19:00:36 +00:00
|
|
|
|
2007-03-23 19:17:18 +00:00
|
|
|
// We have now visited this block! If we've already been here, ignore it.
|
|
|
|
if (!Visited.insert(BB)) continue;
|
2006-05-10 19:00:36 +00:00
|
|
|
|
2007-03-23 19:17:18 +00:00
|
|
|
for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) {
|
|
|
|
Instruction *Inst = BBI++;
|
|
|
|
|
|
|
|
// DCE instruction if trivially dead.
|
|
|
|
if (isInstructionTriviallyDead(Inst)) {
|
|
|
|
++NumDeadInst;
|
|
|
|
DOUT << "IC: DCE: " << *Inst;
|
|
|
|
Inst->eraseFromParent();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConstantProp instruction if trivially constant.
|
|
|
|
if (Constant *C = ConstantFoldInstruction(Inst, TD)) {
|
|
|
|
DOUT << "IC: ConstFold to: " << *C << " from: " << *Inst;
|
|
|
|
Inst->replaceAllUsesWith(C);
|
|
|
|
++NumConstProp;
|
|
|
|
Inst->eraseFromParent();
|
|
|
|
continue;
|
|
|
|
}
|
2007-07-20 22:06:41 +00:00
|
|
|
|
2007-03-23 19:17:18 +00:00
|
|
|
IC.AddToWorkList(Inst);
|
2006-05-10 19:00:36 +00:00
|
|
|
}
|
|
|
|
|
2007-03-23 19:17:18 +00:00
|
|
|
// Recursively visit successors. If this is a branch or switch on a
|
|
|
|
// constant, only visit the reachable successor.
|
|
|
|
TerminatorInst *TI = BB->getTerminator();
|
|
|
|
if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
|
|
|
|
if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) {
|
|
|
|
bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue();
|
2008-03-09 08:50:23 +00:00
|
|
|
BasicBlock *ReachableBB = BI->getSuccessor(!CondVal);
|
2008-04-25 16:53:59 +00:00
|
|
|
Worklist.push_back(ReachableBB);
|
2007-03-23 19:17:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
|
|
|
|
if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) {
|
|
|
|
// See if this is an explicit destination.
|
|
|
|
for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i)
|
|
|
|
if (SI->getCaseValue(i) == Cond) {
|
2008-03-09 08:50:23 +00:00
|
|
|
BasicBlock *ReachableBB = SI->getSuccessor(i);
|
2008-04-25 16:53:59 +00:00
|
|
|
Worklist.push_back(ReachableBB);
|
2007-03-23 19:17:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise it is the default destination.
|
|
|
|
Worklist.push_back(SI->getSuccessor(0));
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-10 19:00:36 +00:00
|
|
|
}
|
2007-03-23 19:17:18 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
|
|
|
|
Worklist.push_back(TI->getSuccessor(i));
|
2006-05-10 19:00:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-03 02:04:50 +00:00
|
|
|
bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) {
|
2002-04-18 17:39:14 +00:00
|
|
|
bool Changed = false;
|
2003-11-02 05:57:39 +00:00
|
|
|
TD = &getAnalysis<TargetData>();
|
2007-03-03 02:04:50 +00:00
|
|
|
|
|
|
|
DEBUG(DOUT << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on "
|
|
|
|
<< F.getNameStr() << "\n");
|
2001-12-14 16:52:21 +00:00
|
|
|
|
2005-07-07 20:40:38 +00:00
|
|
|
{
|
2006-05-10 19:00:36 +00:00
|
|
|
// Do a depth-first traversal of the function, populate the worklist with
|
|
|
|
// the reachable instructions. Ignore blocks that are not reachable. Keep
|
|
|
|
// track of which blocks we visit.
|
2007-02-15 19:41:52 +00:00
|
|
|
SmallPtrSet<BasicBlock*, 64> Visited;
|
2007-03-02 21:28:56 +00:00
|
|
|
AddReachableCodeToWorklist(F.begin(), Visited, *this, TD);
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-07-07 20:40:38 +00:00
|
|
|
// Do a quick scan over the function. If we find any blocks that are
|
|
|
|
// unreachable, remove any instructions inside of them. This prevents
|
|
|
|
// the instcombine code from having to deal with some bad special cases.
|
|
|
|
for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
|
|
|
|
if (!Visited.count(BB)) {
|
|
|
|
Instruction *Term = BB->getTerminator();
|
|
|
|
while (Term != BB->begin()) { // Remove instrs bottom-up
|
|
|
|
BasicBlock::iterator I = Term; --I;
|
|
|
|
|
2006-11-26 09:46:52 +00:00
|
|
|
DOUT << "IC: DCE: " << *I;
|
2005-07-07 20:40:38 +00:00
|
|
|
++NumDeadInst;
|
|
|
|
|
|
|
|
if (!I->use_empty())
|
|
|
|
I->replaceAllUsesWith(UndefValue::get(I->getType()));
|
|
|
|
I->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-12-14 16:52:21 +00:00
|
|
|
|
2007-03-02 21:28:56 +00:00
|
|
|
while (!Worklist.empty()) {
|
|
|
|
Instruction *I = RemoveOneFromWorkList();
|
|
|
|
if (I == 0) continue; // skip null values.
|
2001-12-14 16:52:21 +00:00
|
|
|
|
2006-05-11 17:11:52 +00:00
|
|
|
// Check to see if we can DCE the instruction.
|
2002-09-02 04:59:56 +00:00
|
|
|
if (isInstructionTriviallyDead(I)) {
|
2006-05-11 17:11:52 +00:00
|
|
|
// Add operands to the worklist.
|
2003-10-06 17:11:01 +00:00
|
|
|
if (I->getNumOperands() < 4)
|
2004-02-28 05:22:00 +00:00
|
|
|
AddUsesToWorkList(*I);
|
2002-09-02 04:59:56 +00:00
|
|
|
++NumDeadInst;
|
2003-10-06 17:11:01 +00:00
|
|
|
|
2006-11-26 09:46:52 +00:00
|
|
|
DOUT << "IC: DCE: " << *I;
|
2005-01-28 19:32:01 +00:00
|
|
|
|
|
|
|
I->eraseFromParent();
|
2007-03-02 21:28:56 +00:00
|
|
|
RemoveFromWorkList(I);
|
2003-10-06 17:11:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
2002-09-02 04:59:56 +00:00
|
|
|
|
2006-05-11 17:11:52 +00:00
|
|
|
// Instruction isn't dead, see if we can constant propagate it.
|
2007-01-30 23:16:15 +00:00
|
|
|
if (Constant *C = ConstantFoldInstruction(I, TD)) {
|
2006-11-26 09:46:52 +00:00
|
|
|
DOUT << "IC: ConstFold to: " << *C << " from: " << *I;
|
2005-01-28 19:32:01 +00:00
|
|
|
|
2006-05-11 17:11:52 +00:00
|
|
|
// Add operands to the worklist.
|
2004-02-28 05:22:00 +00:00
|
|
|
AddUsesToWorkList(*I);
|
2002-12-05 22:41:53 +00:00
|
|
|
ReplaceInstUsesWith(*I, C);
|
|
|
|
|
2002-09-02 04:59:56 +00:00
|
|
|
++NumConstProp;
|
2006-05-10 19:00:36 +00:00
|
|
|
I->eraseFromParent();
|
2007-03-02 21:28:56 +00:00
|
|
|
RemoveFromWorkList(I);
|
2003-10-06 17:11:01 +00:00
|
|
|
continue;
|
2002-09-02 04:59:56 +00:00
|
|
|
}
|
2003-10-06 17:11:01 +00:00
|
|
|
|
2004-12-08 23:43:58 +00:00
|
|
|
// See if we can trivially sink this instruction to a successor basic block.
|
|
|
|
if (I->hasOneUse()) {
|
|
|
|
BasicBlock *BB = I->getParent();
|
|
|
|
BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent();
|
|
|
|
if (UserParent != BB) {
|
|
|
|
bool UserIsSuccessor = false;
|
|
|
|
// See if the user is one of our successors.
|
|
|
|
for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
|
|
|
|
if (*SI == UserParent) {
|
|
|
|
UserIsSuccessor = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the user is one of our immediate successors, and if that successor
|
|
|
|
// only has us as a predecessors (we'd have to split the critical edge
|
|
|
|
// otherwise), we can keep going.
|
|
|
|
if (UserIsSuccessor && !isa<PHINode>(I->use_back()) &&
|
|
|
|
next(pred_begin(UserParent)) == pred_end(UserParent))
|
|
|
|
// Okay, the CFG is simple enough, try to sink this instruction.
|
|
|
|
Changed |= TryToSinkInstruction(I, UserParent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-14 16:52:21 +00:00
|
|
|
// Now that we have an instruction, try combining it to simplify it...
|
2007-03-26 17:44:01 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
std::string OrigI;
|
|
|
|
#endif
|
|
|
|
DEBUG(std::ostringstream SS; I->print(SS); OrigI = SS.str(););
|
2002-08-02 19:29:35 +00:00
|
|
|
if (Instruction *Result = visit(*I)) {
|
2002-05-10 15:38:35 +00:00
|
|
|
++NumCombined;
|
2002-04-18 17:39:14 +00:00
|
|
|
// Should we replace the old instruction with a new one?
|
2002-05-14 15:24:07 +00:00
|
|
|
if (Result != I) {
|
2006-11-26 09:46:52 +00:00
|
|
|
DOUT << "IC: Old = " << *I
|
|
|
|
<< " New = " << *Result;
|
2004-03-13 23:54:27 +00:00
|
|
|
|
Be more careful about the order we put stuff onto the worklist. This allow us to
collapse this:
bool %le(int %A, int %B) {
%c1 = setgt int %A, %B
%tmp = select bool %c1, int 1, int 0
%c2 = setlt int %A, %B
%result = select bool %c2, int -1, int %tmp
%c3 = setle int %result, 0
ret bool %c3
}
into:
bool %le(int %A, int %B) {
%c3 = setle int %A, %B ; <bool> [#uses=1]
ret bool %c3
}
which is handy, because the Java FE makes these sequences all over the place.
This is tested as: test/Regression/Transforms/InstCombine/JavaCompare.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@14086 91177308-0d34-0410-b5e6-96231b3b80d8
2004-06-09 05:08:07 +00:00
|
|
|
// Everything uses the new instruction now.
|
|
|
|
I->replaceAllUsesWith(Result);
|
|
|
|
|
|
|
|
// Push the new instruction and any users onto the worklist.
|
2007-03-02 21:28:56 +00:00
|
|
|
AddToWorkList(Result);
|
Be more careful about the order we put stuff onto the worklist. This allow us to
collapse this:
bool %le(int %A, int %B) {
%c1 = setgt int %A, %B
%tmp = select bool %c1, int 1, int 0
%c2 = setlt int %A, %B
%result = select bool %c2, int -1, int %tmp
%c3 = setle int %result, 0
ret bool %c3
}
into:
bool %le(int %A, int %B) {
%c3 = setle int %A, %B ; <bool> [#uses=1]
ret bool %c3
}
which is handy, because the Java FE makes these sequences all over the place.
This is tested as: test/Regression/Transforms/InstCombine/JavaCompare.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@14086 91177308-0d34-0410-b5e6-96231b3b80d8
2004-06-09 05:08:07 +00:00
|
|
|
AddUsersToWorkList(*Result);
|
2003-10-06 17:11:01 +00:00
|
|
|
|
2007-02-11 01:23:03 +00:00
|
|
|
// Move the name to the new instruction first.
|
|
|
|
Result->takeName(I);
|
2003-10-06 17:11:01 +00:00
|
|
|
|
|
|
|
// Insert the new instruction into the basic block...
|
|
|
|
BasicBlock *InstParent = I->getParent();
|
2004-11-14 19:13:23 +00:00
|
|
|
BasicBlock::iterator InsertPos = I;
|
|
|
|
|
|
|
|
if (!isa<PHINode>(Result)) // If combining a PHI, don't insert
|
|
|
|
while (isa<PHINode>(InsertPos)) // middle of a block of PHIs.
|
|
|
|
++InsertPos;
|
|
|
|
|
|
|
|
InstParent->getInstList().insert(InsertPos, Result);
|
2003-10-06 17:11:01 +00:00
|
|
|
|
2004-05-01 23:27:23 +00:00
|
|
|
// Make sure that we reprocess all operands now that we reduced their
|
|
|
|
// use counts.
|
2007-03-02 21:28:56 +00:00
|
|
|
AddUsesToWorkList(*I);
|
2004-05-01 23:19:52 +00:00
|
|
|
|
Be more careful about the order we put stuff onto the worklist. This allow us to
collapse this:
bool %le(int %A, int %B) {
%c1 = setgt int %A, %B
%tmp = select bool %c1, int 1, int 0
%c2 = setlt int %A, %B
%result = select bool %c2, int -1, int %tmp
%c3 = setle int %result, 0
ret bool %c3
}
into:
bool %le(int %A, int %B) {
%c3 = setle int %A, %B ; <bool> [#uses=1]
ret bool %c3
}
which is handy, because the Java FE makes these sequences all over the place.
This is tested as: test/Regression/Transforms/InstCombine/JavaCompare.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@14086 91177308-0d34-0410-b5e6-96231b3b80d8
2004-06-09 05:08:07 +00:00
|
|
|
// Instructions can end up on the worklist more than once. Make sure
|
|
|
|
// we do not process an instruction that has been deleted.
|
2007-03-02 21:28:56 +00:00
|
|
|
RemoveFromWorkList(I);
|
2003-10-06 17:11:01 +00:00
|
|
|
|
|
|
|
// Erase the old instruction.
|
|
|
|
InstParent->getInstList().erase(I);
|
2002-06-25 16:13:24 +00:00
|
|
|
} else {
|
2007-03-27 16:44:48 +00:00
|
|
|
#ifndef NDEBUG
|
2007-03-26 17:44:01 +00:00
|
|
|
DOUT << "IC: Mod = " << OrigI
|
|
|
|
<< " New = " << *I;
|
2007-03-27 16:44:48 +00:00
|
|
|
#endif
|
2004-03-13 23:54:27 +00:00
|
|
|
|
2002-08-02 19:29:35 +00:00
|
|
|
// If the instruction was modified, it's possible that it is now dead.
|
|
|
|
// if so, remove it.
|
2004-05-01 23:27:23 +00:00
|
|
|
if (isInstructionTriviallyDead(I)) {
|
|
|
|
// Make sure we process all operands now that we are reducing their
|
|
|
|
// use counts.
|
2007-03-03 02:04:50 +00:00
|
|
|
AddUsesToWorkList(*I);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-05-01 23:27:23 +00:00
|
|
|
// Instructions may end up in the worklist more than once. Erase all
|
2006-01-13 22:48:06 +00:00
|
|
|
// occurrences of this instruction.
|
2007-03-02 21:28:56 +00:00
|
|
|
RemoveFromWorkList(I);
|
2005-01-31 05:36:43 +00:00
|
|
|
I->eraseFromParent();
|
Be more careful about the order we put stuff onto the worklist. This allow us to
collapse this:
bool %le(int %A, int %B) {
%c1 = setgt int %A, %B
%tmp = select bool %c1, int 1, int 0
%c2 = setlt int %A, %B
%result = select bool %c2, int -1, int %tmp
%c3 = setle int %result, 0
ret bool %c3
}
into:
bool %le(int %A, int %B) {
%c3 = setle int %A, %B ; <bool> [#uses=1]
ret bool %c3
}
which is handy, because the Java FE makes these sequences all over the place.
This is tested as: test/Regression/Transforms/InstCombine/JavaCompare.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@14086 91177308-0d34-0410-b5e6-96231b3b80d8
2004-06-09 05:08:07 +00:00
|
|
|
} else {
|
2007-03-03 02:04:50 +00:00
|
|
|
AddToWorkList(I);
|
|
|
|
AddUsersToWorkList(*I);
|
2002-08-02 19:29:35 +00:00
|
|
|
}
|
2002-05-14 15:24:07 +00:00
|
|
|
}
|
2002-04-18 17:39:14 +00:00
|
|
|
Changed = true;
|
2001-12-14 16:52:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-03 02:04:50 +00:00
|
|
|
assert(WorklistMap.empty() && "Worklist empty, but map not?");
|
2007-08-05 08:47:58 +00:00
|
|
|
|
|
|
|
// Do an explicit clear, this shrinks the map if needed.
|
|
|
|
WorklistMap.clear();
|
2002-04-18 17:39:14 +00:00
|
|
|
return Changed;
|
2002-02-26 21:46:54 +00:00
|
|
|
}
|
|
|
|
|
2007-03-03 02:04:50 +00:00
|
|
|
|
|
|
|
bool InstCombiner::runOnFunction(Function &F) {
|
2007-03-04 04:27:24 +00:00
|
|
|
MustPreserveLCSSA = mustPreserveAnalysisID(LCSSAID);
|
|
|
|
|
2007-03-03 02:04:50 +00:00
|
|
|
bool EverMadeChange = false;
|
|
|
|
|
|
|
|
// Iterate while there is work to do.
|
|
|
|
unsigned Iteration = 0;
|
|
|
|
while (DoOneIteration(F, Iteration++))
|
|
|
|
EverMadeChange = true;
|
|
|
|
return EverMadeChange;
|
|
|
|
}
|
|
|
|
|
2004-07-27 17:43:21 +00:00
|
|
|
FunctionPass *llvm::createInstructionCombiningPass() {
|
2002-04-18 17:39:14 +00:00
|
|
|
return new InstCombiner();
|
2002-02-26 21:46:54 +00:00
|
|
|
}
|
2003-11-11 22:41:34 +00:00
|
|
|
|