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