llvm.org GIT mirror llvm / df98761
Revert r86077 because it caused crashes in 179.art and 175.vpr on ARM git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@86213 91177308-0d34-0410-b5e6-96231b3b80d8 Victor Hernandez 9 years ago
19 changed file(s) with 192 addition(s) and 245 deletion(s). Raw diff Collapse all Expand all
8080 ConstantInt *val_mem = ConstantInt::get(C, APInt(32, memtotal));
8181 BasicBlock* BB = builder->GetInsertBlock();
8282 const Type* IntPtrTy = IntegerType::getInt32Ty(C);
83 const Type* Int8Ty = IntegerType::getInt8Ty(C);
84 Constant* allocsize = ConstantExpr::getSizeOf(Int8Ty);
85 allocsize = ConstantExpr::getTruncOrBitCast(allocsize, IntPtrTy);
86 ptr_arr = CallInst::CreateMalloc(BB, IntPtrTy, Int8Ty, allocsize, val_mem,
87 NULL, "arr");
83 ptr_arr = CallInst::CreateMalloc(BB, IntPtrTy, IntegerType::getInt8Ty(C),
84 val_mem, NULL, "arr");
8885 BB->getInstList().push_back(cast(ptr_arr));
8986
9087 //call void @llvm.memset.i32(i8 *%arr, i8 0, i32 %d, i32 1)
4949 const TargetData* TD);
5050
5151 /// getMallocType - Returns the PointerType resulting from the malloc call.
52 /// The PointerType depends on the number of bitcast uses of the malloc call:
53 /// 0: PointerType is the malloc calls' return type.
54 /// 1: PointerType is the bitcast's result type.
55 /// >1: Unique PointerType cannot be determined, return NULL.
52 /// This PointerType is the result type of the call's only bitcast use.
53 /// If there is no unique bitcast use, then return NULL.
5654 const PointerType* getMallocType(const CallInst* CI);
5755
58 /// getMallocAllocatedType - Returns the Type allocated by malloc call.
59 /// The Type depends on the number of bitcast uses of the malloc call:
60 /// 0: PointerType is the malloc calls' return type.
61 /// 1: PointerType is the bitcast's result type.
62 /// >1: Unique PointerType cannot be determined, return NULL.
56 /// getMallocAllocatedType - Returns the Type allocated by malloc call. This
57 /// Type is the result type of the call's only bitcast use. If there is no
58 /// unique bitcast use, then return NULL.
6359 const Type* getMallocAllocatedType(const CallInst* CI);
6460
6561 /// getMallocArraySize - Returns the array size of a malloc call. If the
898898 /// 3. Bitcast the result of the malloc call to the specified type.
899899 static Instruction *CreateMalloc(Instruction *InsertBefore,
900900 const Type *IntPtrTy, const Type *AllocTy,
901 Value *AllocSize, Value *ArraySize = 0,
901 Value *ArraySize = 0,
902902 const Twine &Name = "");
903903 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
904904 const Type *IntPtrTy, const Type *AllocTy,
905 Value *AllocSize, Value *ArraySize = 0,
906 Function* MallocF = 0,
905 Value *ArraySize = 0, Function* MallocF = 0,
907906 const Twine &Name = "");
908907 /// CreateFree - Generate the IR for a call to the builtin free function.
909908 static void CreateFree(Value* Source, Instruction *InsertBefore);
1616 #include "llvm/Instructions.h"
1717 #include "llvm/Module.h"
1818 #include "llvm/Analysis/ConstantFolding.h"
19 #include "llvm/Target/TargetData.h"
2019 using namespace llvm;
2120
2221 //===----------------------------------------------------------------------===//
9695 if (!CI)
9796 return NULL;
9897
99 // The size of the malloc's result type must be known to determine array size.
98 // Type must be known to determine array size.
10099 const Type *T = getMallocAllocatedType(CI);
101 if (!T || !T->isSized() || !TD)
100 if (!T)
102101 return NULL;
103102
104103 Value *MallocArg = CI->getOperand(1);
105 const Type *ArgType = MallocArg->getType();
106104 ConstantExpr *CO = dyn_cast(MallocArg);
107105 BinaryOperator *BO = dyn_cast(MallocArg);
108106
109 unsigned ElementSizeInt = TD->getTypeAllocSize(T);
110 if (const StructType *ST = dyn_cast(T))
111 ElementSizeInt = TD->getStructLayout(ST)->getSizeInBytes();
112 Constant *ElementSize = ConstantInt::get(ArgType, ElementSizeInt);
107 Constant *ElementSize = ConstantExpr::getSizeOf(T);
108 ElementSize = ConstantExpr::getTruncOrBitCast(ElementSize,
109 MallocArg->getType());
110 Constant *FoldedElementSize =
111 ConstantFoldConstantExpression(cast(ElementSize), Context, TD);
113112
114113 // First, check if CI is a non-array malloc.
115 if (CO && CO == ElementSize)
114 if (CO && ((CO == ElementSize) ||
115 (FoldedElementSize && (CO == FoldedElementSize))))
116116 // Match CreateMalloc's use of constant 1 array-size for non-array mallocs.
117 return ConstantInt::get(ArgType, 1);
117 return ConstantInt::get(MallocArg->getType(), 1);
118118
119119 // Second, check if CI is an array malloc whose array size can be determined.
120 if (isConstantOne(ElementSize))
120 if (isConstantOne(ElementSize) ||
121 (FoldedElementSize && isConstantOne(FoldedElementSize)))
121122 return MallocArg;
122
123 if (ConstantInt *CInt = dyn_cast(MallocArg))
124 if (CInt->getZExtValue() % ElementSizeInt == 0)
125 return ConstantInt::get(ArgType, CInt->getZExtValue() / ElementSizeInt);
126123
127124 if (!CO && !BO)
128125 return NULL;
130127 Value *Op0 = NULL;
131128 Value *Op1 = NULL;
132129 unsigned Opcode = 0;
133 if (CO && ((CO->getOpcode() == Instruction::Mul) ||
130 if (CO && ((CO->getOpcode() == Instruction::Mul) ||
134131 (CO->getOpcode() == Instruction::Shl))) {
135132 Op0 = CO->getOperand(0);
136133 Op1 = CO->getOperand(1);
137134 Opcode = CO->getOpcode();
138135 }
139 if (BO && ((BO->getOpcode() == Instruction::Mul) ||
136 if (BO && ((BO->getOpcode() == Instruction::Mul) ||
140137 (BO->getOpcode() == Instruction::Shl))) {
141138 Op0 = BO->getOperand(0);
142139 Op1 = BO->getOperand(1);
146143 // Determine array size if malloc's argument is the product of a mul or shl.
147144 if (Op0) {
148145 if (Opcode == Instruction::Mul) {
149 if (Op1 == ElementSize)
146 if ((Op1 == ElementSize) ||
147 (FoldedElementSize && (Op1 == FoldedElementSize)))
150148 // ArraySize * ElementSize
151149 return Op0;
152 if (Op0 == ElementSize)
150 if ((Op0 == ElementSize) ||
151 (FoldedElementSize && (Op0 == FoldedElementSize)))
153152 // ElementSize * ArraySize
154153 return Op1;
155154 }
161160 uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1);
162161 Value *Op1Pow = ConstantInt::get(Context,
163162 APInt(Op1Int.getBitWidth(), 0).set(BitToSet));
164 if (Op0 == ElementSize)
163 if (Op0 == ElementSize || (FoldedElementSize && Op0 == FoldedElementSize))
165164 // ArraySize << log2(ElementSize)
166165 return Op1Pow;
167 if (Op1Pow == ElementSize)
166 if (Op1Pow == ElementSize ||
167 (FoldedElementSize && Op1Pow == FoldedElementSize))
168168 // ElementSize << log2(ArraySize)
169169 return Op0;
170170 }
204204 }
205205
206206 /// getMallocType - Returns the PointerType resulting from the malloc call.
207 /// The PointerType depends on the number of bitcast uses of the malloc call:
208 /// 0: PointerType is the calls' return type.
209 /// 1: PointerType is the bitcast's result type.
210 /// >1: Unique PointerType cannot be determined, return NULL.
207 /// This PointerType is the result type of the call's only bitcast use.
208 /// If there is no unique bitcast use, then return NULL.
211209 const PointerType *llvm::getMallocType(const CallInst *CI) {
212210 assert(isMalloc(CI) && "GetMallocType and not malloc call");
213211
214 const PointerType *MallocType = NULL;
215 unsigned NumOfBitCastUses = 0;
216
212 const BitCastInst *BCI = NULL;
213
217214 // Determine if CallInst has a bitcast use.
218215 for (Value::use_const_iterator UI = CI->use_begin(), E = CI->use_end();
219216 UI != E; )
220 if (const BitCastInst *BCI = dyn_cast(*UI++)) {
221 MallocType = cast(BCI->getDestTy());
222 NumOfBitCastUses++;
223 }
224
225 // Malloc call has 1 bitcast use, so type is the bitcast's destination type.
226 if (NumOfBitCastUses == 1)
227 return MallocType;
217 if ((BCI = dyn_cast(cast(*UI++))))
218 break;
219
220 // Malloc call has 1 bitcast use and no other uses, so type is the bitcast's
221 // destination type.
222 if (BCI && CI->hasOneUse())
223 return cast(BCI->getDestTy());
228224
229225 // Malloc call was not bitcast, so type is the malloc function's return type.
230 if (NumOfBitCastUses == 0)
226 if (!BCI)
231227 return cast(CI->getType());
232228
233229 // Type could not be determined.
234230 return NULL;
235231 }
236232
237 /// getMallocAllocatedType - Returns the Type allocated by malloc call.
238 /// The Type depends on the number of bitcast uses of the malloc call:
239 /// 0: PointerType is the malloc calls' return type.
240 /// 1: PointerType is the bitcast's result type.
241 /// >1: Unique PointerType cannot be determined, return NULL.
233 /// getMallocAllocatedType - Returns the Type allocated by malloc call. This
234 /// Type is the result type of the call's only bitcast use. If there is no
235 /// unique bitcast use, then return NULL.
242236 const Type *llvm::getMallocAllocatedType(const CallInst *CI) {
243237 const PointerType *PT = getMallocType(CI);
244238 return PT ? PT->getElementType() : NULL;
36183618 // Autoupgrade old malloc instruction to malloc call.
36193619 // FIXME: Remove in LLVM 3.0.
36203620 const Type *IntPtrTy = Type::getInt32Ty(Context);
3621 Constant *AllocSize = ConstantExpr::getSizeOf(Ty);
3622 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, IntPtrTy);
36233621 if (!MallocF)
36243622 // Prototype malloc as "void *(int32)".
36253623 // This function is renamed as "malloc" in ValidateEndOfModule().
36263624 MallocF = cast(
36273625 M->getOrInsertFunction("", Type::getInt8PtrTy(Context), IntPtrTy, NULL));
3628 Inst = CallInst::CreateMalloc(BB, IntPtrTy, Ty, AllocSize, Size, MallocF);
3626 Inst = CallInst::CreateMalloc(BB, IntPtrTy, Ty, Size, MallocF);
36293627 return false;
36303628 }
36313629
21002100 if (!Ty || !Size) return Error("Invalid MALLOC record");
21012101 if (!CurBB) return Error("Invalid malloc instruction with no BB");
21022102 const Type *Int32Ty = IntegerType::getInt32Ty(CurBB->getContext());
2103 Constant *AllocSize = ConstantExpr::getSizeOf(Ty->getElementType());
2104 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, Int32Ty);
21052103 I = CallInst::CreateMalloc(CurBB, Int32Ty, Ty->getElementType(),
2106 AllocSize, Size, NULL);
2104 Size, NULL);
21072105 InstructionList.push_back(I);
21082106 break;
21092107 }
821821 /// malloc into a global, and any loads of GV as uses of the new global.
822822 static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV,
823823 CallInst *CI,
824 const Type *AllocTy,
824 BitCastInst *BCI,
825825 Value* NElems,
826826 LLVMContext &Context,
827827 TargetData* TD) {
828 DEBUG(errs() << "PROMOTING GLOBAL: " << *GV << " CALL = " << *CI << '\n');
828 DEBUG(errs() << "PROMOTING MALLOC GLOBAL: " << *GV
829 << " CALL = " << *CI << " BCI = " << *BCI << '\n');
829830
830831 const Type *IntPtrTy = TD->getIntPtrType(Context);
831832
832 // CI has either 0 or 1 bitcast uses (getMallocType() would otherwise have
833 // returned NULL and we would not be here).
834 BitCastInst *BCI = NULL;
835 for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end(); UI != E; )
836 if ((BCI = dyn_cast(cast(*UI++))))
837 break;
838
839833 ConstantInt *NElements = cast(NElems);
840834 if (NElements->getZExtValue() != 1) {
841835 // If we have an array allocation, transform it to a single element
842836 // allocation to make the code below simpler.
843 Type *NewTy = ArrayType::get(AllocTy, NElements->getZExtValue());
844 unsigned TypeSize = TD->getTypeAllocSize(NewTy);
845 if (const StructType *ST = dyn_cast(NewTy))
846 TypeSize = TD->getStructLayout(ST)->getSizeInBytes();
847 Instruction *NewCI = CallInst::CreateMalloc(CI, IntPtrTy, NewTy,
848 ConstantInt::get(IntPtrTy, TypeSize));
837 Type *NewTy = ArrayType::get(getMallocAllocatedType(CI),
838 NElements->getZExtValue());
839 Value* NewM = CallInst::CreateMalloc(CI, IntPtrTy, NewTy);
840 Instruction* NewMI = cast(NewM);
849841 Value* Indices[2];
850842 Indices[0] = Indices[1] = Constant::getNullValue(IntPtrTy);
851 Value *NewGEP = GetElementPtrInst::Create(NewCI, Indices, Indices + 2,
852 NewCI->getName()+".el0", CI);
853 Value *Cast = new BitCastInst(NewGEP, CI->getType(), "el0", CI);
854 if (BCI) BCI->replaceAllUsesWith(NewGEP);
855 CI->replaceAllUsesWith(Cast);
856 if (BCI) BCI->eraseFromParent();
843 Value *NewGEP = GetElementPtrInst::Create(NewMI, Indices, Indices + 2,
844 NewMI->getName()+".el0", CI);
845 BCI->replaceAllUsesWith(NewGEP);
846 BCI->eraseFromParent();
857847 CI->eraseFromParent();
858 BCI = dyn_cast(NewCI);
859 CI = BCI ? extractMallocCallFromBitCast(BCI) : cast(NewCI);
848 BCI = cast(NewMI);
849 CI = extractMallocCallFromBitCast(NewMI);
860850 }
861851
862852 // Create the new global variable. The contents of the malloc'd memory is
870860 GV,
871861 GV->isThreadLocal());
872862
873 // Anything that used the malloc or its bitcast now uses the global directly.
874 if (BCI) BCI->replaceAllUsesWith(NewGV);
875 CI->replaceAllUsesWith(new BitCastInst(NewGV, CI->getType(), "newgv", CI));
863 // Anything that used the malloc now uses the global directly.
864 BCI->replaceAllUsesWith(NewGV);
876865
877866 Constant *RepValue = NewGV;
878867 if (NewGV->getType() != GV->getType()->getElementType())
940929 GV->getParent()->getGlobalList().insert(GV, InitBool);
941930
942931
943 // Now the GV is dead, nuke it and the malloc (both CI and BCI).
932 // Now the GV is dead, nuke it and the malloc.
944933 GV->eraseFromParent();
945 if (BCI) BCI->eraseFromParent();
934 BCI->eraseFromParent();
946935 CI->eraseFromParent();
947936
948937 // To further other optimizations, loop over all users of NewGV and try to
12831272
12841273 /// PerformHeapAllocSRoA - CI is an allocation of an array of structures. Break
12851274 /// it up into multiple allocations of arrays of the fields.
1286 static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV, CallInst *CI,
1287 Value* NElems, LLVMContext &Context,
1275 static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV,
1276 CallInst *CI, BitCastInst* BCI,
1277 Value* NElems,
1278 LLVMContext &Context,
12881279 TargetData *TD) {
1289 DEBUG(errs() << "SROA HEAP ALLOC: " << *GV << " MALLOC = " << *CI << '\n');
1280 DEBUG(errs() << "SROA HEAP ALLOC: " << *GV << " MALLOC CALL = " << *CI
1281 << " BITCAST = " << *BCI << '\n');
12901282 const Type* MAT = getMallocAllocatedType(CI);
12911283 const StructType *STy = cast(MAT);
12921284
12941286 // it into GV). If there are other uses, change them to be uses of
12951287 // the global to simplify later code. This also deletes the store
12961288 // into GV.
1297 ReplaceUsesOfMallocWithGlobal(CI, GV);
1298
1289 ReplaceUsesOfMallocWithGlobal(BCI, GV);
1290
12991291 // Okay, at this point, there are no users of the malloc. Insert N
13001292 // new mallocs at the same place as CI, and N globals.
13011293 std::vector FieldGlobals;
13131305 GV->isThreadLocal());
13141306 FieldGlobals.push_back(NGV);
13151307
1316 unsigned TypeSize = TD->getTypeAllocSize(FieldTy);
1317 if (const StructType* ST = dyn_cast(FieldTy))
1318 TypeSize = TD->getStructLayout(ST)->getSizeInBytes();
1319 const Type* IntPtrTy = TD->getIntPtrType(Context);
1320 Value *NMI = CallInst::CreateMalloc(CI, IntPtrTy, FieldTy,
1321 ConstantInt::get(IntPtrTy, TypeSize),
1322 NElems,
1323 CI->getName() + ".f" + Twine(FieldNo));
1308 Value *NMI = CallInst::CreateMalloc(CI, TD->getIntPtrType(Context),
1309 FieldTy, NElems,
1310 BCI->getName() + ".f" + Twine(FieldNo));
13241311 FieldMallocs.push_back(NMI);
1325 new StoreInst(NMI, NGV, CI);
1312 new StoreInst(NMI, NGV, BCI);
13261313 }
13271314
13281315 // The tricky aspect of this transformation is handling the case when malloc
13391326 // }
13401327 Value *RunningOr = 0;
13411328 for (unsigned i = 0, e = FieldMallocs.size(); i != e; ++i) {
1342 Value *Cond = new ICmpInst(CI, ICmpInst::ICMP_EQ, FieldMallocs[i],
1343 Constant::getNullValue(FieldMallocs[i]->getType()),
1344 "isnull");
1329 Value *Cond = new ICmpInst(BCI, ICmpInst::ICMP_EQ, FieldMallocs[i],
1330 Constant::getNullValue(FieldMallocs[i]->getType()),
1331 "isnull");
13451332 if (!RunningOr)
13461333 RunningOr = Cond; // First seteq
13471334 else
1348 RunningOr = BinaryOperator::CreateOr(RunningOr, Cond, "tmp", CI);
1335 RunningOr = BinaryOperator::CreateOr(RunningOr, Cond, "tmp", BCI);
13491336 }
13501337
13511338 // Split the basic block at the old malloc.
1352 BasicBlock *OrigBB = CI->getParent();
1353 BasicBlock *ContBB = OrigBB->splitBasicBlock(CI, "malloc_cont");
1339 BasicBlock *OrigBB = BCI->getParent();
1340 BasicBlock *ContBB = OrigBB->splitBasicBlock(BCI, "malloc_cont");
13541341
13551342 // Create the block to check the first condition. Put all these blocks at the
13561343 // end of the function as they are unlikely to be executed.
13861373 }
13871374
13881375 BranchInst::Create(ContBB, NullPtrBlock);
1389
1390 // CI is no longer needed, remove it.
1376
1377 // CI and BCI are no longer needed, remove them.
1378 BCI->eraseFromParent();
13911379 CI->eraseFromParent();
13921380
13931381 /// InsertedScalarizedLoads - As we process loads, if we can't immediately
14741462 /// cast of malloc.
14751463 static bool TryToOptimizeStoreOfMallocToGlobal(GlobalVariable *GV,
14761464 CallInst *CI,
1477 const Type *AllocTy,
1465 BitCastInst *BCI,
14781466 Module::global_iterator &GVI,
14791467 TargetData *TD,
14801468 LLVMContext &Context) {
1469 // If we can't figure out the type being malloced, then we can't optimize.
1470 const Type *AllocTy = getMallocAllocatedType(CI);
1471 assert(AllocTy);
1472
14811473 // If this is a malloc of an abstract type, don't touch it.
14821474 if (!AllocTy->isSized())
14831475 return false;
14981490 // for.
14991491 {
15001492 SmallPtrSet PHIs;
1501 if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(CI, GV, PHIs))
1493 if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(BCI, GV, PHIs))
15021494 return false;
15031495 }
15041496
15061498 // transform the program to use global memory instead of malloc'd memory.
15071499 // This eliminates dynamic allocation, avoids an indirection accessing the
15081500 // data, and exposes the resultant global to further GlobalOpt.
1501 Value *NElems = getMallocArraySize(CI, Context, TD);
15091502 // We cannot optimize the malloc if we cannot determine malloc array size.
1510 if (Value *NElems = getMallocArraySize(CI, Context, TD)) {
1503 if (NElems) {
15111504 if (ConstantInt *NElements = dyn_cast(NElems))
15121505 // Restrict this transformation to only working on small allocations
15131506 // (2048 bytes currently), as we don't want to introduce a 16M global or
15141507 // something.
15151508 if (TD &&
15161509 NElements->getZExtValue() * TD->getTypeAllocSize(AllocTy) < 2048) {
1517 GVI = OptimizeGlobalAddressOfMalloc(GV, CI, AllocTy, NElems,
1518 Context, TD);
1510 GVI = OptimizeGlobalAddressOfMalloc(GV, CI, BCI, NElems, Context, TD);
15191511 return true;
15201512 }
15211513
15331525 // This the structure has an unreasonable number of fields, leave it
15341526 // alone.
15351527 if (AllocSTy->getNumElements() <= 16 && AllocSTy->getNumElements() != 0 &&
1536 AllGlobalLoadUsesSimpleEnoughForHeapSRA(GV, CI)) {
1528 AllGlobalLoadUsesSimpleEnoughForHeapSRA(GV, BCI)) {
15371529
15381530 // If this is a fixed size array, transform the Malloc to be an alloc of
15391531 // structs. malloc [100 x struct],1 -> malloc struct, 100
15401532 if (const ArrayType *AT =
15411533 dyn_cast(getMallocAllocatedType(CI))) {
1542 const Type *IntPtrTy = TD->getIntPtrType(Context);
1543 unsigned TypeSize = TD->getStructLayout(AllocSTy)->getSizeInBytes();
1544 Value *AllocSize = ConstantInt::get(IntPtrTy, TypeSize);
1545 Value *NumElements = ConstantInt::get(IntPtrTy, AT->getNumElements());
1546 Instruction *Malloc = CallInst::CreateMalloc(CI, IntPtrTy, AllocSTy,
1547 AllocSize, NumElements,
1548 CI->getName());
1549 Instruction *Cast = new BitCastInst(Malloc, CI->getType(), "tmp", CI);
1550 CI->replaceAllUsesWith(Cast);
1534 Value* NumElements = ConstantInt::get(Type::getInt32Ty(Context),
1535 AT->getNumElements());
1536 Value* NewMI = CallInst::CreateMalloc(CI, TD->getIntPtrType(Context),
1537 AllocSTy, NumElements,
1538 BCI->getName());
1539 Value *Cast = new BitCastInst(NewMI, getMallocType(CI), "tmp", CI);
1540 BCI->replaceAllUsesWith(Cast);
1541 BCI->eraseFromParent();
15511542 CI->eraseFromParent();
1552 CI = dyn_cast(Malloc) ?
1553 extractMallocCallFromBitCast(Malloc):
1554 cast(Malloc);
1543 BCI = cast(NewMI);
1544 CI = extractMallocCallFromBitCast(NewMI);
15551545 }
15561546
1557 GVI = PerformHeapAllocSRoA(GV, CI, getMallocArraySize(CI, Context, TD),
1558 Context, TD);
1547 GVI = PerformHeapAllocSRoA(GV, CI, BCI, NElems, Context, TD);
15591548 return true;
15601549 }
15611550 }
15871576 if (OptimizeAwayTrappingUsesOfLoads(GV, SOVC, Context))
15881577 return true;
15891578 } else if (CallInst *CI = extractMallocCall(StoredOnceVal)) {
1590 const Type* MallocType = getMallocAllocatedType(CI);
1591 if (MallocType && TryToOptimizeStoreOfMallocToGlobal(GV, CI, MallocType,
1592 GVI, TD, Context))
1593 return true;
1579 if (getMallocAllocatedType(CI)) {
1580 BitCastInst* BCI = NULL;
1581 for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end();
1582 UI != E; )
1583 BCI = dyn_cast(cast(*UI++));
1584 if (BCI &&
1585 TryToOptimizeStoreOfMallocToGlobal(GV, CI, BCI, GVI, TD, Context))
1586 return true;
1587 }
15941588 }
15951589 }
15961590
16981698
16991699 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
17001700 const char *Name) {
1701 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1702 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1703 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1704 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1705 ITy, unwrap(Ty), AllocSize,
1706 0, 0, "");
1707 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1701 const Type* IntPtrT = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1702 return wrap(unwrap(B)->Insert(CallInst::CreateMalloc(
1703 unwrap(B)->GetInsertBlock(), IntPtrT, unwrap(Ty), 0, 0, ""),
1704 Twine(Name)));
17081705 }
17091706
17101707 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
17111708 LLVMValueRef Val, const char *Name) {
1712 const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1713 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1714 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1715 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
1716 ITy, unwrap(Ty), AllocSize,
1717 unwrap(Val), 0, "");
1718 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1709 const Type* IntPtrT = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1710 return wrap(unwrap(B)->Insert(CallInst::CreateMalloc(
1711 unwrap(B)->GetInsertBlock(), IntPtrT, unwrap(Ty), unwrap(Val), 0, ""),
1712 Twine(Name)));
17191713 }
17201714
17211715 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2323 #include "llvm/Support/CallSite.h"
2424 #include "llvm/Support/ConstantRange.h"
2525 #include "llvm/Support/MathExtras.h"
26 #include "llvm/Target/TargetData.h"
2726
2827 using namespace llvm;
2928
448447 return isa(val) && cast(val)->isOne();
449448 }
450449
450 static Value *checkArraySize(Value *Amt, const Type *IntPtrTy) {
451 if (!Amt)
452 Amt = ConstantInt::get(IntPtrTy, 1);
453 else {
454 assert(!isa(Amt) &&
455 "Passed basic block into malloc size parameter! Use other ctor");
456 assert(Amt->getType() == IntPtrTy &&
457 "Malloc array size is not an intptr!");
458 }
459 return Amt;
460 }
461
451462 static Instruction *createMalloc(Instruction *InsertBefore,
452463 BasicBlock *InsertAtEnd, const Type *IntPtrTy,
453 const Type *AllocTy, Value *AllocSize,
454 Value *ArraySize, Function *MallocF,
455 const Twine &Name) {
464 const Type *AllocTy, Value *ArraySize,
465 Function *MallocF, const Twine &NameStr) {
456466 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
457467 "createMalloc needs either InsertBefore or InsertAtEnd");
458468
460470 // bitcast (i8* malloc(typeSize)) to type*
461471 // malloc(type, arraySize) becomes:
462472 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
463 if (!ArraySize)
464 ArraySize = ConstantInt::get(IntPtrTy, 1);
465 else if (ArraySize->getType() != IntPtrTy) {
466 if (InsertBefore)
467 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, "", InsertBefore);
468 else
469 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, "", InsertAtEnd);
470 }
473 Value *AllocSize = ConstantExpr::getSizeOf(AllocTy);
474 AllocSize = ConstantExpr::getTruncOrBitCast(cast(AllocSize),
475 IntPtrTy);
476 ArraySize = checkArraySize(ArraySize, IntPtrTy);
471477
472478 if (!IsConstantOne(ArraySize)) {
473479 if (IsConstantOne(AllocSize)) {
506512 Result = MCall;
507513 if (Result->getType() != AllocPtrType)
508514 // Create a cast instruction to convert to the right type...
509 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
515 Result = new BitCastInst(MCall, AllocPtrType, NameStr, InsertBefore);
510516 } else {
511517 MCall = CallInst::Create(MallocF, AllocSize, "malloccall");
512518 Result = MCall;
513519 if (Result->getType() != AllocPtrType) {
514520 InsertAtEnd->getInstList().push_back(MCall);
515521 // Create a cast instruction to convert to the right type...
516 Result = new BitCastInst(MCall, AllocPtrType, Name);
522 Result = new BitCastInst(MCall, AllocPtrType, NameStr);
517523 }
518524 }
519525 MCall->setTailCall();
531537 /// 3. Bitcast the result of the malloc call to the specified type.
532538 Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
533539 const Type *IntPtrTy, const Type *AllocTy,
534 Value *AllocSize, Value *ArraySize,
535 const Twine &Name) {
536 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
540 Value *ArraySize, const Twine &Name) {
541 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy,
537542 ArraySize, NULL, Name);
538543 }
539544
547552 /// responsibility of the caller.
548553 Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
549554 const Type *IntPtrTy, const Type *AllocTy,
550 Value *AllocSize, Value *ArraySize,
551 Function *MallocF, const Twine &Name) {
552 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
555 Value *ArraySize, Function* MallocF,
556 const Twine &Name) {
557 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy,
553558 ArraySize, MallocF, Name);
554559 }
555560
3030 }
3131
3232 declare i32 @bar(i8*)
33 declare i32 @bar2(i64*)
3433
3534 define i32 @foo1(i32 %n) nounwind {
3635 entry:
6059 ret i32 %add16
6160 }
6261
63 define i32 @foo2(i64 %n) nounwind {
62 define i32 @foo2(i32 %n) nounwind {
6463 entry:
65 %call = tail call i8* @malloc(i64 %n) ; [#uses=1]
64 %call = malloc i8, i32 %n ; [#uses=1]
6665 ; CHECK: %call =
6766 ; CHECK: ==> %n elements, %n bytes allocated
68 %mallocsize = mul i64 %n, 8 ; [#uses=1]
69 %malloccall = tail call i8* @malloc(i64 %mallocsize) ; [#uses=1]
70 %call3 = bitcast i8* %malloccall to i64* ; [#uses=1]
71 ; CHECK: %malloccall =
72 ; CHECK: ==> (8 * %n) elements, (8 * %n) bytes allocated
7367 %call2 = tail call i8* @calloc(i64 2, i64 4) nounwind ; [#uses=1]
7468 ; CHECK: %call2 =
7569 ; CHECK: ==> 8 elements, 8 bytes allocated
7771 ; CHECK: %call4 =
7872 ; CHECK: ==> 16 elements, 16 bytes allocated
7973 %call6 = tail call i32 @bar(i8* %call) nounwind ; [#uses=1]
80 %call7 = tail call i32 @bar2(i64* %call3) nounwind ; [#uses=1]
8174 %call8 = tail call i32 @bar(i8* %call2) nounwind ; [#uses=1]
8275 %call10 = tail call i32 @bar(i8* %call4) nounwind ; [#uses=1]
83 %add = add i32 %call8, %call6 ; [#uses=1]
84 %add10 = add i32 %add, %call7 ; [#uses=1]
85 %add11 = add i32 %add10, %call10 ; [#uses=1]
76 %add = add i32 %call8, %call6 ; [#uses=1]
77 %add11 = add i32 %add, %call10 ; [#uses=1]
8678 ret i32 %add11
8779 }
88
89 declare noalias i8* @malloc(i64) nounwind
9080
9181 declare noalias i8* @calloc(i64, i64) nounwind
9282
0 ; RUN: opt < %s -globalopt
1 target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
21
32 %struct.s_annealing_sched = type { i32, float, float, float, float }
43 %struct.s_bb = type { i32, i32, i32, i32 }
9695 unreachable
9796
9897 bb1.i38: ; preds = %bb
99 %mallocsize = mul i64 28, undef ; [#uses=1]
100 %malloccall = tail call i8* @malloc(i64 %mallocsize) ; [#uses=1]
101 %0 = bitcast i8* %malloccall to %struct.s_net* ; <%struct.s_net*> [#uses=1]
98 %0 = malloc %struct.s_net, i32 undef ; <%struct.s_net*> [#uses=1]
10299 br i1 undef, label %bb.i1.i39, label %my_malloc.exit2.i
103100
104101 bb.i1.i39: ; preds = %bb1.i38
117114 bb7: ; preds = %bb6.preheader
118115 unreachable
119116 }
120
121 declare noalias i8* @malloc(i64)
None ; RUN: opt < %s -globalopt -S | FileCheck %s
1 target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
0 ; RUN: opt < %s -globalopt -S | grep {@X.f0}
1 ; RUN: opt < %s -globalopt -S | grep {@X.f1}
2 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
3 target triple = "i386-apple-darwin7"
24
35 %struct.foo = type { i32, i32 }
46 @X = internal global %struct.foo* null
5 ; CHECK: @X.f0
6 ; CHECK: @X.f1
77
8 define void @bar(i64 %Size) nounwind noinline {
8 define void @bar(i32 %Size) nounwind noinline {
99 entry:
10 %mallocsize = mul i64 %Size, 8 ; [#uses=1]
11 %malloccall = tail call i8* @malloc(i64 %mallocsize) ; [#uses=1]
12 %.sub = bitcast i8* %malloccall to %struct.foo* ; <%struct.foo*> [#uses=1]
10 %.sub = malloc %struct.foo, i32 %Size
1311 store %struct.foo* %.sub, %struct.foo** @X, align 4
1412 ret void
1513 }
16
17 declare noalias i8* @malloc(i64)
1814
1915 define i32 @baz() nounwind readonly noinline {
2016 bb1.thread:
None ; RUN: opt < %s -globalopt -S | FileCheck %s
1 target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
0 ; RUN: opt < %s -globalopt -S | grep {@X.f0}
1 ; RUN: opt < %s -globalopt -S | grep {@X.f1}
22
3 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
4 target triple = "i386-apple-darwin7"
35 %struct.foo = type { i32, i32 }
46 @X = internal global %struct.foo* null ; <%struct.foo**> [#uses=2]
5 ; CHECK: @X.f0
6 ; CHECK: @X.f1
77
88 define void @bar(i32 %Size) nounwind noinline {
99 entry:
10 %malloccall = tail call i8* @malloc(i64 8000000) ; [#uses=1]
11 %0 = bitcast i8* %malloccall to [1000000 x %struct.foo]* ; <[1000000 x %struct.foo]*> [#uses=1]
10 %0 = malloc [1000000 x %struct.foo]
11 ;%.sub = bitcast [1000000 x %struct.foo]* %0 to %struct.foo*
1212 %.sub = getelementptr [1000000 x %struct.foo]* %0, i32 0, i32 0 ; <%struct.foo*> [#uses=1]
1313 store %struct.foo* %.sub, %struct.foo** @X, align 4
1414 ret void
1515 }
16
17 declare noalias i8* @malloc(i64)
1816
1917 define i32 @baz() nounwind readonly noinline {
2018 bb1.thread:
0 ; RUN: opt < %s -globalopt -S | FileCheck %s
1 target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
1
2 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
3 target triple = "i386-apple-darwin10"
24
35 %struct.foo = type { i32, i32 }
46 @X = internal global %struct.foo* null
57 ; CHECK: @X.f0
68 ; CHECK: @X.f1
79
8 define void @bar(i64 %Size) nounwind noinline {
10 define void @bar(i32 %Size) nounwind noinline {
911 entry:
10 %mallocsize = mul i64 8, %Size, ; [#uses=1]
11 ; CHECK: mul i64 %Size, 4
12 %malloccall = tail call i8* @malloc(i64 %mallocsize) ; > [#uses=1]
12 %mallocsize = mul i32 ptrtoint (%struct.foo* getelementptr (%struct.foo* null, i32 1) to i32), %Size, ; > [#uses=1]
13 ; CHECK: mul i32 %Size
14 %malloccall = tail call i8* @malloc(i32 %mallocsize) ; [#uses=1]
1315 %.sub = bitcast i8* %malloccall to %struct.foo* ; <%struct.foo*> [#uses=1]
1416 store %struct.foo* %.sub, %struct.foo** @X, align 4
1517 ret void
1618 }
1719
18 declare noalias i8* @malloc(i64)
20 declare noalias i8* @malloc(i32)
1921
2022 define i32 @baz() nounwind readonly noinline {
2123 bb1.thread:
0 ; RUN: opt < %s -globalopt -S | FileCheck %s
1 target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
1
2 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
3 target triple = "i386-apple-darwin7"
24
35 %struct.foo = type { i32, i32 }
46 @X = internal global %struct.foo* null
57 ; CHECK: @X.f0
68 ; CHECK: @X.f1
79
8 define void @bar(i64 %Size) nounwind noinline {
10 define void @bar(i32 %Size) nounwind noinline {
911 entry:
10 %mallocsize = shl i64 %Size, 3 ; [#uses=1]
11 %malloccall = tail call i8* @malloc(i64 %mallocsize) ; [#uses=1]
12 ; CHECK: mul i64 %Size, 4
12 %mallocsize = shl i32 ptrtoint (%struct.foo* getelementptr (%struct.foo* null, i32 1) to i32), 9, ; [#uses=1]
13 %malloccall = tail call i8* @malloc(i32 %mallocsize) ; [#uses=1]
14 ; CHECK: @malloc(i32 mul (i32 512
1315 %.sub = bitcast i8* %malloccall to %struct.foo* ; <%struct.foo*> [#uses=1]
1416 store %struct.foo* %.sub, %struct.foo** @X, align 4
1517 ret void
1618 }
1719
18 declare noalias i8* @malloc(i64)
20 declare noalias i8* @malloc(i32)
1921
2022 define i32 @baz() nounwind readonly noinline {
2123 bb1.thread:
0 ; RUN: opt < %s -globalopt -S | grep {tmp.f1 = phi i32. }
11 ; RUN: opt < %s -globalopt -S | grep {tmp.f0 = phi i32. }
2 target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
32
3 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
4 target triple = "i386-apple-darwin7"
45 %struct.foo = type { i32, i32 }
56 @X = internal global %struct.foo* null ; <%struct.foo**> [#uses=2]
67
78 define void @bar(i32 %Size) nounwind noinline {
89 entry:
9 %malloccall = tail call i8* @malloc(i64 8000000) ; [#uses=1]
10 %tmp = bitcast i8* %malloccall to [1000000 x %struct.foo]* ; <[1000000 x %struct.foo]*> [#uses=1]
10 %tmp = malloc [1000000 x %struct.foo] ; <[1000000 x %struct.foo]*> [#uses=1]
1111 %.sub = getelementptr [1000000 x %struct.foo]* %tmp, i32 0, i32 0 ; <%struct.foo*> [#uses=1]
1212 store %struct.foo* %.sub, %struct.foo** @X, align 4
1313 ret void
1414 }
15
16 declare noalias i8* @malloc(i64)
1715
1816 define i32 @baz() nounwind readonly noinline {
1917 bb1.thread:
None ; RUN: opt < %s -globalopt -S | FileCheck %s
0 ; RUN: opt < %s -globalopt -S | not grep global
11 target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
22
33 @G = internal global i32* null ; [#uses=3]
4 ; CHECK-NOT: global
54
65 define void @init() {
7 %malloccall = tail call i8* @malloc(i64 4) ; [#uses=1]
8 %P = bitcast i8* %malloccall to i32* ; [#uses=1]
6 %P = malloc i32 ; [#uses=1]
97 store i32* %P, i32** @G
108 %GV = load i32** @G ; [#uses=1]
119 store i32 0, i32* %GV
1210 ret void
1311 }
1412
15 declare noalias i8* @malloc(i64)
16
1713 define i32 @get() {
1814 %GV = load i32** @G ; [#uses=1]
1915 %V = load i32* %GV ; [#uses=1]
2016 ret i32 %V
21 ; CHECK: ret i32 0
2217 }
2318
0 ; RUN: opt < %s -globalopt -globaldce -S | not grep malloc
1 target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
1 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
2 target triple = "i686-apple-darwin8"
23
34 @G = internal global i32* null ; [#uses=3]
45
56 define void @init() {
6 %malloccall = tail call i8* @malloc(i64 mul (i64 100, i64 4)) ; [#uses=1]
7 %P = bitcast i8* %malloccall to i32* ; [#uses=1]
7 %P = malloc i32, i32 100 ; [#uses=1]
88 store i32* %P, i32** @G
99 %GV = load i32** @G ; [#uses=1]
1010 %GVe = getelementptr i32* %GV, i32 40 ; [#uses=1]
1111 store i32 20, i32* %GVe
1212 ret void
1313 }
14
15 declare noalias i8* @malloc(i64)
1614
1715 define i32 @get() {
1816 %GV = load i32** @G ; [#uses=1]
0 ; RUN: opt < %s -globalopt -globaldce -S | not grep malloc
1 target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
1 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
2 target triple = "i686-apple-darwin8"
23
34 @G = internal global i32* null ; [#uses=4]
45
56 define void @init() {
6 %malloccall = tail call i8* @malloc(i64 mul (i64 100, i64 4)) ; [#uses=1]
7 %P = bitcast i8* %malloccall to i32* ; [#uses=1]
7 %P = malloc i32, i32 100 ; [#uses=1]
88 store i32* %P, i32** @G
99 %GV = load i32** @G ; [#uses=1]
1010 %GVe = getelementptr i32* %GV, i32 40 ; [#uses=1]
1111 store i32 20, i32* %GVe
1212 ret void
1313 }
14
15 declare noalias i8* @malloc(i64)
1614
1715 define i32 @get() {
1816 %GV = load i32** @G ; [#uses=1]