1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===- InstCombineCasts.cpp -----------------------------------------------===// 2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The LLVM Compiler Infrastructure 4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source 6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details. 7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===// 9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file implements the visit functions for cast operations. 11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===// 13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "InstCombine.h" 1519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/Analysis/ConstantFolding.h" 16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetData.h" 17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/PatternMatch.h" 18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanusing namespace llvm; 19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanusing namespace PatternMatch; 20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear 22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// expression. If so, decompose it, returning some value X, such that Val is 23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// X*Scale+Offset. 24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// 25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale, 26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman uint64_t &Offset) { 27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { 28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Offset = CI->getZExtValue(); 29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Scale = 0; 30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return ConstantInt::get(Val->getType(), 0); 31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) { 3419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Cannot look past anything that might overflow. 3519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman OverflowingBinaryOperator *OBI = dyn_cast<OverflowingBinaryOperator>(Val); 3619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (OBI && !OBI->hasNoUnsignedWrap()) { 3719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Scale = 1; 3819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Offset = 0; 3919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return Val; 4019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 4119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { 43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (I->getOpcode() == Instruction::Shl) { 44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // This is a value scaled by '1 << the shift amt'. 45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Scale = UINT64_C(1) << RHS->getZExtValue(); 46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Offset = 0; 47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return I->getOperand(0); 48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (I->getOpcode() == Instruction::Mul) { 51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // This value is scaled by 'RHS'. 52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Scale = RHS->getZExtValue(); 53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Offset = 0; 54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return I->getOperand(0); 55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (I->getOpcode() == Instruction::Add) { 58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We have X+C. Check to see if we really have (X*C2)+C1, 59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // where C1 is divisible by C2. 60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned SubScale; 61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *SubVal = 62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset); 63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Offset += RHS->getZExtValue(); 64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Scale = SubScale; 65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return SubVal; 66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 68894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Otherwise, we can't look past this. 71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Scale = 1; 72894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Offset = 0; 73894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return Val; 74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 76894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// PromoteCastOfAllocation - If we find a cast of an allocation instruction, 77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// try to eliminate the cast by moving the type information into the alloc. 78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanInstruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI, 79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AllocaInst &AI) { 80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // This requires TargetData to get the alloca alignment and size information. 81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!TD) return 0; 82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 8319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman PointerType *PTy = cast<PointerType>(CI.getType()); 84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BuilderTy AllocaBuilder(*Builder); 86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AllocaBuilder.SetInsertPoint(AI.getParent(), &AI); 87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Get the type really allocated and the type casted to. 8919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Type *AllocElTy = AI.getAllocatedType(); 9019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Type *CastElTy = PTy->getElementType(); 91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0; 92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned AllocElTyAlign = TD->getABITypeAlignment(AllocElTy); 94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned CastElTyAlign = TD->getABITypeAlignment(CastElTy); 95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (CastElTyAlign < AllocElTyAlign) return 0; 96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If the allocation has multiple uses, only promote it if we are strictly 98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // increasing the alignment of the resultant allocation. If we keep it the 9919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // same, we open the door to infinite loops of various kinds. 10019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0; 101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman uint64_t AllocElTySize = TD->getTypeAllocSize(AllocElTy); 103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman uint64_t CastElTySize = TD->getTypeAllocSize(CastElTy); 104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (CastElTySize == 0 || AllocElTySize == 0) return 0; 105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // See if we can satisfy the modulus by pulling a scale out of the array 107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // size argument. 108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned ArraySizeScale; 109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman uint64_t ArrayOffset; 110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *NumElements = // See if the array size is a decomposable linear expr. 111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset); 112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If we can now satisfy the modulus, by using a non-1 scale, we really can 114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // do the xform. 115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 || 116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return 0; 117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize; 119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *Amt = 0; 120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Scale == 1) { 121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Amt = NumElements; 122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } else { 123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Amt = ConstantInt::get(AI.getArraySize()->getType(), Scale); 124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Insert before the alloca, not before the cast. 125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Amt = AllocaBuilder.CreateMul(Amt, NumElements); 126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (uint64_t Offset = (AllocElTySize*ArrayOffset)/CastElTySize) { 129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *Off = ConstantInt::get(AI.getArraySize()->getType(), 130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Offset, true); 131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Amt = AllocaBuilder.CreateAdd(Amt, Off); 132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AllocaInst *New = AllocaBuilder.CreateAlloca(CastElTy, Amt); 135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman New->setAlignment(AI.getAlignment()); 136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman New->takeName(&AI); 137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If the allocation has multiple real uses, insert a cast and change all 139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // things that used it to use the new cast. This will also hack on CI, but it 140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // will die soon. 141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!AI.hasOneUse()) { 142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // New is the allocation instruction, pointer typed. AI is the original 143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // allocation instruction, also pointer typed. Thus, cast to use is BitCast. 14419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Value *NewCast = AllocaBuilder.CreateBitCast(New, AI.getType(), "tmpcast"); 14519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ReplaceInstUsesWith(AI, NewCast); 146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return ReplaceInstUsesWith(CI, New); 148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// EvaluateInDifferentType - Given an expression that 153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// CanEvaluateTruncated or CanEvaluateSExtd returns true for, actually 154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// insert the code to evaluate the expression. 15519bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanValue *InstCombiner::EvaluateInDifferentType(Value *V, Type *Ty, 156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool isSigned) { 157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Constant *C = dyn_cast<Constant>(V)) { 158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman C = ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/); 159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If we got a constantexpr back, try to simplify it with TD info. 160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) 161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman C = ConstantFoldConstantExpression(CE, TD); 162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return C; 163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Otherwise, it must be an instruction. 166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Instruction *I = cast<Instruction>(V); 167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Instruction *Res = 0; 168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Opc = I->getOpcode(); 169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman switch (Opc) { 170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Add: 171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Sub: 172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Mul: 173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::And: 174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Or: 175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Xor: 176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::AShr: 177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::LShr: 178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Shl: 179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::UDiv: 180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::URem: { 181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned); 182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); 183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Res = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); 184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman break; 185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Trunc: 187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::ZExt: 188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::SExt: 189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If the source type of the cast is the type we're trying for then we can 190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // just return the source. There's no need to insert it because it is not 191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // new. 192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (I->getOperand(0)->getType() == Ty) 193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return I->getOperand(0); 194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Otherwise, must be the same type of cast, so just reinsert a new one. 196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // This also handles the case of zext(trunc(x)) -> zext(x). 197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Res = CastInst::CreateIntegerCast(I->getOperand(0), Ty, 198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Opc == Instruction::SExt); 199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman break; 200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Select: { 201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *True = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); 202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *False = EvaluateInDifferentType(I->getOperand(2), Ty, isSigned); 203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Res = SelectInst::Create(I->getOperand(0), True, False); 204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman break; 205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::PHI: { 207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman PHINode *OPN = cast<PHINode>(I); 20819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman PHINode *NPN = PHINode::Create(Ty, OPN->getNumIncomingValues()); 209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = OPN->getNumIncomingValues(); i != e; ++i) { 210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *V =EvaluateInDifferentType(OPN->getIncomingValue(i), Ty, isSigned); 211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman NPN->addIncoming(V, OPN->getIncomingBlock(i)); 212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Res = NPN; 214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman break; 215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman default: 217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // TODO: Can handle more cases here. 218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman llvm_unreachable("Unreachable!"); 219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman break; 220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Res->takeName(I); 22319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return InsertNewInstWith(Res, *I); 224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// This function is a wrapper around CastInst::isEliminableCastPair. It 228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// simply extracts arguments and returns what that function returns. 229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic Instruction::CastOps 230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanisEliminableCastPair( 231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const CastInst *CI, ///< The first cast instruction 232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned opcode, ///< The opcode of the second cast instruction 23319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Type *DstTy, ///< The target type for the second cast instruction 234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TargetData *TD ///< The target data for pointer size 235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman) { 236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 23719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Type *SrcTy = CI->getOperand(0)->getType(); // A from above 23819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Type *MidTy = CI->getType(); // B from above 239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Get the opcodes of the two Cast instructions 241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode()); 242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Instruction::CastOps secondOp = Instruction::CastOps(opcode); 243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Res = CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, 245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DstTy, 246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TD ? TD->getIntPtrType(CI->getContext()) : 0); 247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We don't want to form an inttoptr or ptrtoint that converts to an integer 249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // type that differs from the pointer size. 250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if ((Res == Instruction::IntToPtr && 251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman (!TD || SrcTy != TD->getIntPtrType(CI->getContext()))) || 252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman (Res == Instruction::PtrToInt && 253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman (!TD || DstTy != TD->getIntPtrType(CI->getContext())))) 254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Res = 0; 255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return Instruction::CastOps(Res); 257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// ShouldOptimizeCast - Return true if the cast from "V to Ty" actually 260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// results in any code being generated and is interesting to optimize out. If 261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the cast can be eliminated by some other simple transformation, we prefer 262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// to do the simplification first. 263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool InstCombiner::ShouldOptimizeCast(Instruction::CastOps opc, const Value *V, 26419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Type *Ty) { 265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Noop casts and casts of constants should be eliminated trivially. 266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (V->getType() == Ty || isa<Constant>(V)) return false; 267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If this is another cast that can be eliminated, we prefer to have it 269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // eliminated. 270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (const CastInst *CI = dyn_cast<CastInst>(V)) 271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (isEliminableCastPair(CI, opc, Ty, TD)) 272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If this is a vector sext from a compare, then we don't want to break the 275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // idiom where each element of the extended vector is either zero or all ones. 276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (opc == Instruction::SExt && isa<CmpInst>(V) && Ty->isVectorTy()) 277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// @brief Implement the transforms common to all CastInst visitors. 284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanInstruction *InstCombiner::commonCastTransforms(CastInst &CI) { 285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *Src = CI.getOperand(0); 286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Many cases of "cast of a cast" are eliminable. If it's eliminable we just 288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // eliminate it now. 289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast 290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Instruction::CastOps opc = 291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), TD)) { 292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // The first cast (CSrc) is eliminable so we need to fix up or replace 293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // the second cast (CI). CSrc will then have a good chance of being dead. 294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return CastInst::Create(opc, CSrc->getOperand(0), CI.getType()); 295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If we are casting a select then fold the cast into the select 299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SelectInst *SI = dyn_cast<SelectInst>(Src)) 300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Instruction *NV = FoldOpIntoSelect(CI, SI)) 301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return NV; 302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If we are casting a PHI then fold the cast into the PHI 304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (isa<PHINode>(Src)) { 305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We don't do this if this would create a PHI node with an illegal type if 306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // it is currently legal. 307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!Src->getType()->isIntegerTy() || 308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman !CI.getType()->isIntegerTy() || 309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ShouldChangeType(CI.getType(), Src->getType())) 310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Instruction *NV = FoldOpIntoPhi(CI)) 311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return NV; 312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return 0; 315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// CanEvaluateTruncated - Return true if we can evaluate the specified 318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// expression tree as type Ty instead of its larger type, and arrive with the 319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// same value. This is used by code that tries to eliminate truncates. 320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// 321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// Ty will always be a type smaller than V. We should return true if trunc(V) 322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// can be computed by computing V in the smaller type. If V is an instruction, 323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// then trunc(inst(x,y)) can be computed as inst(trunc(x),trunc(y)), which only 324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// makes sense if x and y can be efficiently truncated. 325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// 326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// This function works on both vectors and scalars. 327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// 32819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool CanEvaluateTruncated(Value *V, Type *Ty) { 329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We can always evaluate constants in another type. 330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (isa<Constant>(V)) 331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Instruction *I = dyn_cast<Instruction>(V); 334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!I) return false; 335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 33619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Type *OrigTy = V->getType(); 337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If this is an extension from the dest type, we can eliminate it, even if it 339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // has multiple uses. 340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if ((isa<ZExtInst>(I) || isa<SExtInst>(I)) && 341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman I->getOperand(0)->getType() == Ty) 342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We can't extend or shrink something that has multiple uses: doing so would 345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // require duplicating the instruction in general, which isn't profitable. 346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!I->hasOneUse()) return false; 347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Opc = I->getOpcode(); 349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman switch (Opc) { 350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Add: 351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Sub: 352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Mul: 353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::And: 354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Or: 355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Xor: 356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // These operators can all arbitrarily be extended or truncated. 357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return CanEvaluateTruncated(I->getOperand(0), Ty) && 358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman CanEvaluateTruncated(I->getOperand(1), Ty); 359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::UDiv: 361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::URem: { 362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // UDiv and URem can be truncated if all the truncated bits are zero. 363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits(); 364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman uint32_t BitWidth = Ty->getScalarSizeInBits(); 365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (BitWidth < OrigBitWidth) { 366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman APInt Mask = APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth); 367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (MaskedValueIsZero(I->getOperand(0), Mask) && 368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MaskedValueIsZero(I->getOperand(1), Mask)) { 369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return CanEvaluateTruncated(I->getOperand(0), Ty) && 370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman CanEvaluateTruncated(I->getOperand(1), Ty); 371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman break; 374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Shl: 376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If we are truncating the result of this SHL, and if it's a shift of a 377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // constant amount, we can always perform a SHL in a smaller type. 378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { 379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman uint32_t BitWidth = Ty->getScalarSizeInBits(); 380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (CI->getLimitedValue(BitWidth) < BitWidth) 381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return CanEvaluateTruncated(I->getOperand(0), Ty); 382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman break; 384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::LShr: 385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If this is a truncate of a logical shr, we can truncate it to a smaller 386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // lshr iff we know that the bits we would otherwise be shifting in are 387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // already zeros. 388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { 389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits(); 390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman uint32_t BitWidth = Ty->getScalarSizeInBits(); 391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (MaskedValueIsZero(I->getOperand(0), 392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) && 393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman CI->getLimitedValue(BitWidth) < BitWidth) { 394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return CanEvaluateTruncated(I->getOperand(0), Ty); 395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman break; 398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Trunc: 399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // trunc(trunc(x)) -> trunc(x) 400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 40119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman case Instruction::ZExt: 40219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman case Instruction::SExt: 40319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // trunc(ext(x)) -> ext(x) if the source type is smaller than the new dest 40419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // trunc(ext(x)) -> trunc(x) if the source type is larger than the new dest 40519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return true; 406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Select: { 407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SelectInst *SI = cast<SelectInst>(I); 408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return CanEvaluateTruncated(SI->getTrueValue(), Ty) && 409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman CanEvaluateTruncated(SI->getFalseValue(), Ty); 410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::PHI: { 412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We can change a phi if we can change all operands. Note that we never 413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // get into trouble with cyclic PHIs here because we only consider 414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // instructions with a single use. 415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman PHINode *PN = cast<PHINode>(I); 416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!CanEvaluateTruncated(PN->getIncomingValue(i), Ty)) 418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman default: 422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // TODO: Can handle more cases here. 423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman break; 424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 427894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 428894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanInstruction *InstCombiner::visitTrunc(TruncInst &CI) { 430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Instruction *Result = commonCastTransforms(CI)) 431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return Result; 432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 433894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // See if we can simplify any instructions used by the input whose sole 434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // purpose is to compute bits we don't care about. 435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SimplifyDemandedInstructionBits(CI)) 436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return &CI; 437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *Src = CI.getOperand(0); 43919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Type *DestTy = CI.getType(), *SrcTy = Src->getType(); 440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Attempt to truncate the entire input expression tree to the destination 442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // type. Only do this if the dest type is a simple type, don't convert the 443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // expression tree to something weird like i93 unless the source is also 444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // strange. 445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if ((DestTy->isVectorTy() || ShouldChangeType(SrcTy, DestTy)) && 446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman CanEvaluateTruncated(Src, DestTy)) { 447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If this cast is a truncate, evaluting in a different type always 449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // eliminates the cast, so it is always a win. 450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type" 451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman " to avoid cast: " << CI << '\n'); 452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *Res = EvaluateInDifferentType(Src, DestTy, false); 453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(Res->getType() == DestTy); 454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return ReplaceInstUsesWith(CI, Res); 455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Canonicalize trunc x to i1 -> (icmp ne (and x, 1), 0), likewise for vector. 458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (DestTy->getScalarSizeInBits() == 1) { 459894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Constant *One = ConstantInt::get(Src->getType(), 1); 460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Src = Builder->CreateAnd(Src, One); 461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *Zero = Constant::getNullValue(Src->getType()); 462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return new ICmpInst(ICmpInst::ICMP_NE, Src, Zero); 463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 46419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 46519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Transform trunc(lshr (zext A), Cst) to eliminate one type conversion. 46619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Value *A = 0; ConstantInt *Cst = 0; 46719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (Src->hasOneUse() && 46819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman match(Src, m_LShr(m_ZExt(m_Value(A)), m_ConstantInt(Cst)))) { 46919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // We have three types to worry about here, the type of A, the source of 47019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // the truncate (MidSize), and the destination of the truncate. We know that 47119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // ASize < MidSize and MidSize > ResultSize, but don't know the relation 47219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // between ASize and ResultSize. 47319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned ASize = A->getType()->getPrimitiveSizeInBits(); 47419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 47519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // If the shift amount is larger than the size of A, then the result is 47619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // known to be zero because all the input bits got shifted out. 47719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (Cst->getZExtValue() >= ASize) 47819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return ReplaceInstUsesWith(CI, Constant::getNullValue(CI.getType())); 47919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 48019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Since we're doing an lshr and a zero extend, and know that the shift 48119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // amount is smaller than ASize, it is always safe to do the shift in A's 48219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // type, then zero extend or truncate to the result. 48319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Value *Shift = Builder->CreateLShr(A, Cst->getZExtValue()); 48419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Shift->takeName(Src); 48519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return CastInst::CreateIntegerCast(Shift, CI.getType(), false); 48619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 48719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 48819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Transform "trunc (and X, cst)" -> "and (trunc X), cst" so long as the dest 48919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // type isn't non-native. 49019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (Src->hasOneUse() && isa<IntegerType>(Src->getType()) && 49119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ShouldChangeType(Src->getType(), CI.getType()) && 49219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman match(Src, m_And(m_Value(A), m_ConstantInt(Cst)))) { 49319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Value *NewTrunc = Builder->CreateTrunc(A, CI.getType(), A->getName()+".tr"); 49419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return BinaryOperator::CreateAnd(NewTrunc, 49519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ConstantExpr::getTrunc(Cst, CI.getType())); 49619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return 0; 499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// transformZExtICmp - Transform (zext icmp) to bitwise / integer operations 502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// in order to eliminate the icmp. 503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanInstruction *InstCombiner::transformZExtICmp(ICmpInst *ICI, Instruction &CI, 504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool DoXform) { 505894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If we are just checking for a icmp eq of a single bit and zext'ing it 506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // to an integer, then shift the bit to the appropriate place and then 507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // cast to integer to avoid the comparison. 508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) { 509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const APInt &Op1CV = Op1C->getValue(); 510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // zext (x <s 0) to i32 --> x>>u31 true if signbit set. 512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // zext (x >s -1) to i32 --> (x>>u31)^1 true if signbit clear. 513894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) || 514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())) { 515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!DoXform) return ICI; 516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *In = ICI->getOperand(0); 518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *Sh = ConstantInt::get(In->getType(), 519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman In->getType()->getScalarSizeInBits()-1); 52019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman In = Builder->CreateLShr(In, Sh, In->getName()+".lobit"); 521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (In->getType() != CI.getType()) 522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman In = Builder->CreateIntCast(In, CI.getType(), false/*ZExt*/); 523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (ICI->getPredicate() == ICmpInst::ICMP_SGT) { 525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Constant *One = ConstantInt::get(In->getType(), 1); 52619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman In = Builder->CreateXor(In, One, In->getName()+".not"); 527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 529894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return ReplaceInstUsesWith(CI, In); 530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 532894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 533894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 534894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // zext (X == 0) to i32 --> X^1 iff X has only the low bit set. 535894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. 536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // zext (X == 1) to i32 --> X iff X has only the low bit set. 537894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // zext (X == 2) to i32 --> X>>1 iff X has only the 2nd bit set. 538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // zext (X != 0) to i32 --> X iff X has only the low bit set. 539894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set. 540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // zext (X != 1) to i32 --> X^1 iff X has only the low bit set. 541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // zext (X != 2) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. 542894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if ((Op1CV == 0 || Op1CV.isPowerOf2()) && 543894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // This only works for EQ and NE 544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ICI->isEquality()) { 545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If Op1C some other power of two, convert: 546894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman uint32_t BitWidth = Op1C->getType()->getBitWidth(); 547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); 548894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman APInt TypeMask(APInt::getAllOnesValue(BitWidth)); 549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ComputeMaskedBits(ICI->getOperand(0), TypeMask, KnownZero, KnownOne); 550894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 551894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman APInt KnownZeroMask(~KnownZero); 552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1? 553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!DoXform) return ICI; 554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool isNE = ICI->getPredicate() == ICmpInst::ICMP_NE; 556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Op1CV != 0 && (Op1CV != KnownZeroMask)) { 557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // (X&4) == 2 --> false 558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // (X&4) != 2 --> true 559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Constant *Res = ConstantInt::get(Type::getInt1Ty(CI.getContext()), 560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman isNE); 561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Res = ConstantExpr::getZExt(Res, CI.getType()); 562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return ReplaceInstUsesWith(CI, Res); 563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman uint32_t ShiftAmt = KnownZeroMask.logBase2(); 566894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *In = ICI->getOperand(0); 567894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (ShiftAmt) { 568894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Perform a logical shr by shiftamt. 569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Insert the shift to put the result in the low bit. 57019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman In = Builder->CreateLShr(In, ConstantInt::get(In->getType(),ShiftAmt), 57119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman In->getName()+".lobit"); 572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if ((Op1CV != 0) == isNE) { // Toggle the low bit. 575894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Constant *One = ConstantInt::get(In->getType(), 1); 576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman In = Builder->CreateXor(In, One); 577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (CI.getType() == In->getType()) 580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return ReplaceInstUsesWith(CI, In); 58119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return CastInst::CreateIntegerCast(In, CI.getType(), false/*ZExt*/); 582894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // icmp ne A, B is equal to xor A, B when A and B only really have one bit. 587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // It is also profitable to transform icmp eq into not(xor(A, B)) because that 588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // may lead to additional simplifications. 589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (ICI->isEquality() && CI.getType() == ICI->getOperand(0)->getType()) { 59019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (IntegerType *ITy = dyn_cast<IntegerType>(CI.getType())) { 591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman uint32_t BitWidth = ITy->getBitWidth(); 592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *LHS = ICI->getOperand(0); 593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *RHS = ICI->getOperand(1); 594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman APInt KnownZeroLHS(BitWidth, 0), KnownOneLHS(BitWidth, 0); 596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman APInt KnownZeroRHS(BitWidth, 0), KnownOneRHS(BitWidth, 0); 597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman APInt TypeMask(APInt::getAllOnesValue(BitWidth)); 598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ComputeMaskedBits(LHS, TypeMask, KnownZeroLHS, KnownOneLHS); 599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ComputeMaskedBits(RHS, TypeMask, KnownZeroRHS, KnownOneRHS); 600894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (KnownZeroLHS == KnownZeroRHS && KnownOneLHS == KnownOneRHS) { 602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman APInt KnownBits = KnownZeroLHS | KnownOneLHS; 603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman APInt UnknownBit = ~KnownBits; 604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (UnknownBit.countPopulation() == 1) { 605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!DoXform) return ICI; 606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *Result = Builder->CreateXor(LHS, RHS); 608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Mask off any bits that are set and won't be shifted away. 610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (KnownOneLHS.uge(UnknownBit)) 611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Result = Builder->CreateAnd(Result, 612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ConstantInt::get(ITy, UnknownBit)); 613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Shift the bit we're testing down to the lsb. 615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Result = Builder->CreateLShr( 616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Result, ConstantInt::get(ITy, UnknownBit.countTrailingZeros())); 617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (ICI->getPredicate() == ICmpInst::ICMP_EQ) 619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Result = Builder->CreateXor(Result, ConstantInt::get(ITy, 1)); 620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Result->takeName(ICI); 621894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return ReplaceInstUsesWith(CI, Result); 622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 624894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return 0; 628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// CanEvaluateZExtd - Determine if the specified value can be computed in the 631894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// specified wider type and produce the same low bits. If not, return false. 632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// 633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// If this function returns true, it can also return a non-zero number of bits 634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// (in BitsToClear) which indicates that the value it computes is correct for 635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the zero extend, but that the additional BitsToClear bits need to be zero'd 636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// out. For example, to promote something like: 637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// 638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// %B = trunc i64 %A to i32 639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// %C = lshr i32 %B, 8 640894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// %E = zext i32 %C to i64 641894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// 642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// CanEvaluateZExtd for the 'lshr' will return true, and BitsToClear will be 643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// set to 8 to indicate that the promoted value needs to have bits 24-31 644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// cleared in addition to bits 32-63. Since an 'and' will be generated to 645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// clear the top bits anyway, doing this has no extra cost. 646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// 647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// This function works on both vectors and scalars. 64819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool CanEvaluateZExtd(Value *V, Type *Ty, unsigned &BitsToClear) { 649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitsToClear = 0; 650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (isa<Constant>(V)) 651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 652894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Instruction *I = dyn_cast<Instruction>(V); 654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!I) return false; 655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If the input is a truncate from the destination type, we can trivially 657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // eliminate it, even if it has multiple uses. 658894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // FIXME: This is currently disabled until codegen can handle this without 659894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // pessimizing code, PR5997. 660894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (0 && isa<TruncInst>(I) && I->getOperand(0)->getType() == Ty) 661894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 662894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 663894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We can't extend or shrink something that has multiple uses: doing so would 664894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // require duplicating the instruction in general, which isn't profitable. 665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!I->hasOneUse()) return false; 666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 667894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Opc = I->getOpcode(), Tmp; 668894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman switch (Opc) { 669894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::ZExt: // zext(zext(x)) -> zext(x). 670894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::SExt: // zext(sext(x)) -> sext(x). 671894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Trunc: // zext(trunc(x)) -> trunc(x) or zext(x) 672894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 673894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::And: 674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Or: 675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Xor: 676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Add: 677894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Sub: 678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Mul: 679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Shl: 680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!CanEvaluateZExtd(I->getOperand(0), Ty, BitsToClear) || 681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman !CanEvaluateZExtd(I->getOperand(1), Ty, Tmp)) 682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // These can all be promoted if neither operand has 'bits to clear'. 684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (BitsToClear == 0 && Tmp == 0) 685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If the operation is an AND/OR/XOR and the bits to clear are zero in the 688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // other side, BitsToClear is ok. 689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Tmp == 0 && 690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman (Opc == Instruction::And || Opc == Instruction::Or || 691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Opc == Instruction::Xor)) { 692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We use MaskedValueIsZero here for generality, but the case we care 693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // about the most is constant RHS. 694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned VSize = V->getType()->getScalarSizeInBits(); 695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (MaskedValueIsZero(I->getOperand(1), 696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman APInt::getHighBitsSet(VSize, BitsToClear))) 697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 700894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Otherwise, we don't know how to analyze this BitsToClear case yet. 701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::LShr: 704894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We can promote lshr(x, cst) if we can promote x. This requires the 705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // ultimate 'and' to clear out the high zero bits we're clearing out though. 706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (ConstantInt *Amt = dyn_cast<ConstantInt>(I->getOperand(1))) { 707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!CanEvaluateZExtd(I->getOperand(0), Ty, BitsToClear)) 708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitsToClear += Amt->getZExtValue(); 710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (BitsToClear > V->getType()->getScalarSizeInBits()) 711894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitsToClear = V->getType()->getScalarSizeInBits(); 712894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 713894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Cannot promote variable LSHR. 715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 716894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Select: 717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!CanEvaluateZExtd(I->getOperand(1), Ty, Tmp) || 718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman !CanEvaluateZExtd(I->getOperand(2), Ty, BitsToClear) || 719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // TODO: If important, we could handle the case when the BitsToClear are 720894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // known zero in the disagreeing side. 721894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Tmp != BitsToClear) 722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::PHI: { 726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We can change a phi if we can change all operands. Note that we never 727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // get into trouble with cyclic PHIs here because we only consider 728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // instructions with a single use. 729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman PHINode *PN = cast<PHINode>(I); 730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!CanEvaluateZExtd(PN->getIncomingValue(0), Ty, BitsToClear)) 731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) 733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!CanEvaluateZExtd(PN->getIncomingValue(i), Ty, Tmp) || 734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // TODO: If important, we could handle the case when the BitsToClear 735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // are known zero in the disagreeing input. 736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Tmp != BitsToClear) 737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 740894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman default: 741894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // TODO: Can handle more cases here. 742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 744894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 746894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanInstruction *InstCombiner::visitZExt(ZExtInst &CI) { 747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If this zero extend is only used by a truncate, let the truncate by 748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // eliminated before we try to optimize this zext. 749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (CI.hasOneUse() && isa<TruncInst>(CI.use_back())) 750894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return 0; 751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If one of the common conversion will work, do it. 753894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Instruction *Result = commonCastTransforms(CI)) 754894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return Result; 755894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 756894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // See if we can simplify any instructions used by the input whose sole 757894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // purpose is to compute bits we don't care about. 758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SimplifyDemandedInstructionBits(CI)) 759894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return &CI; 760894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 761894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *Src = CI.getOperand(0); 76219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Type *SrcTy = Src->getType(), *DestTy = CI.getType(); 763894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 764894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Attempt to extend the entire input expression tree to the destination 765894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // type. Only do this if the dest type is a simple type, don't convert the 766894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // expression tree to something weird like i93 unless the source is also 767894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // strange. 768894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned BitsToClear; 769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if ((DestTy->isVectorTy() || ShouldChangeType(SrcTy, DestTy)) && 770894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman CanEvaluateZExtd(Src, DestTy, BitsToClear)) { 771894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(BitsToClear < SrcTy->getScalarSizeInBits() && 772894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "Unreasonable BitsToClear"); 773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 774894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Okay, we can transform this! Insert the new expression now. 775894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type" 776894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman " to avoid zero extend: " << CI); 777894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *Res = EvaluateInDifferentType(Src, DestTy, false); 778894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(Res->getType() == DestTy); 779894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 780894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman uint32_t SrcBitsKept = SrcTy->getScalarSizeInBits()-BitsToClear; 781894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman uint32_t DestBitSize = DestTy->getScalarSizeInBits(); 782894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 783894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If the high bits are already filled with zeros, just replace this 784894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // cast with the result. 785894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (MaskedValueIsZero(Res, APInt::getHighBitsSet(DestBitSize, 786894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DestBitSize-SrcBitsKept))) 787894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return ReplaceInstUsesWith(CI, Res); 788894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 789894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We need to emit an AND to clear the high bits. 790894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Constant *C = ConstantInt::get(Res->getType(), 791894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman APInt::getLowBitsSet(DestBitSize, SrcBitsKept)); 792894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return BinaryOperator::CreateAnd(Res, C); 793894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 794894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 795894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If this is a TRUNC followed by a ZEXT then we are dealing with integral 796894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // types and if the sizes are just right we can convert this into a logical 797894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // 'and' which will be much cheaper than the pair of casts. 798894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (TruncInst *CSrc = dyn_cast<TruncInst>(Src)) { // A->B->C cast 799894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // TODO: Subsume this into EvaluateInDifferentType. 800894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 801894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Get the sizes of the types involved. We know that the intermediate type 802894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // will be smaller than A or C, but don't know the relation between A and C. 803894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *A = CSrc->getOperand(0); 804894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned SrcSize = A->getType()->getScalarSizeInBits(); 805894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned MidSize = CSrc->getType()->getScalarSizeInBits(); 806894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned DstSize = CI.getType()->getScalarSizeInBits(); 807894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If we're actually extending zero bits, then if 808894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // SrcSize < DstSize: zext(a & mask) 809894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // SrcSize == DstSize: a & mask 810894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // SrcSize > DstSize: trunc(a) & mask 811894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SrcSize < DstSize) { 812894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); 813894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Constant *AndConst = ConstantInt::get(A->getType(), AndValue); 81419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Value *And = Builder->CreateAnd(A, AndConst, CSrc->getName()+".mask"); 815894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return new ZExtInst(And, CI.getType()); 816894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 817894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 818894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SrcSize == DstSize) { 819894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); 820894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return BinaryOperator::CreateAnd(A, ConstantInt::get(A->getType(), 821894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AndValue)); 822894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 823894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SrcSize > DstSize) { 824894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *Trunc = Builder->CreateTrunc(A, CI.getType()); 825894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman APInt AndValue(APInt::getLowBitsSet(DstSize, MidSize)); 826894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return BinaryOperator::CreateAnd(Trunc, 827894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ConstantInt::get(Trunc->getType(), 828894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AndValue)); 829894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 830894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 831894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 832894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) 833894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return transformZExtICmp(ICI, CI); 834894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 835894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BinaryOperator *SrcI = dyn_cast<BinaryOperator>(Src); 836894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SrcI && SrcI->getOpcode() == Instruction::Or) { 837894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // zext (or icmp, icmp) --> or (zext icmp), (zext icmp) if at least one 838894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // of the (zext icmp) will be transformed. 839894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ICmpInst *LHS = dyn_cast<ICmpInst>(SrcI->getOperand(0)); 840894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ICmpInst *RHS = dyn_cast<ICmpInst>(SrcI->getOperand(1)); 841894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (LHS && RHS && LHS->hasOneUse() && RHS->hasOneUse() && 842894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman (transformZExtICmp(LHS, CI, false) || 843894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman transformZExtICmp(RHS, CI, false))) { 84419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Value *LCast = Builder->CreateZExt(LHS, CI.getType(), LHS->getName()); 84519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Value *RCast = Builder->CreateZExt(RHS, CI.getType(), RHS->getName()); 846894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return BinaryOperator::Create(Instruction::Or, LCast, RCast); 847894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 848894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 849894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 850894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // zext(trunc(t) & C) -> (t & zext(C)). 851894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SrcI && SrcI->getOpcode() == Instruction::And && SrcI->hasOneUse()) 852894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (ConstantInt *C = dyn_cast<ConstantInt>(SrcI->getOperand(1))) 853894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (TruncInst *TI = dyn_cast<TruncInst>(SrcI->getOperand(0))) { 854894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *TI0 = TI->getOperand(0); 855894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (TI0->getType() == CI.getType()) 856894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return 857894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BinaryOperator::CreateAnd(TI0, 858894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ConstantExpr::getZExt(C, CI.getType())); 859894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 860894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 861894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // zext((trunc(t) & C) ^ C) -> ((t & zext(C)) ^ zext(C)). 862894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SrcI && SrcI->getOpcode() == Instruction::Xor && SrcI->hasOneUse()) 863894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (ConstantInt *C = dyn_cast<ConstantInt>(SrcI->getOperand(1))) 864894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (BinaryOperator *And = dyn_cast<BinaryOperator>(SrcI->getOperand(0))) 865894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (And->getOpcode() == Instruction::And && And->hasOneUse() && 866894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman And->getOperand(1) == C) 867894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (TruncInst *TI = dyn_cast<TruncInst>(And->getOperand(0))) { 868894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *TI0 = TI->getOperand(0); 869894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (TI0->getType() == CI.getType()) { 870894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Constant *ZC = ConstantExpr::getZExt(C, CI.getType()); 871894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *NewAnd = Builder->CreateAnd(TI0, ZC); 872894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return BinaryOperator::CreateXor(NewAnd, ZC); 873894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 874894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 875894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 876894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // zext (xor i1 X, true) to i32 --> xor (zext i1 X to i32), 1 877894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *X; 878894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SrcI && SrcI->hasOneUse() && SrcI->getType()->isIntegerTy(1) && 879894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman match(SrcI, m_Not(m_Value(X))) && 880894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman (!X->hasOneUse() || !isa<CmpInst>(X))) { 881894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *New = Builder->CreateZExt(X, CI.getType()); 882894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return BinaryOperator::CreateXor(New, ConstantInt::get(CI.getType(), 1)); 883894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 884894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 885894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return 0; 886894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 887894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 88819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// transformSExtICmp - Transform (sext icmp) to bitwise / integer operations 88919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// in order to eliminate the icmp. 89019bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanInstruction *InstCombiner::transformSExtICmp(ICmpInst *ICI, Instruction &CI) { 89119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Value *Op0 = ICI->getOperand(0), *Op1 = ICI->getOperand(1); 89219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ICmpInst::Predicate Pred = ICI->getPredicate(); 89319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 89419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { 89519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // (x <s 0) ? -1 : 0 -> ashr x, 31 -> all ones if negative 89619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // (x >s -1) ? -1 : 0 -> not (ashr x, 31) -> all ones if positive 89719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if ((Pred == ICmpInst::ICMP_SLT && Op1C->isZero()) || 89819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman (Pred == ICmpInst::ICMP_SGT && Op1C->isAllOnesValue())) { 89919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 90019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Value *Sh = ConstantInt::get(Op0->getType(), 90119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Op0->getType()->getScalarSizeInBits()-1); 90219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Value *In = Builder->CreateAShr(Op0, Sh, Op0->getName()+".lobit"); 90319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (In->getType() != CI.getType()) 90419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman In = Builder->CreateIntCast(In, CI.getType(), true/*SExt*/); 90519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 90619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (Pred == ICmpInst::ICMP_SGT) 90719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman In = Builder->CreateNot(In, In->getName()+".not"); 90819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return ReplaceInstUsesWith(CI, In); 90919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 91019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 91119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // If we know that only one bit of the LHS of the icmp can be set and we 91219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // have an equality comparison with zero or a power of 2, we can transform 91319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // the icmp and sext into bitwise/integer operations. 91419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (ICI->hasOneUse() && 91519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ICI->isEquality() && (Op1C->isZero() || Op1C->getValue().isPowerOf2())){ 91619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned BitWidth = Op1C->getType()->getBitWidth(); 91719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); 91819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman APInt TypeMask(APInt::getAllOnesValue(BitWidth)); 91919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ComputeMaskedBits(Op0, TypeMask, KnownZero, KnownOne); 92019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 92119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman APInt KnownZeroMask(~KnownZero); 92219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (KnownZeroMask.isPowerOf2()) { 92319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Value *In = ICI->getOperand(0); 92419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 92519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // If the icmp tests for a known zero bit we can constant fold it. 92619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (!Op1C->isZero() && Op1C->getValue() != KnownZeroMask) { 92719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Value *V = Pred == ICmpInst::ICMP_NE ? 92819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ConstantInt::getAllOnesValue(CI.getType()) : 92919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ConstantInt::getNullValue(CI.getType()); 93019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return ReplaceInstUsesWith(CI, V); 93119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 93219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 93319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (!Op1C->isZero() == (Pred == ICmpInst::ICMP_NE)) { 93419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // sext ((x & 2^n) == 0) -> (x >> n) - 1 93519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // sext ((x & 2^n) != 2^n) -> (x >> n) - 1 93619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned ShiftAmt = KnownZeroMask.countTrailingZeros(); 93719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Perform a right shift to place the desired bit in the LSB. 93819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (ShiftAmt) 93919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman In = Builder->CreateLShr(In, 94019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ConstantInt::get(In->getType(), ShiftAmt)); 94119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 94219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // At this point "In" is either 1 or 0. Subtract 1 to turn 94319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // {1, 0} -> {0, -1}. 94419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman In = Builder->CreateAdd(In, 94519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ConstantInt::getAllOnesValue(In->getType()), 94619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman "sext"); 94719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } else { 94819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // sext ((x & 2^n) != 0) -> (x << bitwidth-n) a>> bitwidth-1 94919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // sext ((x & 2^n) == 2^n) -> (x << bitwidth-n) a>> bitwidth-1 95019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned ShiftAmt = KnownZeroMask.countLeadingZeros(); 95119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Perform a left shift to place the desired bit in the MSB. 95219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (ShiftAmt) 95319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman In = Builder->CreateShl(In, 95419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ConstantInt::get(In->getType(), ShiftAmt)); 95519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 95619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Distribute the bit over the whole bit width. 95719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman In = Builder->CreateAShr(In, ConstantInt::get(In->getType(), 95819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman BitWidth - 1), "sext"); 95919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 96019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 96119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (CI.getType() == In->getType()) 96219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return ReplaceInstUsesWith(CI, In); 96319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return CastInst::CreateIntegerCast(In, CI.getType(), true/*SExt*/); 96419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 96519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 96619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 96719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 96819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // vector (x <s 0) ? -1 : 0 -> ashr x, 31 -> all ones if signed. 96919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (VectorType *VTy = dyn_cast<VectorType>(CI.getType())) { 97019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (Pred == ICmpInst::ICMP_SLT && match(Op1, m_Zero()) && 97119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Op0->getType() == CI.getType()) { 97219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Type *EltTy = VTy->getElementType(); 97319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 97419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // splat the shift constant to a constant vector. 97519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Constant *VSh = ConstantInt::get(VTy, EltTy->getScalarSizeInBits()-1); 97619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Value *In = Builder->CreateAShr(Op0, VSh, Op0->getName()+".lobit"); 97719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return ReplaceInstUsesWith(CI, In); 97819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 97919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 98019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 98119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return 0; 98219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman} 98319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 984894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// CanEvaluateSExtd - Return true if we can take the specified value 985894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// and return it as type Ty without inserting any new casts and without 986894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// changing the value of the common low bits. This is used by code that tries 987894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// to promote integer operations to a wider types will allow us to eliminate 988894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the extension. 989894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// 990894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// This function works on both vectors and scalars. 991894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// 99219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool CanEvaluateSExtd(Value *V, Type *Ty) { 993894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(V->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits() && 994894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "Can't sign extend type to a smaller type"); 995894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If this is a constant, it can be trivially promoted. 996894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (isa<Constant>(V)) 997894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 998894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 999894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Instruction *I = dyn_cast<Instruction>(V); 1000894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!I) return false; 1001894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1002894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If this is a truncate from the dest type, we can trivially eliminate it, 1003894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // even if it has multiple uses. 1004894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // FIXME: This is currently disabled until codegen can handle this without 1005894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // pessimizing code, PR5997. 1006894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (0 && isa<TruncInst>(I) && I->getOperand(0)->getType() == Ty) 1007894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 1008894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1009894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We can't extend or shrink something that has multiple uses: doing so would 1010894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // require duplicating the instruction in general, which isn't profitable. 1011894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!I->hasOneUse()) return false; 1012894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1013894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman switch (I->getOpcode()) { 1014894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::SExt: // sext(sext(x)) -> sext(x) 1015894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::ZExt: // sext(zext(x)) -> zext(x) 1016894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Trunc: // sext(trunc(x)) -> trunc(x) or sext(x) 1017894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 1018894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::And: 1019894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Or: 1020894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Xor: 1021894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Add: 1022894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Sub: 1023894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Mul: 1024894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // These operators can all arbitrarily be extended if their inputs can. 1025894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return CanEvaluateSExtd(I->getOperand(0), Ty) && 1026894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman CanEvaluateSExtd(I->getOperand(1), Ty); 1027894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1028894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman //case Instruction::Shl: TODO 1029894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman //case Instruction::LShr: TODO 1030894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1031894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::Select: 1032894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return CanEvaluateSExtd(I->getOperand(1), Ty) && 1033894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman CanEvaluateSExtd(I->getOperand(2), Ty); 1034894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1035894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::PHI: { 1036894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We can change a phi if we can change all operands. Note that we never 1037894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // get into trouble with cyclic PHIs here because we only consider 1038894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // instructions with a single use. 1039894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman PHINode *PN = cast<PHINode>(I); 1040894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 1041894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!CanEvaluateSExtd(PN->getIncomingValue(i), Ty)) return false; 1042894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 1043894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1044894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman default: 1045894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // TODO: Can handle more cases here. 1046894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman break; 1047894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1048894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1049894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 1050894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1051894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1052894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanInstruction *InstCombiner::visitSExt(SExtInst &CI) { 1053894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If this sign extend is only used by a truncate, let the truncate by 1054894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // eliminated before we try to optimize this zext. 1055894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (CI.hasOneUse() && isa<TruncInst>(CI.use_back())) 1056894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return 0; 1057894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1058894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Instruction *I = commonCastTransforms(CI)) 1059894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return I; 1060894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1061894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // See if we can simplify any instructions used by the input whose sole 1062894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // purpose is to compute bits we don't care about. 1063894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SimplifyDemandedInstructionBits(CI)) 1064894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return &CI; 1065894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1066894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *Src = CI.getOperand(0); 106719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Type *SrcTy = Src->getType(), *DestTy = CI.getType(); 1068894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1069894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Attempt to extend the entire input expression tree to the destination 1070894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // type. Only do this if the dest type is a simple type, don't convert the 1071894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // expression tree to something weird like i93 unless the source is also 1072894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // strange. 1073894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if ((DestTy->isVectorTy() || ShouldChangeType(SrcTy, DestTy)) && 1074894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman CanEvaluateSExtd(Src, DestTy)) { 1075894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Okay, we can transform this! Insert the new expression now. 1076894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type" 1077894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman " to avoid sign extend: " << CI); 1078894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *Res = EvaluateInDifferentType(Src, DestTy, true); 1079894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(Res->getType() == DestTy); 1080894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1081894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman uint32_t SrcBitSize = SrcTy->getScalarSizeInBits(); 1082894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman uint32_t DestBitSize = DestTy->getScalarSizeInBits(); 1083894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1084894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If the high bits are already filled with sign bit, just replace this 1085894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // cast with the result. 1086894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (ComputeNumSignBits(Res) > DestBitSize - SrcBitSize) 1087894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return ReplaceInstUsesWith(CI, Res); 1088894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1089894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We need to emit a shl + ashr to do the sign extend. 1090894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *ShAmt = ConstantInt::get(DestTy, DestBitSize-SrcBitSize); 109119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return BinaryOperator::CreateAShr(Builder->CreateShl(Res, ShAmt, "sext"), 1092894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ShAmt); 1093894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1094894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1095894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If this input is a trunc from our destination, then turn sext(trunc(x)) 1096894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // into shifts. 1097894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (TruncInst *TI = dyn_cast<TruncInst>(Src)) 1098894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (TI->hasOneUse() && TI->getOperand(0)->getType() == DestTy) { 1099894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman uint32_t SrcBitSize = SrcTy->getScalarSizeInBits(); 1100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman uint32_t DestBitSize = DestTy->getScalarSizeInBits(); 1101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We need to emit a shl + ashr to do the sign extend. 1103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *ShAmt = ConstantInt::get(DestTy, DestBitSize-SrcBitSize); 110419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Value *Res = Builder->CreateShl(TI->getOperand(0), ShAmt, "sext"); 1105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return BinaryOperator::CreateAShr(Res, ShAmt); 1106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 110719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 110819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) 110919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return transformSExtICmp(ICI, CI); 111019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 1111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If the input is a shl/ashr pair of a same constant, then this is a sign 1112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // extension from a smaller value. If we could trust arbitrary bitwidth 1113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // integers, we could turn this into a truncate to the smaller bit and then 1114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // use a sext for the whole extension. Since we don't, look deeper and check 1115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // for a truncate. If the source and dest are the same type, eliminate the 1116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // trunc and extend and just do shifts. For example, turn: 1117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // %a = trunc i32 %i to i8 1118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // %b = shl i8 %a, 6 1119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // %c = ashr i8 %b, 6 1120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // %d = sext i8 %c to i32 1121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // into: 1122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // %a = shl i32 %i, 30 1123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // %d = ashr i32 %a, 30 1124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *A = 0; 1125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // TODO: Eventually this could be subsumed by EvaluateInDifferentType. 1126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ConstantInt *BA = 0, *CA = 0; 1127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (match(Src, m_AShr(m_Shl(m_Trunc(m_Value(A)), m_ConstantInt(BA)), 1128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman m_ConstantInt(CA))) && 1129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BA == CA && A->getType() == CI.getType()) { 1130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned MidSize = Src->getType()->getScalarSizeInBits(); 1131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned SrcDstSize = CI.getType()->getScalarSizeInBits(); 1132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned ShAmt = CA->getZExtValue()+SrcDstSize-MidSize; 1133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Constant *ShAmtV = ConstantInt::get(CI.getType(), ShAmt); 113419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman A = Builder->CreateShl(A, ShAmtV, CI.getName()); 1135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return BinaryOperator::CreateAShr(A, ShAmtV); 1136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return 0; 1139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// FitsInFPType - Return a Constant* for the specified FP constant if it fits 1143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// in the specified FP type without changing its value. 1144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic Constant *FitsInFPType(ConstantFP *CFP, const fltSemantics &Sem) { 1145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool losesInfo; 1146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman APFloat F = CFP->getValueAPF(); 1147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman (void)F.convert(Sem, APFloat::rmNearestTiesToEven, &losesInfo); 1148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!losesInfo) 1149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return ConstantFP::get(CFP->getContext(), F); 1150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return 0; 1151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// LookThroughFPExtensions - If this is an fp extension instruction, look 1154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// through it until we get the source value. 1155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic Value *LookThroughFPExtensions(Value *V) { 1156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Instruction *I = dyn_cast<Instruction>(V)) 1157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (I->getOpcode() == Instruction::FPExt) 1158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return LookThroughFPExtensions(I->getOperand(0)); 1159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If this value is a constant, return the constant in the smallest FP type 1161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // that can accurately represent it. This allows us to turn 1162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // (float)((double)X+2.0) into x+2.0f. 1163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { 1164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (CFP->getType() == Type::getPPC_FP128Ty(V->getContext())) 1165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return V; // No constant folding of this. 1166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // See if the value can be truncated to float and then reextended. 1167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Value *V = FitsInFPType(CFP, APFloat::IEEEsingle)) 1168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return V; 1169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (CFP->getType()->isDoubleTy()) 1170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return V; // Won't shrink. 1171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Value *V = FitsInFPType(CFP, APFloat::IEEEdouble)) 1172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return V; 1173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Don't try to shrink to various long double types. 1174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return V; 1177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanInstruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) { 1180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Instruction *I = commonCastTransforms(CI)) 1181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return I; 1182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If we have fptrunc(fadd (fpextend x), (fpextend y)), where x and y are 1184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // smaller than the destination type, we can eliminate the truncate by doing 1185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // the add as the smaller type. This applies to fadd/fsub/fmul/fdiv as well 1186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // as many builtins (sqrt, etc). 1187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BinaryOperator *OpI = dyn_cast<BinaryOperator>(CI.getOperand(0)); 1188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (OpI && OpI->hasOneUse()) { 1189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman switch (OpI->getOpcode()) { 1190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman default: break; 1191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::FAdd: 1192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::FSub: 1193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::FMul: 1194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::FDiv: 1195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman case Instruction::FRem: 119619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Type *SrcTy = OpI->getType(); 1197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *LHSTrunc = LookThroughFPExtensions(OpI->getOperand(0)); 1198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *RHSTrunc = LookThroughFPExtensions(OpI->getOperand(1)); 1199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (LHSTrunc->getType() != SrcTy && 1200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RHSTrunc->getType() != SrcTy) { 1201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned DstSize = CI.getType()->getScalarSizeInBits(); 1202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If the source types were both smaller than the destination type of 1203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // the cast, do this xform. 1204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (LHSTrunc->getType()->getScalarSizeInBits() <= DstSize && 1205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RHSTrunc->getType()->getScalarSizeInBits() <= DstSize) { 1206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LHSTrunc = Builder->CreateFPExt(LHSTrunc, CI.getType()); 1207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RHSTrunc = Builder->CreateFPExt(RHSTrunc, CI.getType()); 1208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return BinaryOperator::Create(OpI->getOpcode(), LHSTrunc, RHSTrunc); 1209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman break; 1212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Fold (fptrunc (sqrt (fpext x))) -> (sqrtf x) 1216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // NOTE: This should be disabled by -fno-builtin-sqrt if we ever support it. 1217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman CallInst *Call = dyn_cast<CallInst>(CI.getOperand(0)); 1218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Call && Call->getCalledFunction() && 1219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Call->getCalledFunction()->getName() == "sqrt" && 122019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Call->getNumArgOperands() == 1 && 122119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Call->hasOneUse()) { 1222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman CastInst *Arg = dyn_cast<CastInst>(Call->getArgOperand(0)); 1223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Arg && Arg->getOpcode() == Instruction::FPExt && 1224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman CI.getType()->isFloatTy() && 1225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Call->getType()->isDoubleTy() && 1226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Arg->getType()->isDoubleTy() && 1227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Arg->getOperand(0)->getType()->isFloatTy()) { 1228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Function *Callee = Call->getCalledFunction(); 1229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Module *M = CI.getParent()->getParent()->getParent(); 123019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Constant *SqrtfFunc = M->getOrInsertFunction("sqrtf", 1231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Callee->getAttributes(), 1232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Builder->getFloatTy(), 1233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Builder->getFloatTy(), 1234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman NULL); 1235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman CallInst *ret = CallInst::Create(SqrtfFunc, Arg->getOperand(0), 1236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "sqrtfcall"); 1237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ret->setAttributes(Callee->getAttributes()); 123819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 123919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 124019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Remove the old Call. With -fmath-errno, it won't get marked readnone. 124119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ReplaceInstUsesWith(*Call, UndefValue::get(Call->getType())); 124219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman EraseInstFromFunction(*Call); 1243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return ret; 1244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return 0; 1248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanInstruction *InstCombiner::visitFPExt(CastInst &CI) { 1251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return commonCastTransforms(CI); 1252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanInstruction *InstCombiner::visitFPToUI(FPToUIInst &FI) { 1255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0)); 1256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (OpI == 0) 1257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return commonCastTransforms(FI); 1258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // fptoui(uitofp(X)) --> X 1260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // fptoui(sitofp(X)) --> X 1261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // This is safe if the intermediate type has enough bits in its mantissa to 1262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // accurately represent all values of X. For example, do not do this with 1263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // i64->float->i64. This is also safe for sitofp case, because any negative 1264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // 'X' value would cause an undefined result for the fptoui. 1265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) && 1266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman OpI->getOperand(0)->getType() == FI.getType() && 1267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman (int)FI.getType()->getScalarSizeInBits() < /*extra bit for sign */ 1268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman OpI->getType()->getFPMantissaWidth()) 1269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return ReplaceInstUsesWith(FI, OpI->getOperand(0)); 1270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return commonCastTransforms(FI); 1272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanInstruction *InstCombiner::visitFPToSI(FPToSIInst &FI) { 1275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0)); 1276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (OpI == 0) 1277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return commonCastTransforms(FI); 1278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // fptosi(sitofp(X)) --> X 1280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // fptosi(uitofp(X)) --> X 1281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // This is safe if the intermediate type has enough bits in its mantissa to 1282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // accurately represent all values of X. For example, do not do this with 1283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // i64->float->i64. This is also safe for sitofp case, because any negative 1284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // 'X' value would cause an undefined result for the fptoui. 1285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) && 1286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman OpI->getOperand(0)->getType() == FI.getType() && 1287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman (int)FI.getType()->getScalarSizeInBits() <= 1288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman OpI->getType()->getFPMantissaWidth()) 1289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return ReplaceInstUsesWith(FI, OpI->getOperand(0)); 1290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return commonCastTransforms(FI); 1292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanInstruction *InstCombiner::visitUIToFP(CastInst &CI) { 1295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return commonCastTransforms(CI); 1296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanInstruction *InstCombiner::visitSIToFP(CastInst &CI) { 1299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return commonCastTransforms(CI); 1300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanInstruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) { 1303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If the source integer type is not the intptr_t type for this target, do a 1304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // trunc or zext to the intptr_t type, then inttoptr of it. This allows the 1305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // cast to be exposed to other transforms. 1306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (TD) { 1307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (CI.getOperand(0)->getType()->getScalarSizeInBits() > 1308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TD->getPointerSizeInBits()) { 1309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *P = Builder->CreateTrunc(CI.getOperand(0), 1310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TD->getIntPtrType(CI.getContext())); 1311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return new IntToPtrInst(P, CI.getType()); 1312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (CI.getOperand(0)->getType()->getScalarSizeInBits() < 1314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TD->getPointerSizeInBits()) { 1315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *P = Builder->CreateZExt(CI.getOperand(0), 1316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TD->getIntPtrType(CI.getContext())); 1317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return new IntToPtrInst(P, CI.getType()); 1318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Instruction *I = commonCastTransforms(CI)) 1322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return I; 1323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return 0; 1325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// @brief Implement the transforms for cast of pointer (bitcast/ptrtoint) 1328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanInstruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) { 1329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *Src = CI.getOperand(0); 1330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { 1332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If casting the result of a getelementptr instruction with no offset, turn 1333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // this into a cast of the original pointer! 1334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (GEP->hasAllZeroIndices()) { 1335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Changing the cast operand is usually not a good idea but it is safe 1336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // here because the pointer operand is being replaced with another 1337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // pointer operand so the opcode doesn't need to change. 1338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Worklist.Add(GEP); 1339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman CI.setOperand(0, GEP->getOperand(0)); 1340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return &CI; 1341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If the GEP has a single use, and the base pointer is a bitcast, and the 1344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // GEP computes a constant offset, see if we can convert these three 1345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // instructions into fewer. This typically happens with unions and other 1346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // non-type-safe code. 1347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (TD && GEP->hasOneUse() && isa<BitCastInst>(GEP->getOperand(0)) && 1348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GEP->hasAllConstantIndices()) { 1349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We are guaranteed to get a constant from EmitGEPOffset. 1350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ConstantInt *OffsetV = cast<ConstantInt>(EmitGEPOffset(GEP)); 1351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int64_t Offset = OffsetV->getSExtValue(); 1352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Get the base pointer input of the bitcast, and the type it points to. 1354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *OrigBase = cast<BitCastInst>(GEP->getOperand(0))->getOperand(0); 135519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Type *GEPIdxTy = 1356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman cast<PointerType>(OrigBase->getType())->getElementType(); 1357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<Value*, 8> NewIndices; 1358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (FindElementAtOffset(GEPIdxTy, Offset, NewIndices)) { 1359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If we were able to index down into an element, create the GEP 1360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // and bitcast the result. This eliminates one bitcast, potentially 1361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // two. 1362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *NGEP = cast<GEPOperator>(GEP)->isInBounds() ? 136319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Builder->CreateInBoundsGEP(OrigBase, NewIndices) : 136419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Builder->CreateGEP(OrigBase, NewIndices); 1365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman NGEP->takeName(GEP); 1366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (isa<BitCastInst>(CI)) 1368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return new BitCastInst(NGEP, CI.getType()); 1369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(isa<PtrToIntInst>(CI)); 1370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return new PtrToIntInst(NGEP, CI.getType()); 1371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return commonCastTransforms(CI); 1376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanInstruction *InstCombiner::visitPtrToInt(PtrToIntInst &CI) { 1379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If the destination integer type is not the intptr_t type for this target, 1380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // do a ptrtoint to intptr_t then do a trunc or zext. This allows the cast 1381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // to be exposed to other transforms. 1382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (TD) { 1383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (CI.getType()->getScalarSizeInBits() < TD->getPointerSizeInBits()) { 1384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *P = Builder->CreatePtrToInt(CI.getOperand(0), 1385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TD->getIntPtrType(CI.getContext())); 1386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return new TruncInst(P, CI.getType()); 1387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (CI.getType()->getScalarSizeInBits() > TD->getPointerSizeInBits()) { 1389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *P = Builder->CreatePtrToInt(CI.getOperand(0), 1390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman TD->getIntPtrType(CI.getContext())); 1391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return new ZExtInst(P, CI.getType()); 1392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return commonPointerCastTransforms(CI); 1396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// OptimizeVectorResize - This input value (which is known to have vector type) 1399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// is being zero extended or truncated to the specified vector type. Try to 1400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// replace it with a shuffle (and vector/vector bitcast) if possible. 1401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// 1402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// The source and destination vector types may have different element types. 140319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic Instruction *OptimizeVectorResize(Value *InVal, VectorType *DestTy, 1404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman InstCombiner &IC) { 1405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We can only do this optimization if the output is a multiple of the input 1406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // element size, or the input is a multiple of the output element size. 1407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Convert the input type to have the same element type as the output. 140819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman VectorType *SrcTy = cast<VectorType>(InVal->getType()); 1409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SrcTy->getElementType() != DestTy->getElementType()) { 1411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // The input types don't need to be identical, but for now they must be the 1412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // same size. There is no specific reason we couldn't handle things like 1413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // <4 x i16> -> <4 x i32> by bitcasting to <2 x i32> but haven't gotten 1414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // there yet. 1415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SrcTy->getElementType()->getPrimitiveSizeInBits() != 1416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DestTy->getElementType()->getPrimitiveSizeInBits()) 1417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return 0; 1418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SrcTy = VectorType::get(DestTy->getElementType(), SrcTy->getNumElements()); 1420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman InVal = IC.Builder->CreateBitCast(InVal, SrcTy); 1421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Now that the element types match, get the shuffle mask and RHS of the 1424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // shuffle to use, which depends on whether we're increasing or decreasing the 1425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // size of the input. 1426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<Constant*, 16> ShuffleMask; 1427894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *V2; 142819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman IntegerType *Int32Ty = Type::getInt32Ty(SrcTy->getContext()); 1429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SrcTy->getNumElements() > DestTy->getNumElements()) { 1431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If we're shrinking the number of elements, just shuffle in the low 1432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // elements from the input and use undef as the second shuffle input. 1433894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman V2 = UndefValue::get(SrcTy); 1434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = DestTy->getNumElements(); i != e; ++i) 1435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ShuffleMask.push_back(ConstantInt::get(Int32Ty, i)); 1436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } else { 1438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If we're increasing the number of elements, shuffle in all of the 1439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // elements from InVal and fill the rest of the result elements with zeros 1440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // from a constant zero. 1441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman V2 = Constant::getNullValue(SrcTy); 1442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned SrcElts = SrcTy->getNumElements(); 1443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = SrcElts; i != e; ++i) 1444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ShuffleMask.push_back(ConstantInt::get(Int32Ty, i)); 1445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // The excess elements reference the first element of the zero input. 1447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ShuffleMask.append(DestTy->getNumElements()-SrcElts, 1448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ConstantInt::get(Int32Ty, SrcElts)); 1449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 145119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return new ShuffleVectorInst(InVal, V2, ConstantVector::get(ShuffleMask)); 1452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 145419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool isMultipleOfTypeSize(unsigned Value, Type *Ty) { 145519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return Value % Ty->getPrimitiveSizeInBits() == 0; 145619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman} 145719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 145819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic unsigned getTypeSizeIndex(unsigned Value, Type *Ty) { 145919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return Value / Ty->getPrimitiveSizeInBits(); 146019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman} 146119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 146219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// CollectInsertionElements - V is a value which is inserted into a vector of 146319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// VecEltTy. Look through the value to see if we can decompose it into 146419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// insertions into the vector. See the example in the comment for 146519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// OptimizeIntegerToVectorInsertions for the pattern this handles. 146619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// The type of V is always a non-zero multiple of VecEltTy's size. 146719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 146819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// This returns false if the pattern can't be matched or true if it can, 146919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// filling in Elements with the elements found here. 147019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool CollectInsertionElements(Value *V, unsigned ElementIndex, 147119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman SmallVectorImpl<Value*> &Elements, 147219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Type *VecEltTy) { 147319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Undef values never contribute useful bits to the result. 147419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (isa<UndefValue>(V)) return true; 147519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 147619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // If we got down to a value of the right type, we win, try inserting into the 147719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // right element. 147819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (V->getType() == VecEltTy) { 147919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Inserting null doesn't actually insert any elements. 148019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (Constant *C = dyn_cast<Constant>(V)) 148119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (C->isNullValue()) 148219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return true; 148319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 148419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Fail if multiple elements are inserted into this slot. 148519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (ElementIndex >= Elements.size() || Elements[ElementIndex] != 0) 148619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return false; 148719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 148819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Elements[ElementIndex] = V; 148919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return true; 149019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 149119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 149219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (Constant *C = dyn_cast<Constant>(V)) { 149319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Figure out the # elements this provides, and bitcast it or slice it up 149419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // as required. 149519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned NumElts = getTypeSizeIndex(C->getType()->getPrimitiveSizeInBits(), 149619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman VecEltTy); 149719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // If the constant is the size of a vector element, we just need to bitcast 149819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // it to the right type so it gets properly inserted. 149919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (NumElts == 1) 150019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return CollectInsertionElements(ConstantExpr::getBitCast(C, VecEltTy), 150119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ElementIndex, Elements, VecEltTy); 150219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 150319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Okay, this is a constant that covers multiple elements. Slice it up into 150419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // pieces and insert each element-sized piece into the vector. 150519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (!isa<IntegerType>(C->getType())) 150619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman C = ConstantExpr::getBitCast(C, IntegerType::get(V->getContext(), 150719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman C->getType()->getPrimitiveSizeInBits())); 150819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned ElementSize = VecEltTy->getPrimitiveSizeInBits(); 150919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Type *ElementIntTy = IntegerType::get(C->getContext(), ElementSize); 151019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 151119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman for (unsigned i = 0; i != NumElts; ++i) { 151219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Constant *Piece = ConstantExpr::getLShr(C, ConstantInt::get(C->getType(), 151319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman i*ElementSize)); 151419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Piece = ConstantExpr::getTrunc(Piece, ElementIntTy); 151519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (!CollectInsertionElements(Piece, ElementIndex+i, Elements, VecEltTy)) 151619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return false; 151719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 151819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return true; 151919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 152019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 152119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (!V->hasOneUse()) return false; 152219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 152319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Instruction *I = dyn_cast<Instruction>(V); 152419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (I == 0) return false; 152519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman switch (I->getOpcode()) { 152619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman default: return false; // Unhandled case. 152719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman case Instruction::BitCast: 152819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return CollectInsertionElements(I->getOperand(0), ElementIndex, 152919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Elements, VecEltTy); 153019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman case Instruction::ZExt: 153119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (!isMultipleOfTypeSize( 153219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman I->getOperand(0)->getType()->getPrimitiveSizeInBits(), 153319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman VecEltTy)) 153419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return false; 153519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return CollectInsertionElements(I->getOperand(0), ElementIndex, 153619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Elements, VecEltTy); 153719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman case Instruction::Or: 153819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return CollectInsertionElements(I->getOperand(0), ElementIndex, 153919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Elements, VecEltTy) && 154019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman CollectInsertionElements(I->getOperand(1), ElementIndex, 154119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Elements, VecEltTy); 154219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman case Instruction::Shl: { 154319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Must be shifting by a constant that is a multiple of the element size. 154419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1)); 154519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (CI == 0) return false; 154619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (!isMultipleOfTypeSize(CI->getZExtValue(), VecEltTy)) return false; 154719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned IndexShift = getTypeSizeIndex(CI->getZExtValue(), VecEltTy); 154819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 154919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return CollectInsertionElements(I->getOperand(0), ElementIndex+IndexShift, 155019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Elements, VecEltTy); 155119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 155219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 155319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 155419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman} 155519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 155619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 155719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// OptimizeIntegerToVectorInsertions - If the input is an 'or' instruction, we 155819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// may be doing shifts and ors to assemble the elements of the vector manually. 155919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// Try to rip the code out and replace it with insertelements. This is to 156019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// optimize code like this: 156119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 156219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// %tmp37 = bitcast float %inc to i32 156319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// %tmp38 = zext i32 %tmp37 to i64 156419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// %tmp31 = bitcast float %inc5 to i32 156519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// %tmp32 = zext i32 %tmp31 to i64 156619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// %tmp33 = shl i64 %tmp32, 32 156719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// %ins35 = or i64 %tmp33, %tmp38 156819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// %tmp43 = bitcast i64 %ins35 to <2 x float> 156919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 157019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// Into two insertelements that do "buildvector{%inc, %inc5}". 157119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic Value *OptimizeIntegerToVectorInsertions(BitCastInst &CI, 157219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman InstCombiner &IC) { 157319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman VectorType *DestVecTy = cast<VectorType>(CI.getType()); 157419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Value *IntInput = CI.getOperand(0); 157519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 157619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman SmallVector<Value*, 8> Elements(DestVecTy->getNumElements()); 157719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (!CollectInsertionElements(IntInput, 0, Elements, 157819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman DestVecTy->getElementType())) 157919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return 0; 158019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 158119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // If we succeeded, we know that all of the element are specified by Elements 158219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // or are zero if Elements has a null entry. Recast this as a set of 158319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // insertions. 158419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Value *Result = Constant::getNullValue(CI.getType()); 158519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman for (unsigned i = 0, e = Elements.size(); i != e; ++i) { 158619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (Elements[i] == 0) continue; // Unset element. 158719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 158819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Result = IC.Builder->CreateInsertElement(Result, Elements[i], 158919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman IC.Builder->getInt32(i)); 159019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 159119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 159219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return Result; 159319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman} 159419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 159519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 159619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// OptimizeIntToFloatBitCast - See if we can optimize an integer->float/double 159719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// bitcast. The various long double bitcasts can't get in here. 159819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic Instruction *OptimizeIntToFloatBitCast(BitCastInst &CI,InstCombiner &IC){ 159919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Value *Src = CI.getOperand(0); 160019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Type *DestTy = CI.getType(); 160119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 160219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // If this is a bitcast from int to float, check to see if the int is an 160319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // extraction from a vector. 160419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Value *VecInput = 0; 160519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // bitcast(trunc(bitcast(somevector))) 160619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (match(Src, m_Trunc(m_BitCast(m_Value(VecInput)))) && 160719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman isa<VectorType>(VecInput->getType())) { 160819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman VectorType *VecTy = cast<VectorType>(VecInput->getType()); 160919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned DestWidth = DestTy->getPrimitiveSizeInBits(); 161019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 161119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (VecTy->getPrimitiveSizeInBits() % DestWidth == 0) { 161219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // If the element type of the vector doesn't match the result type, 161319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // bitcast it to be a vector type we can extract from. 161419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (VecTy->getElementType() != DestTy) { 161519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman VecTy = VectorType::get(DestTy, 161619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman VecTy->getPrimitiveSizeInBits() / DestWidth); 161719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman VecInput = IC.Builder->CreateBitCast(VecInput, VecTy); 161819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 161919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 162019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return ExtractElementInst::Create(VecInput, IC.Builder->getInt32(0)); 162119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 162219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 162319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 162419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // bitcast(trunc(lshr(bitcast(somevector), cst)) 162519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ConstantInt *ShAmt = 0; 162619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (match(Src, m_Trunc(m_LShr(m_BitCast(m_Value(VecInput)), 162719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman m_ConstantInt(ShAmt)))) && 162819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman isa<VectorType>(VecInput->getType())) { 162919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman VectorType *VecTy = cast<VectorType>(VecInput->getType()); 163019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned DestWidth = DestTy->getPrimitiveSizeInBits(); 163119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (VecTy->getPrimitiveSizeInBits() % DestWidth == 0 && 163219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ShAmt->getZExtValue() % DestWidth == 0) { 163319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // If the element type of the vector doesn't match the result type, 163419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // bitcast it to be a vector type we can extract from. 163519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (VecTy->getElementType() != DestTy) { 163619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman VecTy = VectorType::get(DestTy, 163719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman VecTy->getPrimitiveSizeInBits() / DestWidth); 163819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman VecInput = IC.Builder->CreateBitCast(VecInput, VecTy); 163919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 164019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 164119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned Elt = ShAmt->getZExtValue() / DestWidth; 164219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return ExtractElementInst::Create(VecInput, IC.Builder->getInt32(Elt)); 164319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 164419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 164519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return 0; 164619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman} 1647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1648894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanInstruction *InstCombiner::visitBitCast(BitCastInst &CI) { 1649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If the operands are integer typed then apply the integer transforms, 1650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // otherwise just apply the common ones. 1651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *Src = CI.getOperand(0); 165219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Type *SrcTy = Src->getType(); 165319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Type *DestTy = CI.getType(); 1654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Get rid of casts from one type to the same type. These are useless and can 1656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // be replaced by the operand. 1657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (DestTy == Src->getType()) 1658894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return ReplaceInstUsesWith(CI, Src); 1659894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 166019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) { 166119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman PointerType *SrcPTy = cast<PointerType>(SrcTy); 166219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Type *DstElTy = DstPTy->getElementType(); 166319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Type *SrcElTy = SrcPTy->getElementType(); 1664894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If the address spaces don't match, don't eliminate the bitcast, which is 1666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // required for changing types. 1667894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SrcPTy->getAddressSpace() != DstPTy->getAddressSpace()) 1668894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return 0; 1669894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1670894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If we are casting a alloca to a pointer to a type of the same 1671894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // size, rewrite the allocation instruction to allocate the "right" type. 1672894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // There is no need to modify malloc calls because it is their bitcast that 1673894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // needs to be cleaned up. 1674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (AllocaInst *AI = dyn_cast<AllocaInst>(Src)) 1675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Instruction *V = PromoteCastOfAllocation(CI, *AI)) 1676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return V; 1677894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If the source and destination are pointers, and this cast is equivalent 1679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // to a getelementptr X, 0, 0, 0... turn it into the appropriate gep. 1680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // This can enhance SROA and other transforms that want type-safe pointers. 1681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Constant *ZeroUInt = 1682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Constant::getNullValue(Type::getInt32Ty(CI.getContext())); 1683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned NumZeros = 0; 1684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman while (SrcElTy != DstElTy && 1685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman isa<CompositeType>(SrcElTy) && !SrcElTy->isPointerTy() && 1686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SrcElTy->getNumContainedTypes() /* not "{}" */) { 1687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt); 1688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++NumZeros; 1689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If we found a path from the src to dest, create the getelementptr now. 1692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SrcElTy == DstElTy) { 1693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt); 169419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return GetElementPtrInst::CreateInBounds(Src, Idxs); 1695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 169719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 169819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Try to optimize int -> float bitcasts. 169919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if ((DestTy->isFloatTy() || DestTy->isDoubleTy()) && isa<IntegerType>(SrcTy)) 170019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (Instruction *I = OptimizeIntToFloatBitCast(CI, *this)) 170119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return I; 1702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 170319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (VectorType *DestVTy = dyn_cast<VectorType>(DestTy)) { 1704894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (DestVTy->getNumElements() == 1 && !SrcTy->isVectorTy()) { 1705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *Elem = Builder->CreateBitCast(Src, DestVTy->getElementType()); 1706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return InsertElementInst::Create(UndefValue::get(DestTy), Elem, 1707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Constant::getNullValue(Type::getInt32Ty(CI.getContext()))); 1708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // FIXME: Canonicalize bitcast(insertelement) -> insertelement(bitcast) 1709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 171119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (isa<IntegerType>(SrcTy)) { 171219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // If this is a cast from an integer to vector, check to see if the input 171319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // is a trunc or zext of a bitcast from vector. If so, we can replace all 171419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // the casts with a shuffle and (potentially) a bitcast. 171519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (isa<TruncInst>(Src) || isa<ZExtInst>(Src)) { 171619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman CastInst *SrcCast = cast<CastInst>(Src); 171719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (BitCastInst *BCIn = dyn_cast<BitCastInst>(SrcCast->getOperand(0))) 171819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (isa<VectorType>(BCIn->getOperand(0)->getType())) 171919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (Instruction *I = OptimizeVectorResize(BCIn->getOperand(0), 1720894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman cast<VectorType>(DestTy), *this)) 172119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return I; 172219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 172319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 172419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // If the input is an 'or' instruction, we may be doing shifts and ors to 172519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // assemble the elements of the vector manually. Try to rip the code out 172619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // and replace it with insertelements. 172719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (Value *V = OptimizeIntegerToVectorInsertions(CI, *this)) 172819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return ReplaceInstUsesWith(CI, V); 1729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 173219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy)) { 1733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SrcVTy->getNumElements() == 1 && !DestTy->isVectorTy()) { 1734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *Elem = 1735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Builder->CreateExtractElement(Src, 1736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Constant::getNullValue(Type::getInt32Ty(CI.getContext()))); 1737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return CastInst::Create(Instruction::BitCast, Elem, DestTy); 1738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1740894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1741894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) { 1742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Okay, we have (bitcast (shuffle ..)). Check to see if this is 1743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // a bitcast to a vector with the same # elts. 1744894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SVI->hasOneUse() && DestTy->isVectorTy() && 1745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman cast<VectorType>(DestTy)->getNumElements() == 1746894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SVI->getType()->getNumElements() && 1747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SVI->getType()->getNumElements() == 1748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements()) { 1749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitCastInst *Tmp; 1750894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If either of the operands is a cast from CI.getType(), then 1751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // evaluating the shuffle in the casted destination's type will allow 1752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // us to eliminate at least one cast. 1753894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (((Tmp = dyn_cast<BitCastInst>(SVI->getOperand(0))) && 1754894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Tmp->getOperand(0)->getType() == DestTy) || 1755894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ((Tmp = dyn_cast<BitCastInst>(SVI->getOperand(1))) && 1756894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Tmp->getOperand(0)->getType() == DestTy)) { 1757894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *LHS = Builder->CreateBitCast(SVI->getOperand(0), DestTy); 1758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *RHS = Builder->CreateBitCast(SVI->getOperand(1), DestTy); 1759894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Return a new shuffle vector. Use the same element ID's, as we 1760894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // know the vector types match #elts. 1761894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2)); 1762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1763894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1764894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1765894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1766894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SrcTy->isPointerTy()) 1767894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return commonPointerCastTransforms(CI); 1768894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return commonCastTransforms(CI); 1769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1770