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" |
| 15 | #include "llvm/Target/TargetData.h" |
| 16 | #include "llvm/Support/PatternMatch.h" |
| 17 | using namespace llvm; |
| 18 | using namespace PatternMatch; |
| 19 | |
Chris Lattner | f3d1b5d | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 20 | /// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear |
| 21 | /// expression. If so, decompose it, returning some value X, such that Val is |
| 22 | /// X*Scale+Offset. |
| 23 | /// |
| 24 | static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale, |
Dan Gohman | 28d2e0a | 2010-05-28 04:33:04 +0000 | [diff] [blame] | 25 | uint64_t &Offset) { |
Chris Lattner | f3d1b5d | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 26 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { |
| 27 | Offset = CI->getZExtValue(); |
| 28 | Scale = 0; |
Dan Gohman | 28d2e0a | 2010-05-28 04:33:04 +0000 | [diff] [blame] | 29 | return ConstantInt::get(Val->getType(), 0); |
Chris Lattner | f86d799 | 2010-01-05 20:57:30 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) { |
Chris Lattner | f3d1b5d | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 33 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 34 | if (I->getOpcode() == Instruction::Shl) { |
| 35 | // This is a value scaled by '1 << the shift amt'. |
Dan Gohman | 28d2e0a | 2010-05-28 04:33:04 +0000 | [diff] [blame] | 36 | Scale = UINT64_C(1) << RHS->getZExtValue(); |
Chris Lattner | f3d1b5d | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 37 | Offset = 0; |
| 38 | return I->getOperand(0); |
Chris Lattner | f86d799 | 2010-01-05 20:57:30 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | if (I->getOpcode() == Instruction::Mul) { |
Chris Lattner | f3d1b5d | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 42 | // This value is scaled by 'RHS'. |
| 43 | Scale = RHS->getZExtValue(); |
| 44 | Offset = 0; |
| 45 | return I->getOperand(0); |
Chris Lattner | f86d799 | 2010-01-05 20:57:30 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | if (I->getOpcode() == Instruction::Add) { |
Chris Lattner | f3d1b5d | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 49 | // We have X+C. Check to see if we really have (X*C2)+C1, |
| 50 | // where C1 is divisible by C2. |
| 51 | unsigned SubScale; |
| 52 | Value *SubVal = |
| 53 | DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset); |
| 54 | Offset += RHS->getZExtValue(); |
| 55 | Scale = SubScale; |
| 56 | return SubVal; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Otherwise, we can't look past this. |
| 62 | Scale = 1; |
| 63 | Offset = 0; |
| 64 | return Val; |
| 65 | } |
| 66 | |
| 67 | /// PromoteCastOfAllocation - If we find a cast of an allocation instruction, |
| 68 | /// try to eliminate the cast by moving the type information into the alloc. |
| 69 | Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI, |
| 70 | AllocaInst &AI) { |
| 71 | // This requires TargetData to get the alloca alignment and size information. |
| 72 | if (!TD) return 0; |
| 73 | |
| 74 | const PointerType *PTy = cast<PointerType>(CI.getType()); |
| 75 | |
| 76 | BuilderTy AllocaBuilder(*Builder); |
| 77 | AllocaBuilder.SetInsertPoint(AI.getParent(), &AI); |
| 78 | |
| 79 | // Get the type really allocated and the type casted to. |
| 80 | const Type *AllocElTy = AI.getAllocatedType(); |
| 81 | const Type *CastElTy = PTy->getElementType(); |
| 82 | if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0; |
| 83 | |
| 84 | unsigned AllocElTyAlign = TD->getABITypeAlignment(AllocElTy); |
| 85 | unsigned CastElTyAlign = TD->getABITypeAlignment(CastElTy); |
| 86 | if (CastElTyAlign < AllocElTyAlign) return 0; |
| 87 | |
| 88 | // If the allocation has multiple uses, only promote it if we are strictly |
| 89 | // increasing the alignment of the resultant allocation. If we keep it the |
| 90 | // same, we open the door to infinite loops of various kinds. (A reference |
| 91 | // from a dbg.declare doesn't count as a use for this purpose.) |
| 92 | if (!AI.hasOneUse() && !hasOneUsePlusDeclare(&AI) && |
| 93 | CastElTyAlign == AllocElTyAlign) return 0; |
| 94 | |
| 95 | uint64_t AllocElTySize = TD->getTypeAllocSize(AllocElTy); |
| 96 | uint64_t CastElTySize = TD->getTypeAllocSize(CastElTy); |
| 97 | if (CastElTySize == 0 || AllocElTySize == 0) return 0; |
| 98 | |
| 99 | // See if we can satisfy the modulus by pulling a scale out of the array |
| 100 | // size argument. |
| 101 | unsigned ArraySizeScale; |
Dan Gohman | 28d2e0a | 2010-05-28 04:33:04 +0000 | [diff] [blame] | 102 | uint64_t ArrayOffset; |
Chris Lattner | f3d1b5d | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 103 | Value *NumElements = // See if the array size is a decomposable linear expr. |
| 104 | DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset); |
| 105 | |
| 106 | // If we can now satisfy the modulus, by using a non-1 scale, we really can |
| 107 | // do the xform. |
| 108 | if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 || |
| 109 | (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return 0; |
| 110 | |
| 111 | unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize; |
| 112 | Value *Amt = 0; |
| 113 | if (Scale == 1) { |
| 114 | Amt = NumElements; |
| 115 | } else { |
Dan Gohman | 28d2e0a | 2010-05-28 04:33:04 +0000 | [diff] [blame] | 116 | Amt = ConstantInt::get(AI.getArraySize()->getType(), Scale); |
Chris Lattner | f3d1b5d | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 117 | // Insert before the alloca, not before the cast. |
| 118 | Amt = AllocaBuilder.CreateMul(Amt, NumElements, "tmp"); |
| 119 | } |
| 120 | |
Dan Gohman | 28d2e0a | 2010-05-28 04:33:04 +0000 | [diff] [blame] | 121 | if (uint64_t Offset = (AllocElTySize*ArrayOffset)/CastElTySize) { |
| 122 | Value *Off = ConstantInt::get(AI.getArraySize()->getType(), |
Chris Lattner | f3d1b5d | 2010-01-04 07:59:07 +0000 | [diff] [blame] | 123 | Offset, true); |
| 124 | Amt = AllocaBuilder.CreateAdd(Amt, Off, "tmp"); |
| 125 | } |
| 126 | |
| 127 | AllocaInst *New = AllocaBuilder.CreateAlloca(CastElTy, Amt); |
| 128 | New->setAlignment(AI.getAlignment()); |
| 129 | New->takeName(&AI); |
| 130 | |
| 131 | // If the allocation has one real use plus a dbg.declare, just remove the |
| 132 | // declare. |
| 133 | if (DbgDeclareInst *DI = hasOneUsePlusDeclare(&AI)) { |
| 134 | EraseInstFromFunction(*(Instruction*)DI); |
| 135 | } |
| 136 | // If the allocation has multiple real uses, insert a cast and change all |
| 137 | // things that used it to use the new cast. This will also hack on CI, but it |
| 138 | // will die soon. |
| 139 | else if (!AI.hasOneUse()) { |
| 140 | // New is the allocation instruction, pointer typed. AI is the original |
| 141 | // allocation instruction, also pointer typed. Thus, cast to use is BitCast. |
| 142 | Value *NewCast = AllocaBuilder.CreateBitCast(New, AI.getType(), "tmpcast"); |
| 143 | AI.replaceAllUsesWith(NewCast); |
| 144 | } |
| 145 | return ReplaceInstUsesWith(CI, New); |
| 146 | } |
| 147 | |
| 148 | |
Chris Lattner | e0e4cc7 | 2010-01-06 01:56:21 +0000 | [diff] [blame] | 149 | |
Chris Lattner | 5f0290e | 2010-01-04 07:54:59 +0000 | [diff] [blame] | 150 | /// EvaluateInDifferentType - Given an expression that |
Chris Lattner | 14bf8f0 | 2010-01-08 19:19:23 +0000 | [diff] [blame] | 151 | /// CanEvaluateTruncated or CanEvaluateSExtd returns true for, actually |
Chris Lattner | e0e4cc7 | 2010-01-06 01:56:21 +0000 | [diff] [blame] | 152 | /// insert the code to evaluate the expression. |
Chris Lattner | 5f0290e | 2010-01-04 07:54:59 +0000 | [diff] [blame] | 153 | Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty, |
| 154 | bool isSigned) { |
Chris Lattner | c8b3fce | 2010-01-08 19:28:47 +0000 | [diff] [blame] | 155 | if (Constant *C = dyn_cast<Constant>(V)) { |
| 156 | C = ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/); |
| 157 | // If we got a constantexpr back, try to simplify it with TD info. |
| 158 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) |
| 159 | C = ConstantFoldConstantExpression(CE, TD); |
| 160 | return C; |
| 161 | } |
Chris Lattner | 5f0290e | 2010-01-04 07:54:59 +0000 | [diff] [blame] | 162 | |
| 163 | // Otherwise, it must be an instruction. |
| 164 | Instruction *I = cast<Instruction>(V); |
| 165 | Instruction *Res = 0; |
| 166 | unsigned Opc = I->getOpcode(); |
| 167 | switch (Opc) { |
| 168 | case Instruction::Add: |
| 169 | case Instruction::Sub: |
| 170 | case Instruction::Mul: |
| 171 | case Instruction::And: |
| 172 | case Instruction::Or: |
| 173 | case Instruction::Xor: |
| 174 | case Instruction::AShr: |
| 175 | case Instruction::LShr: |
| 176 | case Instruction::Shl: |
| 177 | case Instruction::UDiv: |
| 178 | case Instruction::URem: { |
| 179 | Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned); |
| 180 | Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); |
| 181 | Res = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); |
| 182 | break; |
| 183 | } |
| 184 | case Instruction::Trunc: |
| 185 | case Instruction::ZExt: |
| 186 | case Instruction::SExt: |
| 187 | // If the source type of the cast is the type we're trying for then we can |
| 188 | // just return the source. There's no need to insert it because it is not |
| 189 | // new. |
| 190 | if (I->getOperand(0)->getType() == Ty) |
| 191 | return I->getOperand(0); |
| 192 | |
| 193 | // 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] | 194 | // This also handles the case of zext(trunc(x)) -> zext(x). |
| 195 | Res = CastInst::CreateIntegerCast(I->getOperand(0), Ty, |
| 196 | Opc == Instruction::SExt); |
Chris Lattner | 5f0290e | 2010-01-04 07:54:59 +0000 | [diff] [blame] | 197 | break; |
| 198 | case Instruction::Select: { |
| 199 | Value *True = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); |
| 200 | Value *False = EvaluateInDifferentType(I->getOperand(2), Ty, isSigned); |
| 201 | Res = SelectInst::Create(I->getOperand(0), True, False); |
| 202 | break; |
| 203 | } |
| 204 | case Instruction::PHI: { |
| 205 | PHINode *OPN = cast<PHINode>(I); |
| 206 | PHINode *NPN = PHINode::Create(Ty); |
| 207 | for (unsigned i = 0, e = OPN->getNumIncomingValues(); i != e; ++i) { |
| 208 | Value *V =EvaluateInDifferentType(OPN->getIncomingValue(i), Ty, isSigned); |
| 209 | NPN->addIncoming(V, OPN->getIncomingBlock(i)); |
| 210 | } |
| 211 | Res = NPN; |
| 212 | break; |
| 213 | } |
| 214 | default: |
| 215 | // TODO: Can handle more cases here. |
| 216 | llvm_unreachable("Unreachable!"); |
| 217 | break; |
| 218 | } |
| 219 | |
| 220 | Res->takeName(I); |
| 221 | return InsertNewInstBefore(Res, *I); |
| 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 |
| 231 | const Type *DstTy, ///< The target type for the second cast instruction |
| 232 | TargetData *TD ///< The target data for pointer size |
| 233 | ) { |
| 234 | |
| 235 | const Type *SrcTy = CI->getOperand(0)->getType(); // A from above |
| 236 | const Type *MidTy = CI->getType(); // B from above |
| 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, |
| 262 | const Type *Ty) { |
| 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 | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 326 | static bool CanEvaluateTruncated(Value *V, const Type *Ty) { |
| 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 | |
| 334 | const Type *OrigTy = V->getType(); |
| 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; |
| 399 | case Instruction::Select: { |
| 400 | SelectInst *SI = cast<SelectInst>(I); |
| 401 | return CanEvaluateTruncated(SI->getTrueValue(), Ty) && |
| 402 | CanEvaluateTruncated(SI->getFalseValue(), Ty); |
| 403 | } |
| 404 | case Instruction::PHI: { |
| 405 | // We can change a phi if we can change all operands. Note that we never |
| 406 | // get into trouble with cyclic PHIs here because we only consider |
| 407 | // instructions with a single use. |
| 408 | PHINode *PN = cast<PHINode>(I); |
| 409 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 410 | if (!CanEvaluateTruncated(PN->getIncomingValue(i), Ty)) |
| 411 | return false; |
| 412 | return true; |
| 413 | } |
| 414 | default: |
| 415 | // TODO: Can handle more cases here. |
| 416 | break; |
| 417 | } |
| 418 | |
| 419 | return false; |
| 420 | } |
| 421 | |
| 422 | Instruction *InstCombiner::visitTrunc(TruncInst &CI) { |
Chris Lattner | d84dfa4 | 2010-01-10 01:00:46 +0000 | [diff] [blame] | 423 | if (Instruction *Result = commonCastTransforms(CI)) |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 424 | return Result; |
| 425 | |
Chris Lattner | d84dfa4 | 2010-01-10 01:00:46 +0000 | [diff] [blame] | 426 | // See if we can simplify any instructions used by the input whose sole |
| 427 | // purpose is to compute bits we don't care about. |
| 428 | if (SimplifyDemandedInstructionBits(CI)) |
| 429 | return &CI; |
| 430 | |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 431 | Value *Src = CI.getOperand(0); |
| 432 | const Type *DestTy = CI.getType(), *SrcTy = Src->getType(); |
| 433 | |
| 434 | // Attempt to truncate the entire input expression tree to the destination |
| 435 | // 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] | 436 | // expression tree to something weird like i93 unless the source is also |
| 437 | // strange. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 438 | if ((DestTy->isVectorTy() || ShouldChangeType(SrcTy, DestTy)) && |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 439 | CanEvaluateTruncated(Src, DestTy)) { |
Chris Lattner | e0e4cc7 | 2010-01-06 01:56:21 +0000 | [diff] [blame] | 440 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 441 | // If this cast is a truncate, evaluting in a different type always |
Chris Lattner | 68c6e89 | 2010-01-05 23:00:30 +0000 | [diff] [blame] | 442 | // eliminates the cast, so it is always a win. |
Chris Lattner | 075f692 | 2010-01-07 23:41:00 +0000 | [diff] [blame] | 443 | DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type" |
Dan Gohman | 5b71dce | 2010-05-25 21:50:35 +0000 | [diff] [blame] | 444 | " to avoid cast: " << CI << '\n'); |
Chris Lattner | 075f692 | 2010-01-07 23:41:00 +0000 | [diff] [blame] | 445 | Value *Res = EvaluateInDifferentType(Src, DestTy, false); |
| 446 | assert(Res->getType() == DestTy); |
| 447 | return ReplaceInstUsesWith(CI, Res); |
| 448 | } |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 449 | |
Chris Lattner | 7a34d6c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 450 | // Canonicalize trunc x to i1 -> (icmp ne (and x, 1), 0), likewise for vector. |
| 451 | if (DestTy->getScalarSizeInBits() == 1) { |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 452 | Constant *One = ConstantInt::get(Src->getType(), 1); |
| 453 | Src = Builder->CreateAnd(Src, One, "tmp"); |
| 454 | Value *Zero = Constant::getNullValue(Src->getType()); |
| 455 | return new ICmpInst(ICmpInst::ICMP_NE, Src, Zero); |
| 456 | } |
| 457 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | /// transformZExtICmp - Transform (zext icmp) to bitwise / integer operations |
| 462 | /// in order to eliminate the icmp. |
| 463 | Instruction *InstCombiner::transformZExtICmp(ICmpInst *ICI, Instruction &CI, |
| 464 | bool DoXform) { |
| 465 | // If we are just checking for a icmp eq of a single bit and zext'ing it |
| 466 | // to an integer, then shift the bit to the appropriate place and then |
| 467 | // cast to integer to avoid the comparison. |
| 468 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) { |
| 469 | const APInt &Op1CV = Op1C->getValue(); |
| 470 | |
| 471 | // zext (x <s 0) to i32 --> x>>u31 true if signbit set. |
| 472 | // zext (x >s -1) to i32 --> (x>>u31)^1 true if signbit clear. |
| 473 | if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) || |
| 474 | (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())) { |
| 475 | if (!DoXform) return ICI; |
| 476 | |
| 477 | Value *In = ICI->getOperand(0); |
| 478 | Value *Sh = ConstantInt::get(In->getType(), |
| 479 | In->getType()->getScalarSizeInBits()-1); |
| 480 | In = Builder->CreateLShr(In, Sh, In->getName()+".lobit"); |
| 481 | if (In->getType() != CI.getType()) |
| 482 | In = Builder->CreateIntCast(In, CI.getType(), false/*ZExt*/, "tmp"); |
| 483 | |
| 484 | if (ICI->getPredicate() == ICmpInst::ICMP_SGT) { |
| 485 | Constant *One = ConstantInt::get(In->getType(), 1); |
| 486 | In = Builder->CreateXor(In, One, In->getName()+".not"); |
| 487 | } |
| 488 | |
| 489 | return ReplaceInstUsesWith(CI, In); |
| 490 | } |
| 491 | |
| 492 | |
| 493 | |
| 494 | // zext (X == 0) to i32 --> X^1 iff X has only the low bit set. |
| 495 | // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. |
| 496 | // zext (X == 1) to i32 --> X iff X has only the low bit set. |
| 497 | // zext (X == 2) to i32 --> X>>1 iff X has only the 2nd bit set. |
| 498 | // zext (X != 0) to i32 --> X iff X has only the low bit set. |
| 499 | // zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set. |
| 500 | // zext (X != 1) to i32 --> X^1 iff X has only the low bit set. |
| 501 | // zext (X != 2) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. |
| 502 | if ((Op1CV == 0 || Op1CV.isPowerOf2()) && |
| 503 | // This only works for EQ and NE |
| 504 | ICI->isEquality()) { |
| 505 | // If Op1C some other power of two, convert: |
| 506 | uint32_t BitWidth = Op1C->getType()->getBitWidth(); |
| 507 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 508 | APInt TypeMask(APInt::getAllOnesValue(BitWidth)); |
| 509 | ComputeMaskedBits(ICI->getOperand(0), TypeMask, KnownZero, KnownOne); |
| 510 | |
| 511 | APInt KnownZeroMask(~KnownZero); |
| 512 | if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1? |
| 513 | if (!DoXform) return ICI; |
| 514 | |
| 515 | bool isNE = ICI->getPredicate() == ICmpInst::ICMP_NE; |
| 516 | if (Op1CV != 0 && (Op1CV != KnownZeroMask)) { |
| 517 | // (X&4) == 2 --> false |
| 518 | // (X&4) != 2 --> true |
| 519 | Constant *Res = ConstantInt::get(Type::getInt1Ty(CI.getContext()), |
| 520 | isNE); |
| 521 | Res = ConstantExpr::getZExt(Res, CI.getType()); |
| 522 | return ReplaceInstUsesWith(CI, Res); |
| 523 | } |
| 524 | |
| 525 | uint32_t ShiftAmt = KnownZeroMask.logBase2(); |
| 526 | Value *In = ICI->getOperand(0); |
| 527 | if (ShiftAmt) { |
| 528 | // Perform a logical shr by shiftamt. |
| 529 | // Insert the shift to put the result in the low bit. |
| 530 | In = Builder->CreateLShr(In, ConstantInt::get(In->getType(),ShiftAmt), |
| 531 | In->getName()+".lobit"); |
| 532 | } |
| 533 | |
| 534 | if ((Op1CV != 0) == isNE) { // Toggle the low bit. |
| 535 | Constant *One = ConstantInt::get(In->getType(), 1); |
| 536 | In = Builder->CreateXor(In, One, "tmp"); |
| 537 | } |
| 538 | |
| 539 | if (CI.getType() == In->getType()) |
| 540 | return ReplaceInstUsesWith(CI, In); |
| 541 | else |
| 542 | return CastInst::CreateIntegerCast(In, CI.getType(), false/*ZExt*/); |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | // icmp ne A, B is equal to xor A, B when A and B only really have one bit. |
| 548 | // It is also profitable to transform icmp eq into not(xor(A, B)) because that |
| 549 | // may lead to additional simplifications. |
| 550 | if (ICI->isEquality() && CI.getType() == ICI->getOperand(0)->getType()) { |
| 551 | if (const IntegerType *ITy = dyn_cast<IntegerType>(CI.getType())) { |
| 552 | uint32_t BitWidth = ITy->getBitWidth(); |
| 553 | Value *LHS = ICI->getOperand(0); |
| 554 | Value *RHS = ICI->getOperand(1); |
| 555 | |
| 556 | APInt KnownZeroLHS(BitWidth, 0), KnownOneLHS(BitWidth, 0); |
| 557 | APInt KnownZeroRHS(BitWidth, 0), KnownOneRHS(BitWidth, 0); |
| 558 | APInt TypeMask(APInt::getAllOnesValue(BitWidth)); |
| 559 | ComputeMaskedBits(LHS, TypeMask, KnownZeroLHS, KnownOneLHS); |
| 560 | ComputeMaskedBits(RHS, TypeMask, KnownZeroRHS, KnownOneRHS); |
| 561 | |
| 562 | if (KnownZeroLHS == KnownZeroRHS && KnownOneLHS == KnownOneRHS) { |
| 563 | APInt KnownBits = KnownZeroLHS | KnownOneLHS; |
| 564 | APInt UnknownBit = ~KnownBits; |
| 565 | if (UnknownBit.countPopulation() == 1) { |
| 566 | if (!DoXform) return ICI; |
| 567 | |
| 568 | Value *Result = Builder->CreateXor(LHS, RHS); |
| 569 | |
| 570 | // Mask off any bits that are set and won't be shifted away. |
| 571 | if (KnownOneLHS.uge(UnknownBit)) |
| 572 | Result = Builder->CreateAnd(Result, |
| 573 | ConstantInt::get(ITy, UnknownBit)); |
| 574 | |
| 575 | // Shift the bit we're testing down to the lsb. |
| 576 | Result = Builder->CreateLShr( |
| 577 | Result, ConstantInt::get(ITy, UnknownBit.countTrailingZeros())); |
| 578 | |
| 579 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) |
| 580 | Result = Builder->CreateXor(Result, ConstantInt::get(ITy, 1)); |
| 581 | Result->takeName(ICI); |
| 582 | return ReplaceInstUsesWith(CI, Result); |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | return 0; |
| 589 | } |
| 590 | |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 591 | /// CanEvaluateZExtd - Determine if the specified value can be computed in the |
Chris Lattner | 8cf4f6f | 2010-01-11 02:43:35 +0000 | [diff] [blame] | 592 | /// specified wider type and produce the same low bits. If not, return false. |
| 593 | /// |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 594 | /// If this function returns true, it can also return a non-zero number of bits |
| 595 | /// (in BitsToClear) which indicates that the value it computes is correct for |
| 596 | /// the zero extend, but that the additional BitsToClear bits need to be zero'd |
| 597 | /// out. For example, to promote something like: |
| 598 | /// |
| 599 | /// %B = trunc i64 %A to i32 |
| 600 | /// %C = lshr i32 %B, 8 |
| 601 | /// %E = zext i32 %C to i64 |
| 602 | /// |
| 603 | /// CanEvaluateZExtd for the 'lshr' will return true, and BitsToClear will be |
| 604 | /// set to 8 to indicate that the promoted value needs to have bits 24-31 |
| 605 | /// cleared in addition to bits 32-63. Since an 'and' will be generated to |
| 606 | /// clear the top bits anyway, doing this has no extra cost. |
| 607 | /// |
Chris Lattner | 8cf4f6f | 2010-01-11 02:43:35 +0000 | [diff] [blame] | 608 | /// This function works on both vectors and scalars. |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 609 | static bool CanEvaluateZExtd(Value *V, const Type *Ty, unsigned &BitsToClear) { |
| 610 | BitsToClear = 0; |
Chris Lattner | 9e390dd | 2010-01-10 02:50:04 +0000 | [diff] [blame] | 611 | if (isa<Constant>(V)) |
| 612 | return true; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 613 | |
| 614 | Instruction *I = dyn_cast<Instruction>(V); |
Chris Lattner | 9e390dd | 2010-01-10 02:50:04 +0000 | [diff] [blame] | 615 | if (!I) return false; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 616 | |
| 617 | // 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] | 618 | // eliminate it, even if it has multiple uses. |
| 619 | // FIXME: This is currently disabled until codegen can handle this without |
| 620 | // pessimizing code, PR5997. |
| 621 | if (0 && isa<TruncInst>(I) && I->getOperand(0)->getType() == Ty) |
Chris Lattner | 9e390dd | 2010-01-10 02:50:04 +0000 | [diff] [blame] | 622 | return true; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 623 | |
| 624 | // We can't extend or shrink something that has multiple uses: doing so would |
| 625 | // require duplicating the instruction in general, which isn't profitable. |
Chris Lattner | 9e390dd | 2010-01-10 02:50:04 +0000 | [diff] [blame] | 626 | if (!I->hasOneUse()) return false; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 627 | |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 628 | unsigned Opc = I->getOpcode(), Tmp; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 629 | switch (Opc) { |
Chris Lattner | 9ee947c | 2010-01-10 20:25:54 +0000 | [diff] [blame] | 630 | case Instruction::ZExt: // zext(zext(x)) -> zext(x). |
| 631 | case Instruction::SExt: // zext(sext(x)) -> sext(x). |
| 632 | case Instruction::Trunc: // zext(trunc(x)) -> trunc(x) or zext(x) |
| 633 | return true; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 634 | case Instruction::And: |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 635 | case Instruction::Or: |
| 636 | case Instruction::Xor: |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 637 | case Instruction::Add: |
| 638 | case Instruction::Sub: |
| 639 | case Instruction::Mul: |
Chris Lattner | d26c9e1 | 2010-01-10 02:22:12 +0000 | [diff] [blame] | 640 | case Instruction::Shl: |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 641 | if (!CanEvaluateZExtd(I->getOperand(0), Ty, BitsToClear) || |
| 642 | !CanEvaluateZExtd(I->getOperand(1), Ty, Tmp)) |
| 643 | return false; |
| 644 | // These can all be promoted if neither operand has 'bits to clear'. |
| 645 | if (BitsToClear == 0 && Tmp == 0) |
| 646 | return true; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 647 | |
Chris Lattner | 7acc4b1 | 2010-01-11 04:05:13 +0000 | [diff] [blame] | 648 | // If the operation is an AND/OR/XOR and the bits to clear are zero in the |
| 649 | // other side, BitsToClear is ok. |
| 650 | if (Tmp == 0 && |
| 651 | (Opc == Instruction::And || Opc == Instruction::Or || |
| 652 | Opc == Instruction::Xor)) { |
| 653 | // We use MaskedValueIsZero here for generality, but the case we care |
| 654 | // about the most is constant RHS. |
| 655 | unsigned VSize = V->getType()->getScalarSizeInBits(); |
| 656 | if (MaskedValueIsZero(I->getOperand(1), |
| 657 | APInt::getHighBitsSet(VSize, BitsToClear))) |
| 658 | return true; |
| 659 | } |
| 660 | |
| 661 | // Otherwise, we don't know how to analyze this BitsToClear case yet. |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 662 | return false; |
Chris Lattner | d26c9e1 | 2010-01-10 02:22:12 +0000 | [diff] [blame] | 663 | |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 664 | case Instruction::LShr: |
| 665 | // We can promote lshr(x, cst) if we can promote x. This requires the |
| 666 | // ultimate 'and' to clear out the high zero bits we're clearing out though. |
| 667 | if (ConstantInt *Amt = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 668 | if (!CanEvaluateZExtd(I->getOperand(0), Ty, BitsToClear)) |
| 669 | return false; |
| 670 | BitsToClear += Amt->getZExtValue(); |
| 671 | if (BitsToClear > V->getType()->getScalarSizeInBits()) |
| 672 | BitsToClear = V->getType()->getScalarSizeInBits(); |
| 673 | return true; |
| 674 | } |
| 675 | // Cannot promote variable LSHR. |
| 676 | return false; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 677 | case Instruction::Select: |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 678 | if (!CanEvaluateZExtd(I->getOperand(1), Ty, Tmp) || |
| 679 | !CanEvaluateZExtd(I->getOperand(2), Ty, BitsToClear) || |
Chris Lattner | 7acc4b1 | 2010-01-11 04:05:13 +0000 | [diff] [blame] | 680 | // TODO: If important, we could handle the case when the BitsToClear are |
| 681 | // known zero in the disagreeing side. |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 682 | Tmp != BitsToClear) |
| 683 | return false; |
| 684 | return true; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 685 | |
| 686 | case Instruction::PHI: { |
| 687 | // We can change a phi if we can change all operands. Note that we never |
| 688 | // get into trouble with cyclic PHIs here because we only consider |
| 689 | // instructions with a single use. |
| 690 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 691 | if (!CanEvaluateZExtd(PN->getIncomingValue(0), Ty, BitsToClear)) |
| 692 | return false; |
Chris Lattner | 9e390dd | 2010-01-10 02:50:04 +0000 | [diff] [blame] | 693 | for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 694 | if (!CanEvaluateZExtd(PN->getIncomingValue(i), Ty, Tmp) || |
Chris Lattner | 7acc4b1 | 2010-01-11 04:05:13 +0000 | [diff] [blame] | 695 | // TODO: If important, we could handle the case when the BitsToClear |
| 696 | // are known zero in the disagreeing input. |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 697 | Tmp != BitsToClear) |
| 698 | return false; |
Chris Lattner | 9e390dd | 2010-01-10 02:50:04 +0000 | [diff] [blame] | 699 | return true; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 700 | } |
| 701 | default: |
| 702 | // TODO: Can handle more cases here. |
Chris Lattner | 9e390dd | 2010-01-10 02:50:04 +0000 | [diff] [blame] | 703 | return false; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 707 | Instruction *InstCombiner::visitZExt(ZExtInst &CI) { |
Chris Lattner | 5324d80 | 2010-01-10 02:39:31 +0000 | [diff] [blame] | 708 | // If this zero extend is only used by a truncate, let the truncate by |
| 709 | // eliminated before we try to optimize this zext. |
| 710 | if (CI.hasOneUse() && isa<TruncInst>(CI.use_back())) |
| 711 | return 0; |
| 712 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 713 | // If one of the common conversion will work, do it. |
Chris Lattner | d84dfa4 | 2010-01-10 01:00:46 +0000 | [diff] [blame] | 714 | if (Instruction *Result = commonCastTransforms(CI)) |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 715 | return Result; |
| 716 | |
Chris Lattner | d84dfa4 | 2010-01-10 01:00:46 +0000 | [diff] [blame] | 717 | // See if we can simplify any instructions used by the input whose sole |
| 718 | // purpose is to compute bits we don't care about. |
| 719 | if (SimplifyDemandedInstructionBits(CI)) |
| 720 | return &CI; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 721 | |
Chris Lattner | d84dfa4 | 2010-01-10 01:00:46 +0000 | [diff] [blame] | 722 | Value *Src = CI.getOperand(0); |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 723 | const Type *SrcTy = Src->getType(), *DestTy = CI.getType(); |
| 724 | |
| 725 | // Attempt to extend the entire input expression tree to the destination |
| 726 | // type. Only do this if the dest type is a simple type, don't convert the |
| 727 | // expression tree to something weird like i93 unless the source is also |
| 728 | // strange. |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 729 | unsigned BitsToClear; |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 730 | if ((DestTy->isVectorTy() || ShouldChangeType(SrcTy, DestTy)) && |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 731 | CanEvaluateZExtd(Src, DestTy, BitsToClear)) { |
| 732 | assert(BitsToClear < SrcTy->getScalarSizeInBits() && |
| 733 | "Unreasonable BitsToClear"); |
| 734 | |
Chris Lattner | 5324d80 | 2010-01-10 02:39:31 +0000 | [diff] [blame] | 735 | // Okay, we can transform this! Insert the new expression now. |
| 736 | DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type" |
| 737 | " to avoid zero extend: " << CI); |
| 738 | Value *Res = EvaluateInDifferentType(Src, DestTy, false); |
| 739 | assert(Res->getType() == DestTy); |
| 740 | |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 741 | uint32_t SrcBitsKept = SrcTy->getScalarSizeInBits()-BitsToClear; |
| 742 | uint32_t DestBitSize = DestTy->getScalarSizeInBits(); |
| 743 | |
Chris Lattner | 5324d80 | 2010-01-10 02:39:31 +0000 | [diff] [blame] | 744 | // If the high bits are already filled with zeros, just replace this |
| 745 | // cast with the result. |
Chris Lattner | 9e390dd | 2010-01-10 02:50:04 +0000 | [diff] [blame] | 746 | if (MaskedValueIsZero(Res, APInt::getHighBitsSet(DestBitSize, |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 747 | DestBitSize-SrcBitsKept))) |
Chris Lattner | 5324d80 | 2010-01-10 02:39:31 +0000 | [diff] [blame] | 748 | return ReplaceInstUsesWith(CI, Res); |
| 749 | |
| 750 | // We need to emit an AND to clear the high bits. |
Chris Lattner | 9ee947c | 2010-01-10 20:25:54 +0000 | [diff] [blame] | 751 | Constant *C = ConstantInt::get(Res->getType(), |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 752 | APInt::getLowBitsSet(DestBitSize, SrcBitsKept)); |
Chris Lattner | 5324d80 | 2010-01-10 02:39:31 +0000 | [diff] [blame] | 753 | return BinaryOperator::CreateAnd(Res, C); |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 754 | } |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 755 | |
| 756 | // If this is a TRUNC followed by a ZEXT then we are dealing with integral |
| 757 | // types and if the sizes are just right we can convert this into a logical |
| 758 | // 'and' which will be much cheaper than the pair of casts. |
| 759 | if (TruncInst *CSrc = dyn_cast<TruncInst>(Src)) { // A->B->C cast |
Chris Lattner | f4fb911 | 2010-01-10 07:08:30 +0000 | [diff] [blame] | 760 | // TODO: Subsume this into EvaluateInDifferentType. |
| 761 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 762 | // Get the sizes of the types involved. We know that the intermediate type |
| 763 | // will be smaller than A or C, but don't know the relation between A and C. |
| 764 | Value *A = CSrc->getOperand(0); |
| 765 | unsigned SrcSize = A->getType()->getScalarSizeInBits(); |
| 766 | unsigned MidSize = CSrc->getType()->getScalarSizeInBits(); |
| 767 | unsigned DstSize = CI.getType()->getScalarSizeInBits(); |
| 768 | // If we're actually extending zero bits, then if |
| 769 | // SrcSize < DstSize: zext(a & mask) |
| 770 | // SrcSize == DstSize: a & mask |
| 771 | // SrcSize > DstSize: trunc(a) & mask |
| 772 | if (SrcSize < DstSize) { |
| 773 | APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); |
| 774 | Constant *AndConst = ConstantInt::get(A->getType(), AndValue); |
| 775 | Value *And = Builder->CreateAnd(A, AndConst, CSrc->getName()+".mask"); |
| 776 | return new ZExtInst(And, CI.getType()); |
| 777 | } |
| 778 | |
| 779 | if (SrcSize == DstSize) { |
| 780 | APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); |
| 781 | return BinaryOperator::CreateAnd(A, ConstantInt::get(A->getType(), |
| 782 | AndValue)); |
| 783 | } |
| 784 | if (SrcSize > DstSize) { |
| 785 | Value *Trunc = Builder->CreateTrunc(A, CI.getType(), "tmp"); |
| 786 | APInt AndValue(APInt::getLowBitsSet(DstSize, MidSize)); |
| 787 | return BinaryOperator::CreateAnd(Trunc, |
| 788 | ConstantInt::get(Trunc->getType(), |
Chris Lattner | f4fb911 | 2010-01-10 07:08:30 +0000 | [diff] [blame] | 789 | AndValue)); |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 790 | } |
| 791 | } |
| 792 | |
| 793 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) |
| 794 | return transformZExtICmp(ICI, CI); |
| 795 | |
| 796 | BinaryOperator *SrcI = dyn_cast<BinaryOperator>(Src); |
| 797 | if (SrcI && SrcI->getOpcode() == Instruction::Or) { |
| 798 | // zext (or icmp, icmp) --> or (zext icmp), (zext icmp) if at least one |
| 799 | // of the (zext icmp) will be transformed. |
| 800 | ICmpInst *LHS = dyn_cast<ICmpInst>(SrcI->getOperand(0)); |
| 801 | ICmpInst *RHS = dyn_cast<ICmpInst>(SrcI->getOperand(1)); |
| 802 | if (LHS && RHS && LHS->hasOneUse() && RHS->hasOneUse() && |
| 803 | (transformZExtICmp(LHS, CI, false) || |
| 804 | transformZExtICmp(RHS, CI, false))) { |
| 805 | Value *LCast = Builder->CreateZExt(LHS, CI.getType(), LHS->getName()); |
| 806 | Value *RCast = Builder->CreateZExt(RHS, CI.getType(), RHS->getName()); |
| 807 | return BinaryOperator::Create(Instruction::Or, LCast, RCast); |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | // zext(trunc(t) & C) -> (t & zext(C)). |
| 812 | if (SrcI && SrcI->getOpcode() == Instruction::And && SrcI->hasOneUse()) |
| 813 | if (ConstantInt *C = dyn_cast<ConstantInt>(SrcI->getOperand(1))) |
| 814 | if (TruncInst *TI = dyn_cast<TruncInst>(SrcI->getOperand(0))) { |
| 815 | Value *TI0 = TI->getOperand(0); |
| 816 | if (TI0->getType() == CI.getType()) |
| 817 | return |
| 818 | BinaryOperator::CreateAnd(TI0, |
| 819 | ConstantExpr::getZExt(C, CI.getType())); |
| 820 | } |
| 821 | |
| 822 | // zext((trunc(t) & C) ^ C) -> ((t & zext(C)) ^ zext(C)). |
| 823 | if (SrcI && SrcI->getOpcode() == Instruction::Xor && SrcI->hasOneUse()) |
| 824 | if (ConstantInt *C = dyn_cast<ConstantInt>(SrcI->getOperand(1))) |
| 825 | if (BinaryOperator *And = dyn_cast<BinaryOperator>(SrcI->getOperand(0))) |
| 826 | if (And->getOpcode() == Instruction::And && And->hasOneUse() && |
| 827 | And->getOperand(1) == C) |
| 828 | if (TruncInst *TI = dyn_cast<TruncInst>(And->getOperand(0))) { |
| 829 | Value *TI0 = TI->getOperand(0); |
| 830 | if (TI0->getType() == CI.getType()) { |
| 831 | Constant *ZC = ConstantExpr::getZExt(C, CI.getType()); |
| 832 | Value *NewAnd = Builder->CreateAnd(TI0, ZC, "tmp"); |
| 833 | return BinaryOperator::CreateXor(NewAnd, ZC); |
| 834 | } |
| 835 | } |
| 836 | |
Chris Lattner | 718bf3f | 2010-01-05 21:04:47 +0000 | [diff] [blame] | 837 | // zext (xor i1 X, true) to i32 --> xor (zext i1 X to i32), 1 |
| 838 | Value *X; |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 839 | if (SrcI && SrcI->hasOneUse() && SrcI->getType()->isIntegerTy(1) && |
Chris Lattner | 49bdfef | 2010-01-05 21:11:17 +0000 | [diff] [blame] | 840 | match(SrcI, m_Not(m_Value(X))) && |
Chris Lattner | 718bf3f | 2010-01-05 21:04:47 +0000 | [diff] [blame] | 841 | (!X->hasOneUse() || !isa<CmpInst>(X))) { |
| 842 | Value *New = Builder->CreateZExt(X, CI.getType()); |
| 843 | return BinaryOperator::CreateXor(New, ConstantInt::get(CI.getType(), 1)); |
| 844 | } |
| 845 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 846 | return 0; |
| 847 | } |
| 848 | |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 849 | /// CanEvaluateSExtd - Return true if we can take the specified value |
| 850 | /// and return it as type Ty without inserting any new casts and without |
| 851 | /// changing the value of the common low bits. This is used by code that tries |
| 852 | /// to promote integer operations to a wider types will allow us to eliminate |
| 853 | /// the extension. |
| 854 | /// |
Chris Lattner | aa9c894 | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 855 | /// This function works on both vectors and scalars. |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 856 | /// |
Chris Lattner | 8cf4f6f | 2010-01-11 02:43:35 +0000 | [diff] [blame] | 857 | static bool CanEvaluateSExtd(Value *V, const Type *Ty) { |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 858 | assert(V->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits() && |
| 859 | "Can't sign extend type to a smaller type"); |
Chris Lattner | aa9c894 | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 860 | // If this is a constant, it can be trivially promoted. |
| 861 | if (isa<Constant>(V)) |
| 862 | return true; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 863 | |
| 864 | Instruction *I = dyn_cast<Instruction>(V); |
Chris Lattner | aa9c894 | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 865 | if (!I) return false; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 866 | |
Chris Lattner | a958cbf | 2010-01-11 22:45:25 +0000 | [diff] [blame] | 867 | // If this is a truncate from the dest type, we can trivially eliminate it, |
| 868 | // even if it has multiple uses. |
| 869 | // FIXME: This is currently disabled until codegen can handle this without |
| 870 | // pessimizing code, PR5997. |
| 871 | if (0 && isa<TruncInst>(I) && I->getOperand(0)->getType() == Ty) |
Chris Lattner | aa9c894 | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 872 | return true; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 873 | |
| 874 | // We can't extend or shrink something that has multiple uses: doing so would |
| 875 | // require duplicating the instruction in general, which isn't profitable. |
Chris Lattner | aa9c894 | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 876 | if (!I->hasOneUse()) return false; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 877 | |
Chris Lattner | aa9c894 | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 878 | switch (I->getOpcode()) { |
Chris Lattner | 11ea812 | 2010-01-10 20:30:41 +0000 | [diff] [blame] | 879 | case Instruction::SExt: // sext(sext(x)) -> sext(x) |
| 880 | case Instruction::ZExt: // sext(zext(x)) -> zext(x) |
| 881 | case Instruction::Trunc: // sext(trunc(x)) -> trunc(x) or sext(x) |
| 882 | return true; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 883 | case Instruction::And: |
| 884 | case Instruction::Or: |
| 885 | case Instruction::Xor: |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 886 | case Instruction::Add: |
| 887 | case Instruction::Sub: |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 888 | case Instruction::Mul: |
Chris Lattner | aa9c894 | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 889 | // These operators can all arbitrarily be extended if their inputs can. |
Chris Lattner | 8cf4f6f | 2010-01-11 02:43:35 +0000 | [diff] [blame] | 890 | return CanEvaluateSExtd(I->getOperand(0), Ty) && |
| 891 | CanEvaluateSExtd(I->getOperand(1), Ty); |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 892 | |
| 893 | //case Instruction::Shl: TODO |
| 894 | //case Instruction::LShr: TODO |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 895 | |
Chris Lattner | aa9c894 | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 896 | case Instruction::Select: |
Chris Lattner | 8cf4f6f | 2010-01-11 02:43:35 +0000 | [diff] [blame] | 897 | return CanEvaluateSExtd(I->getOperand(1), Ty) && |
| 898 | CanEvaluateSExtd(I->getOperand(2), Ty); |
Chris Lattner | 9ee947c | 2010-01-10 20:25:54 +0000 | [diff] [blame] | 899 | |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 900 | case Instruction::PHI: { |
| 901 | // We can change a phi if we can change all operands. Note that we never |
| 902 | // get into trouble with cyclic PHIs here because we only consider |
| 903 | // instructions with a single use. |
| 904 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | 9ee947c | 2010-01-10 20:25:54 +0000 | [diff] [blame] | 905 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
Chris Lattner | 8cf4f6f | 2010-01-11 02:43:35 +0000 | [diff] [blame] | 906 | if (!CanEvaluateSExtd(PN->getIncomingValue(i), Ty)) return false; |
Chris Lattner | aa9c894 | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 907 | return true; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 908 | } |
| 909 | default: |
| 910 | // TODO: Can handle more cases here. |
| 911 | break; |
| 912 | } |
| 913 | |
Chris Lattner | aa9c894 | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 914 | return false; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 917 | Instruction *InstCombiner::visitSExt(SExtInst &CI) { |
Chris Lattner | 5324d80 | 2010-01-10 02:39:31 +0000 | [diff] [blame] | 918 | // If this sign extend is only used by a truncate, let the truncate by |
| 919 | // eliminated before we try to optimize this zext. |
| 920 | if (CI.hasOneUse() && isa<TruncInst>(CI.use_back())) |
| 921 | return 0; |
| 922 | |
Chris Lattner | d84dfa4 | 2010-01-10 01:00:46 +0000 | [diff] [blame] | 923 | if (Instruction *I = commonCastTransforms(CI)) |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 924 | return I; |
| 925 | |
Chris Lattner | d84dfa4 | 2010-01-10 01:00:46 +0000 | [diff] [blame] | 926 | // See if we can simplify any instructions used by the input whose sole |
| 927 | // purpose is to compute bits we don't care about. |
| 928 | if (SimplifyDemandedInstructionBits(CI)) |
| 929 | return &CI; |
| 930 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 931 | Value *Src = CI.getOperand(0); |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 932 | const Type *SrcTy = Src->getType(), *DestTy = CI.getType(); |
| 933 | |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 934 | // Attempt to extend the entire input expression tree to the destination |
| 935 | // type. Only do this if the dest type is a simple type, don't convert the |
| 936 | // expression tree to something weird like i93 unless the source is also |
| 937 | // strange. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 938 | if ((DestTy->isVectorTy() || ShouldChangeType(SrcTy, DestTy)) && |
Chris Lattner | 8cf4f6f | 2010-01-11 02:43:35 +0000 | [diff] [blame] | 939 | CanEvaluateSExtd(Src, DestTy)) { |
Chris Lattner | dde5ee5 | 2010-01-10 07:40:50 +0000 | [diff] [blame] | 940 | // Okay, we can transform this! Insert the new expression now. |
| 941 | DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type" |
| 942 | " to avoid sign extend: " << CI); |
| 943 | Value *Res = EvaluateInDifferentType(Src, DestTy, true); |
| 944 | assert(Res->getType() == DestTy); |
| 945 | |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 946 | uint32_t SrcBitSize = SrcTy->getScalarSizeInBits(); |
| 947 | uint32_t DestBitSize = DestTy->getScalarSizeInBits(); |
Chris Lattner | dde5ee5 | 2010-01-10 07:40:50 +0000 | [diff] [blame] | 948 | |
| 949 | // If the high bits are already filled with sign bit, just replace this |
| 950 | // cast with the result. |
Chris Lattner | aa9c894 | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 951 | if (ComputeNumSignBits(Res) > DestBitSize - SrcBitSize) |
Chris Lattner | dde5ee5 | 2010-01-10 07:40:50 +0000 | [diff] [blame] | 952 | return ReplaceInstUsesWith(CI, Res); |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 953 | |
Chris Lattner | dde5ee5 | 2010-01-10 07:40:50 +0000 | [diff] [blame] | 954 | // We need to emit a shl + ashr to do the sign extend. |
| 955 | Value *ShAmt = ConstantInt::get(DestTy, DestBitSize-SrcBitSize); |
| 956 | return BinaryOperator::CreateAShr(Builder->CreateShl(Res, ShAmt, "sext"), |
| 957 | ShAmt); |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 958 | } |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 959 | |
Chris Lattner | cd5adbb | 2010-01-18 22:19:16 +0000 | [diff] [blame] | 960 | // If this input is a trunc from our destination, then turn sext(trunc(x)) |
| 961 | // into shifts. |
| 962 | if (TruncInst *TI = dyn_cast<TruncInst>(Src)) |
| 963 | if (TI->hasOneUse() && TI->getOperand(0)->getType() == DestTy) { |
| 964 | uint32_t SrcBitSize = SrcTy->getScalarSizeInBits(); |
| 965 | uint32_t DestBitSize = DestTy->getScalarSizeInBits(); |
| 966 | |
| 967 | // We need to emit a shl + ashr to do the sign extend. |
| 968 | Value *ShAmt = ConstantInt::get(DestTy, DestBitSize-SrcBitSize); |
| 969 | Value *Res = Builder->CreateShl(TI->getOperand(0), ShAmt, "sext"); |
| 970 | return BinaryOperator::CreateAShr(Res, ShAmt); |
| 971 | } |
| 972 | |
Chris Lattner | abb992d | 2010-01-24 00:09:49 +0000 | [diff] [blame] | 973 | |
| 974 | // (x <s 0) ? -1 : 0 -> ashr x, 31 -> all ones if signed |
| 975 | // (x >s -1) ? -1 : 0 -> ashr x, 31 -> all ones if not signed |
| 976 | { |
| 977 | ICmpInst::Predicate Pred; Value *CmpLHS; ConstantInt *CmpRHS; |
| 978 | if (match(Src, m_ICmp(Pred, m_Value(CmpLHS), m_ConstantInt(CmpRHS)))) { |
| 979 | // sext (x <s 0) to i32 --> x>>s31 true if signbit set. |
| 980 | // sext (x >s -1) to i32 --> (x>>s31)^-1 true if signbit clear. |
| 981 | if ((Pred == ICmpInst::ICMP_SLT && CmpRHS->isZero()) || |
| 982 | (Pred == ICmpInst::ICMP_SGT && CmpRHS->isAllOnesValue())) { |
| 983 | Value *Sh = ConstantInt::get(CmpLHS->getType(), |
| 984 | CmpLHS->getType()->getScalarSizeInBits()-1); |
| 985 | Value *In = Builder->CreateAShr(CmpLHS, Sh, CmpLHS->getName()+".lobit"); |
| 986 | if (In->getType() != CI.getType()) |
| 987 | In = Builder->CreateIntCast(In, CI.getType(), true/*SExt*/, "tmp"); |
| 988 | |
| 989 | if (Pred == ICmpInst::ICMP_SGT) |
| 990 | In = Builder->CreateNot(In, In->getName()+".not"); |
| 991 | return ReplaceInstUsesWith(CI, In); |
| 992 | } |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 997 | // If the input is a shl/ashr pair of a same constant, then this is a sign |
| 998 | // extension from a smaller value. If we could trust arbitrary bitwidth |
| 999 | // integers, we could turn this into a truncate to the smaller bit and then |
| 1000 | // use a sext for the whole extension. Since we don't, look deeper and check |
| 1001 | // for a truncate. If the source and dest are the same type, eliminate the |
| 1002 | // trunc and extend and just do shifts. For example, turn: |
| 1003 | // %a = trunc i32 %i to i8 |
| 1004 | // %b = shl i8 %a, 6 |
| 1005 | // %c = ashr i8 %b, 6 |
| 1006 | // %d = sext i8 %c to i32 |
| 1007 | // into: |
| 1008 | // %a = shl i32 %i, 30 |
| 1009 | // %d = ashr i32 %a, 30 |
| 1010 | Value *A = 0; |
Chris Lattner | 4f37978 | 2010-01-10 01:04:31 +0000 | [diff] [blame] | 1011 | // TODO: Eventually this could be subsumed by EvaluateInDifferentType. |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1012 | ConstantInt *BA = 0, *CA = 0; |
Chris Lattner | 4f37978 | 2010-01-10 01:04:31 +0000 | [diff] [blame] | 1013 | 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] | 1014 | m_ConstantInt(CA))) && |
Chris Lattner | 4f37978 | 2010-01-10 01:04:31 +0000 | [diff] [blame] | 1015 | BA == CA && A->getType() == CI.getType()) { |
| 1016 | unsigned MidSize = Src->getType()->getScalarSizeInBits(); |
| 1017 | unsigned SrcDstSize = CI.getType()->getScalarSizeInBits(); |
| 1018 | unsigned ShAmt = CA->getZExtValue()+SrcDstSize-MidSize; |
| 1019 | Constant *ShAmtV = ConstantInt::get(CI.getType(), ShAmt); |
| 1020 | A = Builder->CreateShl(A, ShAmtV, CI.getName()); |
| 1021 | return BinaryOperator::CreateAShr(A, ShAmtV); |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | return 0; |
| 1025 | } |
| 1026 | |
| 1027 | |
| 1028 | /// FitsInFPType - Return a Constant* for the specified FP constant if it fits |
| 1029 | /// in the specified FP type without changing its value. |
| 1030 | static Constant *FitsInFPType(ConstantFP *CFP, const fltSemantics &Sem) { |
| 1031 | bool losesInfo; |
| 1032 | APFloat F = CFP->getValueAPF(); |
| 1033 | (void)F.convert(Sem, APFloat::rmNearestTiesToEven, &losesInfo); |
| 1034 | if (!losesInfo) |
| 1035 | return ConstantFP::get(CFP->getContext(), F); |
| 1036 | return 0; |
| 1037 | } |
| 1038 | |
| 1039 | /// LookThroughFPExtensions - If this is an fp extension instruction, look |
| 1040 | /// through it until we get the source value. |
| 1041 | static Value *LookThroughFPExtensions(Value *V) { |
| 1042 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 1043 | if (I->getOpcode() == Instruction::FPExt) |
| 1044 | return LookThroughFPExtensions(I->getOperand(0)); |
| 1045 | |
| 1046 | // If this value is a constant, return the constant in the smallest FP type |
| 1047 | // that can accurately represent it. This allows us to turn |
| 1048 | // (float)((double)X+2.0) into x+2.0f. |
| 1049 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { |
| 1050 | if (CFP->getType() == Type::getPPC_FP128Ty(V->getContext())) |
| 1051 | return V; // No constant folding of this. |
| 1052 | // See if the value can be truncated to float and then reextended. |
| 1053 | if (Value *V = FitsInFPType(CFP, APFloat::IEEEsingle)) |
| 1054 | return V; |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 1055 | if (CFP->getType()->isDoubleTy()) |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1056 | return V; // Won't shrink. |
| 1057 | if (Value *V = FitsInFPType(CFP, APFloat::IEEEdouble)) |
| 1058 | return V; |
| 1059 | // Don't try to shrink to various long double types. |
| 1060 | } |
| 1061 | |
| 1062 | return V; |
| 1063 | } |
| 1064 | |
| 1065 | Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) { |
| 1066 | if (Instruction *I = commonCastTransforms(CI)) |
| 1067 | return I; |
| 1068 | |
| 1069 | // If we have fptrunc(fadd (fpextend x), (fpextend y)), where x and y are |
| 1070 | // smaller than the destination type, we can eliminate the truncate by doing |
| 1071 | // the add as the smaller type. This applies to fadd/fsub/fmul/fdiv as well |
| 1072 | // as many builtins (sqrt, etc). |
| 1073 | BinaryOperator *OpI = dyn_cast<BinaryOperator>(CI.getOperand(0)); |
| 1074 | if (OpI && OpI->hasOneUse()) { |
| 1075 | switch (OpI->getOpcode()) { |
| 1076 | default: break; |
| 1077 | case Instruction::FAdd: |
| 1078 | case Instruction::FSub: |
| 1079 | case Instruction::FMul: |
| 1080 | case Instruction::FDiv: |
| 1081 | case Instruction::FRem: |
| 1082 | const Type *SrcTy = OpI->getType(); |
| 1083 | Value *LHSTrunc = LookThroughFPExtensions(OpI->getOperand(0)); |
| 1084 | Value *RHSTrunc = LookThroughFPExtensions(OpI->getOperand(1)); |
| 1085 | if (LHSTrunc->getType() != SrcTy && |
| 1086 | RHSTrunc->getType() != SrcTy) { |
| 1087 | unsigned DstSize = CI.getType()->getScalarSizeInBits(); |
| 1088 | // If the source types were both smaller than the destination type of |
| 1089 | // the cast, do this xform. |
| 1090 | if (LHSTrunc->getType()->getScalarSizeInBits() <= DstSize && |
| 1091 | RHSTrunc->getType()->getScalarSizeInBits() <= DstSize) { |
| 1092 | LHSTrunc = Builder->CreateFPExt(LHSTrunc, CI.getType()); |
| 1093 | RHSTrunc = Builder->CreateFPExt(RHSTrunc, CI.getType()); |
| 1094 | return BinaryOperator::Create(OpI->getOpcode(), LHSTrunc, RHSTrunc); |
| 1095 | } |
| 1096 | } |
| 1097 | break; |
| 1098 | } |
| 1099 | } |
| 1100 | return 0; |
| 1101 | } |
| 1102 | |
| 1103 | Instruction *InstCombiner::visitFPExt(CastInst &CI) { |
| 1104 | return commonCastTransforms(CI); |
| 1105 | } |
| 1106 | |
| 1107 | Instruction *InstCombiner::visitFPToUI(FPToUIInst &FI) { |
| 1108 | Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0)); |
| 1109 | if (OpI == 0) |
| 1110 | return commonCastTransforms(FI); |
| 1111 | |
| 1112 | // fptoui(uitofp(X)) --> X |
| 1113 | // fptoui(sitofp(X)) --> X |
| 1114 | // This is safe if the intermediate type has enough bits in its mantissa to |
| 1115 | // accurately represent all values of X. For example, do not do this with |
| 1116 | // i64->float->i64. This is also safe for sitofp case, because any negative |
| 1117 | // 'X' value would cause an undefined result for the fptoui. |
| 1118 | if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) && |
| 1119 | OpI->getOperand(0)->getType() == FI.getType() && |
| 1120 | (int)FI.getType()->getScalarSizeInBits() < /*extra bit for sign */ |
| 1121 | OpI->getType()->getFPMantissaWidth()) |
| 1122 | return ReplaceInstUsesWith(FI, OpI->getOperand(0)); |
| 1123 | |
| 1124 | return commonCastTransforms(FI); |
| 1125 | } |
| 1126 | |
| 1127 | Instruction *InstCombiner::visitFPToSI(FPToSIInst &FI) { |
| 1128 | Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0)); |
| 1129 | if (OpI == 0) |
| 1130 | return commonCastTransforms(FI); |
| 1131 | |
| 1132 | // fptosi(sitofp(X)) --> X |
| 1133 | // fptosi(uitofp(X)) --> X |
| 1134 | // This is safe if the intermediate type has enough bits in its mantissa to |
| 1135 | // accurately represent all values of X. For example, do not do this with |
| 1136 | // i64->float->i64. This is also safe for sitofp case, because any negative |
| 1137 | // 'X' value would cause an undefined result for the fptoui. |
| 1138 | if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) && |
| 1139 | OpI->getOperand(0)->getType() == FI.getType() && |
| 1140 | (int)FI.getType()->getScalarSizeInBits() <= |
| 1141 | OpI->getType()->getFPMantissaWidth()) |
| 1142 | return ReplaceInstUsesWith(FI, OpI->getOperand(0)); |
| 1143 | |
| 1144 | return commonCastTransforms(FI); |
| 1145 | } |
| 1146 | |
| 1147 | Instruction *InstCombiner::visitUIToFP(CastInst &CI) { |
| 1148 | return commonCastTransforms(CI); |
| 1149 | } |
| 1150 | |
| 1151 | Instruction *InstCombiner::visitSIToFP(CastInst &CI) { |
| 1152 | return commonCastTransforms(CI); |
| 1153 | } |
| 1154 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1155 | Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) { |
Dan Gohman | 3b5487e | 2010-02-02 01:44:02 +0000 | [diff] [blame] | 1156 | // If the source integer type is not the intptr_t type for this target, do a |
| 1157 | // trunc or zext to the intptr_t type, then inttoptr of it. This allows the |
| 1158 | // cast to be exposed to other transforms. |
| 1159 | if (TD) { |
| 1160 | if (CI.getOperand(0)->getType()->getScalarSizeInBits() > |
| 1161 | TD->getPointerSizeInBits()) { |
| 1162 | Value *P = Builder->CreateTrunc(CI.getOperand(0), |
| 1163 | TD->getIntPtrType(CI.getContext()), "tmp"); |
| 1164 | return new IntToPtrInst(P, CI.getType()); |
| 1165 | } |
| 1166 | if (CI.getOperand(0)->getType()->getScalarSizeInBits() < |
| 1167 | TD->getPointerSizeInBits()) { |
| 1168 | Value *P = Builder->CreateZExt(CI.getOperand(0), |
| 1169 | TD->getIntPtrType(CI.getContext()), "tmp"); |
| 1170 | return new IntToPtrInst(P, CI.getType()); |
| 1171 | } |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | if (Instruction *I = commonCastTransforms(CI)) |
| 1175 | return I; |
| 1176 | |
| 1177 | return 0; |
| 1178 | } |
| 1179 | |
Chris Lattner | 7a34d6c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1180 | /// @brief Implement the transforms for cast of pointer (bitcast/ptrtoint) |
| 1181 | Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) { |
| 1182 | Value *Src = CI.getOperand(0); |
| 1183 | |
| 1184 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
| 1185 | // If casting the result of a getelementptr instruction with no offset, turn |
| 1186 | // this into a cast of the original pointer! |
| 1187 | if (GEP->hasAllZeroIndices()) { |
| 1188 | // Changing the cast operand is usually not a good idea but it is safe |
| 1189 | // here because the pointer operand is being replaced with another |
| 1190 | // pointer operand so the opcode doesn't need to change. |
| 1191 | Worklist.Add(GEP); |
| 1192 | CI.setOperand(0, GEP->getOperand(0)); |
| 1193 | return &CI; |
| 1194 | } |
| 1195 | |
| 1196 | // If the GEP has a single use, and the base pointer is a bitcast, and the |
| 1197 | // GEP computes a constant offset, see if we can convert these three |
| 1198 | // instructions into fewer. This typically happens with unions and other |
| 1199 | // non-type-safe code. |
| 1200 | if (TD && GEP->hasOneUse() && isa<BitCastInst>(GEP->getOperand(0)) && |
| 1201 | GEP->hasAllConstantIndices()) { |
| 1202 | // We are guaranteed to get a constant from EmitGEPOffset. |
| 1203 | ConstantInt *OffsetV = cast<ConstantInt>(EmitGEPOffset(GEP)); |
| 1204 | int64_t Offset = OffsetV->getSExtValue(); |
| 1205 | |
| 1206 | // Get the base pointer input of the bitcast, and the type it points to. |
| 1207 | Value *OrigBase = cast<BitCastInst>(GEP->getOperand(0))->getOperand(0); |
| 1208 | const Type *GEPIdxTy = |
| 1209 | cast<PointerType>(OrigBase->getType())->getElementType(); |
| 1210 | SmallVector<Value*, 8> NewIndices; |
| 1211 | if (FindElementAtOffset(GEPIdxTy, Offset, NewIndices)) { |
| 1212 | // If we were able to index down into an element, create the GEP |
| 1213 | // and bitcast the result. This eliminates one bitcast, potentially |
| 1214 | // two. |
| 1215 | Value *NGEP = cast<GEPOperator>(GEP)->isInBounds() ? |
| 1216 | Builder->CreateInBoundsGEP(OrigBase, |
| 1217 | NewIndices.begin(), NewIndices.end()) : |
| 1218 | Builder->CreateGEP(OrigBase, NewIndices.begin(), NewIndices.end()); |
| 1219 | NGEP->takeName(GEP); |
| 1220 | |
| 1221 | if (isa<BitCastInst>(CI)) |
| 1222 | return new BitCastInst(NGEP, CI.getType()); |
| 1223 | assert(isa<PtrToIntInst>(CI)); |
| 1224 | return new PtrToIntInst(NGEP, CI.getType()); |
| 1225 | } |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | return commonCastTransforms(CI); |
| 1230 | } |
| 1231 | |
| 1232 | Instruction *InstCombiner::visitPtrToInt(PtrToIntInst &CI) { |
Dan Gohman | 3b5487e | 2010-02-02 01:44:02 +0000 | [diff] [blame] | 1233 | // If the destination integer type is not the intptr_t type for this target, |
| 1234 | // do a ptrtoint to intptr_t then do a trunc or zext. This allows the cast |
| 1235 | // to be exposed to other transforms. |
| 1236 | if (TD) { |
| 1237 | if (CI.getType()->getScalarSizeInBits() < TD->getPointerSizeInBits()) { |
| 1238 | Value *P = Builder->CreatePtrToInt(CI.getOperand(0), |
| 1239 | TD->getIntPtrType(CI.getContext()), |
| 1240 | "tmp"); |
| 1241 | return new TruncInst(P, CI.getType()); |
| 1242 | } |
| 1243 | if (CI.getType()->getScalarSizeInBits() > TD->getPointerSizeInBits()) { |
| 1244 | Value *P = Builder->CreatePtrToInt(CI.getOperand(0), |
| 1245 | TD->getIntPtrType(CI.getContext()), |
| 1246 | "tmp"); |
| 1247 | return new ZExtInst(P, CI.getType()); |
| 1248 | } |
Chris Lattner | 7a34d6c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | return commonPointerCastTransforms(CI); |
| 1252 | } |
| 1253 | |
Chris Lattner | 6745191 | 2010-05-08 21:50:26 +0000 | [diff] [blame] | 1254 | /// OptimizeVectorResize - This input value (which is known to have vector type) |
| 1255 | /// is being zero extended or truncated to the specified vector type. Try to |
| 1256 | /// replace it with a shuffle (and vector/vector bitcast) if possible. |
| 1257 | /// |
| 1258 | /// The source and destination vector types may have different element types. |
| 1259 | static Instruction *OptimizeVectorResize(Value *InVal, const VectorType *DestTy, |
| 1260 | InstCombiner &IC) { |
| 1261 | // We can only do this optimization if the output is a multiple of the input |
| 1262 | // element size, or the input is a multiple of the output element size. |
| 1263 | // Convert the input type to have the same element type as the output. |
| 1264 | const VectorType *SrcTy = cast<VectorType>(InVal->getType()); |
| 1265 | |
| 1266 | if (SrcTy->getElementType() != DestTy->getElementType()) { |
| 1267 | // The input types don't need to be identical, but for now they must be the |
| 1268 | // same size. There is no specific reason we couldn't handle things like |
| 1269 | // <4 x i16> -> <4 x i32> by bitcasting to <2 x i32> but haven't gotten |
| 1270 | // there yet. |
| 1271 | if (SrcTy->getElementType()->getPrimitiveSizeInBits() != |
| 1272 | DestTy->getElementType()->getPrimitiveSizeInBits()) |
| 1273 | return 0; |
| 1274 | |
| 1275 | SrcTy = VectorType::get(DestTy->getElementType(), SrcTy->getNumElements()); |
| 1276 | InVal = IC.Builder->CreateBitCast(InVal, SrcTy); |
| 1277 | } |
| 1278 | |
| 1279 | // Now that the element types match, get the shuffle mask and RHS of the |
| 1280 | // shuffle to use, which depends on whether we're increasing or decreasing the |
| 1281 | // size of the input. |
| 1282 | SmallVector<Constant*, 16> ShuffleMask; |
| 1283 | Value *V2; |
| 1284 | const IntegerType *Int32Ty = Type::getInt32Ty(SrcTy->getContext()); |
| 1285 | |
| 1286 | if (SrcTy->getNumElements() > DestTy->getNumElements()) { |
| 1287 | // If we're shrinking the number of elements, just shuffle in the low |
| 1288 | // elements from the input and use undef as the second shuffle input. |
| 1289 | V2 = UndefValue::get(SrcTy); |
| 1290 | for (unsigned i = 0, e = DestTy->getNumElements(); i != e; ++i) |
| 1291 | ShuffleMask.push_back(ConstantInt::get(Int32Ty, i)); |
| 1292 | |
| 1293 | } else { |
| 1294 | // If we're increasing the number of elements, shuffle in all of the |
| 1295 | // elements from InVal and fill the rest of the result elements with zeros |
| 1296 | // from a constant zero. |
| 1297 | V2 = Constant::getNullValue(SrcTy); |
| 1298 | unsigned SrcElts = SrcTy->getNumElements(); |
| 1299 | for (unsigned i = 0, e = SrcElts; i != e; ++i) |
| 1300 | ShuffleMask.push_back(ConstantInt::get(Int32Ty, i)); |
| 1301 | |
| 1302 | // The excess elements reference the first element of the zero input. |
| 1303 | ShuffleMask.append(DestTy->getNumElements()-SrcElts, |
| 1304 | ConstantInt::get(Int32Ty, SrcElts)); |
| 1305 | } |
| 1306 | |
| 1307 | Constant *Mask = ConstantVector::get(ShuffleMask.data(), ShuffleMask.size()); |
| 1308 | return new ShuffleVectorInst(InVal, V2, Mask); |
| 1309 | } |
| 1310 | |
| 1311 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1312 | Instruction *InstCombiner::visitBitCast(BitCastInst &CI) { |
| 1313 | // If the operands are integer typed then apply the integer transforms, |
| 1314 | // otherwise just apply the common ones. |
| 1315 | Value *Src = CI.getOperand(0); |
| 1316 | const Type *SrcTy = Src->getType(); |
| 1317 | const Type *DestTy = CI.getType(); |
| 1318 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1319 | // Get rid of casts from one type to the same type. These are useless and can |
| 1320 | // be replaced by the operand. |
| 1321 | if (DestTy == Src->getType()) |
| 1322 | return ReplaceInstUsesWith(CI, Src); |
| 1323 | |
| 1324 | if (const PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) { |
| 1325 | const PointerType *SrcPTy = cast<PointerType>(SrcTy); |
| 1326 | const Type *DstElTy = DstPTy->getElementType(); |
| 1327 | const Type *SrcElTy = SrcPTy->getElementType(); |
| 1328 | |
| 1329 | // If the address spaces don't match, don't eliminate the bitcast, which is |
| 1330 | // required for changing types. |
| 1331 | if (SrcPTy->getAddressSpace() != DstPTy->getAddressSpace()) |
| 1332 | return 0; |
| 1333 | |
| 1334 | // If we are casting a alloca to a pointer to a type of the same |
| 1335 | // size, rewrite the allocation instruction to allocate the "right" type. |
| 1336 | // There is no need to modify malloc calls because it is their bitcast that |
| 1337 | // needs to be cleaned up. |
| 1338 | if (AllocaInst *AI = dyn_cast<AllocaInst>(Src)) |
| 1339 | if (Instruction *V = PromoteCastOfAllocation(CI, *AI)) |
| 1340 | return V; |
| 1341 | |
| 1342 | // If the source and destination are pointers, and this cast is equivalent |
| 1343 | // to a getelementptr X, 0, 0, 0... turn it into the appropriate gep. |
| 1344 | // This can enhance SROA and other transforms that want type-safe pointers. |
| 1345 | Constant *ZeroUInt = |
| 1346 | Constant::getNullValue(Type::getInt32Ty(CI.getContext())); |
| 1347 | unsigned NumZeros = 0; |
| 1348 | while (SrcElTy != DstElTy && |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1349 | isa<CompositeType>(SrcElTy) && !SrcElTy->isPointerTy() && |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1350 | SrcElTy->getNumContainedTypes() /* not "{}" */) { |
| 1351 | SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt); |
| 1352 | ++NumZeros; |
| 1353 | } |
| 1354 | |
| 1355 | // If we found a path from the src to dest, create the getelementptr now. |
| 1356 | if (SrcElTy == DstElTy) { |
| 1357 | SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt); |
| 1358 | return GetElementPtrInst::CreateInBounds(Src, Idxs.begin(), Idxs.end(),"", |
Chris Lattner | 7a34d6c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1359 | ((Instruction*)NULL)); |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | if (const VectorType *DestVTy = dyn_cast<VectorType>(DestTy)) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1364 | if (DestVTy->getNumElements() == 1 && !SrcTy->isVectorTy()) { |
Chris Lattner | 7a34d6c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1365 | Value *Elem = Builder->CreateBitCast(Src, DestVTy->getElementType()); |
| 1366 | return InsertElementInst::Create(UndefValue::get(DestTy), Elem, |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1367 | Constant::getNullValue(Type::getInt32Ty(CI.getContext()))); |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1368 | // FIXME: Canonicalize bitcast(insertelement) -> insertelement(bitcast) |
| 1369 | } |
Chris Lattner | 6745191 | 2010-05-08 21:50:26 +0000 | [diff] [blame] | 1370 | |
| 1371 | // If this is a cast from an integer to vector, check to see if the input |
| 1372 | // is a trunc or zext of a bitcast from vector. If so, we can replace all |
| 1373 | // the casts with a shuffle and (potentially) a bitcast. |
| 1374 | if (isa<IntegerType>(SrcTy) && (isa<TruncInst>(Src) || isa<ZExtInst>(Src))){ |
| 1375 | CastInst *SrcCast = cast<CastInst>(Src); |
| 1376 | if (BitCastInst *BCIn = dyn_cast<BitCastInst>(SrcCast->getOperand(0))) |
| 1377 | if (isa<VectorType>(BCIn->getOperand(0)->getType())) |
| 1378 | if (Instruction *I = OptimizeVectorResize(BCIn->getOperand(0), |
| 1379 | cast<VectorType>(DestTy), *this)) |
| 1380 | return I; |
| 1381 | } |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | if (const VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy)) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1385 | if (SrcVTy->getNumElements() == 1 && !DestTy->isVectorTy()) { |
Chris Lattner | 7a34d6c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1386 | Value *Elem = |
| 1387 | Builder->CreateExtractElement(Src, |
| 1388 | Constant::getNullValue(Type::getInt32Ty(CI.getContext()))); |
| 1389 | return CastInst::Create(Instruction::BitCast, Elem, DestTy); |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) { |
Chris Lattner | 7a34d6c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1394 | // Okay, we have (bitcast (shuffle ..)). Check to see if this is |
Dan Gohman | a5ced59 | 2010-04-07 23:22:42 +0000 | [diff] [blame] | 1395 | // a bitcast to a vector with the same # elts. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1396 | if (SVI->hasOneUse() && DestTy->isVectorTy() && |
Chris Lattner | 7a34d6c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1397 | cast<VectorType>(DestTy)->getNumElements() == |
| 1398 | SVI->getType()->getNumElements() && |
| 1399 | SVI->getType()->getNumElements() == |
| 1400 | cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements()) { |
| 1401 | BitCastInst *Tmp; |
| 1402 | // If either of the operands is a cast from CI.getType(), then |
| 1403 | // evaluating the shuffle in the casted destination's type will allow |
| 1404 | // us to eliminate at least one cast. |
| 1405 | if (((Tmp = dyn_cast<BitCastInst>(SVI->getOperand(0))) && |
| 1406 | Tmp->getOperand(0)->getType() == DestTy) || |
| 1407 | ((Tmp = dyn_cast<BitCastInst>(SVI->getOperand(1))) && |
| 1408 | Tmp->getOperand(0)->getType() == DestTy)) { |
| 1409 | Value *LHS = Builder->CreateBitCast(SVI->getOperand(0), DestTy); |
| 1410 | Value *RHS = Builder->CreateBitCast(SVI->getOperand(1), DestTy); |
| 1411 | // Return a new shuffle vector. Use the same element ID's, as we |
| 1412 | // know the vector types match #elts. |
| 1413 | return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2)); |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1414 | } |
| 1415 | } |
| 1416 | } |
Chris Lattner | 7a34d6c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1417 | |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1418 | if (SrcTy->isPointerTy()) |
Chris Lattner | 7a34d6c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1419 | return commonPointerCastTransforms(CI); |
| 1420 | return commonCastTransforms(CI); |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1421 | } |