blob: 601d9b42f31454c4157c0344a75b6342bde41730 [file] [log] [blame]
Chris Lattner80f43d32010-01-04 07:53:58 +00001//===- 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"
17using namespace llvm;
18using namespace PatternMatch;
19
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +000020/// 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///
24static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale,
Dan Gohman28d2e0a2010-05-28 04:33:04 +000025 uint64_t &Offset) {
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +000026 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
27 Offset = CI->getZExtValue();
28 Scale = 0;
Dan Gohman28d2e0a2010-05-28 04:33:04 +000029 return ConstantInt::get(Val->getType(), 0);
Chris Lattnerf86d7992010-01-05 20:57:30 +000030 }
31
32 if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) {
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +000033 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 Gohman28d2e0a2010-05-28 04:33:04 +000036 Scale = UINT64_C(1) << RHS->getZExtValue();
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +000037 Offset = 0;
38 return I->getOperand(0);
Chris Lattnerf86d7992010-01-05 20:57:30 +000039 }
40
41 if (I->getOpcode() == Instruction::Mul) {
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +000042 // This value is scaled by 'RHS'.
43 Scale = RHS->getZExtValue();
44 Offset = 0;
45 return I->getOperand(0);
Chris Lattnerf86d7992010-01-05 20:57:30 +000046 }
47
48 if (I->getOpcode() == Instruction::Add) {
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +000049 // 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.
69Instruction *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
Chad Rosier66638b22011-06-17 22:08:25 +000074 // Insist that the amount-to-allocate not overflow.
75 OverflowingBinaryOperator *OBI =
76 dyn_cast<OverflowingBinaryOperator>(AI.getOperand(0));
77 if (OBI && !(OBI->hasNoSignedWrap() || OBI->hasNoUnsignedWrap())) return 0;
78
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +000079 const PointerType *PTy = cast<PointerType>(CI.getType());
80
81 BuilderTy AllocaBuilder(*Builder);
82 AllocaBuilder.SetInsertPoint(AI.getParent(), &AI);
83
84 // Get the type really allocated and the type casted to.
85 const Type *AllocElTy = AI.getAllocatedType();
86 const Type *CastElTy = PTy->getElementType();
87 if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0;
88
89 unsigned AllocElTyAlign = TD->getABITypeAlignment(AllocElTy);
90 unsigned CastElTyAlign = TD->getABITypeAlignment(CastElTy);
91 if (CastElTyAlign < AllocElTyAlign) return 0;
92
93 // If the allocation has multiple uses, only promote it if we are strictly
94 // increasing the alignment of the resultant allocation. If we keep it the
Devang Patel5aa3fa62011-03-08 22:12:11 +000095 // same, we open the door to infinite loops of various kinds.
96 if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0;
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +000097
98 uint64_t AllocElTySize = TD->getTypeAllocSize(AllocElTy);
99 uint64_t CastElTySize = TD->getTypeAllocSize(CastElTy);
100 if (CastElTySize == 0 || AllocElTySize == 0) return 0;
101
102 // See if we can satisfy the modulus by pulling a scale out of the array
103 // size argument.
104 unsigned ArraySizeScale;
Dan Gohman28d2e0a2010-05-28 04:33:04 +0000105 uint64_t ArrayOffset;
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +0000106 Value *NumElements = // See if the array size is a decomposable linear expr.
107 DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset);
108
109 // If we can now satisfy the modulus, by using a non-1 scale, we really can
110 // do the xform.
111 if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 ||
112 (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return 0;
113
114 unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize;
115 Value *Amt = 0;
116 if (Scale == 1) {
117 Amt = NumElements;
118 } else {
Dan Gohman28d2e0a2010-05-28 04:33:04 +0000119 Amt = ConstantInt::get(AI.getArraySize()->getType(), Scale);
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +0000120 // Insert before the alloca, not before the cast.
121 Amt = AllocaBuilder.CreateMul(Amt, NumElements, "tmp");
122 }
123
Dan Gohman28d2e0a2010-05-28 04:33:04 +0000124 if (uint64_t Offset = (AllocElTySize*ArrayOffset)/CastElTySize) {
125 Value *Off = ConstantInt::get(AI.getArraySize()->getType(),
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +0000126 Offset, true);
127 Amt = AllocaBuilder.CreateAdd(Amt, Off, "tmp");
128 }
129
130 AllocaInst *New = AllocaBuilder.CreateAlloca(CastElTy, Amt);
131 New->setAlignment(AI.getAlignment());
132 New->takeName(&AI);
133
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +0000134 // If the allocation has multiple real uses, insert a cast and change all
135 // things that used it to use the new cast. This will also hack on CI, but it
136 // will die soon.
Devang Patel5aa3fa62011-03-08 22:12:11 +0000137 if (!AI.hasOneUse()) {
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +0000138 // New is the allocation instruction, pointer typed. AI is the original
139 // allocation instruction, also pointer typed. Thus, cast to use is BitCast.
140 Value *NewCast = AllocaBuilder.CreateBitCast(New, AI.getType(), "tmpcast");
Eli Friedman3e22cb92011-05-18 00:32:01 +0000141 ReplaceInstUsesWith(AI, NewCast);
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +0000142 }
143 return ReplaceInstUsesWith(CI, New);
144}
145
146
Chris Lattnere0e4cc72010-01-06 01:56:21 +0000147
Chris Lattner5f0290e2010-01-04 07:54:59 +0000148/// EvaluateInDifferentType - Given an expression that
Chris Lattner14bf8f02010-01-08 19:19:23 +0000149/// CanEvaluateTruncated or CanEvaluateSExtd returns true for, actually
Chris Lattnere0e4cc72010-01-06 01:56:21 +0000150/// insert the code to evaluate the expression.
Chris Lattner5f0290e2010-01-04 07:54:59 +0000151Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty,
152 bool isSigned) {
Chris Lattnerc8b3fce2010-01-08 19:28:47 +0000153 if (Constant *C = dyn_cast<Constant>(V)) {
154 C = ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/);
155 // If we got a constantexpr back, try to simplify it with TD info.
156 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
157 C = ConstantFoldConstantExpression(CE, TD);
158 return C;
159 }
Chris Lattner5f0290e2010-01-04 07:54:59 +0000160
161 // Otherwise, it must be an instruction.
162 Instruction *I = cast<Instruction>(V);
163 Instruction *Res = 0;
164 unsigned Opc = I->getOpcode();
165 switch (Opc) {
166 case Instruction::Add:
167 case Instruction::Sub:
168 case Instruction::Mul:
169 case Instruction::And:
170 case Instruction::Or:
171 case Instruction::Xor:
172 case Instruction::AShr:
173 case Instruction::LShr:
174 case Instruction::Shl:
175 case Instruction::UDiv:
176 case Instruction::URem: {
177 Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned);
178 Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);
179 Res = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
180 break;
181 }
182 case Instruction::Trunc:
183 case Instruction::ZExt:
184 case Instruction::SExt:
185 // If the source type of the cast is the type we're trying for then we can
186 // just return the source. There's no need to insert it because it is not
187 // new.
188 if (I->getOperand(0)->getType() == Ty)
189 return I->getOperand(0);
190
191 // Otherwise, must be the same type of cast, so just reinsert a new one.
Chris Lattner9ee947c2010-01-10 20:25:54 +0000192 // This also handles the case of zext(trunc(x)) -> zext(x).
193 Res = CastInst::CreateIntegerCast(I->getOperand(0), Ty,
194 Opc == Instruction::SExt);
Chris Lattner5f0290e2010-01-04 07:54:59 +0000195 break;
196 case Instruction::Select: {
197 Value *True = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);
198 Value *False = EvaluateInDifferentType(I->getOperand(2), Ty, isSigned);
199 Res = SelectInst::Create(I->getOperand(0), True, False);
200 break;
201 }
202 case Instruction::PHI: {
203 PHINode *OPN = cast<PHINode>(I);
Jay Foad3ecfc862011-03-30 11:28:46 +0000204 PHINode *NPN = PHINode::Create(Ty, OPN->getNumIncomingValues());
Chris Lattner5f0290e2010-01-04 07:54:59 +0000205 for (unsigned i = 0, e = OPN->getNumIncomingValues(); i != e; ++i) {
206 Value *V =EvaluateInDifferentType(OPN->getIncomingValue(i), Ty, isSigned);
207 NPN->addIncoming(V, OPN->getIncomingBlock(i));
208 }
209 Res = NPN;
210 break;
211 }
212 default:
213 // TODO: Can handle more cases here.
214 llvm_unreachable("Unreachable!");
215 break;
216 }
217
218 Res->takeName(I);
Eli Friedmana311c342011-05-27 00:19:40 +0000219 return InsertNewInstWith(Res, *I);
Chris Lattner5f0290e2010-01-04 07:54:59 +0000220}
Chris Lattner80f43d32010-01-04 07:53:58 +0000221
222
223/// This function is a wrapper around CastInst::isEliminableCastPair. It
224/// simply extracts arguments and returns what that function returns.
225static Instruction::CastOps
226isEliminableCastPair(
227 const CastInst *CI, ///< The first cast instruction
228 unsigned opcode, ///< The opcode of the second cast instruction
229 const Type *DstTy, ///< The target type for the second cast instruction
230 TargetData *TD ///< The target data for pointer size
231) {
232
233 const Type *SrcTy = CI->getOperand(0)->getType(); // A from above
234 const Type *MidTy = CI->getType(); // B from above
235
236 // Get the opcodes of the two Cast instructions
237 Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode());
238 Instruction::CastOps secondOp = Instruction::CastOps(opcode);
239
240 unsigned Res = CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy,
241 DstTy,
242 TD ? TD->getIntPtrType(CI->getContext()) : 0);
243
244 // We don't want to form an inttoptr or ptrtoint that converts to an integer
245 // type that differs from the pointer size.
246 if ((Res == Instruction::IntToPtr &&
247 (!TD || SrcTy != TD->getIntPtrType(CI->getContext()))) ||
248 (Res == Instruction::PtrToInt &&
249 (!TD || DstTy != TD->getIntPtrType(CI->getContext()))))
250 Res = 0;
251
252 return Instruction::CastOps(Res);
253}
254
Chris Lattner8c5ad3a2010-02-11 06:26:33 +0000255/// ShouldOptimizeCast - Return true if the cast from "V to Ty" actually
256/// results in any code being generated and is interesting to optimize out. If
257/// the cast can be eliminated by some other simple transformation, we prefer
258/// to do the simplification first.
259bool InstCombiner::ShouldOptimizeCast(Instruction::CastOps opc, const Value *V,
260 const Type *Ty) {
261 // Noop casts and casts of constants should be eliminated trivially.
Chris Lattner80f43d32010-01-04 07:53:58 +0000262 if (V->getType() == Ty || isa<Constant>(V)) return false;
263
Chris Lattner8c5ad3a2010-02-11 06:26:33 +0000264 // If this is another cast that can be eliminated, we prefer to have it
265 // eliminated.
Chris Lattner80f43d32010-01-04 07:53:58 +0000266 if (const CastInst *CI = dyn_cast<CastInst>(V))
Chris Lattner8c5ad3a2010-02-11 06:26:33 +0000267 if (isEliminableCastPair(CI, opc, Ty, TD))
Chris Lattner80f43d32010-01-04 07:53:58 +0000268 return false;
Chris Lattner8c5ad3a2010-02-11 06:26:33 +0000269
270 // If this is a vector sext from a compare, then we don't want to break the
271 // idiom where each element of the extended vector is either zero or all ones.
Duncan Sands1df98592010-02-16 11:11:14 +0000272 if (opc == Instruction::SExt && isa<CmpInst>(V) && Ty->isVectorTy())
Chris Lattner8c5ad3a2010-02-11 06:26:33 +0000273 return false;
274
Chris Lattner80f43d32010-01-04 07:53:58 +0000275 return true;
276}
277
278
279/// @brief Implement the transforms common to all CastInst visitors.
280Instruction *InstCombiner::commonCastTransforms(CastInst &CI) {
281 Value *Src = CI.getOperand(0);
282
283 // Many cases of "cast of a cast" are eliminable. If it's eliminable we just
284 // eliminate it now.
285 if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast
286 if (Instruction::CastOps opc =
287 isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), TD)) {
288 // The first cast (CSrc) is eliminable so we need to fix up or replace
289 // the second cast (CI). CSrc will then have a good chance of being dead.
290 return CastInst::Create(opc, CSrc->getOperand(0), CI.getType());
291 }
292 }
293
294 // If we are casting a select then fold the cast into the select
295 if (SelectInst *SI = dyn_cast<SelectInst>(Src))
296 if (Instruction *NV = FoldOpIntoSelect(CI, SI))
297 return NV;
298
299 // If we are casting a PHI then fold the cast into the PHI
300 if (isa<PHINode>(Src)) {
301 // We don't do this if this would create a PHI node with an illegal type if
302 // it is currently legal.
Duncan Sands1df98592010-02-16 11:11:14 +0000303 if (!Src->getType()->isIntegerTy() ||
304 !CI.getType()->isIntegerTy() ||
Chris Lattner80f43d32010-01-04 07:53:58 +0000305 ShouldChangeType(CI.getType(), Src->getType()))
306 if (Instruction *NV = FoldOpIntoPhi(CI))
307 return NV;
308 }
309
310 return 0;
311}
312
Chris Lattner75215c92010-01-10 00:58:42 +0000313/// CanEvaluateTruncated - Return true if we can evaluate the specified
314/// expression tree as type Ty instead of its larger type, and arrive with the
315/// same value. This is used by code that tries to eliminate truncates.
316///
317/// Ty will always be a type smaller than V. We should return true if trunc(V)
318/// can be computed by computing V in the smaller type. If V is an instruction,
319/// then trunc(inst(x,y)) can be computed as inst(trunc(x),trunc(y)), which only
320/// makes sense if x and y can be efficiently truncated.
321///
Chris Lattner8cf4f6f2010-01-11 02:43:35 +0000322/// This function works on both vectors and scalars.
323///
Chris Lattner75215c92010-01-10 00:58:42 +0000324static bool CanEvaluateTruncated(Value *V, const Type *Ty) {
325 // We can always evaluate constants in another type.
326 if (isa<Constant>(V))
327 return true;
Chris Lattner68c6e892010-01-05 23:00:30 +0000328
Chris Lattner75215c92010-01-10 00:58:42 +0000329 Instruction *I = dyn_cast<Instruction>(V);
330 if (!I) return false;
331
332 const Type *OrigTy = V->getType();
333
Chris Lattnera958cbf2010-01-11 22:45:25 +0000334 // If this is an extension from the dest type, we can eliminate it, even if it
335 // has multiple uses.
Chris Lattner53af2d12010-01-11 22:49:40 +0000336 if ((isa<ZExtInst>(I) || isa<SExtInst>(I)) &&
Chris Lattner75215c92010-01-10 00:58:42 +0000337 I->getOperand(0)->getType() == Ty)
338 return true;
339
340 // We can't extend or shrink something that has multiple uses: doing so would
341 // require duplicating the instruction in general, which isn't profitable.
342 if (!I->hasOneUse()) return false;
343
344 unsigned Opc = I->getOpcode();
345 switch (Opc) {
346 case Instruction::Add:
347 case Instruction::Sub:
348 case Instruction::Mul:
349 case Instruction::And:
350 case Instruction::Or:
351 case Instruction::Xor:
352 // These operators can all arbitrarily be extended or truncated.
353 return CanEvaluateTruncated(I->getOperand(0), Ty) &&
354 CanEvaluateTruncated(I->getOperand(1), Ty);
355
356 case Instruction::UDiv:
357 case Instruction::URem: {
358 // UDiv and URem can be truncated if all the truncated bits are zero.
359 uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
360 uint32_t BitWidth = Ty->getScalarSizeInBits();
361 if (BitWidth < OrigBitWidth) {
362 APInt Mask = APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth);
363 if (MaskedValueIsZero(I->getOperand(0), Mask) &&
364 MaskedValueIsZero(I->getOperand(1), Mask)) {
365 return CanEvaluateTruncated(I->getOperand(0), Ty) &&
366 CanEvaluateTruncated(I->getOperand(1), Ty);
367 }
368 }
369 break;
370 }
371 case Instruction::Shl:
372 // If we are truncating the result of this SHL, and if it's a shift of a
373 // constant amount, we can always perform a SHL in a smaller type.
374 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
375 uint32_t BitWidth = Ty->getScalarSizeInBits();
376 if (CI->getLimitedValue(BitWidth) < BitWidth)
377 return CanEvaluateTruncated(I->getOperand(0), Ty);
378 }
379 break;
380 case Instruction::LShr:
381 // If this is a truncate of a logical shr, we can truncate it to a smaller
382 // lshr iff we know that the bits we would otherwise be shifting in are
383 // already zeros.
384 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
385 uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
386 uint32_t BitWidth = Ty->getScalarSizeInBits();
387 if (MaskedValueIsZero(I->getOperand(0),
388 APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) &&
389 CI->getLimitedValue(BitWidth) < BitWidth) {
390 return CanEvaluateTruncated(I->getOperand(0), Ty);
391 }
392 }
393 break;
394 case Instruction::Trunc:
395 // trunc(trunc(x)) -> trunc(x)
396 return true;
Chris Lattnerf9d05ab2010-08-27 20:32:06 +0000397 case Instruction::ZExt:
398 case Instruction::SExt:
399 // trunc(ext(x)) -> ext(x) if the source type is smaller than the new dest
400 // trunc(ext(x)) -> trunc(x) if the source type is larger than the new dest
401 return true;
Chris Lattner75215c92010-01-10 00:58:42 +0000402 case Instruction::Select: {
403 SelectInst *SI = cast<SelectInst>(I);
404 return CanEvaluateTruncated(SI->getTrueValue(), Ty) &&
405 CanEvaluateTruncated(SI->getFalseValue(), Ty);
406 }
407 case Instruction::PHI: {
408 // We can change a phi if we can change all operands. Note that we never
409 // get into trouble with cyclic PHIs here because we only consider
410 // instructions with a single use.
411 PHINode *PN = cast<PHINode>(I);
412 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
413 if (!CanEvaluateTruncated(PN->getIncomingValue(i), Ty))
414 return false;
415 return true;
416 }
417 default:
418 // TODO: Can handle more cases here.
419 break;
420 }
421
422 return false;
423}
424
425Instruction *InstCombiner::visitTrunc(TruncInst &CI) {
Chris Lattnerd84dfa42010-01-10 01:00:46 +0000426 if (Instruction *Result = commonCastTransforms(CI))
Chris Lattner75215c92010-01-10 00:58:42 +0000427 return Result;
428
Chris Lattnerd84dfa42010-01-10 01:00:46 +0000429 // See if we can simplify any instructions used by the input whose sole
430 // purpose is to compute bits we don't care about.
431 if (SimplifyDemandedInstructionBits(CI))
432 return &CI;
433
Chris Lattner75215c92010-01-10 00:58:42 +0000434 Value *Src = CI.getOperand(0);
435 const Type *DestTy = CI.getType(), *SrcTy = Src->getType();
436
437 // Attempt to truncate the entire input expression tree to the destination
438 // type. Only do this if the dest type is a simple type, don't convert the
Chris Lattner80f43d32010-01-04 07:53:58 +0000439 // expression tree to something weird like i93 unless the source is also
440 // strange.
Duncan Sands1df98592010-02-16 11:11:14 +0000441 if ((DestTy->isVectorTy() || ShouldChangeType(SrcTy, DestTy)) &&
Chris Lattner75215c92010-01-10 00:58:42 +0000442 CanEvaluateTruncated(Src, DestTy)) {
Chris Lattnere0e4cc72010-01-06 01:56:21 +0000443
Chris Lattner80f43d32010-01-04 07:53:58 +0000444 // If this cast is a truncate, evaluting in a different type always
Chris Lattner68c6e892010-01-05 23:00:30 +0000445 // eliminates the cast, so it is always a win.
Chris Lattner075f6922010-01-07 23:41:00 +0000446 DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type"
Dan Gohman5b71dce2010-05-25 21:50:35 +0000447 " to avoid cast: " << CI << '\n');
Chris Lattner075f6922010-01-07 23:41:00 +0000448 Value *Res = EvaluateInDifferentType(Src, DestTy, false);
449 assert(Res->getType() == DestTy);
450 return ReplaceInstUsesWith(CI, Res);
451 }
Chris Lattner80f43d32010-01-04 07:53:58 +0000452
Chris Lattner7a34d6c2010-01-05 22:21:18 +0000453 // Canonicalize trunc x to i1 -> (icmp ne (and x, 1), 0), likewise for vector.
454 if (DestTy->getScalarSizeInBits() == 1) {
Chris Lattner80f43d32010-01-04 07:53:58 +0000455 Constant *One = ConstantInt::get(Src->getType(), 1);
456 Src = Builder->CreateAnd(Src, One, "tmp");
457 Value *Zero = Constant::getNullValue(Src->getType());
458 return new ICmpInst(ICmpInst::ICMP_NE, Src, Zero);
459 }
Chris Lattner784f3332010-08-27 18:31:05 +0000460
461 // Transform trunc(lshr (zext A), Cst) to eliminate one type conversion.
462 Value *A = 0; ConstantInt *Cst = 0;
Chris Lattner62fe4062011-01-15 06:32:33 +0000463 if (Src->hasOneUse() &&
464 match(Src, m_LShr(m_ZExt(m_Value(A)), m_ConstantInt(Cst)))) {
Chris Lattner784f3332010-08-27 18:31:05 +0000465 // We have three types to worry about here, the type of A, the source of
466 // the truncate (MidSize), and the destination of the truncate. We know that
467 // ASize < MidSize and MidSize > ResultSize, but don't know the relation
468 // between ASize and ResultSize.
469 unsigned ASize = A->getType()->getPrimitiveSizeInBits();
470
471 // If the shift amount is larger than the size of A, then the result is
472 // known to be zero because all the input bits got shifted out.
473 if (Cst->getZExtValue() >= ASize)
474 return ReplaceInstUsesWith(CI, Constant::getNullValue(CI.getType()));
475
476 // Since we're doing an lshr and a zero extend, and know that the shift
477 // amount is smaller than ASize, it is always safe to do the shift in A's
478 // type, then zero extend or truncate to the result.
479 Value *Shift = Builder->CreateLShr(A, Cst->getZExtValue());
480 Shift->takeName(Src);
481 return CastInst::CreateIntegerCast(Shift, CI.getType(), false);
482 }
Chris Lattner62fe4062011-01-15 06:32:33 +0000483
484 // Transform "trunc (and X, cst)" -> "and (trunc X), cst" so long as the dest
485 // type isn't non-native.
486 if (Src->hasOneUse() && isa<IntegerType>(Src->getType()) &&
487 ShouldChangeType(Src->getType(), CI.getType()) &&
488 match(Src, m_And(m_Value(A), m_ConstantInt(Cst)))) {
489 Value *NewTrunc = Builder->CreateTrunc(A, CI.getType(), A->getName()+".tr");
490 return BinaryOperator::CreateAnd(NewTrunc,
491 ConstantExpr::getTrunc(Cst, CI.getType()));
492 }
Chris Lattner80f43d32010-01-04 07:53:58 +0000493
Chris Lattner80f43d32010-01-04 07:53:58 +0000494 return 0;
495}
496
497/// transformZExtICmp - Transform (zext icmp) to bitwise / integer operations
498/// in order to eliminate the icmp.
499Instruction *InstCombiner::transformZExtICmp(ICmpInst *ICI, Instruction &CI,
500 bool DoXform) {
501 // If we are just checking for a icmp eq of a single bit and zext'ing it
502 // to an integer, then shift the bit to the appropriate place and then
503 // cast to integer to avoid the comparison.
504 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
505 const APInt &Op1CV = Op1C->getValue();
506
507 // zext (x <s 0) to i32 --> x>>u31 true if signbit set.
508 // zext (x >s -1) to i32 --> (x>>u31)^1 true if signbit clear.
509 if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) ||
510 (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())) {
511 if (!DoXform) return ICI;
512
513 Value *In = ICI->getOperand(0);
514 Value *Sh = ConstantInt::get(In->getType(),
515 In->getType()->getScalarSizeInBits()-1);
516 In = Builder->CreateLShr(In, Sh, In->getName()+".lobit");
517 if (In->getType() != CI.getType())
518 In = Builder->CreateIntCast(In, CI.getType(), false/*ZExt*/, "tmp");
519
520 if (ICI->getPredicate() == ICmpInst::ICMP_SGT) {
521 Constant *One = ConstantInt::get(In->getType(), 1);
522 In = Builder->CreateXor(In, One, In->getName()+".not");
523 }
524
525 return ReplaceInstUsesWith(CI, In);
526 }
527
528
529
530 // zext (X == 0) to i32 --> X^1 iff X has only the low bit set.
531 // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set.
532 // zext (X == 1) to i32 --> X iff X has only the low bit set.
533 // zext (X == 2) to i32 --> X>>1 iff X has only the 2nd bit set.
534 // zext (X != 0) to i32 --> X iff X has only the low bit set.
535 // zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set.
536 // zext (X != 1) to i32 --> X^1 iff X has only the low bit set.
537 // zext (X != 2) to i32 --> (X>>1)^1 iff X has only the 2nd bit set.
538 if ((Op1CV == 0 || Op1CV.isPowerOf2()) &&
539 // This only works for EQ and NE
540 ICI->isEquality()) {
541 // If Op1C some other power of two, convert:
542 uint32_t BitWidth = Op1C->getType()->getBitWidth();
543 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
544 APInt TypeMask(APInt::getAllOnesValue(BitWidth));
545 ComputeMaskedBits(ICI->getOperand(0), TypeMask, KnownZero, KnownOne);
546
547 APInt KnownZeroMask(~KnownZero);
548 if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1?
549 if (!DoXform) return ICI;
550
551 bool isNE = ICI->getPredicate() == ICmpInst::ICMP_NE;
552 if (Op1CV != 0 && (Op1CV != KnownZeroMask)) {
553 // (X&4) == 2 --> false
554 // (X&4) != 2 --> true
555 Constant *Res = ConstantInt::get(Type::getInt1Ty(CI.getContext()),
556 isNE);
557 Res = ConstantExpr::getZExt(Res, CI.getType());
558 return ReplaceInstUsesWith(CI, Res);
559 }
560
561 uint32_t ShiftAmt = KnownZeroMask.logBase2();
562 Value *In = ICI->getOperand(0);
563 if (ShiftAmt) {
564 // Perform a logical shr by shiftamt.
565 // Insert the shift to put the result in the low bit.
566 In = Builder->CreateLShr(In, ConstantInt::get(In->getType(),ShiftAmt),
567 In->getName()+".lobit");
568 }
569
570 if ((Op1CV != 0) == isNE) { // Toggle the low bit.
571 Constant *One = ConstantInt::get(In->getType(), 1);
572 In = Builder->CreateXor(In, One, "tmp");
573 }
574
575 if (CI.getType() == In->getType())
576 return ReplaceInstUsesWith(CI, In);
Chris Lattner29cc0b32010-08-27 22:24:38 +0000577 return CastInst::CreateIntegerCast(In, CI.getType(), false/*ZExt*/);
Chris Lattner80f43d32010-01-04 07:53:58 +0000578 }
579 }
580 }
581
582 // icmp ne A, B is equal to xor A, B when A and B only really have one bit.
583 // It is also profitable to transform icmp eq into not(xor(A, B)) because that
584 // may lead to additional simplifications.
585 if (ICI->isEquality() && CI.getType() == ICI->getOperand(0)->getType()) {
586 if (const IntegerType *ITy = dyn_cast<IntegerType>(CI.getType())) {
587 uint32_t BitWidth = ITy->getBitWidth();
588 Value *LHS = ICI->getOperand(0);
589 Value *RHS = ICI->getOperand(1);
590
591 APInt KnownZeroLHS(BitWidth, 0), KnownOneLHS(BitWidth, 0);
592 APInt KnownZeroRHS(BitWidth, 0), KnownOneRHS(BitWidth, 0);
593 APInt TypeMask(APInt::getAllOnesValue(BitWidth));
594 ComputeMaskedBits(LHS, TypeMask, KnownZeroLHS, KnownOneLHS);
595 ComputeMaskedBits(RHS, TypeMask, KnownZeroRHS, KnownOneRHS);
596
597 if (KnownZeroLHS == KnownZeroRHS && KnownOneLHS == KnownOneRHS) {
598 APInt KnownBits = KnownZeroLHS | KnownOneLHS;
599 APInt UnknownBit = ~KnownBits;
600 if (UnknownBit.countPopulation() == 1) {
601 if (!DoXform) return ICI;
602
603 Value *Result = Builder->CreateXor(LHS, RHS);
604
605 // Mask off any bits that are set and won't be shifted away.
606 if (KnownOneLHS.uge(UnknownBit))
607 Result = Builder->CreateAnd(Result,
608 ConstantInt::get(ITy, UnknownBit));
609
610 // Shift the bit we're testing down to the lsb.
611 Result = Builder->CreateLShr(
612 Result, ConstantInt::get(ITy, UnknownBit.countTrailingZeros()));
613
614 if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
615 Result = Builder->CreateXor(Result, ConstantInt::get(ITy, 1));
616 Result->takeName(ICI);
617 return ReplaceInstUsesWith(CI, Result);
618 }
619 }
620 }
621 }
622
623 return 0;
624}
625
Chris Lattner75215c92010-01-10 00:58:42 +0000626/// CanEvaluateZExtd - Determine if the specified value can be computed in the
Chris Lattner8cf4f6f2010-01-11 02:43:35 +0000627/// specified wider type and produce the same low bits. If not, return false.
628///
Chris Lattner789162a2010-01-11 03:32:00 +0000629/// If this function returns true, it can also return a non-zero number of bits
630/// (in BitsToClear) which indicates that the value it computes is correct for
631/// the zero extend, but that the additional BitsToClear bits need to be zero'd
632/// out. For example, to promote something like:
633///
634/// %B = trunc i64 %A to i32
635/// %C = lshr i32 %B, 8
636/// %E = zext i32 %C to i64
637///
638/// CanEvaluateZExtd for the 'lshr' will return true, and BitsToClear will be
639/// set to 8 to indicate that the promoted value needs to have bits 24-31
640/// cleared in addition to bits 32-63. Since an 'and' will be generated to
641/// clear the top bits anyway, doing this has no extra cost.
642///
Chris Lattner8cf4f6f2010-01-11 02:43:35 +0000643/// This function works on both vectors and scalars.
Chris Lattner789162a2010-01-11 03:32:00 +0000644static bool CanEvaluateZExtd(Value *V, const Type *Ty, unsigned &BitsToClear) {
645 BitsToClear = 0;
Chris Lattner9e390dd2010-01-10 02:50:04 +0000646 if (isa<Constant>(V))
647 return true;
Chris Lattner75215c92010-01-10 00:58:42 +0000648
649 Instruction *I = dyn_cast<Instruction>(V);
Chris Lattner9e390dd2010-01-10 02:50:04 +0000650 if (!I) return false;
Chris Lattner75215c92010-01-10 00:58:42 +0000651
652 // If the input is a truncate from the destination type, we can trivially
Chris Lattnera958cbf2010-01-11 22:45:25 +0000653 // eliminate it, even if it has multiple uses.
654 // FIXME: This is currently disabled until codegen can handle this without
655 // pessimizing code, PR5997.
656 if (0 && isa<TruncInst>(I) && I->getOperand(0)->getType() == Ty)
Chris Lattner9e390dd2010-01-10 02:50:04 +0000657 return true;
Chris Lattner75215c92010-01-10 00:58:42 +0000658
659 // We can't extend or shrink something that has multiple uses: doing so would
660 // require duplicating the instruction in general, which isn't profitable.
Chris Lattner9e390dd2010-01-10 02:50:04 +0000661 if (!I->hasOneUse()) return false;
Chris Lattner75215c92010-01-10 00:58:42 +0000662
Chris Lattner789162a2010-01-11 03:32:00 +0000663 unsigned Opc = I->getOpcode(), Tmp;
Chris Lattner75215c92010-01-10 00:58:42 +0000664 switch (Opc) {
Chris Lattner9ee947c2010-01-10 20:25:54 +0000665 case Instruction::ZExt: // zext(zext(x)) -> zext(x).
666 case Instruction::SExt: // zext(sext(x)) -> sext(x).
667 case Instruction::Trunc: // zext(trunc(x)) -> trunc(x) or zext(x)
668 return true;
Chris Lattner75215c92010-01-10 00:58:42 +0000669 case Instruction::And:
Chris Lattner75215c92010-01-10 00:58:42 +0000670 case Instruction::Or:
671 case Instruction::Xor:
Chris Lattner75215c92010-01-10 00:58:42 +0000672 case Instruction::Add:
673 case Instruction::Sub:
674 case Instruction::Mul:
Chris Lattnerd26c9e12010-01-10 02:22:12 +0000675 case Instruction::Shl:
Chris Lattner789162a2010-01-11 03:32:00 +0000676 if (!CanEvaluateZExtd(I->getOperand(0), Ty, BitsToClear) ||
677 !CanEvaluateZExtd(I->getOperand(1), Ty, Tmp))
678 return false;
679 // These can all be promoted if neither operand has 'bits to clear'.
680 if (BitsToClear == 0 && Tmp == 0)
681 return true;
Chris Lattner75215c92010-01-10 00:58:42 +0000682
Chris Lattner7acc4b12010-01-11 04:05:13 +0000683 // If the operation is an AND/OR/XOR and the bits to clear are zero in the
684 // other side, BitsToClear is ok.
685 if (Tmp == 0 &&
686 (Opc == Instruction::And || Opc == Instruction::Or ||
687 Opc == Instruction::Xor)) {
688 // We use MaskedValueIsZero here for generality, but the case we care
689 // about the most is constant RHS.
690 unsigned VSize = V->getType()->getScalarSizeInBits();
691 if (MaskedValueIsZero(I->getOperand(1),
692 APInt::getHighBitsSet(VSize, BitsToClear)))
693 return true;
694 }
695
696 // Otherwise, we don't know how to analyze this BitsToClear case yet.
Chris Lattner789162a2010-01-11 03:32:00 +0000697 return false;
Chris Lattnerd26c9e12010-01-10 02:22:12 +0000698
Chris Lattner789162a2010-01-11 03:32:00 +0000699 case Instruction::LShr:
700 // We can promote lshr(x, cst) if we can promote x. This requires the
701 // ultimate 'and' to clear out the high zero bits we're clearing out though.
702 if (ConstantInt *Amt = dyn_cast<ConstantInt>(I->getOperand(1))) {
703 if (!CanEvaluateZExtd(I->getOperand(0), Ty, BitsToClear))
704 return false;
705 BitsToClear += Amt->getZExtValue();
706 if (BitsToClear > V->getType()->getScalarSizeInBits())
707 BitsToClear = V->getType()->getScalarSizeInBits();
708 return true;
709 }
710 // Cannot promote variable LSHR.
711 return false;
Chris Lattner75215c92010-01-10 00:58:42 +0000712 case Instruction::Select:
Chris Lattner789162a2010-01-11 03:32:00 +0000713 if (!CanEvaluateZExtd(I->getOperand(1), Ty, Tmp) ||
714 !CanEvaluateZExtd(I->getOperand(2), Ty, BitsToClear) ||
Chris Lattner7acc4b12010-01-11 04:05:13 +0000715 // TODO: If important, we could handle the case when the BitsToClear are
716 // known zero in the disagreeing side.
Chris Lattner789162a2010-01-11 03:32:00 +0000717 Tmp != BitsToClear)
718 return false;
719 return true;
Chris Lattner75215c92010-01-10 00:58:42 +0000720
721 case Instruction::PHI: {
722 // We can change a phi if we can change all operands. Note that we never
723 // get into trouble with cyclic PHIs here because we only consider
724 // instructions with a single use.
725 PHINode *PN = cast<PHINode>(I);
Chris Lattner789162a2010-01-11 03:32:00 +0000726 if (!CanEvaluateZExtd(PN->getIncomingValue(0), Ty, BitsToClear))
727 return false;
Chris Lattner9e390dd2010-01-10 02:50:04 +0000728 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i)
Chris Lattner789162a2010-01-11 03:32:00 +0000729 if (!CanEvaluateZExtd(PN->getIncomingValue(i), Ty, Tmp) ||
Chris Lattner7acc4b12010-01-11 04:05:13 +0000730 // TODO: If important, we could handle the case when the BitsToClear
731 // are known zero in the disagreeing input.
Chris Lattner789162a2010-01-11 03:32:00 +0000732 Tmp != BitsToClear)
733 return false;
Chris Lattner9e390dd2010-01-10 02:50:04 +0000734 return true;
Chris Lattner75215c92010-01-10 00:58:42 +0000735 }
736 default:
737 // TODO: Can handle more cases here.
Chris Lattner9e390dd2010-01-10 02:50:04 +0000738 return false;
Chris Lattner75215c92010-01-10 00:58:42 +0000739 }
740}
741
Chris Lattner80f43d32010-01-04 07:53:58 +0000742Instruction *InstCombiner::visitZExt(ZExtInst &CI) {
Chris Lattner5324d802010-01-10 02:39:31 +0000743 // If this zero extend is only used by a truncate, let the truncate by
744 // eliminated before we try to optimize this zext.
745 if (CI.hasOneUse() && isa<TruncInst>(CI.use_back()))
746 return 0;
747
Chris Lattner80f43d32010-01-04 07:53:58 +0000748 // If one of the common conversion will work, do it.
Chris Lattnerd84dfa42010-01-10 01:00:46 +0000749 if (Instruction *Result = commonCastTransforms(CI))
Chris Lattner80f43d32010-01-04 07:53:58 +0000750 return Result;
751
Chris Lattnerd84dfa42010-01-10 01:00:46 +0000752 // See if we can simplify any instructions used by the input whose sole
753 // purpose is to compute bits we don't care about.
754 if (SimplifyDemandedInstructionBits(CI))
755 return &CI;
Chris Lattner75215c92010-01-10 00:58:42 +0000756
Chris Lattnerd84dfa42010-01-10 01:00:46 +0000757 Value *Src = CI.getOperand(0);
Chris Lattner75215c92010-01-10 00:58:42 +0000758 const Type *SrcTy = Src->getType(), *DestTy = CI.getType();
759
760 // Attempt to extend the entire input expression tree to the destination
761 // type. Only do this if the dest type is a simple type, don't convert the
762 // expression tree to something weird like i93 unless the source is also
763 // strange.
Chris Lattner789162a2010-01-11 03:32:00 +0000764 unsigned BitsToClear;
Duncan Sands1df98592010-02-16 11:11:14 +0000765 if ((DestTy->isVectorTy() || ShouldChangeType(SrcTy, DestTy)) &&
Chris Lattner789162a2010-01-11 03:32:00 +0000766 CanEvaluateZExtd(Src, DestTy, BitsToClear)) {
767 assert(BitsToClear < SrcTy->getScalarSizeInBits() &&
768 "Unreasonable BitsToClear");
769
Chris Lattner5324d802010-01-10 02:39:31 +0000770 // Okay, we can transform this! Insert the new expression now.
771 DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type"
772 " to avoid zero extend: " << CI);
773 Value *Res = EvaluateInDifferentType(Src, DestTy, false);
774 assert(Res->getType() == DestTy);
775
Chris Lattner789162a2010-01-11 03:32:00 +0000776 uint32_t SrcBitsKept = SrcTy->getScalarSizeInBits()-BitsToClear;
777 uint32_t DestBitSize = DestTy->getScalarSizeInBits();
778
Chris Lattner5324d802010-01-10 02:39:31 +0000779 // If the high bits are already filled with zeros, just replace this
780 // cast with the result.
Chris Lattner9e390dd2010-01-10 02:50:04 +0000781 if (MaskedValueIsZero(Res, APInt::getHighBitsSet(DestBitSize,
Chris Lattner789162a2010-01-11 03:32:00 +0000782 DestBitSize-SrcBitsKept)))
Chris Lattner5324d802010-01-10 02:39:31 +0000783 return ReplaceInstUsesWith(CI, Res);
784
785 // We need to emit an AND to clear the high bits.
Chris Lattner9ee947c2010-01-10 20:25:54 +0000786 Constant *C = ConstantInt::get(Res->getType(),
Chris Lattner789162a2010-01-11 03:32:00 +0000787 APInt::getLowBitsSet(DestBitSize, SrcBitsKept));
Chris Lattner5324d802010-01-10 02:39:31 +0000788 return BinaryOperator::CreateAnd(Res, C);
Chris Lattner75215c92010-01-10 00:58:42 +0000789 }
Chris Lattner80f43d32010-01-04 07:53:58 +0000790
791 // If this is a TRUNC followed by a ZEXT then we are dealing with integral
792 // types and if the sizes are just right we can convert this into a logical
793 // 'and' which will be much cheaper than the pair of casts.
794 if (TruncInst *CSrc = dyn_cast<TruncInst>(Src)) { // A->B->C cast
Chris Lattnerf4fb9112010-01-10 07:08:30 +0000795 // TODO: Subsume this into EvaluateInDifferentType.
796
Chris Lattner80f43d32010-01-04 07:53:58 +0000797 // Get the sizes of the types involved. We know that the intermediate type
798 // will be smaller than A or C, but don't know the relation between A and C.
799 Value *A = CSrc->getOperand(0);
800 unsigned SrcSize = A->getType()->getScalarSizeInBits();
801 unsigned MidSize = CSrc->getType()->getScalarSizeInBits();
802 unsigned DstSize = CI.getType()->getScalarSizeInBits();
803 // If we're actually extending zero bits, then if
804 // SrcSize < DstSize: zext(a & mask)
805 // SrcSize == DstSize: a & mask
806 // SrcSize > DstSize: trunc(a) & mask
807 if (SrcSize < DstSize) {
808 APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
809 Constant *AndConst = ConstantInt::get(A->getType(), AndValue);
810 Value *And = Builder->CreateAnd(A, AndConst, CSrc->getName()+".mask");
811 return new ZExtInst(And, CI.getType());
812 }
813
814 if (SrcSize == DstSize) {
815 APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
816 return BinaryOperator::CreateAnd(A, ConstantInt::get(A->getType(),
817 AndValue));
818 }
819 if (SrcSize > DstSize) {
820 Value *Trunc = Builder->CreateTrunc(A, CI.getType(), "tmp");
821 APInt AndValue(APInt::getLowBitsSet(DstSize, MidSize));
822 return BinaryOperator::CreateAnd(Trunc,
823 ConstantInt::get(Trunc->getType(),
Chris Lattnerf4fb9112010-01-10 07:08:30 +0000824 AndValue));
Chris Lattner80f43d32010-01-04 07:53:58 +0000825 }
826 }
827
828 if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src))
829 return transformZExtICmp(ICI, CI);
830
831 BinaryOperator *SrcI = dyn_cast<BinaryOperator>(Src);
832 if (SrcI && SrcI->getOpcode() == Instruction::Or) {
833 // zext (or icmp, icmp) --> or (zext icmp), (zext icmp) if at least one
834 // of the (zext icmp) will be transformed.
835 ICmpInst *LHS = dyn_cast<ICmpInst>(SrcI->getOperand(0));
836 ICmpInst *RHS = dyn_cast<ICmpInst>(SrcI->getOperand(1));
837 if (LHS && RHS && LHS->hasOneUse() && RHS->hasOneUse() &&
838 (transformZExtICmp(LHS, CI, false) ||
839 transformZExtICmp(RHS, CI, false))) {
840 Value *LCast = Builder->CreateZExt(LHS, CI.getType(), LHS->getName());
841 Value *RCast = Builder->CreateZExt(RHS, CI.getType(), RHS->getName());
842 return BinaryOperator::Create(Instruction::Or, LCast, RCast);
843 }
844 }
845
846 // zext(trunc(t) & C) -> (t & zext(C)).
847 if (SrcI && SrcI->getOpcode() == Instruction::And && SrcI->hasOneUse())
848 if (ConstantInt *C = dyn_cast<ConstantInt>(SrcI->getOperand(1)))
849 if (TruncInst *TI = dyn_cast<TruncInst>(SrcI->getOperand(0))) {
850 Value *TI0 = TI->getOperand(0);
851 if (TI0->getType() == CI.getType())
852 return
853 BinaryOperator::CreateAnd(TI0,
854 ConstantExpr::getZExt(C, CI.getType()));
855 }
856
857 // zext((trunc(t) & C) ^ C) -> ((t & zext(C)) ^ zext(C)).
858 if (SrcI && SrcI->getOpcode() == Instruction::Xor && SrcI->hasOneUse())
859 if (ConstantInt *C = dyn_cast<ConstantInt>(SrcI->getOperand(1)))
860 if (BinaryOperator *And = dyn_cast<BinaryOperator>(SrcI->getOperand(0)))
861 if (And->getOpcode() == Instruction::And && And->hasOneUse() &&
862 And->getOperand(1) == C)
863 if (TruncInst *TI = dyn_cast<TruncInst>(And->getOperand(0))) {
864 Value *TI0 = TI->getOperand(0);
865 if (TI0->getType() == CI.getType()) {
866 Constant *ZC = ConstantExpr::getZExt(C, CI.getType());
867 Value *NewAnd = Builder->CreateAnd(TI0, ZC, "tmp");
868 return BinaryOperator::CreateXor(NewAnd, ZC);
869 }
870 }
871
Chris Lattner718bf3f2010-01-05 21:04:47 +0000872 // zext (xor i1 X, true) to i32 --> xor (zext i1 X to i32), 1
873 Value *X;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000874 if (SrcI && SrcI->hasOneUse() && SrcI->getType()->isIntegerTy(1) &&
Chris Lattner49bdfef2010-01-05 21:11:17 +0000875 match(SrcI, m_Not(m_Value(X))) &&
Chris Lattner718bf3f2010-01-05 21:04:47 +0000876 (!X->hasOneUse() || !isa<CmpInst>(X))) {
877 Value *New = Builder->CreateZExt(X, CI.getType());
878 return BinaryOperator::CreateXor(New, ConstantInt::get(CI.getType(), 1));
879 }
880
Chris Lattner80f43d32010-01-04 07:53:58 +0000881 return 0;
882}
883
Benjamin Kramer0a30c422011-04-01 20:09:03 +0000884/// transformSExtICmp - Transform (sext icmp) to bitwise / integer operations
885/// in order to eliminate the icmp.
886Instruction *InstCombiner::transformSExtICmp(ICmpInst *ICI, Instruction &CI) {
887 Value *Op0 = ICI->getOperand(0), *Op1 = ICI->getOperand(1);
888 ICmpInst::Predicate Pred = ICI->getPredicate();
889
890 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Benjamin Kramer406a6502011-04-01 22:29:18 +0000891 // (x <s 0) ? -1 : 0 -> ashr x, 31 -> all ones if negative
892 // (x >s -1) ? -1 : 0 -> not (ashr x, 31) -> all ones if positive
Benjamin Kramer0a30c422011-04-01 20:09:03 +0000893 if ((Pred == ICmpInst::ICMP_SLT && Op1C->isZero()) ||
894 (Pred == ICmpInst::ICMP_SGT && Op1C->isAllOnesValue())) {
895
896 Value *Sh = ConstantInt::get(Op0->getType(),
897 Op0->getType()->getScalarSizeInBits()-1);
898 Value *In = Builder->CreateAShr(Op0, Sh, Op0->getName()+".lobit");
899 if (In->getType() != CI.getType())
900 In = Builder->CreateIntCast(In, CI.getType(), true/*SExt*/, "tmp");
901
902 if (Pred == ICmpInst::ICMP_SGT)
903 In = Builder->CreateNot(In, In->getName()+".not");
904 return ReplaceInstUsesWith(CI, In);
905 }
Benjamin Kramer0baa94a2011-04-01 20:09:10 +0000906
907 // If we know that only one bit of the LHS of the icmp can be set and we
908 // have an equality comparison with zero or a power of 2, we can transform
909 // the icmp and sext into bitwise/integer operations.
Benjamin Kramer5337fab2011-04-01 22:22:11 +0000910 if (ICI->hasOneUse() &&
911 ICI->isEquality() && (Op1C->isZero() || Op1C->getValue().isPowerOf2())){
Benjamin Kramer0baa94a2011-04-01 20:09:10 +0000912 unsigned BitWidth = Op1C->getType()->getBitWidth();
913 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
914 APInt TypeMask(APInt::getAllOnesValue(BitWidth));
915 ComputeMaskedBits(Op0, TypeMask, KnownZero, KnownOne);
916
Benjamin Kramerce1498b2011-04-01 20:15:16 +0000917 APInt KnownZeroMask(~KnownZero);
918 if (KnownZeroMask.isPowerOf2()) {
Benjamin Kramer0baa94a2011-04-01 20:09:10 +0000919 Value *In = ICI->getOperand(0);
920
Benjamin Kramerf5b75932011-04-02 18:50:58 +0000921 // If the icmp tests for a known zero bit we can constant fold it.
922 if (!Op1C->isZero() && Op1C->getValue() != KnownZeroMask) {
923 Value *V = Pred == ICmpInst::ICMP_NE ?
924 ConstantInt::getAllOnesValue(CI.getType()) :
925 ConstantInt::getNullValue(CI.getType());
926 return ReplaceInstUsesWith(CI, V);
927 }
Benjamin Kramer5337fab2011-04-01 22:22:11 +0000928
Benjamin Kramer0baa94a2011-04-01 20:09:10 +0000929 if (!Op1C->isZero() == (Pred == ICmpInst::ICMP_NE)) {
930 // sext ((x & 2^n) == 0) -> (x >> n) - 1
931 // sext ((x & 2^n) != 2^n) -> (x >> n) - 1
932 unsigned ShiftAmt = KnownZeroMask.countTrailingZeros();
933 // Perform a right shift to place the desired bit in the LSB.
934 if (ShiftAmt)
935 In = Builder->CreateLShr(In,
936 ConstantInt::get(In->getType(), ShiftAmt));
937
938 // At this point "In" is either 1 or 0. Subtract 1 to turn
939 // {1, 0} -> {0, -1}.
940 In = Builder->CreateAdd(In,
941 ConstantInt::getAllOnesValue(In->getType()),
942 "sext");
943 } else {
944 // sext ((x & 2^n) != 0) -> (x << bitwidth-n) a>> bitwidth-1
Benjamin Kramer5337fab2011-04-01 22:22:11 +0000945 // sext ((x & 2^n) == 2^n) -> (x << bitwidth-n) a>> bitwidth-1
Benjamin Kramer0baa94a2011-04-01 20:09:10 +0000946 unsigned ShiftAmt = KnownZeroMask.countLeadingZeros();
947 // Perform a left shift to place the desired bit in the MSB.
948 if (ShiftAmt)
949 In = Builder->CreateShl(In,
950 ConstantInt::get(In->getType(), ShiftAmt));
951
952 // Distribute the bit over the whole bit width.
953 In = Builder->CreateAShr(In, ConstantInt::get(In->getType(),
954 BitWidth - 1), "sext");
955 }
956
957 if (CI.getType() == In->getType())
958 return ReplaceInstUsesWith(CI, In);
959 return CastInst::CreateIntegerCast(In, CI.getType(), true/*SExt*/);
960 }
961 }
Benjamin Kramer0a30c422011-04-01 20:09:03 +0000962 }
963
964 // vector (x <s 0) ? -1 : 0 -> ashr x, 31 -> all ones if signed.
965 if (const VectorType *VTy = dyn_cast<VectorType>(CI.getType())) {
966 if (Pred == ICmpInst::ICMP_SLT && match(Op1, m_Zero()) &&
967 Op0->getType() == CI.getType()) {
968 const Type *EltTy = VTy->getElementType();
969
970 // splat the shift constant to a constant vector.
971 Constant *VSh = ConstantInt::get(VTy, EltTy->getScalarSizeInBits()-1);
972 Value *In = Builder->CreateAShr(Op0, VSh, Op0->getName()+".lobit");
973 return ReplaceInstUsesWith(CI, In);
974 }
975 }
976
977 return 0;
978}
979
Chris Lattner75215c92010-01-10 00:58:42 +0000980/// CanEvaluateSExtd - Return true if we can take the specified value
981/// and return it as type Ty without inserting any new casts and without
982/// changing the value of the common low bits. This is used by code that tries
983/// to promote integer operations to a wider types will allow us to eliminate
984/// the extension.
985///
Chris Lattneraa9c8942010-01-10 07:57:20 +0000986/// This function works on both vectors and scalars.
Chris Lattner75215c92010-01-10 00:58:42 +0000987///
Chris Lattner8cf4f6f2010-01-11 02:43:35 +0000988static bool CanEvaluateSExtd(Value *V, const Type *Ty) {
Chris Lattner75215c92010-01-10 00:58:42 +0000989 assert(V->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits() &&
990 "Can't sign extend type to a smaller type");
Chris Lattneraa9c8942010-01-10 07:57:20 +0000991 // If this is a constant, it can be trivially promoted.
992 if (isa<Constant>(V))
993 return true;
Chris Lattner75215c92010-01-10 00:58:42 +0000994
995 Instruction *I = dyn_cast<Instruction>(V);
Chris Lattneraa9c8942010-01-10 07:57:20 +0000996 if (!I) return false;
Chris Lattner75215c92010-01-10 00:58:42 +0000997
Chris Lattnera958cbf2010-01-11 22:45:25 +0000998 // If this is a truncate from the dest type, we can trivially eliminate it,
999 // even if it has multiple uses.
1000 // FIXME: This is currently disabled until codegen can handle this without
1001 // pessimizing code, PR5997.
1002 if (0 && isa<TruncInst>(I) && I->getOperand(0)->getType() == Ty)
Chris Lattneraa9c8942010-01-10 07:57:20 +00001003 return true;
Chris Lattner75215c92010-01-10 00:58:42 +00001004
1005 // We can't extend or shrink something that has multiple uses: doing so would
1006 // require duplicating the instruction in general, which isn't profitable.
Chris Lattneraa9c8942010-01-10 07:57:20 +00001007 if (!I->hasOneUse()) return false;
Chris Lattner75215c92010-01-10 00:58:42 +00001008
Chris Lattneraa9c8942010-01-10 07:57:20 +00001009 switch (I->getOpcode()) {
Chris Lattner11ea8122010-01-10 20:30:41 +00001010 case Instruction::SExt: // sext(sext(x)) -> sext(x)
1011 case Instruction::ZExt: // sext(zext(x)) -> zext(x)
1012 case Instruction::Trunc: // sext(trunc(x)) -> trunc(x) or sext(x)
1013 return true;
Chris Lattner75215c92010-01-10 00:58:42 +00001014 case Instruction::And:
1015 case Instruction::Or:
1016 case Instruction::Xor:
Chris Lattner75215c92010-01-10 00:58:42 +00001017 case Instruction::Add:
1018 case Instruction::Sub:
Chris Lattner75215c92010-01-10 00:58:42 +00001019 case Instruction::Mul:
Chris Lattneraa9c8942010-01-10 07:57:20 +00001020 // These operators can all arbitrarily be extended if their inputs can.
Chris Lattner8cf4f6f2010-01-11 02:43:35 +00001021 return CanEvaluateSExtd(I->getOperand(0), Ty) &&
1022 CanEvaluateSExtd(I->getOperand(1), Ty);
Chris Lattner75215c92010-01-10 00:58:42 +00001023
1024 //case Instruction::Shl: TODO
1025 //case Instruction::LShr: TODO
Chris Lattner75215c92010-01-10 00:58:42 +00001026
Chris Lattneraa9c8942010-01-10 07:57:20 +00001027 case Instruction::Select:
Chris Lattner8cf4f6f2010-01-11 02:43:35 +00001028 return CanEvaluateSExtd(I->getOperand(1), Ty) &&
1029 CanEvaluateSExtd(I->getOperand(2), Ty);
Chris Lattner9ee947c2010-01-10 20:25:54 +00001030
Chris Lattner75215c92010-01-10 00:58:42 +00001031 case Instruction::PHI: {
1032 // We can change a phi if we can change all operands. Note that we never
1033 // get into trouble with cyclic PHIs here because we only consider
1034 // instructions with a single use.
1035 PHINode *PN = cast<PHINode>(I);
Chris Lattner9ee947c2010-01-10 20:25:54 +00001036 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
Chris Lattner8cf4f6f2010-01-11 02:43:35 +00001037 if (!CanEvaluateSExtd(PN->getIncomingValue(i), Ty)) return false;
Chris Lattneraa9c8942010-01-10 07:57:20 +00001038 return true;
Chris Lattner75215c92010-01-10 00:58:42 +00001039 }
1040 default:
1041 // TODO: Can handle more cases here.
1042 break;
1043 }
1044
Chris Lattneraa9c8942010-01-10 07:57:20 +00001045 return false;
Chris Lattner75215c92010-01-10 00:58:42 +00001046}
1047
Chris Lattner80f43d32010-01-04 07:53:58 +00001048Instruction *InstCombiner::visitSExt(SExtInst &CI) {
Chris Lattner5324d802010-01-10 02:39:31 +00001049 // If this sign extend is only used by a truncate, let the truncate by
1050 // eliminated before we try to optimize this zext.
1051 if (CI.hasOneUse() && isa<TruncInst>(CI.use_back()))
1052 return 0;
1053
Chris Lattnerd84dfa42010-01-10 01:00:46 +00001054 if (Instruction *I = commonCastTransforms(CI))
Chris Lattner80f43d32010-01-04 07:53:58 +00001055 return I;
1056
Chris Lattnerd84dfa42010-01-10 01:00:46 +00001057 // See if we can simplify any instructions used by the input whose sole
1058 // purpose is to compute bits we don't care about.
1059 if (SimplifyDemandedInstructionBits(CI))
1060 return &CI;
1061
Chris Lattner80f43d32010-01-04 07:53:58 +00001062 Value *Src = CI.getOperand(0);
Chris Lattner75215c92010-01-10 00:58:42 +00001063 const Type *SrcTy = Src->getType(), *DestTy = CI.getType();
1064
Chris Lattner75215c92010-01-10 00:58:42 +00001065 // Attempt to extend the entire input expression tree to the destination
1066 // type. Only do this if the dest type is a simple type, don't convert the
1067 // expression tree to something weird like i93 unless the source is also
1068 // strange.
Duncan Sands1df98592010-02-16 11:11:14 +00001069 if ((DestTy->isVectorTy() || ShouldChangeType(SrcTy, DestTy)) &&
Chris Lattner8cf4f6f2010-01-11 02:43:35 +00001070 CanEvaluateSExtd(Src, DestTy)) {
Chris Lattnerdde5ee52010-01-10 07:40:50 +00001071 // Okay, we can transform this! Insert the new expression now.
1072 DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type"
1073 " to avoid sign extend: " << CI);
1074 Value *Res = EvaluateInDifferentType(Src, DestTy, true);
1075 assert(Res->getType() == DestTy);
1076
Chris Lattner75215c92010-01-10 00:58:42 +00001077 uint32_t SrcBitSize = SrcTy->getScalarSizeInBits();
1078 uint32_t DestBitSize = DestTy->getScalarSizeInBits();
Chris Lattnerdde5ee52010-01-10 07:40:50 +00001079
1080 // If the high bits are already filled with sign bit, just replace this
1081 // cast with the result.
Chris Lattneraa9c8942010-01-10 07:57:20 +00001082 if (ComputeNumSignBits(Res) > DestBitSize - SrcBitSize)
Chris Lattnerdde5ee52010-01-10 07:40:50 +00001083 return ReplaceInstUsesWith(CI, Res);
Chris Lattner75215c92010-01-10 00:58:42 +00001084
Chris Lattnerdde5ee52010-01-10 07:40:50 +00001085 // We need to emit a shl + ashr to do the sign extend.
1086 Value *ShAmt = ConstantInt::get(DestTy, DestBitSize-SrcBitSize);
1087 return BinaryOperator::CreateAShr(Builder->CreateShl(Res, ShAmt, "sext"),
1088 ShAmt);
Chris Lattner75215c92010-01-10 00:58:42 +00001089 }
Chris Lattner80f43d32010-01-04 07:53:58 +00001090
Chris Lattnercd5adbb2010-01-18 22:19:16 +00001091 // If this input is a trunc from our destination, then turn sext(trunc(x))
1092 // into shifts.
1093 if (TruncInst *TI = dyn_cast<TruncInst>(Src))
1094 if (TI->hasOneUse() && TI->getOperand(0)->getType() == DestTy) {
1095 uint32_t SrcBitSize = SrcTy->getScalarSizeInBits();
1096 uint32_t DestBitSize = DestTy->getScalarSizeInBits();
1097
1098 // We need to emit a shl + ashr to do the sign extend.
1099 Value *ShAmt = ConstantInt::get(DestTy, DestBitSize-SrcBitSize);
1100 Value *Res = Builder->CreateShl(TI->getOperand(0), ShAmt, "sext");
1101 return BinaryOperator::CreateAShr(Res, ShAmt);
1102 }
Nate Begeman9a3dc552010-12-17 23:12:19 +00001103
Benjamin Kramer0a30c422011-04-01 20:09:03 +00001104 if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src))
1105 return transformSExtICmp(ICI, CI);
Bill Wendling2d0537c2010-12-17 23:27:41 +00001106
Chris Lattner80f43d32010-01-04 07:53:58 +00001107 // If the input is a shl/ashr pair of a same constant, then this is a sign
1108 // extension from a smaller value. If we could trust arbitrary bitwidth
1109 // integers, we could turn this into a truncate to the smaller bit and then
1110 // use a sext for the whole extension. Since we don't, look deeper and check
1111 // for a truncate. If the source and dest are the same type, eliminate the
1112 // trunc and extend and just do shifts. For example, turn:
1113 // %a = trunc i32 %i to i8
1114 // %b = shl i8 %a, 6
1115 // %c = ashr i8 %b, 6
1116 // %d = sext i8 %c to i32
1117 // into:
1118 // %a = shl i32 %i, 30
1119 // %d = ashr i32 %a, 30
1120 Value *A = 0;
Chris Lattner4f379782010-01-10 01:04:31 +00001121 // TODO: Eventually this could be subsumed by EvaluateInDifferentType.
Chris Lattner80f43d32010-01-04 07:53:58 +00001122 ConstantInt *BA = 0, *CA = 0;
Chris Lattner4f379782010-01-10 01:04:31 +00001123 if (match(Src, m_AShr(m_Shl(m_Trunc(m_Value(A)), m_ConstantInt(BA)),
Chris Lattner80f43d32010-01-04 07:53:58 +00001124 m_ConstantInt(CA))) &&
Chris Lattner4f379782010-01-10 01:04:31 +00001125 BA == CA && A->getType() == CI.getType()) {
1126 unsigned MidSize = Src->getType()->getScalarSizeInBits();
1127 unsigned SrcDstSize = CI.getType()->getScalarSizeInBits();
1128 unsigned ShAmt = CA->getZExtValue()+SrcDstSize-MidSize;
1129 Constant *ShAmtV = ConstantInt::get(CI.getType(), ShAmt);
1130 A = Builder->CreateShl(A, ShAmtV, CI.getName());
1131 return BinaryOperator::CreateAShr(A, ShAmtV);
Chris Lattner80f43d32010-01-04 07:53:58 +00001132 }
1133
1134 return 0;
1135}
1136
1137
1138/// FitsInFPType - Return a Constant* for the specified FP constant if it fits
1139/// in the specified FP type without changing its value.
1140static Constant *FitsInFPType(ConstantFP *CFP, const fltSemantics &Sem) {
1141 bool losesInfo;
1142 APFloat F = CFP->getValueAPF();
1143 (void)F.convert(Sem, APFloat::rmNearestTiesToEven, &losesInfo);
1144 if (!losesInfo)
1145 return ConstantFP::get(CFP->getContext(), F);
1146 return 0;
1147}
1148
1149/// LookThroughFPExtensions - If this is an fp extension instruction, look
1150/// through it until we get the source value.
1151static Value *LookThroughFPExtensions(Value *V) {
1152 if (Instruction *I = dyn_cast<Instruction>(V))
1153 if (I->getOpcode() == Instruction::FPExt)
1154 return LookThroughFPExtensions(I->getOperand(0));
1155
1156 // If this value is a constant, return the constant in the smallest FP type
1157 // that can accurately represent it. This allows us to turn
1158 // (float)((double)X+2.0) into x+2.0f.
1159 if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
1160 if (CFP->getType() == Type::getPPC_FP128Ty(V->getContext()))
1161 return V; // No constant folding of this.
1162 // See if the value can be truncated to float and then reextended.
1163 if (Value *V = FitsInFPType(CFP, APFloat::IEEEsingle))
1164 return V;
Benjamin Kramerf0127052010-01-05 13:12:22 +00001165 if (CFP->getType()->isDoubleTy())
Chris Lattner80f43d32010-01-04 07:53:58 +00001166 return V; // Won't shrink.
1167 if (Value *V = FitsInFPType(CFP, APFloat::IEEEdouble))
1168 return V;
1169 // Don't try to shrink to various long double types.
1170 }
1171
1172 return V;
1173}
1174
1175Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) {
1176 if (Instruction *I = commonCastTransforms(CI))
1177 return I;
1178
1179 // If we have fptrunc(fadd (fpextend x), (fpextend y)), where x and y are
1180 // smaller than the destination type, we can eliminate the truncate by doing
1181 // the add as the smaller type. This applies to fadd/fsub/fmul/fdiv as well
1182 // as many builtins (sqrt, etc).
1183 BinaryOperator *OpI = dyn_cast<BinaryOperator>(CI.getOperand(0));
1184 if (OpI && OpI->hasOneUse()) {
1185 switch (OpI->getOpcode()) {
1186 default: break;
1187 case Instruction::FAdd:
1188 case Instruction::FSub:
1189 case Instruction::FMul:
1190 case Instruction::FDiv:
1191 case Instruction::FRem:
1192 const Type *SrcTy = OpI->getType();
1193 Value *LHSTrunc = LookThroughFPExtensions(OpI->getOperand(0));
1194 Value *RHSTrunc = LookThroughFPExtensions(OpI->getOperand(1));
1195 if (LHSTrunc->getType() != SrcTy &&
1196 RHSTrunc->getType() != SrcTy) {
1197 unsigned DstSize = CI.getType()->getScalarSizeInBits();
1198 // If the source types were both smaller than the destination type of
1199 // the cast, do this xform.
1200 if (LHSTrunc->getType()->getScalarSizeInBits() <= DstSize &&
1201 RHSTrunc->getType()->getScalarSizeInBits() <= DstSize) {
1202 LHSTrunc = Builder->CreateFPExt(LHSTrunc, CI.getType());
1203 RHSTrunc = Builder->CreateFPExt(RHSTrunc, CI.getType());
1204 return BinaryOperator::Create(OpI->getOpcode(), LHSTrunc, RHSTrunc);
1205 }
1206 }
1207 break;
1208 }
1209 }
Owen Andersond9029012010-07-19 08:09:34 +00001210
1211 // Fold (fptrunc (sqrt (fpext x))) -> (sqrtf x)
1212 // NOTE: This should be disabled by -fno-builtin-sqrt if we ever support it.
1213 CallInst *Call = dyn_cast<CallInst>(CI.getOperand(0));
1214 if (Call && Call->getCalledFunction() &&
1215 Call->getCalledFunction()->getName() == "sqrt" &&
1216 Call->getNumArgOperands() == 1) {
1217 CastInst *Arg = dyn_cast<CastInst>(Call->getArgOperand(0));
1218 if (Arg && Arg->getOpcode() == Instruction::FPExt &&
Owen Anderson5f23a932010-07-19 19:23:32 +00001219 CI.getType()->isFloatTy() &&
1220 Call->getType()->isDoubleTy() &&
1221 Arg->getType()->isDoubleTy() &&
1222 Arg->getOperand(0)->getType()->isFloatTy()) {
1223 Function *Callee = Call->getCalledFunction();
1224 Module *M = CI.getParent()->getParent()->getParent();
Chris Lattner979ed442010-09-07 20:01:38 +00001225 Constant *SqrtfFunc = M->getOrInsertFunction("sqrtf",
Owen Anderson5f23a932010-07-19 19:23:32 +00001226 Callee->getAttributes(),
Owen Andersond9029012010-07-19 08:09:34 +00001227 Builder->getFloatTy(),
1228 Builder->getFloatTy(),
1229 NULL);
1230 CallInst *ret = CallInst::Create(SqrtfFunc, Arg->getOperand(0),
1231 "sqrtfcall");
Owen Anderson5f23a932010-07-19 19:23:32 +00001232 ret->setAttributes(Callee->getAttributes());
Chris Lattner979ed442010-09-07 20:01:38 +00001233
1234
1235 // Remove the old Call. With -fmath-errno, it won't get marked readnone.
Eli Friedman3e22cb92011-05-18 00:32:01 +00001236 ReplaceInstUsesWith(*Call, UndefValue::get(Call->getType()));
Chris Lattner979ed442010-09-07 20:01:38 +00001237 EraseInstFromFunction(*Call);
Owen Andersond9029012010-07-19 08:09:34 +00001238 return ret;
1239 }
1240 }
1241
Chris Lattner80f43d32010-01-04 07:53:58 +00001242 return 0;
1243}
1244
1245Instruction *InstCombiner::visitFPExt(CastInst &CI) {
1246 return commonCastTransforms(CI);
1247}
1248
1249Instruction *InstCombiner::visitFPToUI(FPToUIInst &FI) {
1250 Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0));
1251 if (OpI == 0)
1252 return commonCastTransforms(FI);
1253
1254 // fptoui(uitofp(X)) --> X
1255 // fptoui(sitofp(X)) --> X
1256 // This is safe if the intermediate type has enough bits in its mantissa to
1257 // accurately represent all values of X. For example, do not do this with
1258 // i64->float->i64. This is also safe for sitofp case, because any negative
1259 // 'X' value would cause an undefined result for the fptoui.
1260 if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) &&
1261 OpI->getOperand(0)->getType() == FI.getType() &&
1262 (int)FI.getType()->getScalarSizeInBits() < /*extra bit for sign */
1263 OpI->getType()->getFPMantissaWidth())
1264 return ReplaceInstUsesWith(FI, OpI->getOperand(0));
1265
1266 return commonCastTransforms(FI);
1267}
1268
1269Instruction *InstCombiner::visitFPToSI(FPToSIInst &FI) {
1270 Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0));
1271 if (OpI == 0)
1272 return commonCastTransforms(FI);
1273
1274 // fptosi(sitofp(X)) --> X
1275 // fptosi(uitofp(X)) --> X
1276 // This is safe if the intermediate type has enough bits in its mantissa to
1277 // accurately represent all values of X. For example, do not do this with
1278 // i64->float->i64. This is also safe for sitofp case, because any negative
1279 // 'X' value would cause an undefined result for the fptoui.
1280 if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) &&
1281 OpI->getOperand(0)->getType() == FI.getType() &&
1282 (int)FI.getType()->getScalarSizeInBits() <=
1283 OpI->getType()->getFPMantissaWidth())
1284 return ReplaceInstUsesWith(FI, OpI->getOperand(0));
1285
1286 return commonCastTransforms(FI);
1287}
1288
1289Instruction *InstCombiner::visitUIToFP(CastInst &CI) {
1290 return commonCastTransforms(CI);
1291}
1292
1293Instruction *InstCombiner::visitSIToFP(CastInst &CI) {
1294 return commonCastTransforms(CI);
1295}
1296
Chris Lattner80f43d32010-01-04 07:53:58 +00001297Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) {
Dan Gohman3b5487e2010-02-02 01:44:02 +00001298 // If the source integer type is not the intptr_t type for this target, do a
1299 // trunc or zext to the intptr_t type, then inttoptr of it. This allows the
1300 // cast to be exposed to other transforms.
1301 if (TD) {
1302 if (CI.getOperand(0)->getType()->getScalarSizeInBits() >
1303 TD->getPointerSizeInBits()) {
1304 Value *P = Builder->CreateTrunc(CI.getOperand(0),
1305 TD->getIntPtrType(CI.getContext()), "tmp");
1306 return new IntToPtrInst(P, CI.getType());
1307 }
1308 if (CI.getOperand(0)->getType()->getScalarSizeInBits() <
1309 TD->getPointerSizeInBits()) {
1310 Value *P = Builder->CreateZExt(CI.getOperand(0),
1311 TD->getIntPtrType(CI.getContext()), "tmp");
1312 return new IntToPtrInst(P, CI.getType());
1313 }
Chris Lattner80f43d32010-01-04 07:53:58 +00001314 }
1315
1316 if (Instruction *I = commonCastTransforms(CI))
1317 return I;
1318
1319 return 0;
1320}
1321
Chris Lattner7a34d6c2010-01-05 22:21:18 +00001322/// @brief Implement the transforms for cast of pointer (bitcast/ptrtoint)
1323Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) {
1324 Value *Src = CI.getOperand(0);
1325
1326 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) {
1327 // If casting the result of a getelementptr instruction with no offset, turn
1328 // this into a cast of the original pointer!
1329 if (GEP->hasAllZeroIndices()) {
1330 // Changing the cast operand is usually not a good idea but it is safe
1331 // here because the pointer operand is being replaced with another
1332 // pointer operand so the opcode doesn't need to change.
1333 Worklist.Add(GEP);
1334 CI.setOperand(0, GEP->getOperand(0));
1335 return &CI;
1336 }
1337
1338 // If the GEP has a single use, and the base pointer is a bitcast, and the
1339 // GEP computes a constant offset, see if we can convert these three
1340 // instructions into fewer. This typically happens with unions and other
1341 // non-type-safe code.
1342 if (TD && GEP->hasOneUse() && isa<BitCastInst>(GEP->getOperand(0)) &&
1343 GEP->hasAllConstantIndices()) {
1344 // We are guaranteed to get a constant from EmitGEPOffset.
1345 ConstantInt *OffsetV = cast<ConstantInt>(EmitGEPOffset(GEP));
1346 int64_t Offset = OffsetV->getSExtValue();
1347
1348 // Get the base pointer input of the bitcast, and the type it points to.
1349 Value *OrigBase = cast<BitCastInst>(GEP->getOperand(0))->getOperand(0);
1350 const Type *GEPIdxTy =
1351 cast<PointerType>(OrigBase->getType())->getElementType();
1352 SmallVector<Value*, 8> NewIndices;
1353 if (FindElementAtOffset(GEPIdxTy, Offset, NewIndices)) {
1354 // If we were able to index down into an element, create the GEP
1355 // and bitcast the result. This eliminates one bitcast, potentially
1356 // two.
1357 Value *NGEP = cast<GEPOperator>(GEP)->isInBounds() ?
1358 Builder->CreateInBoundsGEP(OrigBase,
1359 NewIndices.begin(), NewIndices.end()) :
1360 Builder->CreateGEP(OrigBase, NewIndices.begin(), NewIndices.end());
1361 NGEP->takeName(GEP);
1362
1363 if (isa<BitCastInst>(CI))
1364 return new BitCastInst(NGEP, CI.getType());
1365 assert(isa<PtrToIntInst>(CI));
1366 return new PtrToIntInst(NGEP, CI.getType());
1367 }
1368 }
1369 }
1370
1371 return commonCastTransforms(CI);
1372}
1373
1374Instruction *InstCombiner::visitPtrToInt(PtrToIntInst &CI) {
Dan Gohman3b5487e2010-02-02 01:44:02 +00001375 // If the destination integer type is not the intptr_t type for this target,
1376 // do a ptrtoint to intptr_t then do a trunc or zext. This allows the cast
1377 // to be exposed to other transforms.
1378 if (TD) {
1379 if (CI.getType()->getScalarSizeInBits() < TD->getPointerSizeInBits()) {
1380 Value *P = Builder->CreatePtrToInt(CI.getOperand(0),
1381 TD->getIntPtrType(CI.getContext()),
1382 "tmp");
1383 return new TruncInst(P, CI.getType());
1384 }
1385 if (CI.getType()->getScalarSizeInBits() > TD->getPointerSizeInBits()) {
1386 Value *P = Builder->CreatePtrToInt(CI.getOperand(0),
1387 TD->getIntPtrType(CI.getContext()),
1388 "tmp");
1389 return new ZExtInst(P, CI.getType());
1390 }
Chris Lattner7a34d6c2010-01-05 22:21:18 +00001391 }
1392
1393 return commonPointerCastTransforms(CI);
1394}
1395
Chris Lattner67451912010-05-08 21:50:26 +00001396/// OptimizeVectorResize - This input value (which is known to have vector type)
1397/// is being zero extended or truncated to the specified vector type. Try to
1398/// replace it with a shuffle (and vector/vector bitcast) if possible.
1399///
1400/// The source and destination vector types may have different element types.
1401static Instruction *OptimizeVectorResize(Value *InVal, const VectorType *DestTy,
1402 InstCombiner &IC) {
1403 // We can only do this optimization if the output is a multiple of the input
1404 // element size, or the input is a multiple of the output element size.
1405 // Convert the input type to have the same element type as the output.
1406 const VectorType *SrcTy = cast<VectorType>(InVal->getType());
1407
1408 if (SrcTy->getElementType() != DestTy->getElementType()) {
1409 // The input types don't need to be identical, but for now they must be the
1410 // same size. There is no specific reason we couldn't handle things like
1411 // <4 x i16> -> <4 x i32> by bitcasting to <2 x i32> but haven't gotten
1412 // there yet.
1413 if (SrcTy->getElementType()->getPrimitiveSizeInBits() !=
1414 DestTy->getElementType()->getPrimitiveSizeInBits())
1415 return 0;
1416
1417 SrcTy = VectorType::get(DestTy->getElementType(), SrcTy->getNumElements());
1418 InVal = IC.Builder->CreateBitCast(InVal, SrcTy);
1419 }
1420
1421 // Now that the element types match, get the shuffle mask and RHS of the
1422 // shuffle to use, which depends on whether we're increasing or decreasing the
1423 // size of the input.
1424 SmallVector<Constant*, 16> ShuffleMask;
1425 Value *V2;
1426 const IntegerType *Int32Ty = Type::getInt32Ty(SrcTy->getContext());
1427
1428 if (SrcTy->getNumElements() > DestTy->getNumElements()) {
1429 // If we're shrinking the number of elements, just shuffle in the low
1430 // elements from the input and use undef as the second shuffle input.
1431 V2 = UndefValue::get(SrcTy);
1432 for (unsigned i = 0, e = DestTy->getNumElements(); i != e; ++i)
1433 ShuffleMask.push_back(ConstantInt::get(Int32Ty, i));
1434
1435 } else {
1436 // If we're increasing the number of elements, shuffle in all of the
1437 // elements from InVal and fill the rest of the result elements with zeros
1438 // from a constant zero.
1439 V2 = Constant::getNullValue(SrcTy);
1440 unsigned SrcElts = SrcTy->getNumElements();
1441 for (unsigned i = 0, e = SrcElts; i != e; ++i)
1442 ShuffleMask.push_back(ConstantInt::get(Int32Ty, i));
1443
1444 // The excess elements reference the first element of the zero input.
1445 ShuffleMask.append(DestTy->getNumElements()-SrcElts,
1446 ConstantInt::get(Int32Ty, SrcElts));
1447 }
1448
Chris Lattner2ca5c862011-02-15 00:14:00 +00001449 return new ShuffleVectorInst(InVal, V2, ConstantVector::get(ShuffleMask));
Chris Lattner67451912010-05-08 21:50:26 +00001450}
1451
Chris Lattner3dd08732010-08-28 01:20:38 +00001452static bool isMultipleOfTypeSize(unsigned Value, const Type *Ty) {
1453 return Value % Ty->getPrimitiveSizeInBits() == 0;
1454}
1455
Chris Lattner79007792010-08-28 01:50:57 +00001456static unsigned getTypeSizeIndex(unsigned Value, const Type *Ty) {
Chris Lattner3dd08732010-08-28 01:20:38 +00001457 return Value / Ty->getPrimitiveSizeInBits();
1458}
1459
1460/// CollectInsertionElements - V is a value which is inserted into a vector of
1461/// VecEltTy. Look through the value to see if we can decompose it into
1462/// insertions into the vector. See the example in the comment for
1463/// OptimizeIntegerToVectorInsertions for the pattern this handles.
1464/// The type of V is always a non-zero multiple of VecEltTy's size.
1465///
1466/// This returns false if the pattern can't be matched or true if it can,
1467/// filling in Elements with the elements found here.
1468static bool CollectInsertionElements(Value *V, unsigned ElementIndex,
1469 SmallVectorImpl<Value*> &Elements,
1470 const Type *VecEltTy) {
Chris Lattner157d4ea2010-08-28 03:36:51 +00001471 // Undef values never contribute useful bits to the result.
1472 if (isa<UndefValue>(V)) return true;
1473
Chris Lattner3dd08732010-08-28 01:20:38 +00001474 // If we got down to a value of the right type, we win, try inserting into the
1475 // right element.
1476 if (V->getType() == VecEltTy) {
Chris Lattner79007792010-08-28 01:50:57 +00001477 // Inserting null doesn't actually insert any elements.
1478 if (Constant *C = dyn_cast<Constant>(V))
1479 if (C->isNullValue())
1480 return true;
1481
Chris Lattner3dd08732010-08-28 01:20:38 +00001482 // Fail if multiple elements are inserted into this slot.
1483 if (ElementIndex >= Elements.size() || Elements[ElementIndex] != 0)
1484 return false;
1485
1486 Elements[ElementIndex] = V;
1487 return true;
1488 }
1489
Chris Lattner79007792010-08-28 01:50:57 +00001490 if (Constant *C = dyn_cast<Constant>(V)) {
Chris Lattner3dd08732010-08-28 01:20:38 +00001491 // Figure out the # elements this provides, and bitcast it or slice it up
1492 // as required.
Chris Lattner79007792010-08-28 01:50:57 +00001493 unsigned NumElts = getTypeSizeIndex(C->getType()->getPrimitiveSizeInBits(),
1494 VecEltTy);
1495 // If the constant is the size of a vector element, we just need to bitcast
1496 // it to the right type so it gets properly inserted.
1497 if (NumElts == 1)
1498 return CollectInsertionElements(ConstantExpr::getBitCast(C, VecEltTy),
1499 ElementIndex, Elements, VecEltTy);
1500
1501 // Okay, this is a constant that covers multiple elements. Slice it up into
1502 // pieces and insert each element-sized piece into the vector.
1503 if (!isa<IntegerType>(C->getType()))
1504 C = ConstantExpr::getBitCast(C, IntegerType::get(V->getContext(),
1505 C->getType()->getPrimitiveSizeInBits()));
1506 unsigned ElementSize = VecEltTy->getPrimitiveSizeInBits();
1507 const Type *ElementIntTy = IntegerType::get(C->getContext(), ElementSize);
1508
1509 for (unsigned i = 0; i != NumElts; ++i) {
1510 Constant *Piece = ConstantExpr::getLShr(C, ConstantInt::get(C->getType(),
1511 i*ElementSize));
1512 Piece = ConstantExpr::getTrunc(Piece, ElementIntTy);
1513 if (!CollectInsertionElements(Piece, ElementIndex+i, Elements, VecEltTy))
1514 return false;
1515 }
1516 return true;
1517 }
Chris Lattner3dd08732010-08-28 01:20:38 +00001518
1519 if (!V->hasOneUse()) return false;
1520
1521 Instruction *I = dyn_cast<Instruction>(V);
1522 if (I == 0) return false;
1523 switch (I->getOpcode()) {
1524 default: return false; // Unhandled case.
1525 case Instruction::BitCast:
1526 return CollectInsertionElements(I->getOperand(0), ElementIndex,
1527 Elements, VecEltTy);
1528 case Instruction::ZExt:
1529 if (!isMultipleOfTypeSize(
1530 I->getOperand(0)->getType()->getPrimitiveSizeInBits(),
1531 VecEltTy))
1532 return false;
1533 return CollectInsertionElements(I->getOperand(0), ElementIndex,
1534 Elements, VecEltTy);
1535 case Instruction::Or:
1536 return CollectInsertionElements(I->getOperand(0), ElementIndex,
1537 Elements, VecEltTy) &&
1538 CollectInsertionElements(I->getOperand(1), ElementIndex,
1539 Elements, VecEltTy);
1540 case Instruction::Shl: {
1541 // Must be shifting by a constant that is a multiple of the element size.
1542 ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1));
1543 if (CI == 0) return false;
1544 if (!isMultipleOfTypeSize(CI->getZExtValue(), VecEltTy)) return false;
1545 unsigned IndexShift = getTypeSizeIndex(CI->getZExtValue(), VecEltTy);
1546
1547 return CollectInsertionElements(I->getOperand(0), ElementIndex+IndexShift,
1548 Elements, VecEltTy);
1549 }
1550
1551 }
1552}
1553
1554
1555/// OptimizeIntegerToVectorInsertions - If the input is an 'or' instruction, we
1556/// may be doing shifts and ors to assemble the elements of the vector manually.
1557/// Try to rip the code out and replace it with insertelements. This is to
1558/// optimize code like this:
1559///
1560/// %tmp37 = bitcast float %inc to i32
1561/// %tmp38 = zext i32 %tmp37 to i64
1562/// %tmp31 = bitcast float %inc5 to i32
1563/// %tmp32 = zext i32 %tmp31 to i64
1564/// %tmp33 = shl i64 %tmp32, 32
1565/// %ins35 = or i64 %tmp33, %tmp38
1566/// %tmp43 = bitcast i64 %ins35 to <2 x float>
1567///
1568/// Into two insertelements that do "buildvector{%inc, %inc5}".
1569static Value *OptimizeIntegerToVectorInsertions(BitCastInst &CI,
1570 InstCombiner &IC) {
1571 const VectorType *DestVecTy = cast<VectorType>(CI.getType());
1572 Value *IntInput = CI.getOperand(0);
1573
1574 SmallVector<Value*, 8> Elements(DestVecTy->getNumElements());
1575 if (!CollectInsertionElements(IntInput, 0, Elements,
1576 DestVecTy->getElementType()))
1577 return 0;
1578
1579 // If we succeeded, we know that all of the element are specified by Elements
1580 // or are zero if Elements has a null entry. Recast this as a set of
1581 // insertions.
1582 Value *Result = Constant::getNullValue(CI.getType());
1583 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
1584 if (Elements[i] == 0) continue; // Unset element.
1585
1586 Result = IC.Builder->CreateInsertElement(Result, Elements[i],
1587 IC.Builder->getInt32(i));
1588 }
1589
1590 return Result;
1591}
1592
1593
Chris Lattnere5a14262010-08-26 21:55:42 +00001594/// OptimizeIntToFloatBitCast - See if we can optimize an integer->float/double
1595/// bitcast. The various long double bitcasts can't get in here.
Chris Lattner26dbe7e2010-08-26 22:14:59 +00001596static Instruction *OptimizeIntToFloatBitCast(BitCastInst &CI,InstCombiner &IC){
Chris Lattnere5a14262010-08-26 21:55:42 +00001597 Value *Src = CI.getOperand(0);
Chris Lattner26dbe7e2010-08-26 22:14:59 +00001598 const Type *DestTy = CI.getType();
Chris Lattnere5a14262010-08-26 21:55:42 +00001599
1600 // If this is a bitcast from int to float, check to see if the int is an
1601 // extraction from a vector.
1602 Value *VecInput = 0;
Chris Lattner26dbe7e2010-08-26 22:14:59 +00001603 // bitcast(trunc(bitcast(somevector)))
Chris Lattnere5a14262010-08-26 21:55:42 +00001604 if (match(Src, m_Trunc(m_BitCast(m_Value(VecInput)))) &&
1605 isa<VectorType>(VecInput->getType())) {
1606 const VectorType *VecTy = cast<VectorType>(VecInput->getType());
Chris Lattner26dbe7e2010-08-26 22:14:59 +00001607 unsigned DestWidth = DestTy->getPrimitiveSizeInBits();
1608
1609 if (VecTy->getPrimitiveSizeInBits() % DestWidth == 0) {
1610 // If the element type of the vector doesn't match the result type,
1611 // bitcast it to be a vector type we can extract from.
1612 if (VecTy->getElementType() != DestTy) {
1613 VecTy = VectorType::get(DestTy,
1614 VecTy->getPrimitiveSizeInBits() / DestWidth);
1615 VecInput = IC.Builder->CreateBitCast(VecInput, VecTy);
1616 }
Chris Lattnere5a14262010-08-26 21:55:42 +00001617
Chris Lattnere5a14262010-08-26 21:55:42 +00001618 return ExtractElementInst::Create(VecInput, IC.Builder->getInt32(0));
Chris Lattner26dbe7e2010-08-26 22:14:59 +00001619 }
Chris Lattnere5a14262010-08-26 21:55:42 +00001620 }
1621
Chris Lattner26dbe7e2010-08-26 22:14:59 +00001622 // bitcast(trunc(lshr(bitcast(somevector), cst))
1623 ConstantInt *ShAmt = 0;
1624 if (match(Src, m_Trunc(m_LShr(m_BitCast(m_Value(VecInput)),
1625 m_ConstantInt(ShAmt)))) &&
1626 isa<VectorType>(VecInput->getType())) {
1627 const VectorType *VecTy = cast<VectorType>(VecInput->getType());
1628 unsigned DestWidth = DestTy->getPrimitiveSizeInBits();
1629 if (VecTy->getPrimitiveSizeInBits() % DestWidth == 0 &&
1630 ShAmt->getZExtValue() % DestWidth == 0) {
1631 // If the element type of the vector doesn't match the result type,
1632 // bitcast it to be a vector type we can extract from.
1633 if (VecTy->getElementType() != DestTy) {
1634 VecTy = VectorType::get(DestTy,
1635 VecTy->getPrimitiveSizeInBits() / DestWidth);
1636 VecInput = IC.Builder->CreateBitCast(VecInput, VecTy);
1637 }
1638
1639 unsigned Elt = ShAmt->getZExtValue() / DestWidth;
1640 return ExtractElementInst::Create(VecInput, IC.Builder->getInt32(Elt));
1641 }
1642 }
Chris Lattnere5a14262010-08-26 21:55:42 +00001643 return 0;
1644}
Chris Lattner67451912010-05-08 21:50:26 +00001645
Chris Lattner80f43d32010-01-04 07:53:58 +00001646Instruction *InstCombiner::visitBitCast(BitCastInst &CI) {
1647 // If the operands are integer typed then apply the integer transforms,
1648 // otherwise just apply the common ones.
1649 Value *Src = CI.getOperand(0);
1650 const Type *SrcTy = Src->getType();
1651 const Type *DestTy = CI.getType();
1652
Chris Lattner80f43d32010-01-04 07:53:58 +00001653 // Get rid of casts from one type to the same type. These are useless and can
1654 // be replaced by the operand.
1655 if (DestTy == Src->getType())
1656 return ReplaceInstUsesWith(CI, Src);
1657
1658 if (const PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) {
1659 const PointerType *SrcPTy = cast<PointerType>(SrcTy);
1660 const Type *DstElTy = DstPTy->getElementType();
1661 const Type *SrcElTy = SrcPTy->getElementType();
1662
1663 // If the address spaces don't match, don't eliminate the bitcast, which is
1664 // required for changing types.
1665 if (SrcPTy->getAddressSpace() != DstPTy->getAddressSpace())
1666 return 0;
1667
1668 // If we are casting a alloca to a pointer to a type of the same
1669 // size, rewrite the allocation instruction to allocate the "right" type.
1670 // There is no need to modify malloc calls because it is their bitcast that
1671 // needs to be cleaned up.
1672 if (AllocaInst *AI = dyn_cast<AllocaInst>(Src))
1673 if (Instruction *V = PromoteCastOfAllocation(CI, *AI))
1674 return V;
1675
1676 // If the source and destination are pointers, and this cast is equivalent
1677 // to a getelementptr X, 0, 0, 0... turn it into the appropriate gep.
1678 // This can enhance SROA and other transforms that want type-safe pointers.
1679 Constant *ZeroUInt =
1680 Constant::getNullValue(Type::getInt32Ty(CI.getContext()));
1681 unsigned NumZeros = 0;
1682 while (SrcElTy != DstElTy &&
Duncan Sands1df98592010-02-16 11:11:14 +00001683 isa<CompositeType>(SrcElTy) && !SrcElTy->isPointerTy() &&
Chris Lattner80f43d32010-01-04 07:53:58 +00001684 SrcElTy->getNumContainedTypes() /* not "{}" */) {
1685 SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt);
1686 ++NumZeros;
1687 }
1688
1689 // If we found a path from the src to dest, create the getelementptr now.
1690 if (SrcElTy == DstElTy) {
1691 SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt);
Eli Friedman107ffd52011-05-18 23:11:30 +00001692 return GetElementPtrInst::CreateInBounds(Src, Idxs.begin(), Idxs.end());
Chris Lattner80f43d32010-01-04 07:53:58 +00001693 }
1694 }
Chris Lattnere5a14262010-08-26 21:55:42 +00001695
1696 // Try to optimize int -> float bitcasts.
1697 if ((DestTy->isFloatTy() || DestTy->isDoubleTy()) && isa<IntegerType>(SrcTy))
1698 if (Instruction *I = OptimizeIntToFloatBitCast(CI, *this))
1699 return I;
Chris Lattner80f43d32010-01-04 07:53:58 +00001700
1701 if (const VectorType *DestVTy = dyn_cast<VectorType>(DestTy)) {
Duncan Sands1df98592010-02-16 11:11:14 +00001702 if (DestVTy->getNumElements() == 1 && !SrcTy->isVectorTy()) {
Chris Lattner7a34d6c2010-01-05 22:21:18 +00001703 Value *Elem = Builder->CreateBitCast(Src, DestVTy->getElementType());
1704 return InsertElementInst::Create(UndefValue::get(DestTy), Elem,
Chris Lattner80f43d32010-01-04 07:53:58 +00001705 Constant::getNullValue(Type::getInt32Ty(CI.getContext())));
Chris Lattner80f43d32010-01-04 07:53:58 +00001706 // FIXME: Canonicalize bitcast(insertelement) -> insertelement(bitcast)
1707 }
Chris Lattner67451912010-05-08 21:50:26 +00001708
Chris Lattner3dd08732010-08-28 01:20:38 +00001709 if (isa<IntegerType>(SrcTy)) {
1710 // If this is a cast from an integer to vector, check to see if the input
1711 // is a trunc or zext of a bitcast from vector. If so, we can replace all
1712 // the casts with a shuffle and (potentially) a bitcast.
1713 if (isa<TruncInst>(Src) || isa<ZExtInst>(Src)) {
1714 CastInst *SrcCast = cast<CastInst>(Src);
1715 if (BitCastInst *BCIn = dyn_cast<BitCastInst>(SrcCast->getOperand(0)))
1716 if (isa<VectorType>(BCIn->getOperand(0)->getType()))
1717 if (Instruction *I = OptimizeVectorResize(BCIn->getOperand(0),
Chris Lattner67451912010-05-08 21:50:26 +00001718 cast<VectorType>(DestTy), *this))
Chris Lattner3dd08732010-08-28 01:20:38 +00001719 return I;
1720 }
1721
1722 // If the input is an 'or' instruction, we may be doing shifts and ors to
1723 // assemble the elements of the vector manually. Try to rip the code out
1724 // and replace it with insertelements.
1725 if (Value *V = OptimizeIntegerToVectorInsertions(CI, *this))
1726 return ReplaceInstUsesWith(CI, V);
Chris Lattner67451912010-05-08 21:50:26 +00001727 }
Chris Lattner80f43d32010-01-04 07:53:58 +00001728 }
1729
1730 if (const VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy)) {
Duncan Sands1df98592010-02-16 11:11:14 +00001731 if (SrcVTy->getNumElements() == 1 && !DestTy->isVectorTy()) {
Chris Lattner7a34d6c2010-01-05 22:21:18 +00001732 Value *Elem =
1733 Builder->CreateExtractElement(Src,
1734 Constant::getNullValue(Type::getInt32Ty(CI.getContext())));
1735 return CastInst::Create(Instruction::BitCast, Elem, DestTy);
Chris Lattner80f43d32010-01-04 07:53:58 +00001736 }
1737 }
1738
1739 if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) {
Chris Lattner7a34d6c2010-01-05 22:21:18 +00001740 // Okay, we have (bitcast (shuffle ..)). Check to see if this is
Dan Gohmana5ced592010-04-07 23:22:42 +00001741 // a bitcast to a vector with the same # elts.
Duncan Sands1df98592010-02-16 11:11:14 +00001742 if (SVI->hasOneUse() && DestTy->isVectorTy() &&
Chris Lattner7a34d6c2010-01-05 22:21:18 +00001743 cast<VectorType>(DestTy)->getNumElements() ==
1744 SVI->getType()->getNumElements() &&
1745 SVI->getType()->getNumElements() ==
1746 cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements()) {
1747 BitCastInst *Tmp;
1748 // If either of the operands is a cast from CI.getType(), then
1749 // evaluating the shuffle in the casted destination's type will allow
1750 // us to eliminate at least one cast.
1751 if (((Tmp = dyn_cast<BitCastInst>(SVI->getOperand(0))) &&
1752 Tmp->getOperand(0)->getType() == DestTy) ||
1753 ((Tmp = dyn_cast<BitCastInst>(SVI->getOperand(1))) &&
1754 Tmp->getOperand(0)->getType() == DestTy)) {
1755 Value *LHS = Builder->CreateBitCast(SVI->getOperand(0), DestTy);
1756 Value *RHS = Builder->CreateBitCast(SVI->getOperand(1), DestTy);
1757 // Return a new shuffle vector. Use the same element ID's, as we
1758 // know the vector types match #elts.
1759 return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2));
Chris Lattner80f43d32010-01-04 07:53:58 +00001760 }
1761 }
1762 }
Chris Lattner7a34d6c2010-01-05 22:21:18 +00001763
Duncan Sands1df98592010-02-16 11:11:14 +00001764 if (SrcTy->isPointerTy())
Chris Lattner7a34d6c2010-01-05 22:21:18 +00001765 return commonPointerCastTransforms(CI);
1766 return commonCastTransforms(CI);
Chris Lattner80f43d32010-01-04 07:53:58 +00001767}