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