llvm.org GIT mirror llvm / aa76e9e
Add in support for getIntPtrType to get the pointer type based on the address space. This checkin also adds in some tests that utilize these paths and updates some of the clients. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@166578 91177308-0d34-0410-b5e6-96231b3b80d8 Micah Villmow 6 years ago
46 changed file(s) with 822 addition(s) and 459 deletion(s). Raw diff Collapse all Expand all
167167
168168 public:
169169 ObjectSizeOffsetVisitor(const DataLayout *TD, const TargetLibraryInfo *TLI,
170 LLVMContext &Context, bool RoundToAlign = false);
170 LLVMContext &Context, bool RoundToAlign = false,
171 unsigned AS = 0);
171172
172173 SizeOffsetType compute(Value *V);
173174
228229
229230 public:
230231 ObjectSizeOffsetEvaluator(const DataLayout *TD, const TargetLibraryInfo *TLI,
231 LLVMContext &Context);
232 LLVMContext &Context, unsigned AS = 0);
232233 SizeOffsetEvalType compute(Value *V);
233234
234235 bool knownSize(SizeOffsetEvalType SizeOffset) {
627627
628628 /// getSizeOfExpr - Return an expression for sizeof on the given type.
629629 ///
630 const SCEV *getSizeOfExpr(Type *AllocTy);
630 const SCEV *getSizeOfExpr(Type *AllocTy, Type *IntPtrTy);
631631
632632 /// getAlignOfExpr - Return an expression for alignof on the given type.
633633 ///
635635
636636 /// getOffsetOfExpr - Return an expression for offsetof on the given field.
637637 ///
638 const SCEV *getOffsetOfExpr(StructType *STy, unsigned FieldNo);
638 const SCEV *getOffsetOfExpr(StructType *STy, Type *IntPtrTy,
639 unsigned FieldNo);
639640
640641 /// getOffsetOfExpr - Return an expression for offsetof on the given field.
641642 ///
336336 ///
337337 unsigned getPreferredTypeAlignmentShift(Type *Ty) const;
338338
339 /// getIntPtrType - Return an unsigned integer type that is the same size or
340 /// greater to the host pointer size.
341 /// FIXME: Need to remove the default argument when the rest of the LLVM code
342 /// base has been updated.
343 IntegerType *getIntPtrType(LLVMContext &C, unsigned AddressSpace = 0) const;
339 /// getIntPtrType - Return an integer type that is the same size or
340 /// greater to the pointer size based on the address space.
341 IntegerType *getIntPtrType(LLVMContext &C, unsigned AddressSpace) const;
342
343 /// getIntPtrType - Return an integer type that is the same size or
344 /// greater to the pointer size based on the Type.
345 IntegerType *getIntPtrType(Type *) const;
344346
345347 /// getIndexedOffset - return the offset from the beginning of the type for
346348 /// the specified indices. This is used to implement getelementptr.
1616 #define LLVM_INSTRUCTION_TYPES_H
1717
1818 #include "llvm/Instruction.h"
19 #include "llvm/DataLayout.h"
1920 #include "llvm/OperandTraits.h"
2021 #include "llvm/DerivedTypes.h"
2122 #include "llvm/ADT/Twine.h"
574575 bool isNoopCast(
575576 Type *IntPtrTy ///< Integer type corresponding to pointer
576577 ) const;
578
579 /// @brief Determine if this cast is a no-op cast.
580 bool isNoopCast(
581 const DataLayout &DL ///< DataLayout to get the Int Ptr type from.
582 ) const;
577583
578584 /// Determine how a pair of casts can be eliminated, if they can be at all.
579585 /// This is a helper function for both CastInst and ConstantExpr.
176176 template
177177 Value *EmitGEPOffset(IRBuilderTy *Builder, const DataLayout &TD, User *GEP,
178178 bool NoAssumptions = false) {
179 unsigned AS = cast(GEP)->getPointerAddressSpace();
179180 gep_type_iterator GTI = gep_type_begin(GEP);
180 Type *IntPtrTy = TD.getIntPtrType(GEP->getContext());
181 Type *IntPtrTy = TD.getIntPtrType(GEP->getContext(), AS);
181182 Value *Result = Constant::getNullValue(IntPtrTy);
182183
183184 // If the GEP is inbounds, we know that none of the addressing operations will
185186 bool isInBounds = cast(GEP)->isInBounds() && !NoAssumptions;
186187
187188 // Build a mask for high order bits.
188 unsigned AS = cast(GEP)->getPointerAddressSpace();
189189 unsigned IntPtrWidth = TD.getPointerSizeInBits(AS);
190190 uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth);
191191
4040 // Constant Folding internal helper functions
4141 //===----------------------------------------------------------------------===//
4242
43 /// FoldBitCast - Constant fold bitcast, symbolically evaluating it with
43 /// FoldBitCast - Constant fold bitcast, symbolically evaluating it with
4444 /// DataLayout. This always returns a non-null constant, but it may be a
4545 /// ConstantExpr if unfoldable.
4646 static Constant *FoldBitCast(Constant *C, Type *DestTy,
5858 return ConstantExpr::getBitCast(C, DestTy);
5959
6060 unsigned NumSrcElts = CDV->getType()->getNumElements();
61
61
6262 Type *SrcEltTy = CDV->getType()->getElementType();
63
63
6464 // If the vector is a vector of floating point, convert it to vector of int
6565 // to simplify things.
6666 if (SrcEltTy->isFloatingPointTy()) {
7171 C = ConstantExpr::getBitCast(C, SrcIVTy);
7272 CDV = cast(C);
7373 }
74
74
7575 // Now that we know that the input value is a vector of integers, just shift
7676 // and insert them into our result.
7777 unsigned BitShift = TD.getTypeAllocSizeInBits(SrcEltTy);
8383 else
8484 Result |= CDV->getElementAsInteger(i);
8585 }
86
86
8787 return ConstantInt::get(IT, Result);
8888 }
89
89
9090 // The code below only handles casts to vectors currently.
9191 VectorType *DestVTy = dyn_cast(DestTy);
9292 if (DestVTy == 0)
9393 return ConstantExpr::getBitCast(C, DestTy);
94
94
9595 // If this is a scalar -> vector cast, convert the input into a <1 x scalar>
9696 // vector so the code below can handle it uniformly.
9797 if (isa(C) || isa(C)) {
9898 Constant *Ops = C; // don't take the address of C!
9999 return FoldBitCast(ConstantVector::get(Ops), DestTy, TD);
100100 }
101
101
102102 // If this is a bitcast from constant vector -> vector, fold it.
103103 if (!isa(C) && !isa(C))
104104 return ConstantExpr::getBitCast(C, DestTy);
105
105
106106 // If the element types match, VMCore can fold it.
107107 unsigned NumDstElt = DestVTy->getNumElements();
108108 unsigned NumSrcElt = C->getType()->getVectorNumElements();
109109 if (NumDstElt == NumSrcElt)
110110 return ConstantExpr::getBitCast(C, DestTy);
111
111
112112 Type *SrcEltTy = C->getType()->getVectorElementType();
113113 Type *DstEltTy = DestVTy->getElementType();
114
115 // Otherwise, we're changing the number of elements in a vector, which
114
115 // Otherwise, we're changing the number of elements in a vector, which
116116 // requires endianness information to do the right thing. For example,
117117 // bitcast (<2 x i64> to <4 x i32>)
118118 // folds to (little endian):
119119 // <4 x i32>
120120 // and to (big endian):
121121 // <4 x i32>
122
122
123123 // First thing is first. We only want to think about integer here, so if
124124 // we have something in FP form, recast it as integer.
125125 if (DstEltTy->isFloatingPointTy()) {
129129 VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumDstElt);
130130 // Recursively handle this integer conversion, if possible.
131131 C = FoldBitCast(C, DestIVTy, TD);
132
132
133133 // Finally, VMCore can handle this now that #elts line up.
134134 return ConstantExpr::getBitCast(C, DestTy);
135135 }
136
136
137137 // Okay, we know the destination is integer, if the input is FP, convert
138138 // it to integer first.
139139 if (SrcEltTy->isFloatingPointTy()) {
147147 !isa(C))
148148 return C;
149149 }
150
150
151151 // Now we know that the input and output vectors are both integer vectors
152152 // of the same size, and that their #elements is not the same. Do the
153153 // conversion here, which depends on whether the input or output has
154154 // more elements.
155155 bool isLittleEndian = TD.isLittleEndian();
156
156
157157 SmallVector Result;
158158 if (NumDstElt < NumSrcElt) {
159159 // Handle: bitcast (<4 x i32> to <2 x i64>)
169169 Constant *Src =dyn_cast(C->getAggregateElement(SrcElt++));
170170 if (!Src) // Reject constantexpr elements.
171171 return ConstantExpr::getBitCast(C, DestTy);
172
172
173173 // Zero extend the element to the right size.
174174 Src = ConstantExpr::getZExt(Src, Elt->getType());
175
175
176176 // Shift it to the right place, depending on endianness.
177 Src = ConstantExpr::getShl(Src,
177 Src = ConstantExpr::getShl(Src,
178178 ConstantInt::get(Src->getType(), ShiftAmt));
179179 ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
180
180
181181 // Mix it in.
182182 Elt = ConstantExpr::getOr(Elt, Src);
183183 }
185185 }
186186 return ConstantVector::get(Result);
187187 }
188
188
189189 // Handle: bitcast (<2 x i64> to <4 x i32>)
190190 unsigned Ratio = NumDstElt/NumSrcElt;
191191 unsigned DstBitSize = DstEltTy->getPrimitiveSizeInBits();
192
192
193193 // Loop over each source value, expanding into multiple results.
194194 for (unsigned i = 0; i != NumSrcElt; ++i) {
195195 Constant *Src = dyn_cast(C->getAggregateElement(i));
196196 if (!Src) // Reject constantexpr elements.
197197 return ConstantExpr::getBitCast(C, DestTy);
198
198
199199 unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize*(Ratio-1);
200200 for (unsigned j = 0; j != Ratio; ++j) {
201201 // Shift the piece of the value into the right place, depending on
202202 // endianness.
203 Constant *Elt = ConstantExpr::getLShr(Src,
203 Constant *Elt = ConstantExpr::getLShr(Src,
204204 ConstantInt::get(Src->getType(), ShiftAmt));
205205 ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
206
206
207207 // Truncate and remember this piece.
208208 Result.push_back(ConstantExpr::getTrunc(Elt, DstEltTy));
209209 }
210210 }
211
211
212212 return ConstantVector::get(Result);
213213 }
214214
223223 Offset = 0;
224224 return true;
225225 }
226
226
227227 // Otherwise, if this isn't a constant expr, bail out.
228228 ConstantExpr *CE = dyn_cast(C);
229229 if (!CE) return false;
230
230
231231 // Look through ptr->int and ptr->ptr casts.
232232 if (CE->getOpcode() == Instruction::PtrToInt ||
233233 CE->getOpcode() == Instruction::BitCast)
234234 return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD);
235
236 // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5)
235
236 // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5)
237237 if (CE->getOpcode() == Instruction::GetElementPtr) {
238238 // Cannot compute this if the element type of the pointer is missing size
239239 // info.
240240 if (!cast(CE->getOperand(0)->getType())
241241 ->getElementType()->isSized())
242242 return false;
243
243
244244 // If the base isn't a global+constant, we aren't either.
245245 if (!IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD))
246246 return false;
247
247
248248 // Otherwise, add any offset that our operands provide.
249249 gep_type_iterator GTI = gep_type_begin(CE);
250250 for (User::const_op_iterator i = CE->op_begin() + 1, e = CE->op_end();
252252 ConstantInt *CI = dyn_cast(*i);
253253 if (!CI) return false; // Index isn't a simple constant?
254254 if (CI->isZero()) continue; // Not adding anything.
255
255
256256 if (StructType *ST = dyn_cast(*GTI)) {
257257 // N = N + Offset
258258 Offset += TD.getStructLayout(ST)->getElementOffset(CI->getZExtValue());
263263 }
264264 return true;
265265 }
266
266
267267 return false;
268268 }
269269
276276 const DataLayout &TD) {
277277 assert(ByteOffset <= TD.getTypeAllocSize(C->getType()) &&
278278 "Out of range access");
279
279
280280 // If this element is zero or undefined, we can just return since *CurPtr is
281281 // zero initialized.
282282 if (isa(C) || isa(C))
283283 return true;
284
284
285285 if (ConstantInt *CI = dyn_cast(C)) {
286286 if (CI->getBitWidth() > 64 ||
287287 (CI->getBitWidth() & 7) != 0)
288288 return false;
289
289
290290 uint64_t Val = CI->getZExtValue();
291291 unsigned IntBytes = unsigned(CI->getBitWidth()/8);
292
292
293293 for (unsigned i = 0; i != BytesLeft && ByteOffset != IntBytes; ++i) {
294294 CurPtr[i] = (unsigned char)(Val >> (ByteOffset * 8));
295295 ++ByteOffset;
296296 }
297297 return true;
298298 }
299
299
300300 if (ConstantFP *CFP = dyn_cast(C)) {
301301 if (CFP->getType()->isDoubleTy()) {
302302 C = FoldBitCast(C, Type::getInt64Ty(C->getContext()), TD);
308308 }
309309 return false;
310310 }
311
311
312312 if (ConstantStruct *CS = dyn_cast(C)) {
313313 const StructLayout *SL = TD.getStructLayout(CS->getType());
314314 unsigned Index = SL->getElementContainingOffset(ByteOffset);
315315 uint64_t CurEltOffset = SL->getElementOffset(Index);
316316 ByteOffset -= CurEltOffset;
317
317
318318 while (1) {
319319 // If the element access is to the element itself and not to tail padding,
320320 // read the bytes from the element.
324324 !ReadDataFromGlobal(CS->getOperand(Index), ByteOffset, CurPtr,
325325 BytesLeft, TD))
326326 return false;
327
327
328328 ++Index;
329
329
330330 // Check to see if we read from the last struct element, if so we're done.
331331 if (Index == CS->getType()->getNumElements())
332332 return true;
374374 }
375375 return true;
376376 }
377
377
378378 if (ConstantExpr *CE = dyn_cast(C)) {
379379 if (CE->getOpcode() == Instruction::IntToPtr &&
380 CE->getOperand(0)->getType() == TD.getIntPtrType(CE->getContext()))
381 return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr,
380 CE->getOperand(0)->getType() == TD.getIntPtrType(CE->getType()))
381 return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr,
382382 BytesLeft, TD);
383383 }
384384
390390 const DataLayout &TD) {
391391 Type *LoadTy = cast(C->getType())->getElementType();
392392 IntegerType *IntType = dyn_cast(LoadTy);
393
393
394394 // If this isn't an integer load we can't fold it directly.
395395 if (!IntType) {
396396 // If this is a float/double load, we can try folding it as an int32/64 load
414414 return FoldBitCast(Res, LoadTy, TD);
415415 return 0;
416416 }
417
417
418418 unsigned BytesLoaded = (IntType->getBitWidth() + 7) / 8;
419419 if (BytesLoaded > 32 || BytesLoaded == 0) return 0;
420
420
421421 GlobalValue *GVal;
422422 int64_t Offset;
423423 if (!IsConstantOffsetFromGlobal(C, GVal, Offset, TD))
424424 return 0;
425
425
426426 GlobalVariable *GV = dyn_cast(GVal);
427427 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
428428 !GV->getInitializer()->getType()->isSized())
431431 // If we're loading off the beginning of the global, some bytes may be valid,
432432 // but we don't try to handle this.
433433 if (Offset < 0) return 0;
434
434
435435 // If we're not accessing anything in this constant, the result is undefined.
436436 if (uint64_t(Offset) >= TD.getTypeAllocSize(GV->getInitializer()->getType()))
437437 return UndefValue::get(IntType);
438
438
439439 unsigned char RawBytes[32] = {0};
440440 if (!ReadDataFromGlobal(GV->getInitializer(), Offset, RawBytes,
441441 BytesLoaded, TD))
463463 // If the loaded value isn't a constant expr, we can't handle it.
464464 ConstantExpr *CE = dyn_cast(C);
465465 if (!CE) return 0;
466
466
467467 if (CE->getOpcode() == Instruction::GetElementPtr) {
468468 if (GlobalVariable *GV = dyn_cast(CE->getOperand(0)))
469469 if (GV->isConstant() && GV->hasDefinitiveInitializer())
470 if (Constant *V =
470 if (Constant *V =
471471 ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE))
472472 return V;
473473 }
474
474
475475 // Instead of loading constant c string, use corresponding integer value
476476 // directly if string length is small enough.
477477 StringRef Str;
499499 SingleChar = 0;
500500 StrVal = (StrVal << 8) | SingleChar;
501501 }
502
502
503503 Constant *Res = ConstantInt::get(CE->getContext(), StrVal);
504504 if (Ty->isFloatingPointTy())
505505 Res = ConstantExpr::getBitCast(Res, Ty);
506506 return Res;
507507 }
508508 }
509
509
510510 // If this load comes from anywhere in a constant global, and if the global
511511 // is all undef or zero, we know what it loads.
512512 if (GlobalVariable *GV =
519519 return UndefValue::get(ResTy);
520520 }
521521 }
522
522
523523 // Try hard to fold loads from bitcasted strange and non-type-safe things. We
524524 // currently don't do any of this for big endian systems. It can be
525525 // generalized in the future if someone is interested.
530530
531531 static Constant *ConstantFoldLoadInst(const LoadInst *LI, const DataLayout *TD){
532532 if (LI->isVolatile()) return 0;
533
533
534534 if (Constant *C = dyn_cast(LI->getOperand(0)))
535535 return ConstantFoldLoadFromConstPtr(C, TD);
536536
539539
540540 /// SymbolicallyEvaluateBinop - One of Op0/Op1 is a constant expression.
541541 /// Attempt to symbolically evaluate the result of a binary operator merging
542 /// these together. If target data info is available, it is provided as TD,
542 /// these together. If target data info is available, it is provided as TD,
543543 /// otherwise TD is null.
544544 static Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0,
545545 Constant *Op1, const DataLayout *TD){
546546 // SROA
547
547
548548 // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl.
549549 // Fold (lshr (or X, Y), 32) -> (lshr [X/Y], 32) if one doesn't contribute
550550 // bits.
551
552
551
552
553553 // If the constant expr is something like &A[123] - &A[4].f, fold this into a
554554 // constant. This happens frequently when iterating over a global array.
555555 if (Opc == Instruction::Sub && TD) {
556556 GlobalValue *GV1, *GV2;
557557 int64_t Offs1, Offs2;
558
558
559559 if (IsConstantOffsetFromGlobal(Op0, GV1, Offs1, *TD))
560560 if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, *TD) &&
561561 GV1 == GV2) {
563563 return ConstantInt::get(Op0->getType(), Offs1-Offs2);
564564 }
565565 }
566
566
567567 return 0;
568568 }
569569
574574 Type *ResultTy, const DataLayout *TD,
575575 const TargetLibraryInfo *TLI) {
576576 if (!TD) return 0;
577 Type *IntPtrTy = TD->getIntPtrType(ResultTy->getContext());
577 Type *IntPtrTy = TD->getIntPtrType(ResultTy);
578578
579579 bool Any = false;
580580 SmallVector NewIdxs;
627627 if (!TD || !cast(Ptr->getType())->getElementType()->isSized() ||
628628 !Ptr->getType()->isPointerTy())
629629 return 0;
630
631 Type *IntPtrTy = TD->getIntPtrType(Ptr->getContext());
630
631 unsigned AS = cast(Ptr->getType())->getAddressSpace();
632 Type *IntPtrTy = TD->getIntPtrType(Ptr->getContext(), AS);
632633
633634 // If this is a constant expr gep that is effectively computing an
634635 // "offsetof", fold it into 'cast int Size to T*' instead of 'gep 0, 0, 12'
635636 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
636637 if (!isa(Ops[i])) {
637
638
638639 // If this is "gep i8* Ptr, (sub 0, V)", fold this as:
639640 // "inttoptr (sub (ptrtoint Ptr), V)"
640641 if (Ops.size() == 2 &&
701702 // Also, this helps GlobalOpt do SROA on GlobalVariables.
702703 Type *Ty = Ptr->getType();
703704 assert(Ty->isPointerTy() && "Forming regular GEP of non-pointer type");
705 assert(Ty->getPointerAddressSpace() == AS
706 && "Operand and result of GEP should be in the same address space.");
704707 SmallVector NewIdxs;
705708 do {
706709 if (SequentialType *ATy = dyn_cast(Ty)) {
708711 // The only pointer indexing we'll do is on the first index of the GEP.
709712 if (!NewIdxs.empty())
710713 break;
711
714
712715 // Only handle pointers to sized types, not pointers to functions.
713716 if (!ATy->getElementType()->isSized())
714717 return 0;
715718 }
716
719
717720 // Determine which element of the array the offset points into.
718721 APInt ElemSize(BitWidth, TD->getTypeAllocSize(ATy->getElementType()));
719 IntegerType *IntPtrTy = TD->getIntPtrType(Ty->getContext());
722 IntegerType *IntPtrTy = TD->getIntPtrType(Ty->getContext(), AS);
720723 if (ElemSize == 0)
721724 // The element size is 0. This may be [0 x Ty]*, so just use a zero
722725 // index for this level and proceed to the next level to see if it can
836839 if (const CmpInst *CI = dyn_cast(I))
837840 return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1],
838841 TD, TLI);
839
842
840843 if (const LoadInst *LI = dyn_cast(I))
841844 return ConstantFoldLoadInst(LI, TD);
842845
886889 /// information, due to only being passed an opcode and operands. Constant
887890 /// folding using this function strips this information.
888891 ///
889 Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, Type *DestTy,
892 Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, Type *DestTy,
890893 ArrayRef Ops,
891894 const DataLayout *TD,
892 const TargetLibraryInfo *TLI) {
895 const TargetLibraryInfo *TLI) {
893896 // Handle easy binops first.
894897 if (Instruction::isBinaryOp(Opcode)) {
895898 if (isa(Ops[0]) || isa(Ops[1]))
896899 if (Constant *C = SymbolicallyEvaluateBinop(Opcode, Ops[0], Ops[1], TD))
897900 return C;
898
901
899902 return ConstantExpr::get(Opcode, Ops[0], Ops[1]);
900903 }
901
904
902905 switch (Opcode) {
903906 default: return 0;
904907 case Instruction::ICmp:
917920 unsigned InWidth = Input->getType()->getScalarSizeInBits();
918921 unsigned AS = cast(CE->getType())->getAddressSpace();
919922 if (TD->getPointerSizeInBits(AS) < InWidth) {
920 Constant *Mask =
923 Constant *Mask =
921924 ConstantInt::get(CE->getContext(), APInt::getLowBitsSet(InWidth,
922925 TD->getPointerSizeInBits(AS)));
923926 Input = ConstantExpr::getAnd(Input, Mask);
966969 return C;
967970 if (Constant *C = SymbolicallyEvaluateGEP(Ops, DestTy, TD, TLI))
968971 return C;
969
972
970973 return ConstantExpr::getGetElementPtr(Ops[0], Ops.slice(1));
971974 }
972975 }
976979 /// returns a constant expression of the specified operands.
977980 ///
978981 Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate,
979 Constant *Ops0, Constant *Ops1,
982 Constant *Ops0, Constant *Ops1,
980983 const DataLayout *TD,
981984 const TargetLibraryInfo *TLI) {
982985 // fold: icmp (inttoptr x), null -> icmp x, 0
987990 // ConstantExpr::getCompare cannot do this, because it doesn't have TD
988991 // around to know if bit truncation is happening.
989992 if (ConstantExpr *CE0 = dyn_cast(Ops0)) {
993 Type *IntPtrTy = NULL;
990994 if (TD && Ops1->isNullValue()) {
991 Type *IntPtrTy = TD->getIntPtrType(CE0->getContext());
992995 if (CE0->getOpcode() == Instruction::IntToPtr) {
996 IntPtrTy = TD->getIntPtrType(CE0->getType());
993997 // Convert the integer value to the right size to ensure we get the
994998 // proper extension or truncation.
995999 Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0),
9971001 Constant *Null = Constant::getNullValue(C->getType());
9981002 return ConstantFoldCompareInstOperands(Predicate, C, Null, TD, TLI);
9991003 }
1000
1004
10011005 // Only do this transformation if the int is intptrty in size, otherwise
10021006 // there is a truncation or extension that we aren't modeling.
1003 if (CE0->getOpcode() == Instruction::PtrToInt &&
1004 CE0->getType() == IntPtrTy) {
1005 Constant *C = CE0->getOperand(0);
1006 Constant *Null = Constant::getNullValue(C->getType());
1007 return ConstantFoldCompareInstOperands(Predicate, C, Null, TD, TLI);
1008 }
1009 }
1010
1007 if (CE0->getOpcode() == Instruction::PtrToInt) {
1008 IntPtrTy = TD->getIntPtrType(CE0->getOperand(0)->getType());
1009 if (CE0->getType() == IntPtrTy) {
1010 Constant *C = CE0->getOperand(0);
1011 Constant *Null = Constant::getNullValue(C->getType());
1012 return ConstantFoldCompareInstOperands(Predicate, C, Null, TD, TLI);
1013 }
1014 }
1015 }
1016
10111017 if (ConstantExpr *CE1 = dyn_cast(Ops1)) {
10121018 if (TD && CE0->getOpcode() == CE1->getOpcode()) {
1013 Type *IntPtrTy = TD->getIntPtrType(CE0->getContext());
10141019
10151020 if (CE0->getOpcode() == Instruction::IntToPtr) {
1021 Type *IntPtrTy = TD->getIntPtrType(CE0->getType());
10161022 // Convert the integer value to the right size to ensure we get the
10171023 // proper extension or truncation.
10181024 Constant *C0 = ConstantExpr::getIntegerCast(CE0->getOperand(0),
10211027 IntPtrTy, false);
10221028 return ConstantFoldCompareInstOperands(Predicate, C0, C1, TD, TLI);
10231029 }
1024
1025 // Only do this transformation if the int is intptrty in size, otherwise
1026 // there is a truncation or extension that we aren't modeling.
1027 if ((CE0->getOpcode() == Instruction::PtrToInt &&
1028 CE0->getType() == IntPtrTy &&
1029 CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType()))
1030 }
1031
1032 // Only do this transformation if the int is intptrty in size, otherwise
1033 // there is a truncation or extension that we aren't modeling.
1034 if (CE0->getOpcode() == Instruction::PtrToInt) {
1035 IntPtrTy = TD->getIntPtrType(CE0->getOperand(0)->getType());
1036 if (CE0->getType() == IntPtrTy &&
1037 CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType())
10301038 return ConstantFoldCompareInstOperands(Predicate, CE0->getOperand(0),
1031 CE1->getOperand(0), TD, TLI);
1032 }
1033 }
1034
1039 CE1->getOperand(0), TD, TLI);
1040 }
1041 }
1042
10351043 // icmp eq (or x, y), 0 -> (icmp eq x, 0) & (icmp eq y, 0)
10361044 // icmp ne (or x, y), 0 -> (icmp ne x, 0) | (icmp ne y, 0)
10371045 if ((Predicate == ICmpInst::ICMP_EQ || Predicate == ICmpInst::ICMP_NE) &&
10381046 CE0->getOpcode() == Instruction::Or && Ops1->isNullValue()) {
1039 Constant *LHS =
1047 Constant *LHS =
10401048 ConstantFoldCompareInstOperands(Predicate, CE0->getOperand(0), Ops1,
10411049 TD, TLI);
1042 Constant *RHS =
1050 Constant *RHS =
10431051 ConstantFoldCompareInstOperands(Predicate, CE0->getOperand(1), Ops1,
10441052 TD, TLI);
1045 unsigned OpC =
1053 unsigned OpC =
10461054 Predicate == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
10471055 Constant *Ops[] = { LHS, RHS };
10481056 return ConstantFoldInstOperands(OpC, LHS->getType(), Ops, TD, TLI);
10491057 }
10501058 }
1051
1059
10521060 return ConstantExpr::getCompare(Predicate, Ops0, Ops1);
10531061 }
10541062
10561064 /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a
10571065 /// getelementptr constantexpr, return the constant value being addressed by the
10581066 /// constant expression, or null if something is funny and we can't decide.
1059 Constant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C,
1067 Constant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C,
10601068 ConstantExpr *CE) {
10611069 if (!CE->getOperand(1)->isNullValue())
10621070 return 0; // Do not allow stepping over the value!
11261134
11271135 if (!F->hasName()) return false;
11281136 StringRef Name = F->getName();
1129
1137
11301138 // In these cases, the check of the length is required. We don't want to
11311139 // return true for a name like "cos\0blah" which strcmp would return equal to
11321140 // "cos", but has length 8.
11331141 switch (Name[0]) {
11341142 default: return false;
11351143 case 'a':
1136 return Name == "acos" || Name == "asin" ||
1144 return Name == "acos" || Name == "asin" ||
11371145 Name == "atan" || Name == "atan2";
11381146 case 'c':
11391147 return Name == "cos" || Name == "ceil" || Name == "cosf" || Name == "cosh";
11531161 }
11541162 }
11551163
1156 static Constant *ConstantFoldFP(double (*NativeFP)(double), double V,
1164 static Constant *ConstantFoldFP(double (*NativeFP)(double), double V,
11571165 Type *Ty) {
11581166 sys::llvm_fenv_clearexcept();
11591167 V = NativeFP(V);
11611169 sys::llvm_fenv_clearexcept();
11621170 return 0;
11631171 }
1164
1172
11651173 if (Ty->isFloatTy())
11661174 return ConstantFP::get(Ty->getContext(), APFloat((float)V));
11671175 if (Ty->isDoubleTy())
11771185 sys::llvm_fenv_clearexcept();
11781186 return 0;
11791187 }
1180
1188
11811189 if (Ty->isFloatTy())
11821190 return ConstantFP::get(Ty->getContext(), APFloat((float)V));
11831191 if (Ty->isDoubleTy())
12711279 case 'e':
12721280 if (Name == "exp" && TLI->has(LibFunc::exp))
12731281 return ConstantFoldFP(exp, V, Ty);
1274
1282
12751283 if (Name == "exp2" && TLI->has(LibFunc::exp2)) {
12761284 // Constant fold exp2(x) as pow(2,x) in case the host doesn't have a
12771285 // C99 library.
13471355 }
13481356
13491357 // Support ConstantVector in case we have an Undef in the top.
1350 if (isa(Operands[0]) ||
1358 if (isa(Operands[0]) ||
13511359 isa(Operands[0])) {
13521360 Constant *Op = cast(Operands[0]);
13531361 switch (F->getIntrinsicID()) {
13661374 case Intrinsic::x86_sse2_cvttsd2si64:
13671375 if (ConstantFP *FPOp =
13681376 dyn_cast_or_null(Op->getAggregateElement(0U)))
1369 return ConstantFoldConvertToInt(FPOp->getValueAPF(),
1377 return ConstantFoldConvertToInt(FPOp->getValueAPF(),
13701378 /*roundTowardZero=*/true, Ty);
13711379 }
13721380 }
1373
1381
13741382 if (isa(Operands[0])) {
13751383 if (F->getIntrinsicID() == Intrinsic::bswap)
13761384 return Operands[0];
13841392 if (ConstantFP *Op1 = dyn_cast(Operands[0])) {
13851393 if (!Ty->isFloatTy() && !Ty->isDoubleTy())
13861394 return 0;
1387 double Op1V = Ty->isFloatTy() ?
1395 double Op1V = Ty->isFloatTy() ?
13881396 (double)Op1->getValueAPF().convertToFloat() :
13891397 Op1->getValueAPF().convertToDouble();
13901398 if (ConstantFP *Op2 = dyn_cast(Operands[1])) {
13911399 if (Op2->getType() != Op1->getType())
13921400 return 0;
13931401
1394 double Op2V = Ty->isFloatTy() ?
1402 double Op2V = Ty->isFloatTy() ?
13951403 (double)Op2->getValueAPF().convertToFloat():
13961404 Op2->getValueAPF().convertToDouble();
13971405
14181426 }
14191427 return 0;
14201428 }
1421
1429
14221430 if (ConstantInt *Op1 = dyn_cast(Operands[0])) {
14231431 if (ConstantInt *Op2 = dyn_cast(Operands[1])) {
14241432 switch (F->getIntrinsicID()) {
14681476 return ConstantInt::get(Ty, Op1->getValue().countLeadingZeros());
14691477 }
14701478 }
1471
1479
14721480 return 0;
14731481 }
14741482 return 0;
787787 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
788788 } while (Visited.insert(V));
789789
790 Type *IntPtrTy = TD->getIntPtrType(V->getContext());
790 Type *IntPtrTy = TD->getIntPtrType(V->getType());
791791 return cast(ConstantInt::get(IntPtrTy, Offset));
792792 }
793793
727727 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
728728 } while (Visited.insert(V));
729729
730 Type *IntPtrTy = TD.getIntPtrType(V->getContext());
730 Type *IntPtrTy = TD.getIntPtrType(V->getContext(), AS);
731731 return ConstantInt::get(IntPtrTy, Offset);
732732 }
733733
625625 if (W != V)
626626 return findValueImpl(W, OffsetOk, Visited);
627627 } else if (CastInst *CI = dyn_cast(V)) {
628 if (CI->isNoopCast(TD ? TD->getIntPtrType(V->getContext()) :
629 Type::getInt64Ty(V->getContext())))
628 if (CI->isNoopCast(*TD))
630629 return findValueImpl(CI->getOperand(0), OffsetOk, Visited);
631630 } else if (ExtractValueInst *Ex = dyn_cast(V)) {
632631 if (Value *W = FindInsertedValue(Ex->getAggregateOperand(),
639638 if (CastInst::isNoopCast(Instruction::CastOps(CE->getOpcode()),
640639 CE->getOperand(0)->getType(),
641640 CE->getType(),
642 TD ? TD->getIntPtrType(V->getContext()) :
641 TD ? TD->getIntPtrType(CE->getType()) :
643642 Type::getInt64Ty(V->getContext())))
644643 return findValueImpl(CE->getOperand(0), OffsetOk, Visited);
645644 } else if (CE->getOpcode() == Instruction::ExtractValue) {
375375 ObjectSizeOffsetVisitor::ObjectSizeOffsetVisitor(const DataLayout *TD,
376376 const TargetLibraryInfo *TLI,
377377 LLVMContext &Context,
378 bool RoundToAlign)
378 bool RoundToAlign,
379 unsigned AS)
379380 : TD(TD), TLI(TLI), RoundToAlign(RoundToAlign) {
380 IntegerType *IntTy = TD->getIntPtrType(Context);
381 IntegerType *IntTy = TD->getIntPtrType(Context, AS);
381382 IntTyBits = IntTy->getBitWidth();
382383 Zero = APInt::getNullValue(IntTyBits);
383384 }
560561
561562 ObjectSizeOffsetEvaluator::ObjectSizeOffsetEvaluator(const DataLayout *TD,
562563 const TargetLibraryInfo *TLI,
563 LLVMContext &Context)
564 LLVMContext &Context,
565 unsigned AS)
564566 : TD(TD), TLI(TLI), Context(Context), Builder(Context, TargetFolder(TD)) {
565 IntTy = TD->getIntPtrType(Context);
567 IntTy = TD->getIntPtrType(Context, AS);
566568 Zero = ConstantInt::get(IntTy, 0);
567569 }
568570
25802580 return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS)));
25812581 }
25822582
2583 const SCEV *ScalarEvolution::getSizeOfExpr(Type *AllocTy) {
2583 const SCEV *ScalarEvolution::getSizeOfExpr(Type *AllocTy, Type *IntPtrTy) {
25842584 // If we have DataLayout, we can bypass creating a target-independent
25852585 // constant expression and then folding it back into a ConstantInt.
25862586 // This is just a compile-time optimization.
25872587 if (TD)
2588 return getConstant(TD->getIntPtrType(getContext()),
2589 TD->getTypeAllocSize(AllocTy));
2588 return getConstant(IntPtrTy, TD->getTypeAllocSize(AllocTy));
25902589
25912590 Constant *C = ConstantExpr::getSizeOf(AllocTy);
25922591 if (ConstantExpr *CE = dyn_cast(C))
26052604 return getTruncateOrZeroExtend(getSCEV(C), Ty);
26062605 }
26072606
2608 const SCEV *ScalarEvolution::getOffsetOfExpr(StructType *STy,
2607 const SCEV *ScalarEvolution::getOffsetOfExpr(StructType *STy, Type *IntPtrTy,
26092608 unsigned FieldNo) {
26102609 // If we have DataLayout, we can bypass creating a target-independent
26112610 // constant expression and then folding it back into a ConstantInt.
26122611 // This is just a compile-time optimization.
26132612 if (TD)
2614 return getConstant(TD->getIntPtrType(getContext()),
2613 return getConstant(IntPtrTy,
26152614 TD->getStructLayout(STy)->getElementOffset(FieldNo));
26162615
26172616 Constant *C = ConstantExpr::getOffsetOf(STy, FieldNo);
26982697
26992698 // The only other support type is pointer.
27002699 assert(Ty->isPointerTy() && "Unexpected non-pointer non-integer type!");
2701 if (TD) return TD->getIntPtrType(getContext());
2700 if (TD) return TD->getIntPtrType(Ty);
27022701
27032702 // Without DataLayout, conservatively assume pointers are 64-bit.
27042703 return Type::getInt64Ty(getContext());
31513150 if (StructType *STy = dyn_cast(*GTI++)) {
31523151 // For a struct, add the member offset.
31533152 unsigned FieldNo = cast(Index)->getZExtValue();
3154 const SCEV *FieldOffset = getOffsetOfExpr(STy, FieldNo);
3153 const SCEV *FieldOffset = getOffsetOfExpr(STy, IntPtrTy, FieldNo);
31553154
31563155 // Add the field offset to the running total offset.
31573156 TotalOffset = getAddExpr(TotalOffset, FieldOffset);
31583157 } else {
31593158 // For an array, add the element offset, explicitly scaled.
3160 const SCEV *ElementSize = getSizeOfExpr(*GTI);
3159 const SCEV *ElementSize = getSizeOfExpr(*GTI, IntPtrTy);
31613160 const SCEV *IndexS = getSCEV(Index);
31623161 // Getelementptr indices are signed.
31633162 IndexS = getTruncateOrSignExtend(IndexS, IntPtrTy);
416416 // array indexing.
417417 SmallVector ScaledOps;
418418 if (ElTy->isSized()) {
419 const SCEV *ElSize = SE.getSizeOfExpr(ElTy);
419 Type *IntPtrTy = SE.TD ? SE.TD->getIntPtrType(PTy) :
420 IntegerType::getInt64Ty(PTy->getContext());
421 const SCEV *ElSize = SE.getSizeOfExpr(ElTy, IntPtrTy);
420422 if (!ElSize->isZero()) {
421423 SmallVector NewOps;
422424 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
15041504 // Handle casts to pointers by changing them into casts to the appropriate
15051505 // integer type. This promotes constant folding and simplifies this code.
15061506 Constant *Op = CE->getOperand(0);
1507 Op = ConstantExpr::getIntegerCast(Op, TD.getIntPtrType(CV->getContext()),
1507 Op = ConstantExpr::getIntegerCast(Op, TD.getIntPtrType(CE->getType()),
15081508 false/*ZExt*/);
15091509 return lowerConstant(Op, AP);
15101510 }
114114 Type::getInt8PtrTy(Context),
115115 Type::getInt8PtrTy(Context),
116116 Type::getInt8PtrTy(Context),
117 TD.getIntPtrType(Context), (Type *)0);
117 TD.getIntPtrType(Context, 0), (Type *)0);
118118 break;
119119 case Intrinsic::memmove:
120120 M.getOrInsertFunction("memmove",
121121 Type::getInt8PtrTy(Context),
122122 Type::getInt8PtrTy(Context),
123123 Type::getInt8PtrTy(Context),
124 TD.getIntPtrType(Context), (Type *)0);
124 TD.getIntPtrType(Context, 0), (Type *)0);
125125 break;
126126 case Intrinsic::memset:
127127 M.getOrInsertFunction("memset",
128128 Type::getInt8PtrTy(Context),
129129 Type::getInt8PtrTy(Context),
130130 Type::getInt32Ty(M.getContext()),
131 TD.getIntPtrType(Context), (Type *)0);
131 TD.getIntPtrType(Context, 0), (Type *)0);
132132 break;
133133 case Intrinsic::sqrt:
134134 EnsureFPIntrinsicsExist(M, I, "sqrtf", "sqrt", "sqrtl");
456456 break; // Strip out annotate intrinsic
457457
458458 case Intrinsic::memcpy: {
459 IntegerType *IntPtr = TD.getIntPtrType(Context);
459 IntegerType *IntPtr = TD.getIntPtrType(CI->getArgOperand(0)->getType());
460460 Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
461461 /* isSigned */ false);
462462 Value *Ops[3];
467467 break;
468468 }
469469 case Intrinsic::memmove: {
470 IntegerType *IntPtr = TD.getIntPtrType(Context);
470 IntegerType *IntPtr = TD.getIntPtrType(CI->getArgOperand(0)->getType());
471471 Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
472472 /* isSigned */ false);
473473 Value *Ops[3];
478478 break;
479479 }
480480 case Intrinsic::memset: {
481 IntegerType *IntPtr = TD.getIntPtrType(Context);
481 IntegerType *IntPtr = TD.getIntPtrType(CI->getArgOperand(0)->getType());
482482 Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
483483 /* isSigned */ false);
484484 Value *Ops[3];
100100
101101 // No-op casts are trivially coalesced by fast-isel.
102102 if (const CastInst *Cast = dyn_cast(I))
103 if (Cast->isNoopCast(TD.getIntPtrType(Cast->getContext())) &&
104 !hasTrivialKill(Cast->getOperand(0)))
103 if (Cast->isNoopCast(TD) && !hasTrivialKill(Cast->getOperand(0)))
105104 return false;
106105
107106 // GEPs with all zero indices are trivially coalesced by fast-isel.
174173 // Translate this as an integer zero so that it can be
175174 // local-CSE'd with actual integer zeros.
176175 Reg =
177 getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext())));
176 getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getType())));
178177 } else if (const ConstantFP *CF = dyn_cast(V)) {
179178 if (CF->isNullValue()) {
180179 Reg = TargetMaterializeFloatZero(CF);
38033803 // Emit a library call.
38043804 TargetLowering::ArgListTy Args;
38053805 TargetLowering::ArgListEntry Entry;
3806 Entry.Ty = TLI.getDataLayout()->getIntPtrType(*getContext());
3806 unsigned AS = SrcPtrInfo.getAddrSpace();
3807 Entry.Ty = TLI.getDataLayout()->getIntPtrType(*getContext(), AS);
38073808 Entry.Node = Dst; Args.push_back(Entry);
38083809 Entry.Node = Src; Args.push_back(Entry);
38093810 Entry.Node = Size; Args.push_back(Entry);
38583859 // Emit a library call.
38593860 TargetLowering::ArgListTy Args;
38603861 TargetLowering::ArgListEntry Entry;
3861 Entry.Ty = TLI.getDataLayout()->getIntPtrType(*getContext());
3862 unsigned AS = SrcPtrInfo.getAddrSpace();
3863 Entry.Ty = TLI.getDataLayout()->getIntPtrType(*getContext(), AS);
38623864 Entry.Node = Dst; Args.push_back(Entry);
38633865 Entry.Node = Src; Args.push_back(Entry);
38643866 Entry.Node = Size; Args.push_back(Entry);
39073909 return Result;
39083910
39093911 // Emit a library call.
3910 Type *IntPtrTy = TLI.getDataLayout()->getIntPtrType(*getContext());
3912 unsigned AS = DstPtrInfo.getAddrSpace();
3913 Type *IntPtrTy = TLI.getDataLayout()->getIntPtrType(*getContext(), AS);
39113914 TargetLowering::ArgListTy Args;
39123915 TargetLowering::ArgListEntry Entry;
39133916 Entry.Node = Dst; Entry.Ty = IntPtrTy;
154154 TargetLowering::ArgListEntry Entry;
155155
156156 // First argument: data pointer
157 Type *IntPtrTy = TLI.getDataLayout()->getIntPtrType(*DAG.getContext());
157 unsigned AS = DstPtrInfo.getAddrSpace();
158 Type *IntPtrTy = TLI.getDataLayout()->getIntPtrType(*DAG.getContext(), AS);
158159 Entry.Node = Dst;
159160 Entry.Ty = IntPtrTy;
160161 Args.push_back(Entry);
150150 // Handle casts to pointers by changing them into casts to the appropriate
151151 // integer type. This promotes constant folding and simplifies this code.
152152 Constant *Op = CE->getOperand(0);
153 Op = ConstantExpr::getIntegerCast(Op, TD.getIntPtrType(CV->getContext()),
153 Op = ConstantExpr::getIntegerCast(Op, TD.getIntPtrType(CE->getType()),
154154 false/*ZExt*/);
155155 return LowerConstant(Op, AP);
156156 }
14971497
14981498 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
14991499 bool isPPC64 = (PtrVT == MVT::i64);
1500 unsigned AS = 0;
15001501 Type *IntPtrTy =
15011502 DAG.getTargetLoweringInfo().getDataLayout()->getIntPtrType(
1502 *DAG.getContext());
1503 *DAG.getContext(), AS);
15031504
15041505 TargetLowering::ArgListTy Args;
15051506 TargetLowering::ArgListEntry Entry;
6363 }
6464
6565 LLVMTypeRef LLVMIntPtrType(LLVMTargetDataRef TD) {
66 return wrap(unwrap(TD)->getIntPtrType(getGlobalContext()));
66 return wrap(unwrap(TD)->getIntPtrType(getGlobalContext(), 0));
6767 }
6868
6969 LLVMTypeRef LLVMIntPtrTypeForAS(LLVMTargetDataRef TD, unsigned AS) {
281281 bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
282282 const X86AddressMode &AM) {
283283 // Handle 'null' like i32/i64 0.
284 if (isa(Val))
285 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
284 if (isa(Val)) {
285 Val = Constant::getNullValue(TD.getIntPtrType(Val->getType()));
286 }
286287
287288 // If this is a store of a simple constant, fold the constant into the store.
288289 if (const ConstantInt *CI = dyn_cast(Val)) {
893894 if (Op0Reg == 0) return false;
894895
895896 // Handle 'null' like i32/i64 0.
896 if (isa(Op1))
897 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
897 if (isa(Op1)) {
898 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getType()));
899 }
898900
899901 // We have two options: compare with register or immediate. If the RHS of
900902 // the compare is an immediate that we can fold into this compare, use
5353 if (const char *bzeroEntry = V &&
5454 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5555 EVT IntPtr = TLI.getPointerTy();
56 Type *IntPtrTy = getDataLayout()->getIntPtrType(*DAG.getContext());
56 unsigned AS = DstPtrInfo.getAddrSpace();
57 Type *IntPtrTy = getDataLayout()->getIntPtrType(*DAG.getContext(), AS);
5758 TargetLowering::ArgListTy Args;
5859 TargetLowering::ArgListEntry Entry;
5960 Entry.Node = Dst;
476476 }
477477
478478 // Lower to a call to __misaligned_load(BasePtr).
479 Type *IntPtrTy = getDataLayout()->getIntPtrType(*DAG.getContext());
479 unsigned AS = LD->getAddressSpace();
480 Type *IntPtrTy = getDataLayout()->getIntPtrType(*DAG.getContext(), AS);
480481 TargetLowering::ArgListTy Args;
481482 TargetLowering::ArgListEntry Entry;
482483
535536 }
536537
537538 // Lower to a call to __misaligned_store(BasePtr, Value).
538 Type *IntPtrTy = getDataLayout()->getIntPtrType(*DAG.getContext());
539 unsigned AS = ST->getAddressSpace();
540 Type *IntPtrTy = getDataLayout()->getIntPtrType(*DAG.getContext(), AS);
539541 TargetLowering::ArgListTy Args;
540542 TargetLowering::ArgListEntry Entry;
541543
14991499 unsigned TypeSize = TD->getTypeAllocSize(FieldTy);
15001500 if (StructType *ST = dyn_cast(FieldTy))
15011501 TypeSize = TD->getStructLayout(ST)->getSizeInBytes();
1502 Type *IntPtrTy = TD->getIntPtrType(CI->getContext());
1502 Type *IntPtrTy = TD->getIntPtrType(GV->getType());
15031503 Value *NMI = CallInst::CreateMalloc(CI, IntPtrTy, FieldTy,
15041504 ConstantInt::get(IntPtrTy, TypeSize),
15051505 NElems, 0,
17291729 // If this is a fixed size array, transform the Malloc to be an alloc of
17301730 // structs. malloc [100 x struct],1 -> malloc struct, 100
17311731 if (ArrayType *AT = dyn_cast(getMallocAllocatedType(CI, TLI))) {
1732 Type *IntPtrTy = TD->getIntPtrType(CI->getContext());
1732 Type *IntPtrTy = TD->getIntPtrType(GV->getType());
17331733 unsigned TypeSize = TD->getStructLayout(AllocSTy)->getSizeInBytes();
17341734 Value *AllocSize = ConstantInt::get(IntPtrTy, TypeSize);
17351735 Value *NumElements = ConstantInt::get(IntPtrTy, AT->getNumElements());
205205 return true;
206206 if (Ty1->getTypeID() != Ty2->getTypeID()) {
207207 if (TD) {
208 LLVMContext &Ctx = Ty1->getContext();
209 if (isa(Ty1) && Ty2 == TD->getIntPtrType(Ctx)) return true;
210 if (isa(Ty2) && Ty1 == TD->getIntPtrType(Ctx)) return true;
208 if (isa(Ty1) && Ty2 == TD->getIntPtrType(Ty1)) return true;
209 if (isa(Ty2) && Ty1 == TD->getIntPtrType(Ty2)) return true;
211210 }
212211 return false;
213212 }
207207 bool ShouldChangeType(Type *From, Type *To) const;
208208 Value *dyn_castNegVal(Value *V) const;
209209 Value *dyn_castFNegVal(Value *V) const;
210 Type *FindElementAtOffset(Type *Ty, int64_t Offset,
210 Type *FindElementAtOffset(Type *Ty, int64_t Offset, Type *IntPtrTy,
211211 SmallVectorImpl &NewIndices);
212212 Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI);
213213
995995 // Conversion is ok if changing from one pointer type to another or from
996996 // a pointer to an integer of the same size.
997997 !((OldRetTy->isPointerTy() || !TD ||
998 OldRetTy == TD->getIntPtrType(Caller->getContext())) &&
998 OldRetTy == TD->getIntPtrType(NewRetTy)) &&
999999 (NewRetTy->isPointerTy() || !TD ||
1000 NewRetTy == TD->getIntPtrType(Caller->getContext()))))
1000 NewRetTy == TD->getIntPtrType(OldRetTy))))
10011001 return false; // Cannot transform this return value.
10021002
10031003 if (!Caller->use_empty() &&
10561056
10571057 // Converting from one pointer type to another or between a pointer and an
10581058 // integer of the same size is safe even if we do not have a body.
1059 // FIXME: Not sure what to do here, so setting AS to 0.
1060 // How can the AS for a function call be outside the default?
10591061 bool isConvertible = ActTy == ParamTy ||
10601062 (TD && ((ParamTy->isPointerTy() ||
1061 ParamTy == TD->getIntPtrType(Caller->getContext())) &&
1063 ParamTy == TD->getIntPtrType(ActTy)) &&
10621064 (ActTy->isPointerTy() ||
1063 ActTy == TD->getIntPtrType(Caller->getContext()))));
1065 ActTy == TD->getIntPtrType(ParamTy))));
10641066 if (Callee->isDeclaration() && !isConvertible) return false;
10651067 }
10661068
2929 Scale = 0;
3030 return ConstantInt::get(Val->getType(), 0);
3131 }
32
32
3333 if (BinaryOperator *I = dyn_cast(Val)) {
3434 // Cannot look past anything that might overflow.
3535 OverflowingBinaryOperator *OBI = dyn_cast(Val);
4646 Offset = 0;
4747 return I->getOperand(0);
4848 }
49
49
5050 if (I->getOpcode() == Instruction::Mul) {
5151 // This value is scaled by 'RHS'.
5252 Scale = RHS->getZExtValue();
5353 Offset = 0;
5454 return I->getOperand(0);
5555 }
56
56
5757 if (I->getOpcode() == Instruction::Add) {
58 // We have X+C. Check to see if we really have (X*C2)+C1,
58 // We have X+C. Check to see if we really have (X*C2)+C1,
5959 // where C1 is divisible by C2.
6060 unsigned SubScale;
61 Value *SubVal =
61 Value *SubVal =
6262 DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset);
6363 Offset += RHS->getZExtValue();
6464 Scale = SubScale;
8181 if (!TD) return 0;
8282
8383 PointerType *PTy = cast(CI.getType());
84
84
8585 BuilderTy AllocaBuilder(*Builder);
8686 AllocaBuilder.SetInsertPoint(AI.getParent(), &AI);
8787
109109 uint64_t ArrayOffset;
110110 Value *NumElements = // See if the array size is a decomposable linear expr.
111111 DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset);
112
112
113113 // If we can now satisfy the modulus, by using a non-1 scale, we really can
114114 // do the xform.
115115 if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 ||
124124 // Insert before the alloca, not before the cast.
125125 Amt = AllocaBuilder.CreateMul(Amt, NumElements);
126126 }
127
127
128128 if (uint64_t Offset = (AllocElTySize*ArrayOffset)/CastElTySize) {
129129 Value *Off = ConstantInt::get(AI.getArraySize()->getType(),
130130 Offset, true);
131131 Amt = AllocaBuilder.CreateAdd(Amt, Off);
132132 }
133
133
134134 AllocaInst *New = AllocaBuilder.CreateAlloca(CastElTy, Amt);
135135 New->setAlignment(AI.getAlignment());
136136 New->takeName(&AI);
137
137
138138 // If the allocation has multiple real uses, insert a cast and change all
139139 // things that used it to use the new cast. This will also hack on CI, but it
140140 // will die soon.
147147 return ReplaceInstUsesWith(CI, New);
148148 }
149149
150 /// EvaluateInDifferentType - Given an expression that
150 /// EvaluateInDifferentType - Given an expression that
151151 /// CanEvaluateTruncated or CanEvaluateSExtd returns true for, actually
152152 /// insert the code to evaluate the expression.
153 Value *InstCombiner::EvaluateInDifferentType(Value *V, Type *Ty,
153 Value *InstCombiner::EvaluateInDifferentType(Value *V, Type *Ty,
154154 bool isSigned) {
155155 if (Constant *C = dyn_cast(V)) {
156156 C = ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/);
180180 Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);
181181 Res = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
182182 break;
183 }
183 }
184184 case Instruction::Trunc:
185185 case Instruction::ZExt:
186186 case Instruction::SExt:
189189 // new.
190190 if (I->getOperand(0)->getType() == Ty)
191191 return I->getOperand(0);
192
192
193193 // Otherwise, must be the same type of cast, so just reinsert a new one.
194194 // This also handles the case of zext(trunc(x)) -> zext(x).
195195 Res = CastInst::CreateIntegerCast(I->getOperand(0), Ty,
211211 Res = NPN;
212212 break;
213213 }
214 default:
214 default:
215215 // TODO: Can handle more cases here.
216216 llvm_unreachable("Unreachable!");
217217 }
218
218
219219 Res->takeName(I);
220220 return InsertNewInstWith(Res, *I);
221221 }
223223
224224 /// This function is a wrapper around CastInst::isEliminableCastPair. It
225225 /// simply extracts arguments and returns what that function returns.
226 static Instruction::CastOps
226 static Instruction::CastOps
227227 isEliminableCastPair(
228228 const CastInst *CI, ///< The first cast instruction
229229 unsigned opcode, ///< The opcode of the second cast instruction
237237 // Get the opcodes of the two Cast instructions
238238 Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode());
239239 Instruction::CastOps secondOp = Instruction::CastOps(opcode);
240
241240 unsigned Res = CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy,
242241 DstTy,
243 TD ? TD->getIntPtrType(CI->getContext()) : 0);
244
242 TD ? TD->getIntPtrType(DstTy) : 0);
243
245244 // We don't want to form an inttoptr or ptrtoint that converts to an integer
246245 // type that differs from the pointer size.
247246 if ((Res == Instruction::IntToPtr &&
248 (!TD || SrcTy != TD->getIntPtrType(CI->getContext()))) ||
247 (!TD || SrcTy != TD->getIntPtrType(DstTy))) ||
249248 (Res == Instruction::PtrToInt &&
250 (!TD || DstTy != TD->getIntPtrType(CI->getContext()))))
249 (!TD || DstTy != TD->getIntPtrType(SrcTy))))
251250 Res = 0;
252
251
253252 return Instruction::CastOps(Res);
254253 }
255254
261260 Type *Ty) {
262261 // Noop casts and casts of constants should be eliminated trivially.
263262 if (V->getType() == Ty || isa(V)) return false;
264
263
265264 // If this is another cast that can be eliminated, we prefer to have it
266265 // eliminated.
267266 if (const CastInst *CI = dyn_cast(V))
268267 if (isEliminableCastPair(CI, opc, Ty, TD))
269268 return false;
270
269
271270 // If this is a vector sext from a compare, then we don't want to break the
272271 // idiom where each element of the extended vector is either zero or all ones.
273272 if (opc == Instruction::SExt && isa(V) && Ty->isVectorTy())
274273 return false;
275
274
276275 return true;
277276 }
278277
284283 // Many cases of "cast of a cast" are eliminable. If it's eliminable we just
285284 // eliminate it now.
286285 if (CastInst *CSrc = dyn_cast(Src)) { // A->B->C cast
287 if (Instruction::CastOps opc =
286 if (Instruction::CastOps opc =
288287 isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), TD)) {
289288 // The first cast (CSrc) is eliminable so we need to fix up or replace
290289 // the second cast (CI). CSrc will then have a good chance of being dead.
307306 if (Instruction *NV = FoldOpIntoPhi(CI))
308307 return NV;
309308 }
310
309
311310 return 0;
312311 }
313312
326325 // We can always evaluate constants in another type.
327326 if (isa(V))
328327 return true;
329
328
330329 Instruction *I = dyn_cast(V);
331330 if (!I) return false;
332
331
333332 Type *OrigTy = V->getType();
334
333
335334 // If this is an extension from the dest type, we can eliminate it, even if it
336335 // has multiple uses.
337 if ((isa(I) || isa(I)) &&
336 if ((isa(I) || isa(I)) &&
338337 I->getOperand(0)->getType() == Ty)
339338 return true;
340339
419418 // TODO: Can handle more cases here.
420419 break;
421420 }
422
421
423422 return false;
424423 }
425424
426425 Instruction *InstCombiner::visitTrunc(TruncInst &CI) {
427426 if (Instruction *Result = commonCastTransforms(CI))
428427 return Result;
429
430 // See if we can simplify any instructions used by the input whose sole
428
429 // See if we can simplify any instructions used by the input whose sole
431430 // purpose is to compute bits we don't care about.
432431 if (SimplifyDemandedInstructionBits(CI))
433432 return &CI;
434
433
435434 Value *Src = CI.getOperand(0);
436435 Type *DestTy = CI.getType(), *SrcTy = Src->getType();
437
436
438437 // Attempt to truncate the entire input expression tree to the destination
439438 // type. Only do this if the dest type is a simple type, don't convert the
440439 // expression tree to something weird like i93 unless the source is also
441440 // strange.
442441 if ((DestTy->isVectorTy() || ShouldChangeType(SrcTy, DestTy)) &&
443442 CanEvaluateTruncated(Src, DestTy)) {
444
443
445444 // If this cast is a truncate, evaluting in a different type always
446445 // eliminates the cast, so it is always a win.
447446 DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type"
458457 Value *Zero = Constant::getNullValue(Src->getType());
459458 return new ICmpInst(ICmpInst::ICMP_NE, Src, Zero);
460459 }
461
460
462461 // Transform trunc(lshr (zext A), Cst) to eliminate one type conversion.
463462 Value *A = 0; ConstantInt *Cst = 0;
464463 if (Src->hasOneUse() &&
468467 // ASize < MidSize and MidSize > ResultSize, but don't know the relation
469468 // between ASize and ResultSize.
470469 unsigned ASize = A->getType()->getPrimitiveSizeInBits();
471
470
472471 // If the shift amount is larger than the size of A, then the result is
473472 // known to be zero because all the input bits got shifted out.
474473 if (Cst->getZExtValue() >= ASize)
481480 Shift->takeName(Src);
482481 return CastInst::CreateIntegerCast(Shift, CI.getType(), false);
483482 }
484
483
485484 // Transform "trunc (and X, cst)" -> "and (trunc X), cst" so long as the dest
486485 // type isn't non-native.
487486 if (Src->hasOneUse() && isa(Src->getType()) &&
504503 // cast to integer to avoid the comparison.
505504 if (ConstantInt *Op1C = dyn_cast(ICI->getOperand(1))) {
506505 const APInt &Op1CV = Op1C->getValue();
507
506
508507 // zext (x x>>u31 true if signbit set.
509508 // zext (x >s -1) to i32 --> (x>>u31)^1 true if signbit clear.
510509 if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) ||
534533 // zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set.
535534 // zext (X != 1) to i32 --> X^1 iff X has only the low bit set.
536535 // zext (X != 2) to i32 --> (X>>1)^1 iff X has only the 2nd bit set.
537 if ((Op1CV == 0 || Op1CV.isPowerOf2()) &&
536 if ((Op1CV == 0 || Op1CV.isPowerOf2()) &&
538537 // This only works for EQ and NE
539538 ICI->isEquality()) {
540539 // If Op1C some other power of two, convert:
541540 uint32_t BitWidth = Op1C->getType()->getBitWidth();
542541 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
543542 ComputeMaskedBits(ICI->getOperand(0), KnownZero, KnownOne);
544
543
545544 APInt KnownZeroMask(~KnownZero);
546545 if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1?
547546 if (!DoXform) return ICI;
555554 Res = ConstantExpr::getZExt(Res, CI.getType());
556555 return ReplaceInstUsesWith(CI, Res);
557556 }
558
557
559558 uint32_t ShiftAmt = KnownZeroMask.logBase2();
560559 Value *In = ICI->getOperand(0);
561560 if (ShiftAmt) {
564563 In = Builder->CreateLShr(In, ConstantInt::get(In->getType(),ShiftAmt),
565564 In->getName()+".lobit");
566565 }
567
566
568567 if ((Op1CV != 0) == isNE) { // Toggle the low bit.
569568 Constant *One = ConstantInt::get(In->getType(), 1);
570569 In = Builder->CreateXor(In, One);
571570 }
572
571
573572 if (CI.getType() == In->getType())
574573 return ReplaceInstUsesWith(CI, In);
575574 return CastInst::CreateIntegerCast(In, CI.getType(), false/*ZExt*/);
642641 BitsToClear = 0;
643642 if (isa(V))
644643 return true;
645
644
646645 Instruction *I = dyn_cast(V);
647646 if (!I) return false;
648
647
649648 // If the input is a truncate from the destination type, we can trivially
650649 // eliminate it.
651650 if (isa(I) && I->getOperand(0)->getType() == Ty)
652651 return true;
653
652
654653 // We can't extend or shrink something that has multiple uses: doing so would
655654 // require duplicating the instruction in general, which isn't profitable.
656655 if (!I->hasOneUse()) return false;
657
656
658657 unsigned Opc = I->getOpcode(), Tmp;
659658 switch (Opc) {
660659 case Instruction::ZExt: // zext(zext(x)) -> zext(x).
674673 // These can all be promoted if neither operand has 'bits to clear'.
675674 if (BitsToClear == 0 && Tmp == 0)
676675 return true;
677
676
678677 // If the operation is an AND/OR/XOR and the bits to clear are zero in the
679678 // other side, BitsToClear is ok.
680679 if (Tmp == 0 &&
687686 APInt::getHighBitsSet(VSize, BitsToClear)))
688687 return true;
689688 }
690
689
691690 // Otherwise, we don't know how to analyze this BitsToClear case yet.
692691 return false;
693
692
694693 case Instruction::LShr:
695694 // We can promote lshr(x, cst) if we can promote x. This requires the
696695 // ultimate 'and' to clear out the high zero bits we're clearing out though.
712711 Tmp != BitsToClear)
713712 return false;
714713 return true;
715
714
716715 case Instruction::PHI: {
717716 // We can change a phi if we can change all operands. Note that we never
718717 // get into trouble with cyclic PHIs here because we only consider
739738 // eliminated before we try to optimize this zext.
740739 if (CI.hasOneUse() && isa(CI.use_back()))
741740 return 0;
742
741
743742 // If one of the common conversion will work, do it.
744743 if (Instruction *Result = commonCastTransforms(CI))
745744 return Result;
746745
747 // See if we can simplify any instructions used by the input whose sole
746 // See if we can simplify any instructions used by the input whose sole
748747 // purpose is to compute bits we don't care about.
749748 if (SimplifyDemandedInstructionBits(CI))
750749 return &CI;
751
750
752751 Value *Src = CI.getOperand(0);
753752 Type *SrcTy = Src->getType(), *DestTy = CI.getType();
754
753
755754 // Attempt to extend the entire input expression tree to the destination
756755 // type. Only do this if the dest type is a simple type, don't convert the
757756 // expression tree to something weird like i93 unless the source is also
758757 // strange.
759758 unsigned BitsToClear;
760759 if ((DestTy->isVectorTy() || ShouldChangeType(SrcTy, DestTy)) &&
761 CanEvaluateZExtd(Src, DestTy, BitsToClear)) {
760 CanEvaluateZExtd(Src, DestTy, BitsToClear)) {
762761 assert(BitsToClear < SrcTy->getScalarSizeInBits() &&
763762 "Unreasonable BitsToClear");
764
763
765764 // Okay, we can transform this! Insert the new expression now.
766765 DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type"
767766 " to avoid zero extend: " << CI);
768767 Value *Res = EvaluateInDifferentType(Src, DestTy, false);
769768 assert(Res->getType() == DestTy);
770
769
771770 uint32_t SrcBitsKept = SrcTy->getScalarSizeInBits()-BitsToClear;
772771 uint32_t DestBitSize = DestTy->getScalarSizeInBits();
773
772
774773 // If the high bits are already filled with zeros, just replace this
775774 // cast with the result.
776775 if (MaskedValueIsZero(Res, APInt::getHighBitsSet(DestBitSize,
777776 DestBitSize-SrcBitsKept)))
778777 return ReplaceInstUsesWith(CI, Res);
779
778
780779 // We need to emit an AND to clear the high bits.
781780 Constant *C = ConstantInt::get(Res->getType(),
782781 APInt::getLowBitsSet(DestBitSize, SrcBitsKept));
788787 // 'and' which will be much cheaper than the pair of casts.
789788 if (TruncInst *CSrc = dyn_cast(Src)) { // A->B->C cast
790789 // TODO: Subsume this into EvaluateInDifferentType.
791
790
792791 // Get the sizes of the types involved. We know that the intermediate type
793792 // will be smaller than A or C, but don't know the relation between A and C.
794793 Value *A = CSrc->getOperand(0);
805804 Value *And = Builder->CreateAnd(A, AndConst, CSrc->getName()+".mask");
806805 return new ZExtInst(And, CI.getType());
807806 }
808
807
809808 if (SrcSize == DstSize) {
810809 APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
811810 return BinaryOperator::CreateAnd(A, ConstantInt::get(A->getType(),
814813 if (SrcSize > DstSize) {
815814 Value *Trunc = Builder->CreateTrunc(A, CI.getType());
816815 APInt AndValue(APInt::getLowBitsSet(DstSize, MidSize));
817 return BinaryOperator::CreateAnd(Trunc,
816 return BinaryOperator::CreateAnd(Trunc,
818817 ConstantInt::get(Trunc->getType(),
819818 AndValue));
820819 }
872871 Value *New = Builder->CreateZExt(X, CI.getType());
873872 return BinaryOperator::CreateXor(New, ConstantInt::get(CI.getType(), 1));
874873 }
875
874
876875 return 0;
877876 }
878877
985984 // If this is a constant, it can be trivially promoted.
986985 if (isa(V))
987986 return true;
988
987
989988 Instruction *I = dyn_cast(V);
990989 if (!I) return false;
991
990
992991 // If this is a truncate from the dest type, we can trivially eliminate it.
993992 if (isa(I) && I->getOperand(0)->getType() == Ty)
994993 return true;
995
994
996995 // We can't extend or shrink something that has multiple uses: doing so would
997996 // require duplicating the instruction in general, which isn't profitable.
998997 if (!I->hasOneUse()) return false;
10111010 // These operators can all arbitrarily be extended if their inputs can.
10121011 return CanEvaluateSExtd(I->getOperand(0), Ty) &&
10131012 CanEvaluateSExtd(I->getOperand(1), Ty);
1014
1013
10151014 //case Instruction::Shl: TODO
10161015 //case Instruction::LShr: TODO
1017
1016
10181017 case Instruction::Select:
10191018 return CanEvaluateSExtd(I->getOperand(1), Ty) &&
10201019 CanEvaluateSExtd(I->getOperand(2), Ty);
1021
1020
10221021 case Instruction::PHI: {
10231022 // We can change a phi if we can change all operands. Note that we never
10241023 // get into trouble with cyclic PHIs here because we only consider
10321031 // TODO: Can handle more cases here.
10331032 break;
10341033 }
1035
1034
10361035 return false;
10371036 }
10381037
10411040 // eliminated before we try to optimize this zext.
10421041 if (CI.hasOneUse() && isa(CI.use_back()))
10431042 return 0;
1044
1043
10451044 if (Instruction *I = commonCastTransforms(CI))
10461045 return I;
1047
1048 // See if we can simplify any instructions used by the input whose sole
1046
1047 // See if we can simplify any instructions used by the input whose sole
10491048 // purpose is to compute bits we don't care about.
10501049 if (SimplifyDemandedInstructionBits(CI))
10511050 return &CI;
1052
1051
10531052 Value *Src = CI.getOperand(0);
10541053 Type *SrcTy = Src->getType(), *DestTy = CI.getType();
10551054
10721071 // cast with the result.
10731072 if (ComputeNumSignBits(Res) > DestBitSize - SrcBitSize)
10741073 return ReplaceInstUsesWith(CI, Res);
1075
1074
10761075 // We need to emit a shl + ashr to do the sign extend.
10771076 Value *ShAmt = ConstantInt::get(DestTy, DestBitSize-SrcBitSize);
10781077 return BinaryOperator::CreateAShr(Builder->CreateShl(Res, ShAmt, "sext"),
10851084 if (TI->hasOneUse() && TI->getOperand(0)->getType() == DestTy) {
10861085 uint32_t SrcBitSize = SrcTy->getScalarSizeInBits();
10871086 uint32_t DestBitSize = DestTy->getScalarSizeInBits();
1088
1087
10891088 // We need to emit a shl + ashr to do the sign extend.
10901089 Value *ShAmt = ConstantInt::get(DestTy, DestBitSize-SrcBitSize);
10911090 Value *Res = Builder->CreateShl(TI->getOperand(0), ShAmt, "sext");
11211120 A = Builder->CreateShl(A, ShAmtV, CI.getName());
11221121 return BinaryOperator::CreateAShr(A, ShAmtV);
11231122 }
1124
1123
11251124 return 0;
11261125 }
11271126
11431142 if (Instruction *I = dyn_cast(V))
11441143 if (I->getOpcode() == Instruction::FPExt)
11451144 return LookThroughFPExtensions(I->getOperand(0));
1146
1145
11471146 // If this value is a constant, return the constant in the smallest FP type
11481147 // that can accurately represent it. This allows us to turn
11491148 // (float)((double)X+2.0) into x+2.0f.
11621161 return V;
11631162 // Don't try to shrink to various long double types.
11641163 }
1165
1164
11661165 return V;
11671166 }
11681167
11691168 Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) {
11701169 if (Instruction *I = commonCastTransforms(CI))
11711170 return I;
1172
1171
11731172 // If we have fptrunc(fadd (fpextend x), (fpextend y)), where x and y are
11741173 // smaller than the destination type, we can eliminate the truncate by doing
11751174 // the add as the smaller type. This applies to fadd/fsub/fmul/fdiv as well
11861185 Type *SrcTy = OpI->getType();
11871186 Value *LHSTrunc = LookThroughFPExtensions(OpI->getOperand(0));
11881187 Value *RHSTrunc = LookThroughFPExtensions(OpI->getOperand(1));
1189 if (LHSTrunc->getType() != SrcTy &&
1188 if (LHSTrunc->getType() != SrcTy &&
11901189 RHSTrunc->getType() != SrcTy) {
11911190 unsigned DstSize = CI.getType()->getScalarSizeInBits();
11921191 // If the source types were both smaller than the destination type of
11981197 return BinaryOperator::Create(OpI->getOpcode(), LHSTrunc, RHSTrunc);
11991198 }
12001199 }
1201 break;
1202 }
1203 }
1204
1200 break;
1201 }
1202 }
1203
12051204 // Fold (fptrunc (sqrt (fpext x))) -> (sqrtf x)
12061205 CallInst *Call = dyn_cast(CI.getOperand(0));
12071206 if (Call && Call->getCalledFunction() && TLI->has(LibFunc::sqrtf) &&
12161215 Arg->getOperand(0)->getType()->isFloatTy()) {
12171216 Function *Callee = Call->getCalledFunction();
12181217 Module *M = CI.getParent()->getParent()->getParent();
1219 Constant *SqrtfFunc = M->getOrInsertFunction("sqrtf",
1218 Constant *SqrtfFunc = M->getOrInsertFunction("sqrtf",
12201219 Callee->getAttributes(),
12211220 Builder->getFloatTy(),
12221221 Builder->getFloatTy(),
12241223 CallInst *ret = CallInst::Create(SqrtfFunc, Arg->getOperand(0),
12251224 "sqrtfcall");
12261225 ret->setAttributes(Callee->getAttributes());
1227
1228
1226
1227
12291228 // Remove the old Call. With -fmath-errno, it won't get marked readnone.
12301229 ReplaceInstUsesWith(*Call, UndefValue::get(Call->getType()));
12311230 EraseInstFromFunction(*Call);
12321231 return ret;
12331232 }
12341233 }
1235
1234
12361235 return 0;
12371236 }
12381237
12501249 // This is safe if the intermediate type has enough bits in its mantissa to
12511250 // accurately represent all values of X. For example, do not do this with
12521251 // i64->float->i64. This is also safe for sitofp case, because any negative
1253 // 'X' value would cause an undefined result for the fptoui.
1252 // 'X' value would cause an undefined result for the fptoui.
12541253 if ((isa(OpI) || isa(OpI)) &&
12551254 OpI->getOperand(0)->getType() == FI.getType() &&
12561255 (int)FI.getType()->getScalarSizeInBits() < /*extra bit for sign */
12641263 Instruction *OpI = dyn_cast(FI.getOperand(0));
12651264 if (OpI == 0)
12661265 return commonCastTransforms(FI);
1267
1266
12681267 // fptosi(sitofp(X)) --> X
12691268 // fptosi(uitofp(X)) --> X
12701269 // This is safe if the intermediate type has enough bits in its mantissa to
12711270 // accurately represent all values of X. For example, do not do this with
12721271 // i64->float->i64. This is also safe for sitofp case, because any negative
1273 // 'X' value would cause an undefined result for the fptoui.
1272 // 'X' value would cause an undefined result for the fptoui.
12741273 if ((isa(OpI) || isa(OpI)) &&
12751274 OpI->getOperand(0)->getType() == FI.getType() &&
12761275 (int)FI.getType()->getScalarSizeInBits() <=
12771276 OpI->getType()->getFPMantissaWidth())
12781277 return ReplaceInstUsesWith(FI, OpI->getOperand(0));
1279
1278
12801279 return commonCastTransforms(FI);
12811280 }
12821281
12971296 if (CI.getOperand(0)->getType()->getScalarSizeInBits() >
12981297 TD->getPointerSizeInBits(AS)) {
12991298 Value *P = Builder->CreateTrunc(CI.getOperand(0),
1300 TD->getIntPtrType(CI.getContext()));
1299 TD->getIntPtrType(CI.getType()));
13011300 return new IntToPtrInst(P, CI.getType());
13021301 }
13031302 if (CI.getOperand(0)->getType()->getScalarSizeInBits() <
13041303 TD->getPointerSizeInBits(AS)) {
13051304 Value *P = Builder->CreateZExt(CI.getOperand(0),
1306 TD->getIntPtrType(CI.getContext()));
1305 TD->getIntPtrType(CI.getType()));
13071306 return new IntToPtrInst(P, CI.getType());
13081307 }
13091308 }
1310
1309
13111310 if (Instruction *I = commonCastTransforms(CI))
13121311 return I;
13131312
13171316 /// @brief Implement the transforms for cast of pointer (bitcast/ptrtoint)
13181317 Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) {
13191318 Value *Src = CI.getOperand(0);
1320
1319
13211320 if (GetElementPtrInst *GEP = dyn_cast(Src)) {
13221321 // If casting the result of a getelementptr instruction with no offset, turn
13231322 // this into a cast of the original pointer!
13241323 if (GEP->hasAllZeroIndices()) {
13251324 // Changing the cast operand is usually not a good idea but it is safe
1326 // here because the pointer operand is being replaced with another
1325 // here because the pointer operand is being replaced with another
13271326 // pointer operand so the opcode doesn't need to change.
13281327 Worklist.Add(GEP);
13291328 CI.setOperand(0, GEP->getOperand(0));
13301329 return &CI;
13311330 }
1332
1331
13331332 // If the GEP has a single use, and the base pointer is a bitcast, and the
13341333 // GEP computes a constant offset, see if we can convert these three
13351334 // instructions into fewer. This typically happens with unions and other
13441343 Type *GEPIdxTy =
13451344 cast(OrigBase->getType())->getElementType();
13461345 SmallVector NewIndices;
1347 if (FindElementAtOffset(GEPIdxTy, Offset, NewIndices)) {
1346 Type *IntPtrTy = TD->getIntPtrType(OrigBase->getType());
1347 if (FindElementAtOffset(GEPIdxTy, Offset, IntPtrTy, NewIndices)) {
13481348 // If we were able to index down into an element, create the GEP
13491349 // and bitcast the result. This eliminates one bitcast, potentially
13501350 // two.
13521352 Builder->CreateInBoundsGEP(OrigBase, NewIndices) :
13531353 Builder->CreateGEP(OrigBase, NewIndices);
13541354 NGEP->takeName(GEP);
1355
1355
13561356 if (isa(CI))
13571357 return new BitCastInst(NGEP, CI.getType());
13581358 assert(isa(CI));
13591359 return new PtrToIntInst(NGEP, CI.getType());
1360 }
1361 }
1362 }
1363
1360 }
1361 }
1362 }
1363
13641364 return commonCastTransforms(CI);
13651365 }
13661366
13721372 if (TD) {
13731373 if (CI.getType()->getScalarSizeInBits() < TD->getPointerSizeInBits(AS)) {
13741374 Value *P = Builder->CreatePtrToInt(CI.getOperand(0),
1375 TD->getIntPtrType(CI.getContext()));
1375 TD->getIntPtrType(CI.getContext(), AS));
13761376 return new TruncInst(P, CI.getType());
13771377 }
13781378 if (CI.getType()->getScalarSizeInBits() > TD->getPointerSizeInBits(AS)) {
13791379 Value *P = Builder->CreatePtrToInt(CI.getOperand(0),
1380 TD->getIntPtrType(CI.getContext()));
1380 TD->getIntPtrType(CI.getContext(), AS));
13811381 return new ZExtInst(P, CI.getType());
13821382 }
13831383 }
1384
1384
13851385 return commonPointerCastTransforms(CI);
13861386 }
13871387
13961396 // element size, or the input is a multiple of the output element size.
13971397 // Convert the input type to have the same element type as the output.
13981398 VectorType *SrcTy = cast(InVal->getType());
1399
1399
14001400 if (SrcTy->getElementType() != DestTy->getElementType()) {
14011401 // The input types don't need to be identical, but for now they must be the
14021402 // same size. There is no specific reason we couldn't handle things like
14031403 // <4 x i16> -> <4 x i32> by bitcasting to <2 x i32> but haven't gotten
1404 // there yet.
1404 // there yet.
14051405 if (SrcTy->getElementType()->getPrimitiveSizeInBits() !=
14061406 DestTy->getElementType()->getPrimitiveSizeInBits())
14071407 return 0;
1408
1408
14091409 SrcTy = VectorType::get(DestTy->getElementType(), SrcTy->getNumElements());
14101410 InVal = IC.Builder->CreateBitCast(InVal, SrcTy);
14111411 }
1412
1412
14131413 // Now that the element types match, get the shuffle mask and RHS of the
14141414 // shuffle to use, which depends on whether we're increasing or decreasing the
14151415 // size of the input.
14161416 SmallVector ShuffleMask;
14171417 Value *V2;
1418
1418
14191419 if (SrcTy->getNumElements() > DestTy->getNumElements()) {
14201420 // If we're shrinking the number of elements, just shuffle in the low
14211421 // elements from the input and use undef as the second shuffle input.
14221422 V2 = UndefValue::get(SrcTy);
14231423 for (unsigned i = 0, e = DestTy->getNumElements(); i != e; ++i)
14241424 ShuffleMask.push_back(i);
1425
1425
14261426 } else {
14271427 // If we're increasing the number of elements, shuffle in all of the
14281428 // elements from InVal and fill the rest of the result elements with zeros
14361436 for (unsigned i = 0, e = DestTy->getNumElements()-SrcElts; i != e; ++i)
14371437 ShuffleMask.push_back(SrcElts);
14381438 }
1439
1439
14401440 return new ShuffleVectorInst(InVal, V2,
14411441 ConstantDataVector::get(V2->getContext(),
14421442 ShuffleMask));
14631463 Type *VecEltTy) {
14641464 // Undef values never contribute useful bits to the result.
14651465 if (isa(V)) return true;
1466
1466
14671467 // If we got down to a value of the right type, we win, try inserting into the
14681468 // right element.
14691469 if (V->getType() == VecEltTy) {
14711471 if (Constant *C = dyn_cast(V))
14721472 if (C->isNullValue())
14731473 return true;
1474
1474
14751475 // Fail if multiple elements are inserted into this slot.
14761476 if (ElementIndex >= Elements.size() || Elements[ElementIndex] != 0)
14771477 return false;
1478
1478
14791479 Elements[ElementIndex] = V;
14801480 return true;
14811481 }
1482
1482
14831483 if (Constant *C = dyn_cast(V)) {
14841484 // Figure out the # elements this provides, and bitcast it or slice it up
14851485 // as required.
14901490 if (NumElts == 1)
14911491 return CollectInsertionElements(ConstantExpr::getBitCast(C, VecEltTy),
14921492 ElementIndex, Elements, VecEltTy);
1493
1493
14941494 // Okay, this is a constant that covers multiple elements. Slice it up into
14951495 // pieces and insert each element-sized piece into the vector.
14961496 if (!isa(C->getType()))
14981498 C->getType()->getPrimitiveSizeInBits()));
14991499 unsigned ElementSize = VecEltTy->getPrimitiveSizeInBits();
15001500 Type *ElementIntTy = IntegerType::get(C->getContext(), ElementSize);
1501
1501
15021502 for (unsigned i = 0; i != NumElts; ++i) {
15031503 Constant *Piece = ConstantExpr::getLShr(C, ConstantInt::get(C->getType(),
15041504 i*ElementSize));
15081508 }
15091509 return true;
15101510 }
1511
1511
15121512 if (!V->hasOneUse()) return false;
1513
1513
15141514 Instruction *I = dyn_cast(V);
15151515 if (I == 0) return false;
15161516 switch (I->getOpcode()) {
15171517 default: return false; // Unhandled case.
15181518 case Instruction::BitCast:
15191519 return CollectInsertionElements(I->getOperand(0), ElementIndex,
1520 Elements, VecEltTy);
1520 Elements, VecEltTy);
15211521 case Instruction::ZExt:
15221522 if (!isMultipleOfTypeSize(
15231523 I->getOperand(0)->getType()->getPrimitiveSizeInBits(),
15241524 VecEltTy))
15251525 return false;
15261526 return CollectInsertionElements(I->getOperand(0), ElementIndex,
1527 Elements, VecEltTy);
1527 Elements, VecEltTy);
15281528 case Instruction::Or:
15291529 return CollectInsertionElements(I->getOperand(0), ElementIndex,
15301530 Elements, VecEltTy) &&
15361536 if (CI == 0) return false;
15371537 if (!isMultipleOfTypeSize(CI->getZExtValue(), VecEltTy)) return false;
15381538 unsigned IndexShift = getTypeSizeIndex(CI->getZExtValue(), VecEltTy);
1539
1539
15401540 return CollectInsertionElements(I->getOperand(0), ElementIndex+IndexShift,
15411541 Elements, VecEltTy);
15421542 }
1543
1543
15441544 }
15451545 }
15461546
15751575 Value *Result = Constant::getNullValue(CI.getType());
15761576 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
15771577 if (Elements[i] == 0) continue; // Unset element.
1578
1578
15791579 Result = IC.Builder->CreateInsertElement(Result, Elements[i],
15801580 IC.Builder->getInt32(i));
15811581 }
1582
1582
15831583 return Result;
15841584 }
15851585
16071607 VecTy->getPrimitiveSizeInBits() / DestWidth);
16081608 VecInput = IC.Builder->CreateBitCast(VecInput, VecTy);
16091609 }
1610
1610
16111611 return ExtractElementInst::Create(VecInput, IC.Builder->getInt32(0));
16121612 }
16131613 }
1614
1614
16151615 // bitcast(trunc(lshr(bitcast(somevector), cst))
16161616 ConstantInt *ShAmt = 0;
16171617 if (match(Src, m_Trunc(m_LShr(m_BitCast(m_Value(VecInput)),
16281628 VecTy->getPrimitiveSizeInBits() / DestWidth);
16291629 VecInput = IC.Builder->CreateBitCast(VecInput, VecTy);
16301630 }
1631
1631
16321632 unsigned Elt = ShAmt->getZExtValue() / DestWidth;
16331633 return ExtractElementInst::Create(VecInput, IC.Builder->getInt32(Elt));
16341634 }
16521652 PointerType *SrcPTy = cast(SrcTy);
16531653 Type *DstElTy = DstPTy->getElementType();
16541654 Type *SrcElTy = SrcPTy->getElementType();
1655
1655
16561656 // If the address spaces don't match, don't eliminate the bitcast, which is
16571657 // required for changing types.
16581658 if (SrcPTy->getAddressSpace() != DstPTy->getAddressSpace())
16591659 return 0;
1660
1660
16611661 // If we are casting a alloca to a pointer to a type of the same
16621662 // size, rewrite the allocation instruction to allocate the "right" type.
16631663 // There is no need to modify malloc calls because it is their bitcast that
16651665 if (AllocaInst *AI = dyn_cast(Src))
16661666 if (Instruction *V = PromoteCastOfAllocation(CI, *AI))
16671667 return V;
1668
1668
16691669 // If the source and destination are pointers, and this cast is equivalent
16701670 // to a getelementptr X, 0, 0, 0... turn it into the appropriate gep.
16711671 // This can enhance SROA and other transforms that want type-safe pointers.
16721672 Constant *ZeroUInt =
16731673 Constant::getNullValue(Type::getInt32Ty(CI.getContext()));
16741674 unsigned NumZeros = 0;
1675 while (SrcElTy != DstElTy &&
1675 while (SrcElTy != DstElTy &&
16761676 isa(SrcElTy) && !SrcElTy->isPointerTy() &&
16771677 SrcElTy->getNumContainedTypes() /* not "{}" */) {
16781678 SrcElTy = cast(SrcElTy)->getTypeAtIndex(ZeroUInt);
16851685 return GetElementPtrInst::CreateInBounds(Src, Idxs);
16861686 }
16871687 }
1688
1688
16891689 // Try to optimize int -> float bitcasts.
16901690 if ((DestTy->isFloatTy() || DestTy->isDoubleTy()) && isa(SrcTy))
16911691 if (Instruction *I = OptimizeIntToFloatBitCast(CI, *this))
16981698 Constant::getNullValue(Type::getInt32Ty(CI.getContext())));
16991699 // FIXME: Canonicalize bitcast(insertelement) -> insertelement(bitcast)
17001700 }
1701
1701
17021702 if (isa(SrcTy)) {
17031703 // If this is a cast from an integer to vector, check to see if the input
17041704 // is a trunc or zext of a bitcast from vector. If so, we can replace all
17111711 cast(DestTy), *this))
17121712 return I;
17131713 }
1714
1714
17151715 // If the input is an 'or' instruction, we may be doing shifts and ors to
17161716 // assemble the elements of the vector manually. Try to rip the code out
17171717 // and replace it with insertelements.
17221722
17231723 if (VectorType *SrcVTy = dyn_cast(SrcTy)) {
17241724 if (SrcVTy->getNumElements() == 1 && !DestTy->isVectorTy()) {
1725 Value *Elem =
1725 Value *Elem =
17261726 Builder->CreateExtractElement(Src,
17271727 Constant::getNullValue(Type::getInt32Ty(CI.getContext())));
17281728 return CastInst::Create(Instruction::BitCast, Elem, DestTy);
17321732 if (ShuffleVectorInst *SVI = dyn_cast(Src)) {
17331733 // Okay, we have (bitcast (shuffle ..)). Check to see if this is
17341734 // a bitcast to a vector with the same # elts.
1735 if (SVI->hasOneUse() && DestTy->isVectorTy() &&
1735 if (SVI->hasOneUse() && DestTy->isVectorTy() &&
17361736 cast(DestTy)->getNumElements() ==
17371737 SVI->getType()->getNumElements() &&
17381738 SVI->getType()->getNumElements() ==
17411741 // If either of the operands is a cast from CI.getType(), then
17421742 // evaluating the shuffle in the casted destination's type will allow
17431743 // us to eliminate at least one cast.
1744 if (((Tmp = dyn_cast(SVI->getOperand(0))) &&
1744 if (((Tmp = dyn_cast(SVI->getOperand(0))) &&
17451745 Tmp->getOperand(0)->getType() == DestTy) ||
1746 ((Tmp = dyn_cast(SVI->getOperand(1))) &&
1746 ((Tmp = dyn_cast(SVI->getOperand(1))) &&
17471747 Tmp->getOperand(0)->getType() == DestTy)) {
17481748 Value *LHS = Builder->CreateBitCast(SVI->getOperand(0), DestTy);
17491749 Value *RHS = Builder->CreateBitCast(SVI->getOperand(1), DestTy);
17531753 }
17541754 }
17551755 }
1756
1756
17571757 if (SrcTy->isPointerTy())
17581758 return commonPointerCastTransforms(CI);
17591759 return commonCastTransforms(CI);
370370 // an inbounds GEP because the index can't be out of range.
371371 if (!GEP->isInBounds() &&
372372 Idx->getType()->getPrimitiveSizeInBits() > TD->getPointerSizeInBits(AS))
373 Idx = Builder->CreateTrunc(Idx, TD->getIntPtrType(Idx->getContext()));
373 Idx = Builder->CreateTrunc(Idx, TD->getIntPtrType(Idx->getContext(), AS));
374374
375375 // If the comparison is only true for one or two elements, emit direct
376376 // comparisons.
538538 // we don't need to bother extending: the extension won't affect where the
539539 // computation crosses zero.
540540 if (VariableIdx->getType()->getPrimitiveSizeInBits() > IntPtrWidth) {
541 Type *IntPtrTy = TD.getIntPtrType(VariableIdx->getContext());
541 Type *IntPtrTy = TD.getIntPtrType(VariableIdx->getContext(), AS);
542542 VariableIdx = IC.Builder->CreateTrunc(VariableIdx, IntPtrTy);
543543 }
544544 return VariableIdx;
560560 return 0;
561561
562562 // Okay, we can do this evaluation. Start by converting the index to intptr.
563 Type *IntPtrTy = TD.getIntPtrType(VariableIdx->getContext());
563 Type *IntPtrTy = TD.getIntPtrType(VariableIdx->getContext(), AS);
564564 if (VariableIdx->getType() != IntPtrTy)
565565 VariableIdx = IC.Builder->CreateIntCast(VariableIdx, IntPtrTy,
566566 true /*Signed*/);
22502250 case Instruction::IntToPtr:
22512251 // icmp pred inttoptr(X), null -> icmp pred X, 0
22522252 if (RHSC->isNullValue() && TD &&
2253 TD->getIntPtrType(RHSC->getContext()) ==
2253 TD->getIntPtrType(LHSI->getType()) ==
22542254 LHSI->getOperand(0)->getType())
22552255 return new ICmpInst(I.getPredicate(), LHSI->getOperand(0),
22562256 Constant::getNullValue(LHSI->getOperand(0)->getType()));
172172 // Ensure that the alloca array size argument has type intptr_t, so that
173173 // any casting is exposed early.
174174 if (TD) {
175 Type *IntPtrTy = TD->getIntPtrType(AI.getContext());
175 Type *IntPtrTy = TD->getIntPtrType(AI.getType());
176176 if (AI.getArraySize()->getType() != IntPtrTy) {
177177 Value *V = Builder->CreateIntCast(AI.getArraySize(),
178178 IntPtrTy, false);
184184 // Convert: alloca Ty, C - where C is a constant != 1 into: alloca [C x Ty], 1
185185 if (AI.isArrayAllocation()) { // Check C != 1
186186 if (const ConstantInt *C = dyn_cast(AI.getArraySize())) {
187 Type *NewTy =
187 Type *NewTy =
188188 ArrayType::get(AI.getAllocatedType(), C->getZExtValue());
189189 AllocaInst *New = Builder->CreateAlloca(NewTy, 0, AI.getName());
190190 New->setAlignment(AI.getAlignment());
310310
311311 Type *SrcPTy = SrcTy->getElementType();
312312
313 if (DestPTy->isIntegerTy() || DestPTy->isPointerTy() ||
313 if (DestPTy->isIntegerTy() || DestPTy->isPointerTy() ||
314314 DestPTy->isVectorTy()) {
315315 // If the source is an array, the code below will not succeed. Check to
316316 // see if a trivial 'gep P, 0, 0' will help matters. Only do this for
327327 }
328328
329329 if (IC.getDataLayout() &&
330 (SrcPTy->isIntegerTy() || SrcPTy->isPointerTy() ||
330 (SrcPTy->isIntegerTy() || SrcPTy->isPointerTy() ||
331331 SrcPTy->isVectorTy()) &&
332332 // Do not allow turning this into a load of an integer, which is then
333333 // casted to a pointer, this pessimizes pointer analysis a lot.
338338 // Okay, we are casting from one integer or pointer type to another of
339339 // the same size. Instead of casting the pointer before the load, cast
340340 // the result of the loaded value.
341 LoadInst *NewLoad =
341 LoadInst *NewLoad =
342342 IC.Builder->CreateLoad(CastOp, LI.isVolatile(), CI->getName());
343343 NewLoad->setAlignment(LI.getAlignment());
344344 NewLoad->setAtomic(LI.getOrdering(), LI.getSynchScope());
375375 // None of the following transforms are legal for volatile/atomic loads.
376376 // FIXME: Some of it is okay for atomic loads; needs refactoring.
377377 if (!LI.isSimple()) return 0;
378
378
379379 // Do really simple store-to-load forwarding and load CSE, to catch cases
380380 // where there are several consecutive memory accesses to the same location,
381381 // separated by a few arithmetic operations.
396396 Constant::getNullValue(Op->getType()), &LI);
397397 return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
398398 }
399 }
399 }
400400
401401 // load null/undef -> unreachable
402402 // TODO: Consider a target hook for valid address spaces for this xform.
415415 if (CE->isCast())
416416 if (Instruction *Res = InstCombineLoadCast(*this, LI, TD))
417417 return Res;
418
418
419419 if (Op->hasOneUse()) {
420420 // Change select and PHI nodes to select values instead of addresses: this
421421 // helps alias analysis out a lot, allows many others simplifications, and
469469 Type *DestPTy = cast(CI->getType())->getElementType();
470470 PointerType *SrcTy = dyn_cast(CastOp->getType());
471471 if (SrcTy == 0) return 0;
472
472
473473 Type *SrcPTy = SrcTy->getElementType();
474474
475475 if (!DestPTy->isIntegerTy() && !DestPTy->isPointerTy())
476476 return 0;
477
477
478478 /// NewGEPIndices - If SrcPTy is an aggregate type, we can emit a "noop gep"
479479 /// to its first element. This allows us to handle things like:
480480 /// store i32 xxx, (bitcast {foo*, float}* %P to i32*)
481481 /// on 32-bit hosts.
482482 SmallVector NewGEPIndices;
483
483
484484 // If the source is an array, the code below will not succeed. Check to
485485 // see if a trivial 'gep P, 0, 0' will help matters. Only do this for
486486 // constants.
488488 // Index through pointer.
489489 Constant *Zero = Constant::getNullValue(Type::getInt32Ty(SI.getContext()));
490490 NewGEPIndices.push_back(Zero);
491
491
492492 while (1) {
493493 if (StructType *STy = dyn_cast(SrcPTy)) {
494494 if (!STy->getNumElements()) /* Struct can be empty {} */
502502 break;
503503 }
504504 }
505
505
506506 SrcTy = PointerType::get(SrcPTy, SrcTy->getAddressSpace());
507507 }
508508
509509 if (!SrcPTy->isIntegerTy() && !SrcPTy->isPointerTy())
510510 return 0;
511
511
512512 // If the pointers point into different address spaces or if they point to
513513 // values with different sizes, we can't do the transformation.
514514 if (!IC.getDataLayout() ||
515 SrcTy->getAddressSpace() !=
516 cast(CI->getType())->getAddressSpace() ||
515 SrcTy->getAddressSpace() != CI->getType()->getPointerAddressSpace() ||
517516 IC.getDataLayout()->getTypeSizeInBits(SrcPTy) !=
518517 IC.getDataLayout()->getTypeSizeInBits(DestPTy))
519518 return 0;
520519
521520 // Okay, we are casting from one integer or pointer type to another of
522 // the same size. Instead of casting the pointer before
521 // the same size. Instead of casting the pointer before
523522 // the store, cast the value to be stored.
524523 Value *NewCast;
525524 Value *SIOp0 = SI.getOperand(0);
533532 if (SIOp0->getType()->isPointerTy())
534533 opcode = Instruction::PtrToInt;
535534 }
536
535
537536 // SIOp0 is a pointer to aggregate and this is a store to the first field,
538537 // emit a GEP to index into its first field.
539538 if (!NewGEPIndices.empty())
540539 CastOp = IC.Builder->CreateInBoundsGEP(CastOp, NewGEPIndices);
541
540
542541 NewCast = IC.Builder->CreateCast(opcode, SIOp0, CastDstTy,
543542 SIOp0->getName()+".c");
544543 SI.setOperand(0, NewCast);
557556 static bool equivalentAddressValues(Value *A, Value *B) {
558557 // Test if the values are trivially equivalent.
559558 if (A == B) return true;
560
559
561560 // Test if the values come form identical arithmetic instructions.
562561 // This uses isIdenticalToWhenDefined instead of isIdenticalTo because
563562 // its only used to compare two uses within the same basic block, which
570569 if (Instruction *BI = dyn_cast(B))
571570 if (cast(A)->isIdenticalToWhenDefined(BI))
572571 return true;
573
572
574573 // Otherwise they may not be equivalent.
575574 return false;
576575 }
601600 // If the RHS is an alloca with a single use, zapify the store, making the
602601 // alloca dead.
603602 if (Ptr->hasOneUse()) {
604 if (isa(Ptr))
603 if (isa(Ptr))
605604 return EraseInstFromFunction(SI);
606605 if (GetElementPtrInst *GEP = dyn_cast(Ptr)) {
607606 if (isa(GEP->getOperand(0))) {
624623 (isa(BBI) && BBI->getType()->isPointerTy())) {
625624 ScanInsts++;
626625 continue;
627 }
628
626 }
627
629628 if (StoreInst *PrevSI = dyn_cast(BBI)) {
630629 // Prev store isn't volatile, and stores to the same location?
631630 if (PrevSI->isSimple() && equivalentAddressValues(PrevSI->getOperand(1),
637636 }
638637 break;
639638 }
640
639
641640 // If this is a load, we have to stop. However, if the loaded value is from
642641 // the pointer we're loading and is producing the pointer we're storing,
643642 // then *this* store is dead (X = load P; store X -> P).
645644 if (LI == Val && equivalentAddressValues(LI->getOperand(0), Ptr) &&
646645 LI->isSimple())
647646 return EraseInstFromFunction(SI);
648
647
649648 // Otherwise, this is a load from some other location. Stores before it
650649 // may not be dead.
651650 break;
652651 }
653
652
654653 // Don't skip over loads or things that can modify memory.
655654 if (BBI->mayWriteToMemory() || BBI->mayReadFromMemory())
656655 break;
680679 if (Instruction *Res = InstCombineStoreToCast(*this, SI))
681680 return Res;
682681
683
682
684683 // If this store is the last instruction in the basic block (possibly
685684 // excepting debug info instructions), and if the block ends with an
686685 // unconditional branch, try to move it to the successor block.
687 BBI = &SI;
686 BBI = &SI;
688687 do {
689688 ++BBI;
690689 } while (isa(BBI) ||
693692 if (BI->isUnconditional())
694693 if (SimplifyStoreAtEndOfBlock(SI))
695694 return 0; // xform done!
696
695
697696 return 0;
698697 }
699698
707706 ///
708707 bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) {
709708 BasicBlock *StoreBB = SI.getParent();
710
709
711710 // Check to see if the successor block has exactly two incoming edges. If
712711 // so, see if the other predecessor contains a store to the same location.
713712 // if so, insert a PHI node (if needed) and move the stores down.
714713 BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0);
715
714
716715 // Determine whether Dest has exactly two predecessors and, if so, compute
717716 // the other predecessor.
718717 pred_iterator PI = pred_begin(DestBB);
724723
725724 if (++PI == pred_end(DestBB))
726725 return false;
727
726
728727 P = *PI;
729728 if (P != StoreBB) {
730729 if (OtherBB)
744743 BranchInst *OtherBr = dyn_cast(BBI);
745744 if (!OtherBr || BBI == OtherBB->begin())
746745 return false;
747
746
748747 // If the other block ends in an unconditional branch, check for the 'if then
749748 // else' case. there is an instruction before the branch.
750749 StoreInst *OtherStore = 0;
766765 } else {
767766 // Otherwise, the other block ended with a conditional branch. If one of the
768767 // destinations is StoreBB, then we have the if/then case.
769 if (OtherBr->getSuccessor(0) != StoreBB &&
768 if (OtherBr->getSuccessor(0) != StoreBB &&
770769 OtherBr->getSuccessor(1) != StoreBB)
771770 return false;
772
771
773772 // Okay, we know that OtherBr now goes to Dest and StoreBB, so this is an
774773 // if/then triangle. See if there is a store to the same ptr as SI that
775774 // lives in OtherBB.
787786 BBI == OtherBB->begin())
788787 return false;
789788 }
790
789
791790 // In order to eliminate the store in OtherBr, we have to
792791 // make sure nothing reads or overwrites the stored value in
793792 // StoreBB.
797796 return false;
798797 }
799798 }
800
799
801800 // Insert a PHI node now if we need it.
802801 Value *MergedVal = OtherStore->getOperand(0);
803802 if (MergedVal != SI.getOperand(0)) {
806805 PN->addIncoming(OtherStore->getOperand(0), OtherBB);
807806 MergedVal = InsertNewInstBefore(PN, DestBB->front());
808807 }
809
808
810809 // Advance to a place where it is safe to insert the new store and
811810 // insert it.
812811 BBI = DestBB->getFirstInsertionPt();
816815 SI.getOrdering(),
817816 SI.getSynchScope());
818817 InsertNewInstBefore(NewSI, *BBI);
819 NewSI->setDebugLoc(OtherStore->getDebugLoc());
818 NewSI->setDebugLoc(OtherStore->getDebugLoc());
820819
821820 // Nuke the old stores.
822821 EraseInstFromFunction(SI);
737737 /// or not there is a sequence of GEP indices into the type that will land us at
738738 /// the specified offset. If so, fill them into NewIndices and return the
739739 /// resultant element type, otherwise return null.
740 Type *InstCombiner::FindElementAtOffset(Type *Ty, int64_t Offset,
740 Type *InstCombiner::FindElementAtOffset(Type *Ty, int64_t Offset, Type *IntPtrTy,
741741 SmallVectorImpl &NewIndices) {
742742 if (!TD) return 0;
743743 if (!Ty->isSized()) return 0;
745745 // Start with the index over the outer type. Note that the type size
746746 // might be zero (even if the offset isn't zero) if the indexed type
747747 // is something like [0 x {int, int}]
748 Type *IntPtrTy = TD->getIntPtrType(Ty->getContext());
749748 int64_t FirstIdx = 0;
750749 if (int64_t TySize = TD->getTypeAllocSize(Ty)) {
751750 FirstIdx = Offset/TySize;
10541053 // by multiples of a zero size type with zero.
10551054 if (TD) {
10561055 bool MadeChange = false;
1057 Type *IntPtrTy = TD->getIntPtrType(GEP.getContext());
1056 Type *IntPtrTy = TD->getIntPtrType(PtrOp->getType());
10581057
10591058 gep_type_iterator GTI = gep_type_begin(GEP);
10601059 for (User::op_iterator I = GEP.op_begin() + 1, E = GEP.op_end();
12391238
12401239 // Earlier transforms ensure that the index has type IntPtrType, which
12411240 // considerably simplifies the logic by eliminating implicit casts.
1242 assert(Idx->getType() == TD->getIntPtrType(GEP.getContext()) &&
1241 assert(Idx->getType() == TD->getIntPtrType(GEP.getType()) &&
12431242 "Index not cast to pointer width?");
12441243
12451244 bool NSW;
12741273
12751274 // Earlier transforms ensure that the index has type IntPtrType, which
12761275 // considerably simplifies the logic by eliminating implicit casts.
1277 assert(Idx->getType() == TD->getIntPtrType(GEP.getContext()) &&
1276 assert(Idx->getType() == TD->getIntPtrType(GEP.getType()) &&
12781277 "Index not cast to pointer width?");
12791278
12801279 bool NSW;
13361335 SmallVector NewIndices;
13371336 Type *InTy =
13381337 cast(BCI->getOperand(0)->getType())->getElementType();
1339 if (FindElementAtOffset(InTy, Offset, NewIndices)) {
1338 Type *IntPtrTy = TD->getIntPtrType(BCI->getOperand(0)->getType());
1339 if (FindElementAtOffset(InTy, Offset, IntPtrTy, NewIndices)) {
13401340 Value *NGEP = GEP.isInBounds() ?
13411341 Builder->CreateInBoundsGEP(BCI->getOperand(0), NewIndices) :
13421342 Builder->CreateGEP(BCI->getOperand(0), NewIndices);
142142 Value *Offset = SizeOffset.second;
143143 ConstantInt *SizeCI = dyn_cast(Size);
144144
145 IntegerType *IntTy = TD->getIntPtrType(Inst->getContext());
145 IntegerType *IntTy = TD->getIntPtrType(Ptr->getType());
146146 Value *NeededSizeVal = ConstantInt::get(IntTy, NeededSize);
147147
148148 // three checks are required to ensure safety:
934934 DEBUG(dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for "
935935 << *MemoryInst);
936936 Type *IntPtrTy =
937 TLI->getDataLayout()->getIntPtrType(AccessTy->getContext());
937 TLI->getDataLayout()->getIntPtrType(Addr->getType());
938938
939939 Value *Result = 0;
940940
773773
774774 // Convert source pointers to integers, which can be bitcast.
775775 if (StoredValTy->isPointerTy()) {
776 StoredValTy = TD.getIntPtrType(StoredValTy->getContext());
776 StoredValTy = TD.getIntPtrType(StoredValTy);
777777 StoredVal = new PtrToIntInst(StoredVal, StoredValTy, "", InsertPt);
778778 }
779779
780780 Type *TypeToCastTo = LoadedTy;
781781 if (TypeToCastTo->isPointerTy())
782 TypeToCastTo = TD.getIntPtrType(StoredValTy->getContext());
782 TypeToCastTo = TD.getIntPtrType(StoredValTy);
783783
784784 if (StoredValTy != TypeToCastTo)
785785 StoredVal = new BitCastInst(StoredVal, TypeToCastTo, "", InsertPt);
798798
799799 // Convert source pointers to integers, which can be manipulated.
800800 if (StoredValTy->isPointerTy()) {
801 StoredValTy = TD.getIntPtrType(StoredValTy->getContext());
801 StoredValTy = TD.getIntPtrType(StoredValTy);
802802 StoredVal = new PtrToIntInst(StoredVal, StoredValTy, "", InsertPt);
803803 }
804804
10191019 // Compute which bits of the stored value are being used by the load. Convert
10201020 // to an integer type to start with.
10211021 if (SrcVal->getType()->isPointerTy())
1022 SrcVal = Builder.CreatePtrToInt(SrcVal, TD.getIntPtrType(Ctx));
1022 SrcVal = Builder.CreatePtrToInt(SrcVal,
1023 TD.getIntPtrType(SrcVal->getType()));
10231024 if (!SrcVal->getType()->isIntegerTy())
10241025 SrcVal = Builder.CreateBitCast(SrcVal, IntegerType::get(Ctx, StoreSize*8));
10251026
14291429 /// genLoopLimit - Help LinearFunctionTestReplace by generating a value that
14301430 /// holds the RHS of the new loop test.
14311431 static Value *genLoopLimit(PHINode *IndVar, const SCEV *IVCount, Loop *L,
1432 SCEVExpander &Rewriter, ScalarEvolution *SE) {
1432 SCEVExpander &Rewriter, ScalarEvolution *SE,
1433 Type *IntPtrTy) {
14331434 const SCEVAddRecExpr *AR = dyn_cast(SE->getSCEV(IndVar));
14341435 assert(AR && AR->getLoop() == L && AR->isAffine() && "bad loop counter");
14351436 const SCEV *IVInit = AR->getStart();
14551456 // We could handle pointer IVs other than i8*, but we need to compensate for
14561457 // gep index scaling. See canExpandBackedgeTakenCount comments.
14571458 assert(SE->getSizeOfExpr(
1458 cast(GEPBase->getType())->getElementType())->isOne()
1459 cast(GEPBase->getType())->getElementType(),
1460 IntPtrTy)->isOne()
14591461 && "unit stride pointer IV must be i8*");
14601462
14611463 IRBuilder<> Builder(L->getLoopPreheader()->getTerminator());
15541556 CmpIndVar = IndVar;
15551557 }
15561558
1557 Value *ExitCnt = genLoopLimit(IndVar, IVCount, L, Rewriter, SE);
1559 Type *IntPtrTy = TD ? TD->getIntPtrType(IndVar->getType()) :
1560 IntegerType::getInt64Ty(IndVar->getContext());
1561 Value *ExitCnt = genLoopLimit(IndVar, IVCount, L, Rewriter, SE, IntPtrTy);
15581562 assert(ExitCnt->getType()->isPointerTy() == IndVar->getType()->isPointerTy()
15591563 && "genLoopLimit missed a cast");
15601564
485485 // would be unsafe to do if there is anything else in the loop that may read
486486 // or write to the aliased location. Check for any overlap by generating the
487487 // base pointer and checking the region.
488 unsigned AddrSpace = cast(DestPtr->getType())->getAddressSpace();
488 assert(DestPtr->getType()->isPointerTy()
489 && "Must be a pointer type.");
490 unsigned AddrSpace = DestPtr->getType()->getPointerAddressSpace();
489491 Value *BasePtr =
490492 Expander.expandCodeFor(Ev->getStart(), Builder.getInt8PtrTy(AddrSpace),
491493 Preheader->getTerminator());
504506
505507 // The # stored bytes is (BECount+1)*Size. Expand the trip count out to
506508 // pointer size if it isn't already.
507 Type *IntPtr = TD->getIntPtrType(DestPtr->getContext());
509 Type *IntPtr = TD->getIntPtrType(DestPtr->getType());
508510 BECount = SE->getTruncateOrZeroExtend(BECount, IntPtr);
509511
510512 const SCEV *NumBytesS = SE->getAddExpr(BECount, SE->getConstant(IntPtr, 1),
610612
611613 // The # stored bytes is (BECount+1)*Size. Expand the trip count out to
612614 // pointer size if it isn't already.
613 Type *IntPtr = TD->getIntPtrType(SI->getContext());
615 Type *IntPtr = TD->getIntPtrType(SI->getType());
614616 BECount = SE->getTruncateOrZeroExtend(BECount, IntPtr);
615617
616618 const SCEV *NumBytesS = SE->getAddExpr(BECount, SE->getConstant(IntPtr, 1),
962962 if (SV->getType()->isFloatingPointTy() || SV->getType()->isVectorTy())
963963 SV = Builder.CreateBitCast(SV, IntegerType::get(SV->getContext(),SrcWidth));
964964 else if (SV->getType()->isPointerTy())
965 SV = Builder.CreatePtrToInt(SV, TD.getIntPtrType(SV->getContext()));
965 SV = Builder.CreatePtrToInt(SV, TD.getIntPtrType(SV->getType()));
966966
967967 // Zero extend or truncate the value if needed.
968968 if (SV->getType() != AllocaType) {
164164 uint64_t Len = GetStringLength(Src);
165165 if (Len == 0) return 0;
166166
167 Value *LenV = ConstantInt::get(TD->getIntPtrType(*Context), Len);
167 Type *PT = FT->getParamType(0);
168 Value *LenV = ConstantInt::get(TD->getIntPtrType(PT), Len);
168169 Value *DstEnd = B.CreateGEP(Dst,
169 ConstantInt::get(TD->getIntPtrType(*Context),
170 ConstantInt::get(TD->getIntPtrType(PT),
170171 Len - 1));
171172
172173 // We have enough information to now generate the memcpy call to do the
219220 // Let strncpy handle the zero padding
220221 if (Len > SrcLen+1) return 0;
221222
223 Type *PT = FT->getParamType(0);
222224 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
223225 B.CreateMemCpy(Dst, Src,
224 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1);
226 ConstantInt::get(TD->getIntPtrType(PT), Len), 1);
225227
226228 return Dst;
227229 }
507509 if (!TD) return 0;
508510
509511 FunctionType *FT = Callee->getFunctionType();
512 Type *PT = FT->getParamType(0);
510513 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
511514 !FT->getParamType(0)->isPointerTy() ||
512515 !FT->getParamType(1)->isPointerTy() ||
513 FT->getParamType(2) != TD->getIntPtrType(*Context))
516 FT->getParamType(2) != TD->getIntPtrType(PT))
514517 return 0;
515518
516519 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
529532 if (!TD) return 0;
530533
531534 FunctionType *FT = Callee->getFunctionType();
535 Type *PT = FT->getParamType(0);
532536 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
533537 !FT->getParamType(0)->isPointerTy() ||
534538 !FT->getParamType(1)->isPointerTy() ||
535 FT->getParamType(2) != TD->getIntPtrType(*Context))
539 FT->getParamType(2) != TD->getIntPtrType(PT))
536540 return 0;
537541
538542 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
551555 if (!TD) return 0;
552556
553557 FunctionType *FT = Callee->getFunctionType();
558 Type *PT = FT->getParamType(0);
554559 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
555560 !FT->getParamType(0)->isPointerTy() ||
556561 !FT->getParamType(1)->isIntegerTy() ||
557 FT->getParamType(2) != TD->getIntPtrType(*Context))
562 FT->getParamType(2) != TD->getIntPtrType(PT))
558563 return 0;
559564
560565 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
979984 if (!TD) return 0;
980985
981986 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
987 Type *AT = CI->getArgOperand(0)->getType();
982988 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
983 ConstantInt::get(TD->getIntPtrType(*Context), // Copy the
989 ConstantInt::get(TD->getIntPtrType(AT), // Copy the
984990 FormatStr.size() + 1), 1); // nul byte.
985991 return ConstantInt::get(CI->getType(), FormatStr.size());
986992 }
11071113 uint64_t Len = GetStringLength(CI->getArgOperand(0));
11081114 if (!Len) return 0;
11091115 // Known to have no uses (see above).
1116 Type *PT = FT->getParamType(0);
11101117 return EmitFWrite(CI->getArgOperand(0),
1111 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
1118 ConstantInt::get(TD->getIntPtrType(PT), Len-1),
11121119 CI->getArgOperand(1), B, TD, TLI);
11131120 }
11141121 };
11331140 // These optimizations require DataLayout.
11341141 if (!TD) return 0;
11351142
1143 Type *AT = CI->getArgOperand(1)->getType();
11361144 Value *NewCI = EmitFWrite(CI->getArgOperand(1),
1137 ConstantInt::get(TD->getIntPtrType(*Context),
1145 ConstantInt::get(TD->getIntPtrType(AT),
11381146 FormatStr.size()),
11391147 CI->getArgOperand(0), B, TD, TLI);
11401148 return NewCI ? ConstantInt::get(CI->getType(), FormatStr.size()) : 0;
4545 AWI[1] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
4646 ArrayRef(AVs, 2));
4747
48 LLVMContext &Context = B.GetInsertBlock()->getContext();
4948 Constant *StrLen = M->getOrInsertFunction("strlen", AttrListPtr::get(AWI),
50 TD->getIntPtrType(Context),
49 TD->getIntPtrType(Ptr->getType()),
5150 B.getInt8PtrTy(),
5251 NULL);
5352 CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
7271 AWI[1] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
7372 ArrayRef(AVs, 2));
7473
75 LLVMContext &Context = B.GetInsertBlock()->getContext();
7674 Constant *StrNLen = M->getOrInsertFunction("strnlen", AttrListPtr::get(AWI),
77 TD->getIntPtrType(Context),
75 TD->getIntPtrType(Ptr->getType()),
7876 B.getInt8PtrTy(),
79 TD->getIntPtrType(Context),
77 TD->getIntPtrType(Ptr->getType()),
8078 NULL);
8179 CallInst *CI = B.CreateCall2(StrNLen, CastToCStr(Ptr, B), MaxLen, "strnlen");
8280 if (const Function *F = dyn_cast(StrNLen->stripPointerCasts()))
125123 AWI[2] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
126124 ArrayRef(AVs, 2));
127125
128 LLVMContext &Context = B.GetInsertBlock()->getContext();
129126 Value *StrNCmp = M->getOrInsertFunction("strncmp", AttrListPtr::get(AWI),
130127 B.getInt32Ty(),
131128 B.getInt8PtrTy(),
132129 B.getInt8PtrTy(),
133 TD->getIntPtrType(Context), NULL);
130 TD->getIntPtrType(Ptr1->getType()),
131 NULL);
134132 CallInst *CI = B.CreateCall3(StrNCmp, CastToCStr(Ptr1, B),
135133 CastToCStr(Ptr2, B), Len, "strncmp");
136134
200198 AttributeWithIndex AWI;
201199 AWI = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
202200 Attributes::NoUnwind);
203 LLVMContext &Context = B.GetInsertBlock()->getContext();
204201 Value *MemCpy = M->getOrInsertFunction("__memcpy_chk",
205202 AttrListPtr::get(AWI),
206203 B.getInt8PtrTy(),
207204 B.getInt8PtrTy(),
208205 B.getInt8PtrTy(),
209 TD->getIntPtrType(Context),
210 TD->getIntPtrType(Context), NULL);
206 TD->getIntPtrType(Dst->getType()),
207 TD->getIntPtrType(Src->getType()),
208 NULL);
211209 Dst = CastToCStr(Dst, B);
212210 Src = CastToCStr(Src, B);
213211 CallInst *CI = B.CreateCall4(MemCpy, Dst, Src, Len, ObjSize);
229227 Attributes::AttrVal AVs[2] = { Attributes::ReadOnly, Attributes::NoUnwind };
230228 AWI = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
231229 ArrayRef(AVs, 2));
232 LLVMContext &Context = B.GetInsertBlock()->getContext();
233230 Value *MemChr = M->getOrInsertFunction("memchr", AttrListPtr::get(AWI),
234231 B.getInt8PtrTy(),
235232 B.getInt8PtrTy(),
236233 B.getInt32Ty(),
237 TD->getIntPtrType(Context),
234 TD->getIntPtrType(Ptr->getType()),
238235 NULL);
239236 CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
240237
259256 AWI[2] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
260257 ArrayRef(AVs, 2));
261258
262 LLVMContext &Context = B.GetInsertBlock()->getContext();
263259 Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI),
264260 B.getInt32Ty(),
265261 B.getInt8PtrTy(),
266262 B.getInt8PtrTy(),
267 TD->getIntPtrType(Context), NULL);
263 TD->getIntPtrType(Ptr1->getType()),
264 NULL);
268265 CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
269266 Len, "memcmp");
270267
424421 AWI[1] = AttributeWithIndex::get(M->getContext(), 4, Attributes::NoCapture);
425422 AWI[2] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
426423 Attributes::NoUnwind);
427 LLVMContext &Context = B.GetInsertBlock()->getContext();
428424 StringRef FWriteName = TLI->getName(LibFunc::fwrite);
429425 Constant *F;
426 Type *PtrTy = Ptr->getType();
430427 if (File->getType()->isPointerTy())
431428 F = M->getOrInsertFunction(FWriteName, AttrListPtr::get(AWI),
432 TD->getIntPtrType(Context),
429 TD->getIntPtrType(PtrTy),
433430 B.getInt8PtrTy(),
434 TD->getIntPtrType(Context),
435 TD->getIntPtrType(Context),
431 TD->getIntPtrType(PtrTy),
432 TD->getIntPtrType(PtrTy),
436433 File->getType(), NULL);
437434 else
438 F = M->getOrInsertFunction(FWriteName, TD->getIntPtrType(Context),
435 F = M->getOrInsertFunction(FWriteName, TD->getIntPtrType(PtrTy),
439436 B.getInt8PtrTy(),
440 TD->getIntPtrType(Context),
441 TD->getIntPtrType(Context),
437 TD->getIntPtrType(PtrTy),
438 TD->getIntPtrType(PtrTy),
442439 File->getType(), NULL);
443440 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
444 ConstantInt::get(TD->getIntPtrType(Context), 1), File);
441 ConstantInt::get(TD->getIntPtrType(PtrTy), 1), File);
445442
446443 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
447444 CI->setCallingConv(Fn->getCallingConv());
463460 IRBuilder<> B(CI);
464461
465462 if (Name == "__memcpy_chk") {
463 Type *PT = FT->getParamType(0);
466464 // Check if this has the right signature.
467465 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
468466 !FT->getParamType(0)->isPointerTy() ||
469467 !FT->getParamType(1)->isPointerTy() ||
470 FT->getParamType(2) != TD->getIntPtrType(Context) ||
471 FT->getParamType(3) != TD->getIntPtrType(Context))
468 FT->getParamType(2) != TD->getIntPtrType(PT) ||
469 FT->getParamType(3) != TD->getIntPtrType(PT))
472470 return false;
473471
474472 if (isFoldable(3, 2, false)) {
487485
488486 if (Name == "__memmove_chk") {
489487 // Check if this has the right signature.
488 Type *PT = FT->getParamType(0);
490489 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
491490 !FT->getParamType(0)->isPointerTy() ||
492491 !FT->getParamType(1)->isPointerTy() ||
493 FT->getParamType(2) != TD->getIntPtrType(Context) ||
494 FT->getParamType(3) != TD->getIntPtrType(Context))
492 FT->getParamType(2) != TD->getIntPtrType(PT) ||
493 FT->getParamType(3) != TD->getIntPtrType(PT))
495494 return false;
496495
497496 if (isFoldable(3, 2, false)) {
505504
506505 if (Name == "__memset_chk") {
507506 // Check if this has the right signature.
507 Type *PT = FT->getParamType(0);
508508 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
509509 !FT->getParamType(0)->isPointerTy() ||
510510 !FT->getParamType(1)->isIntegerTy() ||
511 FT->getParamType(2) != TD->getIntPtrType(Context) ||
512 FT->getParamType(3) != TD->getIntPtrType(Context))
511 FT->getParamType(2) != TD->getIntPtrType(PT) ||
512 FT->getParamType(3) != TD->getIntPtrType(PT))
513513 return false;
514514
515515 if (isFoldable(3, 2, false)) {
524524
525525 if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") {
526526 // Check if this has the right signature.
527 Type *PT = FT->getParamType(0);
527528 if (FT->getNumParams() != 3 ||
528529 FT->getReturnType() != FT->getParamType(0) ||
529530 FT->getParamType(0) != FT->getParamType(1) ||
530531 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
531 FT->getParamType(2) != TD->getIntPtrType(Context))
532 FT->getParamType(2) != TD->getIntPtrType(PT))
532533 return 0;
533534
534535
550551
551552 if (Name == "__strncpy_chk" || Name == "__stpncpy_chk") {
552553 // Check if this has the right signature.
554 Type *PT = FT->getParamType(0);
553555 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
554556 FT->getParamType(0) != FT->getParamType(1) ||
555557 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
556558 !FT->getParamType(2)->isIntegerTy() ||
557 FT->getParamType(3) != TD->getIntPtrType(Context))
559 FT->getParamType(3) != TD->getIntPtrType(PT))
558560 return false;
559561
560562 if (isFoldable(3, 2, false)) {
391391
392392 // This is some kind of pointer constant. Turn it into a pointer-sized
393393 // ConstantInt if possible.
394 IntegerType *PtrTy = TD->getIntPtrType(V->getContext());
394 IntegerType *PtrTy = TD->getIntPtrType(V->getType());
395395
396396 // Null pointer means 0, see SelectionDAGBuilder::getValue(const Value*).
397397 if (isa(V))
531531 CV = ICI->getOperand(0);
532532
533533 // Unwrap any lossless ptrtoint cast.
534 if (TD && CV && CV->getType() == TD->getIntPtrType(CV->getContext()))
535 if (PtrToIntInst *PTII = dyn_cast(CV))
534 if (TD && CV) {
535 PtrToIntInst *PTII = NULL;
536 if ((PTII = dyn_cast(CV)) &&
537 CV->getType() == TD->getIntPtrType(CV->getContext(),
538 PTII->getPointerAddressSpace()))
536539 CV = PTII->getOperand(0);
540 }
537541 return CV;
538542 }
539543
980984 // Convert pointer to int before we switch.
981985 if (CV->getType()->isPointerTy()) {
982986 assert(TD && "Cannot switch on pointer without DataLayout");
983 CV = Builder.CreatePtrToInt(CV, TD->getIntPtrType(CV->getContext()),
987 CV = Builder.CreatePtrToInt(CV, TD->getIntPtrType(CV->getType()),
984988 "magicptr");
985989 }
986990
27082712 if (CompVal->getType()->isPointerTy()) {
27092713 assert(TD && "Cannot switch on pointer without DataLayout");
27102714 CompVal = Builder.CreatePtrToInt(CompVal,
2711 TD->getIntPtrType(CompVal->getContext()),
2715 TD->getIntPtrType(CompVal->getType()),
27122716 "magicptr");
27132717 }
27142718
101101 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
102102 this->CI = CI;
103103 FunctionType *FT = Callee->getFunctionType();
104 LLVMContext &Context = CI->getParent()->getContext();
105104
106105 // Check if this has the right signature.
107106 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
108107 !FT->getParamType(0)->isPointerTy() ||
109108 !FT->getParamType(1)->isPointerTy() ||
110 FT->getParamType(2) != TD->getIntPtrType(Context) ||
111 FT->getParamType(3) != TD->getIntPtrType(Context))
109 FT->getParamType(2) != TD->getIntPtrType(FT->getParamType(0)) ||
110 FT->getParamType(3) != TD->getIntPtrType(FT->getParamType(1)))
112111 return 0;
113112
114113 if (isFoldable(3, 2, false)) {
124123 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
125124 this->CI = CI;
126125 FunctionType *FT = Callee->getFunctionType();
127 LLVMContext &Context = CI->getParent()->getContext();
128126
129127 // Check if this has the right signature.
130128