blob: 48a747d764af38b93748f5d78eff64557d41b77d [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
Stuart Hastings3761c342011-06-17 20:21:52 +000032 // Insist that the amount-to-allocate not overflow.
33 OverflowingBinaryOperator *OBI = dyn_cast<OverflowingBinaryOperator>(Val);
34 if (OBI && !OBI->hasNoUnsignedWrap()) return 0;
35
Chris Lattnerf86d7992010-01-05 20:57:30 +000036 if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) {
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +000037 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
38 if (I->getOpcode() == Instruction::Shl) {
39 // This is a value scaled by '1 << the shift amt'.
Dan Gohman28d2e0a2010-05-28 04:33:04 +000040 Scale = UINT64_C(1) << RHS->getZExtValue();
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +000041 Offset = 0;
42 return I->getOperand(0);
Chris Lattnerf86d7992010-01-05 20:57:30 +000043 }
44
45 if (I->getOpcode() == Instruction::Mul) {
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +000046 // This value is scaled by 'RHS'.
47 Scale = RHS->getZExtValue();
48 Offset = 0;
49 return I->getOperand(0);
Chris Lattnerf86d7992010-01-05 20:57:30 +000050 }
51
52 if (I->getOpcode() == Instruction::Add) {
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +000053 // We have X+C. Check to see if we really have (X*C2)+C1,
54 // where C1 is divisible by C2.
55 unsigned SubScale;
56 Value *SubVal =
57 DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset);
58 Offset += RHS->getZExtValue();
59 Scale = SubScale;
60 return SubVal;
61 }
62 }
63 }
64
65 // Otherwise, we can't look past this.
66 Scale = 1;
67 Offset = 0;
68 return Val;
69}
70
71/// PromoteCastOfAllocation - If we find a cast of an allocation instruction,
72/// try to eliminate the cast by moving the type information into the alloc.
73Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI,
74 AllocaInst &AI) {
75 // This requires TargetData to get the alloca alignment and size information.
76 if (!TD) return 0;
77
78 const PointerType *PTy = cast<PointerType>(CI.getType());
79
80 BuilderTy AllocaBuilder(*Builder);
81 AllocaBuilder.SetInsertPoint(AI.getParent(), &AI);
82
83 // Get the type really allocated and the type casted to.
84 const Type *AllocElTy = AI.getAllocatedType();
85 const Type *CastElTy = PTy->getElementType();
86 if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0;
87
88 unsigned AllocElTyAlign = TD->getABITypeAlignment(AllocElTy);
89 unsigned CastElTyAlign = TD->getABITypeAlignment(CastElTy);
90 if (CastElTyAlign < AllocElTyAlign) return 0;
91
92 // If the allocation has multiple uses, only promote it if we are strictly
93 // increasing the alignment of the resultant allocation. If we keep it the
Devang Patel5aa3fa62011-03-08 22:12:11 +000094 // same, we open the door to infinite loops of various kinds.
95 if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0;
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +000096
97 uint64_t AllocElTySize = TD->getTypeAllocSize(AllocElTy);
98 uint64_t CastElTySize = TD->getTypeAllocSize(CastElTy);
99 if (CastElTySize == 0 || AllocElTySize == 0) return 0;
100
101 // See if we can satisfy the modulus by pulling a scale out of the array
102 // size argument.
103 unsigned ArraySizeScale;
Dan Gohman28d2e0a2010-05-28 04:33:04 +0000104 uint64_t ArrayOffset;
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +0000105 Value *NumElements = // See if the array size is a decomposable linear expr.
106 DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset);
107
108 // If we can now satisfy the modulus, by using a non-1 scale, we really can
109 // do the xform.
110 if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 ||
111 (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return 0;
112
113 unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize;
114 Value *Amt = 0;
115 if (Scale == 1) {
116 Amt = NumElements;
117 } else {
Dan Gohman28d2e0a2010-05-28 04:33:04 +0000118 Amt = ConstantInt::get(AI.getArraySize()->getType(), Scale);
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +0000119 // Insert before the alloca, not before the cast.
120 Amt = AllocaBuilder.CreateMul(Amt, NumElements, "tmp");
121 }
122
Dan Gohman28d2e0a2010-05-28 04:33:04 +0000123 if (uint64_t Offset = (AllocElTySize*ArrayOffset)/CastElTySize) {
124 Value *Off = ConstantInt::get(AI.getArraySize()->getType(),
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +0000125 Offset, true);
126 Amt = AllocaBuilder.CreateAdd(Amt, Off, "tmp");
127 }
128
129 AllocaInst *New = AllocaBuilder.CreateAlloca(CastElTy, Amt);
130 New->setAlignment(AI.getAlignment());
131 New->takeName(&AI);
132
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +0000133 // If the allocation has multiple real uses, insert a cast and change all
134 // things that used it to use the new cast. This will also hack on CI, but it
135 // will die soon.
Devang Patel5aa3fa62011-03-08 22:12:11 +0000136 if (!AI.hasOneUse()) {
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +0000137 // New is the allocation instruction, pointer typed. AI is the original
138 // allocation instruction, also pointer typed. Thus, cast to use is BitCast.
139 Value *NewCast = AllocaBuilder.CreateBitCast(New, AI.getType(), "tmpcast");
Eli Friedman3e22cb92011-05-18 00:32:01 +0000140 ReplaceInstUsesWith(AI, NewCast);
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +0000141 }
142 return ReplaceInstUsesWith(CI, New);
143}
144
145
Chris Lattnere0e4cc72010-01-06 01:56:21 +0000146
Chris Lattner5f0290e2010-01-04 07:54:59 +0000147/// EvaluateInDifferentType - Given an expression that
Chris Lattner14bf8f02010-01-08 19:19:23 +0000148/// CanEvaluateTruncated or CanEvaluateSExtd returns true for, actually
Chris Lattnere0e4cc72010-01-06 01:56:21 +0000149/// insert the code to evaluate the expression.
Chris Lattner5f0290e2010-01-04 07:54:59 +0000150Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty,
151 bool isSigned) {
Chris Lattnerc8b3fce2010-01-08 19:28:47 +0000152 if (Constant *C = dyn_cast<Constant>(V)) {
153 C = ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/);
154 // If we got a constantexpr back, try to simplify it with TD info.
155 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
156 C = ConstantFoldConstantExpression(CE, TD);
157 return C;
158 }
Chris Lattner5f0290e2010-01-04 07:54:59 +0000159
160 // Otherwise, it must be an instruction.
161 Instruction *I = cast<Instruction>(V);
162 Instruction *Res = 0;
163 unsigned Opc = I->getOpcode();
164 switch (Opc) {
165 case Instruction::Add:
166 case Instruction::Sub:
167 case Instruction::Mul:
168 case Instruction::And:
169 case Instruction::Or:
170 case Instruction::Xor:
171 case Instruction::AShr:
172 case Instruction::LShr:
173 case Instruction::Shl:
174 case Instruction::UDiv:
175 case Instruction::URem: {
176 Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned);
177 Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);
178 Res = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
179 break;
180 }
181 case Instruction::Trunc:
182 case Instruction::ZExt:
183 case Instruction::SExt:
184 // If the source type of the cast is the type we're trying for then we can
185 // just return the source. There's no need to insert it because it is not
186 // new.
187 if (I->getOperand(0)->getType() == Ty)
188 return I->getOperand(0);
189
190 // Otherwise, must be the same type of cast, so just reinsert a new one.
Chris Lattner9ee947c2010-01-10 20:25:54 +0000191 // This also handles the case of zext(trunc(x)) -> zext(x).
192 Res = CastInst::CreateIntegerCast(I->getOperand(0), Ty,
193 Opc == Instruction::SExt);
Chris Lattner5f0290e2010-01-04 07:54:59 +0000194 break;
195 case Instruction::Select: {
196 Value *True = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);
197 Value *False = EvaluateInDifferentType(I->getOperand(2), Ty, isSigned);
198 Res = SelectInst::Create(I->getOperand(0), True, False);
199 break;
200 }
201 case Instruction::PHI: {
202 PHINode *OPN = cast<PHINode>(I);
Jay Foad3ecfc862011-03-30 11:28:46 +0000203 PHINode *NPN = PHINode::Create(Ty, OPN->getNumIncomingValues());
Chris Lattner5f0290e2010-01-04 07:54:59 +0000204 for (unsigned i = 0, e = OPN->getNumIncomingValues(); i != e; ++i) {
205 Value *V =EvaluateInDifferentType(OPN->getIncomingValue(i), Ty, isSigned);
206 NPN->addIncoming(V, OPN->getIncomingBlock(i));
207 }
208 Res = NPN;
209 break;
210 }
211 default:
212 // TODO: Can handle more cases here.
213 llvm_unreachable("Unreachable!");
214 break;
215 }
216
217 Res->takeName(I);
Eli Friedmana311c342011-05-27 00:19:40 +0000218 return InsertNewInstWith(Res, *I);
Chris Lattner5f0290e2010-01-04 07:54:59 +0000219}
Chris Lattner80f43d32010-01-04 07:53:58 +0000220
221
222/// This function is a wrapper around CastInst::isEliminableCastPair. It
223/// simply extracts arguments and returns what that function returns.
224static Instruction::CastOps
225isEliminableCastPair(
226 const CastInst *CI, ///< The first cast instruction
227 unsigned opcode, ///< The opcode of the second cast instruction
228 const Type *DstTy, ///< The target type for the second cast instruction
229 TargetData *TD ///< The target data for pointer size
230) {
231
232 const Type *SrcTy = CI->getOperand(0)->getType(); // A from above
233 const Type *MidTy = CI->getType(); // B from above
234
235 // Get the opcodes of the two Cast instructions
236 Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode());
237 Instruction::CastOps secondOp = Instruction::CastOps(opcode);
238
239 unsigned Res = CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy,
240 DstTy,
241 TD ? TD->getIntPtrType(CI->getContext()) : 0);
242
243 // We don't want to form an inttoptr or ptrtoint that converts to an integer
244 // type that differs from the pointer size.
245 if ((Res == Instruction::IntToPtr &&
246 (!TD || SrcTy != TD->getIntPtrType(CI->getContext()))) ||
247 (Res == Instruction::PtrToInt &&
248 (!TD || DstTy != TD->getIntPtrType(CI->getContext()))))
249 Res = 0;
250
251 return Instruction::CastOps(Res);
252}
253
Chris Lattner8c5ad3a2010-02-11 06:26:33 +0000254/// ShouldOptimizeCast - Return true if the cast from "V to Ty" actually
255/// results in any code being generated and is interesting to optimize out. If
256/// the cast can be eliminated by some other simple transformation, we prefer
257/// to do the simplification first.
258bool InstCombiner::ShouldOptimizeCast(Instruction::CastOps opc, const Value *V,
259 const Type *Ty) {
260 // Noop casts and casts of constants should be eliminated trivially.
Chris Lattner80f43d32010-01-04 07:53:58 +0000261 if (V->getType() == Ty || isa<Constant>(V)) return false;
262
Chris Lattner8c5ad3a2010-02-11 06:26:33 +0000263 // If this is another cast that can be eliminated, we prefer to have it
264 // eliminated.
Chris Lattner80f43d32010-01-04 07:53:58 +0000265 if (const CastInst *CI = dyn_cast<CastInst>(V))
Chris Lattner8c5ad3a2010-02-11 06:26:33 +0000266 if (isEliminableCastPair(CI, opc, Ty, TD))
Chris Lattner80f43d32010-01-04 07:53:58 +0000267 return false;
Chris Lattner8c5ad3a2010-02-11 06:26:33 +0000268
269 // If this is a vector sext from a compare, then we don't want to break the
270 // idiom where each element of the extended vector is either zero or all ones.
Duncan Sands1df98592010-02-16 11:11:14 +0000271 if (opc == Instruction::SExt && isa<CmpInst>(V) && Ty->isVectorTy())
Chris Lattner8c5ad3a2010-02-11 06:26:33 +0000272 return false;
273
Chris Lattner80f43d32010-01-04 07:53:58 +0000274 return true;
275}
276
277
278/// @brief Implement the transforms common to all CastInst visitors.
279Instruction *InstCombiner::commonCastTransforms(CastInst &CI) {
280 Value *Src = CI.getOperand(0);
281
282 // Many cases of "cast of a cast" are eliminable. If it's eliminable we just
283 // eliminate it now.
284 if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast
285 if (Instruction::CastOps opc =
286 isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), TD)) {
287 // The first cast (CSrc) is eliminable so we need to fix up or replace
288 // the second cast (CI). CSrc will then have a good chance of being dead.
289 return CastInst::Create(opc, CSrc->getOperand(0), CI.getType());
290 }
291 }
292
293 // If we are casting a select then fold the cast into the select
294 if (SelectInst *SI = dyn_cast<SelectInst>(Src))
295 if (Instruction *NV = FoldOpIntoSelect(CI, SI))
296 return NV;
297
298 // If we are casting a PHI then fold the cast into the PHI
299 if (isa<PHINode>(Src)) {
300 // We don't do this if this would create a PHI node with an illegal type if
301 // it is currently legal.
Duncan Sands1df98592010-02-16 11:11:14 +0000302 if (!Src->getType()->isIntegerTy() ||
303 !CI.getType()->isIntegerTy() ||
Chris Lattner80f43d32010-01-04 07:53:58 +0000304 ShouldChangeType(CI.getType(), Src->getType()))
305 if (Instruction *NV = FoldOpIntoPhi(CI))
306 return NV;
307 }
308
309 return 0;
310}
311
Chris Lattner75215c92010-01-10 00:58:42 +0000312/// CanEvaluateTruncated - Return true if we can evaluate the specified
313/// expression tree as type Ty instead of its larger type, and arrive with the
314/// same value. This is used by code that tries to eliminate truncates.
315///
316/// Ty will always be a type smaller than V. We should return true if trunc(V)
317/// can be computed by computing V in the smaller type. If V is an instruction,
318/// then trunc(inst(x,y)) can be computed as inst(trunc(x),trunc(y)), which only
319/// makes sense if x and y can be efficiently truncated.
320///
Chris Lattner8cf4f6f2010-01-11 02:43:35 +0000321/// This function works on both vectors and scalars.
322///
Chris Lattner75215c92010-01-10 00:58:42 +0000323static bool CanEvaluateTruncated(Value *V, const Type *Ty) {
324 // We can always evaluate constants in another type.
325 if (isa<Constant>(V))
326 return true;
Chris Lattner68c6e892010-01-05 23:00:30 +0000327
Chris Lattner75215c92010-01-10 00:58:42 +0000328 Instruction *I = dyn_cast<Instruction>(V);
329 if (!I) return false;
330
331 const Type *OrigTy = V->getType();
332
Chris Lattnera958cbf2010-01-11 22:45:25 +0000333 // If this is an extension from the dest type, we can eliminate it, even if it
334 // has multiple uses.
Chris Lattner53af2d12010-01-11 22:49:40 +0000335 if ((isa<ZExtInst>(I) || isa<SExtInst>(I)) &&
Chris Lattner75215c92010-01-10 00:58:42 +0000336 I->getOperand(0)->getType() == Ty)
337 return true;
338
339 // We can't extend or shrink something that has multiple uses: doing so would
340 // require duplicating the instruction in general, which isn't profitable.
341 if (!I->hasOneUse()) return false;
342
343 unsigned Opc = I->getOpcode();
344 switch (Opc) {
345 case Instruction::Add:
346 case Instruction::Sub:
347 case Instruction::Mul:
348 case Instruction::And:
349 case Instruction::Or:
350 case Instruction::Xor:
351 // These operators can all arbitrarily be extended or truncated.
352 return CanEvaluateTruncated(I->getOperand(0), Ty) &&
353 CanEvaluateTruncated(I->getOperand(1), Ty);
354
355 case Instruction::UDiv:
356 case Instruction::URem: {
357 // UDiv and URem can be truncated if all the truncated bits are zero.
358 uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
359 uint32_t BitWidth = Ty->getScalarSizeInBits();
360 if (BitWidth < OrigBitWidth) {
361 APInt Mask = APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth);
362 if (MaskedValueIsZero(I->getOperand(0), Mask) &&
363 MaskedValueIsZero(I->getOperand(1), Mask)) {
364 return CanEvaluateTruncated(I->getOperand(0), Ty) &&
365 CanEvaluateTruncated(I->getOperand(1), Ty);
366 }
367 }
368 break;
369 }
370 case Instruction::Shl:
371 // If we are truncating the result of this SHL, and if it's a shift of a
372 // constant amount, we can always perform a SHL in a smaller type.
373 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
374 uint32_t BitWidth = Ty->getScalarSizeInBits();
375 if (CI->getLimitedValue(BitWidth) < BitWidth)
376 return CanEvaluateTruncated(I->getOperand(0), Ty);
377 }
378 break;
379 case Instruction::LShr:
380 // If this is a truncate of a logical shr, we can truncate it to a smaller
381 // lshr iff we know that the bits we would otherwise be shifting in are
382 // already zeros.
383 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
384 uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
385 uint32_t BitWidth = Ty->getScalarSizeInBits();
386 if (MaskedValueIsZero(I->getOperand(0),
387 APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) &&
388 CI->getLimitedValue(BitWidth) < BitWidth) {
389 return CanEvaluateTruncated(I->getOperand(0), Ty);
390 }
391 }
392 break;
393 case Instruction::Trunc:
394 // trunc(trunc(x)) -> trunc(x)
395 return true;
Chris Lattnerf9d05ab2010-08-27 20:32:06 +0000396 case Instruction::ZExt:
397 case Instruction::SExt:
398 // trunc(ext(x)) -> ext(x) if the source type is smaller than the new dest
399 // trunc(ext(x)) -> trunc(x) if the source type is larger than the new dest
400 return true;
Chris Lattner75215c92010-01-10 00:58:42 +0000401 case Instruction::Select: {
402 SelectInst *SI = cast<SelectInst>(I);
403 return CanEvaluateTruncated(SI->getTrueValue(), Ty) &&
404 CanEvaluateTruncated(SI->getFalseValue(), Ty);
405 }
406 case Instruction::PHI: {
407 // We can change a phi if we can change all operands. Note that we never
408 // get into trouble with cyclic PHIs here because we only consider
409 // instructions with a single use.
410 PHINode *PN = cast<PHINode>(I);
411 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
412 if (!CanEvaluateTruncated(PN->getIncomingValue(i), Ty))
413 return false;
414 return true;
415 }
416 default:
417 // TODO: Can handle more cases here.
418 break;
419 }
420
421 return false;
422}
423
424Instruction *InstCombiner::visitTrunc(TruncInst &CI) {
Chris Lattnerd84dfa42010-01-10 01:00:46 +0000425 if (Instruction *Result = commonCastTransforms(CI))
Chris Lattner75215c92010-01-10 00:58:42 +0000426 return Result;
427
Chris Lattnerd84dfa42010-01-10 01:00:46 +0000428 // See if we can simplify any instructions used by the input whose sole
429 // purpose is to compute bits we don't care about.
430 if (SimplifyDemandedInstructionBits(CI))
431 return &CI;
432
Chris Lattner75215c92010-01-10 00:58:42 +0000433 Value *Src = CI.getOperand(0);
434 const Type *DestTy = CI.getType(), *SrcTy = Src->getType();
435
436 // Attempt to truncate the entire input expression tree to the destination
437 // type. Only do this if the dest type is a simple type, don't convert the
Chris Lattner80f43d32010-01-04 07:53:58 +0000438 // expression tree to something weird like i93 unless the source is also
439 // strange.
Duncan Sands1df98592010-02-16 11:11:14 +0000440 if ((DestTy->isVectorTy() || ShouldChangeType(SrcTy, DestTy)) &&
Chris Lattner75215c92010-01-10 00:58:42 +0000441 CanEvaluateTruncated(Src, DestTy)) {
Chris Lattnere0e4cc72010-01-06 01:56:21 +0000442
Chris Lattner80f43d32010-01-04 07:53:58 +0000443 // If this cast is a truncate, evaluting in a different type always
Chris Lattner68c6e892010-01-05 23:00:30 +0000444 // eliminates the cast, so it is always a win.
Chris Lattner075f6922010-01-07 23:41:00 +0000445 DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type"
Dan Gohman5b71dce2010-05-25 21:50:35 +0000446 " to avoid cast: " << CI << '\n');
Chris Lattner075f6922010-01-07 23:41:00 +0000447 Value *Res = EvaluateInDifferentType(Src, DestTy, false);
448 assert(Res->getType() == DestTy);
449 return ReplaceInstUsesWith(CI, Res);
450 }
Chris Lattner80f43d32010-01-04 07:53:58 +0000451
Chris Lattner7a34d6c2010-01-05 22:21:18 +0000452 // Canonicalize trunc x to i1 -> (icmp ne (and x, 1), 0), likewise for vector.
453 if (DestTy->getScalarSizeInBits() == 1) {
Chris Lattner80f43d32010-01-04 07:53:58 +0000454 Constant *One = ConstantInt::get(Src->getType(), 1);
455 Src = Builder->CreateAnd(Src, One, "tmp");
456 Value *Zero = Constant::getNullValue(Src->getType());
457 return new ICmpInst(ICmpInst::ICMP_NE, Src, Zero);
458 }
Chris Lattner784f3332010-08-27 18:31:05 +0000459
460 // Transform trunc(lshr (zext A), Cst) to eliminate one type conversion.
461 Value *A = 0; ConstantInt *Cst = 0;
Chris Lattner62fe4062011-01-15 06:32:33 +0000462 if (Src->hasOneUse() &&
463 match(Src, m_LShr(m_ZExt(m_Value(A)), m_ConstantInt(Cst)))) {
Chris Lattner784f3332010-08-27 18:31:05 +0000464 // We have three types to worry about here, the type of A, the source of
465 // the truncate (MidSize), and the destination of the truncate. We know that
466 // ASize < MidSize and MidSize > ResultSize, but don't know the relation
467 // between ASize and ResultSize.
468 unsigned ASize = A->getType()->getPrimitiveSizeInBits();
469
470 // If the shift amount is larger than the size of A, then the result is
471 // known to be zero because all the input bits got shifted out.
472 if (Cst->getZExtValue() >= ASize)
473 return ReplaceInstUsesWith(CI, Constant::getNullValue(CI.getType()));
474
475 // Since we're doing an lshr and a zero extend, and know that the shift
476 // amount is smaller than ASize, it is always safe to do the shift in A's
477 // type, then zero extend or truncate to the result.
478 Value *Shift = Builder->CreateLShr(A, Cst->getZExtValue());
479 Shift->takeName(Src);
480 return CastInst::CreateIntegerCast(Shift, CI.getType(), false);
481 }
Chris Lattner62fe4062011-01-15 06:32:33 +0000482
483 // Transform "trunc (and X, cst)" -> "and (trunc X), cst" so long as the dest
484 // type isn't non-native.
485 if (Src->hasOneUse() && isa<IntegerType>(Src->getType()) &&
486 ShouldChangeType(Src->getType(), CI.getType()) &&
487 match(Src, m_And(m_Value(A), m_ConstantInt(Cst)))) {
488 Value *NewTrunc = Builder->CreateTrunc(A, CI.getType(), A->getName()+".tr");
489 return BinaryOperator::CreateAnd(NewTrunc,
490 ConstantExpr::getTrunc(Cst, CI.getType()));
491 }
Chris Lattner80f43d32010-01-04 07:53:58 +0000492
Chris Lattner80f43d32010-01-04 07:53:58 +0000493 return 0;
494}
495
496/// transformZExtICmp - Transform (zext icmp) to bitwise / integer operations
497/// in order to eliminate the icmp.
498Instruction *InstCombiner::transformZExtICmp(ICmpInst *ICI, Instruction &CI,
499 bool DoXform) {
500 // If we are just checking for a icmp eq of a single bit and zext'ing it
501 // to an integer, then shift the bit to the appropriate place and then
502 // cast to integer to avoid the comparison.
503 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
504 const APInt &Op1CV = Op1C->getValue();
505
506 // zext (x <s 0) to i32 --> x>>u31 true if signbit set.
507 // zext (x >s -1) to i32 --> (x>>u31)^1 true if signbit clear.
508 if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) ||
509 (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())) {
510 if (!DoXform) return ICI;
511
512 Value *In = ICI->getOperand(0);
513 Value *Sh = ConstantInt::get(In->getType(),
514 In->getType()->getScalarSizeInBits()-1);
515 In = Builder->CreateLShr(In, Sh, In->getName()+".lobit");
516 if (In->getType() != CI.getType())
517 In = Builder->CreateIntCast(In, CI.getType(), false/*ZExt*/, "tmp");
518
519 if (ICI->getPredicate() == ICmpInst::ICMP_SGT) {
520 Constant *One = ConstantInt::get(In->getType(), 1);
521 In = Builder->CreateXor(In, One, In->getName()+".not");
522 }
523
524 return ReplaceInstUsesWith(CI, In);
525 }
526
527
528
529 // zext (X == 0) to i32 --> X^1 iff X has only the low bit set.
530 // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set.
531 // zext (X == 1) to i32 --> X iff X has only the low bit set.
532 // zext (X == 2) to i32 --> X>>1 iff X has only the 2nd bit set.
533 // zext (X != 0) to i32 --> X iff X has only the low bit set.
534 // zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set.
535 // zext (X != 1) to i32 --> X^1 iff X has only the low bit set.
536 // zext (X != 2) to i32 --> (X>>1)^1 iff X has only the 2nd bit set.
537 if ((Op1CV == 0 || Op1CV.isPowerOf2()) &&
538 // This only works for EQ and NE
539 ICI->isEquality()) {
540 // If Op1C some other power of two, convert:
541 uint32_t BitWidth = Op1C->getType()->getBitWidth();
542 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
543 APInt TypeMask(APInt::getAllOnesValue(BitWidth));
544 ComputeMaskedBits(ICI->getOperand(0), TypeMask, KnownZero, KnownOne);
545
546 APInt KnownZeroMask(~KnownZero);
547 if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1?
548 if (!DoXform) return ICI;
549
550 bool isNE = ICI->getPredicate() == ICmpInst::ICMP_NE;
551 if (Op1CV != 0 && (Op1CV != KnownZeroMask)) {
552 // (X&4) == 2 --> false
553 // (X&4) != 2 --> true
554 Constant *Res = ConstantInt::get(Type::getInt1Ty(CI.getContext()),
555 isNE);
556 Res = ConstantExpr::getZExt(Res, CI.getType());
557 return ReplaceInstUsesWith(CI, Res);
558 }
559
560 uint32_t ShiftAmt = KnownZeroMask.logBase2();
561 Value *In = ICI->getOperand(0);
562 if (ShiftAmt) {
563 // Perform a logical shr by shiftamt.
564 // Insert the shift to put the result in the low bit.
565 In = Builder->CreateLShr(In, ConstantInt::get(In->getType(),ShiftAmt),
566 In->getName()+".lobit");
567 }
568
569 if ((Op1CV != 0) == isNE) { // Toggle the low bit.
570 Constant *One = ConstantInt::get(In->getType(), 1);
571 In = Builder->CreateXor(In, One, "tmp");
572 }
573
574 if (CI.getType() == In->getType())
575 return ReplaceInstUsesWith(CI, In);
Chris Lattner29cc0b32010-08-27 22:24:38 +0000576 return CastInst::CreateIntegerCast(In, CI.getType(), false/*ZExt*/);
Chris Lattner80f43d32010-01-04 07:53:58 +0000577 }
578 }
579 }
580
581 // icmp ne A, B is equal to xor A, B when A and B only really have one bit.
582 // It is also profitable to transform icmp eq into not(xor(A, B)) because that
583 // may lead to additional simplifications.
584 if (ICI->isEquality() && CI.getType() == ICI->getOperand(0)->getType()) {
585 if (const IntegerType *ITy = dyn_cast<IntegerType>(CI.getType())) {
586 uint32_t BitWidth = ITy->getBitWidth();
587 Value *LHS = ICI->getOperand(0);
588 Value *RHS = ICI->getOperand(1);
589
590 APInt KnownZeroLHS(BitWidth, 0), KnownOneLHS(BitWidth, 0);
591 APInt KnownZeroRHS(BitWidth, 0), KnownOneRHS(BitWidth, 0);
592 APInt TypeMask(APInt::getAllOnesValue(BitWidth));
593 ComputeMaskedBits(LHS, TypeMask, KnownZeroLHS, KnownOneLHS);
594 ComputeMaskedBits(RHS, TypeMask, KnownZeroRHS, KnownOneRHS);
595
596 if (KnownZeroLHS == KnownZeroRHS && KnownOneLHS == KnownOneRHS) {
597 APInt KnownBits = KnownZeroLHS | KnownOneLHS;
598 APInt UnknownBit = ~KnownBits;
599 if (UnknownBit.countPopulation() == 1) {
600 if (!DoXform) return ICI;
601
602 Value *Result = Builder->CreateXor(LHS, RHS);
603
604 // Mask off any bits that are set and won't be shifted away.
605 if (KnownOneLHS.uge(UnknownBit))
606 Result = Builder->CreateAnd(Result,
607 ConstantInt::get(ITy, UnknownBit));
608
609 // Shift the bit we're testing down to the lsb.
610 Result = Builder->CreateLShr(
611 Result, ConstantInt::get(ITy, UnknownBit.countTrailingZeros()));
612
613 if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
614 Result = Builder->CreateXor(Result, ConstantInt::get(ITy, 1));
615 Result->takeName(ICI);
616 return ReplaceInstUsesWith(CI, Result);
617 }
618 }
619 }
620 }
621
622 return 0;
623}
624
Chris Lattner75215c92010-01-10 00:58:42 +0000625/// CanEvaluateZExtd - Determine if the specified value can be computed in the
Chris Lattner8cf4f6f2010-01-11 02:43:35 +0000626/// specified wider type and produce the same low bits. If not, return false.
627///
Chris Lattner789162a2010-01-11 03:32:00 +0000628/// If this function returns true, it can also return a non-zero number of bits
629/// (in BitsToClear) which indicates that the value it computes is correct for
630/// the zero extend, but that the additional BitsToClear bits need to be zero'd
631/// out. For example, to promote something like:
632///
633/// %B = trunc i64 %A to i32
634/// %C = lshr i32 %B, 8
635/// %E = zext i32 %C to i64
636///
637/// CanEvaluateZExtd for the 'lshr' will return true, and BitsToClear will be
638/// set to 8 to indicate that the promoted value needs to have bits 24-31
639/// cleared in addition to bits 32-63. Since an 'and' will be generated to
640/// clear the top bits anyway, doing this has no extra cost.
641///
Chris Lattner8cf4f6f2010-01-11 02:43:35 +0000642/// This function works on both vectors and scalars.
Chris Lattner789162a2010-01-11 03:32:00 +0000643static bool CanEvaluateZExtd(Value *V, const Type *Ty, unsigned &BitsToClear) {
644 BitsToClear = 0;
Chris Lattner9e390dd2010-01-10 02:50:04 +0000645 if (isa<Constant>(V))
646 return true;
Chris Lattner75215c92010-01-10 00:58:42 +0000647
648 Instruction *I = dyn_cast<Instruction>(V);
Chris Lattner9e390dd2010-01-10 02:50:04 +0000649 if (!I) return false;
Chris Lattner75215c92010-01-10 00:58:42 +0000650
651 // If the input is a truncate from the destination type, we can trivially
Chris Lattnera958cbf2010-01-11 22:45:25 +0000652 // eliminate it, even if it has multiple uses.
653 // FIXME: This is currently disabled until codegen can handle this without
654 // pessimizing code, PR5997.
655 if (0 && isa<TruncInst>(I) && I->getOperand(0)->getType() == Ty)
Chris Lattner9e390dd2010-01-10 02:50:04 +0000656 return true;
Chris Lattner75215c92010-01-10 00:58:42 +0000657
658 // We can't extend or shrink something that has multiple uses: doing so would
659 // require duplicating the instruction in general, which isn't profitable.
Chris Lattner9e390dd2010-01-10 02:50:04 +0000660 if (!I->hasOneUse()) return false;
Chris Lattner75215c92010-01-10 00:58:42 +0000661
Chris Lattner789162a2010-01-11 03:32:00 +0000662 unsigned Opc = I->getOpcode(), Tmp;
Chris Lattner75215c92010-01-10 00:58:42 +0000663 switch (Opc) {
Chris Lattner9ee947c2010-01-10 20:25:54 +0000664 case Instruction::ZExt: // zext(zext(x)) -> zext(x).
665 case Instruction::SExt: // zext(sext(x)) -> sext(x).
666 case Instruction::Trunc: // zext(trunc(x)) -> trunc(x) or zext(x)
667 return true;
Chris Lattner75215c92010-01-10 00:58:42 +0000668 case Instruction::And:
Chris Lattner75215c92010-01-10 00:58:42 +0000669 case Instruction::Or:
670 case Instruction::Xor:
Chris Lattner75215c92010-01-10 00:58:42 +0000671 case Instruction::Add:
672 case Instruction::Sub:
673 case Instruction::Mul:
Chris Lattnerd26c9e12010-01-10 02:22:12 +0000674 case Instruction::Shl:
Chris Lattner789162a2010-01-11 03:32:00 +0000675 if (!CanEvaluateZExtd(I->getOperand(0), Ty, BitsToClear) ||
676 !CanEvaluateZExtd(I->getOperand(1), Ty, Tmp))
677 return false;
678 // These can all be promoted if neither operand has 'bits to clear'.
679 if (BitsToClear == 0 && Tmp == 0)
680 return true;
Chris Lattner75215c92010-01-10 00:58:42 +0000681
Chris Lattner7acc4b12010-01-11 04:05:13 +0000682 // If the operation is an AND/OR/XOR and the bits to clear are zero in the
683 // other side, BitsToClear is ok.
684 if (Tmp == 0 &&
685 (Opc == Instruction::And || Opc == Instruction::Or ||
686 Opc == Instruction::Xor)) {
687 // We use MaskedValueIsZero here for generality, but the case we care
688 // about the most is constant RHS.
689 unsigned VSize = V->getType()->getScalarSizeInBits();
690 if (MaskedValueIsZero(I->getOperand(1),
691 APInt::getHighBitsSet(VSize, BitsToClear)))
692 return true;
693 }
694
695 // Otherwise, we don't know how to analyze this BitsToClear case yet.
Chris Lattner789162a2010-01-11 03:32:00 +0000696 return false;
Chris Lattnerd26c9e12010-01-10 02:22:12 +0000697
Chris Lattner789162a2010-01-11 03:32:00 +0000698 case Instruction::LShr:
699 // We can promote lshr(x, cst) if we can promote x. This requires the
700 // ultimate 'and' to clear out the high zero bits we're clearing out though.
701 if (ConstantInt *Amt = dyn_cast<ConstantInt>(I->getOperand(1))) {
702 if (!CanEvaluateZExtd(I->getOperand(0), Ty, BitsToClear))
703 return false;
704 BitsToClear += Amt->getZExtValue();
705 if (BitsToClear > V->getType()->getScalarSizeInBits())
706 BitsToClear = V->getType()->getScalarSizeInBits();
707 return true;
708 }
709 // Cannot promote variable LSHR.
710 return false;
Chris Lattner75215c92010-01-10 00:58:42 +0000711 case Instruction::Select:
Chris Lattner789162a2010-01-11 03:32:00 +0000712 if (!CanEvaluateZExtd(I->getOperand(1), Ty, Tmp) ||
713 !CanEvaluateZExtd(I->getOperand(2), Ty, BitsToClear) ||
Chris Lattner7acc4b12010-01-11 04:05:13 +0000714 // TODO: If important, we could handle the case when the BitsToClear are
715 // known zero in the disagreeing side.
Chris Lattner789162a2010-01-11 03:32:00 +0000716 Tmp != BitsToClear)
717 return false;
718 return true;
Chris Lattner75215c92010-01-10 00:58:42 +0000719
720 case Instruction::PHI: {
721 // We can change a phi if we can change all operands. Note that we never
722 // get into trouble with cyclic PHIs here because we only consider
723 // instructions with a single use.
724 PHINode *PN = cast<PHINode>(I);
Chris Lattner789162a2010-01-11 03:32:00 +0000725 if (!CanEvaluateZExtd(PN->getIncomingValue(0), Ty, BitsToClear))
726 return false;
Chris Lattner9e390dd2010-01-10 02:50:04 +0000727 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i)
Chris Lattner789162a2010-01-11 03:32:00 +0000728 if (!CanEvaluateZExtd(PN->getIncomingValue(i), Ty, Tmp) ||
Chris Lattner7acc4b12010-01-11 04:05:13 +0000729 // TODO: If important, we could handle the case when the BitsToClear
730 // are known zero in the disagreeing input.
Chris Lattner789162a2010-01-11 03:32:00 +0000731 Tmp != BitsToClear)
732 return false;
Chris Lattner9e390dd2010-01-10 02:50:04 +0000733 return true;
Chris Lattner75215c92010-01-10 00:58:42 +0000734 }
735 default:
736 // TODO: Can handle more cases here.
Chris Lattner9e390dd2010-01-10 02:50:04 +0000737 return false;
Chris Lattner75215c92010-01-10 00:58:42 +0000738 }
739}
740
Chris Lattner80f43d32010-01-04 07:53:58 +0000741Instruction *InstCombiner::visitZExt(ZExtInst &CI) {
Chris Lattner5324d802010-01-10 02:39:31 +0000742 // If this zero extend is only used by a truncate, let the truncate by
743 // eliminated before we try to optimize this zext.
744 if (CI.hasOneUse() && isa<TruncInst>(CI.use_back()))
745 return 0;
746
Chris Lattner80f43d32010-01-04 07:53:58 +0000747 // If one of the common conversion will work, do it.
Chris Lattnerd84dfa42010-01-10 01:00:46 +0000748 if (Instruction *Result = commonCastTransforms(CI))
Chris Lattner80f43d32010-01-04 07:53:58 +0000749 return Result;
750
Chris Lattnerd84dfa42010-01-10 01:00:46 +0000751 // See if we can simplify any instructions used by the input whose sole
752 // purpose is to compute bits we don't care about.
753 if (SimplifyDemandedInstructionBits(CI))
754 return &CI;
Chris Lattner75215c92010-01-10 00:58:42 +0000755
Chris Lattnerd84dfa42010-01-10 01:00:46 +0000756 Value *Src = CI.getOperand(0);
Chris Lattner75215c92010-01-10 00:58:42 +0000757 const Type *SrcTy = Src->getType(), *DestTy = CI.getType();
758
759 // Attempt to extend the entire input expression tree to the destination
760 // type. Only do this if the dest type is a simple type, don't convert the
761 // expression tree to something weird like i93 unless the source is also
762 // strange.
Chris Lattner789162a2010-01-11 03:32:00 +0000763 unsigned BitsToClear;
Duncan Sands1df98592010-02-16 11:11:14 +0000764 if ((DestTy->isVectorTy() || ShouldChangeType(SrcTy, DestTy)) &&
Chris Lattner789162a2010-01-11 03:32:00 +0000765 CanEvaluateZExtd(Src, DestTy, BitsToClear)) {
766 assert(BitsToClear < SrcTy->getScalarSizeInBits() &&
767 "Unreasonable BitsToClear");
768
Chris Lattner5324d802010-01-10 02:39:31 +0000769 // Okay, we can transform this! Insert the new expression now.
770 DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type"
771 " to avoid zero extend: " << CI);
772 Value *Res = EvaluateInDifferentType(Src, DestTy, false);
773 assert(Res->getType() == DestTy);
774
Chris Lattner789162a2010-01-11 03:32:00 +0000775 uint32_t SrcBitsKept = SrcTy->getScalarSizeInBits()-BitsToClear;
776 uint32_t DestBitSize = DestTy->getScalarSizeInBits();
777
Chris Lattner5324d802010-01-10 02:39:31 +0000778 // If the high bits are already filled with zeros, just replace this
779 // cast with the result.
Chris Lattner9e390dd2010-01-10 02:50:04 +0000780 if (MaskedValueIsZero(Res, APInt::getHighBitsSet(DestBitSize,
Chris Lattner789162a2010-01-11 03:32:00 +0000781 DestBitSize-SrcBitsKept)))
Chris Lattner5324d802010-01-10 02:39:31 +0000782 return ReplaceInstUsesWith(CI, Res);
783
784 // We need to emit an AND to clear the high bits.
Chris Lattner9ee947c2010-01-10 20:25:54 +0000785 Constant *C = ConstantInt::get(Res->getType(),
Chris Lattner789162a2010-01-11 03:32:00 +0000786 APInt::getLowBitsSet(DestBitSize, SrcBitsKept));
Chris Lattner5324d802010-01-10 02:39:31 +0000787 return BinaryOperator::CreateAnd(Res, C);
Chris Lattner75215c92010-01-10 00:58:42 +0000788 }
Chris Lattner80f43d32010-01-04 07:53:58 +0000789
790 // If this is a TRUNC followed by a ZEXT then we are dealing with integral
791 // types and if the sizes are just right we can convert this into a logical
792 // 'and' which will be much cheaper than the pair of casts.
793 if (TruncInst *CSrc = dyn_cast<TruncInst>(Src)) { // A->B->C cast
Chris Lattnerf4fb9112010-01-10 07:08:30 +0000794 // TODO: Subsume this into EvaluateInDifferentType.
795
Chris Lattner80f43d32010-01-04 07:53:58 +0000796 // Get the sizes of the types involved. We know that the intermediate type
797 // will be smaller than A or C, but don't know the relation between A and C.
798 Value *A = CSrc->getOperand(0);
799 unsigned SrcSize = A->getType()->getScalarSizeInBits();
800 unsigned MidSize = CSrc->getType()->getScalarSizeInBits();
801 unsigned DstSize = CI.getType()->getScalarSizeInBits();
802 // If we're actually extending zero bits, then if
803 // SrcSize < DstSize: zext(a & mask)
804 // SrcSize == DstSize: a & mask
805 // SrcSize > DstSize: trunc(a) & mask
806 if (SrcSize < DstSize) {
807 APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
808 Constant *AndConst = ConstantInt::get(A->getType(), AndValue);
809 Value *And = Builder->CreateAnd(A, AndConst, CSrc->getName()+".mask");
810 return new ZExtInst(And, CI.getType());
811 }
812
813 if (SrcSize == DstSize) {
814 APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
815 return BinaryOperator::CreateAnd(A, ConstantInt::get(A->getType(),
816 AndValue));
817 }
818 if (SrcSize > DstSize) {
819 Value *Trunc = Builder->CreateTrunc(A, CI.getType(), "tmp");
820 APInt AndValue(APInt::getLowBitsSet(DstSize, MidSize));
821 return BinaryOperator::CreateAnd(Trunc,
822 ConstantInt::get(Trunc->getType(),
Chris Lattnerf4fb9112010-01-10 07:08:30 +0000823 AndValue));
Chris Lattner80f43d32010-01-04 07:53:58 +0000824 }
825 }
826
827 if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src))
828 return transformZExtICmp(ICI, CI);
829
830 BinaryOperator *SrcI = dyn_cast<BinaryOperator>(Src);
831 if (SrcI && SrcI->getOpcode() == Instruction::Or) {
832 // zext (or icmp, icmp) --> or (zext icmp), (zext icmp) if at least one
833 // of the (zext icmp) will be transformed.
834 ICmpInst *LHS = dyn_cast<ICmpInst>(SrcI->getOperand(0));
835 ICmpInst *RHS = dyn_cast<ICmpInst>(SrcI->getOperand(1));
836 if (LHS && RHS && LHS->hasOneUse() && RHS->hasOneUse() &&
837 (transformZExtICmp(LHS, CI, false) ||
838 transformZExtICmp(RHS, CI, false))) {
839 Value *LCast = Builder->CreateZExt(LHS, CI.getType(), LHS->getName());
840 Value *RCast = Builder->CreateZExt(RHS, CI.getType(), RHS->getName());
841 return BinaryOperator::Create(Instruction::Or, LCast, RCast);
842 }
843 }
844
845 // zext(trunc(t) & C) -> (t & zext(C)).
846 if (SrcI && SrcI->getOpcode() == Instruction::And && SrcI->hasOneUse())
847 if (ConstantInt *C = dyn_cast<ConstantInt>(SrcI->getOperand(1)))
848 if (TruncInst *TI = dyn_cast<TruncInst>(SrcI->getOperand(0))) {
849 Value *TI0 = TI->getOperand(0);
850 if (TI0->getType() == CI.getType())
851 return
852 BinaryOperator::CreateAnd(TI0,
853 ConstantExpr::getZExt(C, CI.getType()));
854 }
855
856 // zext((trunc(t) & C) ^ C) -> ((t & zext(C)) ^ zext(C)).
857 if (SrcI && SrcI->getOpcode() == Instruction::Xor && SrcI->hasOneUse())
858 if (ConstantInt *C = dyn_cast<ConstantInt>(SrcI->getOperand(1)))
859 if (BinaryOperator *And = dyn_cast<BinaryOperator>(SrcI->getOperand(0)))
860 if (And->getOpcode() == Instruction::And && And->hasOneUse() &&
861 And->getOperand(1) == C)
862 if (TruncInst *TI = dyn_cast<TruncInst>(And->getOperand(0))) {
863 Value *TI0 = TI->getOperand(0);
864 if (TI0->getType() == CI.getType()) {
865 Constant *ZC = ConstantExpr::getZExt(C, CI.getType());
866 Value *NewAnd = Builder->CreateAnd(TI0, ZC, "tmp");
867 return BinaryOperator::CreateXor(NewAnd, ZC);
868 }
869 }
870
Chris Lattner718bf3f2010-01-05 21:04:47 +0000871 // zext (xor i1 X, true) to i32 --> xor (zext i1 X to i32), 1
872 Value *X;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000873 if (SrcI && SrcI->hasOneUse() && SrcI->getType()->isIntegerTy(1) &&
Chris Lattner49bdfef2010-01-05 21:11:17 +0000874 match(SrcI, m_Not(m_Value(X))) &&
Chris Lattner718bf3f2010-01-05 21:04:47 +0000875 (!X->hasOneUse() || !isa<CmpInst>(X))) {
876 Value *New = Builder->CreateZExt(X, CI.getType());
877 return BinaryOperator::CreateXor(New, ConstantInt::get(CI.getType(), 1));
878 }
879
Chris Lattner80f43d32010-01-04 07:53:58 +0000880 return 0;
881}
882
Benjamin Kramer0a30c422011-04-01 20:09:03 +0000883/// transformSExtICmp - Transform (sext icmp) to bitwise / integer operations
884/// in order to eliminate the icmp.
885Instruction *InstCombiner::transformSExtICmp(ICmpInst *ICI, Instruction &CI) {
886 Value *Op0 = ICI->getOperand(0), *Op1 = ICI->getOperand(1);
887 ICmpInst::Predicate Pred = ICI->getPredicate();
888
889 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Benjamin Kramer406a6502011-04-01 22:29:18 +0000890 // (x <s 0) ? -1 : 0 -> ashr x, 31 -> all ones if negative
891 // (x >s -1) ? -1 : 0 -> not (ashr x, 31) -> all ones if positive
Benjamin Kramer0a30c422011-04-01 20:09:03 +0000892 if ((Pred == ICmpInst::ICMP_SLT && Op1C->isZero()) ||
893 (Pred == ICmpInst::ICMP_SGT && Op1C->isAllOnesValue())) {
894
895 Value *Sh = ConstantInt::get(Op0->getType(),
896 Op0->getType()->getScalarSizeInBits()-1);
897 Value *In = Builder->CreateAShr(Op0, Sh, Op0->getName()+".lobit");
898 if (In->getType() != CI.getType())
899 In = Builder->CreateIntCast(In, CI.getType(), true/*SExt*/, "tmp");
900
901 if (Pred == ICmpInst::ICMP_SGT)
902 In = Builder->CreateNot(In, In->getName()+".not");
903 return ReplaceInstUsesWith(CI, In);
904 }
Benjamin Kramer0baa94a2011-04-01 20:09:10 +0000905
906 // If we know that only one bit of the LHS of the icmp can be set and we
907 // have an equality comparison with zero or a power of 2, we can transform
908 // the icmp and sext into bitwise/integer operations.
Benjamin Kramer5337fab2011-04-01 22:22:11 +0000909 if (ICI->hasOneUse() &&
910 ICI->isEquality() && (Op1C->isZero() || Op1C->getValue().isPowerOf2())){
Benjamin Kramer0baa94a2011-04-01 20:09:10 +0000911 unsigned BitWidth = Op1C->getType()->getBitWidth();
912 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
913 APInt TypeMask(APInt::getAllOnesValue(BitWidth));
914 ComputeMaskedBits(Op0, TypeMask, KnownZero, KnownOne);
915
Benjamin Kramerce1498b2011-04-01 20:15:16 +0000916 APInt KnownZeroMask(~KnownZero);
917 if (KnownZeroMask.isPowerOf2()) {
Benjamin Kramer0baa94a2011-04-01 20:09:10 +0000918 Value *In = ICI->getOperand(0);
919
Benjamin Kramerf5b75932011-04-02 18:50:58 +0000920 // If the icmp tests for a known zero bit we can constant fold it.
921 if (!Op1C->isZero() && Op1C->getValue() != KnownZeroMask) {
922 Value *V = Pred == ICmpInst::ICMP_NE ?
923 ConstantInt::getAllOnesValue(CI.getType()) :
924 ConstantInt::getNullValue(CI.getType());
925 return ReplaceInstUsesWith(CI, V);
926 }
Benjamin Kramer5337fab2011-04-01 22:22:11 +0000927
Benjamin Kramer0baa94a2011-04-01 20:09:10 +0000928 if (!Op1C->isZero() == (Pred == ICmpInst::ICMP_NE)) {
929 // sext ((x & 2^n) == 0) -> (x >> n) - 1
930 // sext ((x & 2^n) != 2^n) -> (x >> n) - 1
931 unsigned ShiftAmt = KnownZeroMask.countTrailingZeros();
932 // Perform a right shift to place the desired bit in the LSB.
933 if (ShiftAmt)
934 In = Builder->CreateLShr(In,
935 ConstantInt::get(In->getType(), ShiftAmt));
936
937 // At this point "In" is either 1 or 0. Subtract 1 to turn
938 // {1, 0} -> {0, -1}.
939 In = Builder->CreateAdd(In,
940 ConstantInt::getAllOnesValue(In->getType()),
941 "sext");
942 } else {
943 // sext ((x & 2^n) != 0) -> (x << bitwidth-n) a>> bitwidth-1
Benjamin Kramer5337fab2011-04-01 22:22:11 +0000944 // sext ((x & 2^n) == 2^n) -> (x << bitwidth-n) a>> bitwidth-1
Benjamin Kramer0baa94a2011-04-01 20:09:10 +0000945 unsigned ShiftAmt = KnownZeroMask.countLeadingZeros();
946 // Perform a left shift to place the desired bit in the MSB.
947 if (ShiftAmt)
948 In = Builder->CreateShl(In,
949 ConstantInt::get(In->getType(), ShiftAmt));
950
951 // Distribute the bit over the whole bit width.
952 In = Builder->CreateAShr(In, ConstantInt::get(In->getType(),
953 BitWidth - 1), "sext");
954 }
955
956 if (CI.getType() == In->getType())
957 return ReplaceInstUsesWith(CI, In);
958 return CastInst::CreateIntegerCast(In, CI.getType(), true/*SExt*/);
959 }
960 }
Benjamin Kramer0a30c422011-04-01 20:09:03 +0000961 }
962
963 // vector (x <s 0) ? -1 : 0 -> ashr x, 31 -> all ones if signed.
964 if (const VectorType *VTy = dyn_cast<VectorType>(CI.getType())) {
965 if (Pred == ICmpInst::ICMP_SLT && match(Op1, m_Zero()) &&
966 Op0->getType() == CI.getType()) {
967 const Type *EltTy = VTy->getElementType();
968
969 // splat the shift constant to a constant vector.
970 Constant *VSh = ConstantInt::get(VTy, EltTy->getScalarSizeInBits()-1);
971 Value *In = Builder->CreateAShr(Op0, VSh, Op0->getName()+".lobit");
972 return ReplaceInstUsesWith(CI, In);
973 }
974 }
975
976 return 0;
977}
978
Chris Lattner75215c92010-01-10 00:58:42 +0000979/// CanEvaluateSExtd - Return true if we can take the specified value
980/// and return it as type Ty without inserting any new casts and without
981/// changing the value of the common low bits. This is used by code that tries
982/// to promote integer operations to a wider types will allow us to eliminate
983/// the extension.
984///
Chris Lattneraa9c8942010-01-10 07:57:20 +0000985/// This function works on both vectors and scalars.
Chris Lattner75215c92010-01-10 00:58:42 +0000986///
Chris Lattner8cf4f6f2010-01-11 02:43:35 +0000987static bool CanEvaluateSExtd(Value *V, const Type *Ty) {
Chris Lattner75215c92010-01-10 00:58:42 +0000988 assert(V->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits() &&
989 "Can't sign extend type to a smaller type");
Chris Lattneraa9c8942010-01-10 07:57:20 +0000990 // If this is a constant, it can be trivially promoted.
991 if (isa<Constant>(V))
992 return true;
Chris Lattner75215c92010-01-10 00:58:42 +0000993
994 Instruction *I = dyn_cast<Instruction>(V);
Chris Lattneraa9c8942010-01-10 07:57:20 +0000995 if (!I) return false;
Chris Lattner75215c92010-01-10 00:58:42 +0000996
Chris Lattnera958cbf2010-01-11 22:45:25 +0000997 // If this is a truncate from the dest type, we can trivially eliminate it,
998 // even if it has multiple uses.
999 // FIXME: This is currently disabled until codegen can handle this without
1000 // pessimizing code, PR5997.
1001 if (0 && isa<TruncInst>(I) && I->getOperand(0)->getType() == Ty)
Chris Lattneraa9c8942010-01-10 07:57:20 +00001002 return true;
Chris Lattner75215c92010-01-10 00:58:42 +00001003
1004 // We can't extend or shrink something that has multiple uses: doing so would
1005 // require duplicating the instruction in general, which isn't profitable.
Chris Lattneraa9c8942010-01-10 07:57:20 +00001006 if (!I->hasOneUse()) return false;
Chris Lattner75215c92010-01-10 00:58:42 +00001007
Chris Lattneraa9c8942010-01-10 07:57:20 +00001008 switch (I->getOpcode()) {
Chris Lattner11ea8122010-01-10 20:30:41 +00001009 case Instruction::SExt: // sext(sext(x)) -> sext(x)
1010 case Instruction::ZExt: // sext(zext(x)) -> zext(x)
1011 case Instruction::Trunc: // sext(trunc(x)) -> trunc(x) or sext(x)
1012 return true;
Chris Lattner75215c92010-01-10 00:58:42 +00001013 case Instruction::And:
1014 case Instruction::Or:
1015 case Instruction::Xor:
Chris Lattner75215c92010-01-10 00:58:42 +00001016 case Instruction::Add:
1017 case Instruction::Sub:
Chris Lattner75215c92010-01-10 00:58:42 +00001018 case Instruction::Mul:
Chris Lattneraa9c8942010-01-10 07:57:20 +00001019 // These operators can all arbitrarily be extended if their inputs can.
Chris Lattner8cf4f6f2010-01-11 02:43:35 +00001020 return CanEvaluateSExtd(I->getOperand(0), Ty) &&
1021 CanEvaluateSExtd(I->getOperand(1), Ty);
Chris Lattner75215c92010-01-10 00:58:42 +00001022
1023 //case Instruction::Shl: TODO
1024 //case Instruction::LShr: TODO
Chris Lattner75215c92010-01-10 00:58:42 +00001025
Chris Lattneraa9c8942010-01-10 07:57:20 +00001026 case Instruction::Select:
Chris Lattner8cf4f6f2010-01-11 02:43:35 +00001027 return CanEvaluateSExtd(I->getOperand(1), Ty) &&
1028 CanEvaluateSExtd(I->getOperand(2), Ty);
Chris Lattner9ee947c2010-01-10 20:25:54 +00001029
Chris Lattner75215c92010-01-10 00:58:42 +00001030 case Instruction::PHI: {
1031 // We can change a phi if we can change all operands. Note that we never
1032 // get into trouble with cyclic PHIs here because we only consider
1033 // instructions with a single use.
1034 PHINode *PN = cast<PHINode>(I);
Chris Lattner9ee947c2010-01-10 20:25:54 +00001035 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
Chris Lattner8cf4f6f2010-01-11 02:43:35 +00001036 if (!CanEvaluateSExtd(PN->getIncomingValue(i), Ty)) return false;
Chris Lattneraa9c8942010-01-10 07:57:20 +00001037 return true;
Chris Lattner75215c92010-01-10 00:58:42 +00001038 }
1039 default:
1040 // TODO: Can handle more cases here.
1041 break;
1042 }
1043
Chris Lattneraa9c8942010-01-10 07:57:20 +00001044 return false;
Chris Lattner75215c92010-01-10 00:58:42 +00001045}
1046
Chris Lattner80f43d32010-01-04 07:53:58 +00001047Instruction *InstCombiner::visitSExt(SExtInst &CI) {
Chris Lattner5324d802010-01-10 02:39:31 +00001048 // If this sign extend is only used by a truncate, let the truncate by
1049 // eliminated before we try to optimize this zext.
1050 if (CI.hasOneUse() && isa<TruncInst>(CI.use_back()))
1051 return 0;
1052
Chris Lattnerd84dfa42010-01-10 01:00:46 +00001053 if (Instruction *I = commonCastTransforms(CI))
Chris Lattner80f43d32010-01-04 07:53:58 +00001054 return I;
1055
Chris Lattnerd84dfa42010-01-10 01:00:46 +00001056 // See if we can simplify any instructions used by the input whose sole
1057 // purpose is to compute bits we don't care about.
1058 if (SimplifyDemandedInstructionBits(CI))
1059 return &CI;
1060
Chris Lattner80f43d32010-01-04 07:53:58 +00001061 Value *Src = CI.getOperand(0);
Chris Lattner75215c92010-01-10 00:58:42 +00001062 const Type *SrcTy = Src->getType(), *DestTy = CI.getType();
1063
Chris Lattner75215c92010-01-10 00:58:42 +00001064 // Attempt to extend the entire input expression tree to the destination
1065 // type. Only do this if the dest type is a simple type, don't convert the
1066 // expression tree to something weird like i93 unless the source is also
1067 // strange.
Duncan Sands1df98592010-02-16 11:11:14 +00001068 if ((DestTy->isVectorTy() || ShouldChangeType(SrcTy, DestTy)) &&
Chris Lattner8cf4f6f2010-01-11 02:43:35 +00001069 CanEvaluateSExtd(Src, DestTy)) {
Chris Lattnerdde5ee52010-01-10 07:40:50 +00001070 // Okay, we can transform this! Insert the new expression now.
1071 DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type"
1072 " to avoid sign extend: " << CI);
1073 Value *Res = EvaluateInDifferentType(Src, DestTy, true);
1074 assert(Res->getType() == DestTy);
1075
Chris Lattner75215c92010-01-10 00:58:42 +00001076 uint32_t SrcBitSize = SrcTy->getScalarSizeInBits();
1077 uint32_t DestBitSize = DestTy->getScalarSizeInBits();
Chris Lattnerdde5ee52010-01-10 07:40:50 +00001078
1079 // If the high bits are already filled with sign bit, just replace this
1080 // cast with the result.
Chris Lattneraa9c8942010-01-10 07:57:20 +00001081 if (ComputeNumSignBits(Res) > DestBitSize - SrcBitSize)
Chris Lattnerdde5ee52010-01-10 07:40:50 +00001082 return ReplaceInstUsesWith(CI, Res);
Chris Lattner75215c92010-01-10 00:58:42 +00001083
Chris Lattnerdde5ee52010-01-10 07:40:50 +00001084 // We need to emit a shl + ashr to do the sign extend.
1085 Value *ShAmt = ConstantInt::get(DestTy, DestBitSize-SrcBitSize);
1086 return BinaryOperator::CreateAShr(Builder->CreateShl(Res, ShAmt, "sext"),
1087 ShAmt);
Chris Lattner75215c92010-01-10 00:58:42 +00001088 }
Chris Lattner80f43d32010-01-04 07:53:58 +00001089
Chris Lattnercd5adbb2010-01-18 22:19:16 +00001090 // If this input is a trunc from our destination, then turn sext(trunc(x))
1091 // into shifts.
1092 if (TruncInst *TI = dyn_cast<TruncInst>(Src))
1093 if (TI->hasOneUse() && TI->getOperand(0)->getType() == DestTy) {
1094 uint32_t SrcBitSize = SrcTy->getScalarSizeInBits();
1095 uint32_t DestBitSize = DestTy->getScalarSizeInBits();
1096
1097 // We need to emit a shl + ashr to do the sign extend.
1098 Value *ShAmt = ConstantInt::get(DestTy, DestBitSize-SrcBitSize);
1099 Value *Res = Builder->CreateShl(TI->getOperand(0), ShAmt, "sext");
1100 return BinaryOperator::CreateAShr(Res, ShAmt);
1101 }
Nate Begeman9a3dc552010-12-17 23:12:19 +00001102
Benjamin Kramer0a30c422011-04-01 20:09:03 +00001103 if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src))
1104 return transformSExtICmp(ICI, CI);
Bill Wendling2d0537c2010-12-17 23:27:41 +00001105
Chris Lattner80f43d32010-01-04 07:53:58 +00001106 // If the input is a shl/ashr pair of a same constant, then this is a sign
1107 // extension from a smaller value. If we could trust arbitrary bitwidth
1108 // integers, we could turn this into a truncate to the smaller bit and then
1109 // use a sext for the whole extension. Since we don't, look deeper and check
1110 // for a truncate. If the source and dest are the same type, eliminate the
1111 // trunc and extend and just do shifts. For example, turn:
1112 // %a = trunc i32 %i to i8
1113 // %b = shl i8 %a, 6
1114 // %c = ashr i8 %b, 6
1115 // %d = sext i8 %c to i32
1116 // into:
1117 // %a = shl i32 %i, 30
1118 // %d = ashr i32 %a, 30
1119 Value *A = 0;
Chris Lattner4f379782010-01-10 01:04:31 +00001120 // TODO: Eventually this could be subsumed by EvaluateInDifferentType.
Chris Lattner80f43d32010-01-04 07:53:58 +00001121 ConstantInt *BA = 0, *CA = 0;
Chris Lattner4f379782010-01-10 01:04:31 +00001122 if (match(Src, m_AShr(m_Shl(m_Trunc(m_Value(A)), m_ConstantInt(BA)),
Chris Lattner80f43d32010-01-04 07:53:58 +00001123 m_ConstantInt(CA))) &&
Chris Lattner4f379782010-01-10 01:04:31 +00001124 BA == CA && A->getType() == CI.getType()) {
1125 unsigned MidSize = Src->getType()->getScalarSizeInBits();
1126 unsigned SrcDstSize = CI.getType()->getScalarSizeInBits();
1127 unsigned ShAmt = CA->getZExtValue()+SrcDstSize-MidSize;
1128 Constant *ShAmtV = ConstantInt::get(CI.getType(), ShAmt);
1129 A = Builder->CreateShl(A, ShAmtV, CI.getName());
1130 return BinaryOperator::CreateAShr(A, ShAmtV);
Chris Lattner80f43d32010-01-04 07:53:58 +00001131 }
1132
1133 return 0;
1134}
1135
1136
1137/// FitsInFPType - Return a Constant* for the specified FP constant if it fits
1138/// in the specified FP type without changing its value.
1139static Constant *FitsInFPType(ConstantFP *CFP, const fltSemantics &Sem) {
1140 bool losesInfo;
1141 APFloat F = CFP->getValueAPF();
1142 (void)F.convert(Sem, APFloat::rmNearestTiesToEven, &losesInfo);
1143 if (!losesInfo)
1144 return ConstantFP::get(CFP->getContext(), F);
1145 return 0;
1146}
1147
1148/// LookThroughFPExtensions - If this is an fp extension instruction, look
1149/// through it until we get the source value.
1150static Value *LookThroughFPExtensions(Value *V) {
1151 if (Instruction *I = dyn_cast<Instruction>(V))
1152 if (I->getOpcode() == Instruction::FPExt)
1153 return LookThroughFPExtensions(I->getOperand(0));
1154
1155 // If this value is a constant, return the constant in the smallest FP type
1156 // that can accurately represent it. This allows us to turn
1157 // (float)((double)X+2.0) into x+2.0f.
1158 if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
1159 if (CFP->getType() == Type::getPPC_FP128Ty(V->getContext()))
1160 return V; // No constant folding of this.
1161 // See if the value can be truncated to float and then reextended.
1162 if (Value *V = FitsInFPType(CFP, APFloat::IEEEsingle))
1163 return V;
Benjamin Kramerf0127052010-01-05 13:12:22 +00001164 if (CFP->getType()->isDoubleTy())
Chris Lattner80f43d32010-01-04 07:53:58 +00001165 return V; // Won't shrink.
1166 if (Value *V = FitsInFPType(CFP, APFloat::IEEEdouble))
1167 return V;
1168 // Don't try to shrink to various long double types.
1169 }
1170
1171 return V;
1172}
1173
1174Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) {
1175 if (Instruction *I = commonCastTransforms(CI))
1176 return I;
1177
1178 // If we have fptrunc(fadd (fpextend x), (fpextend y)), where x and y are
1179 // smaller than the destination type, we can eliminate the truncate by doing
1180 // the add as the smaller type. This applies to fadd/fsub/fmul/fdiv as well
1181 // as many builtins (sqrt, etc).
1182 BinaryOperator *OpI = dyn_cast<BinaryOperator>(CI.getOperand(0));
1183 if (OpI && OpI->hasOneUse()) {
1184 switch (OpI->getOpcode()) {
1185 default: break;
1186 case Instruction::FAdd:
1187 case Instruction::FSub:
1188 case Instruction::FMul:
1189 case Instruction::FDiv:
1190 case Instruction::FRem:
1191 const Type *SrcTy = OpI->getType();
1192 Value *LHSTrunc = LookThroughFPExtensions(OpI->getOperand(0));
1193 Value *RHSTrunc = LookThroughFPExtensions(OpI->getOperand(1));
1194 if (LHSTrunc->getType() != SrcTy &&
1195 RHSTrunc->getType() != SrcTy) {
1196 unsigned DstSize = CI.getType()->getScalarSizeInBits();
1197 // If the source types were both smaller than the destination type of
1198 // the cast, do this xform.
1199 if (LHSTrunc->getType()->getScalarSizeInBits() <= DstSize &&
1200 RHSTrunc->getType()->getScalarSizeInBits() <= DstSize) {
1201 LHSTrunc = Builder->CreateFPExt(LHSTrunc, CI.getType());
1202 RHSTrunc = Builder->CreateFPExt(RHSTrunc, CI.getType());
1203 return BinaryOperator::Create(OpI->getOpcode(), LHSTrunc, RHSTrunc);
1204 }
1205 }
1206 break;
1207 }
1208 }
Owen Andersond9029012010-07-19 08:09:34 +00001209
1210 // Fold (fptrunc (sqrt (fpext x))) -> (sqrtf x)
1211 // NOTE: This should be disabled by -fno-builtin-sqrt if we ever support it.
1212 CallInst *Call = dyn_cast<CallInst>(CI.getOperand(0));
1213 if (Call && Call->getCalledFunction() &&
1214 Call->getCalledFunction()->getName() == "sqrt" &&
1215 Call->getNumArgOperands() == 1) {
1216 CastInst *Arg = dyn_cast<CastInst>(Call->getArgOperand(0));
1217 if (Arg && Arg->getOpcode() == Instruction::FPExt &&
Owen Anderson5f23a932010-07-19 19:23:32 +00001218 CI.getType()->isFloatTy() &&
1219 Call->getType()->isDoubleTy() &&
1220 Arg->getType()->isDoubleTy() &&
1221 Arg->getOperand(0)->getType()->isFloatTy()) {
1222 Function *Callee = Call->getCalledFunction();
1223 Module *M = CI.getParent()->getParent()->getParent();
Chris Lattner979ed442010-09-07 20:01:38 +00001224 Constant *SqrtfFunc = M->getOrInsertFunction("sqrtf",
Owen Anderson5f23a932010-07-19 19:23:32 +00001225 Callee->getAttributes(),
Owen Andersond9029012010-07-19 08:09:34 +00001226 Builder->getFloatTy(),
1227 Builder->getFloatTy(),
1228 NULL);
1229 CallInst *ret = CallInst::Create(SqrtfFunc, Arg->getOperand(0),
1230 "sqrtfcall");
Owen Anderson5f23a932010-07-19 19:23:32 +00001231 ret->setAttributes(Callee->getAttributes());
Chris Lattner979ed442010-09-07 20:01:38 +00001232
1233
1234 // Remove the old Call. With -fmath-errno, it won't get marked readnone.
Eli Friedman3e22cb92011-05-18 00:32:01 +00001235 ReplaceInstUsesWith(*Call, UndefValue::get(Call->getType()));
Chris Lattner979ed442010-09-07 20:01:38 +00001236 EraseInstFromFunction(*Call);
Owen Andersond9029012010-07-19 08:09:34 +00001237 return ret;
1238 }
1239 }
1240
Chris Lattner80f43d32010-01-04 07:53:58 +00001241 return 0;
1242}
1243
1244Instruction *InstCombiner::visitFPExt(CastInst &CI) {
1245 return commonCastTransforms(CI);
1246}
1247
1248Instruction *InstCombiner::visitFPToUI(FPToUIInst &FI) {
1249 Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0));
1250 if (OpI == 0)
1251 return commonCastTransforms(FI);
1252
1253 // fptoui(uitofp(X)) --> X
1254 // fptoui(sitofp(X)) --> X
1255 // This is safe if the intermediate type has enough bits in its mantissa to
1256 // accurately represent all values of X. For example, do not do this with
1257 // i64->float->i64. This is also safe for sitofp case, because any negative
1258 // 'X' value would cause an undefined result for the fptoui.
1259 if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) &&
1260 OpI->getOperand(0)->getType() == FI.getType() &&
1261 (int)FI.getType()->getScalarSizeInBits() < /*extra bit for sign */
1262 OpI->getType()->getFPMantissaWidth())
1263 return ReplaceInstUsesWith(FI, OpI->getOperand(0));
1264
1265 return commonCastTransforms(FI);
1266}
1267
1268Instruction *InstCombiner::visitFPToSI(FPToSIInst &FI) {
1269 Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0));
1270 if (OpI == 0)
1271 return commonCastTransforms(FI);
1272
1273 // fptosi(sitofp(X)) --> X
1274 // fptosi(uitofp(X)) --> X
1275 // This is safe if the intermediate type has enough bits in its mantissa to
1276 // accurately represent all values of X. For example, do not do this with
1277 // i64->float->i64. This is also safe for sitofp case, because any negative
1278 // 'X' value would cause an undefined result for the fptoui.
1279 if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) &&
1280 OpI->getOperand(0)->getType() == FI.getType() &&
1281 (int)FI.getType()->getScalarSizeInBits() <=
1282 OpI->getType()->getFPMantissaWidth())
1283 return ReplaceInstUsesWith(FI, OpI->getOperand(0));
1284
1285 return commonCastTransforms(FI);
1286}
1287
1288Instruction *InstCombiner::visitUIToFP(CastInst &CI) {
1289 return commonCastTransforms(CI);
1290}
1291
1292Instruction *InstCombiner::visitSIToFP(CastInst &CI) {
1293 return commonCastTransforms(CI);
1294}
1295
Chris Lattner80f43d32010-01-04 07:53:58 +00001296Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) {
Dan Gohman3b5487e2010-02-02 01:44:02 +00001297 // If the source integer type is not the intptr_t type for this target, do a
1298 // trunc or zext to the intptr_t type, then inttoptr of it. This allows the
1299 // cast to be exposed to other transforms.
1300 if (TD) {
1301 if (CI.getOperand(0)->getType()->getScalarSizeInBits() >
1302 TD->getPointerSizeInBits()) {
1303 Value *P = Builder->CreateTrunc(CI.getOperand(0),
1304 TD->getIntPtrType(CI.getContext()), "tmp");
1305 return new IntToPtrInst(P, CI.getType());
1306 }
1307 if (CI.getOperand(0)->getType()->getScalarSizeInBits() <
1308 TD->getPointerSizeInBits()) {
1309 Value *P = Builder->CreateZExt(CI.getOperand(0),
1310 TD->getIntPtrType(CI.getContext()), "tmp");
1311 return new IntToPtrInst(P, CI.getType());
1312 }
Chris Lattner80f43d32010-01-04 07:53:58 +00001313 }
1314
1315 if (Instruction *I = commonCastTransforms(CI))
1316 return I;
1317
1318 return 0;
1319}
1320
Chris Lattner7a34d6c2010-01-05 22:21:18 +00001321/// @brief Implement the transforms for cast of pointer (bitcast/ptrtoint)
1322Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) {
1323 Value *Src = CI.getOperand(0);
1324
1325 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) {
1326 // If casting the result of a getelementptr instruction with no offset, turn
1327 // this into a cast of the original pointer!
1328 if (GEP->hasAllZeroIndices()) {
1329 // Changing the cast operand is usually not a good idea but it is safe
1330 // here because the pointer operand is being replaced with another
1331 // pointer operand so the opcode doesn't need to change.
1332 Worklist.Add(GEP);
1333 CI.setOperand(0, GEP->getOperand(0));
1334 return &CI;
1335 }
1336
1337 // If the GEP has a single use, and the base pointer is a bitcast, and the
1338 // GEP computes a constant offset, see if we can convert these three
1339 // instructions into fewer. This typically happens with unions and other
1340 // non-type-safe code.
1341 if (TD && GEP->hasOneUse() && isa<BitCastInst>(GEP->getOperand(0)) &&
1342 GEP->hasAllConstantIndices()) {
1343 // We are guaranteed to get a constant from EmitGEPOffset.
1344 ConstantInt *OffsetV = cast<ConstantInt>(EmitGEPOffset(GEP));
1345 int64_t Offset = OffsetV->getSExtValue();
1346
1347 // Get the base pointer input of the bitcast, and the type it points to.
1348 Value *OrigBase = cast<BitCastInst>(GEP->getOperand(0))->getOperand(0);
1349 const Type *GEPIdxTy =
1350 cast<PointerType>(OrigBase->getType())->getElementType();
1351 SmallVector<Value*, 8> NewIndices;
1352 if (FindElementAtOffset(GEPIdxTy, Offset, NewIndices)) {
1353 // If we were able to index down into an element, create the GEP
1354 // and bitcast the result. This eliminates one bitcast, potentially
1355 // two.
1356 Value *NGEP = cast<GEPOperator>(GEP)->isInBounds() ?
1357 Builder->CreateInBoundsGEP(OrigBase,
1358 NewIndices.begin(), NewIndices.end()) :
1359 Builder->CreateGEP(OrigBase, NewIndices.begin(), NewIndices.end());
1360 NGEP->takeName(GEP);
1361
1362 if (isa<BitCastInst>(CI))
1363 return new BitCastInst(NGEP, CI.getType());
1364 assert(isa<PtrToIntInst>(CI));
1365 return new PtrToIntInst(NGEP, CI.getType());
1366 }
1367 }
1368 }
1369
1370 return commonCastTransforms(CI);
1371}
1372
1373Instruction *InstCombiner::visitPtrToInt(PtrToIntInst &CI) {
Dan Gohman3b5487e2010-02-02 01:44:02 +00001374 // If the destination integer type is not the intptr_t type for this target,
1375 // do a ptrtoint to intptr_t then do a trunc or zext. This allows the cast
1376 // to be exposed to other transforms.
1377 if (TD) {
1378 if (CI.getType()->getScalarSizeInBits() < TD->getPointerSizeInBits()) {
1379 Value *P = Builder->CreatePtrToInt(CI.getOperand(0),
1380 TD->getIntPtrType(CI.getContext()),
1381 "tmp");
1382 return new TruncInst(P, CI.getType());
1383 }
1384 if (CI.getType()->getScalarSizeInBits() > TD->getPointerSizeInBits()) {
1385 Value *P = Builder->CreatePtrToInt(CI.getOperand(0),
1386 TD->getIntPtrType(CI.getContext()),
1387 "tmp");
1388 return new ZExtInst(P, CI.getType());
1389 }
Chris Lattner7a34d6c2010-01-05 22:21:18 +00001390 }
1391
1392 return commonPointerCastTransforms(CI);
1393}
1394
Chris Lattner67451912010-05-08 21:50:26 +00001395/// OptimizeVectorResize - This input value (which is known to have vector type)
1396/// is being zero extended or truncated to the specified vector type. Try to
1397/// replace it with a shuffle (and vector/vector bitcast) if possible.
1398///
1399/// The source and destination vector types may have different element types.
1400static Instruction *OptimizeVectorResize(Value *InVal, const VectorType *DestTy,
1401 InstCombiner &IC) {
1402 // We can only do this optimization if the output is a multiple of the input
1403 // element size, or the input is a multiple of the output element size.
1404 // Convert the input type to have the same element type as the output.
1405 const VectorType *SrcTy = cast<VectorType>(InVal->getType());
1406
1407 if (SrcTy->getElementType() != DestTy->getElementType()) {
1408 // The input types don't need to be identical, but for now they must be the
1409 // same size. There is no specific reason we couldn't handle things like
1410 // <4 x i16> -> <4 x i32> by bitcasting to <2 x i32> but haven't gotten
1411 // there yet.
1412 if (SrcTy->getElementType()->getPrimitiveSizeInBits() !=
1413 DestTy->getElementType()->getPrimitiveSizeInBits())
1414 return 0;
1415
1416 SrcTy = VectorType::get(DestTy->getElementType(), SrcTy->getNumElements());
1417 InVal = IC.Builder->CreateBitCast(InVal, SrcTy);
1418 }
1419
1420 // Now that the element types match, get the shuffle mask and RHS of the
1421 // shuffle to use, which depends on whether we're increasing or decreasing the
1422 // size of the input.
1423 SmallVector<Constant*, 16> ShuffleMask;
1424 Value *V2;
1425 const IntegerType *Int32Ty = Type::getInt32Ty(SrcTy->getContext());
1426
1427 if (SrcTy->getNumElements() > DestTy->getNumElements()) {
1428 // If we're shrinking the number of elements, just shuffle in the low
1429 // elements from the input and use undef as the second shuffle input.
1430 V2 = UndefValue::get(SrcTy);
1431 for (unsigned i = 0, e = DestTy->getNumElements(); i != e; ++i)
1432 ShuffleMask.push_back(ConstantInt::get(Int32Ty, i));
1433
1434 } else {
1435 // If we're increasing the number of elements, shuffle in all of the
1436 // elements from InVal and fill the rest of the result elements with zeros
1437 // from a constant zero.
1438 V2 = Constant::getNullValue(SrcTy);
1439 unsigned SrcElts = SrcTy->getNumElements();
1440 for (unsigned i = 0, e = SrcElts; i != e; ++i)
1441 ShuffleMask.push_back(ConstantInt::get(Int32Ty, i));
1442
1443 // The excess elements reference the first element of the zero input.
1444 ShuffleMask.append(DestTy->getNumElements()-SrcElts,
1445 ConstantInt::get(Int32Ty, SrcElts));
1446 }
1447
Chris Lattner2ca5c862011-02-15 00:14:00 +00001448 return new ShuffleVectorInst(InVal, V2, ConstantVector::get(ShuffleMask));
Chris Lattner67451912010-05-08 21:50:26 +00001449}
1450
Chris Lattner3dd08732010-08-28 01:20:38 +00001451static bool isMultipleOfTypeSize(unsigned Value, const Type *Ty) {
1452 return Value % Ty->getPrimitiveSizeInBits() == 0;
1453}
1454
Chris Lattner79007792010-08-28 01:50:57 +00001455static unsigned getTypeSizeIndex(unsigned Value, const Type *Ty) {
Chris Lattner3dd08732010-08-28 01:20:38 +00001456 return Value / Ty->getPrimitiveSizeInBits();
1457}
1458
1459/// CollectInsertionElements - V is a value which is inserted into a vector of
1460/// VecEltTy. Look through the value to see if we can decompose it into
1461/// insertions into the vector. See the example in the comment for
1462/// OptimizeIntegerToVectorInsertions for the pattern this handles.
1463/// The type of V is always a non-zero multiple of VecEltTy's size.
1464///
1465/// This returns false if the pattern can't be matched or true if it can,
1466/// filling in Elements with the elements found here.
1467static bool CollectInsertionElements(Value *V, unsigned ElementIndex,
1468 SmallVectorImpl<Value*> &Elements,
1469 const Type *VecEltTy) {
Chris Lattner157d4ea2010-08-28 03:36:51 +00001470 // Undef values never contribute useful bits to the result.
1471 if (isa<UndefValue>(V)) return true;
1472
Chris Lattner3dd08732010-08-28 01:20:38 +00001473 // If we got down to a value of the right type, we win, try inserting into the
1474 // right element.
1475 if (V->getType() == VecEltTy) {
Chris Lattner79007792010-08-28 01:50:57 +00001476 // Inserting null doesn't actually insert any elements.
1477 if (Constant *C = dyn_cast<Constant>(V))
1478 if (C->isNullValue())
1479 return true;
1480
Chris Lattner3dd08732010-08-28 01:20:38 +00001481 // Fail if multiple elements are inserted into this slot.
1482 if (ElementIndex >= Elements.size() || Elements[ElementIndex] != 0)
1483 return false;
1484
1485 Elements[ElementIndex] = V;
1486 return true;
1487 }
1488
Chris Lattner79007792010-08-28 01:50:57 +00001489 if (Constant *C = dyn_cast<Constant>(V)) {
Chris Lattner3dd08732010-08-28 01:20:38 +00001490 // Figure out the # elements this provides, and bitcast it or slice it up
1491 // as required.
Chris Lattner79007792010-08-28 01:50:57 +00001492 unsigned NumElts = getTypeSizeIndex(C->getType()->getPrimitiveSizeInBits(),
1493 VecEltTy);
1494 // If the constant is the size of a vector element, we just need to bitcast
1495 // it to the right type so it gets properly inserted.
1496 if (NumElts == 1)
1497 return CollectInsertionElements(ConstantExpr::getBitCast(C, VecEltTy),
1498 ElementIndex, Elements, VecEltTy);
1499
1500 // Okay, this is a constant that covers multiple elements. Slice it up into
1501 // pieces and insert each element-sized piece into the vector.
1502 if (!isa<IntegerType>(C->getType()))
1503 C = ConstantExpr::getBitCast(C, IntegerType::get(V->getContext(),
1504 C->getType()->getPrimitiveSizeInBits()));
1505 unsigned ElementSize = VecEltTy->getPrimitiveSizeInBits();
1506 const Type *ElementIntTy = IntegerType::get(C->getContext(), ElementSize);
1507
1508 for (unsigned i = 0; i != NumElts; ++i) {
1509 Constant *Piece = ConstantExpr::getLShr(C, ConstantInt::get(C->getType(),
1510 i*ElementSize));
1511 Piece = ConstantExpr::getTrunc(Piece, ElementIntTy);
1512 if (!CollectInsertionElements(Piece, ElementIndex+i, Elements, VecEltTy))
1513 return false;
1514 }
1515 return true;
1516 }
Chris Lattner3dd08732010-08-28 01:20:38 +00001517
1518 if (!V->hasOneUse()) return false;
1519
1520 Instruction *I = dyn_cast<Instruction>(V);
1521 if (I == 0) return false;
1522 switch (I->getOpcode()) {
1523 default: return false; // Unhandled case.
1524 case Instruction::BitCast:
1525 return CollectInsertionElements(I->getOperand(0), ElementIndex,
1526 Elements, VecEltTy);
1527 case Instruction::ZExt:
1528 if (!isMultipleOfTypeSize(
1529 I->getOperand(0)->getType()->getPrimitiveSizeInBits(),
1530 VecEltTy))
1531 return false;
1532 return CollectInsertionElements(I->getOperand(0), ElementIndex,
1533 Elements, VecEltTy);
1534 case Instruction::Or:
1535 return CollectInsertionElements(I->getOperand(0), ElementIndex,
1536 Elements, VecEltTy) &&
1537 CollectInsertionElements(I->getOperand(1), ElementIndex,
1538 Elements, VecEltTy);
1539 case Instruction::Shl: {
1540 // Must be shifting by a constant that is a multiple of the element size.
1541 ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1));
1542 if (CI == 0) return false;
1543 if (!isMultipleOfTypeSize(CI->getZExtValue(), VecEltTy)) return false;
1544 unsigned IndexShift = getTypeSizeIndex(CI->getZExtValue(), VecEltTy);
1545
1546 return CollectInsertionElements(I->getOperand(0), ElementIndex+IndexShift,
1547 Elements, VecEltTy);
1548 }
1549
1550 }
1551}
1552
1553
1554/// OptimizeIntegerToVectorInsertions - If the input is an 'or' instruction, we
1555/// may be doing shifts and ors to assemble the elements of the vector manually.
1556/// Try to rip the code out and replace it with insertelements. This is to
1557/// optimize code like this:
1558///
1559/// %tmp37 = bitcast float %inc to i32
1560/// %tmp38 = zext i32 %tmp37 to i64
1561/// %tmp31 = bitcast float %inc5 to i32
1562/// %tmp32 = zext i32 %tmp31 to i64
1563/// %tmp33 = shl i64 %tmp32, 32
1564/// %ins35 = or i64 %tmp33, %tmp38
1565/// %tmp43 = bitcast i64 %ins35 to <2 x float>
1566///
1567/// Into two insertelements that do "buildvector{%inc, %inc5}".
1568static Value *OptimizeIntegerToVectorInsertions(BitCastInst &CI,
1569 InstCombiner &IC) {
1570 const VectorType *DestVecTy = cast<VectorType>(CI.getType());
1571 Value *IntInput = CI.getOperand(0);
1572
1573 SmallVector<Value*, 8> Elements(DestVecTy->getNumElements());
1574 if (!CollectInsertionElements(IntInput, 0, Elements,
1575 DestVecTy->getElementType()))
1576 return 0;
1577
1578 // If we succeeded, we know that all of the element are specified by Elements
1579 // or are zero if Elements has a null entry. Recast this as a set of
1580 // insertions.
1581 Value *Result = Constant::getNullValue(CI.getType());
1582 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
1583 if (Elements[i] == 0) continue; // Unset element.
1584
1585 Result = IC.Builder->CreateInsertElement(Result, Elements[i],
1586 IC.Builder->getInt32(i));
1587 }
1588
1589 return Result;
1590}
1591
1592
Chris Lattnere5a14262010-08-26 21:55:42 +00001593/// OptimizeIntToFloatBitCast - See if we can optimize an integer->float/double
1594/// bitcast. The various long double bitcasts can't get in here.
Chris Lattner26dbe7e2010-08-26 22:14:59 +00001595static Instruction *OptimizeIntToFloatBitCast(BitCastInst &CI,InstCombiner &IC){
Chris Lattnere5a14262010-08-26 21:55:42 +00001596 Value *Src = CI.getOperand(0);
Chris Lattner26dbe7e2010-08-26 22:14:59 +00001597 const Type *DestTy = CI.getType();
Chris Lattnere5a14262010-08-26 21:55:42 +00001598
1599 // If this is a bitcast from int to float, check to see if the int is an
1600 // extraction from a vector.
1601 Value *VecInput = 0;
Chris Lattner26dbe7e2010-08-26 22:14:59 +00001602 // bitcast(trunc(bitcast(somevector)))
Chris Lattnere5a14262010-08-26 21:55:42 +00001603 if (match(Src, m_Trunc(m_BitCast(m_Value(VecInput)))) &&
1604 isa<VectorType>(VecInput->getType())) {
1605 const VectorType *VecTy = cast<VectorType>(VecInput->getType());
Chris Lattner26dbe7e2010-08-26 22:14:59 +00001606 unsigned DestWidth = DestTy->getPrimitiveSizeInBits();
1607
1608 if (VecTy->getPrimitiveSizeInBits() % DestWidth == 0) {
1609 // If the element type of the vector doesn't match the result type,
1610 // bitcast it to be a vector type we can extract from.
1611 if (VecTy->getElementType() != DestTy) {
1612 VecTy = VectorType::get(DestTy,
1613 VecTy->getPrimitiveSizeInBits() / DestWidth);
1614 VecInput = IC.Builder->CreateBitCast(VecInput, VecTy);
1615 }
Chris Lattnere5a14262010-08-26 21:55:42 +00001616
Chris Lattnere5a14262010-08-26 21:55:42 +00001617 return ExtractElementInst::Create(VecInput, IC.Builder->getInt32(0));
Chris Lattner26dbe7e2010-08-26 22:14:59 +00001618 }
Chris Lattnere5a14262010-08-26 21:55:42 +00001619 }
1620
Chris Lattner26dbe7e2010-08-26 22:14:59 +00001621 // bitcast(trunc(lshr(bitcast(somevector), cst))
1622 ConstantInt *ShAmt = 0;
1623 if (match(Src, m_Trunc(m_LShr(m_BitCast(m_Value(VecInput)),
1624 m_ConstantInt(ShAmt)))) &&
1625 isa<VectorType>(VecInput->getType())) {
1626 const VectorType *VecTy = cast<VectorType>(VecInput->getType());
1627 unsigned DestWidth = DestTy->getPrimitiveSizeInBits();
1628 if (VecTy->getPrimitiveSizeInBits() % DestWidth == 0 &&
1629 ShAmt->getZExtValue() % DestWidth == 0) {
1630 // If the element type of the vector doesn't match the result type,
1631 // bitcast it to be a vector type we can extract from.
1632 if (VecTy->getElementType() != DestTy) {
1633 VecTy = VectorType::get(DestTy,
1634 VecTy->getPrimitiveSizeInBits() / DestWidth);
1635 VecInput = IC.Builder->CreateBitCast(VecInput, VecTy);
1636 }
1637
1638 unsigned Elt = ShAmt->getZExtValue() / DestWidth;
1639 return ExtractElementInst::Create(VecInput, IC.Builder->getInt32(Elt));
1640 }
1641 }
Chris Lattnere5a14262010-08-26 21:55:42 +00001642 return 0;
1643}
Chris Lattner67451912010-05-08 21:50:26 +00001644
Chris Lattner80f43d32010-01-04 07:53:58 +00001645Instruction *InstCombiner::visitBitCast(BitCastInst &CI) {
1646 // If the operands are integer typed then apply the integer transforms,
1647 // otherwise just apply the common ones.
1648 Value *Src = CI.getOperand(0);
1649 const Type *SrcTy = Src->getType();
1650 const Type *DestTy = CI.getType();
1651
Chris Lattner80f43d32010-01-04 07:53:58 +00001652 // Get rid of casts from one type to the same type. These are useless and can
1653 // be replaced by the operand.
1654 if (DestTy == Src->getType())
1655 return ReplaceInstUsesWith(CI, Src);
1656
1657 if (const PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) {
1658 const PointerType *SrcPTy = cast<PointerType>(SrcTy);
1659 const Type *DstElTy = DstPTy->getElementType();
1660 const Type *SrcElTy = SrcPTy->getElementType();
1661
1662 // If the address spaces don't match, don't eliminate the bitcast, which is
1663 // required for changing types.
1664 if (SrcPTy->getAddressSpace() != DstPTy->getAddressSpace())
1665 return 0;
1666
1667 // If we are casting a alloca to a pointer to a type of the same
1668 // size, rewrite the allocation instruction to allocate the "right" type.
1669 // There is no need to modify malloc calls because it is their bitcast that
1670 // needs to be cleaned up.
1671 if (AllocaInst *AI = dyn_cast<AllocaInst>(Src))
1672 if (Instruction *V = PromoteCastOfAllocation(CI, *AI))
1673 return V;
1674
1675 // If the source and destination are pointers, and this cast is equivalent
1676 // to a getelementptr X, 0, 0, 0... turn it into the appropriate gep.
1677 // This can enhance SROA and other transforms that want type-safe pointers.
1678 Constant *ZeroUInt =
1679 Constant::getNullValue(Type::getInt32Ty(CI.getContext()));
1680 unsigned NumZeros = 0;
1681 while (SrcElTy != DstElTy &&
Duncan Sands1df98592010-02-16 11:11:14 +00001682 isa<CompositeType>(SrcElTy) && !SrcElTy->isPointerTy() &&
Chris Lattner80f43d32010-01-04 07:53:58 +00001683 SrcElTy->getNumContainedTypes() /* not "{}" */) {
1684 SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt);
1685 ++NumZeros;
1686 }
1687
1688 // If we found a path from the src to dest, create the getelementptr now.
1689 if (SrcElTy == DstElTy) {
1690 SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt);
Eli Friedman107ffd52011-05-18 23:11:30 +00001691 return GetElementPtrInst::CreateInBounds(Src, Idxs.begin(), Idxs.end());
Chris Lattner80f43d32010-01-04 07:53:58 +00001692 }
1693 }
Chris Lattnere5a14262010-08-26 21:55:42 +00001694
1695 // Try to optimize int -> float bitcasts.
1696 if ((DestTy->isFloatTy() || DestTy->isDoubleTy()) && isa<IntegerType>(SrcTy))
1697 if (Instruction *I = OptimizeIntToFloatBitCast(CI, *this))
1698 return I;
Chris Lattner80f43d32010-01-04 07:53:58 +00001699
1700 if (const VectorType *DestVTy = dyn_cast<VectorType>(DestTy)) {
Duncan Sands1df98592010-02-16 11:11:14 +00001701 if (DestVTy->getNumElements() == 1 && !SrcTy->isVectorTy()) {
Chris Lattner7a34d6c2010-01-05 22:21:18 +00001702 Value *Elem = Builder->CreateBitCast(Src, DestVTy->getElementType());
1703 return InsertElementInst::Create(UndefValue::get(DestTy), Elem,
Chris Lattner80f43d32010-01-04 07:53:58 +00001704 Constant::getNullValue(Type::getInt32Ty(CI.getContext())));
Chris Lattner80f43d32010-01-04 07:53:58 +00001705 // FIXME: Canonicalize bitcast(insertelement) -> insertelement(bitcast)
1706 }
Chris Lattner67451912010-05-08 21:50:26 +00001707
Chris Lattner3dd08732010-08-28 01:20:38 +00001708 if (isa<IntegerType>(SrcTy)) {
1709 // If this is a cast from an integer to vector, check to see if the input
1710 // is a trunc or zext of a bitcast from vector. If so, we can replace all
1711 // the casts with a shuffle and (potentially) a bitcast.
1712 if (isa<TruncInst>(Src) || isa<ZExtInst>(Src)) {
1713 CastInst *SrcCast = cast<CastInst>(Src);
1714 if (BitCastInst *BCIn = dyn_cast<BitCastInst>(SrcCast->getOperand(0)))
1715 if (isa<VectorType>(BCIn->getOperand(0)->getType()))
1716 if (Instruction *I = OptimizeVectorResize(BCIn->getOperand(0),
Chris Lattner67451912010-05-08 21:50:26 +00001717 cast<VectorType>(DestTy), *this))
Chris Lattner3dd08732010-08-28 01:20:38 +00001718 return I;
1719 }
1720
1721 // If the input is an 'or' instruction, we may be doing shifts and ors to
1722 // assemble the elements of the vector manually. Try to rip the code out
1723 // and replace it with insertelements.
1724 if (Value *V = OptimizeIntegerToVectorInsertions(CI, *this))
1725 return ReplaceInstUsesWith(CI, V);
Chris Lattner67451912010-05-08 21:50:26 +00001726 }
Chris Lattner80f43d32010-01-04 07:53:58 +00001727 }
1728
1729 if (const VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy)) {
Duncan Sands1df98592010-02-16 11:11:14 +00001730 if (SrcVTy->getNumElements() == 1 && !DestTy->isVectorTy()) {
Chris Lattner7a34d6c2010-01-05 22:21:18 +00001731 Value *Elem =
1732 Builder->CreateExtractElement(Src,
1733 Constant::getNullValue(Type::getInt32Ty(CI.getContext())));
1734 return CastInst::Create(Instruction::BitCast, Elem, DestTy);
Chris Lattner80f43d32010-01-04 07:53:58 +00001735 }
1736 }
1737
1738 if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) {
Chris Lattner7a34d6c2010-01-05 22:21:18 +00001739 // Okay, we have (bitcast (shuffle ..)). Check to see if this is
Dan Gohmana5ced592010-04-07 23:22:42 +00001740 // a bitcast to a vector with the same # elts.
Duncan Sands1df98592010-02-16 11:11:14 +00001741 if (SVI->hasOneUse() && DestTy->isVectorTy() &&
Chris Lattner7a34d6c2010-01-05 22:21:18 +00001742 cast<VectorType>(DestTy)->getNumElements() ==
1743 SVI->getType()->getNumElements() &&
1744 SVI->getType()->getNumElements() ==
1745 cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements()) {
1746 BitCastInst *Tmp;
1747 // If either of the operands is a cast from CI.getType(), then
1748 // evaluating the shuffle in the casted destination's type will allow
1749 // us to eliminate at least one cast.
1750 if (((Tmp = dyn_cast<BitCastInst>(SVI->getOperand(0))) &&
1751 Tmp->getOperand(0)->getType() == DestTy) ||
1752 ((Tmp = dyn_cast<BitCastInst>(SVI->getOperand(1))) &&
1753 Tmp->getOperand(0)->getType() == DestTy)) {
1754 Value *LHS = Builder->CreateBitCast(SVI->getOperand(0), DestTy);
1755 Value *RHS = Builder->CreateBitCast(SVI->getOperand(1), DestTy);
1756 // Return a new shuffle vector. Use the same element ID's, as we
1757 // know the vector types match #elts.
1758 return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2));
Chris Lattner80f43d32010-01-04 07:53:58 +00001759 }
1760 }
1761 }
Chris Lattner7a34d6c2010-01-05 22:21:18 +00001762
Duncan Sands1df98592010-02-16 11:11:14 +00001763 if (SrcTy->isPointerTy())
Chris Lattner7a34d6c2010-01-05 22:21:18 +00001764 return commonPointerCastTransforms(CI);
1765 return commonCastTransforms(CI);
Chris Lattner80f43d32010-01-04 07:53:58 +00001766}