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; |
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); |
| 437 | const Type *DestTy = CI.getType(), *SrcTy = Src->getType(); |
| 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); |
| 458 | Src = Builder->CreateAnd(Src, One, "tmp"); |
| 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; |
| 465 | if (match(Src, m_LShr(m_ZExt(m_Value(A)), m_ConstantInt(Cst))) && |
| 466 | Src->hasOneUse()) { |
| 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 | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 485 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | /// transformZExtICmp - Transform (zext icmp) to bitwise / integer operations |
| 490 | /// in order to eliminate the icmp. |
| 491 | Instruction *InstCombiner::transformZExtICmp(ICmpInst *ICI, Instruction &CI, |
| 492 | bool DoXform) { |
| 493 | // If we are just checking for a icmp eq of a single bit and zext'ing it |
| 494 | // to an integer, then shift the bit to the appropriate place and then |
| 495 | // cast to integer to avoid the comparison. |
| 496 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) { |
| 497 | const APInt &Op1CV = Op1C->getValue(); |
| 498 | |
| 499 | // zext (x <s 0) to i32 --> x>>u31 true if signbit set. |
| 500 | // zext (x >s -1) to i32 --> (x>>u31)^1 true if signbit clear. |
| 501 | if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) || |
| 502 | (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())) { |
| 503 | if (!DoXform) return ICI; |
| 504 | |
| 505 | Value *In = ICI->getOperand(0); |
| 506 | Value *Sh = ConstantInt::get(In->getType(), |
| 507 | In->getType()->getScalarSizeInBits()-1); |
| 508 | In = Builder->CreateLShr(In, Sh, In->getName()+".lobit"); |
| 509 | if (In->getType() != CI.getType()) |
| 510 | In = Builder->CreateIntCast(In, CI.getType(), false/*ZExt*/, "tmp"); |
| 511 | |
| 512 | if (ICI->getPredicate() == ICmpInst::ICMP_SGT) { |
| 513 | Constant *One = ConstantInt::get(In->getType(), 1); |
| 514 | In = Builder->CreateXor(In, One, In->getName()+".not"); |
| 515 | } |
| 516 | |
| 517 | return ReplaceInstUsesWith(CI, In); |
| 518 | } |
| 519 | |
| 520 | |
| 521 | |
| 522 | // zext (X == 0) to i32 --> X^1 iff X has only the low bit set. |
| 523 | // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. |
| 524 | // zext (X == 1) to i32 --> X iff X has only the low bit set. |
| 525 | // zext (X == 2) to i32 --> X>>1 iff X has only the 2nd bit set. |
| 526 | // zext (X != 0) to i32 --> X iff X has only the low bit set. |
| 527 | // zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set. |
| 528 | // zext (X != 1) to i32 --> X^1 iff X has only the low bit set. |
| 529 | // zext (X != 2) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. |
| 530 | if ((Op1CV == 0 || Op1CV.isPowerOf2()) && |
| 531 | // This only works for EQ and NE |
| 532 | ICI->isEquality()) { |
| 533 | // If Op1C some other power of two, convert: |
| 534 | uint32_t BitWidth = Op1C->getType()->getBitWidth(); |
| 535 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 536 | APInt TypeMask(APInt::getAllOnesValue(BitWidth)); |
| 537 | ComputeMaskedBits(ICI->getOperand(0), TypeMask, KnownZero, KnownOne); |
| 538 | |
| 539 | APInt KnownZeroMask(~KnownZero); |
| 540 | if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1? |
| 541 | if (!DoXform) return ICI; |
| 542 | |
| 543 | bool isNE = ICI->getPredicate() == ICmpInst::ICMP_NE; |
| 544 | if (Op1CV != 0 && (Op1CV != KnownZeroMask)) { |
| 545 | // (X&4) == 2 --> false |
| 546 | // (X&4) != 2 --> true |
| 547 | Constant *Res = ConstantInt::get(Type::getInt1Ty(CI.getContext()), |
| 548 | isNE); |
| 549 | Res = ConstantExpr::getZExt(Res, CI.getType()); |
| 550 | return ReplaceInstUsesWith(CI, Res); |
| 551 | } |
| 552 | |
| 553 | uint32_t ShiftAmt = KnownZeroMask.logBase2(); |
| 554 | Value *In = ICI->getOperand(0); |
| 555 | if (ShiftAmt) { |
| 556 | // Perform a logical shr by shiftamt. |
| 557 | // Insert the shift to put the result in the low bit. |
| 558 | In = Builder->CreateLShr(In, ConstantInt::get(In->getType(),ShiftAmt), |
| 559 | In->getName()+".lobit"); |
| 560 | } |
| 561 | |
| 562 | if ((Op1CV != 0) == isNE) { // Toggle the low bit. |
| 563 | Constant *One = ConstantInt::get(In->getType(), 1); |
| 564 | In = Builder->CreateXor(In, One, "tmp"); |
| 565 | } |
| 566 | |
| 567 | if (CI.getType() == In->getType()) |
| 568 | return ReplaceInstUsesWith(CI, In); |
| 569 | else |
| 570 | return CastInst::CreateIntegerCast(In, CI.getType(), false/*ZExt*/); |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | // icmp ne A, B is equal to xor A, B when A and B only really have one bit. |
| 576 | // It is also profitable to transform icmp eq into not(xor(A, B)) because that |
| 577 | // may lead to additional simplifications. |
| 578 | if (ICI->isEquality() && CI.getType() == ICI->getOperand(0)->getType()) { |
| 579 | if (const IntegerType *ITy = dyn_cast<IntegerType>(CI.getType())) { |
| 580 | uint32_t BitWidth = ITy->getBitWidth(); |
| 581 | Value *LHS = ICI->getOperand(0); |
| 582 | Value *RHS = ICI->getOperand(1); |
| 583 | |
| 584 | APInt KnownZeroLHS(BitWidth, 0), KnownOneLHS(BitWidth, 0); |
| 585 | APInt KnownZeroRHS(BitWidth, 0), KnownOneRHS(BitWidth, 0); |
| 586 | APInt TypeMask(APInt::getAllOnesValue(BitWidth)); |
| 587 | ComputeMaskedBits(LHS, TypeMask, KnownZeroLHS, KnownOneLHS); |
| 588 | ComputeMaskedBits(RHS, TypeMask, KnownZeroRHS, KnownOneRHS); |
| 589 | |
| 590 | if (KnownZeroLHS == KnownZeroRHS && KnownOneLHS == KnownOneRHS) { |
| 591 | APInt KnownBits = KnownZeroLHS | KnownOneLHS; |
| 592 | APInt UnknownBit = ~KnownBits; |
| 593 | if (UnknownBit.countPopulation() == 1) { |
| 594 | if (!DoXform) return ICI; |
| 595 | |
| 596 | Value *Result = Builder->CreateXor(LHS, RHS); |
| 597 | |
| 598 | // Mask off any bits that are set and won't be shifted away. |
| 599 | if (KnownOneLHS.uge(UnknownBit)) |
| 600 | Result = Builder->CreateAnd(Result, |
| 601 | ConstantInt::get(ITy, UnknownBit)); |
| 602 | |
| 603 | // Shift the bit we're testing down to the lsb. |
| 604 | Result = Builder->CreateLShr( |
| 605 | Result, ConstantInt::get(ITy, UnknownBit.countTrailingZeros())); |
| 606 | |
| 607 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) |
| 608 | Result = Builder->CreateXor(Result, ConstantInt::get(ITy, 1)); |
| 609 | Result->takeName(ICI); |
| 610 | return ReplaceInstUsesWith(CI, Result); |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 619 | /// CanEvaluateZExtd - Determine if the specified value can be computed in the |
Chris Lattner | 8cf4f6f | 2010-01-11 02:43:35 +0000 | [diff] [blame] | 620 | /// specified wider type and produce the same low bits. If not, return false. |
| 621 | /// |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 622 | /// If this function returns true, it can also return a non-zero number of bits |
| 623 | /// (in BitsToClear) which indicates that the value it computes is correct for |
| 624 | /// the zero extend, but that the additional BitsToClear bits need to be zero'd |
| 625 | /// out. For example, to promote something like: |
| 626 | /// |
| 627 | /// %B = trunc i64 %A to i32 |
| 628 | /// %C = lshr i32 %B, 8 |
| 629 | /// %E = zext i32 %C to i64 |
| 630 | /// |
| 631 | /// CanEvaluateZExtd for the 'lshr' will return true, and BitsToClear will be |
| 632 | /// set to 8 to indicate that the promoted value needs to have bits 24-31 |
| 633 | /// cleared in addition to bits 32-63. Since an 'and' will be generated to |
| 634 | /// clear the top bits anyway, doing this has no extra cost. |
| 635 | /// |
Chris Lattner | 8cf4f6f | 2010-01-11 02:43:35 +0000 | [diff] [blame] | 636 | /// This function works on both vectors and scalars. |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 637 | static bool CanEvaluateZExtd(Value *V, const Type *Ty, unsigned &BitsToClear) { |
| 638 | BitsToClear = 0; |
Chris Lattner | 9e390dd | 2010-01-10 02:50:04 +0000 | [diff] [blame] | 639 | if (isa<Constant>(V)) |
| 640 | return true; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 641 | |
| 642 | Instruction *I = dyn_cast<Instruction>(V); |
Chris Lattner | 9e390dd | 2010-01-10 02:50:04 +0000 | [diff] [blame] | 643 | if (!I) return false; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 644 | |
| 645 | // 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] | 646 | // eliminate it, even if it has multiple uses. |
| 647 | // FIXME: This is currently disabled until codegen can handle this without |
| 648 | // pessimizing code, PR5997. |
| 649 | if (0 && isa<TruncInst>(I) && I->getOperand(0)->getType() == Ty) |
Chris Lattner | 9e390dd | 2010-01-10 02:50:04 +0000 | [diff] [blame] | 650 | return true; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 651 | |
| 652 | // We can't extend or shrink something that has multiple uses: doing so would |
| 653 | // require duplicating the instruction in general, which isn't profitable. |
Chris Lattner | 9e390dd | 2010-01-10 02:50:04 +0000 | [diff] [blame] | 654 | if (!I->hasOneUse()) return false; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 655 | |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 656 | unsigned Opc = I->getOpcode(), Tmp; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 657 | switch (Opc) { |
Chris Lattner | 9ee947c | 2010-01-10 20:25:54 +0000 | [diff] [blame] | 658 | case Instruction::ZExt: // zext(zext(x)) -> zext(x). |
| 659 | case Instruction::SExt: // zext(sext(x)) -> sext(x). |
| 660 | case Instruction::Trunc: // zext(trunc(x)) -> trunc(x) or zext(x) |
| 661 | return true; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 662 | case Instruction::And: |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 663 | case Instruction::Or: |
| 664 | case Instruction::Xor: |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 665 | case Instruction::Add: |
| 666 | case Instruction::Sub: |
| 667 | case Instruction::Mul: |
Chris Lattner | d26c9e1 | 2010-01-10 02:22:12 +0000 | [diff] [blame] | 668 | case Instruction::Shl: |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 669 | if (!CanEvaluateZExtd(I->getOperand(0), Ty, BitsToClear) || |
| 670 | !CanEvaluateZExtd(I->getOperand(1), Ty, Tmp)) |
| 671 | return false; |
| 672 | // These can all be promoted if neither operand has 'bits to clear'. |
| 673 | if (BitsToClear == 0 && Tmp == 0) |
| 674 | return true; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 675 | |
Chris Lattner | 7acc4b1 | 2010-01-11 04:05:13 +0000 | [diff] [blame] | 676 | // If the operation is an AND/OR/XOR and the bits to clear are zero in the |
| 677 | // other side, BitsToClear is ok. |
| 678 | if (Tmp == 0 && |
| 679 | (Opc == Instruction::And || Opc == Instruction::Or || |
| 680 | Opc == Instruction::Xor)) { |
| 681 | // We use MaskedValueIsZero here for generality, but the case we care |
| 682 | // about the most is constant RHS. |
| 683 | unsigned VSize = V->getType()->getScalarSizeInBits(); |
| 684 | if (MaskedValueIsZero(I->getOperand(1), |
| 685 | APInt::getHighBitsSet(VSize, BitsToClear))) |
| 686 | return true; |
| 687 | } |
| 688 | |
| 689 | // Otherwise, we don't know how to analyze this BitsToClear case yet. |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 690 | return false; |
Chris Lattner | d26c9e1 | 2010-01-10 02:22:12 +0000 | [diff] [blame] | 691 | |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 692 | case Instruction::LShr: |
| 693 | // We can promote lshr(x, cst) if we can promote x. This requires the |
| 694 | // ultimate 'and' to clear out the high zero bits we're clearing out though. |
| 695 | if (ConstantInt *Amt = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 696 | if (!CanEvaluateZExtd(I->getOperand(0), Ty, BitsToClear)) |
| 697 | return false; |
| 698 | BitsToClear += Amt->getZExtValue(); |
| 699 | if (BitsToClear > V->getType()->getScalarSizeInBits()) |
| 700 | BitsToClear = V->getType()->getScalarSizeInBits(); |
| 701 | return true; |
| 702 | } |
| 703 | // Cannot promote variable LSHR. |
| 704 | return false; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 705 | case Instruction::Select: |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 706 | if (!CanEvaluateZExtd(I->getOperand(1), Ty, Tmp) || |
| 707 | !CanEvaluateZExtd(I->getOperand(2), Ty, BitsToClear) || |
Chris Lattner | 7acc4b1 | 2010-01-11 04:05:13 +0000 | [diff] [blame] | 708 | // TODO: If important, we could handle the case when the BitsToClear are |
| 709 | // known zero in the disagreeing side. |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 710 | Tmp != BitsToClear) |
| 711 | return false; |
| 712 | return true; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 713 | |
| 714 | case Instruction::PHI: { |
| 715 | // We can change a phi if we can change all operands. Note that we never |
| 716 | // get into trouble with cyclic PHIs here because we only consider |
| 717 | // instructions with a single use. |
| 718 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 719 | if (!CanEvaluateZExtd(PN->getIncomingValue(0), Ty, BitsToClear)) |
| 720 | return false; |
Chris Lattner | 9e390dd | 2010-01-10 02:50:04 +0000 | [diff] [blame] | 721 | for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 722 | if (!CanEvaluateZExtd(PN->getIncomingValue(i), Ty, Tmp) || |
Chris Lattner | 7acc4b1 | 2010-01-11 04:05:13 +0000 | [diff] [blame] | 723 | // TODO: If important, we could handle the case when the BitsToClear |
| 724 | // are known zero in the disagreeing input. |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 725 | Tmp != BitsToClear) |
| 726 | return false; |
Chris Lattner | 9e390dd | 2010-01-10 02:50:04 +0000 | [diff] [blame] | 727 | return true; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 728 | } |
| 729 | default: |
| 730 | // TODO: Can handle more cases here. |
Chris Lattner | 9e390dd | 2010-01-10 02:50:04 +0000 | [diff] [blame] | 731 | return false; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 735 | Instruction *InstCombiner::visitZExt(ZExtInst &CI) { |
Chris Lattner | 5324d80 | 2010-01-10 02:39:31 +0000 | [diff] [blame] | 736 | // If this zero extend is only used by a truncate, let the truncate by |
| 737 | // eliminated before we try to optimize this zext. |
| 738 | if (CI.hasOneUse() && isa<TruncInst>(CI.use_back())) |
| 739 | return 0; |
| 740 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 741 | // If one of the common conversion will work, do it. |
Chris Lattner | d84dfa4 | 2010-01-10 01:00:46 +0000 | [diff] [blame] | 742 | if (Instruction *Result = commonCastTransforms(CI)) |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 743 | return Result; |
| 744 | |
Chris Lattner | d84dfa4 | 2010-01-10 01:00:46 +0000 | [diff] [blame] | 745 | // See if we can simplify any instructions used by the input whose sole |
| 746 | // purpose is to compute bits we don't care about. |
| 747 | if (SimplifyDemandedInstructionBits(CI)) |
| 748 | return &CI; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 749 | |
Chris Lattner | d84dfa4 | 2010-01-10 01:00:46 +0000 | [diff] [blame] | 750 | Value *Src = CI.getOperand(0); |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 751 | const Type *SrcTy = Src->getType(), *DestTy = CI.getType(); |
| 752 | |
| 753 | // Attempt to extend the entire input expression tree to the destination |
| 754 | // type. Only do this if the dest type is a simple type, don't convert the |
| 755 | // expression tree to something weird like i93 unless the source is also |
| 756 | // strange. |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 757 | unsigned BitsToClear; |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 758 | if ((DestTy->isVectorTy() || ShouldChangeType(SrcTy, DestTy)) && |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 759 | CanEvaluateZExtd(Src, DestTy, BitsToClear)) { |
| 760 | assert(BitsToClear < SrcTy->getScalarSizeInBits() && |
| 761 | "Unreasonable BitsToClear"); |
| 762 | |
Chris Lattner | 5324d80 | 2010-01-10 02:39:31 +0000 | [diff] [blame] | 763 | // Okay, we can transform this! Insert the new expression now. |
| 764 | DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type" |
| 765 | " to avoid zero extend: " << CI); |
| 766 | Value *Res = EvaluateInDifferentType(Src, DestTy, false); |
| 767 | assert(Res->getType() == DestTy); |
| 768 | |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 769 | uint32_t SrcBitsKept = SrcTy->getScalarSizeInBits()-BitsToClear; |
| 770 | uint32_t DestBitSize = DestTy->getScalarSizeInBits(); |
| 771 | |
Chris Lattner | 5324d80 | 2010-01-10 02:39:31 +0000 | [diff] [blame] | 772 | // If the high bits are already filled with zeros, just replace this |
| 773 | // cast with the result. |
Chris Lattner | 9e390dd | 2010-01-10 02:50:04 +0000 | [diff] [blame] | 774 | if (MaskedValueIsZero(Res, APInt::getHighBitsSet(DestBitSize, |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 775 | DestBitSize-SrcBitsKept))) |
Chris Lattner | 5324d80 | 2010-01-10 02:39:31 +0000 | [diff] [blame] | 776 | return ReplaceInstUsesWith(CI, Res); |
| 777 | |
| 778 | // We need to emit an AND to clear the high bits. |
Chris Lattner | 9ee947c | 2010-01-10 20:25:54 +0000 | [diff] [blame] | 779 | Constant *C = ConstantInt::get(Res->getType(), |
Chris Lattner | 789162a | 2010-01-11 03:32:00 +0000 | [diff] [blame] | 780 | APInt::getLowBitsSet(DestBitSize, SrcBitsKept)); |
Chris Lattner | 5324d80 | 2010-01-10 02:39:31 +0000 | [diff] [blame] | 781 | return BinaryOperator::CreateAnd(Res, C); |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 782 | } |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 783 | |
| 784 | // If this is a TRUNC followed by a ZEXT then we are dealing with integral |
| 785 | // types and if the sizes are just right we can convert this into a logical |
| 786 | // 'and' which will be much cheaper than the pair of casts. |
| 787 | if (TruncInst *CSrc = dyn_cast<TruncInst>(Src)) { // A->B->C cast |
Chris Lattner | f4fb911 | 2010-01-10 07:08:30 +0000 | [diff] [blame] | 788 | // TODO: Subsume this into EvaluateInDifferentType. |
| 789 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 790 | // Get the sizes of the types involved. We know that the intermediate type |
| 791 | // will be smaller than A or C, but don't know the relation between A and C. |
| 792 | Value *A = CSrc->getOperand(0); |
| 793 | unsigned SrcSize = A->getType()->getScalarSizeInBits(); |
| 794 | unsigned MidSize = CSrc->getType()->getScalarSizeInBits(); |
| 795 | unsigned DstSize = CI.getType()->getScalarSizeInBits(); |
| 796 | // If we're actually extending zero bits, then if |
| 797 | // SrcSize < DstSize: zext(a & mask) |
| 798 | // SrcSize == DstSize: a & mask |
| 799 | // SrcSize > DstSize: trunc(a) & mask |
| 800 | if (SrcSize < DstSize) { |
| 801 | APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); |
| 802 | Constant *AndConst = ConstantInt::get(A->getType(), AndValue); |
| 803 | Value *And = Builder->CreateAnd(A, AndConst, CSrc->getName()+".mask"); |
| 804 | return new ZExtInst(And, CI.getType()); |
| 805 | } |
| 806 | |
| 807 | if (SrcSize == DstSize) { |
| 808 | APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); |
| 809 | return BinaryOperator::CreateAnd(A, ConstantInt::get(A->getType(), |
| 810 | AndValue)); |
| 811 | } |
| 812 | if (SrcSize > DstSize) { |
| 813 | Value *Trunc = Builder->CreateTrunc(A, CI.getType(), "tmp"); |
| 814 | APInt AndValue(APInt::getLowBitsSet(DstSize, MidSize)); |
| 815 | return BinaryOperator::CreateAnd(Trunc, |
| 816 | ConstantInt::get(Trunc->getType(), |
Chris Lattner | f4fb911 | 2010-01-10 07:08:30 +0000 | [diff] [blame] | 817 | AndValue)); |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | |
| 821 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) |
| 822 | return transformZExtICmp(ICI, CI); |
| 823 | |
| 824 | BinaryOperator *SrcI = dyn_cast<BinaryOperator>(Src); |
| 825 | if (SrcI && SrcI->getOpcode() == Instruction::Or) { |
| 826 | // zext (or icmp, icmp) --> or (zext icmp), (zext icmp) if at least one |
| 827 | // of the (zext icmp) will be transformed. |
| 828 | ICmpInst *LHS = dyn_cast<ICmpInst>(SrcI->getOperand(0)); |
| 829 | ICmpInst *RHS = dyn_cast<ICmpInst>(SrcI->getOperand(1)); |
| 830 | if (LHS && RHS && LHS->hasOneUse() && RHS->hasOneUse() && |
| 831 | (transformZExtICmp(LHS, CI, false) || |
| 832 | transformZExtICmp(RHS, CI, false))) { |
| 833 | Value *LCast = Builder->CreateZExt(LHS, CI.getType(), LHS->getName()); |
| 834 | Value *RCast = Builder->CreateZExt(RHS, CI.getType(), RHS->getName()); |
| 835 | return BinaryOperator::Create(Instruction::Or, LCast, RCast); |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | // zext(trunc(t) & C) -> (t & zext(C)). |
| 840 | if (SrcI && SrcI->getOpcode() == Instruction::And && SrcI->hasOneUse()) |
| 841 | if (ConstantInt *C = dyn_cast<ConstantInt>(SrcI->getOperand(1))) |
| 842 | if (TruncInst *TI = dyn_cast<TruncInst>(SrcI->getOperand(0))) { |
| 843 | Value *TI0 = TI->getOperand(0); |
| 844 | if (TI0->getType() == CI.getType()) |
| 845 | return |
| 846 | BinaryOperator::CreateAnd(TI0, |
| 847 | ConstantExpr::getZExt(C, CI.getType())); |
| 848 | } |
| 849 | |
| 850 | // zext((trunc(t) & C) ^ C) -> ((t & zext(C)) ^ zext(C)). |
| 851 | if (SrcI && SrcI->getOpcode() == Instruction::Xor && SrcI->hasOneUse()) |
| 852 | if (ConstantInt *C = dyn_cast<ConstantInt>(SrcI->getOperand(1))) |
| 853 | if (BinaryOperator *And = dyn_cast<BinaryOperator>(SrcI->getOperand(0))) |
| 854 | if (And->getOpcode() == Instruction::And && And->hasOneUse() && |
| 855 | And->getOperand(1) == C) |
| 856 | if (TruncInst *TI = dyn_cast<TruncInst>(And->getOperand(0))) { |
| 857 | Value *TI0 = TI->getOperand(0); |
| 858 | if (TI0->getType() == CI.getType()) { |
| 859 | Constant *ZC = ConstantExpr::getZExt(C, CI.getType()); |
| 860 | Value *NewAnd = Builder->CreateAnd(TI0, ZC, "tmp"); |
| 861 | return BinaryOperator::CreateXor(NewAnd, ZC); |
| 862 | } |
| 863 | } |
| 864 | |
Chris Lattner | 718bf3f | 2010-01-05 21:04:47 +0000 | [diff] [blame] | 865 | // zext (xor i1 X, true) to i32 --> xor (zext i1 X to i32), 1 |
| 866 | Value *X; |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 867 | if (SrcI && SrcI->hasOneUse() && SrcI->getType()->isIntegerTy(1) && |
Chris Lattner | 49bdfef | 2010-01-05 21:11:17 +0000 | [diff] [blame] | 868 | match(SrcI, m_Not(m_Value(X))) && |
Chris Lattner | 718bf3f | 2010-01-05 21:04:47 +0000 | [diff] [blame] | 869 | (!X->hasOneUse() || !isa<CmpInst>(X))) { |
| 870 | Value *New = Builder->CreateZExt(X, CI.getType()); |
| 871 | return BinaryOperator::CreateXor(New, ConstantInt::get(CI.getType(), 1)); |
| 872 | } |
| 873 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 874 | return 0; |
| 875 | } |
| 876 | |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 877 | /// CanEvaluateSExtd - Return true if we can take the specified value |
| 878 | /// and return it as type Ty without inserting any new casts and without |
| 879 | /// changing the value of the common low bits. This is used by code that tries |
| 880 | /// to promote integer operations to a wider types will allow us to eliminate |
| 881 | /// the extension. |
| 882 | /// |
Chris Lattner | aa9c894 | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 883 | /// This function works on both vectors and scalars. |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 884 | /// |
Chris Lattner | 8cf4f6f | 2010-01-11 02:43:35 +0000 | [diff] [blame] | 885 | static bool CanEvaluateSExtd(Value *V, const Type *Ty) { |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 886 | assert(V->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits() && |
| 887 | "Can't sign extend type to a smaller type"); |
Chris Lattner | aa9c894 | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 888 | // If this is a constant, it can be trivially promoted. |
| 889 | if (isa<Constant>(V)) |
| 890 | return true; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 891 | |
| 892 | Instruction *I = dyn_cast<Instruction>(V); |
Chris Lattner | aa9c894 | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 893 | if (!I) return false; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 894 | |
Chris Lattner | a958cbf | 2010-01-11 22:45:25 +0000 | [diff] [blame] | 895 | // If this is a truncate from the dest type, we can trivially eliminate it, |
| 896 | // even if it has multiple uses. |
| 897 | // FIXME: This is currently disabled until codegen can handle this without |
| 898 | // pessimizing code, PR5997. |
| 899 | if (0 && isa<TruncInst>(I) && I->getOperand(0)->getType() == Ty) |
Chris Lattner | aa9c894 | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 900 | return true; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 901 | |
| 902 | // We can't extend or shrink something that has multiple uses: doing so would |
| 903 | // require duplicating the instruction in general, which isn't profitable. |
Chris Lattner | aa9c894 | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 904 | if (!I->hasOneUse()) return false; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 905 | |
Chris Lattner | aa9c894 | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 906 | switch (I->getOpcode()) { |
Chris Lattner | 11ea812 | 2010-01-10 20:30:41 +0000 | [diff] [blame] | 907 | case Instruction::SExt: // sext(sext(x)) -> sext(x) |
| 908 | case Instruction::ZExt: // sext(zext(x)) -> zext(x) |
| 909 | case Instruction::Trunc: // sext(trunc(x)) -> trunc(x) or sext(x) |
| 910 | return true; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 911 | case Instruction::And: |
| 912 | case Instruction::Or: |
| 913 | case Instruction::Xor: |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 914 | case Instruction::Add: |
| 915 | case Instruction::Sub: |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 916 | case Instruction::Mul: |
Chris Lattner | aa9c894 | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 917 | // These operators can all arbitrarily be extended if their inputs can. |
Chris Lattner | 8cf4f6f | 2010-01-11 02:43:35 +0000 | [diff] [blame] | 918 | return CanEvaluateSExtd(I->getOperand(0), Ty) && |
| 919 | CanEvaluateSExtd(I->getOperand(1), Ty); |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 920 | |
| 921 | //case Instruction::Shl: TODO |
| 922 | //case Instruction::LShr: TODO |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 923 | |
Chris Lattner | aa9c894 | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 924 | case Instruction::Select: |
Chris Lattner | 8cf4f6f | 2010-01-11 02:43:35 +0000 | [diff] [blame] | 925 | return CanEvaluateSExtd(I->getOperand(1), Ty) && |
| 926 | CanEvaluateSExtd(I->getOperand(2), Ty); |
Chris Lattner | 9ee947c | 2010-01-10 20:25:54 +0000 | [diff] [blame] | 927 | |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 928 | case Instruction::PHI: { |
| 929 | // We can change a phi if we can change all operands. Note that we never |
| 930 | // get into trouble with cyclic PHIs here because we only consider |
| 931 | // instructions with a single use. |
| 932 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | 9ee947c | 2010-01-10 20:25:54 +0000 | [diff] [blame] | 933 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
Chris Lattner | 8cf4f6f | 2010-01-11 02:43:35 +0000 | [diff] [blame] | 934 | if (!CanEvaluateSExtd(PN->getIncomingValue(i), Ty)) return false; |
Chris Lattner | aa9c894 | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 935 | return true; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 936 | } |
| 937 | default: |
| 938 | // TODO: Can handle more cases here. |
| 939 | break; |
| 940 | } |
| 941 | |
Chris Lattner | aa9c894 | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 942 | return false; |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 945 | Instruction *InstCombiner::visitSExt(SExtInst &CI) { |
Chris Lattner | 5324d80 | 2010-01-10 02:39:31 +0000 | [diff] [blame] | 946 | // If this sign extend is only used by a truncate, let the truncate by |
| 947 | // eliminated before we try to optimize this zext. |
| 948 | if (CI.hasOneUse() && isa<TruncInst>(CI.use_back())) |
| 949 | return 0; |
| 950 | |
Chris Lattner | d84dfa4 | 2010-01-10 01:00:46 +0000 | [diff] [blame] | 951 | if (Instruction *I = commonCastTransforms(CI)) |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 952 | return I; |
| 953 | |
Chris Lattner | d84dfa4 | 2010-01-10 01:00:46 +0000 | [diff] [blame] | 954 | // See if we can simplify any instructions used by the input whose sole |
| 955 | // purpose is to compute bits we don't care about. |
| 956 | if (SimplifyDemandedInstructionBits(CI)) |
| 957 | return &CI; |
| 958 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 959 | Value *Src = CI.getOperand(0); |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 960 | const Type *SrcTy = Src->getType(), *DestTy = CI.getType(); |
| 961 | |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 962 | // Attempt to extend the entire input expression tree to the destination |
| 963 | // type. Only do this if the dest type is a simple type, don't convert the |
| 964 | // expression tree to something weird like i93 unless the source is also |
| 965 | // strange. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 966 | if ((DestTy->isVectorTy() || ShouldChangeType(SrcTy, DestTy)) && |
Chris Lattner | 8cf4f6f | 2010-01-11 02:43:35 +0000 | [diff] [blame] | 967 | CanEvaluateSExtd(Src, DestTy)) { |
Chris Lattner | dde5ee5 | 2010-01-10 07:40:50 +0000 | [diff] [blame] | 968 | // Okay, we can transform this! Insert the new expression now. |
| 969 | DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type" |
| 970 | " to avoid sign extend: " << CI); |
| 971 | Value *Res = EvaluateInDifferentType(Src, DestTy, true); |
| 972 | assert(Res->getType() == DestTy); |
| 973 | |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 974 | uint32_t SrcBitSize = SrcTy->getScalarSizeInBits(); |
| 975 | uint32_t DestBitSize = DestTy->getScalarSizeInBits(); |
Chris Lattner | dde5ee5 | 2010-01-10 07:40:50 +0000 | [diff] [blame] | 976 | |
| 977 | // If the high bits are already filled with sign bit, just replace this |
| 978 | // cast with the result. |
Chris Lattner | aa9c894 | 2010-01-10 07:57:20 +0000 | [diff] [blame] | 979 | if (ComputeNumSignBits(Res) > DestBitSize - SrcBitSize) |
Chris Lattner | dde5ee5 | 2010-01-10 07:40:50 +0000 | [diff] [blame] | 980 | return ReplaceInstUsesWith(CI, Res); |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 981 | |
Chris Lattner | dde5ee5 | 2010-01-10 07:40:50 +0000 | [diff] [blame] | 982 | // We need to emit a shl + ashr to do the sign extend. |
| 983 | Value *ShAmt = ConstantInt::get(DestTy, DestBitSize-SrcBitSize); |
| 984 | return BinaryOperator::CreateAShr(Builder->CreateShl(Res, ShAmt, "sext"), |
| 985 | ShAmt); |
Chris Lattner | 75215c9 | 2010-01-10 00:58:42 +0000 | [diff] [blame] | 986 | } |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 987 | |
Chris Lattner | cd5adbb | 2010-01-18 22:19:16 +0000 | [diff] [blame] | 988 | // If this input is a trunc from our destination, then turn sext(trunc(x)) |
| 989 | // into shifts. |
| 990 | if (TruncInst *TI = dyn_cast<TruncInst>(Src)) |
| 991 | if (TI->hasOneUse() && TI->getOperand(0)->getType() == DestTy) { |
| 992 | uint32_t SrcBitSize = SrcTy->getScalarSizeInBits(); |
| 993 | uint32_t DestBitSize = DestTy->getScalarSizeInBits(); |
| 994 | |
| 995 | // We need to emit a shl + ashr to do the sign extend. |
| 996 | Value *ShAmt = ConstantInt::get(DestTy, DestBitSize-SrcBitSize); |
| 997 | Value *Res = Builder->CreateShl(TI->getOperand(0), ShAmt, "sext"); |
| 998 | return BinaryOperator::CreateAShr(Res, ShAmt); |
| 999 | } |
| 1000 | |
Chris Lattner | abb992d | 2010-01-24 00:09:49 +0000 | [diff] [blame] | 1001 | |
| 1002 | // (x <s 0) ? -1 : 0 -> ashr x, 31 -> all ones if signed |
| 1003 | // (x >s -1) ? -1 : 0 -> ashr x, 31 -> all ones if not signed |
| 1004 | { |
| 1005 | ICmpInst::Predicate Pred; Value *CmpLHS; ConstantInt *CmpRHS; |
| 1006 | if (match(Src, m_ICmp(Pred, m_Value(CmpLHS), m_ConstantInt(CmpRHS)))) { |
| 1007 | // sext (x <s 0) to i32 --> x>>s31 true if signbit set. |
| 1008 | // sext (x >s -1) to i32 --> (x>>s31)^-1 true if signbit clear. |
| 1009 | if ((Pred == ICmpInst::ICMP_SLT && CmpRHS->isZero()) || |
| 1010 | (Pred == ICmpInst::ICMP_SGT && CmpRHS->isAllOnesValue())) { |
| 1011 | Value *Sh = ConstantInt::get(CmpLHS->getType(), |
| 1012 | CmpLHS->getType()->getScalarSizeInBits()-1); |
| 1013 | Value *In = Builder->CreateAShr(CmpLHS, Sh, CmpLHS->getName()+".lobit"); |
| 1014 | if (In->getType() != CI.getType()) |
| 1015 | In = Builder->CreateIntCast(In, CI.getType(), true/*SExt*/, "tmp"); |
| 1016 | |
| 1017 | if (Pred == ICmpInst::ICMP_SGT) |
| 1018 | In = Builder->CreateNot(In, In->getName()+".not"); |
| 1019 | return ReplaceInstUsesWith(CI, In); |
| 1020 | } |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1025 | // If the input is a shl/ashr pair of a same constant, then this is a sign |
| 1026 | // extension from a smaller value. If we could trust arbitrary bitwidth |
| 1027 | // integers, we could turn this into a truncate to the smaller bit and then |
| 1028 | // use a sext for the whole extension. Since we don't, look deeper and check |
| 1029 | // for a truncate. If the source and dest are the same type, eliminate the |
| 1030 | // trunc and extend and just do shifts. For example, turn: |
| 1031 | // %a = trunc i32 %i to i8 |
| 1032 | // %b = shl i8 %a, 6 |
| 1033 | // %c = ashr i8 %b, 6 |
| 1034 | // %d = sext i8 %c to i32 |
| 1035 | // into: |
| 1036 | // %a = shl i32 %i, 30 |
| 1037 | // %d = ashr i32 %a, 30 |
| 1038 | Value *A = 0; |
Chris Lattner | 4f37978 | 2010-01-10 01:04:31 +0000 | [diff] [blame] | 1039 | // TODO: Eventually this could be subsumed by EvaluateInDifferentType. |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1040 | ConstantInt *BA = 0, *CA = 0; |
Chris Lattner | 4f37978 | 2010-01-10 01:04:31 +0000 | [diff] [blame] | 1041 | 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] | 1042 | m_ConstantInt(CA))) && |
Chris Lattner | 4f37978 | 2010-01-10 01:04:31 +0000 | [diff] [blame] | 1043 | BA == CA && A->getType() == CI.getType()) { |
| 1044 | unsigned MidSize = Src->getType()->getScalarSizeInBits(); |
| 1045 | unsigned SrcDstSize = CI.getType()->getScalarSizeInBits(); |
| 1046 | unsigned ShAmt = CA->getZExtValue()+SrcDstSize-MidSize; |
| 1047 | Constant *ShAmtV = ConstantInt::get(CI.getType(), ShAmt); |
| 1048 | A = Builder->CreateShl(A, ShAmtV, CI.getName()); |
| 1049 | return BinaryOperator::CreateAShr(A, ShAmtV); |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | return 0; |
| 1053 | } |
| 1054 | |
| 1055 | |
| 1056 | /// FitsInFPType - Return a Constant* for the specified FP constant if it fits |
| 1057 | /// in the specified FP type without changing its value. |
| 1058 | static Constant *FitsInFPType(ConstantFP *CFP, const fltSemantics &Sem) { |
| 1059 | bool losesInfo; |
| 1060 | APFloat F = CFP->getValueAPF(); |
| 1061 | (void)F.convert(Sem, APFloat::rmNearestTiesToEven, &losesInfo); |
| 1062 | if (!losesInfo) |
| 1063 | return ConstantFP::get(CFP->getContext(), F); |
| 1064 | return 0; |
| 1065 | } |
| 1066 | |
| 1067 | /// LookThroughFPExtensions - If this is an fp extension instruction, look |
| 1068 | /// through it until we get the source value. |
| 1069 | static Value *LookThroughFPExtensions(Value *V) { |
| 1070 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 1071 | if (I->getOpcode() == Instruction::FPExt) |
| 1072 | return LookThroughFPExtensions(I->getOperand(0)); |
| 1073 | |
| 1074 | // If this value is a constant, return the constant in the smallest FP type |
| 1075 | // that can accurately represent it. This allows us to turn |
| 1076 | // (float)((double)X+2.0) into x+2.0f. |
| 1077 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { |
| 1078 | if (CFP->getType() == Type::getPPC_FP128Ty(V->getContext())) |
| 1079 | return V; // No constant folding of this. |
| 1080 | // See if the value can be truncated to float and then reextended. |
| 1081 | if (Value *V = FitsInFPType(CFP, APFloat::IEEEsingle)) |
| 1082 | return V; |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 1083 | if (CFP->getType()->isDoubleTy()) |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1084 | return V; // Won't shrink. |
| 1085 | if (Value *V = FitsInFPType(CFP, APFloat::IEEEdouble)) |
| 1086 | return V; |
| 1087 | // Don't try to shrink to various long double types. |
| 1088 | } |
| 1089 | |
| 1090 | return V; |
| 1091 | } |
| 1092 | |
| 1093 | Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) { |
| 1094 | if (Instruction *I = commonCastTransforms(CI)) |
| 1095 | return I; |
| 1096 | |
| 1097 | // If we have fptrunc(fadd (fpextend x), (fpextend y)), where x and y are |
| 1098 | // smaller than the destination type, we can eliminate the truncate by doing |
| 1099 | // the add as the smaller type. This applies to fadd/fsub/fmul/fdiv as well |
| 1100 | // as many builtins (sqrt, etc). |
| 1101 | BinaryOperator *OpI = dyn_cast<BinaryOperator>(CI.getOperand(0)); |
| 1102 | if (OpI && OpI->hasOneUse()) { |
| 1103 | switch (OpI->getOpcode()) { |
| 1104 | default: break; |
| 1105 | case Instruction::FAdd: |
| 1106 | case Instruction::FSub: |
| 1107 | case Instruction::FMul: |
| 1108 | case Instruction::FDiv: |
| 1109 | case Instruction::FRem: |
| 1110 | const Type *SrcTy = OpI->getType(); |
| 1111 | Value *LHSTrunc = LookThroughFPExtensions(OpI->getOperand(0)); |
| 1112 | Value *RHSTrunc = LookThroughFPExtensions(OpI->getOperand(1)); |
| 1113 | if (LHSTrunc->getType() != SrcTy && |
| 1114 | RHSTrunc->getType() != SrcTy) { |
| 1115 | unsigned DstSize = CI.getType()->getScalarSizeInBits(); |
| 1116 | // If the source types were both smaller than the destination type of |
| 1117 | // the cast, do this xform. |
| 1118 | if (LHSTrunc->getType()->getScalarSizeInBits() <= DstSize && |
| 1119 | RHSTrunc->getType()->getScalarSizeInBits() <= DstSize) { |
| 1120 | LHSTrunc = Builder->CreateFPExt(LHSTrunc, CI.getType()); |
| 1121 | RHSTrunc = Builder->CreateFPExt(RHSTrunc, CI.getType()); |
| 1122 | return BinaryOperator::Create(OpI->getOpcode(), LHSTrunc, RHSTrunc); |
| 1123 | } |
| 1124 | } |
| 1125 | break; |
| 1126 | } |
| 1127 | } |
Owen Anderson | d902901 | 2010-07-19 08:09:34 +0000 | [diff] [blame] | 1128 | |
| 1129 | // Fold (fptrunc (sqrt (fpext x))) -> (sqrtf x) |
| 1130 | // NOTE: This should be disabled by -fno-builtin-sqrt if we ever support it. |
| 1131 | CallInst *Call = dyn_cast<CallInst>(CI.getOperand(0)); |
| 1132 | if (Call && Call->getCalledFunction() && |
| 1133 | Call->getCalledFunction()->getName() == "sqrt" && |
| 1134 | Call->getNumArgOperands() == 1) { |
| 1135 | CastInst *Arg = dyn_cast<CastInst>(Call->getArgOperand(0)); |
| 1136 | if (Arg && Arg->getOpcode() == Instruction::FPExt && |
Owen Anderson | 5f23a93 | 2010-07-19 19:23:32 +0000 | [diff] [blame] | 1137 | CI.getType()->isFloatTy() && |
| 1138 | Call->getType()->isDoubleTy() && |
| 1139 | Arg->getType()->isDoubleTy() && |
| 1140 | Arg->getOperand(0)->getType()->isFloatTy()) { |
| 1141 | Function *Callee = Call->getCalledFunction(); |
| 1142 | Module *M = CI.getParent()->getParent()->getParent(); |
Owen Anderson | d902901 | 2010-07-19 08:09:34 +0000 | [diff] [blame] | 1143 | Constant* SqrtfFunc = M->getOrInsertFunction("sqrtf", |
Owen Anderson | 5f23a93 | 2010-07-19 19:23:32 +0000 | [diff] [blame] | 1144 | Callee->getAttributes(), |
Owen Anderson | d902901 | 2010-07-19 08:09:34 +0000 | [diff] [blame] | 1145 | Builder->getFloatTy(), |
| 1146 | Builder->getFloatTy(), |
| 1147 | NULL); |
| 1148 | CallInst *ret = CallInst::Create(SqrtfFunc, Arg->getOperand(0), |
| 1149 | "sqrtfcall"); |
Owen Anderson | 5f23a93 | 2010-07-19 19:23:32 +0000 | [diff] [blame] | 1150 | ret->setAttributes(Callee->getAttributes()); |
Owen Anderson | d902901 | 2010-07-19 08:09:34 +0000 | [diff] [blame] | 1151 | return ret; |
| 1152 | } |
| 1153 | } |
| 1154 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1155 | return 0; |
| 1156 | } |
| 1157 | |
| 1158 | Instruction *InstCombiner::visitFPExt(CastInst &CI) { |
| 1159 | return commonCastTransforms(CI); |
| 1160 | } |
| 1161 | |
| 1162 | Instruction *InstCombiner::visitFPToUI(FPToUIInst &FI) { |
| 1163 | Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0)); |
| 1164 | if (OpI == 0) |
| 1165 | return commonCastTransforms(FI); |
| 1166 | |
| 1167 | // fptoui(uitofp(X)) --> X |
| 1168 | // fptoui(sitofp(X)) --> X |
| 1169 | // This is safe if the intermediate type has enough bits in its mantissa to |
| 1170 | // accurately represent all values of X. For example, do not do this with |
| 1171 | // i64->float->i64. This is also safe for sitofp case, because any negative |
| 1172 | // 'X' value would cause an undefined result for the fptoui. |
| 1173 | if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) && |
| 1174 | OpI->getOperand(0)->getType() == FI.getType() && |
| 1175 | (int)FI.getType()->getScalarSizeInBits() < /*extra bit for sign */ |
| 1176 | OpI->getType()->getFPMantissaWidth()) |
| 1177 | return ReplaceInstUsesWith(FI, OpI->getOperand(0)); |
| 1178 | |
| 1179 | return commonCastTransforms(FI); |
| 1180 | } |
| 1181 | |
| 1182 | Instruction *InstCombiner::visitFPToSI(FPToSIInst &FI) { |
| 1183 | Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0)); |
| 1184 | if (OpI == 0) |
| 1185 | return commonCastTransforms(FI); |
| 1186 | |
| 1187 | // fptosi(sitofp(X)) --> X |
| 1188 | // fptosi(uitofp(X)) --> X |
| 1189 | // This is safe if the intermediate type has enough bits in its mantissa to |
| 1190 | // accurately represent all values of X. For example, do not do this with |
| 1191 | // i64->float->i64. This is also safe for sitofp case, because any negative |
| 1192 | // 'X' value would cause an undefined result for the fptoui. |
| 1193 | if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) && |
| 1194 | OpI->getOperand(0)->getType() == FI.getType() && |
| 1195 | (int)FI.getType()->getScalarSizeInBits() <= |
| 1196 | OpI->getType()->getFPMantissaWidth()) |
| 1197 | return ReplaceInstUsesWith(FI, OpI->getOperand(0)); |
| 1198 | |
| 1199 | return commonCastTransforms(FI); |
| 1200 | } |
| 1201 | |
| 1202 | Instruction *InstCombiner::visitUIToFP(CastInst &CI) { |
| 1203 | return commonCastTransforms(CI); |
| 1204 | } |
| 1205 | |
| 1206 | Instruction *InstCombiner::visitSIToFP(CastInst &CI) { |
| 1207 | return commonCastTransforms(CI); |
| 1208 | } |
| 1209 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1210 | Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) { |
Dan Gohman | 3b5487e | 2010-02-02 01:44:02 +0000 | [diff] [blame] | 1211 | // If the source integer type is not the intptr_t type for this target, do a |
| 1212 | // trunc or zext to the intptr_t type, then inttoptr of it. This allows the |
| 1213 | // cast to be exposed to other transforms. |
| 1214 | if (TD) { |
| 1215 | if (CI.getOperand(0)->getType()->getScalarSizeInBits() > |
| 1216 | TD->getPointerSizeInBits()) { |
| 1217 | Value *P = Builder->CreateTrunc(CI.getOperand(0), |
| 1218 | TD->getIntPtrType(CI.getContext()), "tmp"); |
| 1219 | return new IntToPtrInst(P, CI.getType()); |
| 1220 | } |
| 1221 | if (CI.getOperand(0)->getType()->getScalarSizeInBits() < |
| 1222 | TD->getPointerSizeInBits()) { |
| 1223 | Value *P = Builder->CreateZExt(CI.getOperand(0), |
| 1224 | TD->getIntPtrType(CI.getContext()), "tmp"); |
| 1225 | return new IntToPtrInst(P, CI.getType()); |
| 1226 | } |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | if (Instruction *I = commonCastTransforms(CI)) |
| 1230 | return I; |
| 1231 | |
| 1232 | return 0; |
| 1233 | } |
| 1234 | |
Chris Lattner | 7a34d6c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1235 | /// @brief Implement the transforms for cast of pointer (bitcast/ptrtoint) |
| 1236 | Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) { |
| 1237 | Value *Src = CI.getOperand(0); |
| 1238 | |
| 1239 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
| 1240 | // If casting the result of a getelementptr instruction with no offset, turn |
| 1241 | // this into a cast of the original pointer! |
| 1242 | if (GEP->hasAllZeroIndices()) { |
| 1243 | // Changing the cast operand is usually not a good idea but it is safe |
| 1244 | // here because the pointer operand is being replaced with another |
| 1245 | // pointer operand so the opcode doesn't need to change. |
| 1246 | Worklist.Add(GEP); |
| 1247 | CI.setOperand(0, GEP->getOperand(0)); |
| 1248 | return &CI; |
| 1249 | } |
| 1250 | |
| 1251 | // If the GEP has a single use, and the base pointer is a bitcast, and the |
| 1252 | // GEP computes a constant offset, see if we can convert these three |
| 1253 | // instructions into fewer. This typically happens with unions and other |
| 1254 | // non-type-safe code. |
| 1255 | if (TD && GEP->hasOneUse() && isa<BitCastInst>(GEP->getOperand(0)) && |
| 1256 | GEP->hasAllConstantIndices()) { |
| 1257 | // We are guaranteed to get a constant from EmitGEPOffset. |
| 1258 | ConstantInt *OffsetV = cast<ConstantInt>(EmitGEPOffset(GEP)); |
| 1259 | int64_t Offset = OffsetV->getSExtValue(); |
| 1260 | |
| 1261 | // Get the base pointer input of the bitcast, and the type it points to. |
| 1262 | Value *OrigBase = cast<BitCastInst>(GEP->getOperand(0))->getOperand(0); |
| 1263 | const Type *GEPIdxTy = |
| 1264 | cast<PointerType>(OrigBase->getType())->getElementType(); |
| 1265 | SmallVector<Value*, 8> NewIndices; |
| 1266 | if (FindElementAtOffset(GEPIdxTy, Offset, NewIndices)) { |
| 1267 | // If we were able to index down into an element, create the GEP |
| 1268 | // and bitcast the result. This eliminates one bitcast, potentially |
| 1269 | // two. |
| 1270 | Value *NGEP = cast<GEPOperator>(GEP)->isInBounds() ? |
| 1271 | Builder->CreateInBoundsGEP(OrigBase, |
| 1272 | NewIndices.begin(), NewIndices.end()) : |
| 1273 | Builder->CreateGEP(OrigBase, NewIndices.begin(), NewIndices.end()); |
| 1274 | NGEP->takeName(GEP); |
| 1275 | |
| 1276 | if (isa<BitCastInst>(CI)) |
| 1277 | return new BitCastInst(NGEP, CI.getType()); |
| 1278 | assert(isa<PtrToIntInst>(CI)); |
| 1279 | return new PtrToIntInst(NGEP, CI.getType()); |
| 1280 | } |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | return commonCastTransforms(CI); |
| 1285 | } |
| 1286 | |
| 1287 | Instruction *InstCombiner::visitPtrToInt(PtrToIntInst &CI) { |
Dan Gohman | 3b5487e | 2010-02-02 01:44:02 +0000 | [diff] [blame] | 1288 | // If the destination integer type is not the intptr_t type for this target, |
| 1289 | // do a ptrtoint to intptr_t then do a trunc or zext. This allows the cast |
| 1290 | // to be exposed to other transforms. |
| 1291 | if (TD) { |
| 1292 | if (CI.getType()->getScalarSizeInBits() < TD->getPointerSizeInBits()) { |
| 1293 | Value *P = Builder->CreatePtrToInt(CI.getOperand(0), |
| 1294 | TD->getIntPtrType(CI.getContext()), |
| 1295 | "tmp"); |
| 1296 | return new TruncInst(P, CI.getType()); |
| 1297 | } |
| 1298 | if (CI.getType()->getScalarSizeInBits() > TD->getPointerSizeInBits()) { |
| 1299 | Value *P = Builder->CreatePtrToInt(CI.getOperand(0), |
| 1300 | TD->getIntPtrType(CI.getContext()), |
| 1301 | "tmp"); |
| 1302 | return new ZExtInst(P, CI.getType()); |
| 1303 | } |
Chris Lattner | 7a34d6c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | return commonPointerCastTransforms(CI); |
| 1307 | } |
| 1308 | |
Chris Lattner | 6745191 | 2010-05-08 21:50:26 +0000 | [diff] [blame] | 1309 | /// OptimizeVectorResize - This input value (which is known to have vector type) |
| 1310 | /// is being zero extended or truncated to the specified vector type. Try to |
| 1311 | /// replace it with a shuffle (and vector/vector bitcast) if possible. |
| 1312 | /// |
| 1313 | /// The source and destination vector types may have different element types. |
| 1314 | static Instruction *OptimizeVectorResize(Value *InVal, const VectorType *DestTy, |
| 1315 | InstCombiner &IC) { |
| 1316 | // We can only do this optimization if the output is a multiple of the input |
| 1317 | // element size, or the input is a multiple of the output element size. |
| 1318 | // Convert the input type to have the same element type as the output. |
| 1319 | const VectorType *SrcTy = cast<VectorType>(InVal->getType()); |
| 1320 | |
| 1321 | if (SrcTy->getElementType() != DestTy->getElementType()) { |
| 1322 | // The input types don't need to be identical, but for now they must be the |
| 1323 | // same size. There is no specific reason we couldn't handle things like |
| 1324 | // <4 x i16> -> <4 x i32> by bitcasting to <2 x i32> but haven't gotten |
| 1325 | // there yet. |
| 1326 | if (SrcTy->getElementType()->getPrimitiveSizeInBits() != |
| 1327 | DestTy->getElementType()->getPrimitiveSizeInBits()) |
| 1328 | return 0; |
| 1329 | |
| 1330 | SrcTy = VectorType::get(DestTy->getElementType(), SrcTy->getNumElements()); |
| 1331 | InVal = IC.Builder->CreateBitCast(InVal, SrcTy); |
| 1332 | } |
| 1333 | |
| 1334 | // Now that the element types match, get the shuffle mask and RHS of the |
| 1335 | // shuffle to use, which depends on whether we're increasing or decreasing the |
| 1336 | // size of the input. |
| 1337 | SmallVector<Constant*, 16> ShuffleMask; |
| 1338 | Value *V2; |
| 1339 | const IntegerType *Int32Ty = Type::getInt32Ty(SrcTy->getContext()); |
| 1340 | |
| 1341 | if (SrcTy->getNumElements() > DestTy->getNumElements()) { |
| 1342 | // If we're shrinking the number of elements, just shuffle in the low |
| 1343 | // elements from the input and use undef as the second shuffle input. |
| 1344 | V2 = UndefValue::get(SrcTy); |
| 1345 | for (unsigned i = 0, e = DestTy->getNumElements(); i != e; ++i) |
| 1346 | ShuffleMask.push_back(ConstantInt::get(Int32Ty, i)); |
| 1347 | |
| 1348 | } else { |
| 1349 | // If we're increasing the number of elements, shuffle in all of the |
| 1350 | // elements from InVal and fill the rest of the result elements with zeros |
| 1351 | // from a constant zero. |
| 1352 | V2 = Constant::getNullValue(SrcTy); |
| 1353 | unsigned SrcElts = SrcTy->getNumElements(); |
| 1354 | for (unsigned i = 0, e = SrcElts; i != e; ++i) |
| 1355 | ShuffleMask.push_back(ConstantInt::get(Int32Ty, i)); |
| 1356 | |
| 1357 | // The excess elements reference the first element of the zero input. |
| 1358 | ShuffleMask.append(DestTy->getNumElements()-SrcElts, |
| 1359 | ConstantInt::get(Int32Ty, SrcElts)); |
| 1360 | } |
| 1361 | |
| 1362 | Constant *Mask = ConstantVector::get(ShuffleMask.data(), ShuffleMask.size()); |
| 1363 | return new ShuffleVectorInst(InVal, V2, Mask); |
| 1364 | } |
| 1365 | |
Chris Lattner | e5a1426 | 2010-08-26 21:55:42 +0000 | [diff] [blame] | 1366 | /// OptimizeIntToFloatBitCast - See if we can optimize an integer->float/double |
| 1367 | /// bitcast. The various long double bitcasts can't get in here. |
Chris Lattner | 26dbe7e | 2010-08-26 22:14:59 +0000 | [diff] [blame] | 1368 | static Instruction *OptimizeIntToFloatBitCast(BitCastInst &CI,InstCombiner &IC){ |
Chris Lattner | e5a1426 | 2010-08-26 21:55:42 +0000 | [diff] [blame] | 1369 | Value *Src = CI.getOperand(0); |
Chris Lattner | 26dbe7e | 2010-08-26 22:14:59 +0000 | [diff] [blame] | 1370 | const Type *DestTy = CI.getType(); |
Chris Lattner | e5a1426 | 2010-08-26 21:55:42 +0000 | [diff] [blame] | 1371 | |
| 1372 | // If this is a bitcast from int to float, check to see if the int is an |
| 1373 | // extraction from a vector. |
| 1374 | Value *VecInput = 0; |
Chris Lattner | 26dbe7e | 2010-08-26 22:14:59 +0000 | [diff] [blame] | 1375 | // bitcast(trunc(bitcast(somevector))) |
Chris Lattner | e5a1426 | 2010-08-26 21:55:42 +0000 | [diff] [blame] | 1376 | if (match(Src, m_Trunc(m_BitCast(m_Value(VecInput)))) && |
| 1377 | isa<VectorType>(VecInput->getType())) { |
| 1378 | const VectorType *VecTy = cast<VectorType>(VecInput->getType()); |
Chris Lattner | 26dbe7e | 2010-08-26 22:14:59 +0000 | [diff] [blame] | 1379 | unsigned DestWidth = DestTy->getPrimitiveSizeInBits(); |
| 1380 | |
| 1381 | if (VecTy->getPrimitiveSizeInBits() % DestWidth == 0) { |
| 1382 | // If the element type of the vector doesn't match the result type, |
| 1383 | // bitcast it to be a vector type we can extract from. |
| 1384 | if (VecTy->getElementType() != DestTy) { |
| 1385 | VecTy = VectorType::get(DestTy, |
| 1386 | VecTy->getPrimitiveSizeInBits() / DestWidth); |
| 1387 | VecInput = IC.Builder->CreateBitCast(VecInput, VecTy); |
| 1388 | } |
Chris Lattner | e5a1426 | 2010-08-26 21:55:42 +0000 | [diff] [blame] | 1389 | |
Chris Lattner | e5a1426 | 2010-08-26 21:55:42 +0000 | [diff] [blame] | 1390 | return ExtractElementInst::Create(VecInput, IC.Builder->getInt32(0)); |
Chris Lattner | 26dbe7e | 2010-08-26 22:14:59 +0000 | [diff] [blame] | 1391 | } |
Chris Lattner | e5a1426 | 2010-08-26 21:55:42 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
Chris Lattner | 26dbe7e | 2010-08-26 22:14:59 +0000 | [diff] [blame] | 1394 | // bitcast(trunc(lshr(bitcast(somevector), cst)) |
| 1395 | ConstantInt *ShAmt = 0; |
| 1396 | if (match(Src, m_Trunc(m_LShr(m_BitCast(m_Value(VecInput)), |
| 1397 | m_ConstantInt(ShAmt)))) && |
| 1398 | isa<VectorType>(VecInput->getType())) { |
| 1399 | const VectorType *VecTy = cast<VectorType>(VecInput->getType()); |
| 1400 | unsigned DestWidth = DestTy->getPrimitiveSizeInBits(); |
| 1401 | if (VecTy->getPrimitiveSizeInBits() % DestWidth == 0 && |
| 1402 | ShAmt->getZExtValue() % DestWidth == 0) { |
| 1403 | // If the element type of the vector doesn't match the result type, |
| 1404 | // bitcast it to be a vector type we can extract from. |
| 1405 | if (VecTy->getElementType() != DestTy) { |
| 1406 | VecTy = VectorType::get(DestTy, |
| 1407 | VecTy->getPrimitiveSizeInBits() / DestWidth); |
| 1408 | VecInput = IC.Builder->CreateBitCast(VecInput, VecTy); |
| 1409 | } |
| 1410 | |
| 1411 | unsigned Elt = ShAmt->getZExtValue() / DestWidth; |
| 1412 | return ExtractElementInst::Create(VecInput, IC.Builder->getInt32(Elt)); |
| 1413 | } |
| 1414 | } |
Chris Lattner | e5a1426 | 2010-08-26 21:55:42 +0000 | [diff] [blame] | 1415 | return 0; |
| 1416 | } |
Chris Lattner | 6745191 | 2010-05-08 21:50:26 +0000 | [diff] [blame] | 1417 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1418 | Instruction *InstCombiner::visitBitCast(BitCastInst &CI) { |
| 1419 | // If the operands are integer typed then apply the integer transforms, |
| 1420 | // otherwise just apply the common ones. |
| 1421 | Value *Src = CI.getOperand(0); |
| 1422 | const Type *SrcTy = Src->getType(); |
| 1423 | const Type *DestTy = CI.getType(); |
| 1424 | |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1425 | // Get rid of casts from one type to the same type. These are useless and can |
| 1426 | // be replaced by the operand. |
| 1427 | if (DestTy == Src->getType()) |
| 1428 | return ReplaceInstUsesWith(CI, Src); |
| 1429 | |
| 1430 | if (const PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) { |
| 1431 | const PointerType *SrcPTy = cast<PointerType>(SrcTy); |
| 1432 | const Type *DstElTy = DstPTy->getElementType(); |
| 1433 | const Type *SrcElTy = SrcPTy->getElementType(); |
| 1434 | |
| 1435 | // If the address spaces don't match, don't eliminate the bitcast, which is |
| 1436 | // required for changing types. |
| 1437 | if (SrcPTy->getAddressSpace() != DstPTy->getAddressSpace()) |
| 1438 | return 0; |
| 1439 | |
| 1440 | // If we are casting a alloca to a pointer to a type of the same |
| 1441 | // size, rewrite the allocation instruction to allocate the "right" type. |
| 1442 | // There is no need to modify malloc calls because it is their bitcast that |
| 1443 | // needs to be cleaned up. |
| 1444 | if (AllocaInst *AI = dyn_cast<AllocaInst>(Src)) |
| 1445 | if (Instruction *V = PromoteCastOfAllocation(CI, *AI)) |
| 1446 | return V; |
| 1447 | |
| 1448 | // If the source and destination are pointers, and this cast is equivalent |
| 1449 | // to a getelementptr X, 0, 0, 0... turn it into the appropriate gep. |
| 1450 | // This can enhance SROA and other transforms that want type-safe pointers. |
| 1451 | Constant *ZeroUInt = |
| 1452 | Constant::getNullValue(Type::getInt32Ty(CI.getContext())); |
| 1453 | unsigned NumZeros = 0; |
| 1454 | while (SrcElTy != DstElTy && |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1455 | isa<CompositeType>(SrcElTy) && !SrcElTy->isPointerTy() && |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1456 | SrcElTy->getNumContainedTypes() /* not "{}" */) { |
| 1457 | SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt); |
| 1458 | ++NumZeros; |
| 1459 | } |
| 1460 | |
| 1461 | // If we found a path from the src to dest, create the getelementptr now. |
| 1462 | if (SrcElTy == DstElTy) { |
| 1463 | SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt); |
| 1464 | return GetElementPtrInst::CreateInBounds(Src, Idxs.begin(), Idxs.end(),"", |
Chris Lattner | 7a34d6c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1465 | ((Instruction*)NULL)); |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1466 | } |
| 1467 | } |
Chris Lattner | e5a1426 | 2010-08-26 21:55:42 +0000 | [diff] [blame] | 1468 | |
| 1469 | // Try to optimize int -> float bitcasts. |
| 1470 | if ((DestTy->isFloatTy() || DestTy->isDoubleTy()) && isa<IntegerType>(SrcTy)) |
| 1471 | if (Instruction *I = OptimizeIntToFloatBitCast(CI, *this)) |
| 1472 | return I; |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1473 | |
| 1474 | if (const VectorType *DestVTy = dyn_cast<VectorType>(DestTy)) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1475 | if (DestVTy->getNumElements() == 1 && !SrcTy->isVectorTy()) { |
Chris Lattner | 7a34d6c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1476 | Value *Elem = Builder->CreateBitCast(Src, DestVTy->getElementType()); |
| 1477 | return InsertElementInst::Create(UndefValue::get(DestTy), Elem, |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1478 | Constant::getNullValue(Type::getInt32Ty(CI.getContext()))); |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1479 | // FIXME: Canonicalize bitcast(insertelement) -> insertelement(bitcast) |
| 1480 | } |
Chris Lattner | 6745191 | 2010-05-08 21:50:26 +0000 | [diff] [blame] | 1481 | |
| 1482 | // If this is a cast from an integer to vector, check to see if the input |
| 1483 | // is a trunc or zext of a bitcast from vector. If so, we can replace all |
| 1484 | // the casts with a shuffle and (potentially) a bitcast. |
| 1485 | if (isa<IntegerType>(SrcTy) && (isa<TruncInst>(Src) || isa<ZExtInst>(Src))){ |
| 1486 | CastInst *SrcCast = cast<CastInst>(Src); |
| 1487 | if (BitCastInst *BCIn = dyn_cast<BitCastInst>(SrcCast->getOperand(0))) |
| 1488 | if (isa<VectorType>(BCIn->getOperand(0)->getType())) |
| 1489 | if (Instruction *I = OptimizeVectorResize(BCIn->getOperand(0), |
| 1490 | cast<VectorType>(DestTy), *this)) |
| 1491 | return I; |
| 1492 | } |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | if (const VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy)) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1496 | if (SrcVTy->getNumElements() == 1 && !DestTy->isVectorTy()) { |
Chris Lattner | 7a34d6c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1497 | Value *Elem = |
| 1498 | Builder->CreateExtractElement(Src, |
| 1499 | Constant::getNullValue(Type::getInt32Ty(CI.getContext()))); |
| 1500 | return CastInst::Create(Instruction::BitCast, Elem, DestTy); |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) { |
Chris Lattner | 7a34d6c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1505 | // Okay, we have (bitcast (shuffle ..)). Check to see if this is |
Dan Gohman | a5ced59 | 2010-04-07 23:22:42 +0000 | [diff] [blame] | 1506 | // a bitcast to a vector with the same # elts. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1507 | if (SVI->hasOneUse() && DestTy->isVectorTy() && |
Chris Lattner | 7a34d6c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1508 | cast<VectorType>(DestTy)->getNumElements() == |
| 1509 | SVI->getType()->getNumElements() && |
| 1510 | SVI->getType()->getNumElements() == |
| 1511 | cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements()) { |
| 1512 | BitCastInst *Tmp; |
| 1513 | // If either of the operands is a cast from CI.getType(), then |
| 1514 | // evaluating the shuffle in the casted destination's type will allow |
| 1515 | // us to eliminate at least one cast. |
| 1516 | if (((Tmp = dyn_cast<BitCastInst>(SVI->getOperand(0))) && |
| 1517 | Tmp->getOperand(0)->getType() == DestTy) || |
| 1518 | ((Tmp = dyn_cast<BitCastInst>(SVI->getOperand(1))) && |
| 1519 | Tmp->getOperand(0)->getType() == DestTy)) { |
| 1520 | Value *LHS = Builder->CreateBitCast(SVI->getOperand(0), DestTy); |
| 1521 | Value *RHS = Builder->CreateBitCast(SVI->getOperand(1), DestTy); |
| 1522 | // Return a new shuffle vector. Use the same element ID's, as we |
| 1523 | // know the vector types match #elts. |
| 1524 | return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2)); |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1525 | } |
| 1526 | } |
| 1527 | } |
Chris Lattner | 7a34d6c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1528 | |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1529 | if (SrcTy->isPointerTy()) |
Chris Lattner | 7a34d6c | 2010-01-05 22:21:18 +0000 | [diff] [blame] | 1530 | return commonPointerCastTransforms(CI); |
| 1531 | return commonCastTransforms(CI); |
Chris Lattner | 80f43d3 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 1532 | } |