blob: be18311846c7342e7fcc01ebc2643af97abc5617 [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,
25 int &Offset) {
Benjamin Kramer11acaa32010-01-05 20:07:06 +000026 assert(Val->getType()->isInteger(32) && "Unexpected allocation size type!");
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +000027 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
28 Offset = CI->getZExtValue();
29 Scale = 0;
30 return ConstantInt::get(Type::getInt32Ty(Val->getContext()), 0);
Chris Lattnerf86d7992010-01-05 20:57:30 +000031 }
32
33 if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) {
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +000034 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
35 if (I->getOpcode() == Instruction::Shl) {
36 // This is a value scaled by '1 << the shift amt'.
37 Scale = 1U << RHS->getZExtValue();
38 Offset = 0;
39 return I->getOperand(0);
Chris Lattnerf86d7992010-01-05 20:57:30 +000040 }
41
42 if (I->getOpcode() == Instruction::Mul) {
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +000043 // This value is scaled by 'RHS'.
44 Scale = RHS->getZExtValue();
45 Offset = 0;
46 return I->getOperand(0);
Chris Lattnerf86d7992010-01-05 20:57:30 +000047 }
48
49 if (I->getOpcode() == Instruction::Add) {
Chris Lattnerf3d1b5d2010-01-04 07:59:07 +000050 // We have X+C. Check to see if we really have (X*C2)+C1,
51 // where C1 is divisible by C2.
52 unsigned SubScale;
53 Value *SubVal =
54 DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset);
55 Offset += RHS->getZExtValue();
56 Scale = SubScale;
57 return SubVal;
58 }
59 }
60 }
61
62 // Otherwise, we can't look past this.
63 Scale = 1;
64 Offset = 0;
65 return Val;
66}
67
68/// PromoteCastOfAllocation - If we find a cast of an allocation instruction,
69/// try to eliminate the cast by moving the type information into the alloc.
70Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI,
71 AllocaInst &AI) {
72 // This requires TargetData to get the alloca alignment and size information.
73 if (!TD) return 0;
74
75 const PointerType *PTy = cast<PointerType>(CI.getType());
76
77 BuilderTy AllocaBuilder(*Builder);
78 AllocaBuilder.SetInsertPoint(AI.getParent(), &AI);
79
80 // Get the type really allocated and the type casted to.
81 const Type *AllocElTy = AI.getAllocatedType();
82 const Type *CastElTy = PTy->getElementType();
83 if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0;
84
85 unsigned AllocElTyAlign = TD->getABITypeAlignment(AllocElTy);
86 unsigned CastElTyAlign = TD->getABITypeAlignment(CastElTy);
87 if (CastElTyAlign < AllocElTyAlign) return 0;
88
89 // If the allocation has multiple uses, only promote it if we are strictly
90 // increasing the alignment of the resultant allocation. If we keep it the
91 // same, we open the door to infinite loops of various kinds. (A reference
92 // from a dbg.declare doesn't count as a use for this purpose.)
93 if (!AI.hasOneUse() && !hasOneUsePlusDeclare(&AI) &&
94 CastElTyAlign == AllocElTyAlign) return 0;
95
96 uint64_t AllocElTySize = TD->getTypeAllocSize(AllocElTy);
97 uint64_t CastElTySize = TD->getTypeAllocSize(CastElTy);
98 if (CastElTySize == 0 || AllocElTySize == 0) return 0;
99
100 // See if we can satisfy the modulus by pulling a scale out of the array
101 // size argument.
102 unsigned ArraySizeScale;
103 int ArrayOffset;
104 Value *NumElements = // See if the array size is a decomposable linear expr.
105 DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset);
106
107 // If we can now satisfy the modulus, by using a non-1 scale, we really can
108 // do the xform.
109 if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 ||
110 (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return 0;
111
112 unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize;
113 Value *Amt = 0;
114 if (Scale == 1) {
115 Amt = NumElements;
116 } else {
117 Amt = ConstantInt::get(Type::getInt32Ty(CI.getContext()), Scale);
118 // Insert before the alloca, not before the cast.
119 Amt = AllocaBuilder.CreateMul(Amt, NumElements, "tmp");
120 }
121
122 if (int Offset = (AllocElTySize*ArrayOffset)/CastElTySize) {
123 Value *Off = ConstantInt::get(Type::getInt32Ty(CI.getContext()),
124 Offset, true);
125 Amt = AllocaBuilder.CreateAdd(Amt, Off, "tmp");
126 }
127
128 AllocaInst *New = AllocaBuilder.CreateAlloca(CastElTy, Amt);
129 New->setAlignment(AI.getAlignment());
130 New->takeName(&AI);
131
132 // If the allocation has one real use plus a dbg.declare, just remove the
133 // declare.
134 if (DbgDeclareInst *DI = hasOneUsePlusDeclare(&AI)) {
135 EraseInstFromFunction(*(Instruction*)DI);
136 }
137 // If the allocation has multiple real uses, insert a cast and change all
138 // things that used it to use the new cast. This will also hack on CI, but it
139 // will die soon.
140 else if (!AI.hasOneUse()) {
141 // New is the allocation instruction, pointer typed. AI is the original
142 // allocation instruction, also pointer typed. Thus, cast to use is BitCast.
143 Value *NewCast = AllocaBuilder.CreateBitCast(New, AI.getType(), "tmpcast");
144 AI.replaceAllUsesWith(NewCast);
145 }
146 return ReplaceInstUsesWith(CI, New);
147}
148
149
Chris Lattner14bf8f02010-01-08 19:19:23 +0000150/// CanEvaluateTruncated - Return true if we can evaluate the specified
151/// expression tree as type Ty instead of its larger type, and arrive with the
152/// same value. This is used by code that tries to eliminate truncates.
Chris Lattner5f0290e2010-01-04 07:54:59 +0000153///
Chris Lattner14bf8f02010-01-08 19:19:23 +0000154/// Ty will always be a type smaller than V. We should return true if trunc(V)
155/// can be computed by computing V in the smaller type. If V is an instruction,
156/// then trunc(inst(x,y)) can be computed as inst(trunc(x),trunc(y)), which only
157/// makes sense if x and y can be efficiently truncated.
Chris Lattner5f0290e2010-01-04 07:54:59 +0000158///
Chris Lattner14bf8f02010-01-08 19:19:23 +0000159static bool CanEvaluateTruncated(Value *V, const Type *Ty) {
Chris Lattner5f0290e2010-01-04 07:54:59 +0000160 // We can always evaluate constants in another type.
161 if (isa<Constant>(V))
162 return true;
163
164 Instruction *I = dyn_cast<Instruction>(V);
165 if (!I) return false;
166
167 const Type *OrigTy = V->getType();
168
Chris Lattner14bf8f02010-01-08 19:19:23 +0000169 // If this is an extension from the dest type, we can eliminate it.
170 if ((isa<ZExtInst>(I) || isa<SExtInst>(I)) &&
171 I->getOperand(0)->getType() == Ty)
172 return true;
Chris Lattner5f0290e2010-01-04 07:54:59 +0000173
174 // We can't extend or shrink something that has multiple uses: doing so would
175 // require duplicating the instruction in general, which isn't profitable.
176 if (!I->hasOneUse()) return false;
177
178 unsigned Opc = I->getOpcode();
179 switch (Opc) {
180 case Instruction::Add:
181 case Instruction::Sub:
182 case Instruction::Mul:
183 case Instruction::And:
184 case Instruction::Or:
185 case Instruction::Xor:
186 // These operators can all arbitrarily be extended or truncated.
Chris Lattner14bf8f02010-01-08 19:19:23 +0000187 return CanEvaluateTruncated(I->getOperand(0), Ty) &&
188 CanEvaluateTruncated(I->getOperand(1), Ty);
Chris Lattner5f0290e2010-01-04 07:54:59 +0000189
190 case Instruction::UDiv:
191 case Instruction::URem: {
192 // UDiv and URem can be truncated if all the truncated bits are zero.
193 uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
194 uint32_t BitWidth = Ty->getScalarSizeInBits();
195 if (BitWidth < OrigBitWidth) {
196 APInt Mask = APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth);
197 if (MaskedValueIsZero(I->getOperand(0), Mask) &&
198 MaskedValueIsZero(I->getOperand(1), Mask)) {
Chris Lattner14bf8f02010-01-08 19:19:23 +0000199 return CanEvaluateTruncated(I->getOperand(0), Ty) &&
200 CanEvaluateTruncated(I->getOperand(1), Ty);
Chris Lattner5f0290e2010-01-04 07:54:59 +0000201 }
202 }
203 break;
204 }
205 case Instruction::Shl:
206 // If we are truncating the result of this SHL, and if it's a shift of a
207 // constant amount, we can always perform a SHL in a smaller type.
208 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
209 uint32_t BitWidth = Ty->getScalarSizeInBits();
Chris Lattner14bf8f02010-01-08 19:19:23 +0000210 if (CI->getLimitedValue(BitWidth) < BitWidth)
211 return CanEvaluateTruncated(I->getOperand(0), Ty);
Chris Lattner5f0290e2010-01-04 07:54:59 +0000212 }
213 break;
214 case Instruction::LShr:
215 // If this is a truncate of a logical shr, we can truncate it to a smaller
216 // lshr iff we know that the bits we would otherwise be shifting in are
217 // already zeros.
218 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
219 uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
220 uint32_t BitWidth = Ty->getScalarSizeInBits();
Chris Lattner14bf8f02010-01-08 19:19:23 +0000221 if (MaskedValueIsZero(I->getOperand(0),
Chris Lattner5f0290e2010-01-04 07:54:59 +0000222 APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) &&
223 CI->getLimitedValue(BitWidth) < BitWidth) {
Chris Lattner14bf8f02010-01-08 19:19:23 +0000224 return CanEvaluateTruncated(I->getOperand(0), Ty);
Chris Lattner5f0290e2010-01-04 07:54:59 +0000225 }
226 }
227 break;
Chris Lattner5f0290e2010-01-04 07:54:59 +0000228 case Instruction::Trunc:
Chris Lattner14bf8f02010-01-08 19:19:23 +0000229 // trunc(trunc(x)) -> trunc(x)
230 return true;
Chris Lattner5f0290e2010-01-04 07:54:59 +0000231 case Instruction::Select: {
232 SelectInst *SI = cast<SelectInst>(I);
Chris Lattner14bf8f02010-01-08 19:19:23 +0000233 return CanEvaluateTruncated(SI->getTrueValue(), Ty) &&
234 CanEvaluateTruncated(SI->getFalseValue(), Ty);
Chris Lattner5f0290e2010-01-04 07:54:59 +0000235 }
236 case Instruction::PHI: {
Chris Lattner94aab492010-01-05 22:30:42 +0000237 // We can change a phi if we can change all operands. Note that we never
238 // get into trouble with cyclic PHIs here because we only consider
239 // instructions with a single use.
Chris Lattner5f0290e2010-01-04 07:54:59 +0000240 PHINode *PN = cast<PHINode>(I);
241 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
Chris Lattner14bf8f02010-01-08 19:19:23 +0000242 if (!CanEvaluateTruncated(PN->getIncomingValue(i), Ty))
Chris Lattner5f0290e2010-01-04 07:54:59 +0000243 return false;
244 return true;
245 }
246 default:
247 // TODO: Can handle more cases here.
248 break;
249 }
250
251 return false;
252}
253
Chris Lattner075f6922010-01-07 23:41:00 +0000254/// GetLeadingZeros - Compute the number of known-zero leading bits.
255static unsigned GetLeadingZeros(Value *V, const TargetData *TD) {
256 unsigned Bits = V->getType()->getScalarSizeInBits();
257 APInt KnownZero(Bits, 0), KnownOne(Bits, 0);
258 ComputeMaskedBits(V, APInt::getAllOnesValue(Bits), KnownZero, KnownOne, TD);
259 return KnownZero.countLeadingOnes();
260}
261
262/// CanEvaluateZExtd - Determine if the specified value can be computed in the
263/// specified wider type and produce the same low bits. If not, return -1. If
264/// it is possible, return the number of high bits that are known to be zero in
265/// the promoted value.
266static int CanEvaluateZExtd(Value *V, const Type *Ty,unsigned &NumCastsRemoved,
267 const TargetData *TD) {
268 const Type *OrigTy = V->getType();
269
270 if (isa<Constant>(V)) {
271 unsigned Extended = Ty->getScalarSizeInBits()-OrigTy->getScalarSizeInBits();
272
273 // Constants can always be zero ext'd, even if it requires a ConstantExpr.
274 if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
275 return Extended + CI->getValue().countLeadingZeros();
276 return Extended;
277 }
278
279 Instruction *I = dyn_cast<Instruction>(V);
280 if (!I) return -1;
281
282 // If the input is a truncate from the destination type, we can trivially
283 // eliminate it, and this will remove a cast overall.
284 if (isa<TruncInst>(I) && I->getOperand(0)->getType() == Ty) {
285 // If the first operand is itself a cast, and is eliminable, do not count
286 // this as an eliminable cast. We would prefer to eliminate those two
287 // casts first.
288 if (!isa<CastInst>(I->getOperand(0)) && I->hasOneUse())
289 ++NumCastsRemoved;
290
291 // Figure out the number of known-zero bits coming in.
292 return GetLeadingZeros(I->getOperand(0), TD);
293 }
294
295 // We can't extend or shrink something that has multiple uses: doing so would
296 // require duplicating the instruction in general, which isn't profitable.
297 if (!I->hasOneUse()) return -1;
298
299 int Tmp1, Tmp2;
300 unsigned Opc = I->getOpcode();
301 switch (Opc) {
302 case Instruction::And:
303 Tmp1 = CanEvaluateZExtd(I->getOperand(0), Ty, NumCastsRemoved, TD);
304 if (Tmp1 == -1) return -1;
305 Tmp2 = CanEvaluateZExtd(I->getOperand(1), Ty, NumCastsRemoved, TD);
306 if (Tmp2 == -1) return -1;
307 return std::max(Tmp1, Tmp2);
308 case Instruction::Or:
309 case Instruction::Xor:
310 Tmp1 = CanEvaluateZExtd(I->getOperand(0), Ty, NumCastsRemoved, TD);
311 if (Tmp1 == -1) return -1;
312 Tmp2 = CanEvaluateZExtd(I->getOperand(1), Ty, NumCastsRemoved, TD);
313 return std::min(Tmp1, Tmp2);
314
315 case Instruction::Add:
316 case Instruction::Sub:
317 case Instruction::Mul:
318 Tmp1 = CanEvaluateZExtd(I->getOperand(0), Ty, NumCastsRemoved, TD);
319 if (Tmp1 == -1) return -1;
320 Tmp2 = CanEvaluateZExtd(I->getOperand(1), Ty, NumCastsRemoved, TD);
321 if (Tmp2 == -1) return -1;
322 return 0;
323
324 //case Instruction::Shl:
325 //case Instruction::LShr:
326 case Instruction::ZExt:
327 // zext(zext(x)) -> zext(x). Since we're replacing it, it isn't eliminated.
328 Tmp1 = Ty->getScalarSizeInBits()-OrigTy->getScalarSizeInBits();
329 return GetLeadingZeros(I, TD)+Tmp1;
330
331 //case Instruction::SExt: zext(sext(x)) -> sext(x) with no upper bits known.
332 //case Instruction::Trunc:
333 case Instruction::Select:
334 Tmp1 = CanEvaluateZExtd(I->getOperand(1), Ty, NumCastsRemoved, TD);
335 if (Tmp1 == -1) return -1;
336 Tmp2 = CanEvaluateZExtd(I->getOperand(2), Ty, NumCastsRemoved, TD);
337 return std::min(Tmp1, Tmp2);
338
339 case Instruction::PHI: {
340 // We can change a phi if we can change all operands. Note that we never
341 // get into trouble with cyclic PHIs here because we only consider
342 // instructions with a single use.
343 PHINode *PN = cast<PHINode>(I);
344 int Result = CanEvaluateZExtd(PN->getIncomingValue(0), Ty,
345 NumCastsRemoved, TD);
346 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
347 if (Result == -1) return -1;
348 Tmp1 = CanEvaluateZExtd(PN->getIncomingValue(i), Ty, NumCastsRemoved, TD);
349 Result = std::min(Result, Tmp1);
350 }
351 return Result;
352 }
353 default:
354 // TODO: Can handle more cases here.
355 return -1;
356 }
357}
358
Chris Lattnere0e4cc72010-01-06 01:56:21 +0000359/// CanEvaluateSExtd - Return true if we can take the specified value
360/// and return it as type Ty without inserting any new casts and without
Chris Lattner5c24a6d2010-01-06 05:32:15 +0000361/// changing the value of the common low bits. This is used by code that tries
362/// to promote integer operations to a wider types will allow us to eliminate
363/// the extension.
Chris Lattnere0e4cc72010-01-06 01:56:21 +0000364///
365/// This returns 0 if we can't do this or the number of sign bits that would be
366/// set if we can. For example, CanEvaluateSExtd(i16 1, i64) would return 63,
367/// because the computation can be extended (to "i64 1") and the resulting
368/// computation has 63 equal sign bits.
369///
370/// This function works on both vectors and scalars. For vectors, the result is
371/// the number of bits known sign extended in each element.
372///
373static unsigned CanEvaluateSExtd(Value *V, const Type *Ty,
374 unsigned &NumCastsRemoved, TargetData *TD) {
375 assert(V->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits() &&
376 "Can't sign extend type to a smaller type");
377 // If this is a constant, return the number of sign bits the extended version
378 // of it would have.
379 if (Constant *C = dyn_cast<Constant>(V))
380 return ComputeNumSignBits(ConstantExpr::getSExt(C, Ty), TD);
381
382 Instruction *I = dyn_cast<Instruction>(V);
383 if (!I) return 0;
384
385 // If this is a truncate from the destination type, we can trivially eliminate
386 // it, and this will remove a cast overall.
387 if (isa<TruncInst>(I) && I->getOperand(0)->getType() == Ty) {
388 // If the operand of the truncate is itself a cast, and is eliminable, do
389 // not count this as an eliminable cast. We would prefer to eliminate those
390 // two casts first.
391 if (!isa<CastInst>(I->getOperand(0)) && I->hasOneUse())
392 ++NumCastsRemoved;
393 return ComputeNumSignBits(I->getOperand(0), TD);
394 }
395
396 // We can't extend or shrink something that has multiple uses: doing so would
397 // require duplicating the instruction in general, which isn't profitable.
398 if (!I->hasOneUse()) return 0;
399
400 const Type *OrigTy = V->getType();
401
402 unsigned Opc = I->getOpcode();
403 unsigned Tmp1, Tmp2;
404 switch (Opc) {
405 case Instruction::And:
406 case Instruction::Or:
407 case Instruction::Xor:
408 // These operators can all arbitrarily be extended or truncated.
409 Tmp1 = CanEvaluateSExtd(I->getOperand(0), Ty, NumCastsRemoved, TD);
410 if (Tmp1 == 0) return 0;
411 Tmp2 = CanEvaluateSExtd(I->getOperand(1), Ty, NumCastsRemoved, TD);
412 return std::min(Tmp1, Tmp2);
413 case Instruction::Add:
414 case Instruction::Sub:
415 // Add/Sub can have at most one carry/borrow bit.
416 Tmp1 = CanEvaluateSExtd(I->getOperand(0), Ty, NumCastsRemoved, TD);
417 if (Tmp1 == 0) return 0;
418 Tmp2 = CanEvaluateSExtd(I->getOperand(1), Ty, NumCastsRemoved, TD);
419 if (Tmp2 == 0) return 0;
420 return std::min(Tmp1, Tmp2)-1;
421 case Instruction::Mul:
422 // These operators can all arbitrarily be extended or truncated.
423 if (!CanEvaluateSExtd(I->getOperand(0), Ty, NumCastsRemoved, TD))
424 return 0;
425 if (!CanEvaluateSExtd(I->getOperand(1), Ty, NumCastsRemoved, TD))
426 return 0;
427 return 1; // IMPROVE?
428
429 //case Instruction::Shl: TODO
430 //case Instruction::LShr: TODO
431 //case Instruction::Trunc: TODO
432
433 case Instruction::SExt:
434 case Instruction::ZExt: {
435 // sext(sext(x)) -> sext(x)
436 // sext(zext(x)) -> zext(x)
437 // Note that replacing a cast does not reduce the number of casts in the
438 // input.
439 unsigned InSignBits = ComputeNumSignBits(I, TD);
440 unsigned ExtBits = Ty->getScalarSizeInBits()-OrigTy->getScalarSizeInBits();
441 // We'll end up extending it all the way out.
442 return InSignBits+ExtBits;
443 }
444 case Instruction::Select: {
445 SelectInst *SI = cast<SelectInst>(I);
446 Tmp1 = CanEvaluateSExtd(SI->getTrueValue(), Ty, NumCastsRemoved, TD);
447 if (Tmp1 == 0) return 0;
448 Tmp2 = CanEvaluateSExtd(SI->getFalseValue(), Ty, NumCastsRemoved,TD);
449 return std::min(Tmp1, Tmp2);
450 }
451 case Instruction::PHI: {
452 // We can change a phi if we can change all operands. Note that we never
453 // get into trouble with cyclic PHIs here because we only consider
454 // instructions with a single use.
455 PHINode *PN = cast<PHINode>(I);
456 unsigned Result = ~0U;
457 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
458 Result = std::min(Result,
459 CanEvaluateSExtd(PN->getIncomingValue(i), Ty,
460 NumCastsRemoved, TD));
461 if (Result == 0) return 0;
462 }
463 return Result;
464 }
465 default:
466 // TODO: Can handle more cases here.
467 break;
468 }
469
470 return 0;
471}
472
473
Chris Lattner5f0290e2010-01-04 07:54:59 +0000474/// EvaluateInDifferentType - Given an expression that
Chris Lattner14bf8f02010-01-08 19:19:23 +0000475/// CanEvaluateTruncated or CanEvaluateSExtd returns true for, actually
Chris Lattnere0e4cc72010-01-06 01:56:21 +0000476/// insert the code to evaluate the expression.
Chris Lattner5f0290e2010-01-04 07:54:59 +0000477Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty,
478 bool isSigned) {
Chris Lattnerc8b3fce2010-01-08 19:28:47 +0000479 if (Constant *C = dyn_cast<Constant>(V)) {
480 C = ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/);
481 // If we got a constantexpr back, try to simplify it with TD info.
482 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
483 C = ConstantFoldConstantExpression(CE, TD);
484 return C;
485 }
Chris Lattner5f0290e2010-01-04 07:54:59 +0000486
487 // Otherwise, it must be an instruction.
488 Instruction *I = cast<Instruction>(V);
489 Instruction *Res = 0;
490 unsigned Opc = I->getOpcode();
491 switch (Opc) {
492 case Instruction::Add:
493 case Instruction::Sub:
494 case Instruction::Mul:
495 case Instruction::And:
496 case Instruction::Or:
497 case Instruction::Xor:
498 case Instruction::AShr:
499 case Instruction::LShr:
500 case Instruction::Shl:
501 case Instruction::UDiv:
502 case Instruction::URem: {
503 Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned);
504 Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);
505 Res = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
506 break;
507 }
508 case Instruction::Trunc:
509 case Instruction::ZExt:
510 case Instruction::SExt:
511 // If the source type of the cast is the type we're trying for then we can
512 // just return the source. There's no need to insert it because it is not
513 // new.
514 if (I->getOperand(0)->getType() == Ty)
515 return I->getOperand(0);
516
517 // Otherwise, must be the same type of cast, so just reinsert a new one.
518 Res = CastInst::Create(cast<CastInst>(I)->getOpcode(), I->getOperand(0),Ty);
519 break;
520 case Instruction::Select: {
521 Value *True = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);
522 Value *False = EvaluateInDifferentType(I->getOperand(2), Ty, isSigned);
523 Res = SelectInst::Create(I->getOperand(0), True, False);
524 break;
525 }
526 case Instruction::PHI: {
527 PHINode *OPN = cast<PHINode>(I);
528 PHINode *NPN = PHINode::Create(Ty);
529 for (unsigned i = 0, e = OPN->getNumIncomingValues(); i != e; ++i) {
530 Value *V =EvaluateInDifferentType(OPN->getIncomingValue(i), Ty, isSigned);
531 NPN->addIncoming(V, OPN->getIncomingBlock(i));
532 }
533 Res = NPN;
534 break;
535 }
536 default:
537 // TODO: Can handle more cases here.
538 llvm_unreachable("Unreachable!");
539 break;
540 }
541
542 Res->takeName(I);
543 return InsertNewInstBefore(Res, *I);
544}
Chris Lattner80f43d32010-01-04 07:53:58 +0000545
546
547/// This function is a wrapper around CastInst::isEliminableCastPair. It
548/// simply extracts arguments and returns what that function returns.
549static Instruction::CastOps
550isEliminableCastPair(
551 const CastInst *CI, ///< The first cast instruction
552 unsigned opcode, ///< The opcode of the second cast instruction
553 const Type *DstTy, ///< The target type for the second cast instruction
554 TargetData *TD ///< The target data for pointer size
555) {
556
557 const Type *SrcTy = CI->getOperand(0)->getType(); // A from above
558 const Type *MidTy = CI->getType(); // B from above
559
560 // Get the opcodes of the two Cast instructions
561 Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode());
562 Instruction::CastOps secondOp = Instruction::CastOps(opcode);
563
564 unsigned Res = CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy,
565 DstTy,
566 TD ? TD->getIntPtrType(CI->getContext()) : 0);
567
568 // We don't want to form an inttoptr or ptrtoint that converts to an integer
569 // type that differs from the pointer size.
570 if ((Res == Instruction::IntToPtr &&
571 (!TD || SrcTy != TD->getIntPtrType(CI->getContext()))) ||
572 (Res == Instruction::PtrToInt &&
573 (!TD || DstTy != TD->getIntPtrType(CI->getContext()))))
574 Res = 0;
575
576 return Instruction::CastOps(Res);
577}
578
579/// ValueRequiresCast - Return true if the cast from "V to Ty" actually results
580/// in any code being generated. It does not require codegen if V is simple
581/// enough or if the cast can be folded into other casts.
582bool InstCombiner::ValueRequiresCast(Instruction::CastOps opcode,const Value *V,
583 const Type *Ty) {
584 if (V->getType() == Ty || isa<Constant>(V)) return false;
585
586 // If this is another cast that can be eliminated, it isn't codegen either.
587 if (const CastInst *CI = dyn_cast<CastInst>(V))
588 if (isEliminableCastPair(CI, opcode, Ty, TD))
589 return false;
590 return true;
591}
592
593
594/// @brief Implement the transforms common to all CastInst visitors.
595Instruction *InstCombiner::commonCastTransforms(CastInst &CI) {
596 Value *Src = CI.getOperand(0);
597
598 // Many cases of "cast of a cast" are eliminable. If it's eliminable we just
599 // eliminate it now.
600 if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast
601 if (Instruction::CastOps opc =
602 isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), TD)) {
603 // The first cast (CSrc) is eliminable so we need to fix up or replace
604 // the second cast (CI). CSrc will then have a good chance of being dead.
605 return CastInst::Create(opc, CSrc->getOperand(0), CI.getType());
606 }
607 }
608
609 // If we are casting a select then fold the cast into the select
610 if (SelectInst *SI = dyn_cast<SelectInst>(Src))
611 if (Instruction *NV = FoldOpIntoSelect(CI, SI))
612 return NV;
613
614 // If we are casting a PHI then fold the cast into the PHI
615 if (isa<PHINode>(Src)) {
616 // We don't do this if this would create a PHI node with an illegal type if
617 // it is currently legal.
618 if (!isa<IntegerType>(Src->getType()) ||
619 !isa<IntegerType>(CI.getType()) ||
620 ShouldChangeType(CI.getType(), Src->getType()))
621 if (Instruction *NV = FoldOpIntoPhi(CI))
622 return NV;
623 }
624
625 return 0;
626}
627
Chris Lattner80f43d32010-01-04 07:53:58 +0000628/// commonIntCastTransforms - This function implements the common transforms
629/// for trunc, zext, and sext.
630Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) {
631 if (Instruction *Result = commonCastTransforms(CI))
632 return Result;
633
Chris Lattner80f43d32010-01-04 07:53:58 +0000634 // See if we can simplify any instructions used by the LHS whose sole
635 // purpose is to compute bits we don't care about.
636 if (SimplifyDemandedInstructionBits(CI))
637 return &CI;
Chris Lattner274ad682010-01-05 22:07:33 +0000638
Chris Lattner80f43d32010-01-04 07:53:58 +0000639 // If the source isn't an instruction or has more than one use then we
640 // can't do anything more.
Chris Lattner274ad682010-01-05 22:07:33 +0000641 Instruction *Src = dyn_cast<Instruction>(CI.getOperand(0));
642 if (!Src || !Src->hasOneUse())
Chris Lattner80f43d32010-01-04 07:53:58 +0000643 return 0;
Chris Lattner68c6e892010-01-05 23:00:30 +0000644
645 // Check to see if we can eliminate the cast by changing the entire
646 // computation chain to do the computation in the result type.
Chris Lattner274ad682010-01-05 22:07:33 +0000647 const Type *SrcTy = Src->getType();
648 const Type *DestTy = CI.getType();
Chris Lattner68c6e892010-01-05 23:00:30 +0000649
Chris Lattner80f43d32010-01-04 07:53:58 +0000650 // Only do this if the dest type is a simple type, don't convert the
651 // expression tree to something weird like i93 unless the source is also
652 // strange.
Chris Lattner68c6e892010-01-05 23:00:30 +0000653 if (!isa<VectorType>(DestTy) && !ShouldChangeType(SrcTy, DestTy))
654 return 0;
655
Chris Lattner075f6922010-01-07 23:41:00 +0000656 uint32_t SrcBitSize = SrcTy->getScalarSizeInBits();
657 uint32_t DestBitSize = DestTy->getScalarSizeInBits();
658
Chris Lattner68c6e892010-01-05 23:00:30 +0000659 // Attempt to propagate the cast into the instruction for int->int casts.
Chris Lattnere0e4cc72010-01-06 01:56:21 +0000660 unsigned NumCastsRemoved = 0;
Chris Lattner68c6e892010-01-05 23:00:30 +0000661 switch (CI.getOpcode()) {
662 default: assert(0 && "not an integer cast");
Chris Lattner075f6922010-01-07 23:41:00 +0000663 case Instruction::Trunc: {
Chris Lattner14bf8f02010-01-08 19:19:23 +0000664 if (!CanEvaluateTruncated(Src, DestTy))
Chris Lattnere0e4cc72010-01-06 01:56:21 +0000665 return 0;
666
Chris Lattner80f43d32010-01-04 07:53:58 +0000667 // If this cast is a truncate, evaluting in a different type always
Chris Lattner68c6e892010-01-05 23:00:30 +0000668 // eliminates the cast, so it is always a win.
Chris Lattner075f6922010-01-07 23:41:00 +0000669 DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type"
670 " to avoid cast: " << CI);
671 Value *Res = EvaluateInDifferentType(Src, DestTy, false);
672 assert(Res->getType() == DestTy);
673 return ReplaceInstUsesWith(CI, Res);
674 }
675 case Instruction::ZExt: {
676 int BitsZExt = CanEvaluateZExtd(Src, DestTy, NumCastsRemoved, TD);
677 if (BitsZExt == -1) return 0;
678
Chris Lattner68c6e892010-01-05 23:00:30 +0000679 // If this is a zero-extension, we need to do an AND to maintain the clear
Chris Lattner075f6922010-01-07 23:41:00 +0000680 // top-part of the computation. If we know the result will be zero
681 // extended enough already, we don't need the and.
682 if (NumCastsRemoved < 1 &&
683 unsigned(BitsZExt) < DestBitSize-SrcBitSize)
Chris Lattner68c6e892010-01-05 23:00:30 +0000684 return 0;
Chris Lattner075f6922010-01-07 23:41:00 +0000685
686 // Okay, we can transform this! Insert the new expression now.
687 DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type"
688 " to avoid zero extend: " << CI);
689 Value *Res = EvaluateInDifferentType(Src, DestTy, false);
690 assert(Res->getType() == DestTy);
691
692 // If the high bits are already filled with zeros, just replace this
693 // cast with the result.
694 if (unsigned(BitsZExt) >= DestBitSize-SrcBitSize ||
695 MaskedValueIsZero(Res, APInt::getHighBitsSet(DestBitSize,
696 DestBitSize-SrcBitSize)))
697 return ReplaceInstUsesWith(CI, Res);
698
699 // We need to emit an AND to clear the high bits.
700 Constant *C = ConstantInt::get(CI.getContext(),
701 APInt::getLowBitsSet(DestBitSize, SrcBitSize));
702 return BinaryOperator::CreateAnd(Res, C);
703 }
Chris Lattnere0e4cc72010-01-06 01:56:21 +0000704 case Instruction::SExt: {
705 // Check to see if we can do this transformation, and if so, how many bits
706 // of the promoted expression will be known copies of the sign bit in the
707 // result.
708 unsigned NumBitsSExt = CanEvaluateSExtd(Src, DestTy, NumCastsRemoved, TD);
709 if (NumBitsSExt == 0)
Chris Lattner68c6e892010-01-05 23:00:30 +0000710 return 0;
Chris Lattnere0e4cc72010-01-06 01:56:21 +0000711
Chris Lattnere0e4cc72010-01-06 01:56:21 +0000712 // Because this is a sign extension, we can always transform it by inserting
713 // two new shifts (to do the extension). However, this is only profitable
714 // if we've eliminated two or more casts from the input. If we know the
Chris Lattner5c24a6d2010-01-06 05:32:15 +0000715 // result will be sign-extended enough to not require these shifts, we can
Chris Lattnere0e4cc72010-01-06 01:56:21 +0000716 // always do the transformation.
717 if (NumCastsRemoved < 2 &&
718 NumBitsSExt <= DestBitSize-SrcBitSize)
719 return 0;
720
721 // Okay, we can transform this! Insert the new expression now.
Chris Lattner5c24a6d2010-01-06 05:32:15 +0000722 DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type"
Chris Lattnere0e4cc72010-01-06 01:56:21 +0000723 " to avoid sign extend: " << CI);
724 Value *Res = EvaluateInDifferentType(Src, DestTy, true);
725 assert(Res->getType() == DestTy);
726
727 // If the high bits are already filled with sign bit, just replace this
728 // cast with the result.
729 if (NumBitsSExt > DestBitSize - SrcBitSize ||
730 ComputeNumSignBits(Res) > DestBitSize - SrcBitSize)
731 return ReplaceInstUsesWith(CI, Res);
732
733 // We need to emit a cast to truncate, then a cast to sext.
734 return new SExtInst(Builder->CreateTrunc(Res, Src->getType()), DestTy);
735 }
Chris Lattner80f43d32010-01-04 07:53:58 +0000736 }
Chris Lattner80f43d32010-01-04 07:53:58 +0000737}
738
Chris Lattner80f43d32010-01-04 07:53:58 +0000739Instruction *InstCombiner::visitTrunc(TruncInst &CI) {
740 if (Instruction *Result = commonIntCastTransforms(CI))
741 return Result;
742
743 Value *Src = CI.getOperand(0);
Chris Lattner49bdfef2010-01-05 21:11:17 +0000744 const Type *DestTy = CI.getType();
Chris Lattner80f43d32010-01-04 07:53:58 +0000745
Chris Lattner7a34d6c2010-01-05 22:21:18 +0000746 // Canonicalize trunc x to i1 -> (icmp ne (and x, 1), 0), likewise for vector.
747 if (DestTy->getScalarSizeInBits() == 1) {
Chris Lattner80f43d32010-01-04 07:53:58 +0000748 Constant *One = ConstantInt::get(Src->getType(), 1);
749 Src = Builder->CreateAnd(Src, One, "tmp");
750 Value *Zero = Constant::getNullValue(Src->getType());
751 return new ICmpInst(ICmpInst::ICMP_NE, Src, Zero);
752 }
753
Chris Lattner80f43d32010-01-04 07:53:58 +0000754 return 0;
755}
756
757/// transformZExtICmp - Transform (zext icmp) to bitwise / integer operations
758/// in order to eliminate the icmp.
759Instruction *InstCombiner::transformZExtICmp(ICmpInst *ICI, Instruction &CI,
760 bool DoXform) {
761 // If we are just checking for a icmp eq of a single bit and zext'ing it
762 // to an integer, then shift the bit to the appropriate place and then
763 // cast to integer to avoid the comparison.
764 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
765 const APInt &Op1CV = Op1C->getValue();
766
767 // zext (x <s 0) to i32 --> x>>u31 true if signbit set.
768 // zext (x >s -1) to i32 --> (x>>u31)^1 true if signbit clear.
769 if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) ||
770 (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())) {
771 if (!DoXform) return ICI;
772
773 Value *In = ICI->getOperand(0);
774 Value *Sh = ConstantInt::get(In->getType(),
775 In->getType()->getScalarSizeInBits()-1);
776 In = Builder->CreateLShr(In, Sh, In->getName()+".lobit");
777 if (In->getType() != CI.getType())
778 In = Builder->CreateIntCast(In, CI.getType(), false/*ZExt*/, "tmp");
779
780 if (ICI->getPredicate() == ICmpInst::ICMP_SGT) {
781 Constant *One = ConstantInt::get(In->getType(), 1);
782 In = Builder->CreateXor(In, One, In->getName()+".not");
783 }
784
785 return ReplaceInstUsesWith(CI, In);
786 }
787
788
789
790 // zext (X == 0) to i32 --> X^1 iff X has only the low bit set.
791 // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set.
792 // zext (X == 1) to i32 --> X iff X has only the low bit set.
793 // zext (X == 2) to i32 --> X>>1 iff X has only the 2nd bit set.
794 // zext (X != 0) to i32 --> X iff X has only the low bit set.
795 // zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set.
796 // zext (X != 1) to i32 --> X^1 iff X has only the low bit set.
797 // zext (X != 2) to i32 --> (X>>1)^1 iff X has only the 2nd bit set.
798 if ((Op1CV == 0 || Op1CV.isPowerOf2()) &&
799 // This only works for EQ and NE
800 ICI->isEquality()) {
801 // If Op1C some other power of two, convert:
802 uint32_t BitWidth = Op1C->getType()->getBitWidth();
803 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
804 APInt TypeMask(APInt::getAllOnesValue(BitWidth));
805 ComputeMaskedBits(ICI->getOperand(0), TypeMask, KnownZero, KnownOne);
806
807 APInt KnownZeroMask(~KnownZero);
808 if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1?
809 if (!DoXform) return ICI;
810
811 bool isNE = ICI->getPredicate() == ICmpInst::ICMP_NE;
812 if (Op1CV != 0 && (Op1CV != KnownZeroMask)) {
813 // (X&4) == 2 --> false
814 // (X&4) != 2 --> true
815 Constant *Res = ConstantInt::get(Type::getInt1Ty(CI.getContext()),
816 isNE);
817 Res = ConstantExpr::getZExt(Res, CI.getType());
818 return ReplaceInstUsesWith(CI, Res);
819 }
820
821 uint32_t ShiftAmt = KnownZeroMask.logBase2();
822 Value *In = ICI->getOperand(0);
823 if (ShiftAmt) {
824 // Perform a logical shr by shiftamt.
825 // Insert the shift to put the result in the low bit.
826 In = Builder->CreateLShr(In, ConstantInt::get(In->getType(),ShiftAmt),
827 In->getName()+".lobit");
828 }
829
830 if ((Op1CV != 0) == isNE) { // Toggle the low bit.
831 Constant *One = ConstantInt::get(In->getType(), 1);
832 In = Builder->CreateXor(In, One, "tmp");
833 }
834
835 if (CI.getType() == In->getType())
836 return ReplaceInstUsesWith(CI, In);
837 else
838 return CastInst::CreateIntegerCast(In, CI.getType(), false/*ZExt*/);
839 }
840 }
841 }
842
843 // icmp ne A, B is equal to xor A, B when A and B only really have one bit.
844 // It is also profitable to transform icmp eq into not(xor(A, B)) because that
845 // may lead to additional simplifications.
846 if (ICI->isEquality() && CI.getType() == ICI->getOperand(0)->getType()) {
847 if (const IntegerType *ITy = dyn_cast<IntegerType>(CI.getType())) {
848 uint32_t BitWidth = ITy->getBitWidth();
849 Value *LHS = ICI->getOperand(0);
850 Value *RHS = ICI->getOperand(1);
851
852 APInt KnownZeroLHS(BitWidth, 0), KnownOneLHS(BitWidth, 0);
853 APInt KnownZeroRHS(BitWidth, 0), KnownOneRHS(BitWidth, 0);
854 APInt TypeMask(APInt::getAllOnesValue(BitWidth));
855 ComputeMaskedBits(LHS, TypeMask, KnownZeroLHS, KnownOneLHS);
856 ComputeMaskedBits(RHS, TypeMask, KnownZeroRHS, KnownOneRHS);
857
858 if (KnownZeroLHS == KnownZeroRHS && KnownOneLHS == KnownOneRHS) {
859 APInt KnownBits = KnownZeroLHS | KnownOneLHS;
860 APInt UnknownBit = ~KnownBits;
861 if (UnknownBit.countPopulation() == 1) {
862 if (!DoXform) return ICI;
863
864 Value *Result = Builder->CreateXor(LHS, RHS);
865
866 // Mask off any bits that are set and won't be shifted away.
867 if (KnownOneLHS.uge(UnknownBit))
868 Result = Builder->CreateAnd(Result,
869 ConstantInt::get(ITy, UnknownBit));
870
871 // Shift the bit we're testing down to the lsb.
872 Result = Builder->CreateLShr(
873 Result, ConstantInt::get(ITy, UnknownBit.countTrailingZeros()));
874
875 if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
876 Result = Builder->CreateXor(Result, ConstantInt::get(ITy, 1));
877 Result->takeName(ICI);
878 return ReplaceInstUsesWith(CI, Result);
879 }
880 }
881 }
882 }
883
884 return 0;
885}
886
887Instruction *InstCombiner::visitZExt(ZExtInst &CI) {
888 // If one of the common conversion will work, do it.
889 if (Instruction *Result = commonIntCastTransforms(CI))
890 return Result;
891
892 Value *Src = CI.getOperand(0);
893
894 // If this is a TRUNC followed by a ZEXT then we are dealing with integral
895 // types and if the sizes are just right we can convert this into a logical
896 // 'and' which will be much cheaper than the pair of casts.
897 if (TruncInst *CSrc = dyn_cast<TruncInst>(Src)) { // A->B->C cast
898 // Get the sizes of the types involved. We know that the intermediate type
899 // will be smaller than A or C, but don't know the relation between A and C.
900 Value *A = CSrc->getOperand(0);
901 unsigned SrcSize = A->getType()->getScalarSizeInBits();
902 unsigned MidSize = CSrc->getType()->getScalarSizeInBits();
903 unsigned DstSize = CI.getType()->getScalarSizeInBits();
904 // If we're actually extending zero bits, then if
905 // SrcSize < DstSize: zext(a & mask)
906 // SrcSize == DstSize: a & mask
907 // SrcSize > DstSize: trunc(a) & mask
908 if (SrcSize < DstSize) {
909 APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
910 Constant *AndConst = ConstantInt::get(A->getType(), AndValue);
911 Value *And = Builder->CreateAnd(A, AndConst, CSrc->getName()+".mask");
912 return new ZExtInst(And, CI.getType());
913 }
914
915 if (SrcSize == DstSize) {
916 APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
917 return BinaryOperator::CreateAnd(A, ConstantInt::get(A->getType(),
918 AndValue));
919 }
920 if (SrcSize > DstSize) {
921 Value *Trunc = Builder->CreateTrunc(A, CI.getType(), "tmp");
922 APInt AndValue(APInt::getLowBitsSet(DstSize, MidSize));
923 return BinaryOperator::CreateAnd(Trunc,
924 ConstantInt::get(Trunc->getType(),
925 AndValue));
926 }
927 }
928
929 if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src))
930 return transformZExtICmp(ICI, CI);
931
932 BinaryOperator *SrcI = dyn_cast<BinaryOperator>(Src);
933 if (SrcI && SrcI->getOpcode() == Instruction::Or) {
934 // zext (or icmp, icmp) --> or (zext icmp), (zext icmp) if at least one
935 // of the (zext icmp) will be transformed.
936 ICmpInst *LHS = dyn_cast<ICmpInst>(SrcI->getOperand(0));
937 ICmpInst *RHS = dyn_cast<ICmpInst>(SrcI->getOperand(1));
938 if (LHS && RHS && LHS->hasOneUse() && RHS->hasOneUse() &&
939 (transformZExtICmp(LHS, CI, false) ||
940 transformZExtICmp(RHS, CI, false))) {
941 Value *LCast = Builder->CreateZExt(LHS, CI.getType(), LHS->getName());
942 Value *RCast = Builder->CreateZExt(RHS, CI.getType(), RHS->getName());
943 return BinaryOperator::Create(Instruction::Or, LCast, RCast);
944 }
945 }
946
947 // zext(trunc(t) & C) -> (t & zext(C)).
948 if (SrcI && SrcI->getOpcode() == Instruction::And && SrcI->hasOneUse())
949 if (ConstantInt *C = dyn_cast<ConstantInt>(SrcI->getOperand(1)))
950 if (TruncInst *TI = dyn_cast<TruncInst>(SrcI->getOperand(0))) {
951 Value *TI0 = TI->getOperand(0);
952 if (TI0->getType() == CI.getType())
953 return
954 BinaryOperator::CreateAnd(TI0,
955 ConstantExpr::getZExt(C, CI.getType()));
956 }
957
958 // zext((trunc(t) & C) ^ C) -> ((t & zext(C)) ^ zext(C)).
959 if (SrcI && SrcI->getOpcode() == Instruction::Xor && SrcI->hasOneUse())
960 if (ConstantInt *C = dyn_cast<ConstantInt>(SrcI->getOperand(1)))
961 if (BinaryOperator *And = dyn_cast<BinaryOperator>(SrcI->getOperand(0)))
962 if (And->getOpcode() == Instruction::And && And->hasOneUse() &&
963 And->getOperand(1) == C)
964 if (TruncInst *TI = dyn_cast<TruncInst>(And->getOperand(0))) {
965 Value *TI0 = TI->getOperand(0);
966 if (TI0->getType() == CI.getType()) {
967 Constant *ZC = ConstantExpr::getZExt(C, CI.getType());
968 Value *NewAnd = Builder->CreateAnd(TI0, ZC, "tmp");
969 return BinaryOperator::CreateXor(NewAnd, ZC);
970 }
971 }
972
Chris Lattner718bf3f2010-01-05 21:04:47 +0000973 // zext (xor i1 X, true) to i32 --> xor (zext i1 X to i32), 1
974 Value *X;
Chris Lattner49bdfef2010-01-05 21:11:17 +0000975 if (SrcI && SrcI->hasOneUse() && SrcI->getType()->isInteger(1) &&
976 match(SrcI, m_Not(m_Value(X))) &&
Chris Lattner718bf3f2010-01-05 21:04:47 +0000977 (!X->hasOneUse() || !isa<CmpInst>(X))) {
978 Value *New = Builder->CreateZExt(X, CI.getType());
979 return BinaryOperator::CreateXor(New, ConstantInt::get(CI.getType(), 1));
980 }
981
Chris Lattner80f43d32010-01-04 07:53:58 +0000982 return 0;
983}
984
985Instruction *InstCombiner::visitSExt(SExtInst &CI) {
986 if (Instruction *I = commonIntCastTransforms(CI))
987 return I;
988
989 Value *Src = CI.getOperand(0);
990
991 // Canonicalize sign-extend from i1 to a select.
Benjamin Kramer11acaa32010-01-05 20:07:06 +0000992 if (Src->getType()->isInteger(1))
Chris Lattner80f43d32010-01-04 07:53:58 +0000993 return SelectInst::Create(Src,
994 Constant::getAllOnesValue(CI.getType()),
995 Constant::getNullValue(CI.getType()));
996
997 // See if the value being truncated is already sign extended. If so, just
998 // eliminate the trunc/sext pair.
999 if (Operator::getOpcode(Src) == Instruction::Trunc) {
1000 Value *Op = cast<User>(Src)->getOperand(0);
1001 unsigned OpBits = Op->getType()->getScalarSizeInBits();
1002 unsigned MidBits = Src->getType()->getScalarSizeInBits();
1003 unsigned DestBits = CI.getType()->getScalarSizeInBits();
1004 unsigned NumSignBits = ComputeNumSignBits(Op);
1005
1006 if (OpBits == DestBits) {
1007 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
1008 // bits, it is already ready.
1009 if (NumSignBits > DestBits-MidBits)
1010 return ReplaceInstUsesWith(CI, Op);
1011 } else if (OpBits < DestBits) {
1012 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
1013 // bits, just sext from i32.
1014 if (NumSignBits > OpBits-MidBits)
1015 return new SExtInst(Op, CI.getType(), "tmp");
1016 } else {
1017 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
1018 // bits, just truncate to i32.
1019 if (NumSignBits > OpBits-MidBits)
1020 return new TruncInst(Op, CI.getType(), "tmp");
1021 }
1022 }
1023
1024 // If the input is a shl/ashr pair of a same constant, then this is a sign
1025 // extension from a smaller value. If we could trust arbitrary bitwidth
1026 // integers, we could turn this into a truncate to the smaller bit and then
1027 // use a sext for the whole extension. Since we don't, look deeper and check
1028 // for a truncate. If the source and dest are the same type, eliminate the
1029 // trunc and extend and just do shifts. For example, turn:
1030 // %a = trunc i32 %i to i8
1031 // %b = shl i8 %a, 6
1032 // %c = ashr i8 %b, 6
1033 // %d = sext i8 %c to i32
1034 // into:
1035 // %a = shl i32 %i, 30
1036 // %d = ashr i32 %a, 30
1037 Value *A = 0;
1038 ConstantInt *BA = 0, *CA = 0;
1039 if (match(Src, m_AShr(m_Shl(m_Value(A), m_ConstantInt(BA)),
1040 m_ConstantInt(CA))) &&
1041 BA == CA && isa<TruncInst>(A)) {
1042 Value *I = cast<TruncInst>(A)->getOperand(0);
1043 if (I->getType() == CI.getType()) {
1044 unsigned MidSize = Src->getType()->getScalarSizeInBits();
1045 unsigned SrcDstSize = CI.getType()->getScalarSizeInBits();
1046 unsigned ShAmt = CA->getZExtValue()+SrcDstSize-MidSize;
1047 Constant *ShAmtV = ConstantInt::get(CI.getType(), ShAmt);
1048 I = Builder->CreateShl(I, ShAmtV, CI.getName());
1049 return BinaryOperator::CreateAShr(I, ShAmtV);
1050 }
1051 }
1052
1053 return 0;
1054}
1055
1056
1057/// FitsInFPType - Return a Constant* for the specified FP constant if it fits
1058/// in the specified FP type without changing its value.
1059static Constant *FitsInFPType(ConstantFP *CFP, const fltSemantics &Sem) {
1060 bool losesInfo;
1061 APFloat F = CFP->getValueAPF();
1062 (void)F.convert(Sem, APFloat::rmNearestTiesToEven, &losesInfo);
1063 if (!losesInfo)
1064 return ConstantFP::get(CFP->getContext(), F);
1065 return 0;
1066}
1067
1068/// LookThroughFPExtensions - If this is an fp extension instruction, look
1069/// through it until we get the source value.
1070static Value *LookThroughFPExtensions(Value *V) {
1071 if (Instruction *I = dyn_cast<Instruction>(V))
1072 if (I->getOpcode() == Instruction::FPExt)
1073 return LookThroughFPExtensions(I->getOperand(0));
1074
1075 // If this value is a constant, return the constant in the smallest FP type
1076 // that can accurately represent it. This allows us to turn
1077 // (float)((double)X+2.0) into x+2.0f.
1078 if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
1079 if (CFP->getType() == Type::getPPC_FP128Ty(V->getContext()))
1080 return V; // No constant folding of this.
1081 // See if the value can be truncated to float and then reextended.
1082 if (Value *V = FitsInFPType(CFP, APFloat::IEEEsingle))
1083 return V;
Benjamin Kramerf0127052010-01-05 13:12:22 +00001084 if (CFP->getType()->isDoubleTy())
Chris Lattner80f43d32010-01-04 07:53:58 +00001085 return V; // Won't shrink.
1086 if (Value *V = FitsInFPType(CFP, APFloat::IEEEdouble))
1087 return V;
1088 // Don't try to shrink to various long double types.
1089 }
1090
1091 return V;
1092}
1093
1094Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) {
1095 if (Instruction *I = commonCastTransforms(CI))
1096 return I;
1097
1098 // If we have fptrunc(fadd (fpextend x), (fpextend y)), where x and y are
1099 // smaller than the destination type, we can eliminate the truncate by doing
1100 // the add as the smaller type. This applies to fadd/fsub/fmul/fdiv as well
1101 // as many builtins (sqrt, etc).
1102 BinaryOperator *OpI = dyn_cast<BinaryOperator>(CI.getOperand(0));
1103 if (OpI && OpI->hasOneUse()) {
1104 switch (OpI->getOpcode()) {
1105 default: break;
1106 case Instruction::FAdd:
1107 case Instruction::FSub:
1108 case Instruction::FMul:
1109 case Instruction::FDiv:
1110 case Instruction::FRem:
1111 const Type *SrcTy = OpI->getType();
1112 Value *LHSTrunc = LookThroughFPExtensions(OpI->getOperand(0));
1113 Value *RHSTrunc = LookThroughFPExtensions(OpI->getOperand(1));
1114 if (LHSTrunc->getType() != SrcTy &&
1115 RHSTrunc->getType() != SrcTy) {
1116 unsigned DstSize = CI.getType()->getScalarSizeInBits();
1117 // If the source types were both smaller than the destination type of
1118 // the cast, do this xform.
1119 if (LHSTrunc->getType()->getScalarSizeInBits() <= DstSize &&
1120 RHSTrunc->getType()->getScalarSizeInBits() <= DstSize) {
1121 LHSTrunc = Builder->CreateFPExt(LHSTrunc, CI.getType());
1122 RHSTrunc = Builder->CreateFPExt(RHSTrunc, CI.getType());
1123 return BinaryOperator::Create(OpI->getOpcode(), LHSTrunc, RHSTrunc);
1124 }
1125 }
1126 break;
1127 }
1128 }
1129 return 0;
1130}
1131
1132Instruction *InstCombiner::visitFPExt(CastInst &CI) {
1133 return commonCastTransforms(CI);
1134}
1135
1136Instruction *InstCombiner::visitFPToUI(FPToUIInst &FI) {
1137 Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0));
1138 if (OpI == 0)
1139 return commonCastTransforms(FI);
1140
1141 // fptoui(uitofp(X)) --> X
1142 // fptoui(sitofp(X)) --> X
1143 // This is safe if the intermediate type has enough bits in its mantissa to
1144 // accurately represent all values of X. For example, do not do this with
1145 // i64->float->i64. This is also safe for sitofp case, because any negative
1146 // 'X' value would cause an undefined result for the fptoui.
1147 if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) &&
1148 OpI->getOperand(0)->getType() == FI.getType() &&
1149 (int)FI.getType()->getScalarSizeInBits() < /*extra bit for sign */
1150 OpI->getType()->getFPMantissaWidth())
1151 return ReplaceInstUsesWith(FI, OpI->getOperand(0));
1152
1153 return commonCastTransforms(FI);
1154}
1155
1156Instruction *InstCombiner::visitFPToSI(FPToSIInst &FI) {
1157 Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0));
1158 if (OpI == 0)
1159 return commonCastTransforms(FI);
1160
1161 // fptosi(sitofp(X)) --> X
1162 // fptosi(uitofp(X)) --> X
1163 // This is safe if the intermediate type has enough bits in its mantissa to
1164 // accurately represent all values of X. For example, do not do this with
1165 // i64->float->i64. This is also safe for sitofp case, because any negative
1166 // 'X' value would cause an undefined result for the fptoui.
1167 if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) &&
1168 OpI->getOperand(0)->getType() == FI.getType() &&
1169 (int)FI.getType()->getScalarSizeInBits() <=
1170 OpI->getType()->getFPMantissaWidth())
1171 return ReplaceInstUsesWith(FI, OpI->getOperand(0));
1172
1173 return commonCastTransforms(FI);
1174}
1175
1176Instruction *InstCombiner::visitUIToFP(CastInst &CI) {
1177 return commonCastTransforms(CI);
1178}
1179
1180Instruction *InstCombiner::visitSIToFP(CastInst &CI) {
1181 return commonCastTransforms(CI);
1182}
1183
Chris Lattner80f43d32010-01-04 07:53:58 +00001184Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) {
1185 // If the source integer type is larger than the intptr_t type for
1186 // this target, do a trunc to the intptr_t type, then inttoptr of it. This
1187 // allows the trunc to be exposed to other transforms. Don't do this for
1188 // extending inttoptr's, because we don't know if the target sign or zero
1189 // extends to pointers.
1190 if (TD && CI.getOperand(0)->getType()->getScalarSizeInBits() >
1191 TD->getPointerSizeInBits()) {
1192 Value *P = Builder->CreateTrunc(CI.getOperand(0),
1193 TD->getIntPtrType(CI.getContext()), "tmp");
1194 return new IntToPtrInst(P, CI.getType());
1195 }
1196
1197 if (Instruction *I = commonCastTransforms(CI))
1198 return I;
1199
1200 return 0;
1201}
1202
Chris Lattner7a34d6c2010-01-05 22:21:18 +00001203/// @brief Implement the transforms for cast of pointer (bitcast/ptrtoint)
1204Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) {
1205 Value *Src = CI.getOperand(0);
1206
1207 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) {
1208 // If casting the result of a getelementptr instruction with no offset, turn
1209 // this into a cast of the original pointer!
1210 if (GEP->hasAllZeroIndices()) {
1211 // Changing the cast operand is usually not a good idea but it is safe
1212 // here because the pointer operand is being replaced with another
1213 // pointer operand so the opcode doesn't need to change.
1214 Worklist.Add(GEP);
1215 CI.setOperand(0, GEP->getOperand(0));
1216 return &CI;
1217 }
1218
1219 // If the GEP has a single use, and the base pointer is a bitcast, and the
1220 // GEP computes a constant offset, see if we can convert these three
1221 // instructions into fewer. This typically happens with unions and other
1222 // non-type-safe code.
1223 if (TD && GEP->hasOneUse() && isa<BitCastInst>(GEP->getOperand(0)) &&
1224 GEP->hasAllConstantIndices()) {
1225 // We are guaranteed to get a constant from EmitGEPOffset.
1226 ConstantInt *OffsetV = cast<ConstantInt>(EmitGEPOffset(GEP));
1227 int64_t Offset = OffsetV->getSExtValue();
1228
1229 // Get the base pointer input of the bitcast, and the type it points to.
1230 Value *OrigBase = cast<BitCastInst>(GEP->getOperand(0))->getOperand(0);
1231 const Type *GEPIdxTy =
1232 cast<PointerType>(OrigBase->getType())->getElementType();
1233 SmallVector<Value*, 8> NewIndices;
1234 if (FindElementAtOffset(GEPIdxTy, Offset, NewIndices)) {
1235 // If we were able to index down into an element, create the GEP
1236 // and bitcast the result. This eliminates one bitcast, potentially
1237 // two.
1238 Value *NGEP = cast<GEPOperator>(GEP)->isInBounds() ?
1239 Builder->CreateInBoundsGEP(OrigBase,
1240 NewIndices.begin(), NewIndices.end()) :
1241 Builder->CreateGEP(OrigBase, NewIndices.begin(), NewIndices.end());
1242 NGEP->takeName(GEP);
1243
1244 if (isa<BitCastInst>(CI))
1245 return new BitCastInst(NGEP, CI.getType());
1246 assert(isa<PtrToIntInst>(CI));
1247 return new PtrToIntInst(NGEP, CI.getType());
1248 }
1249 }
1250 }
1251
1252 return commonCastTransforms(CI);
1253}
1254
1255Instruction *InstCombiner::visitPtrToInt(PtrToIntInst &CI) {
1256 // If the destination integer type is smaller than the intptr_t type for
1257 // this target, do a ptrtoint to intptr_t then do a trunc. This allows the
1258 // trunc to be exposed to other transforms. Don't do this for extending
1259 // ptrtoint's, because we don't know if the target sign or zero extends its
1260 // pointers.
1261 if (TD &&
1262 CI.getType()->getScalarSizeInBits() < TD->getPointerSizeInBits()) {
1263 Value *P = Builder->CreatePtrToInt(CI.getOperand(0),
1264 TD->getIntPtrType(CI.getContext()),
1265 "tmp");
1266 return new TruncInst(P, CI.getType());
1267 }
1268
1269 return commonPointerCastTransforms(CI);
1270}
1271
Chris Lattner80f43d32010-01-04 07:53:58 +00001272Instruction *InstCombiner::visitBitCast(BitCastInst &CI) {
1273 // If the operands are integer typed then apply the integer transforms,
1274 // otherwise just apply the common ones.
1275 Value *Src = CI.getOperand(0);
1276 const Type *SrcTy = Src->getType();
1277 const Type *DestTy = CI.getType();
1278
Chris Lattner80f43d32010-01-04 07:53:58 +00001279 // Get rid of casts from one type to the same type. These are useless and can
1280 // be replaced by the operand.
1281 if (DestTy == Src->getType())
1282 return ReplaceInstUsesWith(CI, Src);
1283
1284 if (const PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) {
1285 const PointerType *SrcPTy = cast<PointerType>(SrcTy);
1286 const Type *DstElTy = DstPTy->getElementType();
1287 const Type *SrcElTy = SrcPTy->getElementType();
1288
1289 // If the address spaces don't match, don't eliminate the bitcast, which is
1290 // required for changing types.
1291 if (SrcPTy->getAddressSpace() != DstPTy->getAddressSpace())
1292 return 0;
1293
1294 // If we are casting a alloca to a pointer to a type of the same
1295 // size, rewrite the allocation instruction to allocate the "right" type.
1296 // There is no need to modify malloc calls because it is their bitcast that
1297 // needs to be cleaned up.
1298 if (AllocaInst *AI = dyn_cast<AllocaInst>(Src))
1299 if (Instruction *V = PromoteCastOfAllocation(CI, *AI))
1300 return V;
1301
1302 // If the source and destination are pointers, and this cast is equivalent
1303 // to a getelementptr X, 0, 0, 0... turn it into the appropriate gep.
1304 // This can enhance SROA and other transforms that want type-safe pointers.
1305 Constant *ZeroUInt =
1306 Constant::getNullValue(Type::getInt32Ty(CI.getContext()));
1307 unsigned NumZeros = 0;
1308 while (SrcElTy != DstElTy &&
1309 isa<CompositeType>(SrcElTy) && !isa<PointerType>(SrcElTy) &&
1310 SrcElTy->getNumContainedTypes() /* not "{}" */) {
1311 SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt);
1312 ++NumZeros;
1313 }
1314
1315 // If we found a path from the src to dest, create the getelementptr now.
1316 if (SrcElTy == DstElTy) {
1317 SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt);
1318 return GetElementPtrInst::CreateInBounds(Src, Idxs.begin(), Idxs.end(),"",
Chris Lattner7a34d6c2010-01-05 22:21:18 +00001319 ((Instruction*)NULL));
Chris Lattner80f43d32010-01-04 07:53:58 +00001320 }
1321 }
1322
1323 if (const VectorType *DestVTy = dyn_cast<VectorType>(DestTy)) {
Chris Lattner7a34d6c2010-01-05 22:21:18 +00001324 if (DestVTy->getNumElements() == 1 && !isa<VectorType>(SrcTy)) {
1325 Value *Elem = Builder->CreateBitCast(Src, DestVTy->getElementType());
1326 return InsertElementInst::Create(UndefValue::get(DestTy), Elem,
Chris Lattner80f43d32010-01-04 07:53:58 +00001327 Constant::getNullValue(Type::getInt32Ty(CI.getContext())));
Chris Lattner80f43d32010-01-04 07:53:58 +00001328 // FIXME: Canonicalize bitcast(insertelement) -> insertelement(bitcast)
1329 }
1330 }
1331
1332 if (const VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy)) {
Chris Lattner7a34d6c2010-01-05 22:21:18 +00001333 if (SrcVTy->getNumElements() == 1 && !isa<VectorType>(DestTy)) {
1334 Value *Elem =
1335 Builder->CreateExtractElement(Src,
1336 Constant::getNullValue(Type::getInt32Ty(CI.getContext())));
1337 return CastInst::Create(Instruction::BitCast, Elem, DestTy);
Chris Lattner80f43d32010-01-04 07:53:58 +00001338 }
1339 }
1340
1341 if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) {
Chris Lattner7a34d6c2010-01-05 22:21:18 +00001342 // Okay, we have (bitcast (shuffle ..)). Check to see if this is
1343 // a bitconvert to a vector with the same # elts.
1344 if (SVI->hasOneUse() && isa<VectorType>(DestTy) &&
1345 cast<VectorType>(DestTy)->getNumElements() ==
1346 SVI->getType()->getNumElements() &&
1347 SVI->getType()->getNumElements() ==
1348 cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements()) {
1349 BitCastInst *Tmp;
1350 // If either of the operands is a cast from CI.getType(), then
1351 // evaluating the shuffle in the casted destination's type will allow
1352 // us to eliminate at least one cast.
1353 if (((Tmp = dyn_cast<BitCastInst>(SVI->getOperand(0))) &&
1354 Tmp->getOperand(0)->getType() == DestTy) ||
1355 ((Tmp = dyn_cast<BitCastInst>(SVI->getOperand(1))) &&
1356 Tmp->getOperand(0)->getType() == DestTy)) {
1357 Value *LHS = Builder->CreateBitCast(SVI->getOperand(0), DestTy);
1358 Value *RHS = Builder->CreateBitCast(SVI->getOperand(1), DestTy);
1359 // Return a new shuffle vector. Use the same element ID's, as we
1360 // know the vector types match #elts.
1361 return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2));
Chris Lattner80f43d32010-01-04 07:53:58 +00001362 }
1363 }
1364 }
Chris Lattner7a34d6c2010-01-05 22:21:18 +00001365
1366 if (isa<PointerType>(SrcTy))
1367 return commonPointerCastTransforms(CI);
1368 return commonCastTransforms(CI);
Chris Lattner80f43d32010-01-04 07:53:58 +00001369}