llvm.org GIT mirror llvm / 9d0b704
Re-commit r86077 now that r86290 fixes the 179.art and 175.vpr ARM regressions. Here is the original commit message: This commit updates malloc optimizations to operate on malloc calls that have constant int size arguments. Update CreateMalloc so that its callers specify the size to allocate: MallocInst-autoupgrade users use non-TargetData-computed allocation sizes. Optimization uses use TargetData to compute the allocation size. Now that malloc calls can have constant sizes, update isArrayMallocHelper() to use TargetData to determine the size of the malloced type and the size of malloced arrays. Extend getMallocType() to support malloc calls that have non-bitcast uses. Update OptimizeGlobalAddressOfMalloc() to optimize malloc calls that have non-bitcast uses. The bitcast use of a malloc call has to be treated specially here because the uses of the bitcast need to be replaced and the bitcast needs to be erased (just like the malloc call) for OptimizeGlobalAddressOfMalloc() to work correctly. Update PerformHeapAllocSRoA() to optimize malloc calls that have non-bitcast uses. The bitcast use of the malloc is not handled specially here because ReplaceUsesOfMallocWithGlobal replaces through the bitcast use. Update OptimizeOnceStoredGlobal() to not care about the malloc calls' bitcast use. Update all globalopt malloc tests to not rely on autoupgraded-MallocInsts, but instead use explicit malloc calls with correct allocation sizes. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@86311 91177308-0d34-0410-b5e6-96231b3b80d8 Victor Hernandez 9 years ago
19 changed file(s) with 253 addition(s) and 203 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 ptr_arr = CallInst::CreateMalloc(BB, IntPtrTy, IntegerType::getInt8Ty(C),
84 val_mem, NULL, "arr");
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");
8588 BB->getInstList().push_back(cast(ptr_arr));
8689
8790 //call void @llvm.memset.i32(i8 *%arr, i8 0, i32 %d, i32 1)
3232 /// extractMallocCall - Returns the corresponding CallInst if the instruction
3333 /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
3434 /// ignore InvokeInst here.
35 const CallInst* extractMallocCall(const Value *I);
36 CallInst* extractMallocCall(Value *I);
35 const CallInst *extractMallocCall(const Value *I);
36 CallInst *extractMallocCall(Value *I);
3737
3838 /// extractMallocCallFromBitCast - Returns the corresponding CallInst if the
3939 /// instruction is a bitcast of the result of a malloc call.
40 const CallInst* extractMallocCallFromBitCast(const Value *I);
41 CallInst* extractMallocCallFromBitCast(Value *I);
40 const CallInst *extractMallocCallFromBitCast(const Value *I);
41 CallInst *extractMallocCallFromBitCast(Value *I);
4242
4343 /// isArrayMalloc - Returns the corresponding CallInst if the instruction
4444 /// is a call to malloc whose array size can be determined and the array size
4545 /// is not constant 1. Otherwise, return NULL.
46 CallInst* isArrayMalloc(Value *I, const TargetData *TD);
47 const CallInst* isArrayMalloc(const Value *I,
46 CallInst *isArrayMalloc(Value *I, const TargetData *TD);
47 const CallInst *isArrayMalloc(const Value *I,
4848 const TargetData *TD);
4949
5050 /// getMallocType - Returns the PointerType resulting from the malloc call.
51 /// This PointerType is the result type of the call's only bitcast use.
52 /// If there is no unique bitcast use, then return NULL.
53 const PointerType* getMallocType(const CallInst *CI);
51 /// The PointerType depends on the number of bitcast uses of the malloc call:
52 /// 0: PointerType is the malloc calls' return type.
53 /// 1: PointerType is the bitcast's result type.
54 /// >1: Unique PointerType cannot be determined, return NULL.
55 const PointerType *getMallocType(const CallInst *CI);
5456
55 /// getMallocAllocatedType - Returns the Type allocated by malloc call. This
56 /// Type is the result type of the call's only bitcast use. If there is no
57 /// unique bitcast use, then return NULL.
58 const Type* getMallocAllocatedType(const CallInst *CI);
57 /// getMallocAllocatedType - Returns the Type allocated by malloc call.
58 /// The Type depends on the number of bitcast uses of the malloc call:
59 /// 0: PointerType is the malloc calls' return type.
60 /// 1: PointerType is the bitcast's result type.
61 /// >1: Unique PointerType cannot be determined, return NULL.
62 const Type *getMallocAllocatedType(const CallInst *CI);
5963
6064 /// getMallocArraySize - Returns the array size of a malloc call. If the
6165 /// argument passed to malloc is a multiple of the size of the malloced type,
6266 /// then return that multiple. For non-array mallocs, the multiple is
6367 /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
6468 /// determined.
65 Value* getMallocArraySize(CallInst *CI, const TargetData *TD);
69 Value *getMallocArraySize(CallInst *CI, const TargetData *TD);
6670
6771 //===----------------------------------------------------------------------===//
6872 // free Call Utility Functions.
6973 //
7074
7175 /// isFreeCall - Returns true if the the value is a call to the builtin free()
72 bool isFreeCall(const Value* I);
76 bool isFreeCall(const Value *I);
7377
7478 } // End llvm namespace
7579
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 *ArraySize = 0,
901 Value *AllocSize, Value *ArraySize = 0,
902902 const Twine &Name = "");
903903 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
904904 const Type *IntPtrTy, const Type *AllocTy,
905 Value *ArraySize = 0, Function* MallocF = 0,
905 Value *AllocSize, Value *ArraySize = 0,
906 Function* MallocF = 0,
906907 const Twine &Name = "");
907908 /// CreateFree - Generate the IR for a call to the builtin free function.
908909 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"
1920 using namespace llvm;
2021
2122 //===----------------------------------------------------------------------===//
9495 if (!CI)
9596 return NULL;
9697
97 // Type must be known to determine array size.
98 // The size of the malloc's result type must be known to determine array size.
9899 const Type *T = getMallocAllocatedType(CI);
99 if (!T)
100 if (!T || !T->isSized() || !TD)
100101 return NULL;
101102
102103 Value *MallocArg = CI->getOperand(1);
104 const Type *ArgType = MallocArg->getType();
103105 ConstantExpr *CO = dyn_cast(MallocArg);
104106 BinaryOperator *BO = dyn_cast(MallocArg);
105107
106 Constant *ElementSize = ConstantExpr::getSizeOf(T);
107 ElementSize = ConstantExpr::getTruncOrBitCast(ElementSize,
108 MallocArg->getType());
109 Constant *FoldedElementSize =
110 ConstantFoldConstantExpression(cast(ElementSize), TD);
108 unsigned ElementSizeInt = TD->getTypeAllocSize(T);
109 if (const StructType *ST = dyn_cast(T))
110 ElementSizeInt = TD->getStructLayout(ST)->getSizeInBytes();
111 Constant *ElementSize = ConstantInt::get(ArgType, ElementSizeInt);
111112
112113 // First, check if CI is a non-array malloc.
113 if (CO && ((CO == ElementSize) ||
114 (FoldedElementSize && (CO == FoldedElementSize))))
114 if (CO && CO == ElementSize)
115115 // Match CreateMalloc's use of constant 1 array-size for non-array mallocs.
116 return ConstantInt::get(MallocArg->getType(), 1);
116 return ConstantInt::get(ArgType, 1);
117117
118118 // Second, check if CI is an array malloc whose array size can be determined.
119 if (isConstantOne(ElementSize) ||
120 (FoldedElementSize && isConstantOne(FoldedElementSize)))
119 if (isConstantOne(ElementSize))
121120 return MallocArg;
121
122 if (ConstantInt *CInt = dyn_cast(MallocArg))
123 if (CInt->getZExtValue() % ElementSizeInt == 0)
124 return ConstantInt::get(ArgType, CInt->getZExtValue() / ElementSizeInt);
122125
123126 if (!CO && !BO)
124127 return NULL;
126129 Value *Op0 = NULL;
127130 Value *Op1 = NULL;
128131 unsigned Opcode = 0;
129 if (CO && ((CO->getOpcode() == Instruction::Mul) ||
132 if (CO && ((CO->getOpcode() == Instruction::Mul) ||
130133 (CO->getOpcode() == Instruction::Shl))) {
131134 Op0 = CO->getOperand(0);
132135 Op1 = CO->getOperand(1);
133136 Opcode = CO->getOpcode();
134137 }
135 if (BO && ((BO->getOpcode() == Instruction::Mul) ||
138 if (BO && ((BO->getOpcode() == Instruction::Mul) ||
136139 (BO->getOpcode() == Instruction::Shl))) {
137140 Op0 = BO->getOperand(0);
138141 Op1 = BO->getOperand(1);
142145 // Determine array size if malloc's argument is the product of a mul or shl.
143146 if (Op0) {
144147 if (Opcode == Instruction::Mul) {
145 if ((Op1 == ElementSize) ||
146 (FoldedElementSize && (Op1 == FoldedElementSize)))
148 if (Op1 == ElementSize)
147149 // ArraySize * ElementSize
148150 return Op0;
149 if ((Op0 == ElementSize) ||
150 (FoldedElementSize && (Op0 == FoldedElementSize)))
151 if (Op0 == ElementSize)
151152 // ElementSize * ArraySize
152153 return Op1;
153154 }
159160 uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1);
160161 Value *Op1Pow = ConstantInt::get(Op1CI->getContext(),
161162 APInt(Op1Int.getBitWidth(), 0).set(BitToSet));
162 if (Op0 == ElementSize || (FoldedElementSize && Op0 == FoldedElementSize))
163 if (Op0 == ElementSize)
163164 // ArraySize << log2(ElementSize)
164165 return Op1Pow;
165 if (Op1Pow == ElementSize ||
166 (FoldedElementSize && Op1Pow == FoldedElementSize))
166 if (Op1Pow == ElementSize)
167167 // ElementSize << log2(ArraySize)
168168 return Op0;
169169 }
201201 }
202202
203203 /// getMallocType - Returns the PointerType resulting from the malloc call.
204 /// This PointerType is the result type of the call's only bitcast use.
205 /// If there is no unique bitcast use, then return NULL.
204 /// The PointerType depends on the number of bitcast uses of the malloc call:
205 /// 0: PointerType is the calls' return type.
206 /// 1: PointerType is the bitcast's result type.
207 /// >1: Unique PointerType cannot be determined, return NULL.
206208 const PointerType *llvm::getMallocType(const CallInst *CI) {
207209 assert(isMalloc(CI) && "GetMallocType and not malloc call");
208210
209 const BitCastInst *BCI = NULL;
210
211 const PointerType *MallocType = NULL;
212 unsigned NumOfBitCastUses = 0;
213
211214 // Determine if CallInst has a bitcast use.
212215 for (Value::use_const_iterator UI = CI->use_begin(), E = CI->use_end();
213216 UI != E; )
214 if ((BCI = dyn_cast(cast(*UI++))))
215 break;
216
217 // Malloc call has 1 bitcast use and no other uses, so type is the bitcast's
218 // destination type.
219 if (BCI && CI->hasOneUse())
220 return cast(BCI->getDestTy());
217 if (const BitCastInst *BCI = dyn_cast(*UI++)) {
218 MallocType = cast(BCI->getDestTy());
219 NumOfBitCastUses++;
220 }
221
222 // Malloc call has 1 bitcast use, so type is the bitcast's destination type.
223 if (NumOfBitCastUses == 1)
224 return MallocType;
221225
222226 // Malloc call was not bitcast, so type is the malloc function's return type.
223 if (!BCI)
227 if (NumOfBitCastUses == 0)
224228 return cast(CI->getType());
225229
226230 // Type could not be determined.
227231 return NULL;
228232 }
229233
230 /// getMallocAllocatedType - Returns the Type allocated by malloc call. This
231 /// Type is the result type of the call's only bitcast use. If there is no
232 /// unique bitcast use, then return NULL.
234 /// getMallocAllocatedType - Returns the Type allocated by malloc call.
235 /// The Type depends on the number of bitcast uses of the malloc call:
236 /// 0: PointerType is the malloc calls' return type.
237 /// 1: PointerType is the bitcast's result type.
238 /// >1: Unique PointerType cannot be determined, return NULL.
233239 const Type *llvm::getMallocAllocatedType(const CallInst *CI) {
234240 const PointerType *PT = getMallocType(CI);
235241 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);
36213623 if (!MallocF)
36223624 // Prototype malloc as "void *(int32)".
36233625 // This function is renamed as "malloc" in ValidateEndOfModule().
36243626 MallocF = cast(
36253627 M->getOrInsertFunction("", Type::getInt8PtrTy(Context), IntPtrTy, NULL));
3626 Inst = CallInst::CreateMalloc(BB, IntPtrTy, Ty, Size, MallocF);
3628 Inst = CallInst::CreateMalloc(BB, IntPtrTy, Ty, AllocSize, Size, MallocF);
36273629 return false;
36283630 }
36293631
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);
21032105 I = CallInst::CreateMalloc(CurBB, Int32Ty, Ty->getElementType(),
2104 Size, NULL);
2106 AllocSize, Size, NULL);
21052107 InstructionList.push_back(I);
21062108 break;
21072109 }
811811 /// malloc into a global, and any loads of GV as uses of the new global.
812812 static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV,
813813 CallInst *CI,
814 BitCastInst *BCI,
814 const Type *AllocTy,
815815 Value* NElems,
816816 TargetData* TD) {
817 DEBUG(errs() << "PROMOTING MALLOC GLOBAL: " << *GV
818 << " CALL = " << *CI << " BCI = " << *BCI << '\n');
817 DEBUG(errs() << "PROMOTING GLOBAL: " << *GV << " CALL = " << *CI << '\n');
819818
820819 const Type *IntPtrTy = TD->getIntPtrType(GV->getContext());
821820
821 // CI has either 0 or 1 bitcast uses (getMallocType() would otherwise have
822 // returned NULL and we would not be here).
823 BitCastInst *BCI = NULL;
824 for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end(); UI != E; )
825 if ((BCI = dyn_cast(cast(*UI++))))
826 break;
827
822828 ConstantInt *NElements = cast(NElems);
823829 if (NElements->getZExtValue() != 1) {
824830 // If we have an array allocation, transform it to a single element
825831 // allocation to make the code below simpler.
826 Type *NewTy = ArrayType::get(getMallocAllocatedType(CI),
827 NElements->getZExtValue());
828 Value* NewM = CallInst::CreateMalloc(CI, IntPtrTy, NewTy);
829 Instruction* NewMI = cast(NewM);
832 Type *NewTy = ArrayType::get(AllocTy, NElements->getZExtValue());
833 unsigned TypeSize = TD->getTypeAllocSize(NewTy);
834 if (const StructType *ST = dyn_cast(NewTy))
835 TypeSize = TD->getStructLayout(ST)->getSizeInBytes();
836 Instruction *NewCI = CallInst::CreateMalloc(CI, IntPtrTy, NewTy,
837 ConstantInt::get(IntPtrTy, TypeSize));
830838 Value* Indices[2];
831839 Indices[0] = Indices[1] = Constant::getNullValue(IntPtrTy);
832 Value *NewGEP = GetElementPtrInst::Create(NewMI, Indices, Indices + 2,
833 NewMI->getName()+".el0", CI);
834 BCI->replaceAllUsesWith(NewGEP);
835 BCI->eraseFromParent();
840 Value *NewGEP = GetElementPtrInst::Create(NewCI, Indices, Indices + 2,
841 NewCI->getName()+".el0", CI);
842 Value *Cast = new BitCastInst(NewGEP, CI->getType(), "el0", CI);
843 if (BCI) BCI->replaceAllUsesWith(NewGEP);
844 CI->replaceAllUsesWith(Cast);
845 if (BCI) BCI->eraseFromParent();
836846 CI->eraseFromParent();
837 BCI = cast(NewMI);
838 CI = extractMallocCallFromBitCast(NewMI);
847 BCI = dyn_cast(NewCI);
848 CI = BCI ? extractMallocCallFromBitCast(BCI) : cast(NewCI);
839849 }
840850
841851 // Create the new global variable. The contents of the malloc'd memory is
849859 GV,
850860 GV->isThreadLocal());
851861
852 // Anything that used the malloc now uses the global directly.
853 BCI->replaceAllUsesWith(NewGV);
862 // Anything that used the malloc or its bitcast now uses the global directly.
863 if (BCI) BCI->replaceAllUsesWith(NewGV);
864 CI->replaceAllUsesWith(new BitCastInst(NewGV, CI->getType(), "newgv", CI));
854865
855866 Constant *RepValue = NewGV;
856867 if (NewGV->getType() != GV->getType()->getElementType())
918929 GV->getParent()->getGlobalList().insert(GV, InitBool);
919930
920931
921 // Now the GV is dead, nuke it and the malloc.
932 // Now the GV is dead, nuke it and the malloc (both CI and BCI).
922933 GV->eraseFromParent();
923 BCI->eraseFromParent();
934 if (BCI) BCI->eraseFromParent();
924935 CI->eraseFromParent();
925936
926937 // To further other optimizations, loop over all users of NewGV and try to
12541265
12551266 /// PerformHeapAllocSRoA - CI is an allocation of an array of structures. Break
12561267 /// it up into multiple allocations of arrays of the fields.
1257 static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV,
1258 CallInst *CI, BitCastInst* BCI,
1259 Value* NElems,
1260 TargetData *TD) {
1261 DEBUG(errs() << "SROA HEAP ALLOC: " << *GV << " MALLOC CALL = " << *CI
1262 << " BITCAST = " << *BCI << '\n');
1268 static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV, CallInst *CI,
1269 Value* NElems, TargetData *TD) {
1270 DEBUG(errs() << "SROA HEAP ALLOC: " << *GV << " MALLOC = " << *CI << '\n');
12631271 const Type* MAT = getMallocAllocatedType(CI);
12641272 const StructType *STy = cast(MAT);
12651273
12671275 // it into GV). If there are other uses, change them to be uses of
12681276 // the global to simplify later code. This also deletes the store
12691277 // into GV.
1270 ReplaceUsesOfMallocWithGlobal(BCI, GV);
1271
1278 ReplaceUsesOfMallocWithGlobal(CI, GV);
1279
12721280 // Okay, at this point, there are no users of the malloc. Insert N
12731281 // new mallocs at the same place as CI, and N globals.
12741282 std::vector FieldGlobals;
12861294 GV->isThreadLocal());
12871295 FieldGlobals.push_back(NGV);
12881296
1289 Value *NMI = CallInst::CreateMalloc(CI, TD->getIntPtrType(CI->getContext()),
1290 FieldTy, NElems,
1291 BCI->getName() + ".f" + Twine(FieldNo));
1297 unsigned TypeSize = TD->getTypeAllocSize(FieldTy);
1298 if (const StructType* ST = dyn_cast(FieldTy))
1299 TypeSize = TD->getStructLayout(ST)->getSizeInBytes();
1300 const Type* IntPtrTy = TD->getIntPtrType(CI->getContext());
1301 Value *NMI = CallInst::CreateMalloc(CI, IntPtrTy, FieldTy,
1302 ConstantInt::get(IntPtrTy, TypeSize),
1303 NElems,
1304 CI->getName() + ".f" + Twine(FieldNo));
12921305 FieldMallocs.push_back(NMI);
1293 new StoreInst(NMI, NGV, BCI);
1306 new StoreInst(NMI, NGV, CI);
12941307 }
12951308
12961309 // The tricky aspect of this transformation is handling the case when malloc
13071320 // }
13081321 Value *RunningOr = 0;
13091322 for (unsigned i = 0, e = FieldMallocs.size(); i != e; ++i) {
1310 Value *Cond = new ICmpInst(BCI, ICmpInst::ICMP_EQ, FieldMallocs[i],
1311 Constant::getNullValue(FieldMallocs[i]->getType()),
1312 "isnull");
1323 Value *Cond = new ICmpInst(CI, ICmpInst::ICMP_EQ, FieldMallocs[i],
1324 Constant::getNullValue(FieldMallocs[i]->getType()),
1325 "isnull");
13131326 if (!RunningOr)
13141327 RunningOr = Cond; // First seteq
13151328 else
1316 RunningOr = BinaryOperator::CreateOr(RunningOr, Cond, "tmp", BCI);
1329 RunningOr = BinaryOperator::CreateOr(RunningOr, Cond, "tmp", CI);
13171330 }
13181331
13191332 // Split the basic block at the old malloc.
1320 BasicBlock *OrigBB = BCI->getParent();
1321 BasicBlock *ContBB = OrigBB->splitBasicBlock(BCI, "malloc_cont");
1333 BasicBlock *OrigBB = CI->getParent();
1334 BasicBlock *ContBB = OrigBB->splitBasicBlock(CI, "malloc_cont");
13221335
13231336 // Create the block to check the first condition. Put all these blocks at the
13241337 // end of the function as they are unlikely to be executed.
13551368 }
13561369
13571370 BranchInst::Create(ContBB, NullPtrBlock);
1358
1359 // CI and BCI are no longer needed, remove them.
1360 BCI->eraseFromParent();
1371
1372 // CI is no longer needed, remove it.
13611373 CI->eraseFromParent();
13621374
13631375 /// InsertedScalarizedLoads - As we process loads, if we can't immediately
14431455 /// cast of malloc.
14441456 static bool TryToOptimizeStoreOfMallocToGlobal(GlobalVariable *GV,
14451457 CallInst *CI,
1446 BitCastInst *BCI,
1458 const Type *AllocTy,
14471459 Module::global_iterator &GVI,
14481460 TargetData *TD) {
1449 // If we can't figure out the type being malloced, then we can't optimize.
1450 const Type *AllocTy = getMallocAllocatedType(CI);
1451 assert(AllocTy);
1452
14531461 // If this is a malloc of an abstract type, don't touch it.
14541462 if (!AllocTy->isSized())
14551463 return false;
14701478 // for.
14711479 {
14721480 SmallPtrSet PHIs;
1473 if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(BCI, GV, PHIs))
1481 if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(CI, GV, PHIs))
14741482 return false;
14751483 }
14761484
14781486 // transform the program to use global memory instead of malloc'd memory.
14791487 // This eliminates dynamic allocation, avoids an indirection accessing the
14801488 // data, and exposes the resultant global to further GlobalOpt.
1481 Value *NElems = getMallocArraySize(CI, TD);
14821489 // We cannot optimize the malloc if we cannot determine malloc array size.
1483 if (NElems) {
1490 if (Value *NElems = getMallocArraySize(CI, TD)) {
14841491 if (ConstantInt *NElements = dyn_cast(NElems))
14851492 // Restrict this transformation to only working on small allocations
14861493 // (2048 bytes currently), as we don't want to introduce a 16M global or
14871494 // something.
14881495 if (TD &&
14891496 NElements->getZExtValue() * TD->getTypeAllocSize(AllocTy) < 2048) {
1490 GVI = OptimizeGlobalAddressOfMalloc(GV, CI, BCI, NElems, TD);
1497 GVI = OptimizeGlobalAddressOfMalloc(GV, CI, AllocTy, NElems, TD);
14911498 return true;
14921499 }
14931500
15051512 // This the structure has an unreasonable number of fields, leave it
15061513 // alone.
15071514 if (AllocSTy->getNumElements() <= 16 && AllocSTy->getNumElements() != 0 &&
1508 AllGlobalLoadUsesSimpleEnoughForHeapSRA(GV, BCI)) {
1515 AllGlobalLoadUsesSimpleEnoughForHeapSRA(GV, CI)) {
15091516
15101517 // If this is a fixed size array, transform the Malloc to be an alloc of
15111518 // structs. malloc [100 x struct],1 -> malloc struct, 100
15121519 if (const ArrayType *AT =
15131520 dyn_cast(getMallocAllocatedType(CI))) {
1514 Value *NumElements =
1515 ConstantInt::get(Type::getInt32Ty(CI->getContext()),
1516 AT->getNumElements());
1517 Value *NewMI = CallInst::CreateMalloc(CI,
1518 TD->getIntPtrType(CI->getContext()),
1519 AllocSTy, NumElements,
1520 BCI->getName());
1521 Value *Cast = new BitCastInst(NewMI, getMallocType(CI), "tmp", CI);
1522 BCI->replaceAllUsesWith(Cast);
1523 BCI->eraseFromParent();
1521 const Type *IntPtrTy = TD->getIntPtrType(CI->getContext());
1522 unsigned TypeSize = TD->getStructLayout(AllocSTy)->getSizeInBytes();
1523 Value *AllocSize = ConstantInt::get(IntPtrTy, TypeSize);
1524 Value *NumElements = ConstantInt::get(IntPtrTy, AT->getNumElements());
1525 Instruction *Malloc = CallInst::CreateMalloc(CI, IntPtrTy, AllocSTy,
1526 AllocSize, NumElements,
1527 CI->getName());
1528 Instruction *Cast = new BitCastInst(Malloc, CI->getType(), "tmp", CI);
1529 CI->replaceAllUsesWith(Cast);
15241530 CI->eraseFromParent();
1525 BCI = cast(NewMI);
1526 CI = extractMallocCallFromBitCast(NewMI);
1531 CI = dyn_cast(Malloc) ?
1532 extractMallocCallFromBitCast(Malloc):
1533 cast(Malloc);
15271534 }
15281535
1529 GVI = PerformHeapAllocSRoA(GV, CI, BCI, NElems, TD);
1536 GVI = PerformHeapAllocSRoA(GV, CI, getMallocArraySize(CI, TD), TD);
15301537 return true;
15311538 }
15321539 }
15581565 if (OptimizeAwayTrappingUsesOfLoads(GV, SOVC))
15591566 return true;
15601567 } else if (CallInst *CI = extractMallocCall(StoredOnceVal)) {
1561 if (getMallocAllocatedType(CI)) {
1562 BitCastInst* BCI = NULL;
1563 for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end();
1564 UI != E; )
1565 BCI = dyn_cast(cast(*UI++));
1566 if (BCI && TryToOptimizeStoreOfMallocToGlobal(GV, CI, BCI, GVI, TD))
1567 return true;
1568 }
1568 const Type* MallocType = getMallocAllocatedType(CI);
1569 if (MallocType && TryToOptimizeStoreOfMallocToGlobal(GV, CI, MallocType,
1570 GVI, TD))
1571 return true;
15691572 }
15701573 }
15711574
16981698
16991699 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
17001700 const char *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)));
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)));
17051708 }
17061709
17071710 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
17081711 LLVMValueRef Val, const char *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)));
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)));
17131719 }
17141720
17151721 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"
2627
2728 using namespace llvm;
2829
447448 return isa(val) && cast(val)->isOne();
448449 }
449450
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
462451 static Instruction *createMalloc(Instruction *InsertBefore,
463452 BasicBlock *InsertAtEnd, const Type *IntPtrTy,
464 const Type *AllocTy, Value *ArraySize,
465 Function *MallocF, const Twine &NameStr) {
453 const Type *AllocTy, Value *AllocSize,
454 Value *ArraySize, Function *MallocF,
455 const Twine &Name) {
466456 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
467457 "createMalloc needs either InsertBefore or InsertAtEnd");
468458
470460 // bitcast (i8* malloc(typeSize)) to type*
471461 // malloc(type, arraySize) becomes:
472462 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
473 Value *AllocSize = ConstantExpr::getSizeOf(AllocTy);
474 AllocSize = ConstantExpr::getTruncOrBitCast(cast(AllocSize),
475 IntPtrTy);
476 ArraySize = checkArraySize(ArraySize, IntPtrTy);
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 }
477471
478472 if (!IsConstantOne(ArraySize)) {
479473 if (IsConstantOne(AllocSize)) {
512506 Result = MCall;
513507 if (Result->getType() != AllocPtrType)
514508 // Create a cast instruction to convert to the right type...
515 Result = new BitCastInst(MCall, AllocPtrType, NameStr, InsertBefore);
509 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
516510 } else {
517511 MCall = CallInst::Create(MallocF, AllocSize, "malloccall");
518512 Result = MCall;
519513 if (Result->getType() != AllocPtrType) {
520514 InsertAtEnd->getInstList().push_back(MCall);
521515 // Create a cast instruction to convert to the right type...
522 Result = new BitCastInst(MCall, AllocPtrType, NameStr);
516 Result = new BitCastInst(MCall, AllocPtrType, Name);
523517 }
524518 }
525519 MCall->setTailCall();
538532 /// 3. Bitcast the result of the malloc call to the specified type.
539533 Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
540534 const Type *IntPtrTy, const Type *AllocTy,
541 Value *ArraySize, const Twine &Name) {
542 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy,
535 Value *AllocSize, Value *ArraySize,
536 const Twine &Name) {
537 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
543538 ArraySize, NULL, Name);
544539 }
545540
553548 /// responsibility of the caller.
554549 Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
555550 const Type *IntPtrTy, const Type *AllocTy,
556 Value *ArraySize, Function* MallocF,
557 const Twine &Name) {
558 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy,
551 Value *AllocSize, Value *ArraySize,
552 Function *MallocF, const Twine &Name) {
553 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
559554 ArraySize, MallocF, Name);
560555 }
561556
3030 }
3131
3232 declare i32 @bar(i8*)
33 declare i32 @bar2(i64*)
3334
3435 define i32 @foo1(i32 %n) nounwind {
3536 entry:
5960 ret i32 %add16
6061 }
6162
62 define i32 @foo2(i32 %n) nounwind {
63 define i32 @foo2(i64 %n) nounwind {
6364 entry:
64 %call = malloc i8, i32 %n ; [#uses=1]
65 %call = tail call i8* @malloc(i64 %n) ; [#uses=1]
6566 ; CHECK: %call =
6667 ; 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
6773 %call2 = tail call i8* @calloc(i64 2, i64 4) nounwind ; [#uses=1]
6874 ; CHECK: %call2 =
6975 ; CHECK: ==> 8 elements, 8 bytes allocated
7177 ; CHECK: %call4 =
7278 ; CHECK: ==> 16 elements, 16 bytes allocated
7379 %call6 = tail call i32 @bar(i8* %call) nounwind ; [#uses=1]
80 %call7 = tail call i32 @bar2(i64* %call3) nounwind ; [#uses=1]
7481 %call8 = tail call i32 @bar(i8* %call2) nounwind ; [#uses=1]
7582 %call10 = tail call i32 @bar(i8* %call4) nounwind ; [#uses=1]
76 %add = add i32 %call8, %call6 ; [#uses=1]
77 %add11 = add i32 %add, %call10 ; [#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]
7886 ret i32 %add11
7987 }
88
89 declare noalias i8* @malloc(i64) nounwind
8090
8191 declare noalias i8* @calloc(i64, i64) nounwind
8292
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"
12
23 %struct.s_annealing_sched = type { i32, float, float, float, float }
34 %struct.s_bb = type { i32, i32, i32, i32 }
9596 unreachable
9697
9798 bb1.i38: ; preds = %bb
98 %0 = malloc %struct.s_net, i32 undef ; <%struct.s_net*> [#uses=1]
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]
99102 br i1 undef, label %bb.i1.i39, label %my_malloc.exit2.i
100103
101104 bb.i1.i39: ; preds = %bb1.i38
114117 bb7: ; preds = %bb6.preheader
115118 unreachable
116119 }
120
121 declare noalias i8* @malloc(i64)
None ; 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"
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"
42
53 %struct.foo = type { i32, i32 }
64 @X = internal global %struct.foo* null
5 ; CHECK: @X.f0
6 ; CHECK: @X.f1
77
8 define void @bar(i32 %Size) nounwind noinline {
8 define void @bar(i64 %Size) nounwind noinline {
99 entry:
10 %.sub = malloc %struct.foo, i32 %Size
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]
1113 store %struct.foo* %.sub, %struct.foo** @X, align 4
1214 ret void
1315 }
16
17 declare noalias i8* @malloc(i64)
1418
1519 define i32 @baz() nounwind readonly noinline {
1620 bb1.thread:
None ; RUN: opt < %s -globalopt -S | grep {@X.f0}
1 ; RUN: opt < %s -globalopt -S | grep {@X.f1}
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"
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"
53 %struct.foo = type { i32, i32 }
64 @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 %0 = malloc [1000000 x %struct.foo]
11 ;%.sub = bitcast [1000000 x %struct.foo]* %0 to %struct.foo*
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]
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)
1618
1719 define i32 @baz() nounwind readonly noinline {
1820 bb1.thread:
0 ; RUN: opt < %s -globalopt -S | FileCheck %s
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"
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"
42
53 %struct.foo = type { i32, i32 }
64 @X = internal global %struct.foo* null
75 ; CHECK: @X.f0
86 ; CHECK: @X.f1
97
10 define void @bar(i32 %Size) nounwind noinline {
8 define void @bar(i64 %Size) nounwind noinline {
119 entry:
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]
10 %mallocsize = mul i64 8, %Size, ; > [#uses=1]
11 ; CHECK: mul i64 %Size, 4
12 %malloccall = tail call i8* @malloc(i64 %mallocsize) ; [#uses=1]
1513 %.sub = bitcast i8* %malloccall to %struct.foo* ; <%struct.foo*> [#uses=1]
1614 store %struct.foo* %.sub, %struct.foo** @X, align 4
1715 ret void
1816 }
1917
20 declare noalias i8* @malloc(i32)
18 declare noalias i8* @malloc(i64)
2119
2220 define i32 @baz() nounwind readonly noinline {
2321 bb1.thread:
0 ; RUN: opt < %s -globalopt -S | FileCheck %s
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"
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"
42
53 %struct.foo = type { i32, i32 }
64 @X = internal global %struct.foo* null
75 ; CHECK: @X.f0
86 ; CHECK: @X.f1
97
10 define void @bar(i32 %Size) nounwind noinline {
8 define void @bar(i64 %Size) nounwind noinline {
119 entry:
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
10 %mallocsize = shl i64 %Size, 3 ; [#uses=1]
11 %malloccall = tail call i8* @malloc(i64 %mallocsize) ; [#uses=1]
12 ; CHECK: mul i64 %Size, 4
1513 %.sub = bitcast i8* %malloccall to %struct.foo* ; <%struct.foo*> [#uses=1]
1614 store %struct.foo* %.sub, %struct.foo** @X, align 4
1715 ret void
1816 }
1917
20 declare noalias i8* @malloc(i32)
18 declare noalias i8* @malloc(i64)
2119
2220 define i32 @baz() nounwind readonly noinline {
2321 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"
23
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"
54 %struct.foo = type { i32, i32 }
65 @X = internal global %struct.foo* null ; <%struct.foo**> [#uses=2]
76
87 define void @bar(i32 %Size) nounwind noinline {
98 entry:
10 %tmp = malloc [1000000 x %struct.foo] ; <[1000000 x %struct.foo]*> [#uses=1]
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]
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)
1517
1618 define i32 @baz() nounwind readonly noinline {
1719 bb1.thread:
None ; RUN: opt < %s -globalopt -S | not grep global
0 ; RUN: opt < %s -globalopt -S | FileCheck %s
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
45
56 define void @init() {
6 %P = malloc i32 ; *> [#uses=1]
7 %malloccall = tail call i8* @malloc(i64 4) ; *> [#uses=1]
8 %P = bitcast i8* %malloccall to i32* ; [#uses=1]
79 store i32* %P, i32** @G
810 %GV = load i32** @G ; [#uses=1]
911 store i32 0, i32* %GV
1012 ret void
1113 }
1214
15 declare noalias i8* @malloc(i64)
16
1317 define i32 @get() {
1418 %GV = load i32** @G ; [#uses=1]
1519 %V = load i32* %GV ; [#uses=1]
1620 ret i32 %V
21 ; CHECK: ret i32 0
1722 }
1823
0 ; RUN: opt < %s -globalopt -globaldce -S | not grep malloc
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"
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"
32
43 @G = internal global i32* null ; [#uses=3]
54
65 define void @init() {
7 %P = malloc i32, i32 100 ; *> [#uses=1]
6 %malloccall = tail call i8* @malloc(i64 mul (i64 100, i64 4)) ; *> [#uses=1]
7 %P = bitcast i8* %malloccall to i32* ; [#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)
1416
1517 define i32 @get() {
1618 %GV = load i32** @G ; [#uses=1]
0 ; RUN: opt < %s -globalopt -globaldce -S | not grep malloc
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"
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"
32
43 @G = internal global i32* null ; [#uses=4]
54
65 define void @init() {
7 %P = malloc i32, i32 100 ; *> [#uses=1]
6 %malloccall = tail call i8* @malloc(i64 mul (i64 100, i64 4)) ; *> [#uses=1]
7 %P = bitcast i8* %malloccall to i32* ; [#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)
1416
1517 define i32 @get() {
1618 %GV = load i32** @G ; [#uses=1]