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