Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1 | //===- InstCombineCasts.cpp -----------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the visit functions for cast operations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | a917458 | 2015-01-22 05:25:13 +0000 | [diff] [blame] | 14 | #include "InstCombineInternal.h" |
Eli Friedman | 911e12f | 2011-07-20 21:57:23 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/ConstantFolding.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 17 | #include "llvm/IR/PatternMatch.h" |
Chandler Carruth | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | using namespace PatternMatch; |
| 21 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 22 | #define DEBUG_TYPE "instcombine" |
| 23 | |
Sanjay Patel | 2fbab9d8 | 2015-09-09 14:34:26 +0000 | [diff] [blame] | 24 | /// Analyze 'Val', seeing if it is a simple linear expression. |
| 25 | /// If so, decompose it, returning some value X, such that Val is |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 26 | /// X*Scale+Offset. |
| 27 | /// |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 28 | static Value *decomposeSimpleLinearExpr(Value *Val, unsigned &Scale, |
Dan Gohman | 05a6555 | 2010-05-28 04:33:04 +0000 | [diff] [blame] | 29 | uint64_t &Offset) { |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 30 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { |
| 31 | Offset = CI->getZExtValue(); |
| 32 | Scale = 0; |
Dan Gohman | 05a6555 | 2010-05-28 04:33:04 +0000 | [diff] [blame] | 33 | return ConstantInt::get(Val->getType(), 0); |
Chris Lattner | aaccc8d | 2010-01-05 20:57:30 +0000 | [diff] [blame] | 34 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 35 | |
Chris Lattner | aaccc8d | 2010-01-05 20:57:30 +0000 | [diff] [blame] | 36 | if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) { |
Bob Wilson | 3c68b62 | 2011-07-08 22:09:33 +0000 | [diff] [blame] | 37 | // Cannot look past anything that might overflow. |
| 38 | OverflowingBinaryOperator *OBI = dyn_cast<OverflowingBinaryOperator>(Val); |
Stepan Dyatkovskiy | cb2a1a3 | 2012-05-05 07:09:40 +0000 | [diff] [blame] | 39 | if (OBI && !OBI->hasNoUnsignedWrap() && !OBI->hasNoSignedWrap()) { |
Bob Wilson | 3c68b62 | 2011-07-08 22:09:33 +0000 | [diff] [blame] | 40 | Scale = 1; |
| 41 | Offset = 0; |
| 42 | return Val; |
| 43 | } |
| 44 | |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 45 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 46 | if (I->getOpcode() == Instruction::Shl) { |
| 47 | // This is a value scaled by '1 << the shift amt'. |
Dan Gohman | 05a6555 | 2010-05-28 04:33:04 +0000 | [diff] [blame] | 48 | Scale = UINT64_C(1) << RHS->getZExtValue(); |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 49 | Offset = 0; |
| 50 | return I->getOperand(0); |
Chris Lattner | aaccc8d | 2010-01-05 20:57:30 +0000 | [diff] [blame] | 51 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 52 | |
Chris Lattner | aaccc8d | 2010-01-05 20:57:30 +0000 | [diff] [blame] | 53 | if (I->getOpcode() == Instruction::Mul) { |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 54 | // This value is scaled by 'RHS'. |
| 55 | Scale = RHS->getZExtValue(); |
| 56 | Offset = 0; |
| 57 | return I->getOperand(0); |
Chris Lattner | aaccc8d | 2010-01-05 20:57:30 +0000 | [diff] [blame] | 58 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 59 | |
Chris Lattner | aaccc8d | 2010-01-05 20:57:30 +0000 | [diff] [blame] | 60 | if (I->getOpcode() == Instruction::Add) { |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 61 | // We have X+C. Check to see if we really have (X*C2)+C1, |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 62 | // where C1 is divisible by C2. |
| 63 | unsigned SubScale; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 64 | Value *SubVal = |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 65 | decomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset); |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 66 | Offset += RHS->getZExtValue(); |
| 67 | Scale = SubScale; |
| 68 | return SubVal; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // Otherwise, we can't look past this. |
| 74 | Scale = 1; |
| 75 | Offset = 0; |
| 76 | return Val; |
| 77 | } |
| 78 | |
Sanjay Patel | 2fbab9d8 | 2015-09-09 14:34:26 +0000 | [diff] [blame] | 79 | /// If we find a cast of an allocation instruction, try to eliminate the cast by |
| 80 | /// moving the type information into the alloc. |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 81 | Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI, |
| 82 | AllocaInst &AI) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 83 | PointerType *PTy = cast<PointerType>(CI.getType()); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 85 | BuilderTy AllocaBuilder(*Builder); |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame^] | 86 | AllocaBuilder.SetInsertPoint(&AI); |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 87 | |
| 88 | // Get the type really allocated and the type casted to. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 89 | Type *AllocElTy = AI.getAllocatedType(); |
| 90 | Type *CastElTy = PTy->getElementType(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 91 | if (!AllocElTy->isSized() || !CastElTy->isSized()) return nullptr; |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 92 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 93 | unsigned AllocElTyAlign = DL.getABITypeAlignment(AllocElTy); |
| 94 | unsigned CastElTyAlign = DL.getABITypeAlignment(CastElTy); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 95 | if (CastElTyAlign < AllocElTyAlign) return nullptr; |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 96 | |
| 97 | // If the allocation has multiple uses, only promote it if we are strictly |
| 98 | // increasing the alignment of the resultant allocation. If we keep it the |
Devang Patel | fbb482b | 2011-03-08 22:12:11 +0000 | [diff] [blame] | 99 | // same, we open the door to infinite loops of various kinds. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 100 | if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return nullptr; |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 101 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 102 | uint64_t AllocElTySize = DL.getTypeAllocSize(AllocElTy); |
| 103 | uint64_t CastElTySize = DL.getTypeAllocSize(CastElTy); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 104 | if (CastElTySize == 0 || AllocElTySize == 0) return nullptr; |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 105 | |
Jim Grosbach | 95d2eb9 | 2013-03-06 05:44:53 +0000 | [diff] [blame] | 106 | // If the allocation has multiple uses, only promote it if we're not |
| 107 | // shrinking the amount of memory being allocated. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 108 | uint64_t AllocElTyStoreSize = DL.getTypeStoreSize(AllocElTy); |
| 109 | uint64_t CastElTyStoreSize = DL.getTypeStoreSize(CastElTy); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 110 | if (!AI.hasOneUse() && CastElTyStoreSize < AllocElTyStoreSize) return nullptr; |
Jim Grosbach | 95d2eb9 | 2013-03-06 05:44:53 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 112 | // See if we can satisfy the modulus by pulling a scale out of the array |
| 113 | // size argument. |
| 114 | unsigned ArraySizeScale; |
Dan Gohman | 05a6555 | 2010-05-28 04:33:04 +0000 | [diff] [blame] | 115 | uint64_t ArrayOffset; |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 116 | Value *NumElements = // See if the array size is a decomposable linear expr. |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 117 | decomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 118 | |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 119 | // If we can now satisfy the modulus, by using a non-1 scale, we really can |
| 120 | // do the xform. |
| 121 | if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 || |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 122 | (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return nullptr; |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 123 | |
| 124 | unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 125 | Value *Amt = nullptr; |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 126 | if (Scale == 1) { |
| 127 | Amt = NumElements; |
| 128 | } else { |
Dan Gohman | 05a6555 | 2010-05-28 04:33:04 +0000 | [diff] [blame] | 129 | Amt = ConstantInt::get(AI.getArraySize()->getType(), Scale); |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 130 | // Insert before the alloca, not before the cast. |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 131 | Amt = AllocaBuilder.CreateMul(Amt, NumElements); |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 132 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 133 | |
Dan Gohman | 05a6555 | 2010-05-28 04:33:04 +0000 | [diff] [blame] | 134 | if (uint64_t Offset = (AllocElTySize*ArrayOffset)/CastElTySize) { |
| 135 | Value *Off = ConstantInt::get(AI.getArraySize()->getType(), |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 136 | Offset, true); |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 137 | Amt = AllocaBuilder.CreateAdd(Amt, Off); |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 138 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 140 | AllocaInst *New = AllocaBuilder.CreateAlloca(CastElTy, Amt); |
| 141 | New->setAlignment(AI.getAlignment()); |
| 142 | New->takeName(&AI); |
Hans Wennborg | e36e116 | 2014-04-28 17:40:03 +0000 | [diff] [blame] | 143 | New->setUsedWithInAlloca(AI.isUsedWithInAlloca()); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 144 | |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 145 | // If the allocation has multiple real uses, insert a cast and change all |
| 146 | // things that used it to use the new cast. This will also hack on CI, but it |
| 147 | // will die soon. |
Devang Patel | fbb482b | 2011-03-08 22:12:11 +0000 | [diff] [blame] | 148 | if (!AI.hasOneUse()) { |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 149 | // New is the allocation instruction, pointer typed. AI is the original |
| 150 | // allocation instruction, also pointer typed. Thus, cast to use is BitCast. |
| 151 | Value *NewCast = AllocaBuilder.CreateBitCast(New, AI.getType(), "tmpcast"); |
Eli Friedman | b9ed18f | 2011-05-18 00:32:01 +0000 | [diff] [blame] | 152 | ReplaceInstUsesWith(AI, NewCast); |
Chris Lattner | 59d9574 | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 153 | } |
| 154 | return ReplaceInstUsesWith(CI, New); |
| 155 | } |
| 156 | |
Sanjay Patel | 2fbab9d8 | 2015-09-09 14:34:26 +0000 | [diff] [blame] | 157 | /// Given an expression that CanEvaluateTruncated or CanEvaluateSExtd returns |
| 158 | /// true for, actually insert the code to evaluate the expression. |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 159 | Value *InstCombiner::EvaluateInDifferentType(Value *V, Type *Ty, |
Chris Lattner | 92be2ad | 2010-01-04 07:54:59 +0000 | [diff] [blame] | 160 | bool isSigned) { |
Chris Lattner | 9242ae0 | 2010-01-08 19:28:47 +0000 | [diff] [blame] | 161 | if (Constant *C = dyn_cast<Constant>(V)) { |
| 162 | C = ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 163 | // If we got a constantexpr back, try to simplify it with DL info. |
Chris Lattner | 9242ae0 | 2010-01-08 19:28:47 +0000 | [diff] [blame] | 164 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 165 | C = ConstantFoldConstantExpression(CE, DL, TLI); |
Chris Lattner | 9242ae0 | 2010-01-08 19:28:47 +0000 | [diff] [blame] | 166 | return C; |
| 167 | } |
Chris Lattner | 92be2ad | 2010-01-04 07:54:59 +0000 | [diff] [blame] | 168 | |
| 169 | // Otherwise, it must be an instruction. |
| 170 | Instruction *I = cast<Instruction>(V); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 171 | Instruction *Res = nullptr; |
Chris Lattner | 92be2ad | 2010-01-04 07:54:59 +0000 | [diff] [blame] | 172 | unsigned Opc = I->getOpcode(); |
| 173 | switch (Opc) { |
| 174 | case Instruction::Add: |
| 175 | case Instruction::Sub: |
| 176 | case Instruction::Mul: |
| 177 | case Instruction::And: |
| 178 | case Instruction::Or: |
| 179 | case Instruction::Xor: |
| 180 | case Instruction::AShr: |
| 181 | case Instruction::LShr: |
| 182 | case Instruction::Shl: |
| 183 | case Instruction::UDiv: |
| 184 | case Instruction::URem: { |
| 185 | Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned); |
| 186 | Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); |
| 187 | Res = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); |
| 188 | break; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 189 | } |
Chris Lattner | 92be2ad | 2010-01-04 07:54:59 +0000 | [diff] [blame] | 190 | case Instruction::Trunc: |
| 191 | case Instruction::ZExt: |
| 192 | case Instruction::SExt: |
| 193 | // If the source type of the cast is the type we're trying for then we can |
| 194 | // just return the source. There's no need to insert it because it is not |
| 195 | // new. |
| 196 | if (I->getOperand(0)->getType() == Ty) |
| 197 | return I->getOperand(0); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 198 | |
Chris Lattner | 92be2ad | 2010-01-04 07:54:59 +0000 | [diff] [blame] | 199 | // Otherwise, must be the same type of cast, so just reinsert a new one. |
Chris Lattner | 39d2daa | 2010-01-10 20:25:54 +0000 | [diff] [blame] | 200 | // This also handles the case of zext(trunc(x)) -> zext(x). |
| 201 | Res = CastInst::CreateIntegerCast(I->getOperand(0), Ty, |
| 202 | Opc == Instruction::SExt); |
Chris Lattner | 92be2ad | 2010-01-04 07:54:59 +0000 | [diff] [blame] | 203 | break; |
| 204 | case Instruction::Select: { |
| 205 | Value *True = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); |
| 206 | Value *False = EvaluateInDifferentType(I->getOperand(2), Ty, isSigned); |
| 207 | Res = SelectInst::Create(I->getOperand(0), True, False); |
| 208 | break; |
| 209 | } |
| 210 | case Instruction::PHI: { |
| 211 | PHINode *OPN = cast<PHINode>(I); |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 212 | PHINode *NPN = PHINode::Create(Ty, OPN->getNumIncomingValues()); |
Chris Lattner | 92be2ad | 2010-01-04 07:54:59 +0000 | [diff] [blame] | 213 | for (unsigned i = 0, e = OPN->getNumIncomingValues(); i != e; ++i) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 214 | Value *V = |
| 215 | EvaluateInDifferentType(OPN->getIncomingValue(i), Ty, isSigned); |
Chris Lattner | 92be2ad | 2010-01-04 07:54:59 +0000 | [diff] [blame] | 216 | NPN->addIncoming(V, OPN->getIncomingBlock(i)); |
| 217 | } |
| 218 | Res = NPN; |
| 219 | break; |
| 220 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 221 | default: |
Chris Lattner | 92be2ad | 2010-01-04 07:54:59 +0000 | [diff] [blame] | 222 | // TODO: Can handle more cases here. |
| 223 | llvm_unreachable("Unreachable!"); |
Chris Lattner | 92be2ad | 2010-01-04 07:54:59 +0000 | [diff] [blame] | 224 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 225 | |
Chris Lattner | 92be2ad | 2010-01-04 07:54:59 +0000 | [diff] [blame] | 226 | Res->takeName(I); |
Eli Friedman | 35211c6 | 2011-05-27 00:19:40 +0000 | [diff] [blame] | 227 | return InsertNewInstWith(Res, *I); |
Chris Lattner | 92be2ad | 2010-01-04 07:54:59 +0000 | [diff] [blame] | 228 | } |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 229 | |
| 230 | |
| 231 | /// This function is a wrapper around CastInst::isEliminableCastPair. It |
| 232 | /// simply extracts arguments and returns what that function returns. |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 233 | static Instruction::CastOps |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 234 | isEliminableCastPair(const CastInst *CI, ///< First cast instruction |
| 235 | unsigned opcode, ///< Opcode for the second cast |
| 236 | Type *DstTy, ///< Target type for the second cast |
| 237 | const DataLayout &DL) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 238 | Type *SrcTy = CI->getOperand(0)->getType(); // A from above |
| 239 | Type *MidTy = CI->getType(); // B from above |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 240 | |
| 241 | // Get the opcodes of the two Cast instructions |
| 242 | Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode()); |
| 243 | Instruction::CastOps secondOp = Instruction::CastOps(opcode); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 244 | Type *SrcIntPtrTy = |
| 245 | SrcTy->isPtrOrPtrVectorTy() ? DL.getIntPtrType(SrcTy) : nullptr; |
| 246 | Type *MidIntPtrTy = |
| 247 | MidTy->isPtrOrPtrVectorTy() ? DL.getIntPtrType(MidTy) : nullptr; |
| 248 | Type *DstIntPtrTy = |
| 249 | DstTy->isPtrOrPtrVectorTy() ? DL.getIntPtrType(DstTy) : nullptr; |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 250 | unsigned Res = CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 251 | DstTy, SrcIntPtrTy, MidIntPtrTy, |
| 252 | DstIntPtrTy); |
Micah Villmow | 12d9127 | 2012-10-24 15:52:52 +0000 | [diff] [blame] | 253 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 254 | // We don't want to form an inttoptr or ptrtoint that converts to an integer |
| 255 | // type that differs from the pointer size. |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 256 | if ((Res == Instruction::IntToPtr && SrcTy != DstIntPtrTy) || |
| 257 | (Res == Instruction::PtrToInt && DstTy != SrcIntPtrTy)) |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 258 | Res = 0; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 259 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 260 | return Instruction::CastOps(Res); |
| 261 | } |
| 262 | |
Sanjay Patel | 2fbab9d8 | 2015-09-09 14:34:26 +0000 | [diff] [blame] | 263 | /// Return true if the cast from "V to Ty" actually results in any code being |
| 264 | /// generated and is interesting to optimize out. |
| 265 | /// If the cast can be eliminated by some other simple transformation, we prefer |
Chris Lattner | 4e8137d | 2010-02-11 06:26:33 +0000 | [diff] [blame] | 266 | /// to do the simplification first. |
| 267 | bool InstCombiner::ShouldOptimizeCast(Instruction::CastOps opc, const Value *V, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 268 | Type *Ty) { |
Chris Lattner | 4e8137d | 2010-02-11 06:26:33 +0000 | [diff] [blame] | 269 | // Noop casts and casts of constants should be eliminated trivially. |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 270 | if (V->getType() == Ty || isa<Constant>(V)) return false; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 271 | |
Chris Lattner | 4e8137d | 2010-02-11 06:26:33 +0000 | [diff] [blame] | 272 | // If this is another cast that can be eliminated, we prefer to have it |
| 273 | // eliminated. |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 274 | if (const CastInst *CI = dyn_cast<CastInst>(V)) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 275 | if (isEliminableCastPair(CI, opc, Ty, DL)) |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 276 | return false; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 277 | |
Chris Lattner | 4e8137d | 2010-02-11 06:26:33 +0000 | [diff] [blame] | 278 | // If this is a vector sext from a compare, then we don't want to break the |
| 279 | // idiom where each element of the extended vector is either zero or all ones. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 280 | if (opc == Instruction::SExt && isa<CmpInst>(V) && Ty->isVectorTy()) |
Chris Lattner | 4e8137d | 2010-02-11 06:26:33 +0000 | [diff] [blame] | 281 | return false; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 282 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 283 | return true; |
| 284 | } |
| 285 | |
| 286 | |
| 287 | /// @brief Implement the transforms common to all CastInst visitors. |
| 288 | Instruction *InstCombiner::commonCastTransforms(CastInst &CI) { |
| 289 | Value *Src = CI.getOperand(0); |
| 290 | |
| 291 | // Many cases of "cast of a cast" are eliminable. If it's eliminable we just |
| 292 | // eliminate it now. |
| 293 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 294 | if (Instruction::CastOps opc = |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 295 | isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), DL)) { |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 296 | // The first cast (CSrc) is eliminable so we need to fix up or replace |
| 297 | // the second cast (CI). CSrc will then have a good chance of being dead. |
| 298 | return CastInst::Create(opc, CSrc->getOperand(0), CI.getType()); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | // If we are casting a select then fold the cast into the select |
| 303 | if (SelectInst *SI = dyn_cast<SelectInst>(Src)) |
| 304 | if (Instruction *NV = FoldOpIntoSelect(CI, SI)) |
| 305 | return NV; |
| 306 | |
| 307 | // If we are casting a PHI then fold the cast into the PHI |
| 308 | if (isa<PHINode>(Src)) { |
| 309 | // We don't do this if this would create a PHI node with an illegal type if |
| 310 | // it is currently legal. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 311 | if (!Src->getType()->isIntegerTy() || !CI.getType()->isIntegerTy() || |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 312 | ShouldChangeType(CI.getType(), Src->getType())) |
| 313 | if (Instruction *NV = FoldOpIntoPhi(CI)) |
| 314 | return NV; |
| 315 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 316 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 317 | return nullptr; |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Sanjay Patel | 2fbab9d8 | 2015-09-09 14:34:26 +0000 | [diff] [blame] | 320 | /// Return true if we can evaluate the specified expression tree as type Ty |
| 321 | /// instead of its larger type, and arrive with the same value. |
| 322 | /// This is used by code that tries to eliminate truncates. |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 323 | /// |
| 324 | /// Ty will always be a type smaller than V. We should return true if trunc(V) |
| 325 | /// can be computed by computing V in the smaller type. If V is an instruction, |
| 326 | /// then trunc(inst(x,y)) can be computed as inst(trunc(x),trunc(y)), which only |
| 327 | /// makes sense if x and y can be efficiently truncated. |
| 328 | /// |
Chris Lattner | 172630a | 2010-01-11 02:43:35 +0000 | [diff] [blame] | 329 | /// This function works on both vectors and scalars. |
| 330 | /// |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 331 | static bool canEvaluateTruncated(Value *V, Type *Ty, InstCombiner &IC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 332 | Instruction *CxtI) { |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 333 | // We can always evaluate constants in another type. |
| 334 | if (isa<Constant>(V)) |
| 335 | return true; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 336 | |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 337 | Instruction *I = dyn_cast<Instruction>(V); |
| 338 | if (!I) return false; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 339 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 340 | Type *OrigTy = V->getType(); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 341 | |
Chris Lattner | a6b1356 | 2010-01-11 22:45:25 +0000 | [diff] [blame] | 342 | // If this is an extension from the dest type, we can eliminate it, even if it |
| 343 | // has multiple uses. |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 344 | if ((isa<ZExtInst>(I) || isa<SExtInst>(I)) && |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 345 | I->getOperand(0)->getType() == Ty) |
| 346 | return true; |
| 347 | |
| 348 | // We can't extend or shrink something that has multiple uses: doing so would |
| 349 | // require duplicating the instruction in general, which isn't profitable. |
| 350 | if (!I->hasOneUse()) return false; |
| 351 | |
| 352 | unsigned Opc = I->getOpcode(); |
| 353 | switch (Opc) { |
| 354 | case Instruction::Add: |
| 355 | case Instruction::Sub: |
| 356 | case Instruction::Mul: |
| 357 | case Instruction::And: |
| 358 | case Instruction::Or: |
| 359 | case Instruction::Xor: |
| 360 | // These operators can all arbitrarily be extended or truncated. |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 361 | return canEvaluateTruncated(I->getOperand(0), Ty, IC, CxtI) && |
| 362 | canEvaluateTruncated(I->getOperand(1), Ty, IC, CxtI); |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 363 | |
| 364 | case Instruction::UDiv: |
| 365 | case Instruction::URem: { |
| 366 | // UDiv and URem can be truncated if all the truncated bits are zero. |
| 367 | uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits(); |
| 368 | uint32_t BitWidth = Ty->getScalarSizeInBits(); |
| 369 | if (BitWidth < OrigBitWidth) { |
| 370 | APInt Mask = APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 371 | if (IC.MaskedValueIsZero(I->getOperand(0), Mask, 0, CxtI) && |
| 372 | IC.MaskedValueIsZero(I->getOperand(1), Mask, 0, CxtI)) { |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 373 | return canEvaluateTruncated(I->getOperand(0), Ty, IC, CxtI) && |
| 374 | canEvaluateTruncated(I->getOperand(1), Ty, IC, CxtI); |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | break; |
| 378 | } |
| 379 | case Instruction::Shl: |
| 380 | // If we are truncating the result of this SHL, and if it's a shift of a |
| 381 | // constant amount, we can always perform a SHL in a smaller type. |
| 382 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 383 | uint32_t BitWidth = Ty->getScalarSizeInBits(); |
| 384 | if (CI->getLimitedValue(BitWidth) < BitWidth) |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 385 | return canEvaluateTruncated(I->getOperand(0), Ty, IC, CxtI); |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 386 | } |
| 387 | break; |
| 388 | case Instruction::LShr: |
| 389 | // If this is a truncate of a logical shr, we can truncate it to a smaller |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 390 | // lshr iff we know that the bits we would otherwise be shifting in are |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 391 | // already zeros. |
| 392 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 393 | uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits(); |
| 394 | uint32_t BitWidth = Ty->getScalarSizeInBits(); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 395 | if (IC.MaskedValueIsZero(I->getOperand(0), |
| 396 | APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth), 0, CxtI) && |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 397 | CI->getLimitedValue(BitWidth) < BitWidth) { |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 398 | return canEvaluateTruncated(I->getOperand(0), Ty, IC, CxtI); |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | break; |
| 402 | case Instruction::Trunc: |
| 403 | // trunc(trunc(x)) -> trunc(x) |
| 404 | return true; |
Chris Lattner | 7398434 | 2010-08-27 20:32:06 +0000 | [diff] [blame] | 405 | case Instruction::ZExt: |
| 406 | case Instruction::SExt: |
| 407 | // trunc(ext(x)) -> ext(x) if the source type is smaller than the new dest |
| 408 | // trunc(ext(x)) -> trunc(x) if the source type is larger than the new dest |
| 409 | return true; |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 410 | case Instruction::Select: { |
| 411 | SelectInst *SI = cast<SelectInst>(I); |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 412 | return canEvaluateTruncated(SI->getTrueValue(), Ty, IC, CxtI) && |
| 413 | canEvaluateTruncated(SI->getFalseValue(), Ty, IC, CxtI); |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 414 | } |
| 415 | case Instruction::PHI: { |
| 416 | // We can change a phi if we can change all operands. Note that we never |
| 417 | // get into trouble with cyclic PHIs here because we only consider |
| 418 | // instructions with a single use. |
| 419 | PHINode *PN = cast<PHINode>(I); |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 420 | for (Value *IncValue : PN->incoming_values()) |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 421 | if (!canEvaluateTruncated(IncValue, Ty, IC, CxtI)) |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 422 | return false; |
| 423 | return true; |
| 424 | } |
| 425 | default: |
| 426 | // TODO: Can handle more cases here. |
| 427 | break; |
| 428 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 429 | |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 430 | return false; |
| 431 | } |
| 432 | |
| 433 | Instruction *InstCombiner::visitTrunc(TruncInst &CI) { |
Chris Lattner | 883550a | 2010-01-10 01:00:46 +0000 | [diff] [blame] | 434 | if (Instruction *Result = commonCastTransforms(CI)) |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 435 | return Result; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 436 | |
James Molloy | 2b21a7c | 2015-05-20 18:41:25 +0000 | [diff] [blame] | 437 | // Test if the trunc is the user of a select which is part of a |
| 438 | // minimum or maximum operation. If so, don't do any more simplification. |
| 439 | // Even simplifying demanded bits can break the canonical form of a |
| 440 | // min/max. |
| 441 | Value *LHS, *RHS; |
| 442 | if (SelectInst *SI = dyn_cast<SelectInst>(CI.getOperand(0))) |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 443 | if (matchSelectPattern(SI, LHS, RHS).Flavor != SPF_UNKNOWN) |
James Molloy | 2b21a7c | 2015-05-20 18:41:25 +0000 | [diff] [blame] | 444 | return nullptr; |
| 445 | |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 446 | // See if we can simplify any instructions used by the input whose sole |
Chris Lattner | 883550a | 2010-01-10 01:00:46 +0000 | [diff] [blame] | 447 | // purpose is to compute bits we don't care about. |
| 448 | if (SimplifyDemandedInstructionBits(CI)) |
| 449 | return &CI; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 450 | |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 451 | Value *Src = CI.getOperand(0); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 452 | Type *DestTy = CI.getType(), *SrcTy = Src->getType(); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 453 | |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 454 | // Attempt to truncate the entire input expression tree to the destination |
| 455 | // type. Only do this if the dest type is a simple type, don't convert the |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 456 | // expression tree to something weird like i93 unless the source is also |
| 457 | // strange. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 458 | if ((DestTy->isVectorTy() || ShouldChangeType(SrcTy, DestTy)) && |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 459 | canEvaluateTruncated(Src, DestTy, *this, &CI)) { |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 460 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 461 | // If this cast is a truncate, evaluting in a different type always |
Chris Lattner | 8600dd3 | 2010-01-05 23:00:30 +0000 | [diff] [blame] | 462 | // eliminates the cast, so it is always a win. |
Chris Lattner | 3057c37 | 2010-01-07 23:41:00 +0000 | [diff] [blame] | 463 | DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type" |
Dan Gohman | a4abd03 | 2010-05-25 21:50:35 +0000 | [diff] [blame] | 464 | " to avoid cast: " << CI << '\n'); |
Chris Lattner | 3057c37 | 2010-01-07 23:41:00 +0000 | [diff] [blame] | 465 | Value *Res = EvaluateInDifferentType(Src, DestTy, false); |
| 466 | assert(Res->getType() == DestTy); |
| 467 | return ReplaceInstUsesWith(CI, Res); |
| 468 | } |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 469 | |
Chris Lattner | a93c63c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 470 | // Canonicalize trunc x to i1 -> (icmp ne (and x, 1), 0), likewise for vector. |
| 471 | if (DestTy->getScalarSizeInBits() == 1) { |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 472 | Constant *One = ConstantInt::get(Src->getType(), 1); |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 473 | Src = Builder->CreateAnd(Src, One); |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 474 | Value *Zero = Constant::getNullValue(Src->getType()); |
| 475 | return new ICmpInst(ICmpInst::ICMP_NE, Src, Zero); |
| 476 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 477 | |
Chris Lattner | 90cd746 | 2010-08-27 18:31:05 +0000 | [diff] [blame] | 478 | // Transform trunc(lshr (zext A), Cst) to eliminate one type conversion. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 479 | Value *A = nullptr; ConstantInt *Cst = nullptr; |
Chris Lattner | 9c10d58 | 2011-01-15 06:32:33 +0000 | [diff] [blame] | 480 | if (Src->hasOneUse() && |
| 481 | match(Src, m_LShr(m_ZExt(m_Value(A)), m_ConstantInt(Cst)))) { |
Chris Lattner | 90cd746 | 2010-08-27 18:31:05 +0000 | [diff] [blame] | 482 | // We have three types to worry about here, the type of A, the source of |
| 483 | // the truncate (MidSize), and the destination of the truncate. We know that |
| 484 | // ASize < MidSize and MidSize > ResultSize, but don't know the relation |
| 485 | // between ASize and ResultSize. |
| 486 | unsigned ASize = A->getType()->getPrimitiveSizeInBits(); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 487 | |
Chris Lattner | 90cd746 | 2010-08-27 18:31:05 +0000 | [diff] [blame] | 488 | // If the shift amount is larger than the size of A, then the result is |
| 489 | // known to be zero because all the input bits got shifted out. |
| 490 | if (Cst->getZExtValue() >= ASize) |
| 491 | return ReplaceInstUsesWith(CI, Constant::getNullValue(CI.getType())); |
| 492 | |
| 493 | // Since we're doing an lshr and a zero extend, and know that the shift |
| 494 | // amount is smaller than ASize, it is always safe to do the shift in A's |
| 495 | // type, then zero extend or truncate to the result. |
| 496 | Value *Shift = Builder->CreateLShr(A, Cst->getZExtValue()); |
| 497 | Shift->takeName(Src); |
| 498 | return CastInst::CreateIntegerCast(Shift, CI.getType(), false); |
| 499 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 500 | |
Jakub Kuderski | 58ea4ee | 2015-09-10 11:31:20 +0000 | [diff] [blame] | 501 | // Transform trunc(lshr (sext A), Cst) to ashr A, Cst to eliminate type |
| 502 | // conversion. |
| 503 | // It works because bits coming from sign extension have the same value as |
| 504 | // sign bit of the original value; performing ashr instead of lshr |
| 505 | // generates bits of the same value as the sign bit. |
| 506 | if (Src->hasOneUse() && |
| 507 | match(Src, m_LShr(m_SExt(m_Value(A)), m_ConstantInt(Cst))) && |
| 508 | cast<Instruction>(Src)->getOperand(0)->hasOneUse()) { |
| 509 | const unsigned ASize = A->getType()->getPrimitiveSizeInBits(); |
| 510 | // This optimization can be only performed when zero bits generated by |
| 511 | // the original lshr aren't pulled into the value after truncation, so we |
| 512 | // can only shift by values smaller then the size of destination type (in |
| 513 | // bits). |
| 514 | if (Cst->getValue().ult(ASize)) { |
| 515 | Value *Shift = Builder->CreateAShr(A, Cst->getZExtValue()); |
| 516 | Shift->takeName(Src); |
| 517 | return CastInst::CreateIntegerCast(Shift, CI.getType(), true); |
| 518 | } |
| 519 | } |
| 520 | |
Chris Lattner | 9c10d58 | 2011-01-15 06:32:33 +0000 | [diff] [blame] | 521 | // Transform "trunc (and X, cst)" -> "and (trunc X), cst" so long as the dest |
| 522 | // type isn't non-native. |
| 523 | if (Src->hasOneUse() && isa<IntegerType>(Src->getType()) && |
| 524 | ShouldChangeType(Src->getType(), CI.getType()) && |
| 525 | match(Src, m_And(m_Value(A), m_ConstantInt(Cst)))) { |
| 526 | Value *NewTrunc = Builder->CreateTrunc(A, CI.getType(), A->getName()+".tr"); |
| 527 | return BinaryOperator::CreateAnd(NewTrunc, |
| 528 | ConstantExpr::getTrunc(Cst, CI.getType())); |
| 529 | } |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 530 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 531 | return nullptr; |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Sanjay Patel | 2fbab9d8 | 2015-09-09 14:34:26 +0000 | [diff] [blame] | 534 | /// Transform (zext icmp) to bitwise / integer operations in order to eliminate |
| 535 | /// the icmp. |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 536 | Instruction *InstCombiner::transformZExtICmp(ICmpInst *ICI, Instruction &CI, |
| 537 | bool DoXform) { |
| 538 | // If we are just checking for a icmp eq of a single bit and zext'ing it |
| 539 | // to an integer, then shift the bit to the appropriate place and then |
| 540 | // cast to integer to avoid the comparison. |
| 541 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) { |
| 542 | const APInt &Op1CV = Op1C->getValue(); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 543 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 544 | // zext (x <s 0) to i32 --> x>>u31 true if signbit set. |
| 545 | // zext (x >s -1) to i32 --> (x>>u31)^1 true if signbit clear. |
| 546 | if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) || |
| 547 | (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())) { |
| 548 | if (!DoXform) return ICI; |
| 549 | |
| 550 | Value *In = ICI->getOperand(0); |
| 551 | Value *Sh = ConstantInt::get(In->getType(), |
| 552 | In->getType()->getScalarSizeInBits()-1); |
| 553 | In = Builder->CreateLShr(In, Sh, In->getName()+".lobit"); |
| 554 | if (In->getType() != CI.getType()) |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 555 | In = Builder->CreateIntCast(In, CI.getType(), false/*ZExt*/); |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 556 | |
| 557 | if (ICI->getPredicate() == ICmpInst::ICMP_SGT) { |
| 558 | Constant *One = ConstantInt::get(In->getType(), 1); |
| 559 | In = Builder->CreateXor(In, One, In->getName()+".not"); |
| 560 | } |
| 561 | |
| 562 | return ReplaceInstUsesWith(CI, In); |
| 563 | } |
Chad Rosier | 385d9f6 | 2011-11-30 01:59:59 +0000 | [diff] [blame] | 564 | |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 565 | // zext (X == 0) to i32 --> X^1 iff X has only the low bit set. |
| 566 | // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. |
| 567 | // zext (X == 1) to i32 --> X iff X has only the low bit set. |
| 568 | // zext (X == 2) to i32 --> X>>1 iff X has only the 2nd bit set. |
| 569 | // zext (X != 0) to i32 --> X iff X has only the low bit set. |
| 570 | // zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set. |
| 571 | // zext (X != 1) to i32 --> X^1 iff X has only the low bit set. |
| 572 | // zext (X != 2) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 573 | if ((Op1CV == 0 || Op1CV.isPowerOf2()) && |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 574 | // This only works for EQ and NE |
| 575 | ICI->isEquality()) { |
| 576 | // If Op1C some other power of two, convert: |
| 577 | uint32_t BitWidth = Op1C->getType()->getBitWidth(); |
| 578 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 579 | computeKnownBits(ICI->getOperand(0), KnownZero, KnownOne, 0, &CI); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 580 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 581 | APInt KnownZeroMask(~KnownZero); |
| 582 | if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1? |
| 583 | if (!DoXform) return ICI; |
| 584 | |
| 585 | bool isNE = ICI->getPredicate() == ICmpInst::ICMP_NE; |
| 586 | if (Op1CV != 0 && (Op1CV != KnownZeroMask)) { |
| 587 | // (X&4) == 2 --> false |
| 588 | // (X&4) != 2 --> true |
| 589 | Constant *Res = ConstantInt::get(Type::getInt1Ty(CI.getContext()), |
| 590 | isNE); |
| 591 | Res = ConstantExpr::getZExt(Res, CI.getType()); |
| 592 | return ReplaceInstUsesWith(CI, Res); |
| 593 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 594 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 595 | uint32_t ShiftAmt = KnownZeroMask.logBase2(); |
| 596 | Value *In = ICI->getOperand(0); |
| 597 | if (ShiftAmt) { |
| 598 | // Perform a logical shr by shiftamt. |
| 599 | // Insert the shift to put the result in the low bit. |
| 600 | In = Builder->CreateLShr(In, ConstantInt::get(In->getType(),ShiftAmt), |
| 601 | In->getName()+".lobit"); |
| 602 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 603 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 604 | if ((Op1CV != 0) == isNE) { // Toggle the low bit. |
| 605 | Constant *One = ConstantInt::get(In->getType(), 1); |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 606 | In = Builder->CreateXor(In, One); |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 607 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 608 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 609 | if (CI.getType() == In->getType()) |
| 610 | return ReplaceInstUsesWith(CI, In); |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 611 | return CastInst::CreateIntegerCast(In, CI.getType(), false/*ZExt*/); |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | // icmp ne A, B is equal to xor A, B when A and B only really have one bit. |
| 617 | // It is also profitable to transform icmp eq into not(xor(A, B)) because that |
| 618 | // may lead to additional simplifications. |
| 619 | if (ICI->isEquality() && CI.getType() == ICI->getOperand(0)->getType()) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 620 | if (IntegerType *ITy = dyn_cast<IntegerType>(CI.getType())) { |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 621 | uint32_t BitWidth = ITy->getBitWidth(); |
| 622 | Value *LHS = ICI->getOperand(0); |
| 623 | Value *RHS = ICI->getOperand(1); |
| 624 | |
| 625 | APInt KnownZeroLHS(BitWidth, 0), KnownOneLHS(BitWidth, 0); |
| 626 | APInt KnownZeroRHS(BitWidth, 0), KnownOneRHS(BitWidth, 0); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 627 | computeKnownBits(LHS, KnownZeroLHS, KnownOneLHS, 0, &CI); |
| 628 | computeKnownBits(RHS, KnownZeroRHS, KnownOneRHS, 0, &CI); |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 629 | |
| 630 | if (KnownZeroLHS == KnownZeroRHS && KnownOneLHS == KnownOneRHS) { |
| 631 | APInt KnownBits = KnownZeroLHS | KnownOneLHS; |
| 632 | APInt UnknownBit = ~KnownBits; |
| 633 | if (UnknownBit.countPopulation() == 1) { |
| 634 | if (!DoXform) return ICI; |
| 635 | |
| 636 | Value *Result = Builder->CreateXor(LHS, RHS); |
| 637 | |
| 638 | // Mask off any bits that are set and won't be shifted away. |
| 639 | if (KnownOneLHS.uge(UnknownBit)) |
| 640 | Result = Builder->CreateAnd(Result, |
| 641 | ConstantInt::get(ITy, UnknownBit)); |
| 642 | |
| 643 | // Shift the bit we're testing down to the lsb. |
| 644 | Result = Builder->CreateLShr( |
| 645 | Result, ConstantInt::get(ITy, UnknownBit.countTrailingZeros())); |
| 646 | |
| 647 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) |
| 648 | Result = Builder->CreateXor(Result, ConstantInt::get(ITy, 1)); |
| 649 | Result->takeName(ICI); |
| 650 | return ReplaceInstUsesWith(CI, Result); |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 656 | return nullptr; |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Sanjay Patel | 2fbab9d8 | 2015-09-09 14:34:26 +0000 | [diff] [blame] | 659 | /// Determine if the specified value can be computed in the specified wider type |
| 660 | /// and produce the same low bits. If not, return false. |
Chris Lattner | 172630a | 2010-01-11 02:43:35 +0000 | [diff] [blame] | 661 | /// |
Chris Lattner | 12bd899 | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 662 | /// If this function returns true, it can also return a non-zero number of bits |
| 663 | /// (in BitsToClear) which indicates that the value it computes is correct for |
| 664 | /// the zero extend, but that the additional BitsToClear bits need to be zero'd |
| 665 | /// out. For example, to promote something like: |
| 666 | /// |
| 667 | /// %B = trunc i64 %A to i32 |
| 668 | /// %C = lshr i32 %B, 8 |
| 669 | /// %E = zext i32 %C to i64 |
| 670 | /// |
| 671 | /// CanEvaluateZExtd for the 'lshr' will return true, and BitsToClear will be |
| 672 | /// set to 8 to indicate that the promoted value needs to have bits 24-31 |
| 673 | /// cleared in addition to bits 32-63. Since an 'and' will be generated to |
| 674 | /// clear the top bits anyway, doing this has no extra cost. |
| 675 | /// |
Chris Lattner | 172630a | 2010-01-11 02:43:35 +0000 | [diff] [blame] | 676 | /// This function works on both vectors and scalars. |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 677 | static bool canEvaluateZExtd(Value *V, Type *Ty, unsigned &BitsToClear, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 678 | InstCombiner &IC, Instruction *CxtI) { |
Chris Lattner | 12bd899 | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 679 | BitsToClear = 0; |
Chris Lattner | b7be7cc | 2010-01-10 02:50:04 +0000 | [diff] [blame] | 680 | if (isa<Constant>(V)) |
| 681 | return true; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 682 | |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 683 | Instruction *I = dyn_cast<Instruction>(V); |
Chris Lattner | b7be7cc | 2010-01-10 02:50:04 +0000 | [diff] [blame] | 684 | if (!I) return false; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 685 | |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 686 | // If the input is a truncate from the destination type, we can trivially |
Jakob Stoklund Olesen | c5c4e96 | 2012-06-22 16:36:43 +0000 | [diff] [blame] | 687 | // eliminate it. |
| 688 | if (isa<TruncInst>(I) && I->getOperand(0)->getType() == Ty) |
Chris Lattner | b7be7cc | 2010-01-10 02:50:04 +0000 | [diff] [blame] | 689 | return true; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 690 | |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 691 | // We can't extend or shrink something that has multiple uses: doing so would |
| 692 | // require duplicating the instruction in general, which isn't profitable. |
Chris Lattner | b7be7cc | 2010-01-10 02:50:04 +0000 | [diff] [blame] | 693 | if (!I->hasOneUse()) return false; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 694 | |
Chris Lattner | 12bd899 | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 695 | unsigned Opc = I->getOpcode(), Tmp; |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 696 | switch (Opc) { |
Chris Lattner | 39d2daa | 2010-01-10 20:25:54 +0000 | [diff] [blame] | 697 | case Instruction::ZExt: // zext(zext(x)) -> zext(x). |
| 698 | case Instruction::SExt: // zext(sext(x)) -> sext(x). |
| 699 | case Instruction::Trunc: // zext(trunc(x)) -> trunc(x) or zext(x) |
| 700 | return true; |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 701 | case Instruction::And: |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 702 | case Instruction::Or: |
| 703 | case Instruction::Xor: |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 704 | case Instruction::Add: |
| 705 | case Instruction::Sub: |
| 706 | case Instruction::Mul: |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 707 | if (!canEvaluateZExtd(I->getOperand(0), Ty, BitsToClear, IC, CxtI) || |
| 708 | !canEvaluateZExtd(I->getOperand(1), Ty, Tmp, IC, CxtI)) |
Chris Lattner | 12bd899 | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 709 | return false; |
| 710 | // These can all be promoted if neither operand has 'bits to clear'. |
| 711 | if (BitsToClear == 0 && Tmp == 0) |
| 712 | return true; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 713 | |
Chris Lattner | 0a85420 | 2010-01-11 04:05:13 +0000 | [diff] [blame] | 714 | // If the operation is an AND/OR/XOR and the bits to clear are zero in the |
| 715 | // other side, BitsToClear is ok. |
| 716 | if (Tmp == 0 && |
| 717 | (Opc == Instruction::And || Opc == Instruction::Or || |
| 718 | Opc == Instruction::Xor)) { |
| 719 | // We use MaskedValueIsZero here for generality, but the case we care |
| 720 | // about the most is constant RHS. |
| 721 | unsigned VSize = V->getType()->getScalarSizeInBits(); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 722 | if (IC.MaskedValueIsZero(I->getOperand(1), |
| 723 | APInt::getHighBitsSet(VSize, BitsToClear), |
| 724 | 0, CxtI)) |
Chris Lattner | 0a85420 | 2010-01-11 04:05:13 +0000 | [diff] [blame] | 725 | return true; |
| 726 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 727 | |
Chris Lattner | 0a85420 | 2010-01-11 04:05:13 +0000 | [diff] [blame] | 728 | // Otherwise, we don't know how to analyze this BitsToClear case yet. |
Chris Lattner | 12bd899 | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 729 | return false; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 730 | |
Benjamin Kramer | 14e915f | 2013-05-10 16:26:37 +0000 | [diff] [blame] | 731 | case Instruction::Shl: |
| 732 | // We can promote shl(x, cst) if we can promote x. Since shl overwrites the |
| 733 | // upper bits we can reduce BitsToClear by the shift amount. |
| 734 | if (ConstantInt *Amt = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 735 | if (!canEvaluateZExtd(I->getOperand(0), Ty, BitsToClear, IC, CxtI)) |
Benjamin Kramer | 14e915f | 2013-05-10 16:26:37 +0000 | [diff] [blame] | 736 | return false; |
| 737 | uint64_t ShiftAmt = Amt->getZExtValue(); |
| 738 | BitsToClear = ShiftAmt < BitsToClear ? BitsToClear - ShiftAmt : 0; |
| 739 | return true; |
| 740 | } |
| 741 | return false; |
Chris Lattner | 12bd899 | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 742 | case Instruction::LShr: |
| 743 | // We can promote lshr(x, cst) if we can promote x. This requires the |
| 744 | // ultimate 'and' to clear out the high zero bits we're clearing out though. |
| 745 | if (ConstantInt *Amt = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 746 | if (!canEvaluateZExtd(I->getOperand(0), Ty, BitsToClear, IC, CxtI)) |
Chris Lattner | 12bd899 | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 747 | return false; |
| 748 | BitsToClear += Amt->getZExtValue(); |
| 749 | if (BitsToClear > V->getType()->getScalarSizeInBits()) |
| 750 | BitsToClear = V->getType()->getScalarSizeInBits(); |
| 751 | return true; |
| 752 | } |
| 753 | // Cannot promote variable LSHR. |
| 754 | return false; |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 755 | case Instruction::Select: |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 756 | if (!canEvaluateZExtd(I->getOperand(1), Ty, Tmp, IC, CxtI) || |
| 757 | !canEvaluateZExtd(I->getOperand(2), Ty, BitsToClear, IC, CxtI) || |
Chris Lattner | 0a85420 | 2010-01-11 04:05:13 +0000 | [diff] [blame] | 758 | // TODO: If important, we could handle the case when the BitsToClear are |
| 759 | // known zero in the disagreeing side. |
Chris Lattner | 12bd899 | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 760 | Tmp != BitsToClear) |
| 761 | return false; |
| 762 | return true; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 763 | |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 764 | case Instruction::PHI: { |
| 765 | // We can change a phi if we can change all operands. Note that we never |
| 766 | // get into trouble with cyclic PHIs here because we only consider |
| 767 | // instructions with a single use. |
| 768 | PHINode *PN = cast<PHINode>(I); |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 769 | if (!canEvaluateZExtd(PN->getIncomingValue(0), Ty, BitsToClear, IC, CxtI)) |
Chris Lattner | 12bd899 | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 770 | return false; |
Chris Lattner | b7be7cc | 2010-01-10 02:50:04 +0000 | [diff] [blame] | 771 | for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 772 | if (!canEvaluateZExtd(PN->getIncomingValue(i), Ty, Tmp, IC, CxtI) || |
Chris Lattner | 0a85420 | 2010-01-11 04:05:13 +0000 | [diff] [blame] | 773 | // TODO: If important, we could handle the case when the BitsToClear |
| 774 | // are known zero in the disagreeing input. |
Chris Lattner | 12bd899 | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 775 | Tmp != BitsToClear) |
| 776 | return false; |
Chris Lattner | b7be7cc | 2010-01-10 02:50:04 +0000 | [diff] [blame] | 777 | return true; |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 778 | } |
| 779 | default: |
| 780 | // TODO: Can handle more cases here. |
Chris Lattner | b7be7cc | 2010-01-10 02:50:04 +0000 | [diff] [blame] | 781 | return false; |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 785 | Instruction *InstCombiner::visitZExt(ZExtInst &CI) { |
Nick Lewycky | 80ea003 | 2013-01-14 20:56:10 +0000 | [diff] [blame] | 786 | // If this zero extend is only used by a truncate, let the truncate be |
Chris Lattner | 49d2c97 | 2010-01-10 02:39:31 +0000 | [diff] [blame] | 787 | // eliminated before we try to optimize this zext. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 788 | if (CI.hasOneUse() && isa<TruncInst>(CI.user_back())) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 789 | return nullptr; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 790 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 791 | // If one of the common conversion will work, do it. |
Chris Lattner | 883550a | 2010-01-10 01:00:46 +0000 | [diff] [blame] | 792 | if (Instruction *Result = commonCastTransforms(CI)) |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 793 | return Result; |
| 794 | |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 795 | // See if we can simplify any instructions used by the input whose sole |
Chris Lattner | 883550a | 2010-01-10 01:00:46 +0000 | [diff] [blame] | 796 | // purpose is to compute bits we don't care about. |
| 797 | if (SimplifyDemandedInstructionBits(CI)) |
| 798 | return &CI; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 799 | |
Chris Lattner | 883550a | 2010-01-10 01:00:46 +0000 | [diff] [blame] | 800 | Value *Src = CI.getOperand(0); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 801 | Type *SrcTy = Src->getType(), *DestTy = CI.getType(); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 802 | |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 803 | // Attempt to extend the entire input expression tree to the destination |
| 804 | // type. Only do this if the dest type is a simple type, don't convert the |
| 805 | // expression tree to something weird like i93 unless the source is also |
| 806 | // strange. |
Chris Lattner | 12bd899 | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 807 | unsigned BitsToClear; |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 808 | if ((DestTy->isVectorTy() || ShouldChangeType(SrcTy, DestTy)) && |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 809 | canEvaluateZExtd(Src, DestTy, BitsToClear, *this, &CI)) { |
Chris Lattner | 12bd899 | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 810 | assert(BitsToClear < SrcTy->getScalarSizeInBits() && |
| 811 | "Unreasonable BitsToClear"); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 812 | |
Chris Lattner | 49d2c97 | 2010-01-10 02:39:31 +0000 | [diff] [blame] | 813 | // Okay, we can transform this! Insert the new expression now. |
| 814 | DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type" |
| 815 | " to avoid zero extend: " << CI); |
| 816 | Value *Res = EvaluateInDifferentType(Src, DestTy, false); |
| 817 | assert(Res->getType() == DestTy); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 818 | |
Chris Lattner | 12bd899 | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 819 | uint32_t SrcBitsKept = SrcTy->getScalarSizeInBits()-BitsToClear; |
| 820 | uint32_t DestBitSize = DestTy->getScalarSizeInBits(); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 821 | |
Chris Lattner | 49d2c97 | 2010-01-10 02:39:31 +0000 | [diff] [blame] | 822 | // If the high bits are already filled with zeros, just replace this |
| 823 | // cast with the result. |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 824 | if (MaskedValueIsZero(Res, |
| 825 | APInt::getHighBitsSet(DestBitSize, |
| 826 | DestBitSize-SrcBitsKept), |
| 827 | 0, &CI)) |
Chris Lattner | 49d2c97 | 2010-01-10 02:39:31 +0000 | [diff] [blame] | 828 | return ReplaceInstUsesWith(CI, Res); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 829 | |
Chris Lattner | 49d2c97 | 2010-01-10 02:39:31 +0000 | [diff] [blame] | 830 | // We need to emit an AND to clear the high bits. |
Chris Lattner | 39d2daa | 2010-01-10 20:25:54 +0000 | [diff] [blame] | 831 | Constant *C = ConstantInt::get(Res->getType(), |
Chris Lattner | 12bd899 | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 832 | APInt::getLowBitsSet(DestBitSize, SrcBitsKept)); |
Chris Lattner | 49d2c97 | 2010-01-10 02:39:31 +0000 | [diff] [blame] | 833 | return BinaryOperator::CreateAnd(Res, C); |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 834 | } |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 835 | |
| 836 | // If this is a TRUNC followed by a ZEXT then we are dealing with integral |
| 837 | // types and if the sizes are just right we can convert this into a logical |
| 838 | // 'and' which will be much cheaper than the pair of casts. |
| 839 | if (TruncInst *CSrc = dyn_cast<TruncInst>(Src)) { // A->B->C cast |
Chris Lattner | d850942 | 2010-01-10 07:08:30 +0000 | [diff] [blame] | 840 | // TODO: Subsume this into EvaluateInDifferentType. |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 841 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 842 | // Get the sizes of the types involved. We know that the intermediate type |
| 843 | // will be smaller than A or C, but don't know the relation between A and C. |
| 844 | Value *A = CSrc->getOperand(0); |
| 845 | unsigned SrcSize = A->getType()->getScalarSizeInBits(); |
| 846 | unsigned MidSize = CSrc->getType()->getScalarSizeInBits(); |
| 847 | unsigned DstSize = CI.getType()->getScalarSizeInBits(); |
| 848 | // If we're actually extending zero bits, then if |
| 849 | // SrcSize < DstSize: zext(a & mask) |
| 850 | // SrcSize == DstSize: a & mask |
| 851 | // SrcSize > DstSize: trunc(a) & mask |
| 852 | if (SrcSize < DstSize) { |
| 853 | APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); |
| 854 | Constant *AndConst = ConstantInt::get(A->getType(), AndValue); |
| 855 | Value *And = Builder->CreateAnd(A, AndConst, CSrc->getName()+".mask"); |
| 856 | return new ZExtInst(And, CI.getType()); |
| 857 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 858 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 859 | if (SrcSize == DstSize) { |
| 860 | APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); |
| 861 | return BinaryOperator::CreateAnd(A, ConstantInt::get(A->getType(), |
| 862 | AndValue)); |
| 863 | } |
| 864 | if (SrcSize > DstSize) { |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 865 | Value *Trunc = Builder->CreateTrunc(A, CI.getType()); |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 866 | APInt AndValue(APInt::getLowBitsSet(DstSize, MidSize)); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 867 | return BinaryOperator::CreateAnd(Trunc, |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 868 | ConstantInt::get(Trunc->getType(), |
Chris Lattner | d850942 | 2010-01-10 07:08:30 +0000 | [diff] [blame] | 869 | AndValue)); |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 870 | } |
| 871 | } |
| 872 | |
| 873 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) |
| 874 | return transformZExtICmp(ICI, CI); |
| 875 | |
| 876 | BinaryOperator *SrcI = dyn_cast<BinaryOperator>(Src); |
| 877 | if (SrcI && SrcI->getOpcode() == Instruction::Or) { |
| 878 | // zext (or icmp, icmp) --> or (zext icmp), (zext icmp) if at least one |
| 879 | // of the (zext icmp) will be transformed. |
| 880 | ICmpInst *LHS = dyn_cast<ICmpInst>(SrcI->getOperand(0)); |
| 881 | ICmpInst *RHS = dyn_cast<ICmpInst>(SrcI->getOperand(1)); |
| 882 | if (LHS && RHS && LHS->hasOneUse() && RHS->hasOneUse() && |
| 883 | (transformZExtICmp(LHS, CI, false) || |
| 884 | transformZExtICmp(RHS, CI, false))) { |
| 885 | Value *LCast = Builder->CreateZExt(LHS, CI.getType(), LHS->getName()); |
| 886 | Value *RCast = Builder->CreateZExt(RHS, CI.getType(), RHS->getName()); |
| 887 | return BinaryOperator::Create(Instruction::Or, LCast, RCast); |
| 888 | } |
| 889 | } |
| 890 | |
Benjamin Kramer | b80e169 | 2014-01-19 20:05:13 +0000 | [diff] [blame] | 891 | // zext(trunc(X) & C) -> (X & zext(C)). |
| 892 | Constant *C; |
| 893 | Value *X; |
| 894 | if (SrcI && |
| 895 | match(SrcI, m_OneUse(m_And(m_Trunc(m_Value(X)), m_Constant(C)))) && |
| 896 | X->getType() == CI.getType()) |
| 897 | return BinaryOperator::CreateAnd(X, ConstantExpr::getZExt(C, CI.getType())); |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 898 | |
Benjamin Kramer | b80e169 | 2014-01-19 20:05:13 +0000 | [diff] [blame] | 899 | // zext((trunc(X) & C) ^ C) -> ((X & zext(C)) ^ zext(C)). |
| 900 | Value *And; |
| 901 | if (SrcI && match(SrcI, m_OneUse(m_Xor(m_Value(And), m_Constant(C)))) && |
| 902 | match(And, m_OneUse(m_And(m_Trunc(m_Value(X)), m_Specific(C)))) && |
| 903 | X->getType() == CI.getType()) { |
| 904 | Constant *ZC = ConstantExpr::getZExt(C, CI.getType()); |
| 905 | return BinaryOperator::CreateXor(Builder->CreateAnd(X, ZC), ZC); |
| 906 | } |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 907 | |
Chris Lattner | fd7e42b | 2010-01-05 21:04:47 +0000 | [diff] [blame] | 908 | // zext (xor i1 X, true) to i32 --> xor (zext i1 X to i32), 1 |
Benjamin Kramer | b80e169 | 2014-01-19 20:05:13 +0000 | [diff] [blame] | 909 | if (SrcI && SrcI->hasOneUse() && |
| 910 | SrcI->getType()->getScalarType()->isIntegerTy(1) && |
| 911 | match(SrcI, m_Not(m_Value(X))) && (!X->hasOneUse() || !isa<CmpInst>(X))) { |
Chris Lattner | fd7e42b | 2010-01-05 21:04:47 +0000 | [diff] [blame] | 912 | Value *New = Builder->CreateZExt(X, CI.getType()); |
| 913 | return BinaryOperator::CreateXor(New, ConstantInt::get(CI.getType(), 1)); |
| 914 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 915 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 916 | return nullptr; |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Sanjay Patel | 2fbab9d8 | 2015-09-09 14:34:26 +0000 | [diff] [blame] | 919 | /// Transform (sext icmp) to bitwise / integer operations to eliminate the icmp. |
Benjamin Kramer | 398b8c5 | 2011-04-01 20:09:03 +0000 | [diff] [blame] | 920 | Instruction *InstCombiner::transformSExtICmp(ICmpInst *ICI, Instruction &CI) { |
| 921 | Value *Op0 = ICI->getOperand(0), *Op1 = ICI->getOperand(1); |
| 922 | ICmpInst::Predicate Pred = ICI->getPredicate(); |
| 923 | |
David Majnemer | c8bdd23 | 2014-10-27 05:47:49 +0000 | [diff] [blame] | 924 | // Don't bother if Op1 isn't of vector or integer type. |
| 925 | if (!Op1->getType()->isIntOrIntVectorTy()) |
| 926 | return nullptr; |
| 927 | |
Benjamin Kramer | b80e169 | 2014-01-19 20:05:13 +0000 | [diff] [blame] | 928 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
Benjamin Kramer | 8b94c29 | 2011-04-01 22:29:18 +0000 | [diff] [blame] | 929 | // (x <s 0) ? -1 : 0 -> ashr x, 31 -> all ones if negative |
| 930 | // (x >s -1) ? -1 : 0 -> not (ashr x, 31) -> all ones if positive |
Benjamin Kramer | b80e169 | 2014-01-19 20:05:13 +0000 | [diff] [blame] | 931 | if ((Pred == ICmpInst::ICMP_SLT && Op1C->isNullValue()) || |
Benjamin Kramer | 398b8c5 | 2011-04-01 20:09:03 +0000 | [diff] [blame] | 932 | (Pred == ICmpInst::ICMP_SGT && Op1C->isAllOnesValue())) { |
| 933 | |
| 934 | Value *Sh = ConstantInt::get(Op0->getType(), |
| 935 | Op0->getType()->getScalarSizeInBits()-1); |
| 936 | Value *In = Builder->CreateAShr(Op0, Sh, Op0->getName()+".lobit"); |
| 937 | if (In->getType() != CI.getType()) |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 938 | In = Builder->CreateIntCast(In, CI.getType(), true/*SExt*/); |
Benjamin Kramer | 398b8c5 | 2011-04-01 20:09:03 +0000 | [diff] [blame] | 939 | |
| 940 | if (Pred == ICmpInst::ICMP_SGT) |
| 941 | In = Builder->CreateNot(In, In->getName()+".not"); |
| 942 | return ReplaceInstUsesWith(CI, In); |
| 943 | } |
Benjamin Kramer | b80e169 | 2014-01-19 20:05:13 +0000 | [diff] [blame] | 944 | } |
Benjamin Kramer | d121765 | 2011-04-01 20:09:10 +0000 | [diff] [blame] | 945 | |
Benjamin Kramer | b80e169 | 2014-01-19 20:05:13 +0000 | [diff] [blame] | 946 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { |
Benjamin Kramer | d121765 | 2011-04-01 20:09:10 +0000 | [diff] [blame] | 947 | // If we know that only one bit of the LHS of the icmp can be set and we |
| 948 | // have an equality comparison with zero or a power of 2, we can transform |
| 949 | // the icmp and sext into bitwise/integer operations. |
Benjamin Kramer | 5cad453 | 2011-04-01 22:22:11 +0000 | [diff] [blame] | 950 | if (ICI->hasOneUse() && |
| 951 | ICI->isEquality() && (Op1C->isZero() || Op1C->getValue().isPowerOf2())){ |
Benjamin Kramer | d121765 | 2011-04-01 20:09:10 +0000 | [diff] [blame] | 952 | unsigned BitWidth = Op1C->getType()->getBitWidth(); |
| 953 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 954 | computeKnownBits(Op0, KnownZero, KnownOne, 0, &CI); |
Benjamin Kramer | d121765 | 2011-04-01 20:09:10 +0000 | [diff] [blame] | 955 | |
Benjamin Kramer | ac2d565 | 2011-04-01 20:15:16 +0000 | [diff] [blame] | 956 | APInt KnownZeroMask(~KnownZero); |
| 957 | if (KnownZeroMask.isPowerOf2()) { |
Benjamin Kramer | d121765 | 2011-04-01 20:09:10 +0000 | [diff] [blame] | 958 | Value *In = ICI->getOperand(0); |
| 959 | |
Benjamin Kramer | 50a281a | 2011-04-02 18:50:58 +0000 | [diff] [blame] | 960 | // If the icmp tests for a known zero bit we can constant fold it. |
| 961 | if (!Op1C->isZero() && Op1C->getValue() != KnownZeroMask) { |
| 962 | Value *V = Pred == ICmpInst::ICMP_NE ? |
| 963 | ConstantInt::getAllOnesValue(CI.getType()) : |
| 964 | ConstantInt::getNullValue(CI.getType()); |
| 965 | return ReplaceInstUsesWith(CI, V); |
| 966 | } |
Benjamin Kramer | 5cad453 | 2011-04-01 22:22:11 +0000 | [diff] [blame] | 967 | |
Benjamin Kramer | d121765 | 2011-04-01 20:09:10 +0000 | [diff] [blame] | 968 | if (!Op1C->isZero() == (Pred == ICmpInst::ICMP_NE)) { |
| 969 | // sext ((x & 2^n) == 0) -> (x >> n) - 1 |
| 970 | // sext ((x & 2^n) != 2^n) -> (x >> n) - 1 |
| 971 | unsigned ShiftAmt = KnownZeroMask.countTrailingZeros(); |
| 972 | // Perform a right shift to place the desired bit in the LSB. |
| 973 | if (ShiftAmt) |
| 974 | In = Builder->CreateLShr(In, |
| 975 | ConstantInt::get(In->getType(), ShiftAmt)); |
| 976 | |
| 977 | // At this point "In" is either 1 or 0. Subtract 1 to turn |
| 978 | // {1, 0} -> {0, -1}. |
| 979 | In = Builder->CreateAdd(In, |
| 980 | ConstantInt::getAllOnesValue(In->getType()), |
| 981 | "sext"); |
| 982 | } else { |
| 983 | // sext ((x & 2^n) != 0) -> (x << bitwidth-n) a>> bitwidth-1 |
Benjamin Kramer | 5cad453 | 2011-04-01 22:22:11 +0000 | [diff] [blame] | 984 | // sext ((x & 2^n) == 2^n) -> (x << bitwidth-n) a>> bitwidth-1 |
Benjamin Kramer | d121765 | 2011-04-01 20:09:10 +0000 | [diff] [blame] | 985 | unsigned ShiftAmt = KnownZeroMask.countLeadingZeros(); |
| 986 | // Perform a left shift to place the desired bit in the MSB. |
| 987 | if (ShiftAmt) |
| 988 | In = Builder->CreateShl(In, |
| 989 | ConstantInt::get(In->getType(), ShiftAmt)); |
| 990 | |
| 991 | // Distribute the bit over the whole bit width. |
| 992 | In = Builder->CreateAShr(In, ConstantInt::get(In->getType(), |
| 993 | BitWidth - 1), "sext"); |
| 994 | } |
| 995 | |
| 996 | if (CI.getType() == In->getType()) |
| 997 | return ReplaceInstUsesWith(CI, In); |
| 998 | return CastInst::CreateIntegerCast(In, CI.getType(), true/*SExt*/); |
| 999 | } |
| 1000 | } |
Benjamin Kramer | 398b8c5 | 2011-04-01 20:09:03 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1003 | return nullptr; |
Benjamin Kramer | 398b8c5 | 2011-04-01 20:09:03 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Sanjay Patel | 2fbab9d8 | 2015-09-09 14:34:26 +0000 | [diff] [blame] | 1006 | /// Return true if we can take the specified value and return it as type Ty |
| 1007 | /// without inserting any new casts and without changing the value of the common |
| 1008 | /// low bits. This is used by code that tries to promote integer operations to |
| 1009 | /// a wider types will allow us to eliminate the extension. |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 1010 | /// |
Chris Lattner | 1a05fdd | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 1011 | /// This function works on both vectors and scalars. |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 1012 | /// |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1013 | static bool canEvaluateSExtd(Value *V, Type *Ty) { |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 1014 | assert(V->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits() && |
| 1015 | "Can't sign extend type to a smaller type"); |
Chris Lattner | 1a05fdd | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 1016 | // If this is a constant, it can be trivially promoted. |
| 1017 | if (isa<Constant>(V)) |
| 1018 | return true; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1019 | |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 1020 | Instruction *I = dyn_cast<Instruction>(V); |
Chris Lattner | 1a05fdd | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 1021 | if (!I) return false; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1022 | |
Jakob Stoklund Olesen | c5c4e96 | 2012-06-22 16:36:43 +0000 | [diff] [blame] | 1023 | // If this is a truncate from the dest type, we can trivially eliminate it. |
| 1024 | if (isa<TruncInst>(I) && I->getOperand(0)->getType() == Ty) |
Chris Lattner | 1a05fdd | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 1025 | return true; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1026 | |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 1027 | // We can't extend or shrink something that has multiple uses: doing so would |
| 1028 | // require duplicating the instruction in general, which isn't profitable. |
Chris Lattner | 1a05fdd | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 1029 | if (!I->hasOneUse()) return false; |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 1030 | |
Chris Lattner | 1a05fdd | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 1031 | switch (I->getOpcode()) { |
Chris Lattner | 7dd540e | 2010-01-10 20:30:41 +0000 | [diff] [blame] | 1032 | case Instruction::SExt: // sext(sext(x)) -> sext(x) |
| 1033 | case Instruction::ZExt: // sext(zext(x)) -> zext(x) |
| 1034 | case Instruction::Trunc: // sext(trunc(x)) -> trunc(x) or sext(x) |
| 1035 | return true; |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 1036 | case Instruction::And: |
| 1037 | case Instruction::Or: |
| 1038 | case Instruction::Xor: |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 1039 | case Instruction::Add: |
| 1040 | case Instruction::Sub: |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 1041 | case Instruction::Mul: |
Chris Lattner | 1a05fdd | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 1042 | // These operators can all arbitrarily be extended if their inputs can. |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1043 | return canEvaluateSExtd(I->getOperand(0), Ty) && |
| 1044 | canEvaluateSExtd(I->getOperand(1), Ty); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1045 | |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 1046 | //case Instruction::Shl: TODO |
| 1047 | //case Instruction::LShr: TODO |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1048 | |
Chris Lattner | 1a05fdd | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 1049 | case Instruction::Select: |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1050 | return canEvaluateSExtd(I->getOperand(1), Ty) && |
| 1051 | canEvaluateSExtd(I->getOperand(2), Ty); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1052 | |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 1053 | case Instruction::PHI: { |
| 1054 | // We can change a phi if we can change all operands. Note that we never |
| 1055 | // get into trouble with cyclic PHIs here because we only consider |
| 1056 | // instructions with a single use. |
| 1057 | PHINode *PN = cast<PHINode>(I); |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 1058 | for (Value *IncValue : PN->incoming_values()) |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1059 | if (!canEvaluateSExtd(IncValue, Ty)) return false; |
Chris Lattner | 1a05fdd | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 1060 | return true; |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 1061 | } |
| 1062 | default: |
| 1063 | // TODO: Can handle more cases here. |
| 1064 | break; |
| 1065 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1066 | |
Chris Lattner | 1a05fdd | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 1067 | return false; |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1070 | Instruction *InstCombiner::visitSExt(SExtInst &CI) { |
Arnaud A. de Grandmaison | 2e4df4f | 2013-02-13 00:19:19 +0000 | [diff] [blame] | 1071 | // If this sign extend is only used by a truncate, let the truncate be |
| 1072 | // eliminated before we try to optimize this sext. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1073 | if (CI.hasOneUse() && isa<TruncInst>(CI.user_back())) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1074 | return nullptr; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1075 | |
Chris Lattner | 883550a | 2010-01-10 01:00:46 +0000 | [diff] [blame] | 1076 | if (Instruction *I = commonCastTransforms(CI)) |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1077 | return I; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1078 | |
| 1079 | // See if we can simplify any instructions used by the input whose sole |
Chris Lattner | 883550a | 2010-01-10 01:00:46 +0000 | [diff] [blame] | 1080 | // purpose is to compute bits we don't care about. |
| 1081 | if (SimplifyDemandedInstructionBits(CI)) |
| 1082 | return &CI; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1083 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1084 | Value *Src = CI.getOperand(0); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1085 | Type *SrcTy = Src->getType(), *DestTy = CI.getType(); |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 1086 | |
Philip Reames | 9ae1520 | 2015-02-14 00:05:36 +0000 | [diff] [blame] | 1087 | // If we know that the value being extended is positive, we can use a zext |
| 1088 | // instead. |
| 1089 | bool KnownZero, KnownOne; |
| 1090 | ComputeSignBit(Src, KnownZero, KnownOne, 0, &CI); |
| 1091 | if (KnownZero) { |
| 1092 | Value *ZExt = Builder->CreateZExt(Src, DestTy); |
| 1093 | return ReplaceInstUsesWith(CI, ZExt); |
| 1094 | } |
| 1095 | |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 1096 | // Attempt to extend the entire input expression tree to the destination |
| 1097 | // type. Only do this if the dest type is a simple type, don't convert the |
| 1098 | // expression tree to something weird like i93 unless the source is also |
| 1099 | // strange. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1100 | if ((DestTy->isVectorTy() || ShouldChangeType(SrcTy, DestTy)) && |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1101 | canEvaluateSExtd(Src, DestTy)) { |
Chris Lattner | 2fff10c | 2010-01-10 07:40:50 +0000 | [diff] [blame] | 1102 | // Okay, we can transform this! Insert the new expression now. |
| 1103 | DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type" |
| 1104 | " to avoid sign extend: " << CI); |
| 1105 | Value *Res = EvaluateInDifferentType(Src, DestTy, true); |
| 1106 | assert(Res->getType() == DestTy); |
| 1107 | |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 1108 | uint32_t SrcBitSize = SrcTy->getScalarSizeInBits(); |
| 1109 | uint32_t DestBitSize = DestTy->getScalarSizeInBits(); |
Chris Lattner | 2fff10c | 2010-01-10 07:40:50 +0000 | [diff] [blame] | 1110 | |
| 1111 | // If the high bits are already filled with sign bit, just replace this |
| 1112 | // cast with the result. |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1113 | if (ComputeNumSignBits(Res, 0, &CI) > DestBitSize - SrcBitSize) |
Chris Lattner | 2fff10c | 2010-01-10 07:40:50 +0000 | [diff] [blame] | 1114 | return ReplaceInstUsesWith(CI, Res); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1115 | |
Chris Lattner | 2fff10c | 2010-01-10 07:40:50 +0000 | [diff] [blame] | 1116 | // We need to emit a shl + ashr to do the sign extend. |
| 1117 | Value *ShAmt = ConstantInt::get(DestTy, DestBitSize-SrcBitSize); |
| 1118 | return BinaryOperator::CreateAShr(Builder->CreateShl(Res, ShAmt, "sext"), |
| 1119 | ShAmt); |
Chris Lattner | c3aca38 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 1120 | } |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1121 | |
Chris Lattner | 43f2fa6 | 2010-01-18 22:19:16 +0000 | [diff] [blame] | 1122 | // If this input is a trunc from our destination, then turn sext(trunc(x)) |
| 1123 | // into shifts. |
| 1124 | if (TruncInst *TI = dyn_cast<TruncInst>(Src)) |
| 1125 | if (TI->hasOneUse() && TI->getOperand(0)->getType() == DestTy) { |
| 1126 | uint32_t SrcBitSize = SrcTy->getScalarSizeInBits(); |
| 1127 | uint32_t DestBitSize = DestTy->getScalarSizeInBits(); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1128 | |
Chris Lattner | 43f2fa6 | 2010-01-18 22:19:16 +0000 | [diff] [blame] | 1129 | // We need to emit a shl + ashr to do the sign extend. |
| 1130 | Value *ShAmt = ConstantInt::get(DestTy, DestBitSize-SrcBitSize); |
| 1131 | Value *Res = Builder->CreateShl(TI->getOperand(0), ShAmt, "sext"); |
| 1132 | return BinaryOperator::CreateAShr(Res, ShAmt); |
| 1133 | } |
Nate Begeman | 7aa18bf | 2010-12-17 23:12:19 +0000 | [diff] [blame] | 1134 | |
Benjamin Kramer | 398b8c5 | 2011-04-01 20:09:03 +0000 | [diff] [blame] | 1135 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) |
| 1136 | return transformSExtICmp(ICI, CI); |
Bill Wendling | 5e36055 | 2010-12-17 23:27:41 +0000 | [diff] [blame] | 1137 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1138 | // If the input is a shl/ashr pair of a same constant, then this is a sign |
| 1139 | // extension from a smaller value. If we could trust arbitrary bitwidth |
| 1140 | // integers, we could turn this into a truncate to the smaller bit and then |
| 1141 | // use a sext for the whole extension. Since we don't, look deeper and check |
| 1142 | // for a truncate. If the source and dest are the same type, eliminate the |
| 1143 | // trunc and extend and just do shifts. For example, turn: |
| 1144 | // %a = trunc i32 %i to i8 |
| 1145 | // %b = shl i8 %a, 6 |
| 1146 | // %c = ashr i8 %b, 6 |
| 1147 | // %d = sext i8 %c to i32 |
| 1148 | // into: |
| 1149 | // %a = shl i32 %i, 30 |
| 1150 | // %d = ashr i32 %a, 30 |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1151 | Value *A = nullptr; |
Chris Lattner | c95a7a2 | 2010-01-10 01:04:31 +0000 | [diff] [blame] | 1152 | // TODO: Eventually this could be subsumed by EvaluateInDifferentType. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1153 | ConstantInt *BA = nullptr, *CA = nullptr; |
Chris Lattner | c95a7a2 | 2010-01-10 01:04:31 +0000 | [diff] [blame] | 1154 | if (match(Src, m_AShr(m_Shl(m_Trunc(m_Value(A)), m_ConstantInt(BA)), |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1155 | m_ConstantInt(CA))) && |
Chris Lattner | c95a7a2 | 2010-01-10 01:04:31 +0000 | [diff] [blame] | 1156 | BA == CA && A->getType() == CI.getType()) { |
| 1157 | unsigned MidSize = Src->getType()->getScalarSizeInBits(); |
| 1158 | unsigned SrcDstSize = CI.getType()->getScalarSizeInBits(); |
| 1159 | unsigned ShAmt = CA->getZExtValue()+SrcDstSize-MidSize; |
| 1160 | Constant *ShAmtV = ConstantInt::get(CI.getType(), ShAmt); |
| 1161 | A = Builder->CreateShl(A, ShAmtV, CI.getName()); |
| 1162 | return BinaryOperator::CreateAShr(A, ShAmtV); |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1163 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1164 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1165 | return nullptr; |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | |
Sanjay Patel | 2fbab9d8 | 2015-09-09 14:34:26 +0000 | [diff] [blame] | 1169 | /// Return a Constant* for the specified floating-point constant if it fits |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1170 | /// in the specified FP type without changing its value. |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1171 | static Constant *fitsInFPType(ConstantFP *CFP, const fltSemantics &Sem) { |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1172 | bool losesInfo; |
| 1173 | APFloat F = CFP->getValueAPF(); |
| 1174 | (void)F.convert(Sem, APFloat::rmNearestTiesToEven, &losesInfo); |
| 1175 | if (!losesInfo) |
| 1176 | return ConstantFP::get(CFP->getContext(), F); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1177 | return nullptr; |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Sanjay Patel | 2fbab9d8 | 2015-09-09 14:34:26 +0000 | [diff] [blame] | 1180 | /// If this is a floating-point extension instruction, look |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1181 | /// through it until we get the source value. |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1182 | static Value *lookThroughFPExtensions(Value *V) { |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1183 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 1184 | if (I->getOpcode() == Instruction::FPExt) |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1185 | return lookThroughFPExtensions(I->getOperand(0)); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1186 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1187 | // If this value is a constant, return the constant in the smallest FP type |
| 1188 | // that can accurately represent it. This allows us to turn |
| 1189 | // (float)((double)X+2.0) into x+2.0f. |
| 1190 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { |
| 1191 | if (CFP->getType() == Type::getPPC_FP128Ty(V->getContext())) |
| 1192 | return V; // No constant folding of this. |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 1193 | // See if the value can be truncated to half and then reextended. |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1194 | if (Value *V = fitsInFPType(CFP, APFloat::IEEEhalf)) |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 1195 | return V; |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1196 | // See if the value can be truncated to float and then reextended. |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1197 | if (Value *V = fitsInFPType(CFP, APFloat::IEEEsingle)) |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1198 | return V; |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 1199 | if (CFP->getType()->isDoubleTy()) |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1200 | return V; // Won't shrink. |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1201 | if (Value *V = fitsInFPType(CFP, APFloat::IEEEdouble)) |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1202 | return V; |
| 1203 | // Don't try to shrink to various long double types. |
| 1204 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1205 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1206 | return V; |
| 1207 | } |
| 1208 | |
| 1209 | Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) { |
| 1210 | if (Instruction *I = commonCastTransforms(CI)) |
| 1211 | return I; |
Stephen Canon | c454964 | 2013-11-28 21:38:05 +0000 | [diff] [blame] | 1212 | // If we have fptrunc(OpI (fpextend x), (fpextend y)), we would like to |
| 1213 | // simpilify this expression to avoid one or more of the trunc/extend |
| 1214 | // operations if we can do so without changing the numerical results. |
| 1215 | // |
| 1216 | // The exact manner in which the widths of the operands interact to limit |
| 1217 | // what we can and cannot do safely varies from operation to operation, and |
| 1218 | // is explained below in the various case statements. |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1219 | BinaryOperator *OpI = dyn_cast<BinaryOperator>(CI.getOperand(0)); |
| 1220 | if (OpI && OpI->hasOneUse()) { |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1221 | Value *LHSOrig = lookThroughFPExtensions(OpI->getOperand(0)); |
| 1222 | Value *RHSOrig = lookThroughFPExtensions(OpI->getOperand(1)); |
Stephen Canon | c454964 | 2013-11-28 21:38:05 +0000 | [diff] [blame] | 1223 | unsigned OpWidth = OpI->getType()->getFPMantissaWidth(); |
| 1224 | unsigned LHSWidth = LHSOrig->getType()->getFPMantissaWidth(); |
| 1225 | unsigned RHSWidth = RHSOrig->getType()->getFPMantissaWidth(); |
| 1226 | unsigned SrcWidth = std::max(LHSWidth, RHSWidth); |
| 1227 | unsigned DstWidth = CI.getType()->getFPMantissaWidth(); |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1228 | switch (OpI->getOpcode()) { |
Stephen Canon | c454964 | 2013-11-28 21:38:05 +0000 | [diff] [blame] | 1229 | default: break; |
| 1230 | case Instruction::FAdd: |
| 1231 | case Instruction::FSub: |
| 1232 | // For addition and subtraction, the infinitely precise result can |
| 1233 | // essentially be arbitrarily wide; proving that double rounding |
| 1234 | // will not occur because the result of OpI is exact (as we will for |
| 1235 | // FMul, for example) is hopeless. However, we *can* nonetheless |
| 1236 | // frequently know that double rounding cannot occur (or that it is |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 1237 | // innocuous) by taking advantage of the specific structure of |
Stephen Canon | c454964 | 2013-11-28 21:38:05 +0000 | [diff] [blame] | 1238 | // infinitely-precise results that admit double rounding. |
| 1239 | // |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 1240 | // Specifically, if OpWidth >= 2*DstWdith+1 and DstWidth is sufficient |
Stephen Canon | c454964 | 2013-11-28 21:38:05 +0000 | [diff] [blame] | 1241 | // to represent both sources, we can guarantee that the double |
| 1242 | // rounding is innocuous (See p50 of Figueroa's 2000 PhD thesis, |
| 1243 | // "A Rigorous Framework for Fully Supporting the IEEE Standard ..." |
| 1244 | // for proof of this fact). |
| 1245 | // |
| 1246 | // Note: Figueroa does not consider the case where DstFormat != |
| 1247 | // SrcFormat. It's possible (likely even!) that this analysis |
| 1248 | // could be tightened for those cases, but they are rare (the main |
| 1249 | // case of interest here is (float)((double)float + float)). |
| 1250 | if (OpWidth >= 2*DstWidth+1 && DstWidth >= SrcWidth) { |
| 1251 | if (LHSOrig->getType() != CI.getType()) |
| 1252 | LHSOrig = Builder->CreateFPExt(LHSOrig, CI.getType()); |
| 1253 | if (RHSOrig->getType() != CI.getType()) |
| 1254 | RHSOrig = Builder->CreateFPExt(RHSOrig, CI.getType()); |
Owen Anderson | 48b842e | 2014-01-18 00:48:14 +0000 | [diff] [blame] | 1255 | Instruction *RI = |
| 1256 | BinaryOperator::Create(OpI->getOpcode(), LHSOrig, RHSOrig); |
| 1257 | RI->copyFastMathFlags(OpI); |
| 1258 | return RI; |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1259 | } |
Stephen Canon | c454964 | 2013-11-28 21:38:05 +0000 | [diff] [blame] | 1260 | break; |
| 1261 | case Instruction::FMul: |
| 1262 | // For multiplication, the infinitely precise result has at most |
| 1263 | // LHSWidth + RHSWidth significant bits; if OpWidth is sufficient |
| 1264 | // that such a value can be exactly represented, then no double |
| 1265 | // rounding can possibly occur; we can safely perform the operation |
| 1266 | // in the destination format if it can represent both sources. |
| 1267 | if (OpWidth >= LHSWidth + RHSWidth && DstWidth >= SrcWidth) { |
| 1268 | if (LHSOrig->getType() != CI.getType()) |
| 1269 | LHSOrig = Builder->CreateFPExt(LHSOrig, CI.getType()); |
| 1270 | if (RHSOrig->getType() != CI.getType()) |
| 1271 | RHSOrig = Builder->CreateFPExt(RHSOrig, CI.getType()); |
Owen Anderson | 48b842e | 2014-01-18 00:48:14 +0000 | [diff] [blame] | 1272 | Instruction *RI = |
| 1273 | BinaryOperator::CreateFMul(LHSOrig, RHSOrig); |
| 1274 | RI->copyFastMathFlags(OpI); |
| 1275 | return RI; |
Stephen Canon | c454964 | 2013-11-28 21:38:05 +0000 | [diff] [blame] | 1276 | } |
| 1277 | break; |
| 1278 | case Instruction::FDiv: |
| 1279 | // For division, we use again use the bound from Figueroa's |
| 1280 | // dissertation. I am entirely certain that this bound can be |
| 1281 | // tightened in the unbalanced operand case by an analysis based on |
| 1282 | // the diophantine rational approximation bound, but the well-known |
| 1283 | // condition used here is a good conservative first pass. |
| 1284 | // TODO: Tighten bound via rigorous analysis of the unbalanced case. |
| 1285 | if (OpWidth >= 2*DstWidth && DstWidth >= SrcWidth) { |
| 1286 | if (LHSOrig->getType() != CI.getType()) |
| 1287 | LHSOrig = Builder->CreateFPExt(LHSOrig, CI.getType()); |
| 1288 | if (RHSOrig->getType() != CI.getType()) |
| 1289 | RHSOrig = Builder->CreateFPExt(RHSOrig, CI.getType()); |
Owen Anderson | 48b842e | 2014-01-18 00:48:14 +0000 | [diff] [blame] | 1290 | Instruction *RI = |
| 1291 | BinaryOperator::CreateFDiv(LHSOrig, RHSOrig); |
| 1292 | RI->copyFastMathFlags(OpI); |
| 1293 | return RI; |
Stephen Canon | c454964 | 2013-11-28 21:38:05 +0000 | [diff] [blame] | 1294 | } |
| 1295 | break; |
| 1296 | case Instruction::FRem: |
| 1297 | // Remainder is straightforward. Remainder is always exact, so the |
| 1298 | // type of OpI doesn't enter into things at all. We simply evaluate |
| 1299 | // in whichever source type is larger, then convert to the |
| 1300 | // destination type. |
Steven Wu | f179d12 | 2014-12-12 18:48:37 +0000 | [diff] [blame] | 1301 | if (SrcWidth == OpWidth) |
Steven Wu | 1f7402a | 2014-12-12 17:21:54 +0000 | [diff] [blame] | 1302 | break; |
Steven Wu | 1f7402a | 2014-12-12 17:21:54 +0000 | [diff] [blame] | 1303 | if (LHSWidth < SrcWidth) |
| 1304 | LHSOrig = Builder->CreateFPExt(LHSOrig, RHSOrig->getType()); |
| 1305 | else if (RHSWidth <= SrcWidth) |
| 1306 | RHSOrig = Builder->CreateFPExt(RHSOrig, LHSOrig->getType()); |
| 1307 | if (LHSOrig != OpI->getOperand(0) || RHSOrig != OpI->getOperand(1)) { |
| 1308 | Value *ExactResult = Builder->CreateFRem(LHSOrig, RHSOrig); |
| 1309 | if (Instruction *RI = dyn_cast<Instruction>(ExactResult)) |
| 1310 | RI->copyFastMathFlags(OpI); |
| 1311 | return CastInst::CreateFPCast(ExactResult, CI.getType()); |
| 1312 | } |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1313 | } |
Owen Anderson | dbf0ca5 | 2013-01-10 22:06:52 +0000 | [diff] [blame] | 1314 | |
| 1315 | // (fptrunc (fneg x)) -> (fneg (fptrunc x)) |
| 1316 | if (BinaryOperator::isFNeg(OpI)) { |
| 1317 | Value *InnerTrunc = Builder->CreateFPTrunc(OpI->getOperand(1), |
| 1318 | CI.getType()); |
Owen Anderson | 48b842e | 2014-01-18 00:48:14 +0000 | [diff] [blame] | 1319 | Instruction *RI = BinaryOperator::CreateFNeg(InnerTrunc); |
| 1320 | RI->copyFastMathFlags(OpI); |
| 1321 | return RI; |
Owen Anderson | dbf0ca5 | 2013-01-10 22:06:52 +0000 | [diff] [blame] | 1322 | } |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1323 | } |
Owen Anderson | dbf0ca5 | 2013-01-10 22:06:52 +0000 | [diff] [blame] | 1324 | |
Owen Anderson | 5797bfd | 2013-10-03 21:08:05 +0000 | [diff] [blame] | 1325 | // (fptrunc (select cond, R1, Cst)) --> |
| 1326 | // (select cond, (fptrunc R1), (fptrunc Cst)) |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 1327 | // |
| 1328 | // - but only if this isn't part of a min/max operation, else we'll |
| 1329 | // ruin min/max canonical form which is to have the select and |
| 1330 | // compare's operands be of the same type with no casts to look through. |
| 1331 | Value *LHS, *RHS; |
Owen Anderson | 5797bfd | 2013-10-03 21:08:05 +0000 | [diff] [blame] | 1332 | SelectInst *SI = dyn_cast<SelectInst>(CI.getOperand(0)); |
| 1333 | if (SI && |
| 1334 | (isa<ConstantFP>(SI->getOperand(1)) || |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 1335 | isa<ConstantFP>(SI->getOperand(2))) && |
| 1336 | matchSelectPattern(SI, LHS, RHS).Flavor == SPF_UNKNOWN) { |
Owen Anderson | 5797bfd | 2013-10-03 21:08:05 +0000 | [diff] [blame] | 1337 | Value *LHSTrunc = Builder->CreateFPTrunc(SI->getOperand(1), |
| 1338 | CI.getType()); |
| 1339 | Value *RHSTrunc = Builder->CreateFPTrunc(SI->getOperand(2), |
| 1340 | CI.getType()); |
| 1341 | return SelectInst::Create(SI->getOperand(0), LHSTrunc, RHSTrunc); |
| 1342 | } |
| 1343 | |
Owen Anderson | dbf0ca5 | 2013-01-10 22:06:52 +0000 | [diff] [blame] | 1344 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI.getOperand(0)); |
| 1345 | if (II) { |
| 1346 | switch (II->getIntrinsicID()) { |
| 1347 | default: break; |
| 1348 | case Intrinsic::fabs: { |
| 1349 | // (fptrunc (fabs x)) -> (fabs (fptrunc x)) |
| 1350 | Value *InnerTrunc = Builder->CreateFPTrunc(II->getArgOperand(0), |
| 1351 | CI.getType()); |
| 1352 | Type *IntrinsicType[] = { CI.getType() }; |
| 1353 | Function *Overload = |
| 1354 | Intrinsic::getDeclaration(CI.getParent()->getParent()->getParent(), |
| 1355 | II->getIntrinsicID(), IntrinsicType); |
| 1356 | |
| 1357 | Value *Args[] = { InnerTrunc }; |
| 1358 | return CallInst::Create(Overload, Args, II->getName()); |
| 1359 | } |
| 1360 | } |
| 1361 | } |
| 1362 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1363 | return nullptr; |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | Instruction *InstCombiner::visitFPExt(CastInst &CI) { |
| 1367 | return commonCastTransforms(CI); |
| 1368 | } |
| 1369 | |
Mehdi Amini | b9a0fa4 | 2015-02-16 21:47:54 +0000 | [diff] [blame] | 1370 | // fpto{s/u}i({u/s}itofp(X)) --> X or zext(X) or sext(X) or trunc(X) |
| 1371 | // This is safe if the intermediate type has enough bits in its mantissa to |
| 1372 | // accurately represent all values of X. For example, this won't work with |
| 1373 | // i64 -> float -> i64. |
| 1374 | Instruction *InstCombiner::FoldItoFPtoI(Instruction &FI) { |
| 1375 | if (!isa<UIToFPInst>(FI.getOperand(0)) && !isa<SIToFPInst>(FI.getOperand(0))) |
| 1376 | return nullptr; |
| 1377 | Instruction *OpI = cast<Instruction>(FI.getOperand(0)); |
| 1378 | |
| 1379 | Value *SrcI = OpI->getOperand(0); |
| 1380 | Type *FITy = FI.getType(); |
| 1381 | Type *OpITy = OpI->getType(); |
| 1382 | Type *SrcTy = SrcI->getType(); |
| 1383 | bool IsInputSigned = isa<SIToFPInst>(OpI); |
| 1384 | bool IsOutputSigned = isa<FPToSIInst>(FI); |
| 1385 | |
| 1386 | // We can safely assume the conversion won't overflow the output range, |
| 1387 | // because (for example) (uint8_t)18293.f is undefined behavior. |
| 1388 | |
| 1389 | // Since we can assume the conversion won't overflow, our decision as to |
| 1390 | // whether the input will fit in the float should depend on the minimum |
| 1391 | // of the input range and output range. |
| 1392 | |
| 1393 | // This means this is also safe for a signed input and unsigned output, since |
| 1394 | // a negative input would lead to undefined behavior. |
| 1395 | int InputSize = (int)SrcTy->getScalarSizeInBits() - IsInputSigned; |
| 1396 | int OutputSize = (int)FITy->getScalarSizeInBits() - IsOutputSigned; |
| 1397 | int ActualSize = std::min(InputSize, OutputSize); |
| 1398 | |
| 1399 | if (ActualSize <= OpITy->getFPMantissaWidth()) { |
| 1400 | if (FITy->getScalarSizeInBits() > SrcTy->getScalarSizeInBits()) { |
| 1401 | if (IsInputSigned && IsOutputSigned) |
| 1402 | return new SExtInst(SrcI, FITy); |
| 1403 | return new ZExtInst(SrcI, FITy); |
| 1404 | } |
| 1405 | if (FITy->getScalarSizeInBits() < SrcTy->getScalarSizeInBits()) |
| 1406 | return new TruncInst(SrcI, FITy); |
| 1407 | if (SrcTy == FITy) |
| 1408 | return ReplaceInstUsesWith(FI, SrcI); |
| 1409 | return new BitCastInst(SrcI, FITy); |
| 1410 | } |
| 1411 | return nullptr; |
| 1412 | } |
| 1413 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1414 | Instruction *InstCombiner::visitFPToUI(FPToUIInst &FI) { |
| 1415 | Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0)); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1416 | if (!OpI) |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1417 | return commonCastTransforms(FI); |
| 1418 | |
Mehdi Amini | b9a0fa4 | 2015-02-16 21:47:54 +0000 | [diff] [blame] | 1419 | if (Instruction *I = FoldItoFPtoI(FI)) |
| 1420 | return I; |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1421 | |
| 1422 | return commonCastTransforms(FI); |
| 1423 | } |
| 1424 | |
| 1425 | Instruction *InstCombiner::visitFPToSI(FPToSIInst &FI) { |
| 1426 | Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0)); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1427 | if (!OpI) |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1428 | return commonCastTransforms(FI); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1429 | |
Mehdi Amini | b9a0fa4 | 2015-02-16 21:47:54 +0000 | [diff] [blame] | 1430 | if (Instruction *I = FoldItoFPtoI(FI)) |
| 1431 | return I; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1432 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1433 | return commonCastTransforms(FI); |
| 1434 | } |
| 1435 | |
| 1436 | Instruction *InstCombiner::visitUIToFP(CastInst &CI) { |
| 1437 | return commonCastTransforms(CI); |
| 1438 | } |
| 1439 | |
| 1440 | Instruction *InstCombiner::visitSIToFP(CastInst &CI) { |
| 1441 | return commonCastTransforms(CI); |
| 1442 | } |
| 1443 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1444 | Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) { |
Dan Gohman | 949458d | 2010-02-02 01:44:02 +0000 | [diff] [blame] | 1445 | // If the source integer type is not the intptr_t type for this target, do a |
| 1446 | // trunc or zext to the intptr_t type, then inttoptr of it. This allows the |
| 1447 | // cast to be exposed to other transforms. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1448 | unsigned AS = CI.getAddressSpace(); |
| 1449 | if (CI.getOperand(0)->getType()->getScalarSizeInBits() != |
| 1450 | DL.getPointerSizeInBits(AS)) { |
| 1451 | Type *Ty = DL.getIntPtrType(CI.getContext(), AS); |
| 1452 | if (CI.getType()->isVectorTy()) // Handle vectors of pointers. |
| 1453 | Ty = VectorType::get(Ty, CI.getType()->getVectorNumElements()); |
Benjamin Kramer | 944e0ab | 2013-02-05 20:22:40 +0000 | [diff] [blame] | 1454 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1455 | Value *P = Builder->CreateZExtOrTrunc(CI.getOperand(0), Ty); |
| 1456 | return new IntToPtrInst(P, CI.getType()); |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1457 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1458 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1459 | if (Instruction *I = commonCastTransforms(CI)) |
| 1460 | return I; |
| 1461 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1462 | return nullptr; |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
Chris Lattner | a93c63c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1465 | /// @brief Implement the transforms for cast of pointer (bitcast/ptrtoint) |
| 1466 | Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) { |
| 1467 | Value *Src = CI.getOperand(0); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1468 | |
Chris Lattner | a93c63c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1469 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
| 1470 | // If casting the result of a getelementptr instruction with no offset, turn |
| 1471 | // this into a cast of the original pointer! |
Jingyue Wu | 77145d9 | 2014-06-06 21:52:55 +0000 | [diff] [blame] | 1472 | if (GEP->hasAllZeroIndices() && |
| 1473 | // If CI is an addrspacecast and GEP changes the poiner type, merging |
| 1474 | // GEP into CI would undo canonicalizing addrspacecast with different |
| 1475 | // pointer types, causing infinite loops. |
| 1476 | (!isa<AddrSpaceCastInst>(CI) || |
| 1477 | GEP->getType() == GEP->getPointerOperand()->getType())) { |
Chris Lattner | a93c63c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1478 | // Changing the cast operand is usually not a good idea but it is safe |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1479 | // here because the pointer operand is being replaced with another |
Chris Lattner | a93c63c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1480 | // pointer operand so the opcode doesn't need to change. |
| 1481 | Worklist.Add(GEP); |
| 1482 | CI.setOperand(0, GEP->getOperand(0)); |
| 1483 | return &CI; |
| 1484 | } |
Chris Lattner | a93c63c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1485 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1486 | |
Chris Lattner | a93c63c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1487 | return commonCastTransforms(CI); |
| 1488 | } |
| 1489 | |
| 1490 | Instruction *InstCombiner::visitPtrToInt(PtrToIntInst &CI) { |
Dan Gohman | 949458d | 2010-02-02 01:44:02 +0000 | [diff] [blame] | 1491 | // If the destination integer type is not the intptr_t type for this target, |
| 1492 | // do a ptrtoint to intptr_t then do a trunc or zext. This allows the cast |
| 1493 | // to be exposed to other transforms. |
Benjamin Kramer | e477875 | 2013-02-05 19:21:56 +0000 | [diff] [blame] | 1494 | |
Matt Arsenault | 745101d | 2013-08-21 19:53:10 +0000 | [diff] [blame] | 1495 | Type *Ty = CI.getType(); |
| 1496 | unsigned AS = CI.getPointerAddressSpace(); |
| 1497 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1498 | if (Ty->getScalarSizeInBits() == DL.getPointerSizeInBits(AS)) |
Matt Arsenault | 745101d | 2013-08-21 19:53:10 +0000 | [diff] [blame] | 1499 | return commonPointerCastTransforms(CI); |
| 1500 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1501 | Type *PtrTy = DL.getIntPtrType(CI.getContext(), AS); |
Matt Arsenault | 745101d | 2013-08-21 19:53:10 +0000 | [diff] [blame] | 1502 | if (Ty->isVectorTy()) // Handle vectors of pointers. |
| 1503 | PtrTy = VectorType::get(PtrTy, Ty->getVectorNumElements()); |
| 1504 | |
| 1505 | Value *P = Builder->CreatePtrToInt(CI.getOperand(0), PtrTy); |
| 1506 | return CastInst::CreateIntegerCast(P, Ty, /*isSigned=*/false); |
Chris Lattner | a93c63c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
Sanjay Patel | 2fbab9d8 | 2015-09-09 14:34:26 +0000 | [diff] [blame] | 1509 | /// This input value (which is known to have vector type) is being zero extended |
| 1510 | /// or truncated to the specified vector type. |
| 1511 | /// Try to replace it with a shuffle (and vector/vector bitcast) if possible. |
Chris Lattner | 02b0df5 | 2010-05-08 21:50:26 +0000 | [diff] [blame] | 1512 | /// |
| 1513 | /// The source and destination vector types may have different element types. |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1514 | static Instruction *optimizeVectorResize(Value *InVal, VectorType *DestTy, |
Chris Lattner | 02b0df5 | 2010-05-08 21:50:26 +0000 | [diff] [blame] | 1515 | InstCombiner &IC) { |
| 1516 | // We can only do this optimization if the output is a multiple of the input |
| 1517 | // element size, or the input is a multiple of the output element size. |
| 1518 | // Convert the input type to have the same element type as the output. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1519 | VectorType *SrcTy = cast<VectorType>(InVal->getType()); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1520 | |
Chris Lattner | 02b0df5 | 2010-05-08 21:50:26 +0000 | [diff] [blame] | 1521 | if (SrcTy->getElementType() != DestTy->getElementType()) { |
| 1522 | // The input types don't need to be identical, but for now they must be the |
| 1523 | // same size. There is no specific reason we couldn't handle things like |
| 1524 | // <4 x i16> -> <4 x i32> by bitcasting to <2 x i32> but haven't gotten |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1525 | // there yet. |
Chris Lattner | 02b0df5 | 2010-05-08 21:50:26 +0000 | [diff] [blame] | 1526 | if (SrcTy->getElementType()->getPrimitiveSizeInBits() != |
| 1527 | DestTy->getElementType()->getPrimitiveSizeInBits()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1528 | return nullptr; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1529 | |
Chris Lattner | 02b0df5 | 2010-05-08 21:50:26 +0000 | [diff] [blame] | 1530 | SrcTy = VectorType::get(DestTy->getElementType(), SrcTy->getNumElements()); |
| 1531 | InVal = IC.Builder->CreateBitCast(InVal, SrcTy); |
| 1532 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1533 | |
Chris Lattner | 02b0df5 | 2010-05-08 21:50:26 +0000 | [diff] [blame] | 1534 | // Now that the element types match, get the shuffle mask and RHS of the |
| 1535 | // shuffle to use, which depends on whether we're increasing or decreasing the |
| 1536 | // size of the input. |
Chris Lattner | 8213c8a | 2012-02-06 21:56:39 +0000 | [diff] [blame] | 1537 | SmallVector<uint32_t, 16> ShuffleMask; |
Chris Lattner | 02b0df5 | 2010-05-08 21:50:26 +0000 | [diff] [blame] | 1538 | Value *V2; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1539 | |
Chris Lattner | 02b0df5 | 2010-05-08 21:50:26 +0000 | [diff] [blame] | 1540 | if (SrcTy->getNumElements() > DestTy->getNumElements()) { |
| 1541 | // If we're shrinking the number of elements, just shuffle in the low |
| 1542 | // elements from the input and use undef as the second shuffle input. |
| 1543 | V2 = UndefValue::get(SrcTy); |
| 1544 | for (unsigned i = 0, e = DestTy->getNumElements(); i != e; ++i) |
Chris Lattner | 8213c8a | 2012-02-06 21:56:39 +0000 | [diff] [blame] | 1545 | ShuffleMask.push_back(i); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1546 | |
Chris Lattner | 02b0df5 | 2010-05-08 21:50:26 +0000 | [diff] [blame] | 1547 | } else { |
| 1548 | // If we're increasing the number of elements, shuffle in all of the |
| 1549 | // elements from InVal and fill the rest of the result elements with zeros |
| 1550 | // from a constant zero. |
| 1551 | V2 = Constant::getNullValue(SrcTy); |
| 1552 | unsigned SrcElts = SrcTy->getNumElements(); |
| 1553 | for (unsigned i = 0, e = SrcElts; i != e; ++i) |
Chris Lattner | 8213c8a | 2012-02-06 21:56:39 +0000 | [diff] [blame] | 1554 | ShuffleMask.push_back(i); |
Chris Lattner | 02b0df5 | 2010-05-08 21:50:26 +0000 | [diff] [blame] | 1555 | |
| 1556 | // The excess elements reference the first element of the zero input. |
Chris Lattner | 8213c8a | 2012-02-06 21:56:39 +0000 | [diff] [blame] | 1557 | for (unsigned i = 0, e = DestTy->getNumElements()-SrcElts; i != e; ++i) |
| 1558 | ShuffleMask.push_back(SrcElts); |
Chris Lattner | 02b0df5 | 2010-05-08 21:50:26 +0000 | [diff] [blame] | 1559 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1560 | |
Chris Lattner | 8213c8a | 2012-02-06 21:56:39 +0000 | [diff] [blame] | 1561 | return new ShuffleVectorInst(InVal, V2, |
| 1562 | ConstantDataVector::get(V2->getContext(), |
| 1563 | ShuffleMask)); |
Chris Lattner | 02b0df5 | 2010-05-08 21:50:26 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1566 | static bool isMultipleOfTypeSize(unsigned Value, Type *Ty) { |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1567 | return Value % Ty->getPrimitiveSizeInBits() == 0; |
| 1568 | } |
| 1569 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1570 | static unsigned getTypeSizeIndex(unsigned Value, Type *Ty) { |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1571 | return Value / Ty->getPrimitiveSizeInBits(); |
| 1572 | } |
| 1573 | |
Sanjay Patel | 2fbab9d8 | 2015-09-09 14:34:26 +0000 | [diff] [blame] | 1574 | /// V is a value which is inserted into a vector of VecEltTy. |
| 1575 | /// Look through the value to see if we can decompose it into |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1576 | /// insertions into the vector. See the example in the comment for |
| 1577 | /// OptimizeIntegerToVectorInsertions for the pattern this handles. |
| 1578 | /// The type of V is always a non-zero multiple of VecEltTy's size. |
Richard Sandiford | feb3471 | 2013-08-12 07:26:09 +0000 | [diff] [blame] | 1579 | /// Shift is the number of bits between the lsb of V and the lsb of |
| 1580 | /// the vector. |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1581 | /// |
| 1582 | /// This returns false if the pattern can't be matched or true if it can, |
| 1583 | /// filling in Elements with the elements found here. |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1584 | static bool collectInsertionElements(Value *V, unsigned Shift, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1585 | SmallVectorImpl<Value *> &Elements, |
| 1586 | Type *VecEltTy, bool isBigEndian) { |
Richard Sandiford | feb3471 | 2013-08-12 07:26:09 +0000 | [diff] [blame] | 1587 | assert(isMultipleOfTypeSize(Shift, VecEltTy) && |
| 1588 | "Shift should be a multiple of the element type size"); |
| 1589 | |
Chris Lattner | 50df36a | 2010-08-28 03:36:51 +0000 | [diff] [blame] | 1590 | // Undef values never contribute useful bits to the result. |
| 1591 | if (isa<UndefValue>(V)) return true; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1592 | |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1593 | // If we got down to a value of the right type, we win, try inserting into the |
| 1594 | // right element. |
| 1595 | if (V->getType() == VecEltTy) { |
Chris Lattner | d0214f3 | 2010-08-28 01:50:57 +0000 | [diff] [blame] | 1596 | // Inserting null doesn't actually insert any elements. |
| 1597 | if (Constant *C = dyn_cast<Constant>(V)) |
| 1598 | if (C->isNullValue()) |
| 1599 | return true; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1600 | |
Richard Sandiford | feb3471 | 2013-08-12 07:26:09 +0000 | [diff] [blame] | 1601 | unsigned ElementIndex = getTypeSizeIndex(Shift, VecEltTy); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1602 | if (isBigEndian) |
Richard Sandiford | feb3471 | 2013-08-12 07:26:09 +0000 | [diff] [blame] | 1603 | ElementIndex = Elements.size() - ElementIndex - 1; |
| 1604 | |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1605 | // Fail if multiple elements are inserted into this slot. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1606 | if (Elements[ElementIndex]) |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1607 | return false; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1608 | |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1609 | Elements[ElementIndex] = V; |
| 1610 | return true; |
| 1611 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1612 | |
Chris Lattner | d0214f3 | 2010-08-28 01:50:57 +0000 | [diff] [blame] | 1613 | if (Constant *C = dyn_cast<Constant>(V)) { |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1614 | // Figure out the # elements this provides, and bitcast it or slice it up |
| 1615 | // as required. |
Chris Lattner | d0214f3 | 2010-08-28 01:50:57 +0000 | [diff] [blame] | 1616 | unsigned NumElts = getTypeSizeIndex(C->getType()->getPrimitiveSizeInBits(), |
| 1617 | VecEltTy); |
| 1618 | // If the constant is the size of a vector element, we just need to bitcast |
| 1619 | // it to the right type so it gets properly inserted. |
| 1620 | if (NumElts == 1) |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1621 | return collectInsertionElements(ConstantExpr::getBitCast(C, VecEltTy), |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1622 | Shift, Elements, VecEltTy, isBigEndian); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1623 | |
Chris Lattner | d0214f3 | 2010-08-28 01:50:57 +0000 | [diff] [blame] | 1624 | // Okay, this is a constant that covers multiple elements. Slice it up into |
| 1625 | // pieces and insert each element-sized piece into the vector. |
| 1626 | if (!isa<IntegerType>(C->getType())) |
| 1627 | C = ConstantExpr::getBitCast(C, IntegerType::get(V->getContext(), |
| 1628 | C->getType()->getPrimitiveSizeInBits())); |
| 1629 | unsigned ElementSize = VecEltTy->getPrimitiveSizeInBits(); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1630 | Type *ElementIntTy = IntegerType::get(C->getContext(), ElementSize); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1631 | |
Chris Lattner | d0214f3 | 2010-08-28 01:50:57 +0000 | [diff] [blame] | 1632 | for (unsigned i = 0; i != NumElts; ++i) { |
Richard Sandiford | feb3471 | 2013-08-12 07:26:09 +0000 | [diff] [blame] | 1633 | unsigned ShiftI = Shift+i*ElementSize; |
Chris Lattner | d0214f3 | 2010-08-28 01:50:57 +0000 | [diff] [blame] | 1634 | Constant *Piece = ConstantExpr::getLShr(C, ConstantInt::get(C->getType(), |
Richard Sandiford | feb3471 | 2013-08-12 07:26:09 +0000 | [diff] [blame] | 1635 | ShiftI)); |
Chris Lattner | d0214f3 | 2010-08-28 01:50:57 +0000 | [diff] [blame] | 1636 | Piece = ConstantExpr::getTrunc(Piece, ElementIntTy); |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1637 | if (!collectInsertionElements(Piece, ShiftI, Elements, VecEltTy, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1638 | isBigEndian)) |
Chris Lattner | d0214f3 | 2010-08-28 01:50:57 +0000 | [diff] [blame] | 1639 | return false; |
| 1640 | } |
| 1641 | return true; |
| 1642 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1643 | |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1644 | if (!V->hasOneUse()) return false; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1645 | |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1646 | Instruction *I = dyn_cast<Instruction>(V); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1647 | if (!I) return false; |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1648 | switch (I->getOpcode()) { |
| 1649 | default: return false; // Unhandled case. |
| 1650 | case Instruction::BitCast: |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1651 | return collectInsertionElements(I->getOperand(0), Shift, Elements, VecEltTy, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1652 | isBigEndian); |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1653 | case Instruction::ZExt: |
| 1654 | if (!isMultipleOfTypeSize( |
| 1655 | I->getOperand(0)->getType()->getPrimitiveSizeInBits(), |
| 1656 | VecEltTy)) |
| 1657 | return false; |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1658 | return collectInsertionElements(I->getOperand(0), Shift, Elements, VecEltTy, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1659 | isBigEndian); |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1660 | case Instruction::Or: |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1661 | return collectInsertionElements(I->getOperand(0), Shift, Elements, VecEltTy, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1662 | isBigEndian) && |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1663 | collectInsertionElements(I->getOperand(1), Shift, Elements, VecEltTy, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1664 | isBigEndian); |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1665 | case Instruction::Shl: { |
| 1666 | // Must be shifting by a constant that is a multiple of the element size. |
| 1667 | ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1)); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1668 | if (!CI) return false; |
Richard Sandiford | feb3471 | 2013-08-12 07:26:09 +0000 | [diff] [blame] | 1669 | Shift += CI->getZExtValue(); |
| 1670 | if (!isMultipleOfTypeSize(Shift, VecEltTy)) return false; |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1671 | return collectInsertionElements(I->getOperand(0), Shift, Elements, VecEltTy, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1672 | isBigEndian); |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1673 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1674 | |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1675 | } |
| 1676 | } |
| 1677 | |
| 1678 | |
Sanjay Patel | 2fbab9d8 | 2015-09-09 14:34:26 +0000 | [diff] [blame] | 1679 | /// If the input is an 'or' instruction, we may be doing shifts and ors to |
| 1680 | /// assemble the elements of the vector manually. |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1681 | /// Try to rip the code out and replace it with insertelements. This is to |
| 1682 | /// optimize code like this: |
| 1683 | /// |
| 1684 | /// %tmp37 = bitcast float %inc to i32 |
| 1685 | /// %tmp38 = zext i32 %tmp37 to i64 |
| 1686 | /// %tmp31 = bitcast float %inc5 to i32 |
| 1687 | /// %tmp32 = zext i32 %tmp31 to i64 |
| 1688 | /// %tmp33 = shl i64 %tmp32, 32 |
| 1689 | /// %ins35 = or i64 %tmp33, %tmp38 |
| 1690 | /// %tmp43 = bitcast i64 %ins35 to <2 x float> |
| 1691 | /// |
| 1692 | /// Into two insertelements that do "buildvector{%inc, %inc5}". |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1693 | static Value *optimizeIntegerToVectorInsertions(BitCastInst &CI, |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1694 | InstCombiner &IC) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1695 | VectorType *DestVecTy = cast<VectorType>(CI.getType()); |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1696 | Value *IntInput = CI.getOperand(0); |
| 1697 | |
| 1698 | SmallVector<Value*, 8> Elements(DestVecTy->getNumElements()); |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1699 | if (!collectInsertionElements(IntInput, 0, Elements, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1700 | DestVecTy->getElementType(), |
| 1701 | IC.getDataLayout().isBigEndian())) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1702 | return nullptr; |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1703 | |
| 1704 | // If we succeeded, we know that all of the element are specified by Elements |
| 1705 | // or are zero if Elements has a null entry. Recast this as a set of |
| 1706 | // insertions. |
| 1707 | Value *Result = Constant::getNullValue(CI.getType()); |
| 1708 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1709 | if (!Elements[i]) continue; // Unset element. |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1710 | |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1711 | Result = IC.Builder->CreateInsertElement(Result, Elements[i], |
| 1712 | IC.Builder->getInt32(i)); |
| 1713 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1714 | |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1715 | return Result; |
| 1716 | } |
| 1717 | |
| 1718 | |
Sanjay Patel | 2fbab9d8 | 2015-09-09 14:34:26 +0000 | [diff] [blame] | 1719 | /// See if we can optimize an integer->float/double bitcast. |
| 1720 | /// The various long double bitcasts can't get in here. |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1721 | static Instruction *optimizeIntToFloatBitCast(BitCastInst &CI, InstCombiner &IC, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1722 | const DataLayout &DL) { |
Chris Lattner | d4ebd6d | 2010-08-26 21:55:42 +0000 | [diff] [blame] | 1723 | Value *Src = CI.getOperand(0); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1724 | Type *DestTy = CI.getType(); |
Chris Lattner | d4ebd6d | 2010-08-26 21:55:42 +0000 | [diff] [blame] | 1725 | |
| 1726 | // If this is a bitcast from int to float, check to see if the int is an |
| 1727 | // extraction from a vector. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1728 | Value *VecInput = nullptr; |
Chris Lattner | bfd2228 | 2010-08-26 22:14:59 +0000 | [diff] [blame] | 1729 | // bitcast(trunc(bitcast(somevector))) |
Chris Lattner | d4ebd6d | 2010-08-26 21:55:42 +0000 | [diff] [blame] | 1730 | if (match(Src, m_Trunc(m_BitCast(m_Value(VecInput)))) && |
| 1731 | isa<VectorType>(VecInput->getType())) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1732 | VectorType *VecTy = cast<VectorType>(VecInput->getType()); |
Chris Lattner | bfd2228 | 2010-08-26 22:14:59 +0000 | [diff] [blame] | 1733 | unsigned DestWidth = DestTy->getPrimitiveSizeInBits(); |
| 1734 | |
| 1735 | if (VecTy->getPrimitiveSizeInBits() % DestWidth == 0) { |
| 1736 | // If the element type of the vector doesn't match the result type, |
| 1737 | // bitcast it to be a vector type we can extract from. |
| 1738 | if (VecTy->getElementType() != DestTy) { |
| 1739 | VecTy = VectorType::get(DestTy, |
| 1740 | VecTy->getPrimitiveSizeInBits() / DestWidth); |
| 1741 | VecInput = IC.Builder->CreateBitCast(VecInput, VecTy); |
| 1742 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1743 | |
Ulrich Weigand | 8a51d8e | 2013-03-26 15:36:14 +0000 | [diff] [blame] | 1744 | unsigned Elt = 0; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1745 | if (DL.isBigEndian()) |
Ulrich Weigand | 8a51d8e | 2013-03-26 15:36:14 +0000 | [diff] [blame] | 1746 | Elt = VecTy->getPrimitiveSizeInBits() / DestWidth - 1; |
| 1747 | return ExtractElementInst::Create(VecInput, IC.Builder->getInt32(Elt)); |
Chris Lattner | bfd2228 | 2010-08-26 22:14:59 +0000 | [diff] [blame] | 1748 | } |
Chris Lattner | d4ebd6d | 2010-08-26 21:55:42 +0000 | [diff] [blame] | 1749 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1750 | |
Chris Lattner | bfd2228 | 2010-08-26 22:14:59 +0000 | [diff] [blame] | 1751 | // bitcast(trunc(lshr(bitcast(somevector), cst)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1752 | ConstantInt *ShAmt = nullptr; |
Chris Lattner | bfd2228 | 2010-08-26 22:14:59 +0000 | [diff] [blame] | 1753 | if (match(Src, m_Trunc(m_LShr(m_BitCast(m_Value(VecInput)), |
| 1754 | m_ConstantInt(ShAmt)))) && |
| 1755 | isa<VectorType>(VecInput->getType())) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1756 | VectorType *VecTy = cast<VectorType>(VecInput->getType()); |
Chris Lattner | bfd2228 | 2010-08-26 22:14:59 +0000 | [diff] [blame] | 1757 | unsigned DestWidth = DestTy->getPrimitiveSizeInBits(); |
| 1758 | if (VecTy->getPrimitiveSizeInBits() % DestWidth == 0 && |
| 1759 | ShAmt->getZExtValue() % DestWidth == 0) { |
| 1760 | // If the element type of the vector doesn't match the result type, |
| 1761 | // bitcast it to be a vector type we can extract from. |
| 1762 | if (VecTy->getElementType() != DestTy) { |
| 1763 | VecTy = VectorType::get(DestTy, |
| 1764 | VecTy->getPrimitiveSizeInBits() / DestWidth); |
| 1765 | VecInput = IC.Builder->CreateBitCast(VecInput, VecTy); |
| 1766 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1767 | |
Chris Lattner | bfd2228 | 2010-08-26 22:14:59 +0000 | [diff] [blame] | 1768 | unsigned Elt = ShAmt->getZExtValue() / DestWidth; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1769 | if (DL.isBigEndian()) |
Ulrich Weigand | 8a51d8e | 2013-03-26 15:36:14 +0000 | [diff] [blame] | 1770 | Elt = VecTy->getPrimitiveSizeInBits() / DestWidth - 1 - Elt; |
Chris Lattner | bfd2228 | 2010-08-26 22:14:59 +0000 | [diff] [blame] | 1771 | return ExtractElementInst::Create(VecInput, IC.Builder->getInt32(Elt)); |
| 1772 | } |
| 1773 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1774 | return nullptr; |
Chris Lattner | d4ebd6d | 2010-08-26 21:55:42 +0000 | [diff] [blame] | 1775 | } |
Chris Lattner | 02b0df5 | 2010-05-08 21:50:26 +0000 | [diff] [blame] | 1776 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1777 | Instruction *InstCombiner::visitBitCast(BitCastInst &CI) { |
| 1778 | // If the operands are integer typed then apply the integer transforms, |
| 1779 | // otherwise just apply the common ones. |
| 1780 | Value *Src = CI.getOperand(0); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1781 | Type *SrcTy = Src->getType(); |
| 1782 | Type *DestTy = CI.getType(); |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1783 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1784 | // Get rid of casts from one type to the same type. These are useless and can |
| 1785 | // be replaced by the operand. |
| 1786 | if (DestTy == Src->getType()) |
| 1787 | return ReplaceInstUsesWith(CI, Src); |
| 1788 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1789 | if (PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) { |
| 1790 | PointerType *SrcPTy = cast<PointerType>(SrcTy); |
| 1791 | Type *DstElTy = DstPTy->getElementType(); |
| 1792 | Type *SrcElTy = SrcPTy->getElementType(); |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1793 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1794 | // If we are casting a alloca to a pointer to a type of the same |
| 1795 | // size, rewrite the allocation instruction to allocate the "right" type. |
| 1796 | // There is no need to modify malloc calls because it is their bitcast that |
| 1797 | // needs to be cleaned up. |
| 1798 | if (AllocaInst *AI = dyn_cast<AllocaInst>(Src)) |
| 1799 | if (Instruction *V = PromoteCastOfAllocation(CI, *AI)) |
| 1800 | return V; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1801 | |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1802 | // If the source and destination are pointers, and this cast is equivalent |
| 1803 | // to a getelementptr X, 0, 0, 0... turn it into the appropriate gep. |
| 1804 | // This can enhance SROA and other transforms that want type-safe pointers. |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1805 | unsigned NumZeros = 0; |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1806 | while (SrcElTy != DstElTy && |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1807 | isa<CompositeType>(SrcElTy) && !SrcElTy->isPointerTy() && |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1808 | SrcElTy->getNumContainedTypes() /* not "{}" */) { |
Benjamin Kramer | 2a7404a | 2015-04-18 16:52:08 +0000 | [diff] [blame] | 1809 | SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(0U); |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1810 | ++NumZeros; |
| 1811 | } |
| 1812 | |
| 1813 | // If we found a path from the src to dest, create the getelementptr now. |
| 1814 | if (SrcElTy == DstElTy) { |
Benjamin Kramer | 2a7404a | 2015-04-18 16:52:08 +0000 | [diff] [blame] | 1815 | SmallVector<Value *, 8> Idxs(NumZeros + 1, Builder->getInt32(0)); |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1816 | return GetElementPtrInst::CreateInBounds(Src, Idxs); |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1817 | } |
| 1818 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1819 | |
Chris Lattner | d4ebd6d | 2010-08-26 21:55:42 +0000 | [diff] [blame] | 1820 | // Try to optimize int -> float bitcasts. |
| 1821 | if ((DestTy->isFloatTy() || DestTy->isDoubleTy()) && isa<IntegerType>(SrcTy)) |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1822 | if (Instruction *I = optimizeIntToFloatBitCast(CI, *this, DL)) |
Chris Lattner | d4ebd6d | 2010-08-26 21:55:42 +0000 | [diff] [blame] | 1823 | return I; |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1824 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1825 | if (VectorType *DestVTy = dyn_cast<VectorType>(DestTy)) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1826 | if (DestVTy->getNumElements() == 1 && !SrcTy->isVectorTy()) { |
Chris Lattner | a93c63c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1827 | Value *Elem = Builder->CreateBitCast(Src, DestVTy->getElementType()); |
| 1828 | return InsertElementInst::Create(UndefValue::get(DestTy), Elem, |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1829 | Constant::getNullValue(Type::getInt32Ty(CI.getContext()))); |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1830 | // FIXME: Canonicalize bitcast(insertelement) -> insertelement(bitcast) |
| 1831 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1832 | |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1833 | if (isa<IntegerType>(SrcTy)) { |
| 1834 | // If this is a cast from an integer to vector, check to see if the input |
| 1835 | // is a trunc or zext of a bitcast from vector. If so, we can replace all |
| 1836 | // the casts with a shuffle and (potentially) a bitcast. |
| 1837 | if (isa<TruncInst>(Src) || isa<ZExtInst>(Src)) { |
| 1838 | CastInst *SrcCast = cast<CastInst>(Src); |
| 1839 | if (BitCastInst *BCIn = dyn_cast<BitCastInst>(SrcCast->getOperand(0))) |
| 1840 | if (isa<VectorType>(BCIn->getOperand(0)->getType())) |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1841 | if (Instruction *I = optimizeVectorResize(BCIn->getOperand(0), |
Chris Lattner | 02b0df5 | 2010-05-08 21:50:26 +0000 | [diff] [blame] | 1842 | cast<VectorType>(DestTy), *this)) |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1843 | return I; |
| 1844 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1845 | |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1846 | // If the input is an 'or' instruction, we may be doing shifts and ors to |
| 1847 | // assemble the elements of the vector manually. Try to rip the code out |
| 1848 | // and replace it with insertelements. |
Sanjay Patel | e283441 | 2015-09-09 14:54:29 +0000 | [diff] [blame] | 1849 | if (Value *V = optimizeIntegerToVectorInsertions(CI, *this)) |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 1850 | return ReplaceInstUsesWith(CI, V); |
Chris Lattner | 02b0df5 | 2010-05-08 21:50:26 +0000 | [diff] [blame] | 1851 | } |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1854 | if (VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy)) { |
Michael Ilseman | 74a6da9 | 2013-02-11 21:41:44 +0000 | [diff] [blame] | 1855 | if (SrcVTy->getNumElements() == 1) { |
| 1856 | // If our destination is not a vector, then make this a straight |
| 1857 | // scalar-scalar cast. |
| 1858 | if (!DestTy->isVectorTy()) { |
| 1859 | Value *Elem = |
| 1860 | Builder->CreateExtractElement(Src, |
| 1861 | Constant::getNullValue(Type::getInt32Ty(CI.getContext()))); |
| 1862 | return CastInst::Create(Instruction::BitCast, Elem, DestTy); |
| 1863 | } |
| 1864 | |
| 1865 | // Otherwise, see if our source is an insert. If so, then use the scalar |
| 1866 | // component directly. |
| 1867 | if (InsertElementInst *IEI = |
| 1868 | dyn_cast<InsertElementInst>(CI.getOperand(0))) |
| 1869 | return CastInst::Create(Instruction::BitCast, IEI->getOperand(1), |
| 1870 | DestTy); |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1871 | } |
| 1872 | } |
| 1873 | |
| 1874 | if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) { |
Chris Lattner | a93c63c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1875 | // Okay, we have (bitcast (shuffle ..)). Check to see if this is |
Dan Gohman | eb7111b | 2010-04-07 23:22:42 +0000 | [diff] [blame] | 1876 | // a bitcast to a vector with the same # elts. |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1877 | if (SVI->hasOneUse() && DestTy->isVectorTy() && |
Matt Arsenault | fc00f7e | 2013-08-14 00:24:34 +0000 | [diff] [blame] | 1878 | DestTy->getVectorNumElements() == SVI->getType()->getNumElements() && |
Chris Lattner | a93c63c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1879 | SVI->getType()->getNumElements() == |
Matt Arsenault | fc00f7e | 2013-08-14 00:24:34 +0000 | [diff] [blame] | 1880 | SVI->getOperand(0)->getType()->getVectorNumElements()) { |
Chris Lattner | a93c63c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1881 | BitCastInst *Tmp; |
| 1882 | // If either of the operands is a cast from CI.getType(), then |
| 1883 | // evaluating the shuffle in the casted destination's type will allow |
| 1884 | // us to eliminate at least one cast. |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1885 | if (((Tmp = dyn_cast<BitCastInst>(SVI->getOperand(0))) && |
Chris Lattner | a93c63c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1886 | Tmp->getOperand(0)->getType() == DestTy) || |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1887 | ((Tmp = dyn_cast<BitCastInst>(SVI->getOperand(1))) && |
Chris Lattner | a93c63c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1888 | Tmp->getOperand(0)->getType() == DestTy)) { |
| 1889 | Value *LHS = Builder->CreateBitCast(SVI->getOperand(0), DestTy); |
| 1890 | Value *RHS = Builder->CreateBitCast(SVI->getOperand(1), DestTy); |
| 1891 | // Return a new shuffle vector. Use the same element ID's, as we |
| 1892 | // know the vector types match #elts. |
| 1893 | return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2)); |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1894 | } |
| 1895 | } |
| 1896 | } |
Craig Topper | 3529aa5 | 2013-01-24 05:22:40 +0000 | [diff] [blame] | 1897 | |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1898 | if (SrcTy->isPointerTy()) |
Chris Lattner | a93c63c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1899 | return commonPointerCastTransforms(CI); |
| 1900 | return commonCastTransforms(CI); |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1901 | } |
Matt Arsenault | a9e95ab | 2013-11-15 05:45:08 +0000 | [diff] [blame] | 1902 | |
| 1903 | Instruction *InstCombiner::visitAddrSpaceCast(AddrSpaceCastInst &CI) { |
Manuel Jacob | b4db99c | 2014-07-16 01:34:21 +0000 | [diff] [blame] | 1904 | // If the destination pointer element type is not the same as the source's |
| 1905 | // first do a bitcast to the destination type, and then the addrspacecast. |
| 1906 | // This allows the cast to be exposed to other transforms. |
Jingyue Wu | 77145d9 | 2014-06-06 21:52:55 +0000 | [diff] [blame] | 1907 | Value *Src = CI.getOperand(0); |
| 1908 | PointerType *SrcTy = cast<PointerType>(Src->getType()->getScalarType()); |
| 1909 | PointerType *DestTy = cast<PointerType>(CI.getType()->getScalarType()); |
| 1910 | |
| 1911 | Type *DestElemTy = DestTy->getElementType(); |
| 1912 | if (SrcTy->getElementType() != DestElemTy) { |
| 1913 | Type *MidTy = PointerType::get(DestElemTy, SrcTy->getAddressSpace()); |
Jingyue Wu | baabe50 | 2014-06-15 21:40:57 +0000 | [diff] [blame] | 1914 | if (VectorType *VT = dyn_cast<VectorType>(CI.getType())) { |
| 1915 | // Handle vectors of pointers. |
| 1916 | MidTy = VectorType::get(MidTy, VT->getNumElements()); |
| 1917 | } |
Jingyue Wu | 77145d9 | 2014-06-06 21:52:55 +0000 | [diff] [blame] | 1918 | |
| 1919 | Value *NewBitCast = Builder->CreateBitCast(Src, MidTy); |
| 1920 | return new AddrSpaceCastInst(NewBitCast, CI.getType()); |
| 1921 | } |
| 1922 | |
Matt Arsenault | 2d353d1 | 2014-01-14 20:00:45 +0000 | [diff] [blame] | 1923 | return commonPointerCastTransforms(CI); |
Matt Arsenault | a9e95ab | 2013-11-15 05:45:08 +0000 | [diff] [blame] | 1924 | } |