llvm.org GIT mirror llvm / 3e0c99a
Revert 82694 "Auto-upgrade malloc instructions to malloc calls." because it causes regressions in the nightly tests. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82784 91177308-0d34-0410-b5e6-96231b3b80d8 Victor Hernandez 10 years ago
16 changed file(s) with 34 addition(s) and 99 deletion(s). Raw diff Collapse all Expand all
2424
2525 #include "BrainF.h"
2626 #include "llvm/Constants.h"
27 #include "llvm/Instructions.h"
2827 #include "llvm/Intrinsics.h"
2928 #include "llvm/ADT/STLExtras.h"
3029 #include
7877
7978 //%arr = malloc i8, i32 %d
8079 ConstantInt *val_mem = ConstantInt::get(C, APInt(32, memtotal));
81 BasicBlock* BB = builder->GetInsertBlock();
82 const Type* IntPtrTy = IntegerType::getInt32Ty(C);
83 ptr_arr = CallInst::CreateMalloc(BB, IntPtrTy, IntegerType::getInt8Ty(C),
84 val_mem, NULL, "arr");
85 BB->getInstList().push_back(cast(ptr_arr));
80 ptr_arr = builder->CreateMalloc(IntegerType::getInt8Ty(C), val_mem, "arr");
8681
8782 //call void @llvm.memset.i32(i8 *%arr, i8 0, i32 %d, i32 1)
8883 {
10431043 const Twine &Name = "");
10441044 static Value *CreateMalloc(BasicBlock *InsertAtEnd, const Type *IntPtrTy,
10451045 const Type *AllocTy, Value *ArraySize = 0,
1046 Function* MallocF = 0, const Twine &Name = "");
1046 const Twine &Name = "");
10471047
10481048 ~CallInst();
10491049
11471147 /// instruction
11481148 const Value *getCalledValue() const { return Op<0>(); }
11491149 Value *getCalledValue() { return Op<0>(); }
1150
1151 /// setCalledFunction - Set the function called
1152 void setCalledFunction(Value* Fn) {
1153 Op<0>() = Fn;
1154 }
11551150
11561151 // Methods for support type inquiry through isa, cast, and dyn_cast:
11571152 static inline bool classof(const CallInst *) { return true; }
601601 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
602602 if (JustWhitespaceNewLine(CurPtr))
603603 return lltok::kw_zeroext;
604 } else if (Len == 6 && !memcmp(StartChar, "malloc", 6)) {
605 // Autoupgrade malloc instruction
606 return lltok::kw_malloc;
607604 }
608605
609606 // Keywords for instructions.
643640 INSTKEYWORD(unwind, Unwind);
644641 INSTKEYWORD(unreachable, Unreachable);
645642
643 INSTKEYWORD(malloc, Malloc);
646644 INSTKEYWORD(alloca, Alloca);
647645 INSTKEYWORD(free, Free);
648646 INSTKEYWORD(load, Load);
6868 /// ValidateEndOfModule - Do final validity and sanity checks at the end of the
6969 /// module.
7070 bool LLParser::ValidateEndOfModule() {
71 // Update auto-upgraded malloc calls from "autoupgrade_malloc" to "malloc".
72 if (MallocF) {
73 MallocF->setName("malloc");
74 // If setName() does not set the name to "malloc", then there is already a
75 // declaration of "malloc". In that case, iterate over all calls to MallocF
76 // and get them to call the declared "malloc" instead.
77 if (MallocF->getName() != "malloc") {
78 Function* realMallocF = M->getFunction("malloc");
79 for (User::use_iterator UI = MallocF->use_begin(), UE= MallocF->use_end();
80 UI != UE; ) {
81 User* user = *UI;
82 UI++;
83 if (CallInst *Call = dyn_cast(user))
84 Call->setCalledFunction(realMallocF);
85 }
86 if (!realMallocF->doesNotAlias(0)) realMallocF->setDoesNotAlias(0);
87 MallocF->eraseFromParent();
88 MallocF = NULL;
89 }
90 }
91
9271 if (!ForwardRefTypes.empty())
9372 return Error(ForwardRefTypes.begin()->second.second,
9473 "use of undefined type named '" +
27962775 case lltok::kw_call: return ParseCall(Inst, PFS, false);
27972776 case lltok::kw_tail: return ParseCall(Inst, PFS, true);
27982777 // Memory.
2799 case lltok::kw_alloca: return ParseAlloc(Inst, PFS);
2800 case lltok::kw_malloc: return ParseAlloc(Inst, PFS, BB, false);
2778 case lltok::kw_alloca:
2779 case lltok::kw_malloc: return ParseAlloc(Inst, PFS, KeywordVal);
28012780 case lltok::kw_free: return ParseFree(Inst, PFS);
28022781 case lltok::kw_load: return ParseLoad(Inst, PFS, false);
28032782 case lltok::kw_store: return ParseStore(Inst, PFS, false);
33063285 }
33073286
33083287 /// ParsePHI
3309 /// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Valueß ']')*
3288 /// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Valueß ']')*
33103289 bool LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
33113290 PATypeHolder Ty(Type::getVoidTy(Context));
33123291 Value *Op0, *Op1;
34513430 /// ::= 'malloc' Type (',' TypeAndValue)? (',' OptionalInfo)?
34523431 /// ::= 'alloca' Type (',' TypeAndValue)? (',' OptionalInfo)?
34533432 bool LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS,
3454 BasicBlock* BB, bool isAlloca) {
3433 unsigned Opc) {
34553434 PATypeHolder Ty(Type::getVoidTy(Context));
34563435 Value *Size = 0;
34573436 LocTy SizeLoc;
34713450 if (Size && Size->getType() != Type::getInt32Ty(Context))
34723451 return Error(SizeLoc, "element count must be i32");
34733452
3474 if (isAlloca)
3453 if (Opc == Instruction::Malloc)
3454 Inst = new MallocInst(Ty, Size, Alignment);
3455 else
34753456 Inst = new AllocaInst(Ty, Size, Alignment);
3476 else {
3477 // Autoupgrade old malloc instruction to malloc call.
3478 const Type* IntPtrTy = Type::getInt32Ty(Context);
3479 const Type* Int8PtrTy = PointerType::getUnqual(Type::getInt8Ty(Context));
3480 if (!MallocF)
3481 // Prototype malloc as "void *autoupgrade_malloc(int32)".
3482 MallocF = cast(M->getOrInsertFunction("autoupgrade_malloc",
3483 Int8PtrTy, IntPtrTy, NULL));
3484 // "autoupgrade_malloc" updated to "malloc" in ValidateEndOfModule().
3485
3486 Inst = cast(CallInst::CreateMalloc(BB, IntPtrTy, Ty,
3487 Size, MallocF));
3488 }
34893457 return false;
34903458 }
34913459
7474 std::map > ForwardRefVals;
7575 std::map > ForwardRefValIDs;
7676 std::vector NumberedVals;
77 Function* MallocF;
7877 public:
7978 LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) :
80 Context(m->getContext()), Lex(F, SM, Err, m->getContext()),
81 M(m), MallocF(NULL) {}
79 Context(m->getContext()), Lex(F, SM, Err, m->getContext()), M(m) {}
8280 bool Run();
8381
8482 LLVMContext& getContext() { return Context; }
277275 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
278276 bool ParsePHI(Instruction *&I, PerFunctionState &PFS);
279277 bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
280 bool ParseAlloc(Instruction *&I, PerFunctionState &PFS,
281 BasicBlock *BB = 0, bool isAlloca = true);
278 bool ParseAlloc(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
282279 bool ParseFree(Instruction *&I, PerFunctionState &PFS);
283280 bool ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
284281 bool ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
20452045 }
20462046
20472047 case bitc::FUNC_CODE_INST_MALLOC: { // MALLOC: [instty, op, align]
2048 // Autoupgrade malloc instruction to malloc call.
20492048 if (Record.size() < 3)
20502049 return Error("Invalid MALLOC record");
20512050 const PointerType *Ty =
20522051 dyn_cast_or_null(getTypeByID(Record[0]));
20532052 Value *Size = getFnValueByID(Record[1], Type::getInt32Ty(Context));
2053 unsigned Align = Record[2];
20542054 if (!Ty || !Size) return Error("Invalid MALLOC record");
2055 if (!CurBB) return Error("Invalid malloc instruction with no BB");
2056 const Type* Int32Ty = IntegerType::getInt32Ty(CurBB->getContext());
2057 if (Size->getType() != Int32Ty)
2058 Size = CastInst::CreateIntegerCast(Size, Int32Ty, false /*ZExt*/,
2059 "", CurBB);
2060 Value* Malloc = CallInst::CreateMalloc(CurBB, Int32Ty,
2061 Ty->getElementType(), Size, NULL);
2062 I = cast(Malloc);
2055 I = new MallocInst(Ty->getElementType(), Size, (1 << Align) >> 1);
20632056 InstructionList.push_back(I);
20642057 break;
20652058 }
16351635
16361636 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
16371637 const char *Name) {
1638 const Type* IntPtrT = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1639 return wrap(CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), IntPtrT,
1640 unwrap(Ty), 0, 0, Twine(Name)));
1638 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
16411639 }
16421640
16431641 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
16441642 LLVMValueRef Val, const char *Name) {
1645 const Type* IntPtrT = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1646 return wrap(CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), IntPtrT,
1647 unwrap(Ty), unwrap(Val), 0, Twine(Name)));
1643 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
16481644 }
16491645
16501646 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
461461
462462 static Value *createMalloc(Instruction *InsertBefore, BasicBlock *InsertAtEnd,
463463 const Type *IntPtrTy, const Type *AllocTy,
464 Value *ArraySize, Function* MallocF,
465 const Twine &NameStr) {
464 Value *ArraySize, const Twine &NameStr) {
466465 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
467466 "createMalloc needs either InsertBefore or InsertAtEnd");
468467
499498 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
500499 Module* M = BB->getParent()->getParent();
501500 const Type *BPTy = PointerType::getUnqual(Type::getInt8Ty(BB->getContext()));
502 if (!MallocF)
503 // prototype malloc as "void *malloc(size_t)"
504 MallocF = cast(M->getOrInsertFunction("malloc", BPTy,
505 IntPtrTy, NULL));
506 if (!MallocF->doesNotAlias(0)) MallocF->setDoesNotAlias(0);
501 // prototype malloc as "void *malloc(size_t)"
502 Constant *MallocF = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
503 if (!cast(MallocF)->doesNotAlias(0))
504 cast(MallocF)->setDoesNotAlias(0);
507505 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
508506 CallInst *MCall = NULL;
509507 Value *MCast = NULL;
532530 Value *CallInst::CreateMalloc(Instruction *InsertBefore, const Type *IntPtrTy,
533531 const Type *AllocTy, Value *ArraySize,
534532 const Twine &Name) {
535 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy,
536 ArraySize, NULL, Name);
533 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, ArraySize, Name);
537534 }
538535
539536 /// CreateMalloc - Generate the IR for a call to malloc:
546543 /// responsibility of the caller.
547544 Value *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, const Type *IntPtrTy,
548545 const Type *AllocTy, Value *ArraySize,
549 Function* MallocF, const Twine &Name) {
550 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy,
551 ArraySize, MallocF, Name);
546 const Twine &Name) {
547 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, ArraySize, Name);
552548 }
553549
554550 //===----------------------------------------------------------------------===//
6262 define i32 @foo2(i32 %n) nounwind {
6363 entry:
6464 %call = malloc i8, i32 %n ; [#uses=1]
65 ; CHECK: %malloccall =
65 ; CHECK: %call =
6666 ; CHECK: ==> %n elements, %n bytes allocated
6767 %call2 = tail call i8* @calloc(i64 2, i64 4) nounwind ; [#uses=1]
6868 ; CHECK: %call2 =
None ; 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"
0 ; RUN: opt < %s -globalopt -S | not grep malloc
31
42 @G = internal global i32* null ; [#uses=3]
53
None ; 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"
0 ; RUN: opt < %s -globalopt -S | not grep malloc
31
42 @G = internal global i32* null ; [#uses=4]
53
0 ; test that casted mallocs get converted to malloc of the right type
11 ; RUN: opt < %s -instcombine -S | \
2 ; RUN: grep bitcast | count 1
2 ; RUN: not grep bitcast
33
44 ; The target datalayout is important for this test case. We have to tell
55 ; instcombine that the ABI alignment for a long is 4-bytes, not 8, otherwise
7878 }
7979
8080 define i32* @test12() {
81 %c = malloc [4 x i8] ; <[4 x i8]*> [#uses=1]
82 %p = bitcast [4 x i8]* %c to i32* ; [#uses=1]
83 ret i32* %p
81 %p = malloc [4 x i8] ; <[4 x i8]*> [#uses=1]
82 %c = bitcast [4 x i8]* %p to i32* ; [#uses=1]
83 ret i32* %c
8484 }
8585 define i8* @test13(i64 %A) {
8686 %c = getelementptr [0 x i8]* bitcast ([32832 x i8]* @inbuf to [0 x i8]*), i64 0, i64 %A ; [#uses=1]
5757 %B = getelementptr i32* %A, i64 2
5858 ret i32* %B
5959 ; CHECK: @test6
60 ; CHECK: getelementptr i8* %malloccall, i64 8
60 ; CHECK: getelementptr [4 x i32]* %M, i64 0, i64 2
6161 }
6262
6363 define i32* @test7(i32* %I, i64 %C, i64 %D) {
0 ; RUN: opt < %s -instcombine -S | grep {ret i32 0}
1 ; RUN: opt < %s -instcombine -globaldce -S | not grep malloc
1 ; RUN: opt < %s -instcombine -S | not grep malloc
22 ; PR1201
33 define i32 @main(i32 %argc, i8** %argv) {
44 %c_19 = alloca i8* ; [#uses=2]
0 ; RUN: opt < %s -instcombine -S | grep {ret i32 0}
1 ; RUN: opt < %s -instcombine -S | not grep malloc
12 ; PR1313
23
34 define i32 @test1(i32 %argc, i8* %argv, i8* %envp) {